00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "GT2004SoundControl.h"
00011 #include "Tools/Debugging/Debugging.h"
00012
00013
00014 GT2004SoundControl::GT2004SoundControl(const SoundControlInterfaces& interfaces)
00015 : SoundControl(interfaces)
00016 {
00017 playingWave = false;
00018 actWavePosition = 0;
00019 lastSound = SoundRequest::none;
00020
00021 for (int i = 0; i < SoundRequest::numOfSoundIDs; i++)
00022 {
00023 waveData[i] = 0;
00024 waveLength[i] = 0;
00025 if (i != (int) SoundRequest::none)
00026 {
00027 char file[20];
00028 strcpy(file,"Sounds/");
00029 strcat(file,(const char*)soundRequest.getSoundIDName((SoundRequest::SoundID)i));
00030 strcat(file,".wav");
00031 loadWavefile(file, i);
00032 }
00033 }
00034 }
00035
00036
00037 GT2004SoundControl::~GT2004SoundControl()
00038 {
00039 for (int i = 0; i < SoundRequest::numOfSoundIDs; i++)
00040 if (waveData[i]) delete (waveData[i]);
00041 }
00042
00043
00044 void GT2004SoundControl::execute()
00045 {
00046 if(playingWave)
00047 {
00048 fillWaveBuffer(&soundData, lastSound);
00049 }
00050 else
00051 {
00052 if ((soundRequest.soundID != SoundRequest::none) &&
00053 (soundRequest.soundID < SoundRequest::numOfSoundIDs) &&
00054 (soundRequest.soundID != lastSound))
00055 {
00056 playWave(&soundData, (int)soundRequest.soundID);
00057 }
00058 lastSound = soundRequest.soundID;
00059 }
00060 }
00061
00062
00063 void GT2004SoundControl::playWave(SoundData* soundData, int index)
00064 {
00065 if (waveData[index])
00066 {
00067 playingWave = true;
00068 soundData->isInUse = true;
00069 actWavePosition = startWavePosition[index];
00070 fillWaveBuffer(soundData, index);
00071 }
00072 }
00073
00074
00075 bool GT2004SoundControl::loadWavefile(const char* filename, int index)
00076 {
00077 InBinaryFile fin(filename);
00078 if (!fin.exists())
00079 {
00080 OUTPUT(idText,text,"GT2004SoundControl : Error, " << filename << " not found.");
00081 return false;
00082 }
00083 else
00084 {
00085 if (checkWaveHeader(filename, index))
00086 {
00087 waveData[index] = new char[waveLength[index]+startWavePosition[index]];
00088 fin.read(waveData[index],waveLength[index]+startWavePosition[index]);
00089 return true;
00090 }
00091 }
00092 return false;
00093 }
00094
00095
00096 bool GT2004SoundControl::checkWaveHeader(const char* filename, int index)
00097 {
00098 InBinaryFile fin(filename);
00099 int headerPos = 0;
00100 unsigned short format;
00101 unsigned long sampleRate;
00102 unsigned long bytesPerSecond;
00103
00104
00105 fin.read(waveHeader, 100);
00106
00107
00108 if(strncmp(waveHeader,"RIFF",4)!=0)
00109 {
00110 OUTPUT(idText,text, "GT2004SoundControl : Error: " << filename << " is no RIFF file!");
00111 return false;
00112 }
00113 headerPos += 8;
00114 if(strncmp(waveHeader+headerPos,"WAVE",4)!=0)
00115 {
00116 OUTPUT(idText,text, "GT2004SoundControl : Error: " << filename << " is no WAVE file!");
00117 return false;
00118 }
00119 headerPos += 4;
00120 if(strncmp(waveHeader+headerPos,"fmt ",4)!=0)
00121 {
00122 OUTPUT(idText,text, "GT2004SoundControl : Error: " << filename << " has wrong subchunk fmt!");
00123 return false;
00124 }
00125 headerPos += 10;
00126 memcpy(&format,waveHeader+headerPos,2);
00127 if(format != 1)
00128 {
00129 OUTPUT(idText,text, "GT2004SoundControl : Error: " << filename << " is not mono!");
00130 return false;
00131 }
00132 headerPos += 2;
00133 memcpy(&sampleRate,waveHeader+headerPos,4);
00134 if(sampleRate != 8000)
00135 {
00136 OUTPUT(idText,text, "GT2004SoundControl : Error: " << filename << ": sample rate not 8000Hz!");
00137 return false;
00138 }
00139 headerPos += 4;
00140 memcpy(&bytesPerSecond,waveHeader+headerPos,4);
00141 if(bytesPerSecond != 8000)
00142 {
00143 OUTPUT(idText,text, "GT2004SoundControl : Error: " << filename << ": not 8000 bytes per second!");
00144 return false;
00145 }
00146
00147
00148 while ((headerPos < 97) && (strncmp(waveHeader+headerPos,"data",4)!=0))
00149 {
00150 headerPos+=2;
00151 }
00152 if (strncmp(waveHeader+headerPos,"data",4)!=0)
00153 {
00154 OUTPUT(idText,text, "GT2004SoundControl : Error: " << filename << ": 'data' not found!");
00155 return false;
00156 }
00157 memcpy(&waveLength[index],waveHeader+headerPos+4,4);
00158 startWavePosition[index] = headerPos + 8;
00159
00160 return true;
00161 }
00162
00163
00164 void GT2004SoundControl::fillWaveBuffer(SoundData *soundData,int index)
00165 {
00166 if(actWavePosition < waveLength[index] - 256)
00167 {
00168 memcpy(soundData->pcmBuffer, waveData[index] + actWavePosition, 256);
00169
00170 for (int i=0; i < 256; i++)
00171 {
00172 soundData->pcmBuffer[i] = soundData->pcmBuffer[i] ^ 0x80;
00173 }
00174 actWavePosition += 256;
00175 }
00176 else
00177 {
00178 memset(soundData->pcmBuffer, 0, 256);
00179 playingWave = false;
00180 soundData->isInUse = false;
00181 }
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317