From 141258c366e124f731a99d219ec8f4014e4b4f07 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 15 Sep 2019 12:42:49 -0700 Subject: [PATCH] SDL: Use SDL2 audio API for output. --- Qt/QtMain.cpp | 20 +++++++++++++------- SDL/SDLMain.cpp | 16 ++++++++++------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index 3c9bdb102d..0e961d5ff8 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -29,7 +29,9 @@ #ifdef SDL #include "SDL/SDLJoystick.h" #include "SDL_audio.h" +#define MainAudioDeviceID SDL_AudioDeviceID #endif +#define MainAudioDeviceID int #include "QtMain.h" #include "gfx_es2/gpu_features.h" #include "i18n/i18n.h" @@ -168,7 +170,7 @@ float CalculateDPIScale() #endif } -static int mainInternal(QApplication &a) { +static int mainInternal(QApplication &a, MainAudioDeviceID &audioDev) { #ifdef MOBILE_DEVICE emugl = new MainUI(); emugl->resize(pixel_xres, pixel_yres); @@ -194,7 +196,8 @@ static int mainInternal(QApplication &a) { fmt.callback = &mixaudio; fmt.userdata = (void *)0; - if (SDL_OpenAudio(&fmt, &ret_fmt) < 0) { + audioDev = SDL_OpenAudioDevice(nullptr, 0, &fmt, &ret_fmt, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); + if (audioDev <= 0) { ELOG("Failed to open audio: %s", SDL_GetError()); } else { if (ret_fmt.samples != fmt.samples) // Notify, but still use it @@ -205,10 +208,10 @@ static int mainInternal(QApplication &a) { ELOG("Output audio format: %d (requested: %d)", ret_fmt.format, fmt.format); ELOG("Output audio channels: %d (requested: %d)", ret_fmt.channels, fmt.channels); ELOG("Provided output format does not match requirement, turning audio off"); - SDL_CloseAudio(); + SDL_CloseAudioDevice(audioDev); } + SDL_PauseAudioDevice(audioDev, 0); } - SDL_PauseAudio(0); #else QScopedPointer audio(new MainAudio()); audio->run(); @@ -608,12 +611,15 @@ int main(int argc, char *argv[]) // TODO: Support other backends than GL, like Vulkan, in the Qt backend. g_Config.iGPUBackend = (int)GPUBackend::OPENGL; - int ret = mainInternal(a); + MainAudioDeviceID audioDev = 0; + int ret = mainInternal(a, audioDev); ILOG("Left mainInternal here."); #ifdef SDL - SDL_PauseAudio(1); - SDL_CloseAudio(); + if (audioDev > 0) { + SDL_PauseAudioDevice(audioDev, 1); + SDL_CloseAudioDevice(audioDev); + } #endif NativeShutdown(); glslang::FinalizeProcess(); diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index 28c3548201..3a3246e686 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -582,7 +582,8 @@ int main(int argc, char *argv[]) { fmt.callback = &mixaudio; fmt.userdata = (void *)0; - if (SDL_OpenAudio(&fmt, &ret_fmt) < 0) { + SDL_AudioDeviceID audioDev = SDL_OpenAudioDevice(nullptr, 0, &fmt, &ret_fmt, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); + if (audioDev <= 0) { ELOG("Failed to open audio: %s", SDL_GetError()); } else { if (ret_fmt.samples != fmt.samples) // Notify, but still use it @@ -593,12 +594,13 @@ int main(int argc, char *argv[]) { ELOG("Output audio format: %d (requested: %d)", ret_fmt.format, fmt.format); ELOG("Output audio channels: %d (requested: %d)", ret_fmt.channels, fmt.channels); ELOG("Provided output format does not match requirement, turning audio off"); - SDL_CloseAudio(); + SDL_CloseAudioDevice(audioDev); } + + // Audio must be unpaused _after_ NativeInit() + SDL_PauseAudioDevice(audioDev, 0); } - // Audio must be unpaused _after_ NativeInit() - SDL_PauseAudio(0); if (joystick_enabled) { joystick = new SDLJoystick(); } else { @@ -927,8 +929,10 @@ int main(int argc, char *argv[]) { graphicsContext->ShutdownFromRenderThread(); delete graphicsContext; - SDL_PauseAudio(1); - SDL_CloseAudio(); + if (audioDev > 0) { + SDL_PauseAudioDevice(audioDev, 1); + SDL_CloseAudioDevice(audioDev); + } SDL_Quit(); #if PPSSPP_PLATFORM(RPI) bcm_host_deinit();