From 7db3c817eb842b940c0065c9b47eafe7a3f967ac Mon Sep 17 00:00:00 2001 From: Clownacy Date: Mon, 14 Dec 2020 16:22:39 +0000 Subject: [PATCH] Fixed SDL audio using the wrong audio specification This bug was responsible for audio on Windows not working without forcefully setting the audio driver to DirectSound. The problem was that WASAPI (or whatever API SDL2 was choosing) defaults to an incompatible audio specification (possibly F32 sample format - I didn't check). Despite the `audio` struct requesting the S16 sample format, SDL2 was granted permission to use whatever format it preferred because it was given the `SDL_AUDIO_ALLOW_ANY_CHANGE` flag. To fix this, I've removed that flag, effectively forcing SDL2 to use the audio specification VBA-M requires. --- src/common/SoundSDL.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/common/SoundSDL.cpp b/src/common/SoundSDL.cpp index 4abcced8..31dade06 100644 --- a/src/common/SoundSDL.cpp +++ b/src/common/SoundSDL.cpp @@ -55,7 +55,7 @@ void SoundSDL::read(uint16_t* stream, int length) { if (length <= 0) return; - SDL_memset(stream, audio_spec.silence, length); + SDL_memset(stream, 0, length); // if not initialzed, paused or shutting down, do nothing if (!initialized || !emulating) @@ -117,11 +117,6 @@ void SoundSDL::write(uint16_t * finalWave, int length) { bool SoundSDL::init(long sampleRate) { if (initialized) deinit(); - // no sound on windows unless we do this -#ifdef _WIN32 - SDL_setenv("SDL_AUDIODRIVER", "directsound", true); -#endif - SDL_AudioSpec audio; SDL_memset(&audio, 0, sizeof(audio)); @@ -136,7 +131,7 @@ bool SoundSDL::init(long sampleRate) { if (!SDL_WasInit(SDL_INIT_AUDIO)) SDL_Init(SDL_INIT_AUDIO); - sound_device = SDL_OpenAudioDevice(NULL, 0, &audio, &audio_spec, SDL_AUDIO_ALLOW_ANY_CHANGE); + sound_device = SDL_OpenAudioDevice(NULL, 0, &audio, NULL, 0); if(sound_device == 0) { std::cerr << "Failed to open audio: " << SDL_GetError() << std::endl;