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"
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/sgstream.hxx>
36 #include <simgear/math/sg_random.h>
37 #include <simgear/sound/sample_openal.hxx>
39 #include <Main/globals.hxx>
41 FGATCVoice::FGATCVoice() {
46 FGATCVoice::~FGATCVoice() {
52 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
53 // Return true if successful.
54 bool FGATCVoice::LoadVoice(const string& voice) {
55 // FIXME CLO: disabled to try to see if this is causing problemcs
60 SGPath path = globals->get_fg_root();
63 string file = voice + ".wav";
65 SGSoundSample SoundData;
66 rawSoundData = (char *)SoundData.load_file(path.c_str(), file.c_str());
67 rawDataSize = SoundData.get_size();
69 ALenum fmt = SoundData.get_format();
70 cout << "ATCVoice: format: " << fmt
71 << " size: " << rawDataSize << endl;
73 path = globals->get_fg_root();
74 string wordPath = "ATC/" + voice + ".vce";
75 path.append(wordPath);
77 // Now load the word data
78 fin.open(path.c_str(), ios::in);
80 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
83 SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
87 char wrdOffsetStr[20];
88 char wrdLengthStr[20];
89 unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
90 unsigned int wrdLength; // Length of the word sample in bytes
93 unsigned int numwords = atoi(numwds);
94 //cout << numwords << '\n';
95 for(unsigned int i=0; i < numwords; ++i) {
100 wrdOffset = atoi(wrdOffsetStr);
101 wrdLength = atoi(wrdLengthStr);
102 wd.offset = wrdOffset;
103 wd.length = wrdLength;
104 wordMap[wrdstr] = wd;
106 for(string::iterator p = ws2.begin(); p != ws2.end(); p++){
108 if (*p == '-') *p = '_';
110 if (wrdstr != ws2) wordMap[ws2] = wd;
112 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
121 typedef list < string > tokenList_type;
122 typedef tokenList_type::iterator tokenList_iterator;
124 // Given a desired message, return a string containing the
126 string FGATCVoice::WriteMessage(const char* message, bool& dataOK) {
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.
132 tokenList_type tokenList;
133 tokenList_iterator tokenListItr;
135 // TODO - at the moment we're effectively taking 3 passes through the data.
136 // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
137 size_t n1 = 1+strlen(message);
139 strncpy(msg, message, n1); // strtok requires a non-const char*
142 const char delimiters[] = " \t.,;:\"\n";
144 token = strtok_r(msg, delimiters, &context);
145 while(token != NULL) {
146 for (char *t = token; *t; t++) {
147 *t = tolower(*t); // canonicalize the case, to
148 if (*t == '-') *t = '_'; // match what's in the index
150 tokenList.push_back(token);
152 SG_LOG(SG_ATC, SG_DEBUG, "voice synth: token: '"
154 token = strtok_r(NULL, delimiters, &context);
157 WordData wdptr[numWords];
159 unsigned int cumLength = 0;
161 tokenListItr = tokenList.begin();
162 while(tokenListItr != tokenList.end()) {
163 if(wordMap.find(*tokenListItr) == wordMap.end()) {
164 // Oh dear - the token isn't in the sound file
165 SG_LOG(SG_ATC, SG_ALERT, "voice synth: word '"
166 << *tokenListItr << "' not found");
168 wdptr[word] = wordMap[*tokenListItr];
169 cumLength += wdptr[word].length;
170 //cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;
176 // Check for no tokens found else slScheduler can be crashed
182 char tmpbuf[cumLength];
183 unsigned int bufpos = 0;
184 for(int i=0; i<word; ++i) {
186 * Sanity check for corrupt/mismatched sound data input - avoids a seg fault
187 * (As long as the calling function checks the return value!!)
188 * This check should be left in even when the default Flightgear files are known
189 * to be OK since it checks for mis-indexing of voice files by 3rd party developers.
191 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
192 SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
193 SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
194 << " exceeds rawdata size: " << rawDataSize << endl);
199 memcpy(tmpbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
200 bufpos += wdptr[i].length;
203 // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
204 unsigned int offsetIn = (int)(cumLength * sg_random());
205 if(offsetIn > cumLength) offsetIn = cumLength;
207 string front(tmpbuf, offsetIn);
208 string back(tmpbuf+offsetIn, cumLength - offsetIn);