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 SG_LOG(SG_ALL,SG_ALERT,"creating festival voice" );
97 _voices.push_back(new FGFestivalVoice(this, voice));
98 } catch (const std::string& s) {
99 SG_LOG(SG_SOUND, SG_ALERT, "VOICE: " << s);
102 #if defined(ENABLE_FLITE)
103 SG_LOG(SG_ALL,SG_ALERT,"creating flite voice" );
104 _voices.push_back(new FGFLITEVoice(this, voice));
106 SG_LOG(SG_ALL,SG_ALERT,"non festival voice not supported." );
111 #if defined(ENABLE_THREADS)
112 _thread->setProcessorAffinity(1);
117 void FGVoiceMgr::shutdown()
119 #if defined(ENABLE_THREADS)
126 for( std::vector<FGVoice*>::iterator it = _voices.begin(); it != _voices.end(); ++it )
131 void FGVoiceMgr::update(double)
136 _paused = !_pausedNode->getBoolValue();
137 for (unsigned int i = 0; i < _voices.size(); i++) {
138 _voices[i]->update();
139 #if !defined(ENABLE_THREADS)
150 FGFestivalVoice::FGFestivalVoice(FGVoiceMgr *mgr, const SGPropertyNode_ptr node) :
152 _volumeNode(node->getNode("volume", true)),
153 _pitchNode(node->getNode("pitch", true)),
154 _speedNode(node->getNode("speed", true))
156 SG_LOG(SG_SOUND, SG_INFO, "VOICE: adding `" << node->getStringValue("desc", "<unnamed>")
158 const string &host = _mgr->_host;
159 const string &port = _mgr->_port;
161 _sock = new SGSocket(host, port, "tcp");
162 _sock->set_timeout(6000);
163 if (!_sock->open(SG_IO_OUT))
164 throw string("no connection to `") + host + ':' + port + '\'';
167 _sock->writestring("(SayText \"\")\015\012");
169 int len = _sock->read(buf, 3);
170 if (len != 3 || buf[0] != 'L' || buf[1] != 'P')
171 throw string("unexpected or no response from `") + host + ':' + port
172 + "'. Either it's not\n Festival listening,"
173 " or Festival couldn't open a sound device.";
175 SG_LOG(SG_SOUND, SG_INFO, "VOICE: connection to Festival server on `"
176 << host << ':' << port << "' established");
178 setVolume(_volume = _volumeNode->getDoubleValue());
179 setPitch(_pitch = _pitchNode->getDoubleValue());
180 setSpeed(_speed = _speedNode->getDoubleValue());
183 string preamble = node->getStringValue("preamble", "");
184 if (!preamble.empty())
185 pushMessage(preamble);
186 node->getNode("text", true)->addChangeListener(this);
190 FGFestivalVoice::~FGFestivalVoice()
197 void FGVoiceMgr::FGVoice::pushMessage( const string & m)
200 #if defined(ENABLE_THREADS)
201 _mgr->_thread->wake_up();
205 bool FGVoiceMgr::FGVoice::speak(void)
210 const string s = _msg.front();
215 return !_msg.empty();
218 void FGFestivalVoice::speak( const string & msg )
225 if( msg[0] == '(' ) {
228 s.append("(SayText \"");
229 s.append(msg).append("\")");
232 s.append("\015\012");
233 _sock->writestring(s.c_str());
237 void FGFestivalVoice::update(void)
240 d = _volumeNode->getDoubleValue();
242 setVolume(_volume = d);
243 d = _pitchNode->getDoubleValue();
245 setPitch(_pitch = d);
246 d = _speedNode->getDoubleValue();
248 setSpeed(_speed = d);
252 void FGFestivalVoice::setVolume(double d)
254 std::ostringstream s;
255 s << "(set! default_after_synth_hooks (list (lambda (utt)"
256 "(utt.wave.rescale utt " << d << " t))))";
257 pushMessage(s.str());
261 void FGFestivalVoice::setPitch(double d)
263 std::ostringstream s;
264 s << "(set! int_lr_params '((target_f0_mean " << d <<
265 ")(target_f0_std 14)(model_f0_mean 170)"
266 "(model_f0_std 34)))";
267 pushMessage(s.str());
271 void FGFestivalVoice::setSpeed(double d)
273 std::ostringstream s;
274 s << "(Parameter.set 'Duration_Stretch " << d << ')';
275 pushMessage(s.str());
280 #if defined(ENABLE_THREADS)
281 void FGVoiceMgr::FGVoiceThread::run(void)
285 for (unsigned int i = 0; i < _mgr->_voices.size(); i++)
286 busy |= _mgr->_voices[i]->speak();
299 void FGVoiceMgr::FGVoice::valueChanged(SGPropertyNode *node)
304 const string s = node->getStringValue();
305 //cerr << "\033[31;1mBEFORE [" << s << "]\033[m" << endl;
308 for (unsigned int i = 0; i < s.size(); i++) {
310 if (c == '\n' || c == '\r' || c == '\t')
312 else if (!isprint(c))
314 else if (c == '\\' || c == '"')
316 else if (c == '|' || c == '_')
317 m += ' '; // don't say "vertical bar" or "underscore"
320 else if (c == '>' || c == '<')
321 m += ' '; // don't say "greater than" or "less than" either
331 //cerr << "\033[31;1mAFTER [" << m << "]\033[m" << endl;