C:/Users/Mike/Documents/Visual Studio Projects/GameEngine/src/SoundManager.cpp

Go to the documentation of this file.
00001 #include "SoundManager.h"
00002 
00003 namespace GameEngine {
00004 
00005         SoundManager::SoundManager(GameApp* application)
00006                 : mApplication(application), mBuffersUsed(0), mSourcesUsed(0), mListenerNode(NULL)
00007         {
00008                 mBuffersUsed = 0;
00009                 mSourcesUsed = 0;
00010 
00011                 for(int i = 0; i < MAX_NUM_BUFFERS; i++){
00012                         mAudioBuffers[i] = 0;
00013                         mBufferInUse[i] = false;
00014                 }
00015 
00016                 for(int i = 0; i < MAX_NUM_SOURCES; i++){
00017                         mAudioSources[i] = 0;
00018                         mSourceInUse[i] = false;
00019                 }
00020                   
00021         }
00022 
00023 
00024         SoundManager::~SoundManager()
00025         {
00026                 /*alDeleteSources( MAX_NUM_SOURCES, mAudioSources );
00027                 alDeleteBuffers( MAX_NUM_BUFFERS, mAudioBuffers );
00028                   
00029                 // Destroy the sound context and device
00030                 mAudioContext = alcGetCurrentContext();
00031                 mAudioDevice = alcGetContextsDevice( mAudioContext );
00032                 alcMakeContextCurrent( NULL );
00033                 alcDestroyContext( mAudioContext );
00034                 if ( mAudioDevice)
00035                         alcCloseDevice( mAudioDevice );
00036                   
00037                 alutExit();*/
00038         }
00039 
00040 
00041         bool SoundManager::init()
00042         {
00043                 assert(isInitialised() == false);
00044 
00045                 mAudioDevice = alcOpenDevice(NULL); // Open (default) device
00046                 if(!mAudioDevice){ // Failed to open device
00047                         return false;
00048                 }
00049 
00050 
00051                 mAudioContext = alcCreateContext(mAudioDevice, NULL); // Create an audio context
00052                 if(!mAudioContext){ // Failed to create context
00053                         return false;
00054                 }
00055 
00056                 alcMakeContextCurrent(mAudioContext); // Make the context we created the active one
00057 
00058 
00059                 // Set listeners initial position, velocity and orientation
00060                 ALfloat p[] = {0,0,0};
00061                 ALfloat v[] = {0,0,0};
00062                 ALfloat o[] = {0,0,0,0,0};
00063 
00064                 alListenerfv(AL_POSITION, p);
00065                 alListenerfv(AL_VELOCITY, v);
00066                 alListenerfv(AL_ORIENTATION, o);
00067 
00068 
00069                 alGetError(); // Clear errors before we attempt to generate buffers
00070 
00071 
00072                 alGenBuffers(MAX_NUM_BUFFERS, mAudioBuffers); // Generate buffers
00073                 if(alGetError() != AL_NO_ERROR){ // Some error generating buffers
00074                         return false;
00075                 }
00076 
00077                 alGenSources(MAX_NUM_SOURCES, mAudioSources); // Generate sources
00078                 if(alGetError() != AL_NO_ERROR){ // Some error generating sources
00079                         return false;
00080                 }
00081 
00082 
00083                 // Everything initialised ok, now call the parent class version
00084                 Process::init();
00085 
00086                 return isInitialised();
00087         }
00088 
00089 
00090         void SoundManager::setListenerNode(Ogre::SceneNode* listenerNode)
00091         {
00092                 mListenerNode = listenerNode;
00093         }
00094 
00095 
00096         bool SoundManager::loadSound(std::string fileName, ALuint* bufferId)
00097         {
00098                 // Do we have a free buffer ?
00099                 if(mBuffersUsed >= MAX_NUM_BUFFERS){
00100                         return false;
00101                 }
00102 
00103 
00104                 // Find a free buffer and store it's id
00105                 int freeBufId = 0;
00106 
00107                 while(mBufferInUse[freeBufId] == true){
00108                         freeBufId++;
00109                 }
00110 
00111                 
00112                 // Now determine the file type and call approriate function to load file 
00113                 if(fileName.find(".wav",0) != std::string::npos) { // .wav file
00114 
00115                         if( !loadWavFile(fileName, mAudioBuffers[freeBufId]) ){ // Try to load the file
00116                                 return false;
00117                         }
00118 
00119                 }
00120                 else{ // Not a supported file type
00121                         return false;
00122                 }
00123 
00124 
00125                 mBufferInUse[freeBufId] = true; // Buffer is now in use
00126                 mBuffersUsed++;
00127 
00128                 *bufferId = freeBufId; // Inidicate to caller which buffer file was loaded into
00129 
00130                 // Success
00131                 return true;
00132         }
00133 
00134 
00135         bool SoundManager::playSound(ALuint bufId, ALuint* soundId, bool loop, Vector3 position, Vector3 velocity)
00136         {
00137                 // Check buffer ID given coresponds with a valid and filled buffer
00138                 if(bufId >= MAX_NUM_BUFFERS){ 
00139                         return false;
00140                 }
00141 
00142                 if(mBufferInUse[bufId] != true){
00143                         return false;
00144                 }
00145 
00146 
00147                 // This will hold the ID of our chosen source
00148                 int sourceId = -1;
00149 
00150 
00151                 if(mSourcesUsed >= MAX_NUM_SOURCES){ // No free sources
00152 
00153                         for(int id=0; id<MAX_NUM_SOURCES; id++){ // Try and find an occupied source that we can free up (eg one that is now stopped)
00154                                 ALint state;
00155                                 alGetSourcei(mAudioSources[id], AL_SOURCE_STATE, &state );
00156 
00157                                 if(state == AL_STOPPED){
00158                                         mSourceInUse[id] = false; // Idicate the source is now free
00159                                         sourceId =  id;
00160                                 }
00161                         }
00162 
00163                         if(sourceId == -1) return false; // Couldn't free up a source, can't play the sound!
00164 
00165                 }
00166                 else{ //A free source already exists, find it
00167 
00168                         for(int id = 0; id < MAX_NUM_SOURCES; id++){
00169                                 if(mSourceInUse[id] == false){
00170                                         sourceId = id;
00171                                         break; // Found a free source, no need to continue
00172                                 }
00173                         }
00174 
00175                         if(sourceId == -1) return false; // If we ever get here, the universe is about to implode!
00176 
00177                 }
00178 
00179 
00180                 // Found a free source, now attach the buffer to it and set some properties
00181                 ALfloat pos[] = { position.mX, position.mY, position.mZ };
00182                 ALfloat vel[] = { velocity.mX, velocity.mY, velocity.mZ };
00183 
00184                 alSourcei(mAudioSources[sourceId], AL_BUFFER, mAudioBuffers[bufId]);
00185                 alSourcei(mAudioSources[sourceId], AL_LOOPING, loop);
00186                 alSourcefv(mAudioSources[sourceId], AL_POSITION, pos);
00187                 alSourcefv(mAudioSources[sourceId], AL_VELOCITY, vel);
00188                 alSourcef (mAudioSources[sourceId], AL_GAIN, 100.0f);
00189         
00190 
00191                 // Play the sound already!
00192                 alSourcePlay(mAudioSources[sourceId]);
00193 
00194                 if(alGetError() != AL_NO_ERROR){ // Playing failed
00195                         return false;
00196                 }
00197 
00198 
00199                 // Update the status of the source now it is use
00200                 mSourceInUse[sourceId] = true;
00201                 mSourcesUsed++;
00202 
00203                 // Indicate to caller the source ID we are going to play the sound with
00204                 if(soundId != NULL){
00205                         *soundId = sourceId;
00206                 }
00207 
00208                 // All done
00209                 return true;
00210         }
00211 
00212 
00213         bool SoundManager::stopSound(ALuint soundId)
00214         {
00215                 if(soundId >= MAX_NUM_SOURCES || mSourceInUse[soundId] != true){ // Check sound ID given is valid and is in use
00216                         return false;
00217                 }
00218 
00219                 alGetError(); // Clear errors
00220 
00221 
00222                 // Stop the sound
00223                 alSourceStop( mAudioSources[soundId] );
00224 
00225                 if(alGetError() != AL_NO_ERROR){ // Stopping sound failed
00226                         return false;
00227                 }
00228 
00229                 // Sound stopped
00230                 return true;
00231         }
00232 
00233 
00234         bool SoundManager::stopAllSounds()
00235         {
00236                 return true;
00237         }
00238 
00239 
00240         bool SoundManager::loadWavFile(std::string fileName, ALuint destAudioBuffId)
00241         {
00242                 // Set up some temporary variables
00243                 ALenum format;
00244                 ALvoid* data;
00245                 ALsizei size;
00246                 ALsizei freq;
00247                 ALboolean loop;
00248                 std::string fullPathToFile = "";
00249 
00250                 fullPathToFile += fileName;
00251 
00252                 // Clear errors
00253                 alGetError();
00254 
00255 
00256                 // Load file data into memory
00257                 alutLoadWAVFile((ALbyte*)fullPathToFile.c_str(), &format, &data, &size, &freq, &loop);
00258                 if(alGetError() != AL_NO_ERROR){ // Couldn't load file
00259                         return false;
00260                 }
00261 
00262 
00263                 // Load data from memory into our buffer
00264                 alBufferData(destAudioBuffId, format, data, size, freq);
00265 
00266 
00267                 // Check everything worked
00268                 if(alGetError() != AL_NO_ERROR){ // Couldn't load data into buffer
00269                         return false;
00270                 }
00271 
00272 
00273                 // Unload data from memory now that it has been placed in a buffer
00274                 //alutUnloadWAV(format, data, size, freq);
00275 
00276                 return true;
00277         }
00278 
00279 
00280         bool SoundManager::loadOggFile(std::string fileName, ALuint destAudioBuffId)
00281         {
00282                 // Set up some temporary variables
00283                 ALenum format;
00284                 ALvoid* data;
00285                 ALsizei size;
00286                 ALsizei freq;
00287                 ALboolean loop;
00288                 std::string fullPathToFile = "";
00289 
00290                 fullPathToFile += fileName;
00291 
00292                 // Clear errors
00293                 alGetError();
00294 
00295 
00296                 // Load file data into memory
00297                 alutLoadWAVFile((ALbyte*)fullPathToFile.c_str(), &format, &data, &size, &freq, &loop);
00298                 if(alGetError() != AL_NO_ERROR){ // Couldn't load file
00299                         return false;
00300                 }
00301 
00302 
00303                 // Load data from memory into our buffer
00304                 alBufferData(destAudioBuffId, format, data, size, freq);
00305 
00306 
00307                 // Check everything worked
00308                 if(alGetError() != AL_NO_ERROR){ // Couldn't load data into buffer
00309                         return false;
00310                 }
00311 
00312 
00313                 // Unload data from memory now that it has been placed in a buffer
00314                 alutUnloadWAV(format, data, size, freq);
00315 
00316                 return true;
00317         }
00318 
00319 
00320         // Note to self : Broken!
00321         void SoundManager::update(const double& dt){
00322                 if(mListenerNode){
00323                         //this->setListenerPos( Vector3::vecFromOgre( mListenerNode->getPosition() ), Vector3(0,0,0), Quaternion::quatFromOgre( mListenerNode->getOrientation() ) );
00324                 }
00325         }
00326 
00327         
00328         bool SoundManager::setListenerPos(Vector3 pos, Vector3 vel, Quaternion ori)
00329         {
00330                 alGetError(); // Clear errors
00331 
00332                 // Position
00333                 ALfloat p[] = {pos.mX, pos.mY, pos.mZ};
00334                 alListenerfv(AL_POSITION, p);
00335                 if(alGetError() != AL_NO_ERROR){
00336                         return false;
00337                 }
00338 
00339                 // Velocity
00340                 ALfloat v[] = {vel.mX, vel.mY, vel.mZ};
00341                 alListenerfv(AL_VELOCITY, v);
00342                 if(alGetError() != AL_NO_ERROR){
00343                         return false;
00344                 }
00345 
00346                 // Orientation
00347                 ALfloat o[] = {ori.getYawRad(), ori.getPitchRad(), ori.getRollRad()};
00348                 alListenerfv(AL_ORIENTATION, o);
00349                 if(alGetError() != AL_NO_ERROR){
00350                         return false;
00351                 }
00352 
00353                 return true;
00354         }
00355 
00356 }

Generated on Thu Apr 19 09:47:02 2007 for GameEngine by  doxygen 1.5.2