Statistics
| Branch: | Tag: | Revision:

hlrc / server / src / Utterance.cpp @ d8feb404

History | View | Annotate | Download (3.257 KB)

1 0c286af0 Simon Schulz
/*
2 f150aab5 Robert Haschke
 * This file is part of hlrc_server
3
 *
4
 * Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
5
 * http://opensource.cit-ec.de/projects/hlrc_server
6
 *
7
 * This file may be licensed under the terms of the
8
 * GNU General Public License Version 3 (the ``GPL''),
9
 * or (at your option) any later version.
10
 *
11
 * Software distributed under the License is distributed
12
 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13
 * express or implied. See the GPL for the specific language
14
 * governing rights and limitations.
15
 *
16
 * You should have received a copy of the GPL along with this
17
 * program. If not, go to http://www.gnu.org/licenses/gpl.html
18
 * or write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
 *
21
 * The development of this software was supported by the
22
 * Excellence Cluster EXC 277 Cognitive Interaction Technology.
23
 * The Excellence Cluster EXC 277 is a grant of the Deutsche
24
 * Forschungsgemeinschaft (DFG) in the context of the German
25
 * Excellence Initiative.
26
 *
27
 */
28 0c286af0 Simon Schulz
29
#include "Utterance.h"
30
using namespace std;
31
32
#define DEBUG_PRINT_PHONEMES 1
33
34 f150aab5 Robert Haschke
Utterance::Utterance() {
35
        playing = false;
36
        text = "UNINITIALISED UTTERANCE";
37 482adb6d Robert Haschke
        audio_data = std::shared_ptr<AudioData>(new AudioData());
38 0c286af0 Simon Schulz
}
39
40 f150aab5 Robert Haschke
void Utterance::set_phoneme_vector(phonemes_vector_t p) {
41
        phonemes_vector = p;
42 0c286af0 Simon Schulz
}
43
44 f150aab5 Robert Haschke
void Utterance::set_text(string t) {
45
        text = t;
46 0c286af0 Simon Schulz
}
47
48 482adb6d Robert Haschke
void Utterance::set_audio_data(std::shared_ptr<AudioData> a) {
49 f150aab5 Robert Haschke
        audio_data = a;
50 0c286af0 Simon Schulz
}
51
52 f150aab5 Robert Haschke
Utterance::~Utterance() {
53 0c286af0 Simon Schulz
}
54
55 f150aab5 Robert Haschke
string Utterance::get_text() {
56
        return text;
57 0c286af0 Simon Schulz
}
58
59 482adb6d Robert Haschke
std::shared_ptr<AudioData> Utterance::get_audio_data() {
60 f150aab5 Robert Haschke
        return audio_data;
61 0c286af0 Simon Schulz
}
62
63 f150aab5 Robert Haschke
void Utterance::start_playback() {
64
        if (phonemes_vector.size() == 0) {
65
                printf("> we have an empty phoneme vector. not playing.\n");
66
                return;
67
        }
68 0c286af0 Simon Schulz
69 f150aab5 Robert Haschke
        // set iterator to start:
70
        phonemes_vector_iterator = phonemes_vector.begin();
71 0c286af0 Simon Schulz
72 f150aab5 Robert Haschke
        // fetch first end time
73
        symbol_duration_pair_t symbol_duration_pair = *phonemes_vector_iterator;
74 90fa4b4e Robert Haschke
        symbol_end_time = std::chrono::steady_clock::now() + std::chrono::milliseconds(symbol_duration_pair.second);
75 0c286af0 Simon Schulz
76 f150aab5 Robert Haschke
        printf("> we have %d pairs\n", (int)phonemes_vector.size());
77 0c286af0 Simon Schulz
78 f150aab5 Robert Haschke
        // go!
79
        playing = true;
80 0c286af0 Simon Schulz
}
81
82 f150aab5 Robert Haschke
string Utterance::currently_active_phoneme() {
83
        // not active -> return
84
        if (!playing)
85
                return "";
86
87
        // empty phoneme list
88
        if (phonemes_vector.size() == 0)
89
                return "";
90
91 90fa4b4e Robert Haschke
        std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now();
92 f150aab5 Robert Haschke
93
        while (now > symbol_end_time) {
94
                // fetch next phoneme:
95
                symbol_duration_pair_t symbol_duration_pair = *phonemes_vector_iterator;
96 90fa4b4e Robert Haschke
                symbol_end_time = symbol_end_time + std::chrono::milliseconds(symbol_duration_pair.second);
97 f150aab5 Robert Haschke
                // printf("time: %d", symbol_duration_pair.second);
98
99
                // do we have to increment the iterator or are we done? (NOTE: -1 is necessary here!)
100
                if (phonemes_vector_iterator < phonemes_vector.end() - 1) {
101
                        phonemes_vector_iterator++;
102
                }
103
                else {
104
                        // reached end, stop playback
105
                        playing = false;
106
                        return "";
107
                        if (DEBUG_PRINT_PHONEMES)
108
                                printf("\n");
109
                }
110
        }
111
112
        // fetch active symbol:
113
        string symbol = ((symbol_duration_pair_t)*phonemes_vector_iterator).first;
114
        // printf("> active symbol = %s\n",symbol.c_str());
115 23fe067c Robert Haschke
        if (DEBUG_PRINT_PHONEMES) {
116 f150aab5 Robert Haschke
                printf("%s ", symbol.c_str());
117 23fe067c Robert Haschke
                fflush(stdout);
118
        }
119 f150aab5 Robert Haschke
120
        return symbol;
121 0c286af0 Simon Schulz
}
122
123 f150aab5 Robert Haschke
bool Utterance::is_playing() {
124
        return playing;
125 0c286af0 Simon Schulz
}