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