* Now its possible to drop a cAudioSource which takes then care of releasing itself.

-- The user can call IAudioManager::release with an cAudioSource but its also possible just to call drop on the AudioSource until true is returned and the AudioSource will be removed from the IAudioManager and deleted.
This commit is contained in:
Murat Suri 2011-06-12 21:57:42 +00:00
parent 313a043eff
commit cf79cb7dc1
5 changed files with 41 additions and 16 deletions

View File

@ -65,6 +65,8 @@ int main(int argc, char* argv[])
cAudio::cAudioSleep(10000);
}
mysound->drop();
//Delete all IAudio sounds
manager->releaseAllSources();
//Shutdown cAudio

View File

@ -17,6 +17,7 @@
#include "cVector3.h"
#include "ILogger.h"
#include "cSTLAllocator.h"
#include "IAudioDeviceContext.h"
namespace cAudio
{
@ -34,9 +35,9 @@ namespace cAudio
};
#if CAUDIO_EFX_ENABLED == 1
cAudioSource(IAudioDecoder* decoder, ALCcontext* context, cEFXFunctions* oALFunctions);
cAudioSource(IAudioDecoder* decoder, IAudioDeviceContext* context, cEFXFunctions* oALFunctions);
#else
cAudioSource(IAudioDecoder* decoder, ALCcontext* context);
cAudioSource(IAudioDecoder* decoder, IAudioDeviceContext* context);
#endif
~cAudioSource();
@ -113,6 +114,8 @@ namespace cAudio
virtual void unRegisterEventHandler(ISourceEventHandler* handler);
virtual void unRegisterAllEventHandlers();
virtual bool drop(); //! Override the default behavior
#if CAUDIO_EFX_ENABLED == 1
virtual unsigned int getNumEffectSlotsAvailable() const;
virtual bool attachEffect(unsigned int slot, IEffect* effect);
@ -137,8 +140,8 @@ namespace cAudio
ALenum convertAudioFormatEnum(AudioFormats format);
//! The context that owns this source
ALCcontext* Context;
IAudioDeviceContext* Context;
//! Internal audio buffers
ALuint Buffers[CAUDIO_SOURCE_NUM_BUFFERS];
//! OpenAL source

View File

@ -16,13 +16,13 @@ namespace cAudio
virtual ~IRefCounted() { }
//! Increments the reference count by one.
void grab()
virtual void grab()
{
++RefCount;
}
//! Decrements the reference count by one. If it hits zero, this object is deleted.
bool drop()
virtual bool drop()
{
--RefCount;
if (RefCount < 1)
@ -39,7 +39,7 @@ namespace cAudio
return RefCount;
}
private:
protected:
int RefCount;
};
}

View File

@ -119,9 +119,9 @@ namespace cAudio
if(decoder && decoder->isValid())
{
#if CAUDIO_EFX_ENABLED == 1
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, ((cOpenALDeviceContext*)AudioContext)->getOpenALContext(), ((cAudioEffects*)getEffects())->getEFXInterface());
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, AudioContext, ((cAudioEffects*)getEffects())->getEFXInterface());
#else
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, ((cOpenALDeviceContext*)AudioContext)->getOpenALContext());
IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, AudioContext);
#endif
decoder->drop();
@ -473,7 +473,7 @@ namespace cAudio
void cAudioManager::release(IAudioSource* source)
{
if(source)
{
{
cAudioMutexBasicLock lock(Mutex);
audioIndexIterator it = audioIndex.begin();
for ( it=audioIndex.begin(); it != audioIndex.end(); it++ )
@ -488,8 +488,13 @@ namespace cAudio
{
if(source == audioSources[i])
{
audioSources.erase(audioSources.begin()+i);
source->drop();
audioSources.erase(audioSources.begin()+i);
if (source->getReferenceCount() <= 1)
CAUDIO_DELETE source;
else
source->drop();
break;
}
}

View File

@ -8,17 +8,20 @@
#include "cFilter.h"
#include "cEffect.h"
#include "cAudioSleep.h"
#include <string.h>
#if CAUDIO_EFX_ENABLED == 1
#include "cOpenALDeviceContext.h"
#endif
namespace cAudio
{
#if CAUDIO_EFX_ENABLED == 1
cAudioSource::cAudioSource(IAudioDecoder* decoder, ALCcontext* context, cEFXFunctions* oALFunctions)
cAudioSource::cAudioSource(IAudioDecoder* decoder, IAudioDeviceContext* context, cEFXFunctions* oALFunctions)
: Context(context), Source(0), Decoder(decoder), Loop(false), Valid(false),
EFX(oALFunctions), Filter(NULL), EffectSlotsAvailable(0), LastFilterTimeStamp(0)
#else
cAudioSource::cAudioSource(IAudioDecoder* decoder, ALCcontext* context)
cAudioSource::cAudioSource(IAudioDecoder* decoder, IAudioDeviceContext* context)
: Context(context), Source(0), Decoder(decoder), Loop(false), Valid(false)
#endif
{
@ -52,7 +55,7 @@ namespace cAudio
Valid = state && (Decoder != NULL) && (Context != NULL) && (EFX != NULL);
int numSlots = 0;
ALCdevice* device = alcGetContextsDevice(Context);
ALCdevice* device = alcGetContextsDevice(((cOpenALDeviceContext*)Context)->getOpenALContext());
alcGetIntegerv(device, ALC_MAX_AUXILIARY_SENDS, 1, &numSlots);
EffectSlotsAvailable = (numSlots <= CAUDIO_SOURCE_MAX_EFFECT_SLOTS) ? numSlots : CAUDIO_SOURCE_MAX_EFFECT_SLOTS;
@ -95,6 +98,18 @@ namespace cAudio
unRegisterAllEventHandlers();
}
bool cAudioSource::drop()
{
--RefCount;
if (RefCount == 0)
{
Context->getAudioManager()->release(this);
return true;
}
return false;
}
bool cAudioSource::play()
{
cAudioMutexBasicLock lock(Mutex);