From 37de4a8898d2a417a8d2aaf260d6b480e7931a82 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 7 Apr 2004 03:47:12 +0000 Subject: [PATCH] Add RageSoundManager::StopPlayingSoundsForThisThread. We need to stop playing the sounds that a thread started before exiting the thread, or it causes unpredictable behavior with DirectSound. --- stepmania/src/RageSound.cpp | 5 +++++ stepmania/src/RageSound.h | 4 ++++ stepmania/src/RageSoundManager.cpp | 14 ++++++++++++++ stepmania/src/RageSoundManager.h | 4 ++++ 4 files changed, 27 insertions(+) diff --git a/stepmania/src/RageSound.cpp b/stepmania/src/RageSound.cpp index cef7179cd7..a31a0f0598 100644 --- a/stepmania/src/RageSound.cpp +++ b/stepmania/src/RageSound.cpp @@ -71,6 +71,7 @@ RageSound::RageSound() position = 0; stopped_position = 0; playing = false; + playing_thread = 0; databuf.reserve(internal_buffer_size); /* Register ourself, so we have a unique ID and receive Update()s. */ @@ -103,6 +104,7 @@ RageSound::RageSound(const RageSound &cpy): position = cpy.position; stopped_position = cpy.stopped_position; playing = false; + playing_thread = 0; databuf.reserve(internal_buffer_size); Sample = cpy.Sample->Copy(); @@ -579,6 +581,8 @@ void RageSound::StartPlaying() /* Tell the sound manager to start mixing us. */ playing = true; + playing_thread = RageThread::GetCurrentThreadID(); + SOUNDMAN->StartMixing(this); SOUNDMAN->playing_sounds.insert( this ); } @@ -598,6 +602,7 @@ void RageSound::StopPlaying() SOUNDMAN->lock.Unlock(); playing = false; + playing_thread = 0; pos_map.clear(); } diff --git a/stepmania/src/RageSound.h b/stepmania/src/RageSound.h index caf6e6aee2..7e0d7af2b5 100644 --- a/stepmania/src/RageSound.h +++ b/stepmania/src/RageSound.h @@ -104,6 +104,7 @@ public: bool SetPositionSeconds( float fSeconds ); CString GetLoadedFilePath() const { return m_sFilePath; } bool IsPlaying() const { return playing; } + unsigned GetPlayingThread() const { return playing_thread; } float GetPlaybackRate() const; RageTimer GetStartTime() const; @@ -158,6 +159,9 @@ private: int stopped_position; bool playing; + /* If playing, record the thread that called Play(). */ + unsigned playing_thread; + /* Unique ID number for this instance of RageSound. */ int ID; diff --git a/stepmania/src/RageSoundManager.cpp b/stepmania/src/RageSoundManager.cpp index e006de56e6..a086e65c9e 100644 --- a/stepmania/src/RageSoundManager.cpp +++ b/stepmania/src/RageSoundManager.cpp @@ -195,6 +195,20 @@ void RageSoundManager::StopPlayingAllCopiesOfSound(RageSound &snd) } } +void RageSoundManager::StopPlayingSoundsForThisThread() +{ + /* Lock to make sure sounds don't become invalidated below before we get to them. */ + LockMut(lock); + + set Sounds = GetPlayingSounds(); + for( set::iterator it = Sounds.begin(); it != Sounds.end(); ++it ) + { + if( (*it)->GetPlayingThread() != RageThread::GetCurrentThreadID() ) + continue; + (*it)->Stop(); + } +} + void RageSoundManager::GetCopies(RageSound &snd, vector &snds) { LockMut(lock); diff --git a/stepmania/src/RageSoundManager.h b/stepmania/src/RageSoundManager.h index 194d5102b2..f74036c31e 100644 --- a/stepmania/src/RageSoundManager.h +++ b/stepmania/src/RageSoundManager.h @@ -56,6 +56,10 @@ public: RageSound *PlaySound( RageSound &snd, const RageSoundParams *params ); void StopPlayingAllCopiesOfSound(RageSound &snd); + /* Stop all sounds that were started by this thread. This should be called + * before exiting a thread. */ + void StopPlayingSoundsForThisThread(); + /* A list of all sounds that currently exist. RageSound adds and removes * itself to this. */ set all_sounds;