1 // FGATCVoice.cxx - a class to encapsulate an ATC voice
3 // Written by David Luff, started November 2002.
5 // Copyright (C) 2002 David C Luff - david.luff@nottingham.ac.uk
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.
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.
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.
26 #include "ATCVoice.hxx"
34 #include <boost/shared_array.hpp>
36 #include <simgear/sound/soundmgr_openal.hxx>
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>
42 #include <Main/globals.hxx>
47 #define strtok_r strtok_s
52 FGATCVoice::FGATCVoice() {
57 FGATCVoice::~FGATCVoice() {
63 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
64 // Return true if successful.
65 bool FGATCVoice::LoadVoice(const string& voice) {
68 SGPath path = globals->get_fg_root();
69 string file = voice + ".wav";
73 string full_path = path.str();
75 SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr");
77 smgr->load(full_path, &data, &format, &rawDataSize, &freq);
78 rawSoundData = (char*)data;
80 cout << "ATCVoice: format: " << format
81 << " size: " << rawDataSize << endl;
83 path = globals->get_fg_root();
84 string wordPath = "ATC/" + voice + ".vce";
85 path.append(wordPath);
87 // Now load the word data
88 fin.open(path.c_str(), ios::in);
90 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
93 SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
97 char wrdOffsetStr[20];
98 char wrdLengthStr[20];
99 unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
100 unsigned int wrdLength; // Length of the word sample in bytes
103 unsigned int numwords = atoi(numwds);
104 //cout << numwords << '\n';
105 for(unsigned int i=0; i < numwords; ++i) {
110 wrdOffset = atoi(wrdOffsetStr);
111 wrdLength = atoi(wrdLengthStr);
112 wd.offset = wrdOffset;
113 wd.length = wrdLength;
114 wordMap[wrdstr] = wd;
116 for(string::iterator p = ws2.begin(); p != ws2.end(); p++){
118 if (*p == '-') *p = '_';
120 if (wrdstr != ws2) wordMap[ws2] = wd;
122 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
131 typedef list < string > tokenList_type;
132 typedef tokenList_type::iterator tokenList_iterator;
134 // Given a desired message, return a string containing the
136 string FGATCVoice::WriteMessage(const char* message, bool& dataOK) {
138 // What should we do here?
139 // First - parse the message into a list of tokens.
140 // Sort the tokens into those we understand and those we don't.
141 // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
142 tokenList_type tokenList;
143 tokenList_iterator tokenListItr;
145 // TODO - at the moment we're effectively taking 3 passes through the data.
146 // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
147 size_t n1 = 1+strlen(message);
148 boost::shared_array<char> msg(new char[n1]);
149 strncpy(msg.get(), message, n1); // strtok requires a non-const char*
152 const char delimiters[] = " \t.,;:\"\n";
154 token = strtok_r(msg.get(), delimiters, &context);
155 while(token != NULL) {
156 for (char *t = token; *t; t++) {
157 *t = tolower(*t); // canonicalize the case, to
158 if (*t == '-') *t = '_'; // match what's in the index
160 tokenList.push_back(token);
162 SG_LOG(SG_ATC, SG_DEBUG, "voice synth: token: '"
164 token = strtok_r(NULL, delimiters, &context);
167 vector<WordData> wdptr;
168 wdptr.reserve(numWords);
169 unsigned int cumLength = 0;
171 tokenListItr = tokenList.begin();
172 while(tokenListItr != tokenList.end()) {
173 if(wordMap.find(*tokenListItr) == wordMap.end()) {
174 // Oh dear - the token isn't in the sound file
175 SG_LOG(SG_ATC, SG_ALERT, "voice synth: word '"
176 << *tokenListItr << "' not found");
178 wdptr.push_back(wordMap[*tokenListItr]);
179 cumLength += wdptr.back().length;
183 const size_t word = wdptr.size();
185 // Check for no tokens found else slScheduler can be crashed
190 boost::shared_array<char> tmpbuf(new char[cumLength]);
191 unsigned int bufpos = 0;
192 for(int i=0; i<word; ++i) {
194 * Sanity check for corrupt/mismatched sound data input - avoids a seg fault
195 * (As long as the calling function checks the return value!!)
196 * This check should be left in even when the default Flightgear files are known
197 * to be OK since it checks for mis-indexing of voice files by 3rd party developers.
199 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
200 SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
201 SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
202 << " exceeds rawdata size: " << rawDataSize << endl);
207 memcpy(tmpbuf.get() + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
208 bufpos += wdptr[i].length;
211 // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
212 unsigned int offsetIn = (int)(cumLength * sg_random());
213 if(offsetIn > cumLength) offsetIn = cumLength;
215 string front(tmpbuf.get(), offsetIn);
216 string back(tmpbuf.get() + offsetIn, cumLength - offsetIn);