From e66ee1ca9f15e1994811609ec803e856ddef9d8c Mon Sep 17 00:00:00 2001 From: Joerg Mueller Date: Wed, 24 Oct 2012 21:33:44 +0000 Subject: Audaspace: RAII locking implementation. This should fix bug [#32096] Background music stops when playing 3D sounds. --- intern/audaspace/CMakeLists.txt | 2 + intern/audaspace/OpenAL/AUD_OpenALDevice.cpp | 282 +++++++++++---------- .../audaspace/intern/AUD_AnimateableProperty.cpp | 14 +- intern/audaspace/intern/AUD_AnimateableProperty.h | 7 +- intern/audaspace/intern/AUD_C-API.cpp | 6 +- intern/audaspace/intern/AUD_IDevice.h | 3 +- intern/audaspace/intern/AUD_ILockable.h | 21 ++ intern/audaspace/intern/AUD_MutexLock.h | 24 ++ intern/audaspace/intern/AUD_SequencerEntry.cpp | 53 +--- intern/audaspace/intern/AUD_SequencerEntry.h | 7 +- intern/audaspace/intern/AUD_SequencerFactory.cpp | 33 +-- intern/audaspace/intern/AUD_SequencerFactory.h | 7 +- intern/audaspace/intern/AUD_SequencerHandle.cpp | 8 +- intern/audaspace/intern/AUD_SequencerReader.cpp | 5 +- intern/audaspace/intern/AUD_SoftwareDevice.cpp | 58 ++--- 15 files changed, 276 insertions(+), 254 deletions(-) create mode 100644 intern/audaspace/intern/AUD_ILockable.h create mode 100644 intern/audaspace/intern/AUD_MutexLock.h (limited to 'intern/audaspace') diff --git a/intern/audaspace/CMakeLists.txt b/intern/audaspace/CMakeLists.txt index dc4ca7903cd..1671194653a 100644 --- a/intern/audaspace/CMakeLists.txt +++ b/intern/audaspace/CMakeLists.txt @@ -94,6 +94,7 @@ set(SRC intern/AUD_IDevice.h intern/AUD_IFactory.h intern/AUD_IHandle.h + intern/AUD_ILockable.h intern/AUD_IReader.h intern/AUD_IWriter.h intern/AUD_JOSResampleFactory.cpp @@ -108,6 +109,7 @@ set(SRC intern/AUD_Mixer.h intern/AUD_MixerFactory.cpp intern/AUD_MixerFactory.h + intern/AUD_MutexLock.h intern/AUD_NULLDevice.cpp intern/AUD_NULLDevice.h intern/AUD_PyInit.h diff --git a/intern/audaspace/OpenAL/AUD_OpenALDevice.cpp b/intern/audaspace/OpenAL/AUD_OpenALDevice.cpp index f68d41f6a1c..c7d9612020b 100644 --- a/intern/audaspace/OpenAL/AUD_OpenALDevice.cpp +++ b/intern/audaspace/OpenAL/AUD_OpenALDevice.cpp @@ -31,6 +31,7 @@ #include "AUD_IFactory.h" #include "AUD_IReader.h" #include "AUD_ConverterReader.h" +#include "AUD_MutexLock.h" #include #include @@ -125,7 +126,7 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::pause() { if(m_status) { - m_device->lock(); + AUD_MutexLock lock(*m_device); if(m_status == AUD_STATUS_PLAYING) { @@ -135,12 +136,9 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::pause() alSourcePause(m_source); m_status = AUD_STATUS_PAUSED; - m_device->unlock(); return true; } - - m_device->unlock(); } return false; @@ -150,7 +148,7 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::resume() { if(m_status) { - m_device->lock(); + AUD_MutexLock lock(*m_device); if(m_status == AUD_STATUS_PAUSED) { @@ -159,11 +157,8 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::resume() m_device->start(); m_status = AUD_STATUS_PLAYING; - m_device->unlock(); return true; } - - m_device->unlock(); } return false; @@ -174,7 +169,10 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::stop() if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return false; // AUD_XXX Create a reference of our own object so that it doesn't get // deleted before the end of this function @@ -185,8 +183,6 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::stop() else m_device->m_pausedSounds.remove(This); - m_device->unlock(); - alDeleteSources(1, &m_source); if(!m_isBuffered) alDeleteBuffers(CYCLE_BUFFERS, m_buffers); @@ -208,11 +204,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setKeep(bool keep) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - m_keep = keep; + if(!m_status) + return false; - m_device->unlock(); + m_keep = keep; return true; } @@ -222,7 +219,10 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::seek(float position) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return false; if(m_isBuffered) alSourcef(m_source, AL_SEC_OFFSET, position); @@ -272,17 +272,18 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::seek(float position) } } - m_device->unlock(); - return true; } float AUD_OpenALDevice::AUD_OpenALHandle::getPosition() { if(!m_status) - return 0.0f; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return 0.0f; float position = 0.0f; @@ -295,8 +296,6 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getPosition() CYCLE_BUFFERS) / (float)specs.rate; } - m_device->unlock(); - return position; } @@ -310,13 +309,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getVolume() float result = std::numeric_limits::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_GAIN, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_GAIN, &result); return result; } @@ -326,11 +326,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setVolume(float volume) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_GAIN, volume); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_GAIN, volume); return true; } @@ -340,13 +341,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getPitch() float result = std::numeric_limits::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_PITCH, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_PITCH, &result); return result; } @@ -356,11 +358,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setPitch(float pitch) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_PITCH, pitch); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_PITCH, pitch); return true; } @@ -385,13 +388,14 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setStopCallback(stopCallback callback, if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return false; m_stop = callback; m_stop_data = data; - m_device->unlock(); - return true; } @@ -404,15 +408,16 @@ AUD_Vector3 AUD_OpenALDevice::AUD_OpenALHandle::getSourceLocation() AUD_Vector3 result = AUD_Vector3(0, 0, 0); if(!m_status) - return result; + return false; + + AUD_MutexLock lock(*m_device); - m_device->lock(); + if(!m_status) + return result; ALfloat p[3]; alGetSourcefv(m_source, AL_POSITION, p); - m_device->unlock(); - result = AUD_Vector3(p[0], p[1], p[2]); return result; @@ -423,11 +428,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceLocation(const AUD_Vector3& lo if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcefv(m_source, AL_POSITION, (ALfloat*)location.get()); + if(!m_status) + return false; - m_device->unlock(); + alSourcefv(m_source, AL_POSITION, (ALfloat*)location.get()); return true; } @@ -437,15 +443,16 @@ AUD_Vector3 AUD_OpenALDevice::AUD_OpenALHandle::getSourceVelocity() AUD_Vector3 result = AUD_Vector3(0, 0, 0); if(!m_status) - return result; + return false; + + AUD_MutexLock lock(*m_device); - m_device->lock(); + if(!m_status) + return result; ALfloat v[3]; alGetSourcefv(m_source, AL_VELOCITY, v); - m_device->unlock(); - result = AUD_Vector3(v[0], v[1], v[2]); return result; @@ -456,11 +463,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceVelocity(const AUD_Vector3& ve if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcefv(m_source, AL_VELOCITY, (ALfloat*)velocity.get()); + if(!m_status) + return false; - m_device->unlock(); + alSourcefv(m_source, AL_VELOCITY, (ALfloat*)velocity.get()); return true; } @@ -472,9 +480,6 @@ AUD_Quaternion AUD_OpenALDevice::AUD_OpenALHandle::getSourceOrientation() bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceOrientation(const AUD_Quaternion& orientation) { - if(!m_status) - return false; - ALfloat direction[3]; direction[0] = -2 * (orientation.w() * orientation.y() + orientation.x() * orientation.z()); @@ -482,11 +487,16 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setSourceOrientation(const AUD_Quaterni orientation.z() * orientation.y()); direction[2] = 2 * (orientation.x() * orientation.x() + orientation.y() * orientation.y()) - 1; - m_device->lock(); - alSourcefv(m_source, AL_DIRECTION, direction); + if(!m_status) + return false; - m_device->unlock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return false; + + alSourcefv(m_source, AL_DIRECTION, direction); m_orientation = orientation; @@ -500,11 +510,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::isRelative() if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcei(m_source, AL_SOURCE_RELATIVE, &result); + if(!m_status) + return false; - m_device->unlock(); + alGetSourcei(m_source, AL_SOURCE_RELATIVE, &result); return result; } @@ -514,11 +525,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setRelative(bool relative) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcei(m_source, AL_SOURCE_RELATIVE, relative); + if(!m_status) + return false; - m_device->unlock(); + alSourcei(m_source, AL_SOURCE_RELATIVE, relative); return true; } @@ -528,13 +540,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getVolumeMaximum() float result = std::numeric_limits::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_MAX_GAIN, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_MAX_GAIN, &result); return result; } @@ -544,11 +557,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setVolumeMaximum(float volume) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_MAX_GAIN, volume); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_MAX_GAIN, volume); return true; } @@ -558,13 +572,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getVolumeMinimum() float result = std::numeric_limits::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_MIN_GAIN, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_MIN_GAIN, &result); return result; } @@ -574,11 +589,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setVolumeMinimum(float volume) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_MIN_GAIN, volume); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_MIN_GAIN, volume); return true; } @@ -588,13 +604,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getDistanceMaximum() float result = std::numeric_limits::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_MAX_DISTANCE, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_MAX_DISTANCE, &result); return result; } @@ -604,11 +621,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setDistanceMaximum(float distance) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_MAX_DISTANCE, distance); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_MAX_DISTANCE, distance); return true; } @@ -618,13 +636,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getDistanceReference() float result = std::numeric_limits::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_REFERENCE_DISTANCE, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_REFERENCE_DISTANCE, &result); return result; } @@ -634,11 +653,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setDistanceReference(float distance) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_REFERENCE_DISTANCE, distance); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_REFERENCE_DISTANCE, distance); return true; } @@ -648,13 +668,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getAttenuation() float result = std::numeric_limits::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_ROLLOFF_FACTOR, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_ROLLOFF_FACTOR, &result); return result; } @@ -664,11 +685,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setAttenuation(float factor) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_ROLLOFF_FACTOR, factor); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_ROLLOFF_FACTOR, factor); return true; } @@ -678,13 +700,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getConeAngleOuter() float result = std::numeric_limits::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_CONE_OUTER_ANGLE, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_CONE_OUTER_ANGLE, &result); return result; } @@ -694,11 +717,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setConeAngleOuter(float angle) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_CONE_OUTER_ANGLE, angle); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_CONE_OUTER_ANGLE, angle); return true; } @@ -708,13 +732,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getConeAngleInner() float result = std::numeric_limits::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_CONE_INNER_ANGLE, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_CONE_INNER_ANGLE, &result); return result; } @@ -724,11 +749,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setConeAngleInner(float angle) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_CONE_INNER_ANGLE, angle); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_CONE_INNER_ANGLE, angle); return true; } @@ -738,13 +764,14 @@ float AUD_OpenALDevice::AUD_OpenALHandle::getConeVolumeOuter() float result = std::numeric_limits::quiet_NaN(); if(!m_status) - return result; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alGetSourcef(m_source, AL_CONE_OUTER_GAIN, &result); + if(!m_status) + return result; - m_device->unlock(); + alGetSourcef(m_source, AL_CONE_OUTER_GAIN, &result); return result; } @@ -754,11 +781,12 @@ bool AUD_OpenALDevice::AUD_OpenALHandle::setConeVolumeOuter(float volume) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - alSourcef(m_source, AL_CONE_OUTER_GAIN, volume); + if(!m_status) + return false; - m_device->unlock(); + alSourcef(m_source, AL_CONE_OUTER_GAIN, volume); return true; } @@ -776,7 +804,7 @@ static void *AUD_openalRunThread(void *device) void AUD_OpenALDevice::start(bool join) { - lock(); + AUD_MutexLock lock(*this); if(!m_playing) { @@ -793,8 +821,6 @@ void AUD_OpenALDevice::start(bool join) m_playing = true; } - - unlock(); } void AUD_OpenALDevice::updateStreams() @@ -1177,7 +1203,8 @@ AUD_Reference AUD_OpenALDevice::play(AUD_Reference rea if(!getFormat(format, specs)) return AUD_Reference(); - lock(); + AUD_MutexLock lock(*this); + alcSuspendContext(m_context); AUD_Reference sound; @@ -1190,7 +1217,6 @@ AUD_Reference AUD_OpenALDevice::play(AUD_Reference rea catch(AUD_Exception&) { alcProcessContext(m_context); - unlock(); throw; } @@ -1201,8 +1227,6 @@ AUD_Reference AUD_OpenALDevice::play(AUD_Reference rea start(); - unlock(); - return AUD_Reference(sound); } @@ -1285,7 +1309,8 @@ AUD_Reference AUD_OpenALDevice::play(AUD_Reference fa void AUD_OpenALDevice::stopAll() { - lock(); + AUD_MutexLock lock(*this); + alcSuspendContext(m_context); while(!m_playingSounds.empty()) @@ -1295,7 +1320,6 @@ void AUD_OpenALDevice::stopAll() m_pausedSounds.front()->stop(); alcProcessContext(m_context); - unlock(); } void AUD_OpenALDevice::lock() diff --git a/intern/audaspace/intern/AUD_AnimateableProperty.cpp b/intern/audaspace/intern/AUD_AnimateableProperty.cpp index 8a5c538c094..0b333e687ff 100644 --- a/intern/audaspace/intern/AUD_AnimateableProperty.cpp +++ b/intern/audaspace/intern/AUD_AnimateableProperty.cpp @@ -28,6 +28,7 @@ #include "AUD_AnimateableProperty.h" +#include "AUD_MutexLock.h" #include #include @@ -63,17 +64,15 @@ void AUD_AnimateableProperty::unlock() void AUD_AnimateableProperty::write(const float* data) { - lock(); + AUD_MutexLock lock(*this); m_isAnimated = false; memcpy(getBuffer(), data, m_count * sizeof(float)); - - unlock(); } void AUD_AnimateableProperty::write(const float* data, int position, int count) { - lock(); + AUD_MutexLock lock(*this); m_isAnimated = true; @@ -87,18 +86,15 @@ void AUD_AnimateableProperty::write(const float* data, int position, int count) for(int i = pos; i < position; i++) memcpy(buf + i * m_count, buf + (pos - 1) * m_count, m_count * sizeof(float)); - - unlock(); } void AUD_AnimateableProperty::read(float position, float* out) { - lock(); + AUD_MutexLock lock(*this); if(!m_isAnimated) { memcpy(out, getBuffer(), m_count * sizeof(float)); - unlock(); return; } @@ -147,8 +143,6 @@ void AUD_AnimateableProperty::read(float position, float* out) (t3 - 2 * t2 + t) * m0 + (t3 - t2) * m1; } } - - unlock(); } bool AUD_AnimateableProperty::isAnimated() const diff --git a/intern/audaspace/intern/AUD_AnimateableProperty.h b/intern/audaspace/intern/AUD_AnimateableProperty.h index 2f25e330ebd..322748ad571 100644 --- a/intern/audaspace/intern/AUD_AnimateableProperty.h +++ b/intern/audaspace/intern/AUD_AnimateableProperty.h @@ -31,13 +31,14 @@ #define __AUD_ANIMATEABLEPROPERTY_H__ #include "AUD_Buffer.h" +#include "AUD_ILockable.h" #include /** * This class saves animation data for float properties. */ -class AUD_AnimateableProperty : private AUD_Buffer +class AUD_AnimateableProperty : private AUD_Buffer, public AUD_ILockable { private: /// The count of floats for a single property. @@ -68,12 +69,12 @@ public: /** * Locks the property. */ - void lock(); + virtual void lock(); /** * Unlocks the previously locked property. */ - void unlock(); + virtual void unlock(); /** * Writes the properties value and marks it non-animated. diff --git a/intern/audaspace/intern/AUD_C-API.cpp b/intern/audaspace/intern/AUD_C-API.cpp index 46bba237cff..a1da90b073e 100644 --- a/intern/audaspace/intern/AUD_C-API.cpp +++ b/intern/audaspace/intern/AUD_C-API.cpp @@ -68,6 +68,7 @@ #include "AUD_SequencerFactory.h" #include "AUD_SequencerEntry.h" #include "AUD_SilenceFactory.h" +#include "AUD_MutexLock.h" #ifdef WITH_SDL #include "AUD_SDLDevice.h" @@ -858,13 +859,12 @@ AUD_Handle *AUD_pauseAfter(AUD_Handle *handle, float seconds) AUD_Reference silence = new AUD_SilenceFactory; AUD_Reference limiter = new AUD_LimiterFactory(silence, 0, seconds); - AUD_device->lock(); + AUD_MutexLock lock(*AUD_device); try { AUD_Handle handle2 = AUD_device->play(limiter); if (!handle2.isNull()) { handle2->setStopCallback((stopCallback)pauseSound, handle); - AUD_device->unlock(); return new AUD_Handle(handle2); } } @@ -872,8 +872,6 @@ AUD_Handle *AUD_pauseAfter(AUD_Handle *handle, float seconds) { } - AUD_device->unlock(); - return NULL; } diff --git a/intern/audaspace/intern/AUD_IDevice.h b/intern/audaspace/intern/AUD_IDevice.h index 1d6f8ca6efb..f4d6635e79a 100644 --- a/intern/audaspace/intern/AUD_IDevice.h +++ b/intern/audaspace/intern/AUD_IDevice.h @@ -35,6 +35,7 @@ #include "AUD_IFactory.h" #include "AUD_IReader.h" #include "AUD_IHandle.h" +#include "AUD_ILockable.h" /** * This class represents an output device for sound sources. @@ -44,7 +45,7 @@ * \warning Thread safety must be insured so that no reader is beeing called * twice at the same time. */ -class AUD_IDevice +class AUD_IDevice : public AUD_ILockable { public: /** diff --git a/intern/audaspace/intern/AUD_ILockable.h b/intern/audaspace/intern/AUD_ILockable.h new file mode 100644 index 00000000000..9bc417504fe --- /dev/null +++ b/intern/audaspace/intern/AUD_ILockable.h @@ -0,0 +1,21 @@ +#ifndef AUD_ILOCKABLE_H +#define AUD_ILOCKABLE_H + +/** + * This class provides an interface for lockable objects. + * The main reason for this interface is to be used with AUD_MutexLock. + */ +class AUD_ILockable +{ +public: + /** + * Locks the object. + */ + virtual void lock()=0; + /** + * Unlocks the previously locked object. + */ + virtual void unlock()=0; +}; + +#endif // AUD_ILOCKABLE_H diff --git a/intern/audaspace/intern/AUD_MutexLock.h b/intern/audaspace/intern/AUD_MutexLock.h new file mode 100644 index 00000000000..b6f6d2b4334 --- /dev/null +++ b/intern/audaspace/intern/AUD_MutexLock.h @@ -0,0 +1,24 @@ +#ifndef AUD_MUTEXLOCK_H +#define AUD_MUTEXLOCK_H + +#include "AUD_ILockable.h" + +class AUD_MutexLock +{ +public: + inline AUD_MutexLock(AUD_ILockable& lockable) : + lockable(lockable) + { + lockable.lock(); + } + + inline ~AUD_MutexLock() + { + lockable.unlock(); + } + +private: + AUD_ILockable& lockable; +}; + +#endif // AUD_MUTEXLOCK_H diff --git a/intern/audaspace/intern/AUD_SequencerEntry.cpp b/intern/audaspace/intern/AUD_SequencerEntry.cpp index 6e6e2397626..96fd15a0bf8 100644 --- a/intern/audaspace/intern/AUD_SequencerEntry.cpp +++ b/intern/audaspace/intern/AUD_SequencerEntry.cpp @@ -29,6 +29,7 @@ #include "AUD_SequencerEntry.h" #include "AUD_SequencerReader.h" +#include "AUD_MutexLock.h" #include #include @@ -87,20 +88,18 @@ void AUD_SequencerEntry::unlock() void AUD_SequencerEntry::setSound(AUD_Reference sound) { - lock(); + AUD_MutexLock lock(*this); if(m_sound.get() != sound.get()) { m_sound = sound; m_sound_status++; } - - unlock(); } void AUD_SequencerEntry::move(float begin, float end, float skip) { - lock(); + AUD_MutexLock lock(*this); if(m_begin != begin || m_skip != skip || m_end != end) { @@ -109,17 +108,13 @@ void AUD_SequencerEntry::move(float begin, float end, float skip) m_end = end; m_pos_status++; } - - unlock(); } void AUD_SequencerEntry::mute(bool mute) { - lock(); + AUD_MutexLock lock(*this); m_muted = mute; - - unlock(); } int AUD_SequencerEntry::getID() const @@ -150,7 +145,7 @@ void AUD_SequencerEntry::updateAll(float volume_max, float volume_min, float dis float distance_reference, float attenuation, float cone_angle_outer, float cone_angle_inner, float cone_volume_outer) { - lock(); + AUD_MutexLock lock(*this); if(volume_max != m_volume_max) { @@ -199,8 +194,6 @@ void AUD_SequencerEntry::updateAll(float volume_max, float volume_min, float dis m_cone_volume_outer = cone_volume_outer; m_status++; } - - unlock(); } bool AUD_SequencerEntry::isRelative() @@ -210,15 +203,13 @@ bool AUD_SequencerEntry::isRelative() void AUD_SequencerEntry::setRelative(bool relative) { - lock(); + AUD_MutexLock lock(*this); if(m_relative != relative) { m_relative = relative; m_status++; } - - unlock(); } float AUD_SequencerEntry::getVolumeMaximum() @@ -228,12 +219,10 @@ float AUD_SequencerEntry::getVolumeMaximum() void AUD_SequencerEntry::setVolumeMaximum(float volume) { - lock(); + AUD_MutexLock lock(*this); m_volume_max = volume; m_status++; - - unlock(); } float AUD_SequencerEntry::getVolumeMinimum() @@ -243,12 +232,10 @@ float AUD_SequencerEntry::getVolumeMinimum() void AUD_SequencerEntry::setVolumeMinimum(float volume) { - lock(); + AUD_MutexLock lock(*this); m_volume_min = volume; m_status++; - - unlock(); } float AUD_SequencerEntry::getDistanceMaximum() @@ -258,12 +245,10 @@ float AUD_SequencerEntry::getDistanceMaximum() void AUD_SequencerEntry::setDistanceMaximum(float distance) { - lock(); + AUD_MutexLock lock(*this); m_distance_max = distance; m_status++; - - unlock(); } float AUD_SequencerEntry::getDistanceReference() @@ -273,12 +258,10 @@ float AUD_SequencerEntry::getDistanceReference() void AUD_SequencerEntry::setDistanceReference(float distance) { - lock(); + AUD_MutexLock lock(*this); m_distance_reference = distance; m_status++; - - unlock(); } float AUD_SequencerEntry::getAttenuation() @@ -288,12 +271,10 @@ float AUD_SequencerEntry::getAttenuation() void AUD_SequencerEntry::setAttenuation(float factor) { - lock(); + AUD_MutexLock lock(*this); m_attenuation = factor; m_status++; - - unlock(); } float AUD_SequencerEntry::getConeAngleOuter() @@ -303,12 +284,10 @@ float AUD_SequencerEntry::getConeAngleOuter() void AUD_SequencerEntry::setConeAngleOuter(float angle) { - lock(); + AUD_MutexLock lock(*this); m_cone_angle_outer = angle; m_status++; - - unlock(); } float AUD_SequencerEntry::getConeAngleInner() @@ -318,12 +297,10 @@ float AUD_SequencerEntry::getConeAngleInner() void AUD_SequencerEntry::setConeAngleInner(float angle) { - lock(); + AUD_MutexLock lock(*this); m_cone_angle_inner = angle; m_status++; - - unlock(); } float AUD_SequencerEntry::getConeVolumeOuter() @@ -333,10 +310,8 @@ float AUD_SequencerEntry::getConeVolumeOuter() void AUD_SequencerEntry::setConeVolumeOuter(float volume) { - lock(); + AUD_MutexLock lock(*this); m_cone_volume_outer = volume; m_status++; - - unlock(); } diff --git a/intern/audaspace/intern/AUD_SequencerEntry.h b/intern/audaspace/intern/AUD_SequencerEntry.h index 7ff6fffea11..46efc52d66b 100644 --- a/intern/audaspace/intern/AUD_SequencerEntry.h +++ b/intern/audaspace/intern/AUD_SequencerEntry.h @@ -33,13 +33,14 @@ #include "AUD_Reference.h" #include "AUD_AnimateableProperty.h" #include "AUD_IFactory.h" +#include "AUD_ILockable.h" #include /** * This class represents a sequenced entry in a sequencer factory. */ -class AUD_SequencerEntry +class AUD_SequencerEntry : public AUD_ILockable { friend class AUD_SequencerHandle; private: @@ -130,12 +131,12 @@ public: /** * Locks the entry. */ - void lock(); + virtual void lock(); /** * Unlocks the previously locked entry. */ - void unlock(); + virtual void unlock(); /** * Sets the sound of the entry. diff --git a/intern/audaspace/intern/AUD_SequencerFactory.cpp b/intern/audaspace/intern/AUD_SequencerFactory.cpp index fd495f467cf..2cc0a656cf1 100644 --- a/intern/audaspace/intern/AUD_SequencerFactory.cpp +++ b/intern/audaspace/intern/AUD_SequencerFactory.cpp @@ -30,6 +30,7 @@ #include "AUD_SequencerFactory.h" #include "AUD_SequencerReader.h" #include "AUD_3DMath.h" +#include "AUD_MutexLock.h" AUD_SequencerFactory::AUD_SequencerFactory(AUD_Specs specs, float fps, bool muted) : m_specs(specs), @@ -75,30 +76,24 @@ void AUD_SequencerFactory::unlock() void AUD_SequencerFactory::setSpecs(AUD_Specs specs) { - lock(); + AUD_MutexLock lock(*this); m_specs = specs; m_status++; - - unlock(); } void AUD_SequencerFactory::setFPS(float fps) { - lock(); + AUD_MutexLock lock(*this); m_fps = fps; - - unlock(); } void AUD_SequencerFactory::mute(bool muted) { - lock(); + AUD_MutexLock lock(*this); m_muted = muted; - - unlock(); } bool AUD_SequencerFactory::getMute() const @@ -113,12 +108,10 @@ float AUD_SequencerFactory::getSpeedOfSound() const void AUD_SequencerFactory::setSpeedOfSound(float speed) { - lock(); + AUD_MutexLock lock(*this); m_speed_of_sound = speed; m_status++; - - unlock(); } float AUD_SequencerFactory::getDopplerFactor() const @@ -128,12 +121,10 @@ float AUD_SequencerFactory::getDopplerFactor() const void AUD_SequencerFactory::setDopplerFactor(float factor) { - lock(); + AUD_MutexLock lock(*this); m_doppler_factor = factor; m_status++; - - unlock(); } AUD_DistanceModel AUD_SequencerFactory::getDistanceModel() const @@ -143,12 +134,10 @@ AUD_DistanceModel AUD_SequencerFactory::getDistanceModel() const void AUD_SequencerFactory::setDistanceModel(AUD_DistanceModel model) { - lock(); + AUD_MutexLock lock(*this); m_distance_model = model; m_status++; - - unlock(); } AUD_AnimateableProperty* AUD_SequencerFactory::getAnimProperty(AUD_AnimateablePropertyType type) @@ -168,26 +157,22 @@ AUD_AnimateableProperty* AUD_SequencerFactory::getAnimProperty(AUD_AnimateablePr AUD_Reference AUD_SequencerFactory::add(AUD_Reference sound, float begin, float end, float skip) { - lock(); + AUD_MutexLock lock(*this); AUD_Reference entry = new AUD_SequencerEntry(sound, begin, end, skip, m_id++); m_entries.push_front(entry); m_entry_status++; - unlock(); - return entry; } void AUD_SequencerFactory::remove(AUD_Reference entry) { - lock(); + AUD_MutexLock lock(*this); m_entries.remove(entry); m_entry_status++; - - unlock(); } AUD_Reference AUD_SequencerFactory::createQualityReader() diff --git a/intern/audaspace/intern/AUD_SequencerFactory.h b/intern/audaspace/intern/AUD_SequencerFactory.h index 38241112020..38cbc2d1867 100644 --- a/intern/audaspace/intern/AUD_SequencerFactory.h +++ b/intern/audaspace/intern/AUD_SequencerFactory.h @@ -32,6 +32,7 @@ #include "AUD_IFactory.h" #include "AUD_AnimateableProperty.h" +#include "AUD_ILockable.h" #include #include @@ -41,7 +42,7 @@ class AUD_SequencerEntry; /** * This factory represents sequenced entries to play a sound scene. */ -class AUD_SequencerFactory : public AUD_IFactory +class AUD_SequencerFactory : public AUD_IFactory, public AUD_ILockable { friend class AUD_SequencerReader; private: @@ -104,12 +105,12 @@ public: /** * Locks the factory. */ - void lock(); + virtual void lock(); /** * Unlocks the previously locked factory. */ - void unlock(); + virtual void unlock(); /** * Sets the audio output specification. diff --git a/intern/audaspace/intern/AUD_SequencerHandle.cpp b/intern/audaspace/intern/AUD_SequencerHandle.cpp index f4bfae6cee7..38d2ed38a7e 100644 --- a/intern/audaspace/intern/AUD_SequencerHandle.cpp +++ b/intern/audaspace/intern/AUD_SequencerHandle.cpp @@ -29,6 +29,7 @@ #include "AUD_SequencerHandle.h" #include "AUD_ReadDevice.h" +#include "AUD_MutexLock.h" AUD_SequencerHandle::AUD_SequencerHandle(AUD_Reference entry, AUD_ReadDevice& device) : m_entry(entry), @@ -68,7 +69,7 @@ void AUD_SequencerHandle::update(float position, float frame, float fps) { if(!m_handle.isNull()) { - m_entry->lock(); + AUD_MutexLock lock(*m_entry); if(position >= m_entry->m_end && m_entry->m_end >= 0) m_handle->pause(); else if(position >= m_entry->m_begin) @@ -134,7 +135,6 @@ void AUD_SequencerHandle::update(float position, float frame, float fps) if(m_entry->m_muted) m_handle->setVolume(0); - m_entry->unlock(); } } @@ -142,11 +142,10 @@ void AUD_SequencerHandle::seek(float position) { if(!m_handle.isNull()) { - m_entry->lock(); + AUD_MutexLock lock(*m_entry); if(position >= m_entry->m_end && m_entry->m_end >= 0) { m_handle->pause(); - m_entry->unlock(); return; } @@ -160,6 +159,5 @@ void AUD_SequencerHandle::seek(float position) m_handle->pause(); else m_handle->resume(); - m_entry->unlock(); } } diff --git a/intern/audaspace/intern/AUD_SequencerReader.cpp b/intern/audaspace/intern/AUD_SequencerReader.cpp index 2e41a99d3db..39a4d29e6e4 100644 --- a/intern/audaspace/intern/AUD_SequencerReader.cpp +++ b/intern/audaspace/intern/AUD_SequencerReader.cpp @@ -28,6 +28,7 @@ #include "AUD_SequencerReader.h" +#include "AUD_MutexLock.h" typedef std::list >::iterator AUD_HandleIterator; typedef std::list >::iterator AUD_EntryIterator; @@ -77,7 +78,7 @@ AUD_Specs AUD_SequencerReader::getSpecs() const void AUD_SequencerReader::read(int& length, bool& eos, sample_t* buffer) { - m_factory->lock(); + AUD_MutexLock lock(*m_factory); if(m_factory->m_status != m_status) { @@ -197,8 +198,6 @@ void AUD_SequencerReader::read(int& length, bool& eos, sample_t* buffer) time += float(len) / float(specs.rate); } - m_factory->unlock(); - m_position += length; eos = false; diff --git a/intern/audaspace/intern/AUD_SoftwareDevice.cpp b/intern/audaspace/intern/AUD_SoftwareDevice.cpp index 1d993abab73..14f0c6429e9 100644 --- a/intern/audaspace/intern/AUD_SoftwareDevice.cpp +++ b/intern/audaspace/intern/AUD_SoftwareDevice.cpp @@ -33,6 +33,7 @@ #include "AUD_IFactory.h" #include "AUD_JOSResampleReader.h" #include "AUD_LinearResampleReader.h" +#include "AUD_MutexLock.h" #include #include @@ -226,7 +227,7 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::pause() { if(m_status) { - m_device->lock(); + AUD_MutexLock lock(*m_device); if(m_status == AUD_STATUS_PLAYING) { @@ -236,12 +237,9 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::pause() if(m_device->m_playingSounds.empty()) m_device->playing(m_device->m_playback = false); m_status = AUD_STATUS_PAUSED; - m_device->unlock(); return true; } - - m_device->unlock(); } return false; @@ -251,7 +249,7 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::resume() { if(m_status) { - m_device->lock(); + AUD_MutexLock lock(*m_device); if(m_status == AUD_STATUS_PAUSED) { @@ -261,11 +259,9 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::resume() if(!m_device->m_playback) m_device->playing(m_device->m_playback = true); m_status = AUD_STATUS_PLAYING; - m_device->unlock(); return true; } - m_device->unlock(); } return false; @@ -276,7 +272,10 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::stop() if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return false; // AUD_XXX Create a reference of our own object so that it doesn't get // deleted before the end of this function @@ -292,7 +291,6 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::stop() else m_device->m_pausedSounds.remove(This); - m_device->unlock(); m_status = AUD_STATUS_INVALID; return true; } @@ -310,11 +308,12 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::setKeep(bool keep) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - m_keep = keep; + if(!m_status) + return false; - m_device->unlock(); + m_keep = keep; return true; } @@ -324,11 +323,12 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::seek(float position) if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - m_reader->seek((int)(position * m_reader->getSpecs().rate)); + if(!m_status) + return false; - m_device->unlock(); + m_reader->seek((int)(position * m_reader->getSpecs().rate)); return true; } @@ -336,13 +336,14 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::seek(float position) float AUD_SoftwareDevice::AUD_SoftwareHandle::getPosition() { if(!m_status) - return 0.0f; + return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); - float position = m_reader->getPosition() / (float)m_device->m_specs.rate; + if(!m_status) + return 0.0f; - m_device->unlock(); + float position = m_reader->getPosition() / (float)m_device->m_specs.rate; return position; } @@ -407,13 +408,14 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::setStopCallback(stopCallback callba if(!m_status) return false; - m_device->lock(); + AUD_MutexLock lock(*m_device); + + if(!m_status) + return false; m_stop = callback; m_stop_data = data; - m_device->unlock(); - return true; } @@ -691,7 +693,7 @@ void AUD_SoftwareDevice::mix(data_t* buffer, int length) { m_buffer.assureSize(length * AUD_SAMPLE_SIZE(m_specs)); - lock(); + AUD_MutexLock lock(*this); { AUD_Reference sound; @@ -775,8 +777,6 @@ void AUD_SoftwareDevice::mix(data_t* buffer, int length) sound->pause(); } } - - unlock(); } void AUD_SoftwareDevice::setPanning(AUD_IHandle* handle, float pan) @@ -833,12 +833,12 @@ AUD_Reference AUD_SoftwareDevice::play(AUD_Reference r // play sound AUD_Reference sound = new AUD_SoftwareDevice::AUD_SoftwareHandle(this, reader, pitch, resampler, mapper, keep); - lock(); + AUD_MutexLock lock(*this); + m_playingSounds.push_back(sound); if(!m_playback) playing(m_playback = true); - unlock(); return AUD_Reference(sound); } @@ -850,15 +850,13 @@ AUD_Reference AUD_SoftwareDevice::play(AUD_Reference void AUD_SoftwareDevice::stopAll() { - lock(); + AUD_MutexLock lock(*this); while(!m_playingSounds.empty()) m_playingSounds.front()->stop(); while(!m_pausedSounds.empty()) m_pausedSounds.front()->stop(); - - unlock(); } void AUD_SoftwareDevice::lock() -- cgit v1.2.3