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");
66 for (unsigned int i = 0; i < voices.size(); i++)
67 _voices.push_back(new FGVoice(this, voices[i]));
69 #if defined(ENABLE_THREADS)
75 void FGVoiceMgr::update(double)
80 _paused = _pausedNode->getBoolValue();
81 for (unsigned int i = 0; i < _voices.size(); i++) {
83 #if !defined(ENABLE_THREADS)
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)),
102 SG_LOG(SG_IO, SG_INFO, "VOICE: adding `" << node->getStringValue("desc", "<unnamed>")
104 const string &host = _mgr->_host;
105 const string &port = _mgr->_port;
107 _sock = new SGSocket(host, port, "tcp");
108 _sock->set_timeout(10000);
109 _connected = _sock->open(SG_IO_OUT);
111 SG_LOG(SG_IO, SG_ALERT, "VOICE: no connection to `"
112 << 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 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 "
129 SG_LOG(SG_IO, SG_BULK, "VOICE: connection to Festival server on `"
130 << host << ':' << port << "' established");
132 setVolume(_volume = _volumeNode->getDoubleValue());
133 setPitch(_pitch = _pitchNode->getDoubleValue());
134 setSpeed(_speed = _speedNode->getDoubleValue());
137 string preamble = node->getStringValue("preamble", "");
138 if (!preamble.empty())
139 pushMessage(preamble);
141 node->getNode("text", true)->addChangeListener(new FGVoiceListener(this));
145 FGVoiceMgr::FGVoice::~FGVoice()
152 void FGVoiceMgr::FGVoice::pushMessage(string m)
154 _msg.push(m + "\015\012");
155 #if defined(ENABLE_THREADS)
156 _mgr->_thread->wake_up();
161 bool FGVoiceMgr::FGVoice::speak(void)
164 // cerr << "<nothing to say>" << endl;
168 const string s = _msg.front();
170 // cerr << "POP " << s;
171 _sock->writestring(s.c_str());
172 return !_msg.empty();
176 void FGVoiceMgr::FGVoice::update(void)
178 if (_connected && _festival) {
180 d = _volumeNode->getDoubleValue();
182 setVolume(_volume = d);
183 d = _pitchNode->getDoubleValue();
185 setPitch(_pitch = d);
186 d = _speedNode->getDoubleValue();
188 setSpeed(_speed = d);
193 void FGVoiceMgr::FGVoice::setVolume(double d)
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());
202 void FGVoiceMgr::FGVoice::setPitch(double d)
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());
212 void FGVoiceMgr::FGVoice::setSpeed(double d)
214 std::ostringstream s;
215 s << "(Parameter.set 'Duration_Stretch " << d << ')';
216 pushMessage(s.str());
224 #if defined(ENABLE_THREADS)
225 void FGVoiceMgr::FGVoiceThread::run(void)
229 for (unsigned int i = 0; i < _mgr->_voices.size(); i++)
230 busy |= _mgr->_voices[i]->speak();
243 void FGVoiceMgr::FGVoice::FGVoiceListener::valueChanged(SGPropertyNode *node)
245 if (_voice->_mgr->_paused)
248 const string s = node->getStringValue();
249 // cerr << "PUSH " << s << endl;
252 for (unsigned int i = 0; i < s.size(); i++) {
256 else if (c == '"' || c == '\\')
258 else if (c == '|' || c == '_')
259 m += ' '; // don't say "vertical bar" or "underscore"
265 if (_voice->_festival)
266 m = string("(SayText \"") + m + "\")";
268 _voice->pushMessage(m);