]> git.mxchange.org Git - flightgear.git/blobdiff - src/Sound/voice.cxx
Added two missing files from JSBSim.org that were missing in the last sync.
[flightgear.git] / src / Sound / voice.cxx
index ae23ae88b7738cad042b0525d21eea406d0d57a4..04e9a3debaac352d34bd4eb73acc6aa48ddc6359 100644 (file)
@@ -52,6 +52,7 @@ FGVoiceMgr::~FGVoiceMgr()
                return;
        _thread->cancel();
        _thread->join();
+       delete _thread;
 #endif
 }
 
@@ -63,11 +64,16 @@ void FGVoiceMgr::init()
 
        SGPropertyNode *base = fgGetNode(VOICE, true);
        vector<SGPropertyNode_ptr> voices = base->getChildren("voice");
-       for (unsigned int i = 0; i < voices.size(); i++)
-               _voices.push_back(new FGVoice(this, voices[i]));
+       try {
+               for (unsigned int i = 0; i < voices.size(); i++)
+                       _voices.push_back(new FGVoice(this, voices[i]));
+       } catch (const string& s) {
+               SG_LOG(SG_IO, SG_ALERT, "VOICE: " << s);
+       }
 
 #if defined(ENABLE_THREADS)
-       _thread->start(1);
+       _thread->setProcessorAffinity(1);
+       _thread->start();
 #endif
 }
 
@@ -105,28 +111,20 @@ FGVoiceMgr::FGVoice::FGVoice(FGVoiceMgr *mgr, const SGPropertyNode_ptr node) :
        const string &port = _mgr->_port;
 
        _sock = new SGSocket(host, port, "tcp");
-       _sock->set_timeout(10000);
-       _connected = _sock->open(SG_IO_OUT);
-       if (!_connected) {
-               SG_LOG(SG_IO, SG_ALERT, "VOICE: no connection to `"
-                               << host << ':' << port << '\'');
-               return;
-       }
+       _sock->set_timeout(6000);
+       if (!_sock->open(SG_IO_OUT))
+               throw string("no connection to `") + host + ':' + port + '\'';
 
        if (_festival) {
                _sock->writestring("(SayText \"\")\015\012");
                char buf[4];
                int len = _sock->read(buf, 3);
-               if (len != 3 || buf[0] != 'L' || buf[1] != 'P') {
-                       SG_LOG(SG_IO, SG_ALERT, "VOICE: unexpected or no response from `"
-                                       << host << ':' << port << "'. Either it's not " << endl
-                                       << "       Festival listening, or Festival couldn't open a "
-                                       "sound device.");
-                       _connected = false;
-                       return;
-               }
-
-               SG_LOG(SG_IO, SG_BULK, "VOICE: connection to Festival server on `"
+               if (len != 3 || buf[0] != 'L' || buf[1] != 'P')
+                       throw string("unexpected or no response from `") + host + ':' + port
+                                       + "'. Either it's not\n       Festival listening,"
+                                       " or Festival couldn't open a sound device.";
+
+               SG_LOG(SG_IO, SG_INFO, "VOICE: connection to Festival server on `"
                                << host << ':' << port << "' established");
 
                setVolume(_volume = _volumeNode->getDoubleValue());
@@ -160,14 +158,11 @@ void FGVoiceMgr::FGVoice::pushMessage(string m)
 
 bool FGVoiceMgr::FGVoice::speak(void)
 {
-       if (_msg.empty()) {
-//             cerr << "<nothing to say>" << endl;
+       if (_msg.empty())
                return false;
-       }
 
        const string s = _msg.front();
        _msg.pop();
-//     cerr << "POP " << s;
        _sock->writestring(s.c_str());
        return !_msg.empty();
 }
@@ -175,7 +170,7 @@ bool FGVoiceMgr::FGVoice::speak(void)
 
 void FGVoiceMgr::FGVoice::update(void)
 {
-       if (_connected && _festival) {
+       if (_festival) {
                double d;
                d = _volumeNode->getDoubleValue();
                if (d != _volume)
@@ -246,22 +241,31 @@ void FGVoiceMgr::FGVoice::FGVoiceListener::valueChanged(SGPropertyNode *node)
                return;
 
        const string s = node->getStringValue();
-//     cerr << "PUSH " << s << endl;
+       //cerr << "\033[31;1mBEFORE [" << s << "]\033[m" << endl;
 
        string m;
        for (unsigned int i = 0; i < s.size(); i++) {
                char c = s[i];
-               if (!isprint(c))
+               if (c == '\n' || c == '\r' || c == '\t')
+                       m += ' ';
+               else if (!isprint(c))
                        continue;
-               else if (c == '"' || c == '\\')
-                       m += '\\' + c;
+               else if (c == '\\' || c == '"')
+                       m += '\\', m += c;
                else if (c == '|' || c == '_')
                        m += ' ';       // don't say "vertical bar" or "underscore"
                else if (c == '&')
                        m += " and ";
+               else if (c == '{') {
+                       while (i < s.size())
+                               if (s[++i] == '|')
+                                       break;
+               } else if (c == '}')
+                       ;
                else
                        m += c;
        }
+       //cerr << "\033[31;1mAFTER [" << m << "]\033[m" << endl;
        if (_voice->_festival)
                m = string("(SayText \"") + m + "\")";