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., 675 Mass Ave, Cambridge, MA 02139, 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() {
39 FGATCVoice::~FGATCVoice() {
43 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
44 // Return true if successful.
45 bool FGATCVoice::LoadVoice(string voice) {
48 SGPath path = globals->get_fg_root();
49 string soundPath = "ATC/" + voice + ".wav";
50 path.append(soundPath);
52 SoundData = new slSample( (char*)path.c_str() );
53 rawDataSize = SoundData->getLength();
54 rawSoundData = (char*)SoundData->getBuffer();
56 path = globals->get_fg_root();
57 string wordPath = "ATC/" + voice + ".vce";
58 path.append(wordPath);
60 // Now load the word data
61 fin.open(path.c_str(), ios::in);
63 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
66 SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
70 char wrdOffsetStr[20];
71 char wrdLengthStr[20];
72 unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
73 unsigned int wrdLength; // Length of the word sample in bytes
76 unsigned int numwords = atoi(numwds);
77 //cout << numwords << '\n';
78 for(unsigned int i=0; i < numwords; ++i) {
83 wrdOffset = atoi(wrdOffsetStr);
84 wrdLength = atoi(wrdLengthStr);
85 wd.offset = wrdOffset;
86 wd.length = wrdLength;
88 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
97 typedef list < string > tokenList_type;
98 typedef tokenList_type::iterator tokenList_iterator;
100 // Given a desired message, return a pointer to the data buffer and write the buffer length into len.
101 unsigned char* FGATCVoice::WriteMessage(char* message, int& len, bool& dataOK) {
103 // What should we do here?
104 // First - parse the message into a list of tokens.
105 // Sort the tokens into those we understand and those we don't.
106 // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
107 tokenList_type tokenList;
108 tokenList_iterator tokenListItr;
110 // TODO - at the moment we're effectively taking 3 passes through the data.
111 // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
115 strcpy(mes, message);
116 const char delimiters[] = " \t.,;:\"";
117 token = strtok(mes, delimiters);
118 while(token != NULL) {
119 tokenList.push_back(token);
121 //cout << "token = " << token << '\n';
122 token = strtok(NULL, delimiters);
125 WordData* wdptr = new WordData[numWords];
127 unsigned int cumLength = 0;
129 tokenListItr = tokenList.begin();
130 while(tokenListItr != tokenList.end()) {
131 if(wordMap.find(*tokenListItr) == wordMap.end()) {
132 // Oh dear - the token isn't in the sound file
133 //cout << "word " << *tokenListItr << " not found :-(\n";
135 wdptr[word] = wordMap[*tokenListItr];
136 cumLength += wdptr[word].length;
137 //cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;
143 // Check for no tokens found else slScheduler can be crashed
149 unsigned char* tmpbuf = new unsigned char[cumLength];
150 unsigned char* outbuf = new unsigned char[cumLength];
152 unsigned int bufpos = 0;
153 for(int i=0; i<word; ++i) {
155 * Sanity check for corrupt/mismatched sound data input - avoids a seg fault
156 * (As long as the calling function checks the return value!!)
157 * This check should be left in even when the default Flightgear files are known
158 * to be OK since it checks for mis-indexing of voice files by 3rd party developers.
160 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
161 SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
162 SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
163 << " exceeds rawdata size: " << rawDataSize << endl);
168 memcpy(tmpbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
169 bufpos += wdptr[i].length;
172 // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
173 unsigned int offsetIn = (int)(cumLength * sg_random());
174 if(offsetIn > cumLength) offsetIn = cumLength;
175 memcpy(outbuf, tmpbuf + offsetIn, (cumLength - offsetIn));
176 memcpy(outbuf + (cumLength - offsetIn), tmpbuf, offsetIn);