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

Modules/SoundControl/GT2004SoundControl.cpp

Go to the documentation of this file.
00001 /**
00002 * @file GT2004SoundControl.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 "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) //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 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   // 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, "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   //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, "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     // 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 }
00183 
00184 
00185 
00186 /*
00187 * Change log :
00188 * 
00189 * $Log: GT2004SoundControl.cpp,v $
00190 * Revision 1.1  2004/07/10 00:18:32  spranger
00191 * renamed (readded) for coderelease
00192 *
00193 * Revision 1.2  2004/05/22 22:52:02  juengel
00194 * Renamed ballP_osition to ballModel.
00195 *
00196 * Revision 1.1.1.1  2004/05/22 17:21:30  cvsadm
00197 * created new repository GT2004_WM
00198 *
00199 * Revision 1.2  2004/03/08 02:11:52  roefer
00200 * Interfaces should be const
00201 *
00202 * Revision 1.1  2003/10/06 14:10:14  cvsadm
00203 * Created GT2004 (M.J.)
00204 *
00205 * Revision 1.2  2003/07/03 17:15:03  dueffert
00206 * error output improved
00207 *
00208 * Revision 1.1.1.1  2003/07/02 09:40:24  cvsadm
00209 * created new repository for the competitions in Padova from the 
00210 * tamara CVS (Tuesday 2:00 pm)
00211 *
00212 * removed unused solutions
00213 *
00214 * Revision 1.10  2003/05/02 18:26:18  risler
00215 * SensorDataBuffer added
00216 * replaced SensorData with SensorDataBuffer
00217 * full SensorData resolution now accessible
00218 *
00219 * Revision 1.9  2003/04/16 18:06:58  risler
00220 * bugfix: useless if removed from destructor
00221 *
00222 * Revision 1.8  2003/04/16 07:00:52  roefer
00223 * Destruction problem solved
00224 *
00225 * Revision 1.7  2003/04/15 18:55:03  risler
00226 * destructor corrected
00227 *
00228 * Revision 1.6  2003/04/15 15:52:09  risler
00229 * DDD GO 2003 code integrated
00230 *
00231 * Revision 1.8  2003/04/09 11:07:13  max
00232 * bugfix: lastsound set always
00233 *
00234 * Revision 1.7  2003/04/08 21:20:36  max
00235 * preload wave files at startup
00236 *
00237 * Revision 1.6  2003/04/06 12:32:48  max
00238 * added ddd sound stuff
00239 *
00240 * Revision 1.5  2003/03/10 18:19:11  dueffert
00241 * const cast warning removed
00242 *
00243 * Revision 1.4  2002/11/28 23:07:02  hebbel
00244 * using variable isInUse of soundData
00245 *
00246 * Revision 1.3  2002/11/28 14:20:53  hebbel
00247 * Cleaned up and added support for playing *.wav files
00248 *
00249 * Revision 1.2  2002/09/11 00:06:58  loetzsch
00250 * continued change of module/solution mechanisms
00251 *
00252 * Revision 1.1  2002/09/10 15:36:16  cvsadm
00253 * Created new project GT2003 (M.L.)
00254 * - Cleaned up the /Src/DataTypes directory
00255 * - Removed challenge related source code
00256 * - Removed processing of incoming audio data
00257 * - Renamed AcousticMessage to SoundRequest
00258 *
00259 * Revision 1.14  2002/07/23 13:33:43  loetzsch
00260 * new streaming classes
00261 *
00262 * removed many #include statements
00263 *
00264 * Revision 1.13  2002/06/04 15:33:57  dueffert
00265 * lengths corrected
00266 *
00267 * Revision 1.12  2002/06/03 18:53:39  dueffert
00268 * messagetype in AcousticMessage is now private
00269 *
00270 * Revision 1.11  2002/05/27 16:15:55  schley
00271 * ballp_osition can be transmitted
00272 *
00273 * Revision 1.10  2002/05/27 15:39:04  fischer
00274 * Added SoundState (Sender and Receiver)
00275 *
00276 * Revision 1.9  2002/05/22 13:46:22  fischer
00277 * Changed SoundInProcessor interface to accept WorldState
00278 *
00279 * Revision 1.8  2002/05/21 15:42:30  hebbel
00280 * acoustic signals can be detected now
00281 *
00282 * Revision 1.7  2002/05/21 11:44:47  hebbel
00283 * added table for signalcoding
00284 *
00285 * Revision 1.6  2002/05/17 11:20:57  fischer
00286 * ball position exchange via acoustic communication
00287 *
00288 * Revision 1.5  2002/05/15 10:20:00  schley
00289 * moved the audio.cfg to SoundProtocol
00290 *
00291 * Revision 1.4  2002/05/15 07:28:18  hebbel
00292 * Removed mute, uses Soundprotocol instead, plays wave for patternchallenge
00293 *
00294 * Revision 1.3  2002/05/14 18:52:58  hebbel
00295 * Added variable mute
00296 *
00297 * Revision 1.2  2002/05/13 13:29:43  schley
00298 * Deconstructor added and audio.cfg
00299 *
00300 * Revision 1.1.1.1  2002/05/10 12:40:16  cvsadm
00301 * Moved GT2002 Project from ute to tamara.
00302 *
00303 * Revision 1.4  2002/05/06 15:06:54  schley
00304 * Added the PCMCreator
00305 *
00306 * Revision 1.3  2002/05/03 13:47:57  giese
00307 * Added timestamp to AcousticMessage
00308 *
00309 * Revision 1.2  2002/04/29 18:04:11  hebbel
00310 * Put SoundPlay to Motion Process
00311 *
00312 * Revision 1.1  2002/04/28 19:14:12  giese
00313 * SoundPlay added...
00314 *
00315 *
00316 * 
00317 */

Generated on Thu Sep 23 19:57:32 2004 for GT2004 by doxygen 1.3.6