]> git.mxchange.org Git - flightgear.git/blob - src/Sound/VoiceSynthesizer.cxx
Render voice to memory, no more temp files.
[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 class FLITEVoiceSynthesizer::WorkerThread: public OpenThreads::Thread {
30 public:
31   WorkerThread(FLITEVoiceSynthesizer * synthesizer)
32       : _synthesizer(synthesizer)
33   {
34   }
35   virtual void run();
36 private:
37   FLITEVoiceSynthesizer * _synthesizer;
38 };
39
40 void FLITEVoiceSynthesizer::WorkerThread::run()
41 {
42   for (;;) {
43     SynthesizeRequest request = _synthesizer->_requests.pop();
44     if ( NULL != request.listener) {
45       SGSharedPtr<SGSoundSample> sample = _synthesizer->synthesize(request.text, request.volume, request.speed, request.pitch);
46       request.listener->SoundSampleReady( sample );
47     }
48   }
49 }
50
51 void FLITEVoiceSynthesizer::synthesize( SynthesizeRequest & request)
52 {
53   _requests.push(request);
54 }
55
56 FLITEVoiceSynthesizer::FLITEVoiceSynthesizer(const std::string & voice)
57     : _engine(new Flite_HTS_Engine), _worker(new FLITEVoiceSynthesizer::WorkerThread(this)), _volume(6.0)
58 {
59   _volume = fgGetDouble("/sim/sound/voice-synthesizer/volume", _volume );
60   Flite_HTS_Engine_initialize(_engine);
61   Flite_HTS_Engine_load(_engine, voice.c_str());
62   _worker->start();
63 }
64
65 FLITEVoiceSynthesizer::~FLITEVoiceSynthesizer()
66 {
67   _worker->cancel();
68   _worker->join();
69   Flite_HTS_Engine_clear(_engine);
70 }
71
72 SGSoundSample * FLITEVoiceSynthesizer::synthesize(const std::string & text, double volume, double speed, double pitch )
73 {
74   SG_CLAMP_RANGE( volume, 0.0, 1.0 );
75   SG_CLAMP_RANGE( speed, 0.0, 1.0 );
76   SG_CLAMP_RANGE( pitch, 0.0, 1.0 );
77   HTS_Engine_set_volume( &_engine->engine, _volume );
78   HTS_Engine_set_speed( &_engine->engine, 0.8 + 0.4 * speed );
79   HTS_Engine_add_half_tone(&_engine->engine, -4.0 + 8.0 * pitch );
80
81     
82   ALvoid* data;
83   ALsizei rate, count;
84   if ( FALSE == Flite_HTS_Engine_synthesize_samples_mono16(_engine, text.c_str(), &data, &count, &rate)) return NULL;
85
86   return new SGSoundSample(&data,
87                            count * sizeof(short),
88                            rate,
89                            AL_FORMAT_MONO16);
90 }
91