1 // speech synthesis interface subsystem
3 // Written by Melchior FRANZ, started February 2006.
5 // Copyright (C) 2006 Melchior FRANZ - mfranz@aon.at
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.
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.
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.
23 #include <Main/globals.hxx>
25 #include <simgear/compiler.h>
26 #include <Main/fg_props.hxx>
29 #define VOICE "/sim/sound/voices"
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))
40 #if defined(ENABLE_THREADS)
43 _thread = new FGVoiceThread(this);
48 FGVoiceMgr::~FGVoiceMgr()
50 #if defined(ENABLE_THREADS)
59 void FGVoiceMgr::init()
64 SGPropertyNode *base = fgGetNode(VOICE, true);
65 vector<SGPropertyNode_ptr> voices = base->getChildren("voice");
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);
73 #if defined(ENABLE_THREADS)
79 void FGVoiceMgr::update(double)
84 _paused = _pausedNode->getBoolValue();
85 for (unsigned int i = 0; i < _voices.size(); i++) {
87 #if !defined(ENABLE_THREADS)
99 FGVoiceMgr::FGVoice::FGVoice(FGVoiceMgr *mgr, const SGPropertyNode_ptr node) :
100 _volumeNode(node->getNode("volume", true)),
101 _pitchNode(node->getNode("pitch", true)),
102 _speedNode(node->getNode("speed", true)),
103 _festival(node->getBoolValue("festival", true)),
106 SG_LOG(SG_IO, SG_INFO, "VOICE: adding `" << node->getStringValue("desc", "<unnamed>")
108 const string &host = _mgr->_host;
109 const string &port = _mgr->_port;
111 _sock = new SGSocket(host, port, "tcp");
112 _sock->set_timeout(6000);
113 if (!_sock->open(SG_IO_OUT))
114 throw string("no connection to `") + host + ':' + port + '\'';
117 _sock->writestring("(SayText \"\")\015\012");
119 int len = _sock->read(buf, 3);
120 if (len != 3 || buf[0] != 'L' || buf[1] != 'P')
121 throw string("unexpected or no response from `") + host + ':' + port
122 + "'. Either it's not\n Festival listening,"
123 " or Festival couldn't open a sound device.";
125 SG_LOG(SG_IO, SG_INFO, "VOICE: connection to Festival server on `"
126 << host << ':' << port << "' established");
128 setVolume(_volume = _volumeNode->getDoubleValue());
129 setPitch(_pitch = _pitchNode->getDoubleValue());
130 setSpeed(_speed = _speedNode->getDoubleValue());
133 string preamble = node->getStringValue("preamble", "");
134 if (!preamble.empty())
135 pushMessage(preamble);
137 node->getNode("text", true)->addChangeListener(new FGVoiceListener(this));
141 FGVoiceMgr::FGVoice::~FGVoice()
148 void FGVoiceMgr::FGVoice::pushMessage(string m)
150 _msg.push(m + "\015\012");
151 #if defined(ENABLE_THREADS)
152 _mgr->_thread->wake_up();
157 bool FGVoiceMgr::FGVoice::speak(void)
162 const string s = _msg.front();
164 _sock->writestring(s.c_str());
165 return !_msg.empty();
169 void FGVoiceMgr::FGVoice::update(void)
173 d = _volumeNode->getDoubleValue();
175 setVolume(_volume = d);
176 d = _pitchNode->getDoubleValue();
178 setPitch(_pitch = d);
179 d = _speedNode->getDoubleValue();
181 setSpeed(_speed = d);
186 void FGVoiceMgr::FGVoice::setVolume(double d)
188 std::ostringstream s;
189 s << "(set! default_after_synth_hooks (list (lambda (utt)"
190 "(utt.wave.rescale utt " << d << " t))))";
191 pushMessage(s.str());
195 void FGVoiceMgr::FGVoice::setPitch(double d)
197 std::ostringstream s;
198 s << "(set! int_lr_params '((target_f0_mean " << d <<
199 ")(target_f0_std 14)(model_f0_mean 170)"
200 "(model_f0_std 34)))";
201 pushMessage(s.str());
205 void FGVoiceMgr::FGVoice::setSpeed(double d)
207 std::ostringstream s;
208 s << "(Parameter.set 'Duration_Stretch " << d << ')';
209 pushMessage(s.str());
217 #if defined(ENABLE_THREADS)
218 void FGVoiceMgr::FGVoiceThread::run(void)
222 for (unsigned int i = 0; i < _mgr->_voices.size(); i++)
223 busy |= _mgr->_voices[i]->speak();
236 void FGVoiceMgr::FGVoice::FGVoiceListener::valueChanged(SGPropertyNode *node)
238 if (_voice->_mgr->_paused)
241 const string s = node->getStringValue();
242 //cerr << "\033[31;1mBEFORE [" << s << "]\033[m" << endl;
245 for (unsigned int i = 0; i < s.size(); i++) {
247 if (c == '\n' || c == '\r' || c == '\t')
249 else if (!isprint(c))
251 else if (c == '\\' || c == '"')
253 else if (c == '|' || c == '_')
254 m += ' '; // don't say "vertical bar" or "underscore"
266 //cerr << "\033[31;1mAFTER [" << m << "]\033[m" << endl;
267 if (_voice->_festival)
268 m = string("(SayText \"") + m + "\")";
270 _voice->pushMessage(m);