From f88faef1b2f5432d1fddae73c2bfbd2a56b7aa1c Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Wed, 16 Aug 2017 15:20:05 -0700 Subject: [PATCH] SoundSDL: write silence when paused #139 The SDL API documentation for the audio callback specifies that the callback *MUST* write to the buffer and not just return: https://wiki.libsdl.org/SDL_AudioSpec#Remarks write silence to the buffer (value taken from the AudioSpec returned from OpenAudioDevice) when the emulator is paused. --- src/common/SoundSDL.cpp | 12 +++++++++--- src/common/SoundSDL.h | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/common/SoundSDL.cpp b/src/common/SoundSDL.cpp index b7378cb4..679c2246 100644 --- a/src/common/SoundSDL.cpp +++ b/src/common/SoundSDL.cpp @@ -46,8 +46,13 @@ bool SoundSDL::should_wait() void SoundSDL::read(uint16_t * stream, int length) { - if (!_initialized || length <= 0 || !emulating) - return; + if (!_initialized || length <= 0) + return; + + if (!emulating) { + SDL_memset(stream, _audio_spec.silence, length); + return; + } if (should_wait()) SDL_SemWait (_semBufferFull); @@ -111,6 +116,7 @@ void SoundSDL::write(uint16_t * finalWave, int length) bool SoundSDL::init(long sampleRate) { SDL_AudioSpec audio; + SDL_memset(&audio, 0, sizeof(audio)); audio.freq = throttle ? sampleRate * (throttle / 100.0) : sampleRate; audio.format = AUDIO_S16SYS; audio.channels = 2; @@ -120,7 +126,7 @@ bool SoundSDL::init(long sampleRate) if (!SDL_WasInit(SDL_INIT_AUDIO)) SDL_Init(SDL_INIT_AUDIO); - _dev = SDL_OpenAudioDevice(NULL, 0, &audio, NULL, SDL_AUDIO_ALLOW_ANY_CHANGE); + _dev = SDL_OpenAudioDevice(NULL, 0, &audio, &_audio_spec, SDL_AUDIO_ALLOW_ANY_CHANGE); if(_dev < 0) { fprintf(stderr,"Failed to open audio: %s\n", SDL_GetError()); diff --git a/src/common/SoundSDL.h b/src/common/SoundSDL.h index 5d96fed7..fbe52b74 100644 --- a/src/common/SoundSDL.h +++ b/src/common/SoundSDL.h @@ -43,6 +43,7 @@ class SoundSDL : public SoundDriver SDL_sem *_semBufferFull; SDL_sem *_semBufferEmpty; SDL_AudioDeviceID _dev; + SDL_AudioSpec _audio_spec; int current_rate;