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/misc/sg_path.hxx>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/misc/sgstream.hxx>
38 #include <simgear/math/sg_random.h>
40 #include <Main/globals.hxx>
44 FGATCVoice::FGATCVoice() {
49 FGATCVoice::~FGATCVoice() {
55 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
56 // Return true if successful.
57 bool FGATCVoice::LoadVoice(const string& voice) {
60 SGPath path = globals->get_fg_root();
61 string file = voice + ".wav";
65 string full_path = path.str();
67 SGSoundMgr *smgr = globals->get_soundmgr();
69 if (!smgr->load(full_path, &data, &format, &rawDataSize, &freq))
71 rawSoundData = (char*)data;
73 cout << "ATCVoice: format: " << format
74 << " size: " << rawDataSize << endl;
76 path = globals->get_fg_root();
77 string wordPath = "ATC/" + voice + ".vce";
78 path.append(wordPath);
80 // Now load the word data
81 fin.open(path.c_str(), ios::in);
83 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
86 SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
90 char wrdOffsetStr[20];
91 char wrdLengthStr[20];
92 unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
93 unsigned int wrdLength; // Length of the word sample in bytes
96 unsigned int numwords = atoi(numwds);
97 //cout << numwords << '\n';
98 for(unsigned int i=0; i < numwords; ++i) {
103 wrdOffset = atoi(wrdOffsetStr);
104 wrdLength = atoi(wrdLengthStr);
105 wd.offset = wrdOffset;
106 wd.length = wrdLength;
107 wordMap[wrdstr] = wd;
109 for(string::iterator p = ws2.begin(); p != ws2.end(); p++){
111 if (*p == '-') *p = '_';
113 if (wrdstr != ws2) wordMap[ws2] = wd;
115 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
124 // Given a desired message, return a string containing the
126 void* FGATCVoice::WriteMessage(const string& message, size_t* len) {
128 // What should we do here?
129 // First - parse the message into a list of tokens.
130 // Sort the tokens into those we understand and those we don't.
131 // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
134 const char delimiters[] = " \t.,;:\"\n";
135 string::size_type token_start = message.find_first_not_of(delimiters);
136 while(token_start != string::npos) {
137 string::size_type token_end = message.find_first_of(delimiters, token_start);
139 if (token_end == string::npos) {
140 token = message.substr(token_start);
141 token_start = string::npos;
143 token = message.substr(token_start, token_end - token_start);
144 token_start = message.find_first_not_of(delimiters, token_end);
147 if (token == "/_") continue;
149 for(string::iterator t = token.begin(); t != token.end(); t++) {
150 // canonicalize the token, to match what's in the index
151 *t = (*t == '-') ? '_' : tolower(*t);
153 SG_LOG(SG_ATC, SG_DEBUG, "voice synth: token: '"
156 atc_word_map_const_iterator wordIt = wordMap.find(token);
157 if(wordIt == wordMap.end()) {
158 // Oh dear - the token isn't in the sound file
159 SG_LOG(SG_ATC, SG_ALERT, "voice synth: word '"
160 << token << "' not found");
162 const WordData& word = wordIt->second;
164 * Sanity check for corrupt/mismatched sound data input - avoids a seg fault
165 * (As long as the calling function checks the return value!!)
166 * This check should be left in even when the default Flightgear files are known
167 * to be OK since it checks for mis-indexing of voice files by 3rd party developers.
169 if((word.offset + word.length) > rawDataSize) {
170 SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
171 SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << word.offset + word.length
172 << " exceeds rawdata size: " << rawDataSize << endl);
177 sound.insert(sound.end(), rawSoundData + word.offset, rawSoundData + word.offset + word.length);
181 // Check for no tokens found else slScheduler can be crashed
187 char* data = (char*)malloc(*len);
189 SG_LOG(SG_ATC, SG_ALERT, "ERROR - could not allocate " << *len << " bytes of memory for ATIS sound\n");
194 // randomize start position
195 unsigned int offsetIn = (unsigned int)(*len * sg_random());
196 if (offsetIn > 0 && offsetIn < *len) {
197 copy(sound.begin() + offsetIn, sound.end(), data);
198 copy(sound.begin(), sound.begin() + offsetIn, data + *len - offsetIn);
200 copy(sound.begin(), sound.end(), data);