| 1 | // speech synthesis interface subsystem |
| 2 | // |
| 3 | // Written by Torsten Dreyer, started April 2014 |
| 4 | // |
| 5 | // Copyright (C) 2014 Torsten Dreyer - torsten (at) t3r (dot) de |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | // |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | # include "config.h" |
| 24 | #endif |
| 25 | |
| 26 | #include "flitevoice.hxx" |
| 27 | #include <Main/fg_props.hxx> |
| 28 | #include <simgear/sound/sample_group.hxx> |
| 29 | #include <flite_hts_engine.h> |
| 30 | |
| 31 | using std::string; |
| 32 | |
| 33 | #include "VoiceSynthesizer.hxx" |
| 34 | |
| 35 | FGFLITEVoice::FGFLITEVoice(FGVoiceMgr * mgr, const SGPropertyNode_ptr node, const char * sampleGroupRefName) |
| 36 | : FGVoice(mgr), _synthesizer( NULL), _seconds_to_run(0.0) |
| 37 | { |
| 38 | |
| 39 | _sampleName = node->getStringValue("desc", node->getPath().c_str()); |
| 40 | |
| 41 | SGPath voice = globals->get_fg_root() / "ATC" / |
| 42 | node->getStringValue("htsvoice", "cmu_us_arctic_slt.htsvoice"); |
| 43 | |
| 44 | _synthesizer = new FLITEVoiceSynthesizer(voice.utf8Str()); |
| 45 | |
| 46 | auto smgr = globals->get_subsystem<SGSoundMgr>(); |
| 47 | _sgr = smgr->find(sampleGroupRefName, true); |
| 48 | _sgr->tie_to_listener(); |
| 49 | |
| 50 | node->getNode("text", true)->addChangeListener(this); |
| 51 | |
| 52 | SG_LOG(SG_SOUND, SG_DEBUG, "FLITEVoice initialized for sample-group '" << sampleGroupRefName |
| 53 | << "'. Samples will be named '" << _sampleName << "' " |
| 54 | << "voice is '" << voice << "'"); |
| 55 | } |
| 56 | |
| 57 | FGFLITEVoice::~FGFLITEVoice() |
| 58 | { |
| 59 | delete _synthesizer; |
| 60 | } |
| 61 | |
| 62 | void FGFLITEVoice::speak(const string & msg) |
| 63 | { |
| 64 | // this is called from voice.cxx:FGVoiceMgr::FGVoiceThread::run |
| 65 | string s = simgear::strutils::strip(msg); |
| 66 | if (!s.empty()) { |
| 67 | _sampleQueue.push(_synthesizer->synthesize(msg, 1.0, 0.5, 0.5)); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void FGFLITEVoice::update(double dt) |
| 72 | { |
| 73 | _seconds_to_run -= dt; |
| 74 | |
| 75 | if (_seconds_to_run < 0.0) { |
| 76 | SGSharedPtr<SGSoundSample> sample = _sampleQueue.pop(); |
| 77 | if (sample.valid()) { |
| 78 | _sgr->remove(_sampleName); |
| 79 | _sgr->add(sample, _sampleName); |
| 80 | _sgr->resume(); |
| 81 | _sgr->play(_sampleName, false); |
| 82 | |
| 83 | // Don't play any further TTS until we've finished playing this sample, |
| 84 | // allowing 500ms for a gap between transmissions. Good radio comms! |
| 85 | _seconds_to_run = 0.5 + ((float) sample->get_no_samples()) / ((float) sample->get_frequency()); |
| 86 | } |
| 87 | } |
| 88 | } |