]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCVoice.cxx
Merge branch 'tbm/graphics-bug' into next
[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 <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>
31
32 #include "ATCVoice.hxx"
33
34 #include <stdlib.h>
35
36 FGATCVoice::FGATCVoice() {
37   SoundData = 0;
38   rawSoundData = 0;
39 }
40
41 FGATCVoice::~FGATCVoice() {
42     if (rawSoundData)
43         free( rawSoundData );
44     delete SoundData;
45 }
46
47 // Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
48 // Return true if successful.
49 bool FGATCVoice::LoadVoice(const string& voice) {
50     // FIXME CLO: disabled to try to see if this is causign problemcs
51     // return false;
52
53         ifstream fin;
54
55         SGPath path = globals->get_fg_root();
56         path.append( "ATC" );
57
58         string file = voice + ".wav";
59         
60         SoundData = new SGSoundSample();
61         rawSoundData = (char *)SoundData->load_file(path.c_str(), file.c_str());
62         rawDataSize = SoundData->get_size();
63         
64         path = globals->get_fg_root();
65         string wordPath = "ATC/" + voice + ".vce";
66         path.append(wordPath);
67         
68         // Now load the word data
69         fin.open(path.c_str(), ios::in);
70         if(!fin) {
71                 SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
72                 return(false);
73         }
74         SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
75         char numwds[10];
76         char wrd[100];
77         string wrdstr;
78         char wrdOffsetStr[20];
79         char wrdLengthStr[20];
80         unsigned int wrdOffset;         // Offset into the raw sound data that the word sample begins
81         unsigned int wrdLength;         // Length of the word sample in bytes
82         WordData wd;
83         fin >> numwds;
84         unsigned int numwords = atoi(numwds);
85         //cout << numwords << '\n';
86         for(unsigned int i=0; i < numwords; ++i) {
87                 fin >> wrd;
88                 wrdstr = wrd;
89                 fin >> wrdOffsetStr;
90                 fin >> wrdLengthStr;
91                 wrdOffset = atoi(wrdOffsetStr);
92                 wrdLength = atoi(wrdLengthStr);
93                 wd.offset = wrdOffset;
94                 wd.length = wrdLength;
95                 wordMap[wrdstr] = wd;
96                 //cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
97                 //cout << i << '\n';
98         }
99         
100         fin.close();
101         return(true);
102 }
103
104
105 typedef list < string > tokenList_type;
106 typedef tokenList_type::iterator tokenList_iterator;
107
108 // Given a desired message, return a pointer to the data buffer and write the buffer length into len.
109 unsigned char* FGATCVoice::WriteMessage(char* message, int& len, bool& dataOK) {
110         
111         // What should we do here?
112         // First - parse the message into a list of tokens.
113         // Sort the tokens into those we understand and those we don't.
114         // Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
115         tokenList_type tokenList;
116         tokenList_iterator tokenListItr;
117
118         // TODO - at the moment we're effectively taking 3 passes through the data.
119         // There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
120         char* token;
121         char mes[1000];
122         int numWords = 0;
123         strcpy(mes, message);
124         const char delimiters[] = " \t.,;:\"";
125         token = strtok(mes, delimiters);
126         while(token != NULL) {
127                 tokenList.push_back(token);
128                 ++numWords;
129                 //cout << "token = " << token << '\n';
130                 token = strtok(NULL, delimiters);
131         }
132
133         WordData* wdptr = new WordData[numWords];
134         int word = 0;
135         unsigned int cumLength = 0;
136
137         tokenListItr = tokenList.begin();
138         while(tokenListItr != tokenList.end()) {
139                 if(wordMap.find(*tokenListItr) == wordMap.end()) {
140                         // Oh dear - the token isn't in the sound file
141                         //cout << "word " << *tokenListItr << " not found :-(\n";
142                 } else {
143                         wdptr[word] = wordMap[*tokenListItr];
144                         cumLength += wdptr[word].length;
145                         //cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;  
146                         word++;
147                 }
148                 ++tokenListItr;
149         }
150
151         // Check for no tokens found else slScheduler can be crashed
152         if(!word) {
153                 dataOK = false;
154                 return(NULL);
155         }
156
157         unsigned char* tmpbuf = new unsigned char[cumLength];   
158         unsigned char* outbuf = new unsigned char[cumLength];
159         len = cumLength;
160         unsigned int bufpos = 0;
161         for(int i=0; i<word; ++i) {
162                 /*
163                 *  Sanity check for corrupt/mismatched sound data input - avoids a seg fault
164                 *  (As long as the calling function checks the return value!!)
165                 *  This check should be left in even when the default Flightgear files are known
166                 *  to be OK since it checks for mis-indexing of voice files by 3rd party developers.
167                 */
168                 if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
169                         SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
170                         SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
171                              << " exceeds rawdata size: " << rawDataSize << endl);
172                         delete[] wdptr;
173                         dataOK = false;
174                         return(NULL);
175                 }
176                 memcpy(tmpbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
177                 bufpos += wdptr[i].length;
178         }
179         
180         // tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
181         unsigned int offsetIn = (int)(cumLength * sg_random());
182         if(offsetIn > cumLength) offsetIn = cumLength;
183         memcpy(outbuf, tmpbuf + offsetIn, (cumLength - offsetIn));
184         memcpy(outbuf + (cumLength - offsetIn), tmpbuf, offsetIn);
185         
186         delete[] tmpbuf;
187         delete[] wdptr;
188
189         dataOK = true;  
190         return(outbuf);
191 }