SDL_AudioStreamCallback(3type)

πŸ’– SDL_AudioStreamCallback β€” a 3type from the SDL3 DATATYPES squad πŸ’–

okay bestie, sit down, hydrate, and let me tell you about SDL_AudioStreamCallback ✨ this is a 3type, which is the manual-section way of saying "it's a typedef, not a function you call yourself" β€” she lives in the SDL3 DATATYPES crew πŸŽ€


🌸 NAME 🌸

SDL_AudioStreamCallback β€” a callback that fires when data passes through an SDL_AudioStream. it's giving "hey, something's moving through the pipe, you wanna handle it?" πŸ’…


✨ SYNOPSIS ✨

#include <SDL3/SDL_audio.h>

typedef void (SDLCALL *SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount);

like… that's the whole shape of her. no cap 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea, bestie β˜• apps can optionally register a callback with an audio stream, and that callback gets called when data is added with SDL_PutAudioStreamData, or requested with SDL_GetAudioStreamData. optionally! as in, you don't have to. she's not needy πŸ’œ

Two values are offered here, and the way they work is kinda the whole point:

  • one is the amount of additional data needed to satisfy the immediate request β€” and lowkey this might be zero if the stream already has enough data queued up πŸ’€
  • the other is the total amount being requested

and the way these two interact depends on the direction, so listen up:

  • In a Get call triggering a Put callback, these values can be different πŸ™ˆ
  • In a Put call triggering a Get callback, these values are always the same βœ…

Byte counts might be slightly overestimated due to buffering or resampling, and may change from call to call. so don't get attached to your numbers, they're fluid, they're main characters, they do what they want 🌈

This callback is not required to do anything. genuinely! Generally this is useful for adding/reading data on demand, and the app will often put/get data as appropriate, but the system goes on with the data currently available to it if this callback does nothing. she's chill like that 😌

⚠️ DO NOT call SDL_DestroyAudioStream() on stream during this callback. fr fr. do not. not the vibe. absolutely not the move πŸ’€πŸš«

πŸŽ€ FUNCTION PARAMETERS πŸŽ€

  • stream β€” the SDL audio stream associated with this callback. your girl, the stream in question πŸ¦‹
  • additional_amount β€” the amount of data, in bytes, that is needed right now ⏰
  • total_amount β€” the total amount of data requested, in bytes, that is requested or available πŸ“Š
  • userdata β€” an opaque pointer provided by the app for their personal use πŸ™ˆ it's yours, do what you want with it

πŸ”₯ THREAD SAFETY πŸ”₯

okay this part matters, bestie. These callbacks may run from any thread 😭 so if you need to protect shared data, you should use SDL_LockAudioStream to serialize access. and here's the slay part: this lock will be held before your callback is called, so your callback does not need to manage the lock explicitly. the system already did it for you. she's got you 🫢


⭐ AVAILABILITY ⭐

This datatype is available since SDL 3.2.0. she's not new new, but she's not ancient either. iconic behavior πŸ‘‘


πŸ’œ SEE ALSO πŸ’œ

go read those next, iykyk πŸ“šβœ¨


k bye ily, this page came to you from Simple Directmedia Layer, running version SDL 3.4.18 πŸŽ€ that's the source and the version, right there, in the sign-off like a little wax seal πŸ’Œ mwah, go make some audio πŸ’…

πŸ“¦ where this page comes from