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 <simgear/misc/sg_path.hxx>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/sgstream.hxx>
29 #include <simgear/math/sg_random.h>
30 #include <Main/globals.hxx>
32 #include "ATCVoice.hxx"
36 FGATCVoice::FGATCVoice() {
41 FGATCVoice::~FGATCVoice() {
47 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
48 // Return true if successful.
49 bool FGATCVoice::LoadVoice(const string& voice) {
50 // FIXME CLO: disabled to try to see if this is causign problemcs
55 SGPath path = globals->get_fg_root();
58 string file = voice + ".wav";
60 SoundData = new SGSoundSample();
61 rawSoundData = (char *)SoundData->load_file(path.c_str(), file.c_str());
62 rawDataSize = SoundData->get_size();
64 path = globals->get_fg_root();
65 string wordPath = "ATC/" + voice + ".vce";
66 path.append(wordPath);
68 // Now load the word data
69 fin.open(path.c_str(), ios::in);
71 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
74 SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
78 char wrdOffsetStr[20];
79 char wrdLengthStr[20];
80 unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
81 unsigned int wrdLength; // Length of the word sample in bytes
84 unsigned int numwords = atoi(numwds);
85 //cout << numwords << '\n';
86 for(unsigned int i=0; i < numwords; ++i) {
91 wrdOffset = atoi(wrdOffsetStr);
92 wrdLength = atoi(wrdLengthStr);
93 wd.offset = wrdOffset;
94 wd.length = wrdLength;
96 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
105 typedef list < string > tokenList_type;
106 typedef tokenList_type::iterator tokenList_iterator;
108 // Given a desired message, return a pointer to the data buffer and write the buffer length into len.
109 unsigned char* FGATCVoice::WriteMessage(char* message, int& len, bool& dataOK) {
111 // What should we do here?
112 // First - parse the message into a list of tokens.
113 // Sort the tokens into those we understand and those we don't.
114 // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
115 tokenList_type tokenList;
116 tokenList_iterator tokenListItr;
118 // TODO - at the moment we're effectively taking 3 passes through the data.
119 // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
123 strcpy(mes, message);
124 const char delimiters[] = " \t.,;:\"";
125 token = strtok(mes, delimiters);
126 while(token != NULL) {
127 tokenList.push_back(token);
129 //cout << "token = " << token << '\n';
130 token = strtok(NULL, delimiters);
133 WordData* wdptr = new WordData[numWords];
135 unsigned int cumLength = 0;
137 tokenListItr = tokenList.begin();
138 while(tokenListItr != tokenList.end()) {
139 if(wordMap.find(*tokenListItr) == wordMap.end()) {
140 // Oh dear - the token isn't in the sound file
141 //cout << "word " << *tokenListItr << " not found :-(\n";
143 wdptr[word] = wordMap[*tokenListItr];
144 cumLength += wdptr[word].length;
145 //cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;
151 // Check for no tokens found else slScheduler can be crashed
158 unsigned char* tmpbuf = new unsigned char[cumLength];
159 unsigned char* outbuf = new unsigned char[cumLength];
161 unsigned int bufpos = 0;
162 for(int i=0; i<word; ++i) {
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((wdptr[i].offset + wdptr[i].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: " << wdptr[i].offset + wdptr[i].length
172 << " exceeds rawdata size: " << rawDataSize << endl);
179 memcpy(tmpbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
180 bufpos += wdptr[i].length;
183 // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
184 unsigned int offsetIn = (int)(cumLength * sg_random());
185 if(offsetIn > cumLength) offsetIn = cumLength;
186 memcpy(outbuf, tmpbuf + offsetIn, (cumLength - offsetIn));
187 memcpy(outbuf + (cumLength - offsetIn), tmpbuf, offsetIn);