]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCVoice.cxx
Initial draft of a canned ATC voice handling class
[flightgear.git] / src / ATC / ATCVoice.cxx
1 // FGATCVoice.cxx - a class to encapsulate an ATC voice
2 //
3 // Written by David Luff, started November 2002.
4 //
5 // Copyright (C) 2002  David C Luff - david.luff@nottingham.ac.uk
6 //
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.
11 //
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.
16 //
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.
20
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include <simgear/misc/sg_path.hxx>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/sgstream.hxx>
29 #include <Main/globals.hxx>
30
31 #include "ATCVoice.hxx"
32
33 #include <stdlib.h>
34
35 FGATCVoice::FGATCVoice() {
36 }
37
38 FGATCVoice::~FGATCVoice() {
39         delete[] rawSoundData;
40 }
41
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) {
45         ifstream fin;
46
47         SGPath path = globals->get_fg_root();
48         string soundPath = "ATC/" + voice + ".wav";
49         path.append(soundPath);
50         
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.
54         char chunkID[5];
55         unsigned int chunkSize;
56         char junk[100];         // WARNING - Assumes all non-data chunk sizes are < 100
57         
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);
61         if(!fin) {
62                 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to open input file " << path.c_str());
63                 return(false);
64         }
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
70         fin.read(chunkID, 4);
71         // TODO - Should we check that the above == "RIFF" ?
72         // read and discard the next 8 bytes
73         fin.read(junk, 8);
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!).
76         while(1) {
77                 fin.read(chunkID, 4);
78                 chunkID[4] = '\0';
79                 //cout << "sizeof(chunkID) = " << sizeof(chunkID) << '\n';
80                 //cout << "chunkID = " << chunkID << '\n';
81                 if(!strcmp(chunkID, "data")) {
82                         break;
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);
91                 } else {
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");
94                         fin.close();
95                         return(false);
96                 }
97         }
98
99         fin.read((char*)&rawDataSize, sizeof(rawDataSize));
100         //cout << "rawDataSize = " << rawDataSize << endl;
101         rawSoundData = new char[rawDataSize];
102         fin.read(rawSoundData, rawDataSize);
103         fin.close();
104
105         path = globals->get_fg_root();
106         string wordPath = "ATC/" + voice + ".vce";
107         path.append(wordPath);
108         
109         // Now load the word data
110         fin.open(path.c_str(), ios::in);
111         if(!fin) {
112                 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to open input file " << path.c_str() << '\n');
113                 return(false);
114         }
115         cout << "Opened word data file " << wordPath << " OK...\n";
116         char numwds[10];
117         char wrd[100];
118         string wrdstr;
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
123         WordData wd;
124         fin >> numwds;
125         unsigned int numwords = atoi(numwds);
126         //cout << numwords << '\n';
127         for(unsigned int i=0; i < numwords; ++i) {
128                 fin >> wrd;
129                 wrdstr = wrd;
130                 fin >> wrdOffsetStr;
131                 fin >> wrdLengthStr;
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';
138                 //cout << i << '\n';
139         }
140         
141         fin.close();
142         return(true);
143 }
144
145
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) {
148         
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;
155
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.
158         char* token;
159         char mes[1000];
160         int numWords = 0;
161         strcpy(mes, message);
162         const char delimiters[] = " \t.,;:\"";
163         token = strtok(mes, delimiters);
164         while(token != NULL) {
165                 tokenList.push_back(token);
166                 ++numWords;
167                 //cout << "token = " << token << '\n';
168                 token = strtok(NULL, delimiters);
169         }
170
171         WordData* wdptr = new WordData[numWords];
172         int word = 0;
173         unsigned int cumLength = 0;
174
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";
180                 } else {
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;  
184                         word++;
185                 }
186                 ++tokenListItr;
187         }
188
189         // Check for no tokens found else slScheduler can be crashed
190         if(!word) {
191                 dataOK = false;
192                 return(NULL);
193         }
194         
195         unsigned char* outbuf = new unsigned char[cumLength];
196         len = cumLength;
197         unsigned int bufpos = 0;
198         for(int i=0; i<word; ++i) {
199                 /*
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.
204                 */
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);
209                         delete[] wdptr;
210                         dataOK = false;
211                         // I suppose we have to return something
212                         return(NULL);
213                 }
214                 memcpy(outbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
215                 bufpos += wdptr[i].length;
216         }
217         
218         delete[] wdptr;
219
220         dataOK = true;  
221         return(outbuf);
222 }