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 = globals->get_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 void* FGATCVoice::WriteMessage(const string& message, size_t* len) {
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 char* msg = (char *)message.c_str();
151 const char delimiters[] = " \t.,;:\"\n";
153 token = strtok_r(msg, delimiters, &context);
154 while(token != NULL) {
155 for (char *t = token; *t; t++) {
156 *t = tolower(*t); // canonicalize the case, to
157 if (*t == '-') *t = '_'; // match what's in the index
159 tokenList.push_back(token);
161 SG_LOG(SG_ATC, SG_DEBUG, "voice synth: token: '"
163 token = strtok_r(NULL, delimiters, &context);
166 vector<WordData> wdptr;
167 wdptr.reserve(numWords);
168 unsigned int cumLength = 0;
170 tokenListItr = tokenList.begin();
171 while(tokenListItr != tokenList.end()) {
172 if(wordMap.find(*tokenListItr) == wordMap.end()) {
173 // Oh dear - the token isn't in the sound file
174 SG_LOG(SG_ATC, SG_DEBUG, "voice synth: word '"
175 << *tokenListItr << "' not found");
177 wdptr.push_back(wordMap[*tokenListItr]);
178 cumLength += wdptr.back().length;
182 const size_t word = wdptr.size();
184 // Check for no tokens found else slScheduler can be crashed
189 boost::shared_array<char> tmpbuf(new char[cumLength]);
190 unsigned int bufpos = 0;
191 for(int i=0; i<word; ++i) {
193 * Sanity check for corrupt/mismatched sound data input - avoids a seg fault
194 * (As long as the calling function checks the return value!!)
195 * This check should be left in even when the default Flightgear files are known
196 * to be OK since it checks for mis-indexing of voice files by 3rd party developers.
198 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
199 SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
200 SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
201 << " exceeds rawdata size: " << rawDataSize << endl);
206 memcpy(tmpbuf.get() + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
207 bufpos += wdptr[i].length;
210 // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
211 unsigned int offsetIn = (int)(cumLength * sg_random());
212 if(offsetIn > cumLength) offsetIn = cumLength;
214 unsigned char *data = (unsigned char *)calloc(1, cumLength);
217 memcpy(data, tmpbuf.get() + offsetIn, cumLength - offsetIn);
218 memcpy(data + cumLength - offsetIn, tmpbuf.get(), offsetIn);