AudioSDL 3.2

SDL_OpenAudioDeviceStream

The one-call way to play sound: opens a device and gives you a stream already connected to it.

Signature

SDL_AudioStream * SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata);

How it works

SDL3's audio is built on streams. A stream is a queue: you put samples in, in your format, and SDL converts them (rate, channels, sample type) to whatever the sound card wants. This function opens a device, creates one stream, and binds them together.

The device starts PAUSED, so nothing plays until SDL_ResumeAudioStreamDevice. Destroying the stream also closes the device. For several sounds at once (music + effects), open a device with SDL_OpenAudioDevice and bind several streams to it instead.

Parameters

devidSDL_AudioDeviceID

Usually SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK (follows the user's default output, even if it changes).

specconst SDL_AudioSpec *

The format YOU will put in: {format, channels, freq}, e.g. {SDL_AUDIO_F32, 2, 48000}.

callbackSDL_AudioStreamCallback

NULL to push data yourself with SDL_PutAudioStreamData; or a function SDL calls when the stream needs more (synths, generated sound).

userdatavoid *

Passed to the callback. Ignored if callback is NULL.

Return value

The stream, or NULL on failure.

Tips & pitfalls

  • A callback runs on the AUDIO thread: keep it short, never block, and protect shared data.

Example

example.cpp
1SDL_AudioSpec spec{SDL_AUDIO_F32, 1, 48000};   // mono float at 48 kHz
2SDL_AudioStream* stream = SDL_OpenAudioDeviceStream(
3    SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, nullptr, nullptr);
4SDL_ResumeAudioStreamDevice(stream);          // devices start paused
5SDL_PutAudioStreamData(stream, samples, int(count * sizeof(float)));
Official SDL wiki