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 if (!smgr->load(full_path, &data, &format, &rawDataSize, &freq))
79 rawSoundData = (char*)data;
81 cout << "ATCVoice: format: " << format
82 << " size: " << rawDataSize << endl;
84 path = globals->get_fg_root();
85 string wordPath = "ATC/" + voice + ".vce";
86 path.append(wordPath);
88 // Now load the word data
89 fin.open(path.c_str(), ios::in);
91 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
94 SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
98 char wrdOffsetStr[20];
99 char wrdLengthStr[20];
100 unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
101 unsigned int wrdLength; // Length of the word sample in bytes
104 unsigned int numwords = atoi(numwds);
105 //cout << numwords << '\n';
106 for(unsigned int i=0; i < numwords; ++i) {
111 wrdOffset = atoi(wrdOffsetStr);
112 wrdLength = atoi(wrdLengthStr);
113 wd.offset = wrdOffset;
114 wd.length = wrdLength;
115 wordMap[wrdstr] = wd;
117 for(string::iterator p = ws2.begin(); p != ws2.end(); p++){
119 if (*p == '-') *p = '_';
121 if (wrdstr != ws2) wordMap[ws2] = wd;
123 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
132 typedef list < string > tokenList_type;
133 typedef tokenList_type::iterator tokenList_iterator;
135 // Given a desired message, return a string containing the
137 string FGATCVoice::WriteMessage(const char* message, bool& dataOK) {
139 // What should we do here?
140 // First - parse the message into a list of tokens.
141 // Sort the tokens into those we understand and those we don't.
142 // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
143 tokenList_type tokenList;
144 tokenList_iterator tokenListItr;
146 // TODO - at the moment we're effectively taking 3 passes through the data.
147 // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
148 size_t n1 = 1+strlen(message);
149 boost::shared_array<char> msg(new char[n1]);
150 strncpy(msg.get(), message, n1); // strtok requires a non-const char*
153 const char delimiters[] = " \t.,;:\"\n";
155 token = strtok_r(msg.get(), delimiters, &context);
156 while(token != NULL) {
157 for (char *t = token; *t; t++) {
158 *t = tolower(*t); // canonicalize the case, to
159 if (*t == '-') *t = '_'; // match what's in the index
161 tokenList.push_back(token);
163 SG_LOG(SG_ATC, SG_DEBUG, "voice synth: token: '"
165 token = strtok_r(NULL, delimiters, &context);
168 vector<WordData> wdptr;
169 wdptr.reserve(numWords);
170 unsigned int cumLength = 0;
172 tokenListItr = tokenList.begin();
173 while(tokenListItr != tokenList.end()) {
174 if(wordMap.find(*tokenListItr) == wordMap.end()) {
175 // Oh dear - the token isn't in the sound file
176 SG_LOG(SG_ATC, SG_ALERT, "voice synth: word '"
177 << *tokenListItr << "' not found");
179 wdptr.push_back(wordMap[*tokenListItr]);
180 cumLength += wdptr.back().length;
184 const size_t word = wdptr.size();
186 // Check for no tokens found else slScheduler can be crashed
191 boost::shared_array<char> tmpbuf(new char[cumLength]);
192 unsigned int bufpos = 0;
193 for(int i=0; i<word; ++i) {
195 * Sanity check for corrupt/mismatched sound data input - avoids a seg fault
196 * (As long as the calling function checks the return value!!)
197 * This check should be left in even when the default Flightgear files are known
198 * to be OK since it checks for mis-indexing of voice files by 3rd party developers.
200 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
201 SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
202 SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
203 << " exceeds rawdata size: " << rawDataSize << endl);
208 memcpy(tmpbuf.get() + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
209 bufpos += wdptr[i].length;
212 // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
213 unsigned int offsetIn = (int)(cumLength * sg_random());
214 if(offsetIn > cumLength) offsetIn = cumLength;
216 string front(tmpbuf.get(), offsetIn);
217 string back(tmpbuf.get() + offsetIn, cumLength - offsetIn);