]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCVoice.cxx
Catch sound exceptions at the earliest, report problem has an alert, and continue...
[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 <simgear/math/sg_random.h>
30 #include <Main/globals.hxx>
31
32 #include "ATCVoice.hxx"
33
34 #include <stdlib.h>
35
36 FGATCVoice::FGATCVoice() {
37 }
38
39 FGATCVoice::~FGATCVoice() {
40     free( rawSoundData );
41     delete SoundData;
42 }
43
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(const string& voice) {
47     // FIXME CLO: disabled to try to see if this is causign problemcs
48     // return false;
49
50         ifstream fin;
51
52         SGPath path = globals->get_fg_root();
53         path.append( "ATC" );
54
55         string file = voice + ".wav";
56         
57         SoundData = new SGSoundSample();
58         rawSoundData = (char *)SoundData->load_file(path.c_str(), file.c_str());
59         rawDataSize = SoundData->get_size();
60         
61         path = globals->get_fg_root();
62         string wordPath = "ATC/" + voice + ".vce";
63         path.append(wordPath);
64         
65         // Now load the word data
66         fin.open(path.c_str(), ios::in);
67         if(!fin) {
68                 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
69                 return(false);
70         }
71         SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
72         char numwds[10];
73         char wrd[100];
74         string wrdstr;
75         char wrdOffsetStr[20];
76         char wrdLengthStr[20];
77         unsigned int wrdOffset;         // Offset into the raw sound data that the word sample begins
78         unsigned int wrdLength;         // Length of the word sample in bytes
79         WordData wd;
80         fin >> numwds;
81         unsigned int numwords = atoi(numwds);
82         //cout << numwords << '\n';
83         for(unsigned int i=0; i < numwords; ++i) {
84                 fin >> wrd;
85                 wrdstr = wrd;
86                 fin >> wrdOffsetStr;
87                 fin >> wrdLengthStr;
88                 wrdOffset = atoi(wrdOffsetStr);
89                 wrdLength = atoi(wrdLengthStr);
90                 wd.offset = wrdOffset;
91                 wd.length = wrdLength;
92                 wordMap[wrdstr] = wd;
93                 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
94                 //cout << i << '\n';
95         }
96         
97         fin.close();
98         return(true);
99 }
100
101
102 typedef list < string > tokenList_type;
103 typedef tokenList_type::iterator tokenList_iterator;
104
105 // Given a desired message, return a pointer to the data buffer and write the buffer length into len.
106 unsigned char* FGATCVoice::WriteMessage(char* message, int& len, bool& dataOK) {
107         
108         // What should we do here?
109         // First - parse the message into a list of tokens.
110         // Sort the tokens into those we understand and those we don't.
111         // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
112         tokenList_type tokenList;
113         tokenList_iterator tokenListItr;
114
115         // TODO - at the moment we're effectively taking 3 passes through the data.
116         // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
117         char* token;
118         char mes[1000];
119         int numWords = 0;
120         strcpy(mes, message);
121         const char delimiters[] = " \t.,;:\"";
122         token = strtok(mes, delimiters);
123         while(token != NULL) {
124                 tokenList.push_back(token);
125                 ++numWords;
126                 //cout << "token = " << token << '\n';
127                 token = strtok(NULL, delimiters);
128         }
129
130         WordData* wdptr = new WordData[numWords];
131         int word = 0;
132         unsigned int cumLength = 0;
133
134         tokenListItr = tokenList.begin();
135         while(tokenListItr != tokenList.end()) {
136                 if(wordMap.find(*tokenListItr) == wordMap.end()) {
137                         // Oh dear - the token isn't in the sound file
138                         //cout << "word " << *tokenListItr << " not found :-(\n";
139                 } else {
140                         wdptr[word] = wordMap[*tokenListItr];
141                         cumLength += wdptr[word].length;
142                         //cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;  
143                         word++;
144                 }
145                 ++tokenListItr;
146         }
147
148         // Check for no tokens found else slScheduler can be crashed
149         if(!word) {
150                 dataOK = false;
151                 return(NULL);
152         }
153
154         unsigned char* tmpbuf = new unsigned char[cumLength];   
155         unsigned char* outbuf = new unsigned char[cumLength];
156         len = cumLength;
157         unsigned int bufpos = 0;
158         for(int i=0; i<word; ++i) {
159                 /*
160                 *  Sanity check for corrupt/mismatched sound data input - avoids a seg fault
161                 *  (As long as the calling function checks the return value!!)
162                 *  This check should be left in even when the default Flightgear files are known
163                 *  to be OK since it checks for mis-indexing of voice files by 3rd party developers.
164                 */
165                 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
166                         SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
167                         SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
168                              << " exceeds rawdata size: " << rawDataSize << endl);
169                         delete[] wdptr;
170                         dataOK = false;
171                         return(NULL);
172                 }
173                 memcpy(tmpbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
174                 bufpos += wdptr[i].length;
175         }
176         
177         // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
178         unsigned int offsetIn = (int)(cumLength * sg_random());
179         if(offsetIn > cumLength) offsetIn = cumLength;
180         memcpy(outbuf, tmpbuf + offsetIn, (cumLength - offsetIn));
181         memcpy(outbuf + (cumLength - offsetIn), tmpbuf, offsetIn);
182         
183         delete[] tmpbuf;
184         delete[] wdptr;
185
186         dataOK = true;  
187         return(outbuf);
188 }