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/working", true))
40 #if defined(ENABLE_THREADS)
43 _thread = new FGVoiceThread(this);
48 FGVoiceMgr::~FGVoiceMgr()
50 #if defined(ENABLE_THREADS)
60 void FGVoiceMgr::init()
65 SGPropertyNode *base = fgGetNode(VOICE, true);
66 vector<SGPropertyNode_ptr> voices = base->getChildren("voice");
68 for (unsigned int i = 0; i < voices.size(); i++)
69 _voices.push_back(new FGVoice(this, voices[i]));
70 } catch (const string& s) {
71 SG_LOG(SG_IO, SG_ALERT, "VOICE: " << s);
74 #if defined(ENABLE_THREADS)
75 _thread->setProcessorAffinity(1);
81 void FGVoiceMgr::update(double)
86 _paused = !_pausedNode->getBoolValue();
87 for (unsigned int i = 0; i < _voices.size(); i++) {
89 #if !defined(ENABLE_THREADS)
101 FGVoiceMgr::FGVoice::FGVoice(FGVoiceMgr *mgr, const SGPropertyNode_ptr node) :
102 _volumeNode(node->getNode("volume", true)),
103 _pitchNode(node->getNode("pitch", true)),
104 _speedNode(node->getNode("speed", true)),
105 _festival(node->getBoolValue("festival", true)),
108 SG_LOG(SG_IO, SG_INFO, "VOICE: adding `" << node->getStringValue("desc", "<unnamed>")
110 const string &host = _mgr->_host;
111 const string &port = _mgr->_port;
113 _sock = new SGSocket(host, port, "tcp");
114 _sock->set_timeout(6000);
115 if (!_sock->open(SG_IO_OUT))
116 throw string("no connection to `") + host + ':' + port + '\'';
119 _sock->writestring("(SayText \"\")\015\012");
121 int len = _sock->read(buf, 3);
122 if (len != 3 || buf[0] != 'L' || buf[1] != 'P')
123 throw string("unexpected or no response from `") + host + ':' + port
124 + "'. Either it's not\n Festival listening,"
125 " or Festival couldn't open a sound device.";
127 SG_LOG(SG_IO, SG_INFO, "VOICE: connection to Festival server on `"
128 << host << ':' << port << "' established");
130 setVolume(_volume = _volumeNode->getDoubleValue());
131 setPitch(_pitch = _pitchNode->getDoubleValue());
132 setSpeed(_speed = _speedNode->getDoubleValue());
135 string preamble = node->getStringValue("preamble", "");
136 if (!preamble.empty())
137 pushMessage(preamble);
139 node->getNode("text", true)->addChangeListener(new FGVoiceListener(this));
143 FGVoiceMgr::FGVoice::~FGVoice()
150 void FGVoiceMgr::FGVoice::pushMessage(string m)
152 _msg.push(m + "\015\012");
153 #if defined(ENABLE_THREADS)
154 _mgr->_thread->wake_up();
159 bool FGVoiceMgr::FGVoice::speak(void)
164 const string s = _msg.front();
166 _sock->writestring(s.c_str());
167 return !_msg.empty();
171 void FGVoiceMgr::FGVoice::update(void)
175 d = _volumeNode->getDoubleValue();
177 setVolume(_volume = d);
178 d = _pitchNode->getDoubleValue();
180 setPitch(_pitch = d);
181 d = _speedNode->getDoubleValue();
183 setSpeed(_speed = d);
188 void FGVoiceMgr::FGVoice::setVolume(double d)
190 std::ostringstream s;
191 s << "(set! default_after_synth_hooks (list (lambda (utt)"
192 "(utt.wave.rescale utt " << d << " t))))";
193 pushMessage(s.str());
197 void FGVoiceMgr::FGVoice::setPitch(double d)
199 std::ostringstream s;
200 s << "(set! int_lr_params '((target_f0_mean " << d <<
201 ")(target_f0_std 14)(model_f0_mean 170)"
202 "(model_f0_std 34)))";
203 pushMessage(s.str());
207 void FGVoiceMgr::FGVoice::setSpeed(double d)
209 std::ostringstream s;
210 s << "(Parameter.set 'Duration_Stretch " << d << ')';
211 pushMessage(s.str());
219 #if defined(ENABLE_THREADS)
220 void FGVoiceMgr::FGVoiceThread::run(void)
224 for (unsigned int i = 0; i < _mgr->_voices.size(); i++)
225 busy |= _mgr->_voices[i]->speak();
238 void FGVoiceMgr::FGVoice::FGVoiceListener::valueChanged(SGPropertyNode *node)
240 if (_voice->_mgr->_paused)
243 const string s = node->getStringValue();
244 //cerr << "\033[31;1mBEFORE [" << s << "]\033[m" << endl;
247 for (unsigned int i = 0; i < s.size(); i++) {
249 if (c == '\n' || c == '\r' || c == '\t')
251 else if (!isprint(c))
253 else if (c == '\\' || c == '"')
255 else if (c == '|' || c == '_')
256 m += ' '; // don't say "vertical bar" or "underscore"
268 //cerr << "\033[31;1mAFTER [" << m << "]\033[m" << endl;
269 if (_voice->_festival)
270 m = string("(SayText \"") + m + "\")";
272 _voice->pushMessage(m);