From c2d396526b57a0325fa43fd0a3fe39870c018645 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sat, 7 Aug 2021 21:27:49 +0100 Subject: [PATCH 1/4] AudioCommon: fix "Error stopping stream" when emulation is paused --- Source/Core/AudioCommon/OpenSLESStream.h | 2 +- Source/Core/AudioCommon/PulseAudioStream.h | 2 +- Source/Core/AudioCommon/SoundStream.h | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/Core/AudioCommon/OpenSLESStream.h b/Source/Core/AudioCommon/OpenSLESStream.h index 97c2070a7b..588d6a97d3 100644 --- a/Source/Core/AudioCommon/OpenSLESStream.h +++ b/Source/Core/AudioCommon/OpenSLESStream.h @@ -14,7 +14,7 @@ class OpenSLESStream final : public SoundStream public: ~OpenSLESStream() override; bool Init() override; - bool SetRunning(bool running) override { return running; } + bool SetRunning(bool running) override { return true; } void SetVolume(int volume) override; static bool isValid() { return true; } diff --git a/Source/Core/AudioCommon/PulseAudioStream.h b/Source/Core/AudioCommon/PulseAudioStream.h index ef0852cfb4..ad3519e21d 100644 --- a/Source/Core/AudioCommon/PulseAudioStream.h +++ b/Source/Core/AudioCommon/PulseAudioStream.h @@ -20,7 +20,7 @@ public: ~PulseAudio() override; bool Init() override; - bool SetRunning(bool running) override { return running; } + bool SetRunning(bool running) override { return true; } static bool isValid() { return true; } void StateCallback(pa_context* c); void WriteCallback(pa_stream* s, size_t length); diff --git a/Source/Core/AudioCommon/SoundStream.h b/Source/Core/AudioCommon/SoundStream.h index 771c606245..1b3300ae97 100644 --- a/Source/Core/AudioCommon/SoundStream.h +++ b/Source/Core/AudioCommon/SoundStream.h @@ -22,5 +22,6 @@ public: virtual void SetVolume(int) {} virtual void SoundLoop() {} virtual void Update() {} + // Returns true if successful. virtual bool SetRunning(bool running) { return false; } }; From d14b9a73b2f69377a80a14b5561a26e28dcf42b3 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sat, 7 Aug 2021 22:02:10 +0100 Subject: [PATCH 2/4] AudioCommon: get rid of Update(), it never does anything --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 5 ----- Source/Core/AudioCommon/AlsaSoundStream.h | 1 - Source/Core/AudioCommon/AudioCommon.cpp | 2 -- Source/Core/AudioCommon/NullSoundStream.cpp | 4 ---- Source/Core/AudioCommon/NullSoundStream.h | 1 - Source/Core/AudioCommon/OpenALStream.cpp | 8 -------- Source/Core/AudioCommon/OpenALStream.h | 3 --- Source/Core/AudioCommon/SoundStream.h | 1 - 8 files changed, 25 deletions(-) diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index 8008e1bd00..c45d7bee1c 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -41,11 +41,6 @@ bool AlsaSound::Init() return true; } -void AlsaSound::Update() -{ - // don't need to do anything here. -} - // Called on audio thread. void AlsaSound::SoundLoop() { diff --git a/Source/Core/AudioCommon/AlsaSoundStream.h b/Source/Core/AudioCommon/AlsaSoundStream.h index d4f7d02150..8926cb34a6 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.h +++ b/Source/Core/AudioCommon/AlsaSoundStream.h @@ -24,7 +24,6 @@ public: bool Init() override; void SoundLoop() override; - void Update() override; bool SetRunning(bool running) override; static bool isValid() { return true; } diff --git a/Source/Core/AudioCommon/AudioCommon.cpp b/Source/Core/AudioCommon/AudioCommon.cpp index 6623b5ad8b..4b1b812f94 100644 --- a/Source/Core/AudioCommon/AudioCommon.cpp +++ b/Source/Core/AudioCommon/AudioCommon.cpp @@ -197,8 +197,6 @@ void SendAIBuffer(const short* samples, unsigned int num_samples) { pMixer->PushSamples(samples, num_samples); } - - g_sound_stream->Update(); } void StartAudioDump() diff --git a/Source/Core/AudioCommon/NullSoundStream.cpp b/Source/Core/AudioCommon/NullSoundStream.cpp index 0918e8bb35..fb97f23667 100644 --- a/Source/Core/AudioCommon/NullSoundStream.cpp +++ b/Source/Core/AudioCommon/NullSoundStream.cpp @@ -20,7 +20,3 @@ bool NullSound::SetRunning(bool running) void NullSound::SetVolume(int volume) { } - -void NullSound::Update() -{ -} diff --git a/Source/Core/AudioCommon/NullSoundStream.h b/Source/Core/AudioCommon/NullSoundStream.h index 0a39250c99..91cf113e7d 100644 --- a/Source/Core/AudioCommon/NullSoundStream.h +++ b/Source/Core/AudioCommon/NullSoundStream.h @@ -12,7 +12,6 @@ public: void SoundLoop() override; bool SetRunning(bool running) override; void SetVolume(int volume) override; - void Update() override; static bool isValid() { return true; } }; diff --git a/Source/Core/AudioCommon/OpenALStream.cpp b/Source/Core/AudioCommon/OpenALStream.cpp index 96b2a5d628..b2e61dae98 100644 --- a/Source/Core/AudioCommon/OpenALStream.cpp +++ b/Source/Core/AudioCommon/OpenALStream.cpp @@ -126,9 +126,6 @@ bool OpenALStream::Init() OpenALStream::~OpenALStream() { m_run_thread.Clear(); - // kick the thread if it's waiting - m_sound_sync_event.Set(); - m_thread.join(); palSourceStop(m_source); @@ -155,11 +152,6 @@ void OpenALStream::SetVolume(int volume) palSourcef(m_source, AL_GAIN, m_volume); } -void OpenALStream::Update() -{ - m_sound_sync_event.Set(); -} - bool OpenALStream::SetRunning(bool running) { if (running) diff --git a/Source/Core/AudioCommon/OpenALStream.h b/Source/Core/AudioCommon/OpenALStream.h index 962e7a2ac4..4fb858069b 100644 --- a/Source/Core/AudioCommon/OpenALStream.h +++ b/Source/Core/AudioCommon/OpenALStream.h @@ -59,7 +59,6 @@ public: void SoundLoop() override; void SetVolume(int volume) override; bool SetRunning(bool running) override; - void Update() override; static bool isValid(); @@ -67,8 +66,6 @@ private: std::thread m_thread; Common::Flag m_run_thread; - Common::Event m_sound_sync_event; - std::vector m_realtime_buffer; std::array m_buffers; ALuint m_source; diff --git a/Source/Core/AudioCommon/SoundStream.h b/Source/Core/AudioCommon/SoundStream.h index 1b3300ae97..dd6deecf9b 100644 --- a/Source/Core/AudioCommon/SoundStream.h +++ b/Source/Core/AudioCommon/SoundStream.h @@ -21,7 +21,6 @@ public: virtual bool Init() { return false; } virtual void SetVolume(int) {} virtual void SoundLoop() {} - virtual void Update() {} // Returns true if successful. virtual bool SetRunning(bool running) { return false; } }; From b6d8c111bc259021398adcc9820339c9376b5955 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sat, 7 Aug 2021 22:15:45 +0100 Subject: [PATCH 3/4] AudioCommon: make SoundLoop() non-virtual and private --- Source/Core/AudioCommon/AlsaSoundStream.h | 3 ++- Source/Core/AudioCommon/NullSoundStream.cpp | 4 ---- Source/Core/AudioCommon/NullSoundStream.h | 1 - Source/Core/AudioCommon/OpenALStream.h | 3 ++- Source/Core/AudioCommon/PulseAudioStream.h | 2 +- Source/Core/AudioCommon/SoundStream.h | 1 - Source/Core/AudioCommon/WASAPIStream.h | 3 ++- 7 files changed, 7 insertions(+), 10 deletions(-) diff --git a/Source/Core/AudioCommon/AlsaSoundStream.h b/Source/Core/AudioCommon/AlsaSoundStream.h index 8926cb34a6..37a4db2bf2 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.h +++ b/Source/Core/AudioCommon/AlsaSoundStream.h @@ -23,12 +23,13 @@ public: ~AlsaSound() override; bool Init() override; - void SoundLoop() override; bool SetRunning(bool running) override; static bool isValid() { return true; } private: + void SoundLoop(); + // maximum number of frames the buffer can hold static constexpr size_t BUFFER_SIZE_MAX = 8192; diff --git a/Source/Core/AudioCommon/NullSoundStream.cpp b/Source/Core/AudioCommon/NullSoundStream.cpp index fb97f23667..3c7a0ebb98 100644 --- a/Source/Core/AudioCommon/NullSoundStream.cpp +++ b/Source/Core/AudioCommon/NullSoundStream.cpp @@ -3,10 +3,6 @@ #include "AudioCommon/NullSoundStream.h" -void NullSound::SoundLoop() -{ -} - bool NullSound::Init() { return true; diff --git a/Source/Core/AudioCommon/NullSoundStream.h b/Source/Core/AudioCommon/NullSoundStream.h index 91cf113e7d..6d90092425 100644 --- a/Source/Core/AudioCommon/NullSoundStream.h +++ b/Source/Core/AudioCommon/NullSoundStream.h @@ -9,7 +9,6 @@ class NullSound final : public SoundStream { public: bool Init() override; - void SoundLoop() override; bool SetRunning(bool running) override; void SetVolume(int volume) override; diff --git a/Source/Core/AudioCommon/OpenALStream.h b/Source/Core/AudioCommon/OpenALStream.h index 4fb858069b..bb42bf6689 100644 --- a/Source/Core/AudioCommon/OpenALStream.h +++ b/Source/Core/AudioCommon/OpenALStream.h @@ -56,13 +56,14 @@ public: OpenALStream() : m_source(0) {} ~OpenALStream() override; bool Init() override; - void SoundLoop() override; void SetVolume(int volume) override; bool SetRunning(bool running) override; static bool isValid(); private: + void SoundLoop(); + std::thread m_thread; Common::Flag m_run_thread; diff --git a/Source/Core/AudioCommon/PulseAudioStream.h b/Source/Core/AudioCommon/PulseAudioStream.h index ad3519e21d..0be6143552 100644 --- a/Source/Core/AudioCommon/PulseAudioStream.h +++ b/Source/Core/AudioCommon/PulseAudioStream.h @@ -27,7 +27,7 @@ public: void UnderflowCallback(pa_stream* s); private: - void SoundLoop() override; + void SoundLoop(); bool PulseInit(); void PulseShutdown(); diff --git a/Source/Core/AudioCommon/SoundStream.h b/Source/Core/AudioCommon/SoundStream.h index dd6deecf9b..3fa4a1775a 100644 --- a/Source/Core/AudioCommon/SoundStream.h +++ b/Source/Core/AudioCommon/SoundStream.h @@ -20,7 +20,6 @@ public: Mixer* GetMixer() const { return m_mixer.get(); } virtual bool Init() { return false; } virtual void SetVolume(int) {} - virtual void SoundLoop() {} // Returns true if successful. virtual bool SetRunning(bool running) { return false; } }; diff --git a/Source/Core/AudioCommon/WASAPIStream.h b/Source/Core/AudioCommon/WASAPIStream.h index e9a751ee99..d0cdaf900c 100644 --- a/Source/Core/AudioCommon/WASAPIStream.h +++ b/Source/Core/AudioCommon/WASAPIStream.h @@ -35,13 +35,14 @@ public: ~WASAPIStream(); bool Init() override; bool SetRunning(bool running) override; - void SoundLoop() override; static bool isValid(); static std::vector GetAvailableDevices(); static Microsoft::WRL::ComPtr GetDeviceByName(std::string_view name); private: + void SoundLoop(); + u32 m_frames_in_buffer = 0; std::atomic m_running = false; std::thread m_thread; From eda2035874309233a3971aed075c0417c1f18ef7 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sun, 8 Aug 2021 00:23:58 +0100 Subject: [PATCH 4/4] AudioCommon: rename isValid() to IsValid() --- Source/Core/AudioCommon/AlsaSoundStream.h | 2 +- Source/Core/AudioCommon/AudioCommon.cpp | 22 +++++++++++----------- Source/Core/AudioCommon/NullSoundStream.h | 2 +- Source/Core/AudioCommon/OpenALStream.cpp | 2 +- Source/Core/AudioCommon/OpenALStream.h | 2 +- Source/Core/AudioCommon/OpenSLESStream.h | 2 +- Source/Core/AudioCommon/PulseAudioStream.h | 2 +- Source/Core/AudioCommon/SoundStream.h | 2 +- Source/Core/AudioCommon/WASAPIStream.cpp | 2 +- Source/Core/AudioCommon/WASAPIStream.h | 2 +- 10 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Source/Core/AudioCommon/AlsaSoundStream.h b/Source/Core/AudioCommon/AlsaSoundStream.h index 37a4db2bf2..d3e34386a7 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.h +++ b/Source/Core/AudioCommon/AlsaSoundStream.h @@ -25,7 +25,7 @@ public: bool Init() override; bool SetRunning(bool running) override; - static bool isValid() { return true; } + static bool IsValid() { return true; } private: void SoundLoop(); diff --git a/Source/Core/AudioCommon/AudioCommon.cpp b/Source/Core/AudioCommon/AudioCommon.cpp index 4b1b812f94..d3bdcde445 100644 --- a/Source/Core/AudioCommon/AudioCommon.cpp +++ b/Source/Core/AudioCommon/AudioCommon.cpp @@ -30,17 +30,17 @@ static std::unique_ptr CreateSoundStreamForBackend(std::string_view { if (backend == BACKEND_CUBEB) return std::make_unique(); - else if (backend == BACKEND_OPENAL && OpenALStream::isValid()) + else if (backend == BACKEND_OPENAL && OpenALStream::IsValid()) return std::make_unique(); else if (backend == BACKEND_NULLSOUND) return std::make_unique(); - else if (backend == BACKEND_ALSA && AlsaSound::isValid()) + else if (backend == BACKEND_ALSA && AlsaSound::IsValid()) return std::make_unique(); - else if (backend == BACKEND_PULSEAUDIO && PulseAudio::isValid()) + else if (backend == BACKEND_PULSEAUDIO && PulseAudio::IsValid()) return std::make_unique(); - else if (backend == BACKEND_OPENSLES && OpenSLESStream::isValid()) + else if (backend == BACKEND_OPENSLES && OpenSLESStream::IsValid()) return std::make_unique(); - else if (backend == BACKEND_WASAPI && WASAPIStream::isValid()) + else if (backend == BACKEND_WASAPI && WASAPIStream::IsValid()) return std::make_unique(); return {}; } @@ -96,7 +96,7 @@ std::string GetDefaultSoundBackend() #if defined ANDROID backend = BACKEND_OPENSLES; #elif defined __linux__ - if (AlsaSound::isValid()) + if (AlsaSound::IsValid()) backend = BACKEND_ALSA; #elif defined(__APPLE__) || defined(_WIN32) backend = BACKEND_CUBEB; @@ -115,15 +115,15 @@ std::vector GetSoundBackends() backends.emplace_back(BACKEND_NULLSOUND); backends.emplace_back(BACKEND_CUBEB); - if (AlsaSound::isValid()) + if (AlsaSound::IsValid()) backends.emplace_back(BACKEND_ALSA); - if (PulseAudio::isValid()) + if (PulseAudio::IsValid()) backends.emplace_back(BACKEND_PULSEAUDIO); - if (OpenALStream::isValid()) + if (OpenALStream::IsValid()) backends.emplace_back(BACKEND_OPENAL); - if (OpenSLESStream::isValid()) + if (OpenSLESStream::IsValid()) backends.emplace_back(BACKEND_OPENSLES); - if (WASAPIStream::isValid()) + if (WASAPIStream::IsValid()) backends.emplace_back(BACKEND_WASAPI); return backends; diff --git a/Source/Core/AudioCommon/NullSoundStream.h b/Source/Core/AudioCommon/NullSoundStream.h index 6d90092425..f186787900 100644 --- a/Source/Core/AudioCommon/NullSoundStream.h +++ b/Source/Core/AudioCommon/NullSoundStream.h @@ -12,5 +12,5 @@ public: bool SetRunning(bool running) override; void SetVolume(int volume) override; - static bool isValid() { return true; } + static bool IsValid() { return true; } }; diff --git a/Source/Core/AudioCommon/OpenALStream.cpp b/Source/Core/AudioCommon/OpenALStream.cpp index b2e61dae98..3e56fe8e08 100644 --- a/Source/Core/AudioCommon/OpenALStream.cpp +++ b/Source/Core/AudioCommon/OpenALStream.cpp @@ -83,7 +83,7 @@ static bool InitLibrary() return true; } -bool OpenALStream::isValid() +bool OpenALStream::IsValid() { return InitLibrary(); } diff --git a/Source/Core/AudioCommon/OpenALStream.h b/Source/Core/AudioCommon/OpenALStream.h index bb42bf6689..c252838568 100644 --- a/Source/Core/AudioCommon/OpenALStream.h +++ b/Source/Core/AudioCommon/OpenALStream.h @@ -59,7 +59,7 @@ public: void SetVolume(int volume) override; bool SetRunning(bool running) override; - static bool isValid(); + static bool IsValid(); private: void SoundLoop(); diff --git a/Source/Core/AudioCommon/OpenSLESStream.h b/Source/Core/AudioCommon/OpenSLESStream.h index 588d6a97d3..08e8d0ed4a 100644 --- a/Source/Core/AudioCommon/OpenSLESStream.h +++ b/Source/Core/AudioCommon/OpenSLESStream.h @@ -16,7 +16,7 @@ public: bool Init() override; bool SetRunning(bool running) override { return true; } void SetVolume(int volume) override; - static bool isValid() { return true; } + static bool IsValid() { return true; } private: std::thread thread; diff --git a/Source/Core/AudioCommon/PulseAudioStream.h b/Source/Core/AudioCommon/PulseAudioStream.h index 0be6143552..acbf0dadcb 100644 --- a/Source/Core/AudioCommon/PulseAudioStream.h +++ b/Source/Core/AudioCommon/PulseAudioStream.h @@ -21,7 +21,7 @@ public: bool Init() override; bool SetRunning(bool running) override { return true; } - static bool isValid() { return true; } + static bool IsValid() { return true; } void StateCallback(pa_context* c); void WriteCallback(pa_stream* s, size_t length); void UnderflowCallback(pa_stream* s); diff --git a/Source/Core/AudioCommon/SoundStream.h b/Source/Core/AudioCommon/SoundStream.h index 3fa4a1775a..9e1be0d33f 100644 --- a/Source/Core/AudioCommon/SoundStream.h +++ b/Source/Core/AudioCommon/SoundStream.h @@ -16,7 +16,7 @@ protected: public: SoundStream() : m_mixer(new Mixer(48000)) {} virtual ~SoundStream() {} - static bool isValid() { return false; } + static bool IsValid() { return false; } Mixer* GetMixer() const { return m_mixer.get(); } virtual bool Init() { return false; } virtual void SetVolume(int) {} diff --git a/Source/Core/AudioCommon/WASAPIStream.cpp b/Source/Core/AudioCommon/WASAPIStream.cpp index efe630689a..d65471226b 100644 --- a/Source/Core/AudioCommon/WASAPIStream.cpp +++ b/Source/Core/AudioCommon/WASAPIStream.cpp @@ -49,7 +49,7 @@ WASAPIStream::~WASAPIStream() m_thread.join(); } -bool WASAPIStream::isValid() +bool WASAPIStream::IsValid() { return true; } diff --git a/Source/Core/AudioCommon/WASAPIStream.h b/Source/Core/AudioCommon/WASAPIStream.h index d0cdaf900c..6f3218cc4b 100644 --- a/Source/Core/AudioCommon/WASAPIStream.h +++ b/Source/Core/AudioCommon/WASAPIStream.h @@ -36,7 +36,7 @@ public: bool Init() override; bool SetRunning(bool running) override; - static bool isValid(); + static bool IsValid(); static std::vector GetAvailableDevices(); static Microsoft::WRL::ComPtr GetDeviceByName(std::string_view name);