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.
This commit is contained in:
Rafael Kitover 2017-08-16 15:20:05 -07:00
parent 4d7e102e26
commit f88faef1b2
2 changed files with 10 additions and 3 deletions

View File

@ -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());

View File

@ -43,6 +43,7 @@ class SoundSDL : public SoundDriver
SDL_sem *_semBufferFull;
SDL_sem *_semBufferEmpty;
SDL_AudioDeviceID _dev;
SDL_AudioSpec _audio_spec;
int current_rate;