From 14980b25a86f6d4657ed1dfa8e273b62238a3259 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 2 Nov 2023 22:32:25 -0700 Subject: [PATCH] Clarify documentation for audio callback migration --- docs/README-migration.md | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/README-migration.md b/docs/README-migration.md index 6d0c9131f1..109c5543fc 100644 --- a/docs/README-migration.md +++ b/docs/README-migration.md @@ -62,7 +62,7 @@ In SDL2, you might have done something like this to play audio... ```c void SDLCALL MyAudioCallback(void *userdata, Uint8 * stream, int len) { - /* calculate a little more audio here, maybe using `userdata`, write it to `stream` */ + /* Calculate a little more audio here, maybe using `userdata`, write it to `stream` */ } /* ...somewhere near startup... */ @@ -81,15 +81,25 @@ In SDL2, you might have done something like this to play audio... ...in SDL3, you can do this... ```c - void SDLCALL MyAudioCallback(SDL_AudioStream *stream, int len, void *userdata) + void SDLCALL MyAudioCallback3(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount) { - /* calculate a little more audio here, maybe using `userdata`, write it to `stream` */ - SDL_PutAudioStreamData(stream, newdata, len); + /* Calculate a little more audio here, maybe using `userdata`, write it to `stream` + * + * If you want to reuse the original callback, you could do something like this: + */ + if (additional_amount > 0) { + Uint16 *data = SDL_stack_alloc(Uint16, additional_amount / sizeof(Uint16)); + if (data) { + MyAudioCallback(userdata, data, additional_amount); + SDL_PutAudioStreamData(stream, data, additional_amount); + SDL_stack_free(data); + } + } } /* ...somewhere near startup... */ const SDL_AudioSpec spec = { SDL_AUDIO_S16, 2, 44100 }; - SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &spec, MyAudioCallback, &my_audio_callback_user_data); + SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &spec, MyAudioCallback3, &my_audio_callback_user_data); SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream)); ```