From 7b8ab475d542ff4a94109b0fb1b2575e37feeffa Mon Sep 17 00:00:00 2001 From: neonloop Date: Wed, 22 Sep 2021 15:37:20 +0000 Subject: Skips audio writing when buffer size is 0 This keeps from hanging when audio is sent before content is loaded (pcsx_rearmed). picoarch cannot know correct frame rate / sample rate before content is loaded (incorrect to call retro_get_system_av_info then) so best to just skip it --- plat_sdl.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'plat_sdl.c') diff --git a/plat_sdl.c b/plat_sdl.c index c7c6050..0596ce7 100644 --- a/plat_sdl.c +++ b/plat_sdl.c @@ -16,7 +16,6 @@ struct audio_state { unsigned buf_r; size_t buf_len; struct audio_frame *buf; - int freq; int in_sample_rate; int out_sample_rate; }; @@ -256,6 +255,9 @@ finish: static void plat_sound_callback(void *unused, uint8_t *stream, int len) { int16_t *p = (int16_t *)stream; + if (audio.buf_len == 0) + return; + len /= (sizeof(int16_t) * 2); while (audio.buf_r != audio.buf_w && len > 0) { @@ -315,6 +317,9 @@ static int plat_sound_init(void) float plat_sound_capacity(void) { int buffered = 0; + if (audio.buf_len == 0) + return 1.0; + if (audio.buf_w != audio.buf_r) { buffered = audio.buf_w > audio.buf_r ? audio.buf_w - audio.buf_r : @@ -360,14 +365,22 @@ void plat_sound_write(const struct audio_frame *data, int frames) void plat_sound_resize_buffer(void) { size_t buf_size; + SDL_LockAudio(); + audio.buf_len = frame_rate > 0 ? current_audio_buffer_size * audio.in_sample_rate / frame_rate - : 2; + : 0; + + if (audio.buf_len == 0) { + SDL_UnlockAudio(); + return; + } buf_size = audio.buf_len * sizeof(struct audio_frame); audio.buf = realloc(audio.buf, buf_size); if (!audio.buf) { + SDL_UnlockAudio(); PA_ERROR("Error initializing sound buffer\n"); plat_sound_finish(); return; @@ -377,6 +390,7 @@ void plat_sound_resize_buffer(void) { audio.buf_w = 0; audio.buf_r = 0; audio.max_buf_w = audio.buf_len - 1; + SDL_UnlockAudio(); } void plat_sdl_event_handler(void *event_) -- cgit v1.2.3