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.
27 #include <Main/globals.hxx>
29 #include <simgear/compiler.h>
30 #include <Main/fg_props.hxx>
33 #if defined(ENABLE_FLITE)
34 #include "flitevoice.hxx"
37 #define VOICE "/sim/sound/voices"
41 class FGFestivalVoice : public FGVoiceMgr::FGVoice {
43 FGFestivalVoice(FGVoiceMgr *, const SGPropertyNode_ptr);
44 virtual ~FGFestivalVoice();
45 virtual void speak( const string & msg );
46 virtual void update();
47 void setVolume(double);
48 void setPitch(double);
49 void setSpeed(double);
56 SGPropertyNode_ptr _volumeNode;
57 SGPropertyNode_ptr _pitchNode;
58 SGPropertyNode_ptr _speedNode;
63 FGVoiceMgr::FGVoiceMgr() :
64 #if defined(ENABLE_THREADS)
67 _host(fgGetString(VOICE "/host", "localhost")),
68 _port(fgGetString(VOICE "/port", "1314")),
69 _enabled(fgGetBool(VOICE "/enabled", false)),
70 _pausedNode(fgGetNode("/sim/sound/working", true)),
76 FGVoiceMgr::~FGVoiceMgr()
81 void FGVoiceMgr::init()
86 #if defined(ENABLE_THREADS)
87 _thread = new FGVoiceThread(this);
90 SGPropertyNode *base = fgGetNode(VOICE, true);
91 vector<SGPropertyNode_ptr> voices = base->getChildren("voice");
92 for (unsigned int i = 0; i < voices.size(); i++) {
93 SGPropertyNode_ptr voice = voices[i];
94 if( voice->getBoolValue("festival", false ) ) {
96 _voices.push_back(new FGFestivalVoice(this, voice));
98 } catch (const std::string& s) {
99 SG_LOG(SG_SOUND, SG_WARN, "failed to create festival voice, falling back to flite voice" );
102 #if defined(ENABLE_FLITE)
103 SG_LOG(SG_ALL,SG_INFO,"creating flite voice" );
104 _voices.push_back(new FGFLITEVoice(this, voice));
106 SG_LOG(SG_ALL,SG_ALERT,"non festival voice not supported." );
110 #if defined(ENABLE_THREADS)
111 _thread->setProcessorAffinity(1);
116 void FGVoiceMgr::shutdown()
118 #if defined(ENABLE_THREADS)
127 for( std::vector<FGVoice*>::iterator it = _voices.begin(); it != _voices.end(); ++it )
132 void FGVoiceMgr::update(double)
137 _paused = !_pausedNode->getBoolValue();
138 for (unsigned int i = 0; i < _voices.size(); i++) {
139 _voices[i]->update();
140 #if !defined(ENABLE_THREADS)
151 FGFestivalVoice::FGFestivalVoice(FGVoiceMgr *mgr, const SGPropertyNode_ptr node) :
153 _volumeNode(node->getNode("volume", true)),
154 _pitchNode(node->getNode("pitch", true)),
155 _speedNode(node->getNode("speed", true))
157 SG_LOG(SG_SOUND, SG_INFO, "VOICE: adding `" << node->getStringValue("desc", "<unnamed>")
159 const string &host = _mgr->_host;
160 const string &port = _mgr->_port;
162 _sock = new SGSocket(host, port, "tcp");
163 _sock->set_timeout(6000);
164 if (!_sock->open(SG_IO_OUT))
165 throw string("no connection to `") + host + ':' + port + '\'';
168 _sock->writestring("(SayText \"\")\015\012");
170 int len = _sock->read(buf, 3);
171 if (len != 3 || buf[0] != 'L' || buf[1] != 'P')
172 throw string("unexpected or no response from `") + host + ':' + port
173 + "'. Either it's not\n Festival listening,"
174 " or Festival couldn't open a sound device.";
176 SG_LOG(SG_SOUND, SG_INFO, "VOICE: connection to Festival server on `"
177 << host << ':' << port << "' established");
179 setVolume(_volume = _volumeNode->getDoubleValue());
180 setPitch(_pitch = _pitchNode->getDoubleValue());
181 setSpeed(_speed = _speedNode->getDoubleValue());
184 string preamble = node->getStringValue("preamble", "");
185 if (!preamble.empty())
186 pushMessage(preamble);
187 node->getNode("text", true)->addChangeListener(this);
191 FGFestivalVoice::~FGFestivalVoice()
198 void FGVoiceMgr::FGVoice::pushMessage( const string & m)
201 #if defined(ENABLE_THREADS)
202 _mgr->_thread->wake_up();
206 bool FGVoiceMgr::FGVoice::speak(void)
211 const string s = _msg.front();
216 return !_msg.empty();
219 void FGFestivalVoice::speak( const string & msg )
226 if( msg[0] == '(' ) {
229 s.append("(SayText \"");
230 s.append(msg).append("\")");
233 s.append("\015\012");
234 _sock->writestring(s.c_str());
238 void FGFestivalVoice::update(void)
241 d = _volumeNode->getDoubleValue();
243 setVolume(_volume = d);
244 d = _pitchNode->getDoubleValue();
246 setPitch(_pitch = d);
247 d = _speedNode->getDoubleValue();
249 setSpeed(_speed = d);
253 void FGFestivalVoice::setVolume(double d)
255 std::ostringstream s;
256 s << "(set! default_after_synth_hooks (list (lambda (utt)"
257 "(utt.wave.rescale utt " << d << " t))))";
258 pushMessage(s.str());
262 void FGFestivalVoice::setPitch(double d)
264 std::ostringstream s;
265 s << "(set! int_lr_params '((target_f0_mean " << d <<
266 ")(target_f0_std 14)(model_f0_mean 170)"
267 "(model_f0_std 34)))";
268 pushMessage(s.str());
272 void FGFestivalVoice::setSpeed(double d)
274 std::ostringstream s;
275 s << "(Parameter.set 'Duration_Stretch " << d << ')';
276 pushMessage(s.str());
281 #if defined(ENABLE_THREADS)
282 void FGVoiceMgr::FGVoiceThread::run(void)
286 for (unsigned int i = 0; i < _mgr->_voices.size(); i++)
287 busy |= _mgr->_voices[i]->speak();
300 void FGVoiceMgr::FGVoice::valueChanged(SGPropertyNode *node)
305 const string s = node->getStringValue();
306 //cerr << "\033[31;1mBEFORE [" << s << "]\033[m" << endl;
309 for (unsigned int i = 0; i < s.size(); i++) {
311 if (c == '\n' || c == '\r' || c == '\t')
313 else if (!isprint(c))
315 else if (c == '\\' || c == '"')
317 else if (c == '|' || c == '_')
318 m += ' '; // don't say "vertical bar" or "underscore"
321 else if (c == '>' || c == '<')
322 m += ' '; // don't say "greater than" or "less than" either
332 //cerr << "\033[31;1mAFTER [" << m << "]\033[m" << endl;