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 <Main/globals.hxx>
31 #include "ATCVoice.hxx"
35 FGATCVoice::FGATCVoice() {
38 FGATCVoice::~FGATCVoice() {
39 delete[] rawSoundData;
42 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
43 // Return true if successful.
44 bool FGATCVoice::LoadVoice(string voice) {
47 SGPath path = globals->get_fg_root();
48 string soundPath = "ATC/" + voice + ".wav";
49 path.append(soundPath);
51 // Input data parameters - some of these might need to be class variables eventually
52 // but at the moment we're just using them to find the header size to get the start
53 // of the data properly.
55 unsigned int chunkSize;
56 char junk[100]; // WARNING - Assumes all non-data chunk sizes are < 100
58 // do the sound data first
59 SG_LOG(SG_GENERAL, SG_INFO, "Trying to open voice input...");
60 fin.open(path.c_str(), ios::in|ios::binary);
62 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to open input file " << path.c_str());
65 cout << "Opened voice input file " << soundPath << " OK...\n";
66 // Strip the initial headers and ignore.
67 // Note that this assumes we know the sound format etc - fix this eventually
68 // (I've assumed sample-rate = 8000, bits = 8, mono, which is what the other FGFS sound samples seem to use.
69 // The file should always start with the 12 byte RIFF header
71 // TODO - Should we check that the above == "RIFF" ?
72 // read and discard the next 8 bytes
74 // Now it gets more complicated - although the format chunk is normally before the data chunk,
75 // this is not guaranteed, and there may be a fact chunk as well. (And possibly more that I haven't heard of!).
79 //cout << "sizeof(chunkID) = " << sizeof(chunkID) << '\n';
80 //cout << "chunkID = " << chunkID << '\n';
81 if(!strcmp(chunkID, "data")) {
83 } else if((!strcmp(chunkID, "fmt ")) || (!strcmp(chunkID, "fact"))) {
84 fin.read((char*)&chunkSize, sizeof(chunkSize));
85 // Chunksizes must be word-aligned (ie every 2 bytes), but the given chunk size
86 // is not guaranteed to be word-aligned, and there may be an extra padding byte.
87 // Add 1 to chunkSize if it's odd.
88 // Well, it is a microsoft file format!!!
89 chunkSize += (chunkSize % 2);
90 fin.read(junk, chunkSize);
92 // Oh dear - its all gone pear-shaped - abort :-(
93 SG_LOG(SG_GENERAL, SG_ALERT, "Unknown chunk ID in input wave file in ATCVoice.cxx... aborting voice ATC load");
99 fin.read((char*)&rawDataSize, sizeof(rawDataSize));
100 //cout << "rawDataSize = " << rawDataSize << endl;
101 rawSoundData = new char[rawDataSize];
102 fin.read(rawSoundData, rawDataSize);
105 path = globals->get_fg_root();
106 string wordPath = "ATC/" + voice + ".vce";
107 path.append(wordPath);
109 // Now load the word data
110 fin.open(path.c_str(), ios::in);
112 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to open input file " << path.c_str() << '\n');
115 cout << "Opened word data file " << wordPath << " OK...\n";
119 char wrdOffsetStr[20];
120 char wrdLengthStr[20];
121 unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
122 unsigned int wrdLength; // Length of the word sample in bytes
125 unsigned int numwords = atoi(numwds);
126 //cout << numwords << '\n';
127 for(unsigned int i=0; i < numwords; ++i) {
132 wrdOffset = atoi(wrdOffsetStr);
133 wrdLength = atoi(wrdLengthStr);
134 wd.offset = wrdOffset;
135 wd.length = wrdLength;
136 wordMap[wrdstr] = wd;
137 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
146 // Given a desired message, return a pointer to the data buffer and write the buffer length into len.
147 unsigned char* FGATCVoice::WriteMessage(char* message, int& len, bool& dataOK) {
149 // What should we do here?
150 // First - parse the message into a list of tokens.
151 // Sort the tokens into those we understand and those we don't.
152 // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
153 list < string > tokenList;
154 list < string >::iterator tokenListItr;
156 // TODO - at the moment we're effectively taking 3 passes through the data.
157 // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
161 strcpy(mes, message);
162 const char delimiters[] = " \t.,;:\"";
163 token = strtok(mes, delimiters);
164 while(token != NULL) {
165 tokenList.push_back(token);
167 //cout << "token = " << token << '\n';
168 token = strtok(NULL, delimiters);
171 WordData* wdptr = new WordData[numWords];
173 unsigned int cumLength = 0;
175 tokenListItr = tokenList.begin();
176 while(tokenListItr != tokenList.end()) {
177 if(wordMap.find(*tokenListItr) == wordMap.end()) {
178 // Oh dear - the token isn't in the sound file
179 //cout << "word " << *tokenListItr << " not found :-(\n";
181 wdptr[word] = wordMap[*tokenListItr];
182 cumLength += wdptr[word].length;
183 //cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;
189 // Check for no tokens found else slScheduler can be crashed
195 unsigned char* outbuf = new unsigned char[cumLength];
197 unsigned int bufpos = 0;
198 for(int i=0; i<word; ++i) {
200 * Sanity check for corrupt/mismatched sound data input - avoids a seg fault
201 * (As long as the calling function checks the return value!!)
202 * This check should be left in even when the default Flightgear files are known
203 * to be OK since it checks for mis-indexing of voice files by 3rd party developers.
205 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
206 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
207 SG_LOG(SG_GENERAL, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
208 << " exceeds rawdata size: " << rawDataSize << endl);
211 // I suppose we have to return something
214 memcpy(outbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
215 bufpos += wdptr[i].length;