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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <simgear/misc/sg_path.hxx>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/sgstream.hxx>
29 #include <simgear/math/sg_random.h>
30 #include <Main/globals.hxx>
32 #include "ATCVoice.hxx"
36 FGATCVoice::FGATCVoice() {
39 FGATCVoice::~FGATCVoice() {
44 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
45 // Return true if successful.
46 bool FGATCVoice::LoadVoice(string voice) {
47 // FIXME CLO: disabled to try to see if this is causign problemcs
52 SGPath path = globals->get_fg_root();
55 string file = voice + ".wav";
57 SoundData = new SGSoundSample( path.c_str(), file.c_str(), false );
58 rawDataSize = SoundData->get_size();
59 rawSoundData = SoundData->get_data();
61 path = globals->get_fg_root();
62 string wordPath = "ATC/" + voice + ".vce";
63 path.append(wordPath);
65 // Now load the word data
66 fin.open(path.c_str(), ios::in);
68 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
71 SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
75 char wrdOffsetStr[20];
76 char wrdLengthStr[20];
77 unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
78 unsigned int wrdLength; // Length of the word sample in bytes
81 unsigned int numwords = atoi(numwds);
82 //cout << numwords << '\n';
83 for(unsigned int i=0; i < numwords; ++i) {
88 wrdOffset = atoi(wrdOffsetStr);
89 wrdLength = atoi(wrdLengthStr);
90 wd.offset = wrdOffset;
91 wd.length = wrdLength;
93 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
102 typedef list < string > tokenList_type;
103 typedef tokenList_type::iterator tokenList_iterator;
105 // Given a desired message, return a pointer to the data buffer and write the buffer length into len.
106 unsigned char* FGATCVoice::WriteMessage(char* message, int& len, bool& dataOK) {
108 // What should we do here?
109 // First - parse the message into a list of tokens.
110 // Sort the tokens into those we understand and those we don't.
111 // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
112 tokenList_type tokenList;
113 tokenList_iterator tokenListItr;
115 // TODO - at the moment we're effectively taking 3 passes through the data.
116 // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
120 strcpy(mes, message);
121 const char delimiters[] = " \t.,;:\"";
122 token = strtok(mes, delimiters);
123 while(token != NULL) {
124 tokenList.push_back(token);
126 //cout << "token = " << token << '\n';
127 token = strtok(NULL, delimiters);
130 WordData* wdptr = new WordData[numWords];
132 unsigned int cumLength = 0;
134 tokenListItr = tokenList.begin();
135 while(tokenListItr != tokenList.end()) {
136 if(wordMap.find(*tokenListItr) == wordMap.end()) {
137 // Oh dear - the token isn't in the sound file
138 //cout << "word " << *tokenListItr << " not found :-(\n";
140 wdptr[word] = wordMap[*tokenListItr];
141 cumLength += wdptr[word].length;
142 //cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;
148 // Check for no tokens found else slScheduler can be crashed
154 unsigned char* tmpbuf = new unsigned char[cumLength];
155 unsigned char* outbuf = new unsigned char[cumLength];
157 unsigned int bufpos = 0;
158 for(int i=0; i<word; ++i) {
160 * Sanity check for corrupt/mismatched sound data input - avoids a seg fault
161 * (As long as the calling function checks the return value!!)
162 * This check should be left in even when the default Flightgear files are known
163 * to be OK since it checks for mis-indexing of voice files by 3rd party developers.
165 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
166 SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
167 SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
168 << " exceeds rawdata size: " << rawDataSize << endl);
173 memcpy(tmpbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
174 bufpos += wdptr[i].length;
177 // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
178 unsigned int offsetIn = (int)(cumLength * sg_random());
179 if(offsetIn > cumLength) offsetIn = cumLength;
180 memcpy(outbuf, tmpbuf + offsetIn, (cumLength - offsetIn));
181 memcpy(outbuf + (cumLength - offsetIn), tmpbuf, offsetIn);