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.
28 #include <simgear/misc/sg_path.hxx>
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/misc/sgstream.hxx>
31 #include <simgear/math/sg_random.h>
32 #include <Main/globals.hxx>
34 #include "ATCVoice.hxx"
38 FGATCVoice::FGATCVoice() {
41 FGATCVoice::~FGATCVoice() {
45 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
46 // Return true if successful.
47 bool FGATCVoice::LoadVoice(string voice) {
50 SGPath path = globals->get_fg_root();
51 string soundPath = "ATC/" + voice + ".wav";
52 path.append(soundPath);
54 SoundData = new slSample( (char*)path.c_str() );
55 rawDataSize = SoundData->getLength();
56 rawSoundData = (char*)SoundData->getBuffer();
58 path = globals->get_fg_root();
59 string wordPath = "ATC/" + voice + ".vce";
60 path.append(wordPath);
62 // Now load the word data
63 fin.open(path.c_str(), ios::in);
65 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to open input file " << path.c_str() << '\n');
68 cout << "Opened word data file " << wordPath << " OK...\n";
72 char wrdOffsetStr[20];
73 char wrdLengthStr[20];
74 unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
75 unsigned int wrdLength; // Length of the word sample in bytes
78 unsigned int numwords = atoi(numwds);
79 //cout << numwords << '\n';
80 for(unsigned int i=0; i < numwords; ++i) {
85 wrdOffset = atoi(wrdOffsetStr);
86 wrdLength = atoi(wrdLengthStr);
87 wd.offset = wrdOffset;
88 wd.length = wrdLength;
90 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
99 typedef list < string > tokenList_type;
100 typedef tokenList_type::iterator tokenList_iterator;
102 // Given a desired message, return a pointer to the data buffer and write the buffer length into len.
103 unsigned char* FGATCVoice::WriteMessage(char* message, int& len, bool& dataOK) {
105 // What should we do here?
106 // First - parse the message into a list of tokens.
107 // Sort the tokens into those we understand and those we don't.
108 // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
109 tokenList_type tokenList;
110 tokenList_iterator tokenListItr;
112 // TODO - at the moment we're effectively taking 3 passes through the data.
113 // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
117 strcpy(mes, message);
118 const char delimiters[] = " \t.,;:\"";
119 token = strtok(mes, delimiters);
120 while(token != NULL) {
121 tokenList.push_back(token);
123 //cout << "token = " << token << '\n';
124 token = strtok(NULL, delimiters);
127 WordData* wdptr = new WordData[numWords];
129 unsigned int cumLength = 0;
131 tokenListItr = tokenList.begin();
132 while(tokenListItr != tokenList.end()) {
133 if(wordMap.find(*tokenListItr) == wordMap.end()) {
134 // Oh dear - the token isn't in the sound file
135 //cout << "word " << *tokenListItr << " not found :-(\n";
137 wdptr[word] = wordMap[*tokenListItr];
138 cumLength += wdptr[word].length;
139 //cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;
145 // Check for no tokens found else slScheduler can be crashed
151 unsigned char* tmpbuf = new unsigned char[cumLength];
152 unsigned char* outbuf = new unsigned char[cumLength];
154 unsigned int bufpos = 0;
155 for(int i=0; i<word; ++i) {
157 * Sanity check for corrupt/mismatched sound data input - avoids a seg fault
158 * (As long as the calling function checks the return value!!)
159 * This check should be left in even when the default Flightgear files are known
160 * to be OK since it checks for mis-indexing of voice files by 3rd party developers.
162 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
163 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
164 SG_LOG(SG_GENERAL, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
165 << " exceeds rawdata size: " << rawDataSize << endl);
170 memcpy(tmpbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
171 bufpos += wdptr[i].length;
174 // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
175 unsigned int offsetIn = (int)(cumLength * sg_random());
176 if(offsetIn > cumLength) offsetIn = cumLength;
177 memcpy(outbuf, tmpbuf + offsetIn, (cumLength - offsetIn));
178 memcpy(outbuf + (cumLength - offsetIn), tmpbuf, offsetIn);