2 * VoiceSynthesizer.cxx - wraps flite+hts_engine
3 * Copyright (C) 2014 Torsten Dreyer - torsten (at) t3r (dot) de
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.
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.
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.
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>
29 class FLITEVoiceSynthesizer::WorkerThread: public OpenThreads::Thread {
31 WorkerThread(FLITEVoiceSynthesizer * synthesizer)
32 : _synthesizer(synthesizer)
37 FLITEVoiceSynthesizer * _synthesizer;
40 void FLITEVoiceSynthesizer::WorkerThread::run()
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 );
51 void FLITEVoiceSynthesizer::synthesize( SynthesizeRequest & request)
53 _requests.push(request);
56 FLITEVoiceSynthesizer::FLITEVoiceSynthesizer(const std::string & voice)
57 : _engine(new Flite_HTS_Engine), _worker(new FLITEVoiceSynthesizer::WorkerThread(this)), _volume(6.0)
59 _volume = fgGetDouble("/sim/sound/voice-synthesizer/volume", _volume );
60 Flite_HTS_Engine_initialize(_engine);
61 Flite_HTS_Engine_load(_engine, voice.c_str());
65 FLITEVoiceSynthesizer::~FLITEVoiceSynthesizer()
69 Flite_HTS_Engine_clear(_engine);
72 SGSoundSample * FLITEVoiceSynthesizer::synthesize(const std::string & text, double volume, double speed, double pitch )
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 );
84 if ( FALSE == Flite_HTS_Engine_synthesize_samples_mono16(_engine, text.c_str(), &data, &count, &rate)) return NULL;
86 return new SGSoundSample(&data,
87 count * sizeof(short),