diff --git a/stepmania/src/RageSoundManager.cpp b/stepmania/src/RageSoundManager.cpp index 9bef5a0535..bd1840a5f8 100644 --- a/stepmania/src/RageSoundManager.cpp +++ b/stepmania/src/RageSoundManager.cpp @@ -9,7 +9,6 @@ #include "RageSound.h" #include "RageLog.h" #include "RageTimer.h" -#include "PrefsManager.h" #include "arch/arch.h" #include "arch/Sound/RageSoundDriver.h" @@ -17,13 +16,14 @@ RageSoundManager::RageSoundManager(CString drivers) { + /* needs to be done first */ + SOUNDMAN = this; + MixVolume = 1.0f; + driver = MakeRageSoundDriver(drivers); if(!driver) RageException::Throw("Couldn't find a sound driver that works"); - /* needs to be done before RageSound::RageSound */ - SOUNDMAN = this; - music = new RageSound; } @@ -271,9 +271,15 @@ void RageSoundManager::PlayMusic(CString file, bool loop, float start_sec, float music->StartPlaying(); } +void RageSoundManager::SetPrefs(float MixVol) +{ + MixVolume = MixVol; + driver->VolumeChanged(); +} + SoundMixBuffer::SoundMixBuffer() { - vol = PREFSMAN->m_fSoundVolume; + vol = SOUNDMAN->GetMixVolume(); } void SoundMixBuffer::write(const Sint16 *buf, unsigned size) diff --git a/stepmania/src/RageSoundManager.h b/stepmania/src/RageSoundManager.h index 0be495df5c..a66d996990 100644 --- a/stepmania/src/RageSoundManager.h +++ b/stepmania/src/RageSoundManager.h @@ -28,12 +28,18 @@ class RageSoundManager RageSoundDriver *driver; + /* Prefs: */ + float MixVolume; + public: RageMutex lock; RageSoundManager(CString drivers); ~RageSoundManager(); + float GetMixVolume() const { return MixVolume; } + void SetPrefs(float MixVol); + void Update(float delta); void StartMixing(RageSound *snd); /* used by RageSound */ void StopMixing(RageSound *snd); /* used by RageSound */ diff --git a/stepmania/src/StepMania.cpp b/stepmania/src/StepMania.cpp index 3f60f6cf80..d31ac41473 100644 --- a/stepmania/src/StepMania.cpp +++ b/stepmania/src/StepMania.cpp @@ -268,6 +268,7 @@ int main(int argc, char* argv[]) GAMEMAN = new GameManager; THEME = new ThemeManager; SOUNDMAN = new RageSoundManager(PREFSMAN->m_bSoundDrivers); + SOUNDMAN->SetPrefs(PREFSMAN->m_fSoundVolume); ANNOUNCER = new AnnouncerManager; INPUTFILTER = new InputFilter; INPUTMAPPER = new InputMapper; diff --git a/stepmania/src/arch/Sound/DSoundHelpers.cpp b/stepmania/src/arch/Sound/DSoundHelpers.cpp index 484e443a98..dbebe38455 100644 --- a/stepmania/src/arch/Sound/DSoundHelpers.cpp +++ b/stepmania/src/arch/Sound/DSoundHelpers.cpp @@ -46,12 +46,8 @@ bool DSound::IsEmulated() const } DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware, - int channels_, int samplerate_, int samplebits_, int writeahead_, - float vol) + int channels_, int samplerate_, int samplebits_, int writeahead_) { - ASSERT(vol >= 0); - ASSERT(vol <= 1); - channels = channels_; samplerate = samplerate_; samplebits = samplebits_; @@ -79,15 +75,14 @@ DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware, DSBUFFERDESC format; memset(&format, 0, sizeof(format)); format.dwSize = sizeof(format); - format.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS; + format.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLVOLUME; /* Don't use DSBCAPS_STATIC. It's meant for static buffers, and we * only use streaming buffers. */ if(hardware == HW_HARDWARE) format.dwFlags |= DSBCAPS_LOCHARDWARE; else format.dwFlags |= DSBCAPS_LOCSOFTWARE; - if(vol != 1) - format.dwFlags |= DSBCAPS_CTRLVOLUME; + format.dwBufferBytes = buffersize; format.dwReserved = 0; format.lpwfxFormat = &waveformat; @@ -113,6 +108,12 @@ DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware, buffersize = bcaps.dwBufferBytes; writeahead = min(writeahead, buffersize); } +} + +void DSoundBuf::SetVolume(float vol) +{ + ASSERT(vol >= 0); + ASSERT(vol <= 1); float vl2 = log10f(vol) / log10f(2); /* vol log 2 */ @@ -122,7 +123,6 @@ DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware, buf->SetVolume(max(int(1000 * vl2), DSBVOLUME_MIN)); } - DSoundBuf::~DSoundBuf() { buf->Release(); diff --git a/stepmania/src/arch/Sound/DSoundHelpers.h b/stepmania/src/arch/Sound/DSoundHelpers.h index d912c444ad..79fba62023 100644 --- a/stepmania/src/arch/Sound/DSoundHelpers.h +++ b/stepmania/src/arch/Sound/DSoundHelpers.h @@ -39,8 +39,7 @@ class DSoundBuf public: enum hw { HW_HARDWARE, HW_SOFTWARE, HW_DONT_CARE }; DSoundBuf(DSound &ds, hw hardware, - int channels, int samplerate, int samplebits, int writeahead, - float vol=1); + int channels, int samplerate, int samplebits, int writeahead); bool get_output_buf(char **buffer, unsigned *bufsiz, int *play_pos, int chunksize); void release_output_buf(char *buffer, unsigned bufsiz); @@ -48,6 +47,7 @@ public: void Reset(); void Play(); void Stop(); + void SetVolume(float vol); ~DSoundBuf(); int GetPosition() const; diff --git a/stepmania/src/arch/Sound/RageSoundDriver.h b/stepmania/src/arch/Sound/RageSoundDriver.h index 09bdceee81..bdaedb3d68 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver.h +++ b/stepmania/src/arch/Sound/RageSoundDriver.h @@ -33,6 +33,10 @@ protected: * hearing it. (This isn't necessarily the same as the buffer latency.) */ virtual float GetPlayLatency() const { return 0.0f; } + /* This is called if the volume changed; call SOUNDMAN->GetMixVolume() to + * get it. */ + virtual void VolumeChanged() { } + public: virtual ~RageSoundDriver() { } }; diff --git a/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp b/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp index a5e6105aa9..95a1591a07 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp @@ -12,7 +12,6 @@ #include "../../RageUtil.h" #include "../../RageSound.h" #include "../../RageLog.h" -#include "../../PrefsManager.h" #include "../../tls.h" #include "SDL.h" @@ -165,8 +164,7 @@ RageSound_DSound::RageSound_DSound() try { newbuf = new DSoundBuf(ds, DSoundBuf::HW_HARDWARE, - channels, samplerate, 16, buffersize, - PREFSMAN->m_fSoundVolume); + channels, samplerate, 16, buffersize); } catch(const RageException &e) { /* If we didn't get at least 8, fail. */ if(i >= 8) break; /* OK */ @@ -188,8 +186,12 @@ RageSound_DSound::RageSound_DSound() s->str_ds = newbuf; stream_pool.push_back(s); } + LOG->Trace("Got %i hardware buffers", stream_pool.size()); + /* Set channel volumes. */ + VolumeChanged(); + MixerThreadPtr = SDL_CreateThread(MixerThread_start, this); } @@ -197,12 +199,24 @@ RageSound_DSound::~RageSound_DSound() { /* Signal the mixing thread to quit. */ shutdown = true; + LOG->Trace("Shutting down mixer thread ..."); + LOG->Flush(); SDL_WaitThread(MixerThreadPtr, NULL); + LOG->Trace("Mixer thread shut down."); + LOG->Flush(); for(unsigned i = 0; i < stream_pool.size(); ++i) delete stream_pool[i]; } +void RageSound_DSound::VolumeChanged() +{ + for(unsigned i = 0; i < stream_pool.size(); ++i) + { + stream_pool[i]->str_ds->SetVolume(SOUNDMAN->GetMixVolume()); + } +} + void RageSound_DSound::StartMixing(RageSound *snd) { LockMutex L(SOUNDMAN->lock); diff --git a/stepmania/src/arch/Sound/RageSoundDriver_DSound.h b/stepmania/src/arch/Sound/RageSoundDriver_DSound.h index 5474a86e05..cf5a9640ed 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_DSound.h +++ b/stepmania/src/arch/Sound/RageSoundDriver_DSound.h @@ -48,6 +48,7 @@ class RageSound_DSound: public RageSoundDriver void StopMixing(RageSound *snd); /* used by RageSound */ int GetPosition(const RageSound *snd) const; void Update(float delta); + void VolumeChanged(); public: RageSound_DSound(); diff --git a/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp b/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp index 36cc5eb706..d19d8309b5 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp @@ -43,7 +43,6 @@ void RageSound_DSound_Software::MixerThread() while(!shutdown) { Sleep(10); - while(GetData()) ; } @@ -179,9 +178,11 @@ RageSound_DSound_Software::~RageSound_DSound_Software() { /* Signal the mixing thread to quit. */ shutdown = true; - LOG->Trace("Shutting down mixer thread ..."); + LOG->Trace("Shutting down mixer thread %p ...", MixerThreadPtr); + LOG->Flush(); SDL_WaitThread(MixerThreadPtr, NULL); LOG->Trace("Mixer thread shut down."); + LOG->Flush(); delete str_ds; }