From a659ffb8b38ea025c3a007ae5c4cd193ed74e5ba Mon Sep 17 00:00:00 2001 From: thekovic <72971433+thekovic@users.noreply.github.com> Date: Wed, 11 Oct 2023 22:52:46 +0200 Subject: [PATCH] mixer: Add mixer_try_play helper function (#456) --- include/mixer.h | 12 ++++++++++++ src/audio/mixer.c | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/mixer.h b/include/mixer.h index bdde7d3cfb..8affc2c958 100644 --- a/include/mixer.h +++ b/include/mixer.h @@ -357,6 +357,18 @@ void mixer_unthrottle(void); */ void mixer_poll(int16_t *out, int nsamples); +/** + * @brief Request the mixer to try and write audio samples to be played, + * if possible. + * + * This function is a user helper for asking the mixer and audio subsystems + * to play audio during a game frame. You should call this function many times + * during one frame (eg. during the render step or after processing each game + * object) as many times as necessary. Not polling the audio subsystem often + * enough will result in audio stutter. + */ +void mixer_try_play(); + /** * @brief Callback invoked by mixer_poll at a specified time * diff --git a/src/audio/mixer.c b/src/audio/mixer.c index 08bbfdb0d5..118b727624 100644 --- a/src/audio/mixer.c +++ b/src/audio/mixer.c @@ -750,3 +750,13 @@ void mixer_poll(int16_t *out16, int num_samples) { } } } + +void mixer_try_play() +{ + if (audio_can_write()) + { + short *buf = audio_write_begin(); + mixer_poll(buf, audio_get_buffer_length()); + audio_write_end(); + } +}