]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCVoice.cxx
Slight change to some STL stuff that was breaking MSVC6
[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 <plib/sl.h>
27
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>
33
34 #include "ATCVoice.hxx"
35
36 #include <stdlib.h>
37
38 FGATCVoice::FGATCVoice() {
39 }
40
41 FGATCVoice::~FGATCVoice() {
42         delete SoundData;
43 }
44
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) {
48         ifstream fin;
49
50         SGPath path = globals->get_fg_root();
51         string soundPath = "ATC/" + voice + ".wav";
52         path.append(soundPath);
53         
54         SoundData = new slSample( (char*)path.c_str() );
55         rawDataSize = SoundData->getLength();
56         rawSoundData = (char*)SoundData->getBuffer();
57         
58         path = globals->get_fg_root();
59         string wordPath = "ATC/" + voice + ".vce";
60         path.append(wordPath);
61         
62         // Now load the word data
63         fin.open(path.c_str(), ios::in);
64         if(!fin) {
65                 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to open input file " << path.c_str() << '\n');
66                 return(false);
67         }
68         cout << "Opened word data file " << wordPath << " OK...\n";
69         char numwds[10];
70         char wrd[100];
71         string wrdstr;
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
76         WordData wd;
77         fin >> numwds;
78         unsigned int numwords = atoi(numwds);
79         //cout << numwords << '\n';
80         for(unsigned int i=0; i < numwords; ++i) {
81                 fin >> wrd;
82                 wrdstr = wrd;
83                 fin >> wrdOffsetStr;
84                 fin >> wrdLengthStr;
85                 wrdOffset = atoi(wrdOffsetStr);
86                 wrdLength = atoi(wrdLengthStr);
87                 wd.offset = wrdOffset;
88                 wd.length = wrdLength;
89                 wordMap[wrdstr] = wd;
90                 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
91                 //cout << i << '\n';
92         }
93         
94         fin.close();
95         return(true);
96 }
97
98
99 typedef list < string > tokenList_type;
100 typedef tokenList_type::iterator tokenList_iterator;
101
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) {
104         
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;
111
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.
114         char* token;
115         char mes[1000];
116         int numWords = 0;
117         strcpy(mes, message);
118         const char delimiters[] = " \t.,;:\"";
119         token = strtok(mes, delimiters);
120         while(token != NULL) {
121                 tokenList.push_back(token);
122                 ++numWords;
123                 //cout << "token = " << token << '\n';
124                 token = strtok(NULL, delimiters);
125         }
126
127         WordData* wdptr = new WordData[numWords];
128         int word = 0;
129         unsigned int cumLength = 0;
130
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";
136                 } else {
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;  
140                         word++;
141                 }
142                 ++tokenListItr;
143         }
144
145         // Check for no tokens found else slScheduler can be crashed
146         if(!word) {
147                 dataOK = false;
148                 return(NULL);
149         }
150
151         unsigned char* tmpbuf = new unsigned char[cumLength];   
152         unsigned char* outbuf = new unsigned char[cumLength];
153         len = cumLength;
154         unsigned int bufpos = 0;
155         for(int i=0; i<word; ++i) {
156                 /*
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.
161                 */
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);
166                         delete[] wdptr;
167                         dataOK = false;
168                         return(NULL);
169                 }
170                 memcpy(tmpbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
171                 bufpos += wdptr[i].length;
172         }
173         
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);
179         
180         delete[] tmpbuf;
181         delete[] wdptr;
182
183         dataOK = true;  
184         return(outbuf);
185 }