]> git.mxchange.org Git - flightgear.git/blob - src/Sound/voice.cxx
Don't restore initial screen geometry because there is nothing in fg_os* to resize...
[flightgear.git] / src / Sound / voice.cxx
1 // speech synthesis interface subsystem
2 //
3 // Written by Melchior FRANZ, started February 2006.
4 //
5 // Copyright (C) 2006  Melchior FRANZ - mfranz@aon.at
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #include <Main/globals.hxx>
24 #include <sstream>
25 #include <simgear/compiler.h>
26 #include <Main/fg_props.hxx>
27 #include "voice.hxx"
28
29 #define VOICE "/sim/sound/voices"
30
31
32 /// MANAGER ///
33
34 FGVoiceMgr::FGVoiceMgr() :
35         _host(fgGetString(VOICE "/host", "localhost")),
36         _port(fgGetString(VOICE "/port", "1314")),
37         _enabled(fgGetBool(VOICE "/enabled", false)),
38         _pausedNode(fgGetNode("/sim/sound/pause", true))
39 {
40 #if defined(ENABLE_THREADS)
41         if (!_enabled)
42                 return;
43         _thread = new FGVoiceThread(this);
44 #endif
45 }
46
47
48 FGVoiceMgr::~FGVoiceMgr()
49 {
50 #if defined(ENABLE_THREADS)
51         if (!_enabled)
52                 return;
53         _thread->cancel();
54         _thread->join();
55 #endif
56 }
57
58
59 void FGVoiceMgr::init()
60 {
61         if (!_enabled)
62                 return;
63
64         SGPropertyNode *base = fgGetNode(VOICE, true);
65         vector<SGPropertyNode_ptr> voices = base->getChildren("voice");
66         for (unsigned int i = 0; i < voices.size(); i++)
67                 _voices.push_back(new FGVoice(this, voices[i]));
68
69 #if defined(ENABLE_THREADS)
70         _thread->start(1);
71 #endif
72 }
73
74
75 void FGVoiceMgr::update(double)
76 {
77         if (!_enabled)
78                 return;
79
80         _paused = _pausedNode->getBoolValue();
81         for (unsigned int i = 0; i < _voices.size(); i++) {
82                 _voices[i]->update();
83 #if !defined(ENABLE_THREADS)
84                 _voices[i]->speak();
85 #endif
86         }
87
88 }
89
90
91
92
93 /// VOICE ///
94
95 FGVoiceMgr::FGVoice::FGVoice(FGVoiceMgr *mgr, const SGPropertyNode_ptr node) :
96         _volumeNode(node->getNode("volume", true)),
97         _pitchNode(node->getNode("pitch", true)),
98         _speedNode(node->getNode("speed", true)),
99         _festival(node->getBoolValue("festival", true)),
100         _mgr(mgr)
101 {
102         SG_LOG(SG_IO, SG_INFO, "VOICE: adding `" << node->getStringValue("desc", "<unnamed>")
103                         << "' voice");
104         const string &host = _mgr->_host;
105         const string &port = _mgr->_port;
106
107         _sock = new SGSocket(host, port, "tcp");
108         _sock->set_timeout(10000);
109         _connected = _sock->open(SG_IO_OUT);
110         if (!_connected) {
111                 SG_LOG(SG_IO, SG_ALERT, "VOICE: no connection to `"
112                                 << host << ':' << port << '\'');
113                 return;
114         }
115
116         if (_festival) {
117                 _sock->writestring("(SayText \"\")\015\012");
118                 char buf[4];
119                 int len = _sock->read(buf, 3);
120                 if (len != 3 || buf[0] != 'L' || buf[1] != 'P') {
121                         SG_LOG(SG_IO, SG_ALERT, "VOICE: unexpected or no response from `"
122                                         << host << ':' << port << "'. Either it's not " << endl
123                                         << "       Festival listening, or Festival couldn't open a "
124                                         "sound device.");
125                         _connected = false;
126                         return;
127                 }
128
129                 SG_LOG(SG_IO, SG_BULK, "VOICE: connection to Festival server on `"
130                                 << host << ':' << port << "' established");
131
132                 setVolume(_volume = _volumeNode->getDoubleValue());
133                 setPitch(_pitch = _pitchNode->getDoubleValue());
134                 setSpeed(_speed = _speedNode->getDoubleValue());
135         }
136
137         string preamble = node->getStringValue("preamble", "");
138         if (!preamble.empty())
139                 pushMessage(preamble);
140
141         node->getNode("text", true)->addChangeListener(new FGVoiceListener(this));
142 }
143
144
145 FGVoiceMgr::FGVoice::~FGVoice()
146 {
147         _sock->close();
148         delete _sock;
149 }
150
151
152 void FGVoiceMgr::FGVoice::pushMessage(string m)
153 {
154         _msg.push(m + "\015\012");
155 #if defined(ENABLE_THREADS)
156         _mgr->_thread->wake_up();
157 #endif
158 }
159
160
161 bool FGVoiceMgr::FGVoice::speak(void)
162 {
163         if (_msg.empty()) {
164 //              cerr << "<nothing to say>" << endl;
165                 return false;
166         }
167
168         const string s = _msg.front();
169         _msg.pop();
170 //      cerr << "POP " << s;
171         _sock->writestring(s.c_str());
172         return !_msg.empty();
173 }
174
175
176 void FGVoiceMgr::FGVoice::update(void)
177 {
178         if (_connected && _festival) {
179                 double d;
180                 d = _volumeNode->getDoubleValue();
181                 if (d != _volume)
182                         setVolume(_volume = d);
183                 d = _pitchNode->getDoubleValue();
184                 if (d != _pitch)
185                         setPitch(_pitch = d);
186                 d = _speedNode->getDoubleValue();
187                 if (d != _speed)
188                         setSpeed(_speed = d);
189         }
190 }
191
192
193 void FGVoiceMgr::FGVoice::setVolume(double d)
194 {
195         std::ostringstream s;
196         s << "(set! default_after_synth_hooks (list (lambda (utt)"
197                         "(utt.wave.rescale utt " << d << " t))))";
198         pushMessage(s.str());
199 }
200
201
202 void FGVoiceMgr::FGVoice::setPitch(double d)
203 {
204         std::ostringstream s;
205         s << "(set! int_lr_params '((target_f0_mean " << d <<
206                         ")(target_f0_std 14)(model_f0_mean 170)"
207                         "(model_f0_std 34)))";
208         pushMessage(s.str());
209 }
210
211
212 void FGVoiceMgr::FGVoice::setSpeed(double d)
213 {
214         std::ostringstream s;
215         s << "(Parameter.set 'Duration_Stretch " << d << ')';
216         pushMessage(s.str());
217 }
218
219
220
221
222 /// THREAD ///
223
224 #if defined(ENABLE_THREADS)
225 void FGVoiceMgr::FGVoiceThread::run(void)
226 {
227         while (1) {
228                 bool busy = false;
229                 for (unsigned int i = 0; i < _mgr->_voices.size(); i++)
230                         busy |= _mgr->_voices[i]->speak();
231
232                 if (!busy)
233                         wait_for_jobs();
234         }
235 }
236 #endif
237
238
239
240
241 /// LISTENER ///
242
243 void FGVoiceMgr::FGVoice::FGVoiceListener::valueChanged(SGPropertyNode *node)
244 {
245         if (_voice->_mgr->_paused)
246                 return;
247
248         const string s = node->getStringValue();
249 //      cerr << "PUSH " << s << endl;
250
251         string m;
252         for (unsigned int i = 0; i < s.size(); i++) {
253                 char c = s[i];
254                 if (!isprint(c))
255                         continue;
256                 else if (c == '"' || c == '\\')
257                         m += '\\' + c;
258                 else if (c == '|' || c == '_')
259                         m += ' ';       // don't say "vertical bar" or "underscore"
260                 else if (c == '&')
261                         m += " and ";
262                 else
263                         m += c;
264         }
265         if (_voice->_festival)
266                 m = string("(SayText \"") + m + "\")";
267
268         _voice->pushMessage(m);
269 }
270
271