]> git.mxchange.org Git - flightgear.git/blob - src/Sound/voice.cxx
Remove the StaticLeaf visitor methods.
[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         try {
67                 for (unsigned int i = 0; i < voices.size(); i++)
68                         _voices.push_back(new FGVoice(this, voices[i]));
69         } catch (const string& s) {
70                 SG_LOG(SG_IO, SG_ALERT, "VOICE: " << s);
71         }
72
73 #if defined(ENABLE_THREADS)
74         _thread->setProcessorAffinity(1);
75         _thread->start();
76 #endif
77 }
78
79
80 void FGVoiceMgr::update(double)
81 {
82         if (!_enabled)
83                 return;
84
85         _paused = _pausedNode->getBoolValue();
86         for (unsigned int i = 0; i < _voices.size(); i++) {
87                 _voices[i]->update();
88 #if !defined(ENABLE_THREADS)
89                 _voices[i]->speak();
90 #endif
91         }
92
93 }
94
95
96
97
98 /// VOICE ///
99
100 FGVoiceMgr::FGVoice::FGVoice(FGVoiceMgr *mgr, const SGPropertyNode_ptr node) :
101         _volumeNode(node->getNode("volume", true)),
102         _pitchNode(node->getNode("pitch", true)),
103         _speedNode(node->getNode("speed", true)),
104         _festival(node->getBoolValue("festival", true)),
105         _mgr(mgr)
106 {
107         SG_LOG(SG_IO, SG_INFO, "VOICE: adding `" << node->getStringValue("desc", "<unnamed>")
108                         << "' voice");
109         const string &host = _mgr->_host;
110         const string &port = _mgr->_port;
111
112         _sock = new SGSocket(host, port, "tcp");
113         _sock->set_timeout(6000);
114         if (!_sock->open(SG_IO_OUT))
115                 throw string("no connection to `") + host + ':' + port + '\'';
116
117         if (_festival) {
118                 _sock->writestring("(SayText \"\")\015\012");
119                 char buf[4];
120                 int len = _sock->read(buf, 3);
121                 if (len != 3 || buf[0] != 'L' || buf[1] != 'P')
122                         throw string("unexpected or no response from `") + host + ':' + port
123                                         + "'. Either it's not\n       Festival listening,"
124                                         " or Festival couldn't open a sound device.";
125
126                 SG_LOG(SG_IO, SG_INFO, "VOICE: connection to Festival server on `"
127                                 << host << ':' << port << "' established");
128
129                 setVolume(_volume = _volumeNode->getDoubleValue());
130                 setPitch(_pitch = _pitchNode->getDoubleValue());
131                 setSpeed(_speed = _speedNode->getDoubleValue());
132         }
133
134         string preamble = node->getStringValue("preamble", "");
135         if (!preamble.empty())
136                 pushMessage(preamble);
137
138         node->getNode("text", true)->addChangeListener(new FGVoiceListener(this));
139 }
140
141
142 FGVoiceMgr::FGVoice::~FGVoice()
143 {
144         _sock->close();
145         delete _sock;
146 }
147
148
149 void FGVoiceMgr::FGVoice::pushMessage(string m)
150 {
151         _msg.push(m + "\015\012");
152 #if defined(ENABLE_THREADS)
153         _mgr->_thread->wake_up();
154 #endif
155 }
156
157
158 bool FGVoiceMgr::FGVoice::speak(void)
159 {
160         if (_msg.empty())
161                 return false;
162
163         const string s = _msg.front();
164         _msg.pop();
165         _sock->writestring(s.c_str());
166         return !_msg.empty();
167 }
168
169
170 void FGVoiceMgr::FGVoice::update(void)
171 {
172         if (_festival) {
173                 double d;
174                 d = _volumeNode->getDoubleValue();
175                 if (d != _volume)
176                         setVolume(_volume = d);
177                 d = _pitchNode->getDoubleValue();
178                 if (d != _pitch)
179                         setPitch(_pitch = d);
180                 d = _speedNode->getDoubleValue();
181                 if (d != _speed)
182                         setSpeed(_speed = d);
183         }
184 }
185
186
187 void FGVoiceMgr::FGVoice::setVolume(double d)
188 {
189         std::ostringstream s;
190         s << "(set! default_after_synth_hooks (list (lambda (utt)"
191                         "(utt.wave.rescale utt " << d << " t))))";
192         pushMessage(s.str());
193 }
194
195
196 void FGVoiceMgr::FGVoice::setPitch(double d)
197 {
198         std::ostringstream s;
199         s << "(set! int_lr_params '((target_f0_mean " << d <<
200                         ")(target_f0_std 14)(model_f0_mean 170)"
201                         "(model_f0_std 34)))";
202         pushMessage(s.str());
203 }
204
205
206 void FGVoiceMgr::FGVoice::setSpeed(double d)
207 {
208         std::ostringstream s;
209         s << "(Parameter.set 'Duration_Stretch " << d << ')';
210         pushMessage(s.str());
211 }
212
213
214
215
216 /// THREAD ///
217
218 #if defined(ENABLE_THREADS)
219 void FGVoiceMgr::FGVoiceThread::run(void)
220 {
221         while (1) {
222                 bool busy = false;
223                 for (unsigned int i = 0; i < _mgr->_voices.size(); i++)
224                         busy |= _mgr->_voices[i]->speak();
225
226                 if (!busy)
227                         wait_for_jobs();
228         }
229 }
230 #endif
231
232
233
234
235 /// LISTENER ///
236
237 void FGVoiceMgr::FGVoice::FGVoiceListener::valueChanged(SGPropertyNode *node)
238 {
239         if (_voice->_mgr->_paused)
240                 return;
241
242         const string s = node->getStringValue();
243         //cerr << "\033[31;1mBEFORE [" << s << "]\033[m" << endl;
244
245         string m;
246         for (unsigned int i = 0; i < s.size(); i++) {
247                 char c = s[i];
248                 if (c == '\n' || c == '\r' || c == '\t')
249                         m += ' ';
250                 else if (!isprint(c))
251                         continue;
252                 else if (c == '\\' || c == '"')
253                         m += '\\', m += c;
254                 else if (c == '|' || c == '_')
255                         m += ' ';       // don't say "vertical bar" or "underscore"
256                 else if (c == '&')
257                         m += " and ";
258                 else if (c == '{') {
259                         while (i < s.size())
260                                 if (s[++i] == '|')
261                                         break;
262                 } else if (c == '}')
263                         ;
264                 else
265                         m += c;
266         }
267         //cerr << "\033[31;1mAFTER [" << m << "]\033[m" << endl;
268         if (_voice->_festival)
269                 m = string("(SayText \"") + m + "\")";
270
271         _voice->pushMessage(m);
272 }
273
274