1 /*
2  * VoiceSynthesizer.cxx - wraps flite+hts_engine
3  * Copyright (C) 2014  Torsten Dreyer - torsten (at) t3r (dot) de
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 #include "VoiceSynthesizer.hxx"
20 #include <Main/globals.hxx>
21 #include <Main/fg_props.hxx>
22 #include <simgear/sg_inlines.h>
23 #include <simgear/debug/logstream.hxx>
24 #include <simgear/misc/sg_path.hxx>
25 #include <simgear/threads/SGThread.hxx>
26 
27 #include <flite_hts_engine.h>
28 
29 using std::string;
30 
31 static const char * VOICE_FILES[] = {
32   "cmu_us_arctic_slt.htsvoice",
33   "cstr_uk_female-1.0.htsvoice"
34 };
35 
36 class FLITEVoiceSynthesizer::WorkerThread : public SGThread
37 {
38 public:
WorkerThread(FLITEVoiceSynthesizer * synthesizer)39   WorkerThread(FLITEVoiceSynthesizer * synthesizer)
40       : _synthesizer(synthesizer)
41   {
42   }
43   virtual void run();
44 private:
45   FLITEVoiceSynthesizer * _synthesizer;
46 };
47 
run()48 void FLITEVoiceSynthesizer::WorkerThread::run()
49 {
50   for (;;) {
51     SynthesizeRequest request = _synthesizer->_requests.pop();
52 
53     // marker value indicating termination requested
54     if ((request.speed < 0.0) && (request.volume < 0.0)) {
55       SG_LOG(SG_SOUND, SG_DEBUG, "FLITE synthesis thread exiting");
56       return;
57     }
58 
59     if ( NULL != request.listener) {
60       SGSharedPtr<SGSoundSample> sample = _synthesizer->synthesize(request.text, request.volume, request.speed, request.pitch);
61       request.listener->SoundSampleReady( sample );
62     }
63   }
64 }
65 
cancelThreadRequest()66 SynthesizeRequest SynthesizeRequest::cancelThreadRequest()
67 {
68   SynthesizeRequest marker;
69   marker.volume = -999.0;
70   marker.speed = -999.0;
71   return marker;
72 }
73 
getVoicePath(voice_t voice)74 string FLITEVoiceSynthesizer::getVoicePath( voice_t voice )
75 {
76   if( voice < 0 || voice >= VOICE_UNKNOWN ) return string("");
77   SGPath voicePath = globals->get_fg_root() / "ATC" /  VOICE_FILES[voice];
78   return voicePath.utf8Str();
79 }
80 
getVoicePath(const string & voice)81 string FLITEVoiceSynthesizer::getVoicePath( const string & voice )
82 {
83   if( voice == "cmu_us_arctic_slt" ) return getVoicePath(CMU_US_ARCTIC_SLT);
84   if( voice == "cstr_uk_female" ) return getVoicePath(CSTR_UK_FEMALE);
85   return getVoicePath(VOICE_UNKNOWN);
86 }
87 
88 
synthesize(SynthesizeRequest & request)89 void FLITEVoiceSynthesizer::synthesize( SynthesizeRequest & request)
90 {
91   _requests.push(request);
92 }
93 
FLITEVoiceSynthesizer(const std::string & voice)94 FLITEVoiceSynthesizer::FLITEVoiceSynthesizer(const std::string & voice)
95     : _engine(new Flite_HTS_Engine), _worker(new FLITEVoiceSynthesizer::WorkerThread(this)), _volume(6.0)
96 {
97   _volume = fgGetDouble("/sim/sound/voice-synthesizer/volume", _volume );
98   Flite_HTS_Engine_initialize(_engine);
99   Flite_HTS_Engine_load(_engine, voice.c_str());
100   _worker->start();
101 }
102 
~FLITEVoiceSynthesizer()103 FLITEVoiceSynthesizer::~FLITEVoiceSynthesizer()
104 {
105   // push the special marker value
106   _requests.push(SynthesizeRequest::cancelThreadRequest());
107   _worker->join();
108   Flite_HTS_Engine_clear(_engine);
109 }
110 
synthesize(const std::string & text,double volume,double speed,double pitch)111 SGSoundSample * FLITEVoiceSynthesizer::synthesize(const std::string & text, double volume, double speed, double pitch )
112 {
113   SG_CLAMP_RANGE( volume, 0.0, 1.0 );
114   SG_CLAMP_RANGE( speed, 0.0, 1.0 );
115   SG_CLAMP_RANGE( pitch, 0.0, 1.0 );
116   HTS_Engine_set_volume( &_engine->engine, _volume );
117   HTS_Engine_set_speed( &_engine->engine, 0.8 + 0.4 * speed );
118   HTS_Engine_add_half_tone(&_engine->engine, -4.0 + 8.0 * pitch );
119 
120 
121   void* data;
122   int rate, count;
123   if ( FALSE == Flite_HTS_Engine_synthesize_samples_mono16(_engine, text.c_str(), &data, &count, &rate)) return NULL;
124 
125   auto buf = std::unique_ptr<unsigned char, decltype(free)*>{
126     reinterpret_cast<unsigned char*>( data ),
127     free
128   };
129   return new SGSoundSample(buf,
130                            count * sizeof(short),
131                            rate,
132                            SG_SAMPLE_MONO16);
133 }
134 
135