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>
31 static const char * VOICE_FILES[] = {
32 "cmu_us_arctic_slt.htsvoice",
33 "cstr_uk_female-1.0.htsvoice"
36 class FLITEVoiceSynthesizer::WorkerThread: public OpenThreads::Thread {
38 WorkerThread(FLITEVoiceSynthesizer * synthesizer)
39 : _synthesizer(synthesizer)
44 FLITEVoiceSynthesizer * _synthesizer;
47 void FLITEVoiceSynthesizer::WorkerThread::run()
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 );
58 string FLITEVoiceSynthesizer::getVoicePath( voice_t voice )
60 if( voice < 0 || voice >= VOICE_UNKNOWN ) return string("");
61 string voicePath = globals->get_fg_root() + "/ATC/" + VOICE_FILES[voice];
65 string FLITEVoiceSynthesizer::getVoicePath( const string & voice )
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);
73 void FLITEVoiceSynthesizer::synthesize( SynthesizeRequest & request)
75 _requests.push(request);
78 FLITEVoiceSynthesizer::FLITEVoiceSynthesizer(const std::string & voice)
79 : _engine(new Flite_HTS_Engine), _worker(new FLITEVoiceSynthesizer::WorkerThread(this)), _volume(6.0)
81 _volume = fgGetDouble("/sim/sound/voice-synthesizer/volume", _volume );
82 Flite_HTS_Engine_initialize(_engine);
83 Flite_HTS_Engine_load(_engine, voice.c_str());
87 FLITEVoiceSynthesizer::~FLITEVoiceSynthesizer()
91 Flite_HTS_Engine_clear(_engine);
94 SGSoundSample * FLITEVoiceSynthesizer::synthesize(const std::string & text, double volume, double speed, double pitch )
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 );
106 if ( FALSE == Flite_HTS_Engine_synthesize_samples_mono16(_engine, text.c_str(), &data, &count, &rate)) return NULL;
108 return new SGSoundSample(&data,
109 count * sizeof(short),