From 990b74cc176b564b1d417235120022df273278cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 16 Jun 2025 18:48:53 +0200 Subject: [PATCH] More audio refactoring --- CMakeLists.txt | 4 ++-- Common/System/NativeApp.h | 2 +- Common/System/System.h | 1 + Qt/QtMain.cpp | 2 +- SDL/SDLMain.cpp | 2 +- UI/AudioCommon.cpp | 2 +- UI/NativeApp.cpp | 2 ++ Windows/PPSSPP.vcxproj | 6 +++--- Windows/PPSSPP.vcxproj.filters | 4 ++-- Windows/{WASAPIStream.cpp => WASAPIContext.cpp} | 6 +++--- Windows/{WASAPIStream.h => WASAPIContext.h} | 1 + Windows/WindowsAudio.cpp | 2 +- Windows/WindowsAudio.h | 3 ++- Windows/main.cpp | 7 +++++++ android/jni/AndroidAudio.h | 2 +- android/jni/OpenSLContext.cpp | 2 +- ios/AudioEngine.mm | 4 ++-- ios/iOSCoreAudio.mm | 4 ++-- 18 files changed, 34 insertions(+), 22 deletions(-) rename Windows/{WASAPIStream.cpp => WASAPIContext.cpp} (99%) rename Windows/{WASAPIStream.h => WASAPIContext.h} (95%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cfb147ca6..3d0fb9cc34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2625,8 +2625,8 @@ add_dependencies(${CoreLibName} GitVersion) set(WindowsFiles Windows/WindowsAudio.cpp Windows/WindowsAudio.h - Windows/WASAPIStream.cpp - Windows/WASAPIStream.h + Windows/WASAPIContext.cpp + Windows/WASAPIContext.h Windows/Debugger/BreakpointWindow.cpp Windows/Debugger/BreakpointWindow.h Windows/Debugger/DumpMemoryWindow.cpp diff --git a/Common/System/NativeApp.h b/Common/System/NativeApp.h index 8eef162c5b..ac9ef3bb45 100644 --- a/Common/System/NativeApp.h +++ b/Common/System/NativeApp.h @@ -64,7 +64,7 @@ void NativeFrame(GraphicsContext *graphicsContext); // the rest of the game, so be careful with synchronization. // Returns the number of samples actually output. The app should do everything it can // to fill the buffer completely. -int NativeMix(short *audio, int num_samples, int sampleRateHz); +int NativeMix(short *audio, int num_samples, int sampleRateHz, void *userdata); // Called when it's time to shutdown. After this has been called, // no more calls to any other function will be made from the framework diff --git a/Common/System/System.h b/Common/System/System.h index a629d05c70..cd44f882c4 100644 --- a/Common/System/System.h +++ b/Common/System/System.h @@ -254,6 +254,7 @@ enum class SystemNotification { UI_STATE_CHANGED, AUDIO_MODE_CHANGED, APP_SWITCH_MODE_CHANGED, + POLL_AUDIO_DEVICE, }; // I guess it's not super great architecturally to centralize this, since it's not general - but same with a lot of diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index 4da875d610..5e68f16153 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -78,7 +78,7 @@ SDL_AudioSpec g_retFmt; static SDL_AudioDeviceID audioDev = 0; extern void mixaudio(void *userdata, Uint8 *stream, int len) { - NativeMix((short *)stream, len / 4, AUDIO_FREQ); + NativeMix((short *)stream, len / 4, AUDIO_FREQ, userdata); } static void InitSDLAudioDevice() { diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index 7a2d836c07..0f11295def 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -134,7 +134,7 @@ int getDisplayNumber(void) { } void sdl_mixaudio_callback(void *userdata, Uint8 *stream, int len) { - NativeMix((short *)stream, len / (2 * 2), g_sampleRate); + NativeMix((short *)stream, len / (2 * 2), g_sampleRate, userdata); } static SDL_AudioDeviceID audioDev = 0; diff --git a/UI/AudioCommon.cpp b/UI/AudioCommon.cpp index d43dad337f..15b345f8b4 100644 --- a/UI/AudioCommon.cpp +++ b/UI/AudioCommon.cpp @@ -7,7 +7,7 @@ StereoResampler g_resampler; // numFrames is number of stereo frames. // This is called from *outside* the emulator thread. -int NativeMix(int16_t *outStereo, int numFrames, int sampleRateHz) { +int NativeMix(int16_t *outStereo, int numFrames, int sampleRateHz, void *userdata) { int validFrames = g_resampler.Mix(outStereo, numFrames, false, sampleRateHz); // Mix sound effects on top. diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 477f75648f..be88fd9a35 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1071,6 +1071,8 @@ void NativeFrame(GraphicsContext *graphicsContext) { ProcessWheelRelease(NKCODE_EXT_MOUSEWHEEL_UP, startTime, false); ProcessWheelRelease(NKCODE_EXT_MOUSEWHEEL_DOWN, startTime, false); + System_Notify(SystemNotification::POLL_AUDIO_DEVICE); + SetOverrideScreenFrame(nullptr); // it's ok to call this redundantly with DoFrame from EmuScreen diff --git a/Windows/PPSSPP.vcxproj b/Windows/PPSSPP.vcxproj index 6d887b7620..0bf9a82814 100644 --- a/Windows/PPSSPP.vcxproj +++ b/Windows/PPSSPP.vcxproj @@ -718,7 +718,7 @@ true - + @@ -1166,7 +1166,7 @@ - + @@ -1434,4 +1434,4 @@ - + \ No newline at end of file diff --git a/Windows/PPSSPP.vcxproj.filters b/Windows/PPSSPP.vcxproj.filters index b10c6bfaf4..10cd3c39bc 100644 --- a/Windows/PPSSPP.vcxproj.filters +++ b/Windows/PPSSPP.vcxproj.filters @@ -181,7 +181,7 @@ Windows\System - + Windows\System @@ -390,7 +390,7 @@ Windows\System - + Windows\System diff --git a/Windows/WASAPIStream.cpp b/Windows/WASAPIContext.cpp similarity index 99% rename from Windows/WASAPIStream.cpp rename to Windows/WASAPIContext.cpp index c825fb60c0..a7adfd73df 100644 --- a/Windows/WASAPIStream.cpp +++ b/Windows/WASAPIContext.cpp @@ -2,7 +2,7 @@ #include #include "WindowsAudio.h" -#include "WASAPIStream.h" +#include "WASAPIContext.h" #include "Common/Log.h" #include "Common/LogReporting.h" #include "Core/Config.h" @@ -492,7 +492,7 @@ void WASAPIAudioThread::Run() { int chans = deviceFormat_->Format.nChannels; switch (format_) { case Format::IEEE_FLOAT: - callback_(shortBuf_, pNumAvFrames, sampleRate_); + callback_(shortBuf_, pNumAvFrames, sampleRate_, nullptr); if (chans == 1) { float *ptr = (float *)pData; memset(ptr, 0, pNumAvFrames * chans * sizeof(float)); @@ -511,7 +511,7 @@ void WASAPIAudioThread::Run() { } break; case Format::PCM16: - callback_((short *)pData, pNumAvFrames, sampleRate_); + callback_((short *)pData, pNumAvFrames, sampleRate_, nullptr); break; case Format::UNKNOWN: break; diff --git a/Windows/WASAPIStream.h b/Windows/WASAPIContext.h similarity index 95% rename from Windows/WASAPIStream.h rename to Windows/WASAPIContext.h index a518634d93..83163c1957 100644 --- a/Windows/WASAPIStream.h +++ b/Windows/WASAPIContext.h @@ -13,6 +13,7 @@ public: bool Init(StreamCallback callback, void *userdata) override; // If fails, can safely delete the object int GetSampleRate() const override { return sampleRate_; } int PeriodFrames() const override { return periodFrames_; } // amount of frames normally requested + void FrameUpdate() override {} private: int RunThread(); diff --git a/Windows/WindowsAudio.cpp b/Windows/WindowsAudio.cpp index ecef97ad30..de41fc2c54 100644 --- a/Windows/WindowsAudio.cpp +++ b/Windows/WindowsAudio.cpp @@ -1,6 +1,6 @@ #include "Common/OSVersion.h" #include "WindowsAudio.h" -#include "WASAPIStream.h" +#include "WASAPIContext.h" WindowsAudioBackend *CreateAudioBackend(AudioBackendType type) { return new WASAPIAudioBackend(); diff --git a/Windows/WindowsAudio.h b/Windows/WindowsAudio.h index 3d182d1c07..1c6122f263 100644 --- a/Windows/WindowsAudio.h +++ b/Windows/WindowsAudio.h @@ -4,7 +4,7 @@ #include "Core/ConfigValues.h" // Always 2 channels, 16-bit audio. -typedef int (*StreamCallback)(short *buffer, int numSamples, int rate); +typedef int (*StreamCallback)(short *buffer, int numSamples, int rate, void *userdata); // Note that the backend may override the passed in sample rate. The actual sample rate // should be returned by GetSampleRate though. @@ -14,6 +14,7 @@ public: virtual bool Init(StreamCallback _callback, void *userdata = nullptr) = 0; virtual int GetSampleRate() const = 0; virtual int PeriodFrames() const = 0; + virtual void FrameUpdate() {} }; // Factory diff --git a/Windows/main.cpp b/Windows/main.cpp index c00299411c..3964432d20 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -460,6 +460,12 @@ void System_Notify(SystemNotification notification) { PostDialogMessage(memoryWindow, WM_DEB_UPDATE); break; + case SystemNotification::POLL_AUDIO_DEVICE: + if (winAudioBackend) { + winAudioBackend->FrameUpdate(); + } + break; + case SystemNotification::DISASSEMBLY: if (disasmWindow) PostDialogMessage(disasmWindow, WM_DEB_UPDATE); @@ -487,6 +493,7 @@ void System_Notify(SystemNotification notification) { case SystemNotification::POLL_CONTROLLERS: g_inputManager.PollControllers(); + // Also poll the audio backend for changes. break; case SystemNotification::TOGGLE_DEBUG_CONSOLE: diff --git a/android/jni/AndroidAudio.h b/android/jni/AndroidAudio.h index 50d0a26d28..c1879a5a89 100644 --- a/android/jni/AndroidAudio.h +++ b/android/jni/AndroidAudio.h @@ -3,7 +3,7 @@ #include #include -typedef int (*AndroidAudioCallback)(short *buffer, int numSamples, int sampleRateHz); +typedef int (*AndroidAudioCallback)(short *buffer, int numSamples, int sampleRateHz, void *userdata); class AudioContext { public: diff --git a/android/jni/OpenSLContext.cpp b/android/jni/OpenSLContext.cpp index f90796974f..e0a5fbbdda 100644 --- a/android/jni/OpenSLContext.cpp +++ b/android/jni/OpenSLContext.cpp @@ -49,7 +49,7 @@ void OpenSLContext::BqPlayerCallback(SLAndroidSimpleBufferQueueItf bq) { return; } - int renderedFrames = audioCallback(buffer[curBuffer], framesPerBuffer, SampleRate()); + int renderedFrames = audioCallback(buffer[curBuffer], framesPerBuffer, SampleRate(), nullptr); int sizeInBytes = framesPerBuffer * 2 * sizeof(short); int byteCount = (framesPerBuffer - renderedFrames) * 4; diff --git a/ios/AudioEngine.mm b/ios/AudioEngine.mm index 8090d7a93b..0c638608ac 100644 --- a/ios/AudioEngine.mm +++ b/ios/AudioEngine.mm @@ -19,7 +19,7 @@ static volatile BOOL done = 0; #define SAMPLE_SIZE 44100 static short stream[SAMPLE_SIZE]; -int NativeMix(short *audio, int numSamples, int sampleRateHz); +int NativeMix(short *audio, int numSamples, int sampleRateHz, void *userdata); @interface AudioEngine () @@ -125,7 +125,7 @@ int NativeMix(short *audio, int numSamples, int sampleRateHz); { size_t frames_ready; if (![self playing]) - frames_ready = NativeMix(stream, SAMPLE_SIZE / 2, sampleRateHz); + frames_ready = NativeMix(stream, SAMPLE_SIZE / 2, sampleRateHz, 0); else frames_ready = 0; diff --git a/ios/iOSCoreAudio.mm b/ios/iOSCoreAudio.mm index fb1ea97f1c..3152cc0005 100644 --- a/ios/iOSCoreAudio.mm +++ b/ios/iOSCoreAudio.mm @@ -74,7 +74,7 @@ void iOSCoreAudioSetDisplayConnected(bool connected) { iOSCoreAudioUpdateSession(); } -int NativeMix(short *audio, int numSamples, int sampleRate); +int NativeMix(short *audio, int numSamples, int sampleRate, void *userdata); OSStatus iOSCoreAudioCallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, @@ -85,7 +85,7 @@ OSStatus iOSCoreAudioCallback(void *inRefCon, { // see if we have any sound to play short *output = (short *)ioData->mBuffers[0].mData; - UInt32 framesReady = NativeMix(output, inNumberFrames, SAMPLE_RATE); + UInt32 framesReady = NativeMix(output, inNumberFrames, SAMPLE_RATE, nullptr); if (framesReady == 0) { // oops, we don't currently have any sound, so return silence