From 5f2c7b966feb03b4a8d87b140c39f48a3ac7c953 Mon Sep 17 00:00:00 2001 From: Rupert Carmichael <5050061-carmiker@users.noreply.gitlab.com> Date: Sat, 9 May 2020 11:49:20 -0400 Subject: [PATCH 1/2] Pre-buffer audio samples before passing to SDL's queue --- SDL/audio/sdl.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/SDL/audio/sdl.c b/SDL/audio/sdl.c index 1f8a529..d8e7e37 100644 --- a/SDL/audio/sdl.c +++ b/SDL/audio/sdl.c @@ -25,6 +25,10 @@ static SDL_AudioDeviceID device_id; static SDL_AudioSpec want_aspec, have_aspec; +#define BUFFERSIZE 1024 +static int bufferpos = 0; +static int16_t audiobuffer[BUFFERSIZE]; + bool GB_audio_is_playing(void) { return SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING; @@ -53,7 +57,14 @@ size_t GB_audio_get_queue_length(void) void GB_audio_queue_sample(GB_sample_t *sample) { - SDL_QueueAudio(device_id, sample, sizeof(*sample)); + audiobuffer[bufferpos++] = sample->left; + audiobuffer[bufferpos++] = sample->right; + + if (bufferpos == BUFFERSIZE) + { + bufferpos = 0; + SDL_QueueAudio(device_id, (const void*)audiobuffer, BUFFERSIZE * sizeof(int16_t)); + } } void GB_audio_init(void) @@ -63,7 +74,7 @@ void GB_audio_init(void) want_aspec.freq = AUDIO_FREQUENCY; want_aspec.format = AUDIO_S16SYS; want_aspec.channels = 2; - want_aspec.samples = 512; + want_aspec.samples = 1024; SDL_version _sdl_version; SDL_GetVersion(&_sdl_version); From 1b7c3c4c7c29cff93f89b5a9836e5620a9ea6459 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 10 May 2020 22:16:49 +0300 Subject: [PATCH 2/2] Minor fixes, style update --- SDL/audio/sdl.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/SDL/audio/sdl.c b/SDL/audio/sdl.c index d8e7e37..12ee69a 100644 --- a/SDL/audio/sdl.c +++ b/SDL/audio/sdl.c @@ -25,9 +25,9 @@ static SDL_AudioDeviceID device_id; static SDL_AudioSpec want_aspec, have_aspec; -#define BUFFERSIZE 1024 -static int bufferpos = 0; -static int16_t audiobuffer[BUFFERSIZE]; +#define AUDIO_BUFFER_SIZE 512 +static unsigned buffer_pos = 0; +static GB_sample_t audio_buffer[AUDIO_BUFFER_SIZE]; bool GB_audio_is_playing(void) { @@ -57,13 +57,11 @@ size_t GB_audio_get_queue_length(void) void GB_audio_queue_sample(GB_sample_t *sample) { - audiobuffer[bufferpos++] = sample->left; - audiobuffer[bufferpos++] = sample->right; + audio_buffer[buffer_pos++] = *sample; - if (bufferpos == BUFFERSIZE) - { - bufferpos = 0; - SDL_QueueAudio(device_id, (const void*)audiobuffer, BUFFERSIZE * sizeof(int16_t)); + if (buffer_pos == AUDIO_BUFFER_SIZE) { + buffer_pos = 0; + SDL_QueueAudio(device_id, (const void *)audio_buffer, sizeof(audio_buffer)); } } @@ -74,7 +72,7 @@ void GB_audio_init(void) want_aspec.freq = AUDIO_FREQUENCY; want_aspec.format = AUDIO_S16SYS; want_aspec.channels = 2; - want_aspec.samples = 1024; + want_aspec.samples = 512; SDL_version _sdl_version; SDL_GetVersion(&_sdl_version);