From 26bae5f76905b3c084a40fe5e040c2d0889d6505 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sat, 8 Jan 2005 16:58:37 +0000 Subject: [PATCH] add DeleteSoundWhenFinished, PlayCopyOfSound --- stepmania/src/RageSoundManager.cpp | 53 +++++++++++++++++------------- stepmania/src/RageSoundManager.h | 4 ++- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/stepmania/src/RageSoundManager.cpp b/stepmania/src/RageSoundManager.cpp index 4b21d43946..7f3c45db4b 100644 --- a/stepmania/src/RageSoundManager.cpp +++ b/stepmania/src/RageSoundManager.cpp @@ -200,30 +200,37 @@ int RageSoundManager::GetDriverSampleRate( int rate ) const return driver->GetSampleRate( rate ); } +/* The return value of PlaySound and PlayCopyOfSound is only valid in the main + * thread, until the next call to Update(). After that, it may be deleted. */ RageSound *RageSoundManager::PlaySound( RageSound &snd, const RageSoundParams *params ) { - RageSound *sound_to_play; - if(!snd.IsPlaying()) - sound_to_play = &snd; - else - { - sound_to_play = new RageSound(snd); - - /* We're responsible for freeing it. */ - g_SoundManMutex.Lock(); /* lock for access to owned_sounds */ - owned_sounds.insert(sound_to_play); - g_SoundManMutex.Unlock(); /* finished with owned_sounds */ - } + if( snd.IsPlaying() ) + return PlayCopyOfSound( snd, params ); if( params ) - sound_to_play->SetParams( *params ); + snd.SetParams( *params ); // Move to the start position. - sound_to_play->SetPositionSeconds( sound_to_play->GetParams().m_StartSecond ); + snd.SetPositionSeconds( snd.GetParams().m_StartSecond ); - sound_to_play->StartPlaying(); + snd.StartPlaying(); + return &snd; +} - return sound_to_play; +RageSound *RageSoundManager::PlayCopyOfSound( RageSound &snd, const RageSoundParams *params ) +{ + RageSound *pSound = new RageSound(snd); + DeleteSoundWhenFinished( pSound ); + + if( params ) + pSound->SetParams( *params ); + + // Move to the start position. + pSound->SetPositionSeconds( pSound->GetParams().m_StartSecond ); + + pSound->StartPlaying(); + + return pSound; } void RageSoundManager::DeleteSound( RageSound *p ) @@ -231,10 +238,14 @@ void RageSoundManager::DeleteSound( RageSound *p ) /* Stop playing the sound. */ p->StopPlaying(); - /* Add it to owned_sounds. It'll be deleted the next time we come around - * to Update(). */ + /* We might be in a thread, so don't delete it here. */ + DeleteSoundWhenFinished( p ); +} + +void RageSoundManager::DeleteSoundWhenFinished( RageSound *pSound ) +{ g_SoundManMutex.Lock(); /* lock for access to owned_sounds */ - owned_sounds.insert( p ); + owned_sounds.insert( pSound ); g_SoundManMutex.Unlock(); /* finished with owned_sounds */ } @@ -251,9 +262,7 @@ void RageSoundManager::PlayOnce( CString sPath ) /* We're responsible for freeing it. Add it to owned_sounds *after* we start * playing, so RageSoundManager::Update doesn't free it before we actually start * it. */ - g_SoundManMutex.Lock(); /* lock for access to owned_sounds */ - owned_sounds.insert(snd); - g_SoundManMutex.Unlock(); /* finished with owned_sounds */ + DeleteSoundWhenFinished( snd ); } void RageSoundManager::SetPrefs(float MixVol) diff --git a/stepmania/src/RageSoundManager.h b/stepmania/src/RageSoundManager.h index 3abe7e5a08..e38fb125ab 100644 --- a/stepmania/src/RageSoundManager.h +++ b/stepmania/src/RageSoundManager.h @@ -60,10 +60,12 @@ public: /* When deleting a sound from any thread except the one calling Update(), this * must be used to prevent race conditions. */ void DeleteSound( RageSound *p ); + void DeleteSoundWhenFinished( RageSound *pSound ); void PlayOnce( CString sPath ); - RageSound *PlaySound( RageSound &snd, const RageSoundParams *params ); + RageSound *PlaySound( RageSound &snd, const RageSoundParams *params = NULL ); + RageSound *PlayCopyOfSound( RageSound &snd, const RageSoundParams *params = NULL ); static void AttenuateBuf( int16_t *buf, int samples, float vol );