]> git.mxchange.org Git - flightgear.git/blob - src/Sound/VoiceSynthesizer.cxx
Interim windows build fix
[flightgear.git] / src / Sound / VoiceSynthesizer.cxx
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/sound/readwav.hxx>
25 #include <simgear/misc/sg_path.hxx>
26 #include <OpenThreads/Thread>
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 OpenThreads::Thread {
37 public:
38   WorkerThread(FLITEVoiceSynthesizer * synthesizer)
39       : _synthesizer(synthesizer)
40   {
41   }
42   virtual void run();
43 private:
44   FLITEVoiceSynthesizer * _synthesizer;
45 };
46
47 void FLITEVoiceSynthesizer::WorkerThread::run()
48 {
49   for (;;) {
50     SynthesizeRequest request = _synthesizer->_requests.pop();
51     if ( NULL != request.listener) {
52       SGSharedPtr<SGSoundSample> sample = _synthesizer->synthesize(request.text, request.volume, request.speed, request.pitch);
53       request.listener->SoundSampleReady( sample );
54     }
55   }
56 }
57
58 string FLITEVoiceSynthesizer::getVoicePath( voice_t voice )
59 {
60   if( voice < 0 || voice >= VOICE_UNKNOWN ) return string("");
61   string voicePath = globals->get_fg_root() + "/ATC/" + VOICE_FILES[voice];
62   return voicePath;
63 }
64
65 string FLITEVoiceSynthesizer::getVoicePath( const string & voice )
66 {
67   if( voice == "cmu_us_arctic_slt" ) return getVoicePath(CMU_US_ARCTIC_SLT);
68   if( voice == "cstr_uk_female" ) return getVoicePath(CSTR_UK_FEMALE);
69   return getVoicePath(VOICE_UNKNOWN);
70 }
71
72
73 void FLITEVoiceSynthesizer::synthesize( SynthesizeRequest & request)
74 {
75   _requests.push(request);
76 }
77
78 FLITEVoiceSynthesizer::FLITEVoiceSynthesizer(const std::string & voice)
79     : _engine(new Flite_HTS_Engine), _worker(new FLITEVoiceSynthesizer::WorkerThread(this)), _volume(6.0)
80 {
81   _volume = fgGetDouble("/sim/sound/voice-synthesizer/volume", _volume );
82   Flite_HTS_Engine_initialize(_engine);
83   Flite_HTS_Engine_load(_engine, voice.c_str());
84   _worker->start();
85 }
86
87 FLITEVoiceSynthesizer::~FLITEVoiceSynthesizer()
88 {
89   _worker->cancel();
90   _worker->join();
91   Flite_HTS_Engine_clear(_engine);
92 }
93
94 SGSoundSample * FLITEVoiceSynthesizer::synthesize(const std::string & text, double volume, double speed, double pitch )
95 {
96   SG_CLAMP_RANGE( volume, 0.0, 1.0 );
97   SG_CLAMP_RANGE( speed, 0.0, 1.0 );
98   SG_CLAMP_RANGE( pitch, 0.0, 1.0 );
99   HTS_Engine_set_volume( &_engine->engine, _volume );
100   HTS_Engine_set_speed( &_engine->engine, 0.8 + 0.4 * speed );
101   HTS_Engine_add_half_tone(&_engine->engine, -4.0 + 8.0 * pitch );
102
103     
104   ALvoid* data;
105   ALsizei rate, count;
106   if ( FALSE == Flite_HTS_Engine_synthesize_samples_mono16(_engine, text.c_str(), &data, &count, &rate)) return NULL;
107
108   return new SGSoundSample(&data,
109                            count * sizeof(short),
110                            rate,
111                            AL_FORMAT_MONO16);
112 }
113