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 <Main/globals.hxx>
33 #include "ATCVoice.hxx"
37 FGATCVoice::FGATCVoice() {
40 FGATCVoice::~FGATCVoice() {
44 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
45 // Return true if successful.
46 bool FGATCVoice::LoadVoice(string voice) {
49 SGPath path = globals->get_fg_root();
50 string soundPath = "ATC/" + voice + ".wav";
51 path.append(soundPath);
53 SoundData = new slSample( (char*)path.c_str() );
54 rawDataSize = SoundData->getLength();
55 rawSoundData = (char*)SoundData->getBuffer();
57 path = globals->get_fg_root();
58 string wordPath = "ATC/" + voice + ".vce";
59 path.append(wordPath);
61 // Now load the word data
62 fin.open(path.c_str(), ios::in);
64 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to open input file " << path.c_str() << '\n');
67 cout << "Opened word data file " << wordPath << " OK...\n";
71 char wrdOffsetStr[20];
72 char wrdLengthStr[20];
73 unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
74 unsigned int wrdLength; // Length of the word sample in bytes
77 unsigned int numwords = atoi(numwds);
78 //cout << numwords << '\n';
79 for(unsigned int i=0; i < numwords; ++i) {
84 wrdOffset = atoi(wrdOffsetStr);
85 wrdLength = atoi(wrdLengthStr);
86 wd.offset = wrdOffset;
87 wd.length = wrdLength;
89 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
98 // Given a desired message, return a pointer to the data buffer and write the buffer length into len.
99 unsigned char* FGATCVoice::WriteMessage(char* message, int& len, bool& dataOK) {
101 // What should we do here?
102 // First - parse the message into a list of tokens.
103 // Sort the tokens into those we understand and those we don't.
104 // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
105 list < string > tokenList;
106 list < string >::iterator tokenListItr;
108 // TODO - at the moment we're effectively taking 3 passes through the data.
109 // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
113 strcpy(mes, message);
114 const char delimiters[] = " \t.,;:\"";
115 token = strtok(mes, delimiters);
116 while(token != NULL) {
117 tokenList.push_back(token);
119 //cout << "token = " << token << '\n';
120 token = strtok(NULL, delimiters);
123 WordData* wdptr = new WordData[numWords];
125 unsigned int cumLength = 0;
127 tokenListItr = tokenList.begin();
128 while(tokenListItr != tokenList.end()) {
129 if(wordMap.find(*tokenListItr) == wordMap.end()) {
130 // Oh dear - the token isn't in the sound file
131 //cout << "word " << *tokenListItr << " not found :-(\n";
133 wdptr[word] = wordMap[*tokenListItr];
134 cumLength += wdptr[word].length;
135 //cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;
141 // Check for no tokens found else slScheduler can be crashed
147 unsigned char* outbuf = new unsigned char[cumLength];
149 unsigned int bufpos = 0;
150 for(int i=0; i<word; ++i) {
152 * Sanity check for corrupt/mismatched sound data input - avoids a seg fault
153 * (As long as the calling function checks the return value!!)
154 * This check should be left in even when the default Flightgear files are known
155 * to be OK since it checks for mis-indexing of voice files by 3rd party developers.
157 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
158 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
159 SG_LOG(SG_GENERAL, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
160 << " exceeds rawdata size: " << rawDataSize << endl);
165 memcpy(outbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
166 bufpos += wdptr[i].length;