]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCVoice.cxx
Merge branch 'vivian/train' into next
[flightgear.git] / src / ATCDCL / 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include "ATCVoice.hxx"
27
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <fstream>
31 #include <list>
32
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/sgstream.hxx>
36 #include <simgear/math/sg_random.h>
37 #include <simgear/sound/sample_openal.hxx>
38
39 #include <Main/globals.hxx>
40
41 FGATCVoice::FGATCVoice() {
42   SoundData = 0;
43   rawSoundData = 0;
44 }
45
46 FGATCVoice::~FGATCVoice() {
47     if (rawSoundData)
48         free( rawSoundData );
49     delete SoundData;
50 }
51
52 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
53 // Return true if successful.
54 bool FGATCVoice::LoadVoice(const string& voice) {
55     // FIXME CLO: disabled to try to see if this is causing problemcs
56     // return false;
57
58         std::ifstream fin;
59
60         SGPath path = globals->get_fg_root();
61         path.append( "ATC" );
62
63         string file = voice + ".wav";
64         
65         SGSoundSample SoundData;
66         rawSoundData = (char *)SoundData.load_file(path.c_str(), file.c_str());
67         rawDataSize = SoundData.get_size();
68 #ifdef VOICE_TEST
69         ALenum fmt = SoundData.get_format();
70         cout << "ATCVoice:  format: " << fmt 
71                         << "  size: " << rawDataSize << endl;
72 #endif  
73         path = globals->get_fg_root();
74         string wordPath = "ATC/" + voice + ".vce";
75         path.append(wordPath);
76         
77         // Now load the word data
78         fin.open(path.c_str(), ios::in);
79         if(!fin) {
80                 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
81                 return(false);
82         }
83         SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
84         char numwds[10];
85         char wrd[100];
86         string wrdstr;
87         char wrdOffsetStr[20];
88         char wrdLengthStr[20];
89         unsigned int wrdOffset;         // Offset into the raw sound data that the word sample begins
90         unsigned int wrdLength;         // Length of the word sample in bytes
91         WordData wd;
92         fin >> numwds;
93         unsigned int numwords = atoi(numwds);
94         //cout << numwords << '\n';
95         for(unsigned int i=0; i < numwords; ++i) {
96                 fin >> wrd;
97                 wrdstr = wrd;
98                 fin >> wrdOffsetStr;
99                 fin >> wrdLengthStr;
100                 wrdOffset = atoi(wrdOffsetStr);
101                 wrdLength = atoi(wrdLengthStr);
102                 wd.offset = wrdOffset;
103                 wd.length = wrdLength;
104                 wordMap[wrdstr] = wd;
105                 string ws2 = wrdstr;
106                 for(string::iterator p = ws2.begin(); p != ws2.end(); p++){
107                   *p = tolower(*p);
108                   if (*p == '-') *p = '_';
109                 }
110                 if (wrdstr != ws2) wordMap[ws2] = wd;
111
112                 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
113                 //cout << i << '\n';
114         }
115         
116         fin.close();
117         return(true);
118 }
119
120
121 typedef list < string > tokenList_type;
122 typedef tokenList_type::iterator tokenList_iterator;
123
124 // Given a desired message, return a string containing the
125 // sound-sample data
126 string FGATCVoice::WriteMessage(const char* message, bool& dataOK) {
127         
128         // What should we do here?
129         // First - parse the message into a list of tokens.
130         // Sort the tokens into those we understand and those we don't.
131         // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
132         tokenList_type tokenList;
133         tokenList_iterator tokenListItr;
134
135         // TODO - at the moment we're effectively taking 3 passes through the data.
136         // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
137         size_t n1 = 1+strlen(message);
138         char msg[n1];
139         strncpy(msg, message, n1);      // strtok requires a non-const char*
140         char* token;
141         int numWords = 0;
142         const char delimiters[] = " \t.,;:\"\n";
143         char* context;
144         token = strtok_r(msg, delimiters, &context);
145         while(token != NULL) {
146                 for (char *t = token; *t; t++) {
147                   *t = tolower(*t);     // canonicalize the case, to
148                   if (*t == '-') *t = '_';   // match what's in the index
149                 }
150                 tokenList.push_back(token);
151                 ++numWords;
152                 SG_LOG(SG_ATC, SG_DEBUG, "voice synth: token: '"
153                     << token << "'");
154                 token = strtok_r(NULL, delimiters, &context);
155         }
156
157         WordData wdptr[numWords];
158         int word = 0;
159         unsigned int cumLength = 0;
160
161         tokenListItr = tokenList.begin();
162         while(tokenListItr != tokenList.end()) {
163                 if(wordMap.find(*tokenListItr) == wordMap.end()) {
164                 // Oh dear - the token isn't in the sound file
165                   SG_LOG(SG_ATC, SG_ALERT, "voice synth: word '"
166                       << *tokenListItr << "' not found");
167                 } else {
168                         wdptr[word] = wordMap[*tokenListItr];
169                         cumLength += wdptr[word].length;
170                         //cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;  
171                         word++;
172                 }
173                 ++tokenListItr;
174         }
175
176         // Check for no tokens found else slScheduler can be crashed
177         if(!word) {
178                 dataOK = false;
179                 return "";
180         }
181
182         char tmpbuf[cumLength];         
183         unsigned int bufpos = 0;
184         for(int i=0; i<word; ++i) {
185                 /*
186                 *  Sanity check for corrupt/mismatched sound data input - avoids a seg fault
187                 *  (As long as the calling function checks the return value!!)
188                 *  This check should be left in even when the default Flightgear files are known
189                 *  to be OK since it checks for mis-indexing of voice files by 3rd party developers.
190                 */
191                 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
192                         SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
193                         SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
194                              << " exceeds rawdata size: " << rawDataSize << endl);
195
196                         dataOK = false;
197                         return "";
198                 }
199                 memcpy(tmpbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
200                 bufpos += wdptr[i].length;
201         }
202         
203         // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
204         unsigned int offsetIn = (int)(cumLength * sg_random());
205         if(offsetIn > cumLength) offsetIn = cumLength;
206
207         string front(tmpbuf, offsetIn);
208         string back(tmpbuf+offsetIn, cumLength - offsetIn);
209
210         dataOK = true;  
211         return back + front;
212 }