SDL_GetAudioDeviceFormat(3)

πŸ’– SDL_GetAudioDeviceFormat πŸ’–

okay bestie, welcome to the SDL3 FUNCTIONS corner of the manual, this one's a whole function called SDL_GetAudioDeviceFormat and it's genuinely so useful, come sit with me 🫢

✨ NAME ✨

SDL_GetAudioDeviceFormat β€” get the current audio format of a specific audio device. yes that's literally it and it's iconic for it πŸ’…


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_audio.h>

bool SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec, int *sample_frames);

like look at that signature. three args, one boolean, no notes. slay.


πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the vibe. if the device is already opened, this function reports the format that device is currently using. periodt. that's the actual live format, not a guess.

but if the device isn't opened yet? then it reports the device's preferred format instead β€” or a reasonable default if that can't even be determined. so either way you get something, no cap πŸ™Œ

you can also pass in SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or SDL_AUDIO_DEVICE_DEFAULT_RECORDING as the devid, which is lowkey the best move for getting a reasonable recommendation before you go opening the system-recommended default device. chef's kiss, very main character energy πŸ‘‘

and here's the part that makes people go 😭 in a good way: you can use this to request the current device buffer size too. that number is in sample frames, and it represents how much data SDL will feed to the physical hardware in each chunk. so it's literally the size of one bite of audio, every time.

wanna convert that into milliseconds of audio? the equation is right here, copy it exactly, do not freestyle it:

ms = (int) ((((Sint64) frames) * 1000) / spec.freq);
real talk though: buffer size only matters if you need low-level control over the audio playback timing. most apps do NOT need this. like, at all. if you're not doing fancy timing stuff, you can genuinely just vibe and ignore it πŸ’€

πŸŽ€ FUNCTION PARAMETERS πŸŽ€

  • devid β€” the instance ID of the device you wanna query. that's it, that's the whole thing.
  • spec β€” on return, this gets filled with the device details. it's the output, bestie, that's where the tea lives β˜•
  • sample_frames β€” pointer to store the device buffer size, in sample frames. and good news: this one can be NULL. so if you don't care about the buffer size, just pass NULL and move on 🫢

🌈 RETURN VALUE 🌈

returns true on success or false on failure. if it fails, hit up SDL_GetError() for the whole story, because it will tell you what went wrong. always check your returns, i'm literally begging πŸ™ˆ


⭐ THREAD SAFETY ⭐

it is safe to call this function from any thread. any. thread. go wild, gorl 🧚


πŸ‘‘ AVAILABILITY πŸ‘‘

this function is available since SDL 3.2.0. so if you're on anything older, sorry babe, it's not there yet πŸ’œ


and that's the whole page!! brought to you by Simple Directmedia Layer, version SDL 3.4.18 β€” go make some beautiful audio, you've got this πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from