]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCVoice.cxx
Start the ATIS recorded message at a random position into the broadcast when the...
[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 // Given a desired message, return a pointer to the data buffer and write the buffer length into len.
100 unsigned char* FGATCVoice::WriteMessage(char* message, int& len, bool& dataOK) {
101         
102         // What should we do here?
103         // First - parse the message into a list of tokens.
104         // Sort the tokens into those we understand and those we don't.
105         // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
106         list < string > tokenList;
107         list < string >::iterator tokenListItr;
108
109         // TODO - at the moment we're effectively taking 3 passes through the data.
110         // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
111         char* token;
112         char mes[1000];
113         int numWords = 0;
114         strcpy(mes, message);
115         const char delimiters[] = " \t.,;:\"";
116         token = strtok(mes, delimiters);
117         while(token != NULL) {
118                 tokenList.push_back(token);
119                 ++numWords;
120                 //cout << "token = " << token << '\n';
121                 token = strtok(NULL, delimiters);
122         }
123
124         WordData* wdptr = new WordData[numWords];
125         int word = 0;
126         unsigned int cumLength = 0;
127
128         tokenListItr = tokenList.begin();
129         while(tokenListItr != tokenList.end()) {
130                 if(wordMap.find(*tokenListItr) == wordMap.end()) {
131                         // Oh dear - the token isn't in the sound file
132                         //cout << "word " << *tokenListItr << " not found :-(\n";
133                 } else {
134                         wdptr[word] = wordMap[*tokenListItr];
135                         cumLength += wdptr[word].length;
136                         //cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;  
137                         word++;
138                 }
139                 ++tokenListItr;
140         }
141
142         // Check for no tokens found else slScheduler can be crashed
143         if(!word) {
144                 dataOK = false;
145                 return(NULL);
146         }
147
148         unsigned char* tmpbuf = new unsigned char[cumLength];   
149         unsigned char* outbuf = new unsigned char[cumLength];
150         len = cumLength;
151         unsigned int bufpos = 0;
152         for(int i=0; i<word; ++i) {
153                 /*
154                 *  Sanity check for corrupt/mismatched sound data input - avoids a seg fault
155                 *  (As long as the calling function checks the return value!!)
156                 *  This check should be left in even when the default Flightgear files are known
157                 *  to be OK since it checks for mis-indexing of voice files by 3rd party developers.
158                 */
159                 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
160                         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
161                         SG_LOG(SG_GENERAL, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
162                              << " exceeds rawdata size: " << rawDataSize << endl);
163                         delete[] wdptr;
164                         dataOK = false;
165                         return(NULL);
166                 }
167                 memcpy(tmpbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
168                 bufpos += wdptr[i].length;
169         }
170         
171         // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
172         unsigned int offsetIn = (int)(cumLength * sg_random());
173         if(offsetIn > cumLength) offsetIn = cumLength;
174         memcpy(outbuf, tmpbuf + offsetIn, (cumLength - offsetIn));
175         memcpy(outbuf + (cumLength - offsetIn), tmpbuf, offsetIn);
176         
177         delete[] tmpbuf;
178         delete[] wdptr;
179
180         dataOK = true;  
181         return(outbuf);
182 }