1 // FGATCVoice.cxx - a class to encapsulate an ATC voice
3 // Written by David Luff, started November 2002.
5 // Copyright (C) 2002 David C Luff - david.luff@nottingham.ac.uk
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.
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.
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.
26 #include "ATCVoice.hxx"
34 #include <simgear/sound/soundmgr_openal.hxx>
35 #include <simgear/sound/sample_openal.hxx>
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/sgstream.hxx>
40 #include <simgear/math/sg_random.h>
42 #include <Main/globals.hxx>
46 FGATCVoice::FGATCVoice() {
51 FGATCVoice::~FGATCVoice() {
57 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
58 // Return true if successful.
59 bool FGATCVoice::LoadVoice(const string& voice) {
62 SGPath path = globals->get_fg_root();
63 string file = voice + ".wav";
67 string full_path = path.str();
69 SGSoundMgr *smgr = globals->get_soundmgr();
71 if (!smgr->load(full_path, &data, &format, &rawDataSize, &freq))
73 rawSoundData = (char*)data;
75 cout << "ATCVoice: format: " << format
76 << " size: " << rawDataSize << endl;
78 path = globals->get_fg_root();
79 string wordPath = "ATC/" + voice + ".vce";
80 path.append(wordPath);
82 // Now load the word data
83 fin.open(path.c_str(), ios::in);
85 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
88 SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
92 char wrdOffsetStr[20];
93 char wrdLengthStr[20];
94 unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
95 unsigned int wrdLength; // Length of the word sample in bytes
98 unsigned int numwords = atoi(numwds);
99 //cout << numwords << '\n';
100 for(unsigned int i=0; i < numwords; ++i) {
105 wrdOffset = atoi(wrdOffsetStr);
106 wrdLength = atoi(wrdLengthStr);
107 wd.offset = wrdOffset;
108 wd.length = wrdLength;
109 wordMap[wrdstr] = wd;
111 for(string::iterator p = ws2.begin(); p != ws2.end(); p++){
113 if (*p == '-') *p = '_';
115 if (wrdstr != ws2) wordMap[ws2] = wd;
117 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
126 // Given a desired message, return a string containing the
128 void* FGATCVoice::WriteMessage(const string& message, size_t* len) {
130 // What should we do here?
131 // First - parse the message into a list of tokens.
132 // Sort the tokens into those we understand and those we don't.
133 // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
136 const char delimiters[] = " \t.,;:\"\n";
137 string::size_type token_start = message.find_first_not_of(delimiters);
138 while(token_start != string::npos) {
139 string::size_type token_end = message.find_first_of(delimiters, token_start);
141 if (token_end == string::npos) {
142 token = message.substr(token_start);
143 token_start = string::npos;
145 token = message.substr(token_start, token_end - token_start);
146 token_start = message.find_first_not_of(delimiters, token_end);
149 if (token == "/_") continue;
151 for(string::iterator t = token.begin(); t != token.end(); t++) {
152 // canonicalize the token, to match what's in the index
153 *t = (*t == '-') ? '_' : tolower(*t);
155 SG_LOG(SG_ATC, SG_DEBUG, "voice synth: token: '"
158 atc_word_map_const_iterator wordIt = wordMap.find(token);
159 if(wordIt == wordMap.end()) {
160 // Oh dear - the token isn't in the sound file
161 SG_LOG(SG_ATC, SG_ALERT, "voice synth: word '"
162 << token << "' not found");
164 const WordData& word = wordIt->second;
166 * Sanity check for corrupt/mismatched sound data input - avoids a seg fault
167 * (As long as the calling function checks the return value!!)
168 * This check should be left in even when the default Flightgear files are known
169 * to be OK since it checks for mis-indexing of voice files by 3rd party developers.
171 if((word.offset + word.length) > rawDataSize) {
172 SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
173 SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << word.offset + word.length
174 << " exceeds rawdata size: " << rawDataSize << endl);
179 sound.insert(sound.end(), rawSoundData + word.offset, rawSoundData + word.offset + word.length);
183 // Check for no tokens found else slScheduler can be crashed
189 char* data = (char*)malloc(*len);
191 SG_LOG(SG_ATC, SG_ALERT, "ERROR - could not allocate " << *len << " bytes of memory for ATIS sound\n");
196 // randomize start position
197 unsigned int offsetIn = (unsigned int)(*len * sg_random());
198 if (offsetIn > 0 && offsetIn < *len) {
199 copy(sound.begin() + offsetIn, sound.end(), data);
200 copy(sound.begin(), sound.begin() + offsetIn, data + *len - offsetIn);
202 copy(sound.begin(), sound.end(), data);