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.
28 #include <simgear/compiler.h>
29 #include <Main/fg_props.hxx>
30 #include <Main/globals.hxx>
33 #define VOICE "/sim/sound/voices"
38 FGVoiceMgr::FGVoiceMgr() :
39 _host(fgGetString(VOICE "/host", "localhost")),
40 _port(fgGetString(VOICE "/port", "1314")),
41 _enabled(fgGetBool(VOICE "/enabled", false)),
42 _pausedNode(fgGetNode("/sim/sound/pause", true))
44 #if defined(ENABLE_THREADS)
47 _thread = new FGVoiceThread(this);
52 FGVoiceMgr::~FGVoiceMgr()
54 #if defined(ENABLE_THREADS)
63 void FGVoiceMgr::init()
68 SGPropertyNode *base = fgGetNode(VOICE, true);
69 vector<SGPropertyNode_ptr> voices = base->getChildren("voice");
70 for (unsigned int i = 0; i < voices.size(); i++)
71 _voices.push_back(new FGVoice(this, voices[i]));
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(10000);
113 _connected = _sock->open(SG_IO_OUT);
115 SG_LOG(SG_IO, SG_ALERT, "VOICE: no connection to `"
116 << host << ':' << port << '\'');
121 _sock->writestring("(SayText \"\")\015\012");
123 int len = _sock->read(buf, 3);
124 if (len != 3 || buf[0] != 'L' || buf[1] != 'P') {
125 SG_LOG(SG_IO, SG_ALERT, "VOICE: something is listening to "
126 << host << ':' << port << "', but it doesn't seem "
132 SG_LOG(SG_IO, SG_BULK, "VOICE: connection to Festival server on `"
133 << host << ':' << port << "' established");
135 setVolume(_volume = _volumeNode->getDoubleValue());
136 setPitch(_pitch = _pitchNode->getDoubleValue());
137 setSpeed(_speed = _speedNode->getDoubleValue());
140 string preamble = node->getStringValue("preamble", "");
141 if (!preamble.empty())
142 pushMessage(preamble);
144 node->getNode("text", true)->addChangeListener(new FGVoiceListener(this));
148 FGVoiceMgr::FGVoice::~FGVoice()
155 void FGVoiceMgr::FGVoice::pushMessage(string m)
157 _msg.push(m + "\015\012");
158 #if defined(ENABLE_THREADS)
159 _mgr->_thread->wake_up();
164 bool FGVoiceMgr::FGVoice::speak(void)
167 // cerr << "<nothing to say>" << endl;
171 const string s = _msg.front();
173 // cerr << "POP " << s;
174 _sock->writestring(s.c_str());
175 return !_msg.empty();
179 void FGVoiceMgr::FGVoice::update(void)
181 if (_connected && _festival) {
183 d = _volumeNode->getDoubleValue();
185 setVolume(_volume = d);
186 d = _pitchNode->getDoubleValue();
188 setPitch(_pitch = d);
189 d = _speedNode->getDoubleValue();
191 setSpeed(_speed = d);
196 void FGVoiceMgr::FGVoice::setVolume(double d)
198 std::ostringstream s;
199 s << "(set! default_after_synth_hooks (list (lambda (utt)"
200 "(utt.wave.rescale utt " << d << " t))))";
201 pushMessage(s.str());
205 void FGVoiceMgr::FGVoice::setPitch(double d)
207 std::ostringstream s;
208 s << "(set! int_lr_params '((target_f0_mean " << d <<
209 ")(target_f0_std 14)(model_f0_mean 170)"
210 "(model_f0_std 34)))";
211 pushMessage(s.str());
215 void FGVoiceMgr::FGVoice::setSpeed(double d)
217 std::ostringstream s;
218 s << "(Parameter.set 'Duration_Stretch " << d << ')';
219 pushMessage(s.str());
227 #if defined(ENABLE_THREADS)
228 void FGVoiceMgr::FGVoiceThread::run(void)
232 for (unsigned int i = 0; i < _mgr->_voices.size(); i++)
233 busy |= _mgr->_voices[i]->speak();
246 void FGVoiceMgr::FGVoice::FGVoiceListener::valueChanged(SGPropertyNode *node)
248 if (_voice->_mgr->_paused)
251 const string s = node->getStringValue();
252 // cerr << "PUSH " << s << endl;
255 for (unsigned int i = 0; i < s.size(); i++) {
259 else if (c == '"' || c == '\\')
261 else if (c == '|' || c == '_')
262 m += ' '; // don't say "vertical bar" or "underscore"
268 if (_voice->_festival)
269 m = string("(SayText \"") + m + "\")";
271 _voice->pushMessage(m);