Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Modules/SoundControl/GT2005SoundControl.cpp

Go to the documentation of this file.
00001 /**
00002 * @file GT2005SoundControl.cpp
00003 * 
00004 * Implementation of class SoundControl.
00005 *
00006 * @author <a href="mailto:Oliver.Giese@uni-dortmund.de">Oliver Giese</a>
00007 * @author <a href="mailto:matthias.hebbel@uni-dortmund.de">Matthias Hebbel</a>
00008 *
00009 */
00010 #include "GT2005SoundControl.h"
00011 #include "Tools/Debugging/Debugging.h"
00012 
00013 
00014 GT2005SoundControl::GT2005SoundControl(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 GT2005SoundControl::~GT2005SoundControl()
00038 {
00039   for (int i = 0; i < SoundRequest::numOfSoundIDs; i++)
00040     if (waveData[i]) delete (waveData[i]);
00041 }
00042 
00043 
00044 void GT2005SoundControl::execute()
00045 {
00046   if(playingWave) //occupied with playing wavesound
00047   {
00048     fillWaveBuffer(&soundData, lastSound);
00049   }
00050   else //open wavefile and play it
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 GT2005SoundControl::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 GT2005SoundControl::loadWavefile(const char* filename, int index)
00076 {
00077   InBinaryFile fin(filename);
00078   if (!fin.exists())
00079   {
00080     OUTPUT(idText,text,"GT2005SoundControl : 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 GT2005SoundControl::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   // load waveheader of maximum size
00105   fin.read(waveHeader, 100);
00106 
00107   // check waveformat
00108   if(strncmp(waveHeader,"RIFF",4)!=0)
00109   {
00110     OUTPUT(idText,text, "GT2005SoundControl : 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, "GT2005SoundControl : 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, "GT2005SoundControl : 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, "GT2005SoundControl : 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, "GT2005SoundControl : 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, "GT2005SoundControl : Error: " << filename << ": not 8000 bytes per second!");
00144     return false;
00145   }
00146 
00147   //find keyword "data"
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, "GT2005SoundControl : 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 GT2005SoundControl::fillWaveBuffer(SoundData *soundData,int index)
00165 {
00166   if(actWavePosition < waveLength[index] - 256)
00167   {
00168     memcpy(soundData->pcmBuffer, waveData[index] + actWavePosition, 256);
00169     // convert unsigned values to signed values
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 }

Generated on Mon Mar 20 21:59:57 2006 for GT2005 by doxygen 1.3.6