From 6bea0a2f5314f12ad1d163f7c7d63e49c4bdc905 Mon Sep 17 00:00:00 2001 From: "XTra.KrazzY" Date: Thu, 10 Dec 2009 21:00:52 +0000 Subject: [PATCH] Fixed annoying sound when pausing/shutting down (please test for ALL backends) (couldn't do this for CoreAudio and PulseAudio too) git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4676 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/AudioCommon/Src/AOSoundStream.cpp | 11 ++++++++++- Source/Core/AudioCommon/Src/AOSoundStream.h | 2 ++ Source/Core/AudioCommon/Src/AlsaSoundStream.cpp | 9 ++++++++- Source/Core/AudioCommon/Src/AlsaSoundStream.h | 2 ++ Source/Core/AudioCommon/Src/DSoundStream.cpp | 13 +++++++++++++ Source/Core/AudioCommon/Src/DSoundStream.h | 1 + Source/Core/AudioCommon/Src/OpenALStream.cpp | 17 +++++++++++++++++ Source/Core/AudioCommon/Src/OpenALStream.h | 2 ++ Source/Core/AudioCommon/Src/SoundStream.h | 4 +++- Source/Plugins/Plugin_DSP_HLE/Src/main.cpp | 9 +++++++++ 10 files changed, 67 insertions(+), 3 deletions(-) diff --git a/Source/Core/AudioCommon/Src/AOSoundStream.cpp b/Source/Core/AudioCommon/Src/AOSoundStream.cpp index 137864a832..85766495be 100644 --- a/Source/Core/AudioCommon/Src/AOSoundStream.cpp +++ b/Source/Core/AudioCommon/Src/AOSoundStream.cpp @@ -51,7 +51,8 @@ void AOSound::SoundLoop() { soundCriticalSection.Enter(); m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2); - ao_play(device, (char*)realtimeBuffer, numBytesToRender); + if(!g_muted) + ao_play(device, (char*)realtimeBuffer, numBytesToRender); soundCriticalSection.Leave(); if (! threadData) @@ -105,4 +106,12 @@ AOSound::~AOSound() { // FIXME: crashes dolphin // ao_shutdown(); } + +void AOSound::Mute(bool bMute) { + if((bMute && g_muted) || (!bMute && !g_muted)) + return; + + g_muted = bMute; +} + #endif diff --git a/Source/Core/AudioCommon/Src/AOSoundStream.h b/Source/Core/AudioCommon/Src/AOSoundStream.h index dbe0a09471..9f6fd20b9b 100644 --- a/Source/Core/AudioCommon/Src/AOSoundStream.h +++ b/Source/Core/AudioCommon/Src/AOSoundStream.h @@ -64,6 +64,8 @@ public: virtual void Update(); + virtual void Mute(bool bMute); + #else public: AOSound(CMixer *mixer) : SoundStream(mixer) {} diff --git a/Source/Core/AudioCommon/Src/AlsaSoundStream.cpp b/Source/Core/AudioCommon/Src/AlsaSoundStream.cpp index 8158fc6a86..dfc23300b0 100644 --- a/Source/Core/AudioCommon/Src/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/Src/AlsaSoundStream.cpp @@ -67,7 +67,7 @@ void AlsaSound::SoundLoop() // nakee: What is the optimal value? int frames_to_deliver = 4096; m_mixer->Mix(reinterpret_cast(mix_buffer), frames_to_deliver); - int rc = snd_pcm_writei(handle, mix_buffer, frames_to_deliver); + int rc = g_muted ? 1337 : snd_pcm_writei(handle, mix_buffer, frames_to_deliver); if (rc == -EPIPE) { // Underrun @@ -180,6 +180,13 @@ bool AlsaSound::AlsaInit() NOTICE_LOG(AUDIO, "ALSA successfully initialized.\n"); return true; } + +void AlsaSound::Mute(bool bMute) { + if((bMute && g_muted) || (!bMute && !g_muted)) + return; + + g_muted = bMute; +} void AlsaSound::AlsaShutdown() { diff --git a/Source/Core/AudioCommon/Src/AlsaSoundStream.h b/Source/Core/AudioCommon/Src/AlsaSoundStream.h index 22755c7b79..df4e206baa 100644 --- a/Source/Core/AudioCommon/Src/AlsaSoundStream.h +++ b/Source/Core/AudioCommon/Src/AlsaSoundStream.h @@ -47,6 +47,8 @@ public: virtual void Update(); + virtual void Mute(bool bMute); + private: bool AlsaInit(); void AlsaShutdown(); diff --git a/Source/Core/AudioCommon/Src/DSoundStream.cpp b/Source/Core/AudioCommon/Src/DSoundStream.cpp index aa08f3f7f8..35dbf4f6e6 100644 --- a/Source/Core/AudioCommon/Src/DSoundStream.cpp +++ b/Source/Core/AudioCommon/Src/DSoundStream.cpp @@ -192,3 +192,16 @@ void DSound::Stop() soundSyncEvent.Shutdown(); thread = NULL; } + +void DSound::Mute(bool bMute) { + if((bMute && g_muted) || (!bMute && !g_muted)) + return; + + if(bMute) + dsBuffer->Stop(); + else + dsBuffer->Play(0, 0, DSBPLAY_LOOPING); + + g_muted = bMute; +} + diff --git a/Source/Core/AudioCommon/Src/DSoundStream.h b/Source/Core/AudioCommon/Src/DSoundStream.h index cad273d268..1877a8b43e 100644 --- a/Source/Core/AudioCommon/Src/DSoundStream.h +++ b/Source/Core/AudioCommon/Src/DSoundStream.h @@ -79,6 +79,7 @@ public: virtual void SetVolume(int volume); virtual void Stop(); virtual void Clear(); + virtual void Mute(bool bMute); static bool isValid() { return true; } virtual bool usesMixer() const { return true; } virtual void Update(); diff --git a/Source/Core/AudioCommon/Src/OpenALStream.cpp b/Source/Core/AudioCommon/Src/OpenALStream.cpp index 53a3a629bb..6d72158413 100644 --- a/Source/Core/AudioCommon/Src/OpenALStream.cpp +++ b/Source/Core/AudioCommon/Src/OpenALStream.cpp @@ -30,6 +30,8 @@ bool OpenALStream::Start() ALCdevice *pDevice = NULL; bool bReturn = false; + g_uiSource = 0; + pDeviceList = new ALDeviceList(); if ((pDeviceList) && (pDeviceList->GetNumDevices())) { @@ -125,6 +127,9 @@ void OpenALStream::SoundLoop() alSourceQueueBuffers(uiSource, 1, &uiBuffers[iLoop]); } //*/ + + g_uiSource = uiSource; + alSourcePlay(uiSource); err = alGetError(); @@ -173,5 +178,17 @@ void OpenALStream::SoundLoop() } +void OpenALStream::Mute(bool bMute) { + if((bMute && g_muted) || (!bMute && !g_muted)) + return; + + if(bMute && g_uiSource) + alSourceStop(g_uiSource); + else if(g_uiSource) + alSourcePlay(g_uiSource); + + g_muted = bMute; +} + #endif //HAVE_OPENAL diff --git a/Source/Core/AudioCommon/Src/OpenALStream.h b/Source/Core/AudioCommon/Src/OpenALStream.h index 083723951b..61deb32781 100644 --- a/Source/Core/AudioCommon/Src/OpenALStream.h +++ b/Source/Core/AudioCommon/Src/OpenALStream.h @@ -51,6 +51,7 @@ public: virtual void SoundLoop(); virtual void Stop(); virtual void Clear(); + virtual void Mute(bool bMute); static bool isValid() { return true; } virtual bool usesMixer() const { return true; } virtual void Update(); @@ -63,6 +64,7 @@ private: Common::Event soundSyncEvent; short realtimeBuffer[OAL_BUFFER_SIZE]; + ALuint g_uiSource; #else public: OpenALStream(CMixer *mixer, void *hWnd = NULL): SoundStream(mixer) {} diff --git a/Source/Core/AudioCommon/Src/SoundStream.h b/Source/Core/AudioCommon/Src/SoundStream.h index 2fba16756e..ed11f77b19 100644 --- a/Source/Core/AudioCommon/Src/SoundStream.h +++ b/Source/Core/AudioCommon/Src/SoundStream.h @@ -32,9 +32,10 @@ protected: volatile int threadData; bool m_logAudio; WaveFileWriter g_wave_writer; + bool g_muted; public: - SoundStream(CMixer *mixer) : m_mixer(mixer), threadData(0) {} + SoundStream(CMixer *mixer) : m_mixer(mixer), threadData(0), g_muted(false) {} virtual ~SoundStream() { delete m_mixer;} static bool isValid() { return false; } @@ -45,6 +46,7 @@ public: virtual void Stop() {} virtual void Update() {} virtual void Clear() {} + virtual void Mute(bool bMute) {} virtual void StartLogAudio(const char *filename) { if (! m_logAudio) { m_logAudio = true; diff --git a/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp b/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp index 6ecd47705d..d8ce82ef00 100644 --- a/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp +++ b/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp @@ -42,6 +42,7 @@ PLUGIN_GLOBALS* globals = NULL; DSPInitialize g_dspInitialize; u8* g_pMemory; extern std::vector sMailLog, sMailTime; +bool g_bMuted = false; SoundStream *soundStream = NULL; @@ -204,6 +205,8 @@ void Initialize(void *init) { g_dspInitialize = *(DSPInitialize*)init; + g_bMuted = false; + g_Config.Load(); g_pMemory = g_dspInitialize.pGetMemoryPointer(0); @@ -314,6 +317,10 @@ unsigned short DSP_ReadControlRegister() void DSP_Update(int cycles) { + // Handle muting + if(g_bMuted && !*g_dspInitialize.pEmulatorState && soundStream) + soundStream->Mute(g_bMuted = false); + // This is called OFTEN - better not do anything expensive! CDSPHandler::GetInstance().Update(cycles); } @@ -365,4 +372,6 @@ void DSP_ClearAudioBuffer() { if (soundStream) soundStream->Clear(); + if(*g_dspInitialize.pEmulatorState && soundStream && !g_bMuted) + soundStream->Mute(g_bMuted = true); }