]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCVoice.cxx
Fix problem with cout
[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 <Main/globals.hxx>
32
33 #include "ATCVoice.hxx"
34
35 #include <stdlib.h>
36
37 FGATCVoice::FGATCVoice() {
38 }
39
40 FGATCVoice::~FGATCVoice() {
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(string voice) {
47         ifstream fin;
48
49         SGPath path = globals->get_fg_root();
50         string soundPath = "ATC/" + voice + ".wav";
51         path.append(soundPath);
52         
53         SoundData = new slSample( (char*)path.c_str() );
54         rawDataSize = SoundData->getLength();
55         rawSoundData = (char*)SoundData->getBuffer();
56         
57         path = globals->get_fg_root();
58         string wordPath = "ATC/" + voice + ".vce";
59         path.append(wordPath);
60         
61         // Now load the word data
62         fin.open(path.c_str(), ios::in);
63         if(!fin) {
64                 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to open input file " << path.c_str() << '\n');
65                 return(false);
66         }
67         cout << "Opened word data file " << wordPath << " OK...\n";
68         char numwds[10];
69         char wrd[100];
70         string wrdstr;
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
75         WordData wd;
76         fin >> numwds;
77         unsigned int numwords = atoi(numwds);
78         //cout << numwords << '\n';
79         for(unsigned int i=0; i < numwords; ++i) {
80                 fin >> wrd;
81                 wrdstr = wrd;
82                 fin >> wrdOffsetStr;
83                 fin >> wrdLengthStr;
84                 wrdOffset = atoi(wrdOffsetStr);
85                 wrdLength = atoi(wrdLengthStr);
86                 wd.offset = wrdOffset;
87                 wd.length = wrdLength;
88                 wordMap[wrdstr] = wd;
89                 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
90                 //cout << i << '\n';
91         }
92         
93         fin.close();
94         return(true);
95 }
96
97
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) {
100         
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;
107
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.
110         char* token;
111         char mes[1000];
112         int numWords = 0;
113         strcpy(mes, message);
114         const char delimiters[] = " \t.,;:\"";
115         token = strtok(mes, delimiters);
116         while(token != NULL) {
117                 tokenList.push_back(token);
118                 ++numWords;
119                 //cout << "token = " << token << '\n';
120                 token = strtok(NULL, delimiters);
121         }
122
123         WordData* wdptr = new WordData[numWords];
124         int word = 0;
125         unsigned int cumLength = 0;
126
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";
132                 } else {
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;  
136                         word++;
137                 }
138                 ++tokenListItr;
139         }
140
141         // Check for no tokens found else slScheduler can be crashed
142         if(!word) {
143                 dataOK = false;
144                 return(NULL);
145         }
146         
147         unsigned char* outbuf = new unsigned char[cumLength];
148         len = cumLength;
149         unsigned int bufpos = 0;
150         for(int i=0; i<word; ++i) {
151                 /*
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.
156                 */
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);
161                         delete[] wdptr;
162                         dataOK = false;
163                         return(NULL);
164                 }
165                 memcpy(outbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
166                 bufpos += wdptr[i].length;
167         }
168         
169         delete[] wdptr;
170
171         dataOK = true;  
172         return(outbuf);
173 }