From 84c6455c953212c206517b9f4a2a5ed647bff040 Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Mon, 26 Oct 2015 00:01:01 +0100 Subject: [PATCH] spu2x-sdl: add a gui setting to select the API The purpose is to workaround bug with default API Code is not ideal because SDL/gui are mixed. But it would be enough for future release. V2: ifdef SDL2 code --- plugins/spu2-x/src/Linux/Config.cpp | 41 +++++++++++++++++++++++++ plugins/spu2-x/src/SndOut_SDL.cpp | 46 ++++++++++++++++++++++++----- 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/plugins/spu2-x/src/Linux/Config.cpp b/plugins/spu2-x/src/Linux/Config.cpp index a61df22fb6..81c7579af5 100644 --- a/plugins/spu2-x/src/Linux/Config.cpp +++ b/plugins/spu2-x/src/Linux/Config.cpp @@ -19,6 +19,9 @@ #include "Dialogs.h" #include "Config.h" +#include +#include + #ifdef PCSX2_DEVBUILD static const int LATENCY_MAX = 3000; #else @@ -70,6 +73,7 @@ u32 OutputModule = 0; int SndOutLatencyMS = 300; int SynchMode = 0; // Time Stretch, Async or Disabled static u32 OutputAPI = 0; +static u32 SdlOutputAPI = 0; int numSpeakers = 0; int dplLevel = 0; @@ -121,10 +125,21 @@ void ReadSettings() if (temp == L"OSS") OutputAPI = 1; if (temp == L"JACK") OutputAPI = 2; + CfgReadStr( L"SDL", L"HostApi", temp, L"pulseaudio" ); + SdlOutputAPI = -1; +#if SDL_MAJOR_VERSION >= 2 + // YES It sucks ... + for (int i = 0; i < SDL_GetNumAudioDrivers(); ++i) { + if (!temp.Cmp(SDL_GetAudioDriver(i))) + SdlOutputAPI = i; + } +#endif + SndOutLatencyMS = CfgReadInt(L"OUTPUT",L"Latency", 300); SynchMode = CfgReadInt( L"OUTPUT", L"Synch_Mode", 0); PortaudioOut->ReadSettings(); + SDLOut->ReadSettings(); SoundtouchCfg::ReadSettings(); DebugConfig::ReadSettings(); @@ -167,6 +182,7 @@ void WriteSettings() CfgWriteInt(L"OUTPUT",L"Synch_Mode", SynchMode); PortaudioOut->WriteSettings(); + SDLOut->WriteSettings(); SoundtouchCfg::WriteSettings(); DebugConfig::WriteSettings(); } @@ -198,6 +214,9 @@ void DisplayDialog() GtkWidget *output_frame, *output_box; GtkWidget *mod_label, *mod_box; GtkWidget *api_label, *api_box; +#if SDL_MAJOR_VERSION >= 2 + GtkWidget *sdl_api_label, *sdl_api_box; +#endif GtkWidget *latency_label, *latency_slide; GtkWidget *sync_label, *sync_box; GtkWidget *advanced_button; @@ -242,6 +261,16 @@ void DisplayDialog() gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(api_box), "2 - JACK"); gtk_combo_box_set_active(GTK_COMBO_BOX(api_box), OutputAPI); +#if SDL_MAJOR_VERSION >= 2 + sdl_api_label = gtk_label_new ("SDL API:"); + sdl_api_box = gtk_combo_box_text_new (); + // YES It sucks ... + for (int i = 0; i < SDL_GetNumAudioDrivers(); ++i) { + gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(sdl_api_box), SDL_GetAudioDriver(i)); + } + gtk_combo_box_set_active(GTK_COMBO_BOX(sdl_api_box), SdlOutputAPI); +#endif + latency_label = gtk_label_new ("Latency:"); #if GTK_MAJOR_VERSION < 3 latency_slide = gtk_hscale_new_with_range(LATENCY_MIN, LATENCY_MAX, 5); @@ -282,6 +311,10 @@ void DisplayDialog() gtk_container_add(GTK_CONTAINER(output_box), mod_box); gtk_container_add(GTK_CONTAINER(output_box), api_label); gtk_container_add(GTK_CONTAINER(output_box), api_box); +#if SDL_MAJOR_VERSION >= 2 + gtk_container_add(GTK_CONTAINER(output_box), sdl_api_label); + gtk_container_add(GTK_CONTAINER(output_box), sdl_api_box); +#endif gtk_container_add(GTK_CONTAINER(output_box), sync_label); gtk_container_add(GTK_CONTAINER(output_box), sync_box); gtk_container_add(GTK_CONTAINER(output_box), latency_label); @@ -327,6 +360,14 @@ void DisplayDialog() } } +#if SDL_MAJOR_VERSION >= 2 + if (gtk_combo_box_get_active(GTK_COMBO_BOX(sdl_api_box)) != -1) { + SdlOutputAPI = gtk_combo_box_get_active(GTK_COMBO_BOX(sdl_api_box)); + // YES It sucks ... + SDLOut->SetApiSettings(wxString(SDL_GetAudioDriver(SdlOutputAPI))); + } +#endif + SndOutLatencyMS = gtk_range_get_value(GTK_RANGE(latency_slide)); if (gtk_combo_box_get_active(GTK_COMBO_BOX(sync_box)) != -1) diff --git a/plugins/spu2-x/src/SndOut_SDL.cpp b/plugins/spu2-x/src/SndOut_SDL.cpp index 4e3c63dad4..dce06f4ec9 100644 --- a/plugins/spu2-x/src/SndOut_SDL.cpp +++ b/plugins/spu2-x/src/SndOut_SDL.cpp @@ -18,6 +18,7 @@ #include "Global.h" #include "SndOut.h" +#include "Dialogs.h" #include @@ -67,8 +68,13 @@ namespace { struct SDLAudioMod : public SndOutModule { static SDLAudioMod mod; + wxString m_api; s32 Init() { + ReadSettings(); + + fprintf(stderr, "SDL audio driver is %s\n", static_cast(m_api.c_str())); + /* SDL backends will mangle the AudioSpec and change the sample count. If we reopen * the audio backend, we need to make sure we keep our desired samples in the spec */ spec.samples = desiredSamples; @@ -96,20 +102,46 @@ struct SDLAudioMod : public SndOutModule { SDL_CloseAudio(); } + ~SDLAudioMod() { Close(); } + s32 Test() const { return 0; } - void Configure(uptr parent) {} - void ReadSettings() {} - void SetApiSettings(wxString api) {} - void WriteSettings() const {}; int GetEmptySampleCount() { return 0; } - ~SDLAudioMod() { Close(); } + void Configure(uptr parent) {} + + void ReadSettings() { + wxString api(L"EMPTYEMPTYEMPTY"); + CfgReadStr(L"SDL", L"HostApi", api, L"pulseaudio"); + SetApiSettings(api); + } + + void WriteSettings() const { + CfgWriteStr(L"SDL", L"HostApi", m_api); + }; + + void SetApiSettings(wxString api) { +#if SDL_MAJOR_VERSION >= 2 + // Validate the api name + bool valid = false; + for (int i = 0; i < SDL_GetNumAudioDrivers(); ++i) { + valid |= (api.Cmp(wxString(SDL_GetAudioDriver(i))) == 0); + } + if (valid) { + m_api = api; + } else { + fprintf(stderr, "SDL audio driver configuration is invalid!\n" + "It will be replaced by pulseaudio!\n"); + m_api = "pulseaudio"; + } +#endif + } + private: SDL_AudioSpec spec; - SDLAudioMod() - : spec({SampleRate, format, channels, 0, + SDLAudioMod() : m_api("pulseaudio"), + spec({SampleRate, format, channels, 0, desiredSamples, 0, 0, &callback_fillBuffer, nullptr}) { // Number of samples must be a multiple of packet size.