AudioSDL 3.2

SDL_PutAudioStreamData

Queues sound samples into a stream. SDL copies them and plays them in order.

Signature

bool SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);

How it works

The data must be in the stream's input format (the spec you gave). It is copied, so you can free or reuse your buffer right after the call: "fire and forget" for a sound effect.

len is in BYTES, not samples: for float samples multiply the count by sizeof(float). SDL_GetAudioStreamQueued tells how many bytes are still waiting, which lets a generator keep the queue short (low latency) but never empty (no gaps).

Parameters

streamSDL_AudioStream *

The stream.

bufconst void *

The samples, interleaved for stereo (L R L R…).

lenint

Size in bytes.

Return value

true on success, false on failure.

Example

example.cpp
1// keep about 0.1 s of a 440 Hz tone queued
2const int rate = 48000;
3if (SDL_GetAudioStreamQueued(stream) < int(rate * 0.1f * sizeof(float))) {
4    float buf[1024];
5    for (float& s : buf) { s = 0.2f * std::sin(phase); phase += 2 * SDL_PI_F * 440 / rate; }
6    SDL_PutAudioStreamData(stream, buf, sizeof buf);
7}
Official SDL wiki