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