SDL_MixAudio(3)

πŸ’– SDL_MixAudio πŸ’–

okay bestie, so this one's from the SDL3 FUNCTIONS family, and it's literally the girl who mixes your audio for you ✨


🌸 NAME 🌸

SDL_MixAudio β€” mixes audio data in a specified format. that's it, that's the job, she does it flawlessly πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <SDL3/SDL_audio.h>

bool SDL_MixAudio(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, float volume);

look at her, so clean, so iconic πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

so what she does is: she takes an audio buffer src β€” that's len bytes of format data β€” and mixes it into dst, and the way she does that is by performing addition, volume adjustment, and overflow clipping. no cap. and the buffer pointed to by dst has to also be len bytes of format data, so keep those two matching, bestie 🀝

she's provided for convenience β€” you can totally mix your own audio data if you want to be extra, that's your main character moment ✨

⚠️ do NOT use this function for mixing together more than two streams of sample data. like, seriously. the output from repeatedly applying this function may be distorted by clipping, because there's no accumulator with greater range than the input. (not to mention it's also just a straight up inefficient way of doing it πŸ™ˆ). so, two streams max. periodt.

ok and here's a thing people get wrong all the time, it's a common misconception that this function is required to write audio data to an output stream in an audio callback. and like β€” sure, you can do that, but SDL_MixAudio() is really only needed when you're mixing a single audio stream with a volume adjustment. that's her whole thing πŸ’œ


🌈 FUNCTION PARAMETERS 🌈

  • dst β€” the destination for the mixed audio 🫢
  • src β€” the source audio buffer to be mixed ✨
  • format β€” the SDL_AudioFormat structure representing the desired audio format πŸŽ€
  • len β€” the length of the audio buffer in bytes πŸ“
  • volume β€” ranges from 0.0 - 1.0, and should be set to 1.0 for full audio volume πŸ”Š (so 1.0 = loud and proud, 0.0 = silence, you get it)

⭐ RETURN VALUE ⭐

returns true on success or false on failure 😭 and if she fails, call SDL_GetError() for more information β€” she'll tell you what went wrong, she's not mysterious like that.


πŸ’€ THREAD SAFETY πŸ’€

it is safe to call this function from any thread. multitasking queen, we love to see it πŸ‘‘


πŸ“ AVAILABILITY πŸ“

this function has been available since SDL 3.2.0 🌱


🧚 SEE ALSO 🧚

hmm, the original didn't send us any SEE ALSO girlies, so that's a wrap on the cross-refs β€” but you already know SDL_GetError() from the return value section, iykyk πŸ’–


and that's the whole page!! written by the fine folks over at Simple Directmedia Layer, from SDL 3.4.18, dated SDL_MixAudio(3) πŸŽ€ stay mixed, stay clipped-free, mwah πŸ’…πŸ’–βœ¨

πŸ“¦ where this page comes from