cAudioSource.cpp

00001 // Copyright (c) 2008-2010 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones
00002 // This file is part of the "cAudio Engine"
00003 // For conditions of distribution and use, see copyright notice in cAudio.h
00004 
00005 #include "../Headers/cAudioSource.h"
00006 #include "../Headers/cLogger.h"
00007 #include "../Headers/cFilter.h"
00008 #include "../Headers/cEffect.h"
00009 
00010 #include <string.h>
00011 
00012 namespace cAudio
00013 {
00014 #ifdef CAUDIO_EFX_ENABLED
00015     cAudioSource::cAudioSource(IAudioDecoder* decoder, ALCcontext* context, cEFXFunctions* oALFunctions) 
00016                 : Context(context), Source(0), Decoder(decoder), Loop(false), Valid(false), 
00017                 EFX(oALFunctions), Filter(NULL), EffectSlotsAvailable(0), LastFilterTimeStamp(0)
00018 #else
00019         cAudioSource::cAudioSource(IAudioDecoder* decoder, ALCcontext* context)
00020                 : Context(context), Source(0), Decoder(decoder), Loop(false), Valid(false)
00021 #endif
00022     {
00023                 cAudioMutexBasicLock lock(Mutex);
00024 
00025                 for(int i=0; i<CAUDIO_SOURCE_NUM_BUFFERS; ++i)
00026                         Buffers[i] = 0;
00027 
00028 #ifdef CAUDIO_EFX_ENABLED
00029                 for(int i=0; i<CAUDIO_SOURCE_MAX_EFFECT_SLOTS; ++i)
00030                         Effects[i] = NULL;
00031 
00032                 for(int i=0; i<CAUDIO_SOURCE_MAX_EFFECT_SLOTS; ++i)
00033                         LastEffectTimeStamp[i] = 0;
00034 #endif
00035 
00036                 if(Decoder)
00037                         Decoder->grab();
00038 
00039                 //Generates 3 buffers for the ogg file
00040                 alGenBuffers(CAUDIO_SOURCE_NUM_BUFFERS, Buffers);
00041                 bool state = !checkError();
00042                 if(state)
00043                 {
00044                         //Creates one source to be stored.
00045                         alGenSources(1, &Source);
00046                         state = !checkError();
00047                 }
00048 #ifdef CAUDIO_EFX_ENABLED
00049                 Valid = state && (Decoder != NULL) && (Context != NULL) && (EFX != NULL);
00050 #else
00051                 Valid = state && (Decoder != NULL) && (Context != NULL);
00052 #endif
00053 
00054 #ifdef CAUDIO_EFX_ENABLED
00055                 int numSlots = 0;
00056                 ALCdevice* device = alcGetContextsDevice(Context);
00057                 alcGetIntegerv(device, ALC_MAX_AUXILIARY_SENDS, 1, &numSlots);
00058 
00059                 EffectSlotsAvailable = (numSlots <= CAUDIO_SOURCE_MAX_EFFECT_SLOTS) ? numSlots : CAUDIO_SOURCE_MAX_EFFECT_SLOTS;
00060 #endif
00061 
00062     }
00063 
00064     cAudioSource::~cAudioSource()
00065     {
00066                 cAudioMutexBasicLock lock(Mutex);
00067                 if(Decoder)
00068                         Decoder->drop();
00069 
00070 #ifdef CAUDIO_EFX_ENABLED
00071                 for(int i=0; i<CAUDIO_SOURCE_MAX_EFFECT_SLOTS; ++i)
00072                 {
00073                         if(Effects[i])
00074                                 Effects[i]->drop();
00075                         Effects[i] = NULL;
00076                 }
00077 
00078                 if(Filter)
00079                         Filter->drop();
00080                 Filter = NULL;
00081 #endif
00082                 unRegisterAllEventHandlers();
00083     }
00084 
00085         bool cAudioSource::play()
00086         {
00087                 cAudioMutexBasicLock lock(Mutex);
00088                 if (!isPaused()) 
00089         { 
00090             int queueSize = 0;
00091                         //Purges all buffers from the source
00092                         alSourcei(Source, AL_BUFFER, 0);
00093                         checkError();
00094             for(int u = 0; u < CAUDIO_SOURCE_NUM_BUFFERS; u++) 
00095             { 
00096                 int val = stream(Buffers[u]); 
00097  
00098                 if(val < 0) 
00099                 {  
00100                     return false;
00101                 } 
00102                 else if(val > 0) 
00103                     ++queueSize; 
00104             } 
00105             //Stores the sources 3 buffers to be used in the queue 
00106             alSourceQueueBuffers(Source, queueSize, Buffers); 
00107                         checkError();
00108         }
00109 #ifdef CAUDIO_EFX_ENABLED
00110                 updateFilter();
00111                 for(unsigned int i=0; i<CAUDIO_SOURCE_MAX_EFFECT_SLOTS; ++i)
00112                         updateEffect(i);
00113 #endif
00114         alSourcePlay(Source);
00115                 checkError();
00116                 getLogger()->logDebug("Audio Source", "Source playing.");
00117                 signalEvent(ON_PLAY);
00118                 oldState = AL_PLAYING;
00119         return true; 
00120     }
00121 
00122         bool cAudioSource::play2d(const bool& toLoop)
00123         {
00124                 cAudioMutexBasicLock lock(Mutex);
00125         alSourcei(Source, AL_SOURCE_RELATIVE, true);
00126         loop(toLoop);
00127         bool state = play();
00128                 checkError();
00129                 return state;
00130     }
00131 
00132         bool cAudioSource::play3d(const cVector3& position, const float& soundstr, const bool& toLoop)
00133         {
00134                 cAudioMutexBasicLock lock(Mutex);
00135         alSourcei(Source, AL_SOURCE_RELATIVE, false);
00136         setPosition(position);
00137         setStrength(soundstr);
00138         loop(toLoop);
00139         bool state = play();
00140                 checkError();
00141                 return state;
00142     }
00143 
00144         void cAudioSource::pause()
00145         {
00146                 cAudioMutexBasicLock lock(Mutex);
00147         alSourcePause(Source);
00148                 checkError();
00149                 getLogger()->logDebug("Audio Source", "Source paused.");
00150                 signalEvent(ON_PAUSE);
00151                 oldState = AL_PAUSED;
00152     }
00153      
00154         void cAudioSource::stop()
00155         {
00156                 cAudioMutexBasicLock lock(Mutex);
00157         alSourceStop(Source);
00158                 //Resets the audio to the beginning
00159                 Decoder->setPosition(0, false);
00160                 checkError();
00161                 getLogger()->logDebug("Audio Source", "Source stopped.");
00162                 signalEvent(ON_STOP);
00163                 oldState = AL_STOPPED;
00164     }
00165 
00166         void cAudioSource::loop(const bool& loop)
00167         {
00168                 cAudioMutexBasicLock lock(Mutex);
00169         Loop = loop;
00170     }
00171 
00172         bool cAudioSource::seek(const float& seconds, bool relative)
00173         {
00174                 bool state = false;
00175                 cAudioMutexBasicLock lock(Mutex);
00176         if(Decoder->isSeekingSupported())
00177         {
00178                         state = Decoder->seek(seconds, relative);
00179         }
00180                 return state;
00181     }
00182 
00183         float cAudioSource::getTotalAudioTime()
00184         {
00185                 return Decoder->getTotalTime();
00186         }
00187 
00188         int cAudioSource::getTotalAudioSize()
00189         {
00190                 return Decoder->getTotalSize();
00191         }
00192 
00193         int cAudioSource::getCompressedAudioSize()
00194         {
00195                 return Decoder->getCompressedSize();
00196         }
00197 
00198         float cAudioSource::getCurrentAudioTime()
00199         {
00200                 return Decoder->getCurrentTime();
00201         }
00202 
00203         int cAudioSource::getCurrentAudioPosition()
00204         {
00205                 return Decoder->getCurrentPosition();
00206         }
00207 
00208         int cAudioSource::getCurrentCompressedAudioPosition()
00209         {
00210                 return Decoder->getCurrentCompressedPosition();
00211         }
00212 
00213         bool cAudioSource::update()
00214         {
00215                 cAudioMutexBasicLock lock(Mutex);
00216 
00217                 int processed = 0;
00218                 bool active = true;
00219         if(isValid() || isPlaying())
00220                 {
00221 #ifdef CAUDIO_EFX_ENABLED
00222                         updateFilter();
00223                         for(unsigned int i=0; i<CAUDIO_SOURCE_MAX_EFFECT_SLOTS; ++i)
00224                                 updateEffect(i);
00225 #endif
00226 
00227                         //gets the sound source processed buffers
00228                         alGetSourcei(Source, AL_BUFFERS_PROCESSED, &processed);
00229 
00230                         //while there is more data refill buffers with audio data.
00231                         while (processed--)
00232                         {
00233                                 ALuint buffer;
00234                                 alSourceUnqueueBuffers(Source, 1, &buffer);
00235                                 active = stream(buffer);
00236 
00237                                 //if more in stream continue playing.
00238                                 if(active)
00239                                 {
00240                                         alSourceQueueBuffers(Source, 1, &buffer);
00241                                 }
00242 
00243                                 checkError();
00244                         }
00245 
00246                         signalEvent(ON_UPDATE);
00247                 }
00248 
00249                 ALenum state;
00250                 alGetSourcei(Source, AL_SOURCE_STATE, &state);
00251                 if(state == AL_STOPPED && oldState != state)
00252                 {
00253                         //Resets the audio to the beginning
00254                         Decoder->setPosition(0, false);
00255                         oldState = state;
00256                 }
00257 
00258                 return active;
00259     }
00260 
00261         void cAudioSource::release()
00262     {
00263                 cAudioMutexBasicLock lock(Mutex);
00264                 //Stops the audio Source
00265                 alSourceStop(Source);
00266                 empty();
00267                 //Deletes the source
00268                 alDeleteSources(1, &Source);
00269                 //deletes the last filled buffer
00270                 alDeleteBuffers(CAUDIO_SOURCE_NUM_BUFFERS, Buffers);
00271                 checkError();
00272                 getLogger()->logDebug("Audio Source", "Audio source released.");
00273                 signalEvent(ON_RELEASE);
00274     }
00275 
00276         const bool cAudioSource::isValid() const
00277         {
00278         return Valid;
00279         }
00280 
00281         const bool cAudioSource::isPlaying() const
00282         {
00283                 ALenum state = 0;
00284         alGetSourcei(Source, AL_SOURCE_STATE, &state);
00285         return (state == AL_PLAYING);
00286     }
00287 
00288         const bool cAudioSource::isPaused() const
00289         {
00290                 ALenum state = 0;
00291         alGetSourcei(Source, AL_SOURCE_STATE, &state);
00292         return (state == AL_PAUSED);
00293     }
00294 
00295         const bool cAudioSource::isStopped() const
00296         {
00297                 ALenum state = 0;
00298         alGetSourcei(Source, AL_SOURCE_STATE, &state);
00299                 return (state == AL_STOPPED);
00300     }
00301 
00302         const bool cAudioSource::isLooping() const
00303         {
00304                 return Loop;
00305         }
00306      
00307         void cAudioSource::setPosition(const cVector3& position)
00308         {
00309                 cAudioMutexBasicLock lock(Mutex);
00310         alSource3f(Source, AL_POSITION, position.x, position.y, position.z);
00311                 checkError();
00312     }
00313 
00314         void cAudioSource::setVelocity(const cVector3& velocity)
00315         {
00316                 cAudioMutexBasicLock lock(Mutex);
00317         alSource3f(Source, AL_VELOCITY, velocity.x, velocity.y, velocity.z);
00318                 checkError();
00319     }
00320 
00321         void cAudioSource::setDirection(const cVector3& direction)
00322         {
00323                 cAudioMutexBasicLock lock(Mutex);
00324         alSource3f(Source, AL_DIRECTION, direction.x, direction.y, direction.z);
00325                 checkError();
00326     }
00327 
00328         void cAudioSource::setRolloffFactor(const float& rolloff)
00329         {
00330                 cAudioMutexBasicLock lock(Mutex);
00331         alSourcef(Source, AL_ROLLOFF_FACTOR, rolloff);
00332                 checkError();
00333     }
00334 
00335         void cAudioSource::setStrength(const float& soundstrength)
00336         {
00337                 float inverseStrength = 0.0f;
00338                 if(soundstrength > 0.0f)
00339                         inverseStrength = 1.0f / soundstrength;
00340 
00341                 cAudioMutexBasicLock lock(Mutex);
00342         alSourcef(Source, AL_ROLLOFF_FACTOR, inverseStrength);
00343                 checkError();
00344     }
00345 
00346         void cAudioSource::setMinDistance(const float& minDistance)
00347         {
00348                 cAudioMutexBasicLock lock(Mutex);
00349         alSourcef(Source, AL_REFERENCE_DISTANCE, minDistance);
00350                 checkError();
00351         }
00352 
00353         void cAudioSource::setMaxDistance(const float& maxDistance)
00354         {
00355                 cAudioMutexBasicLock lock(Mutex);
00356         alSourcef(Source, AL_MAX_DISTANCE, maxDistance);
00357                 checkError();
00358         }
00359 
00360         void cAudioSource::setPitch(const float& pitch)
00361         {
00362                 cAudioMutexBasicLock lock(Mutex);
00363         alSourcef (Source, AL_PITCH, pitch);
00364                 checkError();
00365     }
00366 
00367         void cAudioSource::setVolume(const float& volume)
00368         {
00369                 cAudioMutexBasicLock lock(Mutex);
00370         alSourcef(Source, AL_GAIN, volume);
00371                 checkError();
00372     }
00373 
00374         void cAudioSource::setMinVolume(const float& minVolume)
00375         {
00376                 cAudioMutexBasicLock lock(Mutex);
00377         alSourcef(Source, AL_MIN_GAIN, minVolume);
00378                 checkError();
00379         }
00380 
00381         void cAudioSource::setMaxVolume(const float& maxVolume)
00382         {
00383                 cAudioMutexBasicLock lock(Mutex);
00384         alSourcef(Source, AL_MAX_GAIN, maxVolume);
00385                 checkError();
00386         }
00387 
00388         void cAudioSource::setInnerConeAngle(const float& innerAngle)
00389         {
00390                 cAudioMutexBasicLock lock(Mutex);
00391         alSourcef(Source, AL_CONE_INNER_ANGLE, innerAngle);
00392                 checkError();
00393         }
00394 
00395         void cAudioSource::setOuterConeAngle(const float& outerAngle)
00396         {
00397                 cAudioMutexBasicLock lock(Mutex);
00398         alSourcef(Source, AL_CONE_OUTER_ANGLE, outerAngle);
00399                 checkError();
00400         }
00401 
00402         void cAudioSource::setOuterConeVolume(const float& outerVolume)
00403         {
00404                 cAudioMutexBasicLock lock(Mutex);
00405         alSourcef(Source, AL_CONE_OUTER_GAIN, outerVolume);
00406                 checkError();
00407         }
00408 
00409         void cAudioSource::setDopplerStrength(const float& dstrength)
00410         {
00411                 cAudioMutexBasicLock lock(Mutex);
00412         alSourcef(Source, AL_DOPPLER_FACTOR, dstrength);
00413                 checkError();
00414     }
00415 
00416         void cAudioSource::setDopplerVelocity(const cVector3& dvelocity)
00417         {
00418                 cAudioMutexBasicLock lock(Mutex);
00419         alSource3f(Source, AL_DOPPLER_VELOCITY, dvelocity.x, dvelocity.y, dvelocity.z);
00420                 checkError();
00421     }
00422 
00423         void cAudioSource::move(const cVector3& position)
00424         {
00425                 cAudioMutexBasicLock lock(Mutex);
00426                 cVector3 oldPos = getPosition();
00427                 cVector3 velocity = position - oldPos;
00428 
00429         alSource3f(Source, AL_VELOCITY, velocity.x, velocity.y, velocity.z);
00430                 alSource3f(Source, AL_POSITION, position.x, position.y, position.z);
00431                 checkError();
00432         }
00433 
00434         const cVector3 cAudioSource::getPosition() const
00435         {
00436                 cVector3 position;
00437                 alGetSourcefv(Source, AL_POSITION, &position.x);
00438                 return position;
00439         }
00440 
00441         const cVector3 cAudioSource::getVelocity() const
00442         {
00443                 cVector3 velocity;
00444                 alGetSourcefv(Source, AL_VELOCITY, &velocity.x);
00445                 return velocity;
00446         }
00447 
00448         const cVector3 cAudioSource::getDirection() const
00449         {
00450                 cVector3 direction;
00451                 alGetSourcefv(Source, AL_DIRECTION, &direction.x);
00452                 return direction;
00453         }
00454 
00455         const float cAudioSource::getRolloffFactor() const
00456         {
00457                 float value = 0.0f;
00458                 alGetSourcef(Source, AL_ROLLOFF_FACTOR, &value);
00459                 return value;
00460         }
00461 
00462         const float cAudioSource::getStrength() const
00463         {
00464                 float value = 0.0f;
00465                 alGetSourcef(Source, AL_ROLLOFF_FACTOR, &value);
00466 
00467                 float inverseStrength = 0.0f;
00468                 if(value > 0.0f)
00469                         inverseStrength = 1.0f / value;
00470 
00471                 return inverseStrength;
00472         }
00473 
00474         const float cAudioSource::getMinDistance() const
00475         {
00476                 float value = 0.0f;
00477                 alGetSourcef(Source, AL_REFERENCE_DISTANCE, &value);
00478                 return value;
00479         }
00480 
00481         const float cAudioSource::getMaxDistance() const
00482         {
00483                 float value = 0.0f;
00484                 alGetSourcef(Source, AL_MAX_DISTANCE, &value);
00485                 return value;
00486         }
00487 
00488         const float cAudioSource::getPitch() const
00489         {
00490                 float value = 0.0f;
00491                 alGetSourcef(Source, AL_PITCH, &value);
00492                 return value;
00493         }
00494 
00495         const float cAudioSource::getVolume() const
00496         {
00497                 float value = 0.0f;
00498                 alGetSourcef(Source, AL_GAIN, &value);
00499                 return value;
00500         }
00501 
00502         const float cAudioSource::getMinVolume() const
00503         {
00504                 float value = 0.0f;
00505                 alGetSourcef(Source, AL_MIN_GAIN, &value);
00506                 return value;
00507         }
00508 
00509         const float cAudioSource::getMaxVolume() const
00510         {
00511                 float value = 0.0f;
00512                 alGetSourcef(Source, AL_MAX_GAIN, &value);
00513                 return value;
00514         }
00515 
00516         const float cAudioSource::getInnerConeAngle() const
00517         {
00518                 float value = 0.0f;
00519                 alGetSourcef(Source, AL_CONE_INNER_ANGLE, &value);
00520                 return value;
00521         }
00522 
00523         const float cAudioSource::getOuterConeAngle() const
00524         {
00525                 float value = 0.0f;
00526                 alGetSourcef(Source, AL_CONE_OUTER_ANGLE, &value);
00527                 return value;
00528         }
00529 
00530         const float cAudioSource::getOuterConeVolume() const
00531         {
00532                 float value = 0.0f;
00533                 alGetSourcef(Source, AL_CONE_OUTER_GAIN, &value);
00534                 return value;
00535         }
00536 
00537         const float cAudioSource::getDopplerStrength() const
00538         {
00539                 float value = 0.0f;
00540                 alGetSourcef(Source, AL_DOPPLER_FACTOR, &value);
00541                 return value;
00542         }
00543 
00544         const cVector3 cAudioSource::getDopplerVelocity() const
00545         {
00546                 cVector3 velocity;
00547                 alGetSourcefv(Source, AL_DOPPLER_VELOCITY, &velocity.x);
00548                 return velocity;
00549         }
00550 
00551 #ifdef CAUDIO_EFX_ENABLED
00552         unsigned int cAudioSource::getNumEffectSlotsAvailable() const
00553         {
00554                 return EffectSlotsAvailable;
00555         }
00556 
00557         bool cAudioSource::attachEffect(unsigned int slot, IEffect* effect)
00558         {
00559                 cAudioMutexBasicLock lock(Mutex);
00560                 if(slot < EffectSlotsAvailable)
00561                 {
00562                         Effects[slot] = effect;
00563 
00564                         if(Effects[slot])
00565                                 Effects[slot]->grab();
00566 
00567                         updateEffect(slot);
00568                         return true;
00569                 }
00570                 return false;
00571         }
00572 
00573         void cAudioSource::removeEffect(unsigned int slot)
00574         {
00575                 cAudioMutexBasicLock lock(Mutex);
00576                 if(slot < EffectSlotsAvailable)
00577                 {
00578                         if(Effects[slot])
00579                                 Effects[slot]->drop();
00580 
00581                         Effects[slot] = NULL;
00582                         LastEffectTimeStamp[slot] = 0;
00583                         updateEffect(slot, true);
00584                 }
00585         }
00586 
00587         bool cAudioSource::attachFilter(IFilter* filter)
00588         {
00589                 cAudioMutexBasicLock lock(Mutex);
00590                 Filter = filter;
00591 
00592                 if(Filter)
00593                         Filter->grab();
00594 
00595                 updateFilter();
00596                 return true;
00597         }
00598 
00599         void cAudioSource::removeFilter()
00600         {
00601                 cAudioMutexBasicLock lock(Mutex);
00602                 if(Filter)
00603                         Filter->drop();
00604                 Filter = NULL;
00605                 LastFilterTimeStamp = 0;
00606                 updateFilter(true);
00607         }
00608 #endif
00609 
00610     void cAudioSource::empty()
00611     {
00612         int queued = 0;
00613         alGetSourcei(Source, AL_BUFFERS_QUEUED, &queued);
00614 
00615         while (queued--)
00616         {
00617             ALuint buffer;
00618             alSourceUnqueueBuffers(Source, 1, &buffer);
00619                         checkError();
00620         }
00621     }
00622 
00623         bool cAudioSource::checkError()
00624     {
00625         int error = alGetError();
00626                 const char* errorString;
00627 
00628         if (error != AL_NO_ERROR)
00629         {
00630                         errorString = alGetString(error);
00631                         if(error == AL_OUT_OF_MEMORY)
00632                                 getLogger()->logCritical("Audio Source", "OpenAL Error: %s.", errorString);
00633                         else
00634                                 getLogger()->logError("Audio Source", "OpenAL Error: %s.", errorString);
00635                         return true;
00636         }
00637                 return false;
00638     }
00639 
00640     bool cAudioSource::stream(ALuint buffer)
00641     {
00642         if(Decoder)
00643         {
00644                 //stores the caculated data into buffer that is passed to output.
00645                         size_t totalread = 0;
00646                         unsigned int errorcount = 0;
00647                 char tempbuffer[CAUDIO_SOURCE_BUFFER_SIZE];
00648                         while( totalread < CAUDIO_SOURCE_BUFFER_SIZE )
00649                         {
00650                                 char tempbuffer2[CAUDIO_SOURCE_BUFFER_SIZE];
00651                                 int actualread = Decoder->readAudioData(tempbuffer2, CAUDIO_SOURCE_BUFFER_SIZE-totalread);
00652                                 if(actualread > 0)
00653                                 {
00654                                         memcpy(tempbuffer+totalread,tempbuffer2,actualread);
00655                                         totalread += actualread;
00656                                 }
00657                                 if(actualread < 0)
00658                                 {
00659                                         ++errorcount;
00660                                         getLogger()->logDebug("Audio Source", "Decoder returned an error: %i (%i of 3)", actualread, errorcount);
00661                                         if(errorcount >= 3)
00662                                         {
00663                                                 stop();
00664                                                 break;
00665                                         }
00666                                 }
00667                                 if(actualread == 0)
00668                                 {
00669                                         if(isLooping())
00670                                         {
00671                                                 //If we are to loop, set to the beginning and reload from the start
00672                                                 Decoder->setPosition(0, false);
00673                                                 getLogger()->logDebug("Audio Source", "Buffer looping.");
00674                                         }
00675                                         else
00676                                                 break;
00677                                 }
00678                         }
00679 
00680                 //Second check, in case looping is not enabled, we will return false for end of stream
00681                 if(totalread == 0)
00682                 {
00683                         return false;
00684                 }
00685                         getLogger()->logDebug("Audio Source", "Buffered %i bytes of data into buffer %i at %i hz.", totalread, buffer, Decoder->getFrequency());
00686             alBufferData(buffer, convertAudioFormatEnum(Decoder->getFormat()), tempbuffer, totalread, Decoder->getFrequency());
00687                         checkError();
00688             return true;
00689         }
00690                 return false;
00691     }
00692 
00693         ALenum cAudioSource::convertAudioFormatEnum(AudioFormats format)
00694         {
00695                 switch(format)
00696                 {
00697                 case EAF_8BIT_MONO:
00698                         return AL_FORMAT_MONO8;
00699                 case EAF_16BIT_MONO:
00700                         return AL_FORMAT_MONO16;
00701                 case EAF_8BIT_STEREO:
00702                         return AL_FORMAT_STEREO8;
00703                 case EAF_16BIT_STEREO:
00704                         return AL_FORMAT_STEREO16;
00705                 default:
00706                         return AL_FORMAT_MONO8;
00707                 };
00708         }
00709 
00710 #ifdef CAUDIO_EFX_ENABLED
00711         void cAudioSource::updateFilter(bool remove)
00712         {
00713                 if(!remove)
00714                 {
00715                         if(Filter && Filter->isValid())
00716                         {
00717                                 if(LastFilterTimeStamp != Filter->getLastUpdated())
00718                                 {
00719                                         LastFilterTimeStamp = Filter->getLastUpdated();
00720                                         cFilter* theFilter = static_cast<cFilter*>(Filter);
00721                                         if(theFilter)
00722                                         {
00723                                                 alSourcei(Source, AL_DIRECT_FILTER, theFilter->getOpenALFilter());
00724                                                 checkError();
00725                                                 return;
00726                                         }
00727                                 }
00728                                 return;
00729                         }
00730                 }
00731                 alSourcei(Source, AL_DIRECT_FILTER, AL_FILTER_NULL);
00732                 checkError();
00733         }
00734 
00735         void cAudioSource::updateEffect(unsigned int slot, bool remove)
00736         {
00737                 if(slot < EffectSlotsAvailable)
00738                 {
00739                         if(!remove)
00740                         {
00741                                 if(Effects[slot] && Effects[slot]->isValid())
00742                                 {
00743                                         if(LastEffectTimeStamp[slot] != Effects[slot]->getLastUpdated())
00744                                         {
00745                                                 LastEffectTimeStamp[slot] = Effects[slot]->getLastUpdated();
00746                                                 cEffect* theEffect = static_cast<cEffect*>(Effects[slot]);
00747                                                 if(theEffect)
00748                                                 {
00749                                                         ALuint filterID = AL_FILTER_NULL;
00750                                                         cFilter* theFilter = static_cast<cFilter*>(theEffect->getFilter());
00751                                                         if(theFilter)
00752                                                         {
00753                                                                 filterID = theFilter->getOpenALFilter();
00754                                                         }
00755                                                         alSource3i(Source, AL_AUXILIARY_SEND_FILTER, theEffect->getOpenALEffectSlot(), slot, filterID);
00756                                                         checkError();
00757                                                         return;
00758                                                 }
00759                                         }
00760                                         return;
00761                                 }
00762                         }
00763                         alSource3i(Source, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, slot, AL_FILTER_NULL);
00764                         checkError();
00765                 }
00766         }
00767 #endif
00768 
00769         void cAudioSource::registerEventHandler(ISourceEventHandler* handler)
00770         {
00771                 if(handler)
00772                 {
00773                         eventHandlerList.push_back(handler);
00774                 }
00775         }
00776 
00777         void cAudioSource::unRegisterEventHandler(ISourceEventHandler* handler)
00778         {
00779                 if(handler)
00780                 {
00781                         eventHandlerList.remove(handler);
00782                 }
00783         }
00784 
00785         void cAudioSource::unRegisterAllEventHandlers()
00786         {
00787                 eventHandlerList.clear();
00788         }
00789 
00790         void cAudioSource::signalEvent(Events sevent)
00791         {
00792                 cAudioMutexBasicLock lock(Mutex);
00793                 std::list<ISourceEventHandler*>::iterator it = eventHandlerList.begin();
00794 
00795                 if(it != eventHandlerList.end()){
00796 
00797                         switch(sevent){
00798 
00799                                 case ON_UPDATE:
00800 
00801                                         for(it; it != eventHandlerList.end(); it++){
00802                                                 (*it)->onUpdate();
00803                                         }
00804 
00805                                         break;
00806 
00807                                 case ON_RELEASE:
00808 
00809                                         for(it; it != eventHandlerList.end(); it++){
00810                                                 (*it)->onRelease();
00811                                         }
00812 
00813                                         break;
00814 
00815                                 case ON_PLAY:
00816 
00817                                         for(it; it != eventHandlerList.end(); it++){
00818                                                 (*it)->onPlay();
00819                                         }
00820 
00821 
00822                                         break;
00823 
00824                                 case ON_PAUSE:
00825 
00826                                         for(it; it != eventHandlerList.end(); it++){
00827                                                 (*it)->onPause();
00828                                         }
00829 
00830                                         break;
00831 
00832                                 case ON_STOP:
00833 
00834                                         for(it; it != eventHandlerList.end(); it++){
00835                                                 (*it)->onStop();
00836                                         }
00837 
00838                                         break;
00839                         }
00840                 }
00841         }
00842 }
 All Classes Namespaces Functions Variables Enumerations

Generated on Sat Feb 20 22:55:08 2010 for cAudio by  doxygen 1.6.2