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