]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCVoice.cxx
Fix ATIS output.
[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 <vector>
32 #include <algorithm>
33
34 #include <simgear/sound/soundmgr_openal.hxx>
35 #include <simgear/sound/sample_openal.hxx>
36
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/sgstream.hxx>
40 #include <simgear/math/sg_random.h>
41
42 #include <Main/globals.hxx>
43
44 using namespace std;
45
46 FGATCVoice::FGATCVoice() {
47   SoundData = 0;
48   rawSoundData = 0;
49 }
50
51 FGATCVoice::~FGATCVoice() {
52     if (rawSoundData)
53         free( rawSoundData );
54     delete SoundData;
55 }
56
57 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
58 // Return true if successful.
59 bool FGATCVoice::LoadVoice(const string& voice) {
60         std::ifstream fin;
61
62         SGPath path = globals->get_fg_root();
63         string file = voice + ".wav";
64         path.append( "ATC" );
65         path.append( file );
66         
67         string full_path = path.str();
68         int format, freq;
69         SGSoundMgr *smgr = globals->get_soundmgr();
70         void *data;
71         if (!smgr->load(full_path, &data, &format, &rawDataSize, &freq))
72             return false;
73         rawSoundData = (char*)data;
74 #ifdef VOICE_TEST
75         cout << "ATCVoice:  format: " << format
76                         << "  size: " << rawDataSize << endl;
77 #endif  
78         path = globals->get_fg_root();
79         string wordPath = "ATC/" + voice + ".vce";
80         path.append(wordPath);
81         
82         // Now load the word data
83         fin.open(path.c_str(), ios::in);
84         if(!fin) {
85                 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
86                 return(false);
87         }
88         SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
89         char numwds[10];
90         char wrd[100];
91         string wrdstr;
92         char wrdOffsetStr[20];
93         char wrdLengthStr[20];
94         unsigned int wrdOffset;         // Offset into the raw sound data that the word sample begins
95         unsigned int wrdLength;         // Length of the word sample in bytes
96         WordData wd;
97         fin >> numwds;
98         unsigned int numwords = atoi(numwds);
99         //cout << numwords << '\n';
100         for(unsigned int i=0; i < numwords; ++i) {
101                 fin >> wrd;
102                 wrdstr = wrd;
103                 fin >> wrdOffsetStr;
104                 fin >> wrdLengthStr;
105                 wrdOffset = atoi(wrdOffsetStr);
106                 wrdLength = atoi(wrdLengthStr);
107                 wd.offset = wrdOffset;
108                 wd.length = wrdLength;
109                 wordMap[wrdstr] = wd;
110                 string ws2 = wrdstr;
111                 for(string::iterator p = ws2.begin(); p != ws2.end(); p++){
112                   *p = tolower(*p);
113                   if (*p == '-') *p = '_';
114                 }
115                 if (wrdstr != ws2)  wordMap[ws2] = wd;
116
117                 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
118                 //cout << i << '\n';
119         }
120         
121         fin.close();
122         return(true);
123 }
124
125
126 // Given a desired message, return a string containing the
127 // sound-sample data
128 void* FGATCVoice::WriteMessage(const string& message, size_t* len) {
129         
130         // What should we do here?
131         // First - parse the message into a list of tokens.
132         // Sort the tokens into those we understand and those we don't.
133         // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
134
135         vector<char> sound;
136         const char delimiters[] = " \t.,;:\"\n";
137         string::size_type token_start = message.find_first_not_of(delimiters);
138         while(token_start != string::npos) {
139                 string::size_type token_end = message.find_first_of(delimiters, token_start);
140                 string token;
141                 if (token_end == string::npos) {
142                         token = message.substr(token_start);
143                         token_start = string::npos;
144                 } else {
145                         token = message.substr(token_start, token_end - token_start);
146                         token_start = message.find_first_not_of(delimiters, token_end);
147                 }
148
149                 if (token == "/_") continue;
150
151                 for(string::iterator t = token.begin(); t != token.end(); t++) {
152                         // canonicalize the token, to match what's in the index
153                         *t = (*t == '-') ? '_' : tolower(*t);
154                 }
155                 SG_LOG(SG_ATC, SG_DEBUG, "voice synth: token: '"
156                     << token << "'");
157
158                 atc_word_map_const_iterator wordIt = wordMap.find(token);
159                 if(wordIt == wordMap.end()) {
160                         // Oh dear - the token isn't in the sound file
161                         SG_LOG(SG_ATC, SG_ALERT, "voice synth: word '"
162                                 << token << "' not found");
163                 } else {
164                         const WordData& word = wordIt->second;
165                         /*
166                         *  Sanity check for corrupt/mismatched sound data input - avoids a seg fault
167                         *  (As long as the calling function checks the return value!!)
168                         *  This check should be left in even when the default Flightgear files are known
169                         *  to be OK since it checks for mis-indexing of voice files by 3rd party developers.
170                         */
171                         if((word.offset + word.length) > rawDataSize) {
172                                 SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
173                                 SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << word.offset + word.length
174                                         << " exceeds rawdata size: " << rawDataSize << endl);
175
176                                 *len = 0;
177                                 return 0;
178                         }
179                         sound.insert(sound.end(), rawSoundData + word.offset, rawSoundData + word.offset + word.length);
180                 }
181         }
182
183         // Check for no tokens found else slScheduler can be crashed
184         *len = sound.size();
185         if (*len == 0) {
186                 return 0;
187         }
188
189         char* data = (char*)malloc(*len);
190         if (data == 0) {
191                 SG_LOG(SG_ATC, SG_ALERT, "ERROR - could not allocate " << *len << " bytes of memory for ATIS sound\n");
192                 *len = 0;
193                 return 0;
194         }
195
196         // randomize start position
197         unsigned int offsetIn = (unsigned int)(*len * sg_random());
198         if (offsetIn > 0 && offsetIn < *len) {
199                 copy(sound.begin() + offsetIn, sound.end(), data);
200                 copy(sound.begin(), sound.begin() + offsetIn, data + *len - offsetIn);
201         } else {
202                 copy(sound.begin(), sound.end(), data);
203         }
204
205         return data;
206 }