SDL 3.0
SDL_audio.h File Reference
+ Include dependency graph for SDL_audio.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDL_AudioSpec
 

Macros

#define SDL_AUDIO_MASK_BITSIZE   (0xFFu)
 
#define SDL_AUDIO_MASK_FLOAT   (1u<<8)
 
#define SDL_AUDIO_MASK_BIG_ENDIAN   (1u<<12)
 
#define SDL_AUDIO_MASK_SIGNED   (1u<<15)
 
#define SDL_DEFINE_AUDIO_FORMAT(signed, bigendian, flt, size)    (((Uint16)(signed) << 15) | ((Uint16)(bigendian) << 12) | ((Uint16)(flt) << 8) | ((size) & SDL_AUDIO_MASK_BITSIZE))
 
#define SDL_AUDIO_BITSIZE(x)   ((x) & SDL_AUDIO_MASK_BITSIZE)
 
#define SDL_AUDIO_BYTESIZE(x)   (SDL_AUDIO_BITSIZE(x) / 8)
 
#define SDL_AUDIO_ISFLOAT(x)   ((x) & SDL_AUDIO_MASK_FLOAT)
 
#define SDL_AUDIO_ISBIGENDIAN(x)   ((x) & SDL_AUDIO_MASK_BIG_ENDIAN)
 
#define SDL_AUDIO_ISLITTLEENDIAN(x)   (!SDL_AUDIO_ISBIGENDIAN(x))
 
#define SDL_AUDIO_ISSIGNED(x)   ((x) & SDL_AUDIO_MASK_SIGNED)
 
#define SDL_AUDIO_ISINT(x)   (!SDL_AUDIO_ISFLOAT(x))
 
#define SDL_AUDIO_ISUNSIGNED(x)   (!SDL_AUDIO_ISSIGNED(x))
 
#define SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK   ((SDL_AudioDeviceID) 0xFFFFFFFFu)
 
#define SDL_AUDIO_DEVICE_DEFAULT_RECORDING   ((SDL_AudioDeviceID) 0xFFFFFFFEu)
 
#define SDL_AUDIO_FRAMESIZE(x)   (SDL_AUDIO_BYTESIZE((x).format) * (x).channels)
 
#define SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN   "SDL.audiostream.auto_cleanup"
 

Typedefs

typedef Uint32 SDL_AudioDeviceID
 
typedef struct SDL_AudioStream SDL_AudioStream
 
typedef void(* SDL_AudioStreamDataCompleteCallback) (void *userdata, const void *buf, int buflen)
 
typedef void(* SDL_AudioStreamCallback) (void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount)
 
typedef void(* SDL_AudioPostmixCallback) (void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen)
 

Enumerations

enum  SDL_AudioFormat {
  SDL_AUDIO_UNKNOWN = 0x0000u ,
  SDL_AUDIO_U8 = 0x0008u ,
  SDL_AUDIO_S8 = 0x8008u ,
  SDL_AUDIO_S16LE = 0x8010u ,
  SDL_AUDIO_S16BE = 0x9010u ,
  SDL_AUDIO_S32LE = 0x8020u ,
  SDL_AUDIO_S32BE = 0x9020u ,
  SDL_AUDIO_F32LE = 0x8120u ,
  SDL_AUDIO_F32BE = 0x9120u ,
  SDL_AUDIO_S16 = SDL_AUDIO_S16LE ,
  SDL_AUDIO_S32 = SDL_AUDIO_S32LE ,
  SDL_AUDIO_F32 = SDL_AUDIO_F32LE
}
 

Functions

int SDL_GetNumAudioDrivers (void)
 
const char * SDL_GetAudioDriver (int index)
 
const char * SDL_GetCurrentAudioDriver (void)
 
SDL_AudioDeviceIDSDL_GetAudioPlaybackDevices (int *count)
 
SDL_AudioDeviceIDSDL_GetAudioRecordingDevices (int *count)
 
const char * SDL_GetAudioDeviceName (SDL_AudioDeviceID devid)
 
bool SDL_GetAudioDeviceFormat (SDL_AudioDeviceID devid, SDL_AudioSpec *spec, int *sample_frames)
 
int * SDL_GetAudioDeviceChannelMap (SDL_AudioDeviceID devid, int *count)
 
SDL_AudioDeviceID SDL_OpenAudioDevice (SDL_AudioDeviceID devid, const SDL_AudioSpec *spec)
 
bool SDL_IsAudioDevicePhysical (SDL_AudioDeviceID devid)
 
bool SDL_IsAudioDevicePlayback (SDL_AudioDeviceID devid)
 
bool SDL_PauseAudioDevice (SDL_AudioDeviceID devid)
 
bool SDL_ResumeAudioDevice (SDL_AudioDeviceID devid)
 
bool SDL_AudioDevicePaused (SDL_AudioDeviceID devid)
 
float SDL_GetAudioDeviceGain (SDL_AudioDeviceID devid)
 
bool SDL_SetAudioDeviceGain (SDL_AudioDeviceID devid, float gain)
 
void SDL_CloseAudioDevice (SDL_AudioDeviceID devid)
 
bool SDL_BindAudioStreams (SDL_AudioDeviceID devid, SDL_AudioStream *const *streams, int num_streams)
 
bool SDL_BindAudioStream (SDL_AudioDeviceID devid, SDL_AudioStream *stream)
 
void SDL_UnbindAudioStreams (SDL_AudioStream *const *streams, int num_streams)
 
void SDL_UnbindAudioStream (SDL_AudioStream *stream)
 
SDL_AudioDeviceID SDL_GetAudioStreamDevice (SDL_AudioStream *stream)
 
SDL_AudioStreamSDL_CreateAudioStream (const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec)
 
SDL_PropertiesID SDL_GetAudioStreamProperties (SDL_AudioStream *stream)
 
bool SDL_GetAudioStreamFormat (SDL_AudioStream *stream, SDL_AudioSpec *src_spec, SDL_AudioSpec *dst_spec)
 
bool SDL_SetAudioStreamFormat (SDL_AudioStream *stream, const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec)
 
float SDL_GetAudioStreamFrequencyRatio (SDL_AudioStream *stream)
 
bool SDL_SetAudioStreamFrequencyRatio (SDL_AudioStream *stream, float ratio)
 
float SDL_GetAudioStreamGain (SDL_AudioStream *stream)
 
bool SDL_SetAudioStreamGain (SDL_AudioStream *stream, float gain)
 
int * SDL_GetAudioStreamInputChannelMap (SDL_AudioStream *stream, int *count)
 
int * SDL_GetAudioStreamOutputChannelMap (SDL_AudioStream *stream, int *count)
 
bool SDL_SetAudioStreamInputChannelMap (SDL_AudioStream *stream, const int *chmap, int count)
 
bool SDL_SetAudioStreamOutputChannelMap (SDL_AudioStream *stream, const int *chmap, int count)
 
bool SDL_PutAudioStreamData (SDL_AudioStream *stream, const void *buf, int len)
 
bool SDL_PutAudioStreamDataNoCopy (SDL_AudioStream *stream, const void *buf, int len, SDL_AudioStreamDataCompleteCallback callback, void *userdata)
 
bool SDL_PutAudioStreamPlanarData (SDL_AudioStream *stream, const void *const *channel_buffers, int num_channels, int num_samples)
 
int SDL_GetAudioStreamData (SDL_AudioStream *stream, void *buf, int len)
 
int SDL_GetAudioStreamAvailable (SDL_AudioStream *stream)
 
int SDL_GetAudioStreamQueued (SDL_AudioStream *stream)
 
bool SDL_FlushAudioStream (SDL_AudioStream *stream)
 
bool SDL_ClearAudioStream (SDL_AudioStream *stream)
 
bool SDL_PauseAudioStreamDevice (SDL_AudioStream *stream)
 
bool SDL_ResumeAudioStreamDevice (SDL_AudioStream *stream)
 
bool SDL_AudioStreamDevicePaused (SDL_AudioStream *stream)
 
bool SDL_LockAudioStream (SDL_AudioStream *stream)
 
bool SDL_UnlockAudioStream (SDL_AudioStream *stream)
 
bool SDL_SetAudioStreamGetCallback (SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata)
 
bool SDL_SetAudioStreamPutCallback (SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata)
 
void SDL_DestroyAudioStream (SDL_AudioStream *stream)
 
SDL_AudioStreamSDL_OpenAudioDeviceStream (SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata)
 
bool SDL_SetAudioPostmixCallback (SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata)
 
bool SDL_LoadWAV_IO (SDL_IOStream *src, bool closeio, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
 
bool SDL_LoadWAV (const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
 
bool SDL_MixAudio (Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, float volume)
 
bool SDL_ConvertAudioSamples (const SDL_AudioSpec *src_spec, const Uint8 *src_data, int src_len, const SDL_AudioSpec *dst_spec, Uint8 **dst_data, int *dst_len)
 
const char * SDL_GetAudioFormatName (SDL_AudioFormat format)
 
int SDL_GetSilenceValueForFormat (SDL_AudioFormat format)
 

Macro Definition Documentation

◆ SDL_AUDIO_BITSIZE

#define SDL_AUDIO_BITSIZE (   x)    ((x) & SDL_AUDIO_MASK_BITSIZE)

Retrieve the size, in bits, from an SDL_AudioFormat.

For example, SDL_AUDIO_BITSIZE(SDL_AUDIO_S16) returns 16.

Parameters
xan SDL_AudioFormat value.
Returns
data size in bits.

\threadsafety It is safe to call this macro from any thread.

Since
This macro is available since SDL 3.2.0.

Definition at line 266 of file SDL_audio.h.

◆ SDL_AUDIO_BYTESIZE

#define SDL_AUDIO_BYTESIZE (   x)    (SDL_AUDIO_BITSIZE(x) / 8)

Retrieve the size, in bytes, from an SDL_AudioFormat.

For example, SDL_AUDIO_BYTESIZE(SDL_AUDIO_S16) returns 2.

Parameters
xan SDL_AudioFormat value.
Returns
data size in bytes.

\threadsafety It is safe to call this macro from any thread.

Since
This macro is available since SDL 3.2.0.

Definition at line 280 of file SDL_audio.h.

◆ SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK

#define SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK   ((SDL_AudioDeviceID) 0xFFFFFFFFu)

A value used to request a default playback audio device.

Several functions that require an SDL_AudioDeviceID will accept this value to signify the app just wants the system to choose a default device instead of the app providing a specific one.

Since
This macro is available since SDL 3.2.0.

Definition at line 385 of file SDL_audio.h.

◆ SDL_AUDIO_DEVICE_DEFAULT_RECORDING

#define SDL_AUDIO_DEVICE_DEFAULT_RECORDING   ((SDL_AudioDeviceID) 0xFFFFFFFEu)

A value used to request a default recording audio device.

Several functions that require an SDL_AudioDeviceID will accept this value to signify the app just wants the system to choose a default device instead of the app providing a specific one.

Since
This macro is available since SDL 3.2.0.

Definition at line 396 of file SDL_audio.h.

◆ SDL_AUDIO_FRAMESIZE

#define SDL_AUDIO_FRAMESIZE (   x)    (SDL_AUDIO_BYTESIZE((x).format) * (x).channels)

Calculate the size of each audio frame (in bytes) from an SDL_AudioSpec.

This reports on the size of an audio sample frame: stereo Sint16 data (2 channels of 2 bytes each) would be 4 bytes per frame, for example.

Parameters
xan SDL_AudioSpec to query.
Returns
the number of bytes used per sample frame.

\threadsafety It is safe to call this macro from any thread.

Since
This macro is available since SDL 3.2.0.

Definition at line 425 of file SDL_audio.h.

◆ SDL_AUDIO_ISBIGENDIAN

#define SDL_AUDIO_ISBIGENDIAN (   x)    ((x) & SDL_AUDIO_MASK_BIG_ENDIAN)

Determine if an SDL_AudioFormat represents bigendian data.

For example, SDL_AUDIO_ISBIGENDIAN(SDL_AUDIO_S16LE) returns 0.

Parameters
xan SDL_AudioFormat value.
Returns
non-zero if format is bigendian, zero otherwise.

\threadsafety It is safe to call this macro from any thread.

Since
This macro is available since SDL 3.2.0.

Definition at line 308 of file SDL_audio.h.

◆ SDL_AUDIO_ISFLOAT

#define SDL_AUDIO_ISFLOAT (   x)    ((x) & SDL_AUDIO_MASK_FLOAT)

Determine if an SDL_AudioFormat represents floating point data.

For example, SDL_AUDIO_ISFLOAT(SDL_AUDIO_S16) returns 0.

Parameters
xan SDL_AudioFormat value.
Returns
non-zero if format is floating point, zero otherwise.

\threadsafety It is safe to call this macro from any thread.

Since
This macro is available since SDL 3.2.0.

Definition at line 294 of file SDL_audio.h.

◆ SDL_AUDIO_ISINT

#define SDL_AUDIO_ISINT (   x)    (!SDL_AUDIO_ISFLOAT(x))

Determine if an SDL_AudioFormat represents integer data.

For example, SDL_AUDIO_ISINT(SDL_AUDIO_F32) returns 0.

Parameters
xan SDL_AudioFormat value.
Returns
non-zero if format is integer, zero otherwise.

\threadsafety It is safe to call this macro from any thread.

Since
This macro is available since SDL 3.2.0.

Definition at line 350 of file SDL_audio.h.

◆ SDL_AUDIO_ISLITTLEENDIAN

#define SDL_AUDIO_ISLITTLEENDIAN (   x)    (!SDL_AUDIO_ISBIGENDIAN(x))

Determine if an SDL_AudioFormat represents littleendian data.

For example, SDL_AUDIO_ISLITTLEENDIAN(SDL_AUDIO_S16BE) returns 0.

Parameters
xan SDL_AudioFormat value.
Returns
non-zero if format is littleendian, zero otherwise.

\threadsafety It is safe to call this macro from any thread.

Since
This macro is available since SDL 3.2.0.

Definition at line 322 of file SDL_audio.h.

◆ SDL_AUDIO_ISSIGNED

#define SDL_AUDIO_ISSIGNED (   x)    ((x) & SDL_AUDIO_MASK_SIGNED)

Determine if an SDL_AudioFormat represents signed data.

For example, SDL_AUDIO_ISSIGNED(SDL_AUDIO_U8) returns 0.

Parameters
xan SDL_AudioFormat value.
Returns
non-zero if format is signed, zero otherwise.

\threadsafety It is safe to call this macro from any thread.

Since
This macro is available since SDL 3.2.0.

Definition at line 336 of file SDL_audio.h.

◆ SDL_AUDIO_ISUNSIGNED

#define SDL_AUDIO_ISUNSIGNED (   x)    (!SDL_AUDIO_ISSIGNED(x))

Determine if an SDL_AudioFormat represents unsigned data.

For example, SDL_AUDIO_ISUNSIGNED(SDL_AUDIO_S16) returns 0.

Parameters
xan SDL_AudioFormat value.
Returns
non-zero if format is unsigned, zero otherwise.

\threadsafety It is safe to call this macro from any thread.

Since
This macro is available since SDL 3.2.0.

Definition at line 364 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_BIG_ENDIAN

#define SDL_AUDIO_MASK_BIG_ENDIAN   (1u<<12)

Mask of bits in an SDL_AudioFormat that contain the bigendian flag.

Generally one should use SDL_AUDIO_ISBIGENDIAN or SDL_AUDIO_ISLITTLEENDIAN instead of this macro directly.

Since
This macro is available since SDL 3.2.0.

Definition at line 170 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_BITSIZE

#define SDL_AUDIO_MASK_BITSIZE   (0xFFu)

CategoryAudio

Audio functionality for the SDL library.

All audio in SDL3 revolves around SDL_AudioStream. Whether you want to play or record audio, convert it, stream it, buffer it, or mix it, you're going to be passing it through an audio stream.

Audio streams are quite flexible; they can accept any amount of data at a time, in any supported format, and output it as needed in any other format, even if the data format changes on either side halfway through.

An app opens an audio device and binds any number of audio streams to it, feeding more data to the streams as available. When the device needs more data, it will pull it from all bound streams and mix them together for playback.

Audio streams can also use an app-provided callback to supply data on-demand, which maps pretty closely to the SDL2 audio model.

SDL also provides a simple .WAV loader in SDL_LoadWAV (and SDL_LoadWAV_IO if you aren't reading from a file) as a basic means to load sound data into your program.

Logical audio devices

In SDL3, opening a physical device (like a SoundBlaster 16 Pro) gives you a logical device ID that you can bind audio streams to. In almost all cases, logical devices can be used anywhere in the API that a physical device is normally used. However, since each device opening generates a new logical device, different parts of the program (say, a VoIP library, or text-to-speech framework, or maybe some other sort of mixer on top of SDL) can have their own device opens that do not interfere with each other; each logical device will mix its separate audio down to a single buffer, fed to the physical device, behind the scenes. As many logical devices as you like can come and go; SDL will only have to open the physical device at the OS level once, and will manage all the logical devices on top of it internally.

One other benefit of logical devices: if you don't open a specific physical device, instead opting for the default, SDL can automatically migrate those logical devices to different hardware as circumstances change: a user plugged in headphones? The system default changed? SDL can transparently migrate the logical devices to the correct physical device seamlessly and keep playing; the app doesn't even have to know it happened if it doesn't want to.

Simplified audio

As a simplified model for when a single source of audio is all that's needed, an app can use SDL_OpenAudioDeviceStream, which is a single function to open an audio device, create an audio stream, bind that stream to the newly-opened device, and (optionally) provide a callback for obtaining audio data. When using this function, the primary interface is the SDL_AudioStream and the device handle is mostly hidden away; destroying a stream created through this function will also close the device, stream bindings cannot be changed, etc. One other quirk of this is that the device is started in a paused state and must be explicitly resumed; this is partially to offer a clean migration for SDL2 apps and partially because the app might have to do more setup before playback begins; in the non-simplified form, nothing will play until a stream is bound to a device, so they start unpaused.

Channel layouts

Audio data passing through SDL is uncompressed PCM data, interleaved. One can provide their own decompression through an MP3, etc, decoder, but SDL does not provide this directly. Each interleaved channel of data is meant to be in a specific order.

Abbreviations:

  • FRONT = single mono speaker
  • FL = front left speaker
  • FR = front right speaker
  • FC = front center speaker
  • BL = back left speaker
  • BR = back right speaker
  • SR = surround right speaker
  • SL = surround left speaker
  • BC = back center speaker
  • LFE = low-frequency speaker

These are listed in the order they are laid out in memory, so "FL, FR" means "the front left speaker is laid out in memory first, then the front right, then it repeats for the next audio frame".

  • 1 channel (mono) layout: FRONT
  • 2 channels (stereo) layout: FL, FR
  • 3 channels (2.1) layout: FL, FR, LFE
  • 4 channels (quad) layout: FL, FR, BL, BR
  • 5 channels (4.1) layout: FL, FR, LFE, BL, BR
  • 6 channels (5.1) layout: FL, FR, FC, LFE, BL, BR (last two can also be SL, SR)
  • 7 channels (6.1) layout: FL, FR, FC, LFE, BC, SL, SR
  • 8 channels (7.1) layout: FL, FR, FC, LFE, BL, BR, SL, SR

This is the same order as DirectSound expects, but applied to all platforms; SDL will swizzle the channels as necessary if a platform expects something different.

SDL_AudioStream can also be provided channel maps to change this ordering to whatever is necessary, in other audio processing scenarios. Mask of bits in an SDL_AudioFormat that contains the format bit size.

Generally one should use SDL_AUDIO_BITSIZE instead of this macro directly.

Since
This macro is available since SDL 3.2.0.

Definition at line 151 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_FLOAT

#define SDL_AUDIO_MASK_FLOAT   (1u<<8)

Mask of bits in an SDL_AudioFormat that contain the floating point flag.

Generally one should use SDL_AUDIO_ISFLOAT instead of this macro directly.

Since
This macro is available since SDL 3.2.0.

Definition at line 160 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_SIGNED

#define SDL_AUDIO_MASK_SIGNED   (1u<<15)

Mask of bits in an SDL_AudioFormat that contain the signed data flag.

Generally one should use SDL_AUDIO_ISSIGNED instead of this macro directly.

Since
This macro is available since SDL 3.2.0.

Definition at line 179 of file SDL_audio.h.

◆ SDL_DEFINE_AUDIO_FORMAT

#define SDL_DEFINE_AUDIO_FORMAT (   signed,
  bigendian,
  flt,
  size 
)     (((Uint16)(signed) << 15) | ((Uint16)(bigendian) << 12) | ((Uint16)(flt) << 8) | ((size) & SDL_AUDIO_MASK_BITSIZE))

Define an SDL_AudioFormat value.

SDL does not support custom audio formats, so this macro is not of much use externally, but it can be illustrative as to what the various bits of an SDL_AudioFormat mean.

For example, SDL_AUDIO_S32LE looks like this:

#define SDL_DEFINE_AUDIO_FORMAT(signed, bigendian, flt, size)
Definition SDL_audio.h:204
Parameters
signed1 for signed data, 0 for unsigned data.
bigendian1 for bigendian data, 0 for littleendian data.
flt1 for floating point data, 0 for integer data.
sizenumber of bits per sample.
Returns
a format value in the style of SDL_AudioFormat.

\threadsafety It is safe to call this macro from any thread.

Since
This macro is available since SDL 3.2.0.

Definition at line 204 of file SDL_audio.h.

221{
222 SDL_AUDIO_UNKNOWN = 0x0000u, /**< Unspecified audio format */
223 SDL_AUDIO_U8 = 0x0008u, /**< Unsigned 8-bit samples */
224 /* SDL_DEFINE_AUDIO_FORMAT(0, 0, 0, 8), */
225 SDL_AUDIO_S8 = 0x8008u, /**< Signed 8-bit samples */
226 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 8), */
227 SDL_AUDIO_S16LE = 0x8010u, /**< Signed 16-bit samples */
228 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 16), */
229 SDL_AUDIO_S16BE = 0x9010u, /**< As above, but big-endian byte order */
230 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 16), */
231 SDL_AUDIO_S32LE = 0x8020u, /**< 32-bit integer samples */
232 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 32), */
233 SDL_AUDIO_S32BE = 0x9020u, /**< As above, but big-endian byte order */
234 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 32), */
235 SDL_AUDIO_F32LE = 0x8120u, /**< 32-bit floating point samples */
236 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 1, 32), */
237 SDL_AUDIO_F32BE = 0x9120u, /**< As above, but big-endian byte order */
238 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 1, 32), */
239
240 /* These represent the current system's byteorder. */
241 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
245 #else
249 #endif
251
252
253/**
254 * Retrieve the size, in bits, from an SDL_AudioFormat.
255 *
256 * For example, `SDL_AUDIO_BITSIZE(SDL_AUDIO_S16)` returns 16.
257 *
258 * \param x an SDL_AudioFormat value.
259 * \returns data size in bits.
260 *
261 * \threadsafety It is safe to call this macro from any thread.
262 *
263 * \since This macro is available since SDL 3.2.0.
264 */
265#define SDL_AUDIO_BITSIZE(x) ((x) & SDL_AUDIO_MASK_BITSIZE)
266
267/**
268 * Retrieve the size, in bytes, from an SDL_AudioFormat.
269 *
270 * For example, `SDL_AUDIO_BYTESIZE(SDL_AUDIO_S16)` returns 2.
271 *
272 * \param x an SDL_AudioFormat value.
273 * \returns data size in bytes.
274 *
275 * \threadsafety It is safe to call this macro from any thread.
276 *
277 * \since This macro is available since SDL 3.2.0.
278 */
279#define SDL_AUDIO_BYTESIZE(x) (SDL_AUDIO_BITSIZE(x) / 8)
280
281/**
282 * Determine if an SDL_AudioFormat represents floating point data.
283 *
284 * For example, `SDL_AUDIO_ISFLOAT(SDL_AUDIO_S16)` returns 0.
285 *
286 * \param x an SDL_AudioFormat value.
287 * \returns non-zero if format is floating point, zero otherwise.
288 *
289 * \threadsafety It is safe to call this macro from any thread.
290 *
291 * \since This macro is available since SDL 3.2.0.
292 */
293#define SDL_AUDIO_ISFLOAT(x) ((x) & SDL_AUDIO_MASK_FLOAT)
294
295/**
296 * Determine if an SDL_AudioFormat represents bigendian data.
297 *
298 * For example, `SDL_AUDIO_ISBIGENDIAN(SDL_AUDIO_S16LE)` returns 0.
299 *
300 * \param x an SDL_AudioFormat value.
301 * \returns non-zero if format is bigendian, zero otherwise.
302 *
303 * \threadsafety It is safe to call this macro from any thread.
304 *
305 * \since This macro is available since SDL 3.2.0.
306 */
307#define SDL_AUDIO_ISBIGENDIAN(x) ((x) & SDL_AUDIO_MASK_BIG_ENDIAN)
308
309/**
310 * Determine if an SDL_AudioFormat represents littleendian data.
311 *
312 * For example, `SDL_AUDIO_ISLITTLEENDIAN(SDL_AUDIO_S16BE)` returns 0.
313 *
314 * \param x an SDL_AudioFormat value.
315 * \returns non-zero if format is littleendian, zero otherwise.
316 *
317 * \threadsafety It is safe to call this macro from any thread.
318 *
319 * \since This macro is available since SDL 3.2.0.
320 */
321#define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))
322
323/**
324 * Determine if an SDL_AudioFormat represents signed data.
325 *
326 * For example, `SDL_AUDIO_ISSIGNED(SDL_AUDIO_U8)` returns 0.
327 *
328 * \param x an SDL_AudioFormat value.
329 * \returns non-zero if format is signed, zero otherwise.
330 *
331 * \threadsafety It is safe to call this macro from any thread.
332 *
333 * \since This macro is available since SDL 3.2.0.
334 */
335#define SDL_AUDIO_ISSIGNED(x) ((x) & SDL_AUDIO_MASK_SIGNED)
336
337/**
338 * Determine if an SDL_AudioFormat represents integer data.
339 *
340 * For example, `SDL_AUDIO_ISINT(SDL_AUDIO_F32)` returns 0.
341 *
342 * \param x an SDL_AudioFormat value.
343 * \returns non-zero if format is integer, zero otherwise.
344 *
345 * \threadsafety It is safe to call this macro from any thread.
346 *
347 * \since This macro is available since SDL 3.2.0.
348 */
349#define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))
350
351/**
352 * Determine if an SDL_AudioFormat represents unsigned data.
353 *
354 * For example, `SDL_AUDIO_ISUNSIGNED(SDL_AUDIO_S16)` returns 0.
355 *
356 * \param x an SDL_AudioFormat value.
357 * \returns non-zero if format is unsigned, zero otherwise.
358 *
359 * \threadsafety It is safe to call this macro from any thread.
360 *
361 * \since This macro is available since SDL 3.2.0.
362 */
363#define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))
364
365
366/**
367 * SDL Audio Device instance IDs.
368 *
369 * Zero is used to signify an invalid/null device.
370 *
371 * \since This datatype is available since SDL 3.2.0.
372 */
374
375/**
376 * A value used to request a default playback audio device.
377 *
378 * Several functions that require an SDL_AudioDeviceID will accept this value
379 * to signify the app just wants the system to choose a default device instead
380 * of the app providing a specific one.
381 *
382 * \since This macro is available since SDL 3.2.0.
383 */
384#define SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK ((SDL_AudioDeviceID) 0xFFFFFFFFu)
385
386/**
387 * A value used to request a default recording audio device.
388 *
389 * Several functions that require an SDL_AudioDeviceID will accept this value
390 * to signify the app just wants the system to choose a default device instead
391 * of the app providing a specific one.
392 *
393 * \since This macro is available since SDL 3.2.0.
394 */
395#define SDL_AUDIO_DEVICE_DEFAULT_RECORDING ((SDL_AudioDeviceID) 0xFFFFFFFEu)
396
397/**
398 * Format specifier for audio data.
399 *
400 * \since This struct is available since SDL 3.2.0.
401 *
402 * \sa SDL_AudioFormat
403 */
404typedef struct SDL_AudioSpec
405{
406 SDL_AudioFormat format; /**< Audio data format */
407 int channels; /**< Number of channels: 1 mono, 2 stereo, etc */
408 int freq; /**< sample rate: sample frames per second */
410
411/**
412 * Calculate the size of each audio frame (in bytes) from an SDL_AudioSpec.
413 *
414 * This reports on the size of an audio sample frame: stereo Sint16 data (2
415 * channels of 2 bytes each) would be 4 bytes per frame, for example.
416 *
417 * \param x an SDL_AudioSpec to query.
418 * \returns the number of bytes used per sample frame.
419 *
420 * \threadsafety It is safe to call this macro from any thread.
421 *
422 * \since This macro is available since SDL 3.2.0.
423 */
424#define SDL_AUDIO_FRAMESIZE(x) (SDL_AUDIO_BYTESIZE((x).format) * (x).channels)
425
426/**
427 * The opaque handle that represents an audio stream.
428 *
429 * SDL_AudioStream is an audio conversion interface.
430 *
431 * - It can handle resampling data in chunks without generating artifacts,
432 * when it doesn't have the complete buffer available.
433 * - It can handle incoming data in any variable size.
434 * - It can handle input/output format changes on the fly.
435 * - It can remap audio channels between inputs and outputs.
436 * - You push data as you have it, and pull it when you need it; the
437 * stream will buffer data as needed.
438 * - It can also function as a basic audio data queue even if you just have
439 * sound that needs to pass from one place to another.
440 * - You can hook callbacks up to them when more data is added or requested,
441 * to manage data on-the-fly.
442 *
443 * Audio streams are the core of the SDL3 audio interface. You create one or
444 * more of them, bind them to an opened audio device, and feed data to them
445 * (or for recording, consume data from them).
446 *
447 * \since This struct is available since SDL 3.2.0.
448 *
449 * \sa SDL_CreateAudioStream
450 */
451typedef struct SDL_AudioStream SDL_AudioStream;
452
453
454/* Function prototypes */
455
456/**
457 * Use this function to get the number of built-in audio drivers.
458 *
459 * This function returns a hardcoded number. This never returns a negative
460 * value; if there are no drivers compiled into this build of SDL, this
461 * function returns zero. The presence of a driver in this list does not mean
462 * it will function, it just means SDL is capable of interacting with that
463 * interface. For example, a build of SDL might have esound support, but if
464 * there's no esound server available, SDL's esound driver would fail if used.
465 *
466 * By default, SDL tries all drivers, in its preferred order, until one is
467 * found to be usable.
468 *
469 * \returns the number of built-in audio drivers.
470 *
471 * \threadsafety It is safe to call this function from any thread.
472 *
473 * \since This function is available since SDL 3.2.0.
474 *
475 * \sa SDL_GetAudioDriver
476 */
477extern SDL_DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
478
479/**
480 * Use this function to get the name of a built in audio driver.
481 *
482 * The list of audio drivers is given in the order that they are normally
483 * initialized by default; the drivers that seem more reasonable to choose
484 * first (as far as the SDL developers believe) are earlier in the list.
485 *
486 * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
487 * "coreaudio" or "wasapi". These never have Unicode characters, and are not
488 * meant to be proper names.
489 *
490 * \param index the index of the audio driver; the value ranges from 0 to
491 * SDL_GetNumAudioDrivers() - 1.
492 * \returns the name of the audio driver at the requested index, or NULL if an
493 * invalid index was specified.
494 *
495 * \threadsafety It is safe to call this function from any thread.
496 *
497 * \since This function is available since SDL 3.2.0.
498 *
499 * \sa SDL_GetNumAudioDrivers
500 */
501extern SDL_DECLSPEC const char * SDLCALL SDL_GetAudioDriver(int index);
502
503/**
504 * Get the name of the current audio driver.
505 *
506 * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
507 * "coreaudio" or "wasapi". These never have Unicode characters, and are not
508 * meant to be proper names.
509 *
510 * \returns the name of the current audio driver or NULL if no driver has been
511 * initialized.
512 *
513 * \threadsafety It is safe to call this function from any thread.
514 *
515 * \since This function is available since SDL 3.2.0.
516 */
517extern SDL_DECLSPEC const char * SDLCALL SDL_GetCurrentAudioDriver(void);
518
519/**
520 * Get a list of currently-connected audio playback devices.
521 *
522 * This returns of list of available devices that play sound, perhaps to
523 * speakers or headphones ("playback" devices). If you want devices that
524 * record audio, like a microphone ("recording" devices), use
525 * SDL_GetAudioRecordingDevices() instead.
526 *
527 * This only returns a list of physical devices; it will not have any device
528 * IDs returned by SDL_OpenAudioDevice().
529 *
530 * If this function returns NULL, to signify an error, `*count` will be set to
531 * zero.
532 *
533 * \param count a pointer filled in with the number of devices returned, may
534 * be NULL.
535 * \returns a 0 terminated array of device instance IDs or NULL on error; call
536 * SDL_GetError() for more information. This should be freed with
537 * SDL_free() when it is no longer needed.
538 *
539 * \threadsafety It is safe to call this function from any thread.
540 *
541 * \since This function is available since SDL 3.2.0.
542 *
543 * \sa SDL_OpenAudioDevice
544 * \sa SDL_GetAudioRecordingDevices
545 */
546extern SDL_DECLSPEC SDL_AudioDeviceID * SDLCALL SDL_GetAudioPlaybackDevices(int *count);
547
548/**
549 * Get a list of currently-connected audio recording devices.
550 *
551 * This returns of list of available devices that record audio, like a
552 * microphone ("recording" devices). If you want devices that play sound,
553 * perhaps to speakers or headphones ("playback" devices), use
554 * SDL_GetAudioPlaybackDevices() instead.
555 *
556 * This only returns a list of physical devices; it will not have any device
557 * IDs returned by SDL_OpenAudioDevice().
558 *
559 * If this function returns NULL, to signify an error, `*count` will be set to
560 * zero.
561 *
562 * \param count a pointer filled in with the number of devices returned, may
563 * be NULL.
564 * \returns a 0 terminated array of device instance IDs, or NULL on failure;
565 * call SDL_GetError() for more information. This should be freed
566 * with SDL_free() when it is no longer needed.
567 *
568 * \threadsafety It is safe to call this function from any thread.
569 *
570 * \since This function is available since SDL 3.2.0.
571 *
572 * \sa SDL_OpenAudioDevice
573 * \sa SDL_GetAudioPlaybackDevices
574 */
575extern SDL_DECLSPEC SDL_AudioDeviceID * SDLCALL SDL_GetAudioRecordingDevices(int *count);
576
577/**
578 * Get the human-readable name of a specific audio device.
579 *
580 * **WARNING**: this function will work with SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK
581 * and SDL_AUDIO_DEVICE_DEFAULT_RECORDING, returning the current default
582 * physical devices' names. However, as the default device may change at any
583 * time, it is likely better to show a generic name to the user, like "System
584 * default audio device" or perhaps "default [currently %s]". Do not store
585 * this name to disk to reidentify the device in a later run of the program,
586 * as the default might change in general, and the string will be the name of
587 * a specific device and not the abstract system default.
588 *
589 * \param devid the instance ID of the device to query.
590 * \returns the name of the audio device, or NULL on failure; call
591 * SDL_GetError() for more information.
592 *
593 * \threadsafety It is safe to call this function from any thread.
594 *
595 * \since This function is available since SDL 3.2.0.
596 *
597 * \sa SDL_GetAudioPlaybackDevices
598 * \sa SDL_GetAudioRecordingDevices
599 */
600extern SDL_DECLSPEC const char * SDLCALL SDL_GetAudioDeviceName(SDL_AudioDeviceID devid);
601
602/**
603 * Get the current audio format of a specific audio device.
604 *
605 * For an opened device, this will report the format the device is currently
606 * using. If the device isn't yet opened, this will report the device's
607 * preferred format (or a reasonable default if this can't be determined).
608 *
609 * You may also specify SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or
610 * SDL_AUDIO_DEVICE_DEFAULT_RECORDING here, which is useful for getting a
611 * reasonable recommendation before opening the system-recommended default
612 * device.
613 *
614 * You can also use this to request the current device buffer size. This is
615 * specified in sample frames and represents the amount of data SDL will feed
616 * to the physical hardware in each chunk. This can be converted to
617 * milliseconds of audio with the following equation:
618 *
619 * `ms = (int) ((((Sint64) frames) * 1000) / spec.freq);`
620 *
621 * Buffer size is only important if you need low-level control over the audio
622 * playback timing. Most apps do not need this.
623 *
624 * \param devid the instance ID of the device to query.
625 * \param spec on return, will be filled with device details.
626 * \param sample_frames pointer to store device buffer size, in sample frames.
627 * Can be NULL.
628 * \returns true on success or false on failure; call SDL_GetError() for more
629 * information.
630 *
631 * \threadsafety It is safe to call this function from any thread.
632 *
633 * \since This function is available since SDL 3.2.0.
634 */
635extern SDL_DECLSPEC bool SDLCALL SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec, int *sample_frames);
636
637/**
638 * Get the current channel map of an audio device.
639 *
640 * Channel maps are optional; most things do not need them, instead passing
641 * data in the [order that SDL expects](CategoryAudio#channel-layouts).
642 *
643 * Audio devices usually have no remapping applied. This is represented by
644 * returning NULL, and does not signify an error.
645 *
646 * \param devid the instance ID of the device to query.
647 * \param count On output, set to number of channels in the map. Can be NULL.
648 * \returns an array of the current channel mapping, with as many elements as
649 * the current output spec's channels, or NULL if default. This
650 * should be freed with SDL_free() when it is no longer needed.
651 *
652 * \threadsafety It is safe to call this function from any thread.
653 *
654 * \since This function is available since SDL 3.2.0.
655 *
656 * \sa SDL_SetAudioStreamInputChannelMap
657 */
658extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioDeviceChannelMap(SDL_AudioDeviceID devid, int *count);
659
660/**
661 * Open a specific audio device.
662 *
663 * You can open both playback and recording devices through this function.
664 * Playback devices will take data from bound audio streams, mix it, and send
665 * it to the hardware. Recording devices will feed any bound audio streams
666 * with a copy of any incoming data.
667 *
668 * An opened audio device starts out with no audio streams bound. To start
669 * audio playing, bind a stream and supply audio data to it. Unlike SDL2,
670 * there is no audio callback; you only bind audio streams and make sure they
671 * have data flowing into them (however, you can simulate SDL2's semantics
672 * fairly closely by using SDL_OpenAudioDeviceStream instead of this
673 * function).
674 *
675 * If you don't care about opening a specific device, pass a `devid` of either
676 * `SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK` or
677 * `SDL_AUDIO_DEVICE_DEFAULT_RECORDING`. In this case, SDL will try to pick
678 * the most reasonable default, and may also switch between physical devices
679 * seamlessly later, if the most reasonable default changes during the
680 * lifetime of this opened device (user changed the default in the OS's system
681 * preferences, the default got unplugged so the system jumped to a new
682 * default, the user plugged in headphones on a mobile device, etc). Unless
683 * you have a good reason to choose a specific device, this is probably what
684 * you want.
685 *
686 * You may request a specific format for the audio device, but there is no
687 * promise the device will honor that request for several reasons. As such,
688 * it's only meant to be a hint as to what data your app will provide. Audio
689 * streams will accept data in whatever format you specify and manage
690 * conversion for you as appropriate. SDL_GetAudioDeviceFormat can tell you
691 * the preferred format for the device before opening and the actual format
692 * the device is using after opening.
693 *
694 * It's legal to open the same device ID more than once; each successful open
695 * will generate a new logical SDL_AudioDeviceID that is managed separately
696 * from others on the same physical device. This allows libraries to open a
697 * device separately from the main app and bind its own streams without
698 * conflicting.
699 *
700 * It is also legal to open a device ID returned by a previous call to this
701 * function; doing so just creates another logical device on the same physical
702 * device. This may be useful for making logical groupings of audio streams.
703 *
704 * This function returns the opened device ID on success. This is a new,
705 * unique SDL_AudioDeviceID that represents a logical device.
706 *
707 * Some backends might offer arbitrary devices (for example, a networked audio
708 * protocol that can connect to an arbitrary server). For these, as a change
709 * from SDL2, you should open a default device ID and use an SDL hint to
710 * specify the target if you care, or otherwise let the backend figure out a
711 * reasonable default. Most backends don't offer anything like this, and often
712 * this would be an end user setting an environment variable for their custom
713 * need, and not something an application should specifically manage.
714 *
715 * When done with an audio device, possibly at the end of the app's life, one
716 * should call SDL_CloseAudioDevice() on the returned device id.
717 *
718 * \param devid the device instance id to open, or
719 * SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or
720 * SDL_AUDIO_DEVICE_DEFAULT_RECORDING for the most reasonable
721 * default device.
722 * \param spec the requested device configuration. Can be NULL to use
723 * reasonable defaults.
724 * \returns the device ID on success or 0 on failure; call SDL_GetError() for
725 * more information.
726 *
727 * \threadsafety It is safe to call this function from any thread.
728 *
729 * \since This function is available since SDL 3.2.0.
730 *
731 * \sa SDL_CloseAudioDevice
732 * \sa SDL_GetAudioDeviceFormat
733 */
734extern SDL_DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec);
735
736/**
737 * Determine if an audio device is physical (instead of logical).
738 *
739 * An SDL_AudioDeviceID that represents physical hardware is a physical
740 * device; there is one for each piece of hardware that SDL can see. Logical
741 * devices are created by calling SDL_OpenAudioDevice or
742 * SDL_OpenAudioDeviceStream, and while each is associated with a physical
743 * device, there can be any number of logical devices on one physical device.
744 *
745 * For the most part, logical and physical IDs are interchangeable--if you try
746 * to open a logical device, SDL understands to assign that effort to the
747 * underlying physical device, etc. However, it might be useful to know if an
748 * arbitrary device ID is physical or logical. This function reports which.
749 *
750 * This function may return either true or false for invalid device IDs.
751 *
752 * \param devid the device ID to query.
753 * \returns true if devid is a physical device, false if it is logical.
754 *
755 * \threadsafety It is safe to call this function from any thread.
756 *
757 * \since This function is available since SDL 3.2.0.
758 */
759extern SDL_DECLSPEC bool SDLCALL SDL_IsAudioDevicePhysical(SDL_AudioDeviceID devid);
760
761/**
762 * Determine if an audio device is a playback device (instead of recording).
763 *
764 * This function may return either true or false for invalid device IDs.
765 *
766 * \param devid the device ID to query.
767 * \returns true if devid is a playback device, false if it is recording.
768 *
769 * \threadsafety It is safe to call this function from any thread.
770 *
771 * \since This function is available since SDL 3.2.0.
772 */
773extern SDL_DECLSPEC bool SDLCALL SDL_IsAudioDevicePlayback(SDL_AudioDeviceID devid);
774
775/**
776 * Use this function to pause audio playback on a specified device.
777 *
778 * This function pauses audio processing for a given device. Any bound audio
779 * streams will not progress, and no audio will be generated. Pausing one
780 * device does not prevent other unpaused devices from running.
781 *
782 * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
783 * has to bind a stream before any audio will flow. Pausing a paused device is
784 * a legal no-op.
785 *
786 * Pausing a device can be useful to halt all audio without unbinding all the
787 * audio streams. This might be useful while a game is paused, or a level is
788 * loading, etc.
789 *
790 * Physical devices can not be paused or unpaused, only logical devices
791 * created through SDL_OpenAudioDevice() can be.
792 *
793 * \param devid a device opened by SDL_OpenAudioDevice().
794 * \returns true on success or false on failure; call SDL_GetError() for more
795 * information.
796 *
797 * \threadsafety It is safe to call this function from any thread.
798 *
799 * \since This function is available since SDL 3.2.0.
800 *
801 * \sa SDL_ResumeAudioDevice
802 * \sa SDL_AudioDevicePaused
803 */
804extern SDL_DECLSPEC bool SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID devid);
805
806/**
807 * Use this function to unpause audio playback on a specified device.
808 *
809 * This function unpauses audio processing for a given device that has
810 * previously been paused with SDL_PauseAudioDevice(). Once unpaused, any
811 * bound audio streams will begin to progress again, and audio can be
812 * generated.
813 *
814 * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
815 * has to bind a stream before any audio will flow. Unpausing an unpaused
816 * device is a legal no-op.
817 *
818 * Physical devices can not be paused or unpaused, only logical devices
819 * created through SDL_OpenAudioDevice() can be.
820 *
821 * \param devid a device opened by SDL_OpenAudioDevice().
822 * \returns true on success or false on failure; call SDL_GetError() for more
823 * information.
824 *
825 * \threadsafety It is safe to call this function from any thread.
826 *
827 * \since This function is available since SDL 3.2.0.
828 *
829 * \sa SDL_AudioDevicePaused
830 * \sa SDL_PauseAudioDevice
831 */
832extern SDL_DECLSPEC bool SDLCALL SDL_ResumeAudioDevice(SDL_AudioDeviceID devid);
833
834/**
835 * Use this function to query if an audio device is paused.
836 *
837 * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
838 * has to bind a stream before any audio will flow.
839 *
840 * Physical devices can not be paused or unpaused, only logical devices
841 * created through SDL_OpenAudioDevice() can be. Physical and invalid device
842 * IDs will report themselves as unpaused here.
843 *
844 * \param devid a device opened by SDL_OpenAudioDevice().
845 * \returns true if device is valid and paused, false otherwise.
846 *
847 * \threadsafety It is safe to call this function from any thread.
848 *
849 * \since This function is available since SDL 3.2.0.
850 *
851 * \sa SDL_PauseAudioDevice
852 * \sa SDL_ResumeAudioDevice
853 */
854extern SDL_DECLSPEC bool SDLCALL SDL_AudioDevicePaused(SDL_AudioDeviceID devid);
855
856/**
857 * Get the gain of an audio device.
858 *
859 * The gain of a device is its volume; a larger gain means a louder output,
860 * with a gain of zero being silence.
861 *
862 * Audio devices default to a gain of 1.0f (no change in output).
863 *
864 * Physical devices may not have their gain changed, only logical devices, and
865 * this function will always return -1.0f when used on physical devices.
866 *
867 * \param devid the audio device to query.
868 * \returns the gain of the device or -1.0f on failure; call SDL_GetError()
869 * for more information.
870 *
871 * \threadsafety It is safe to call this function from any thread.
872 *
873 * \since This function is available since SDL 3.2.0.
874 *
875 * \sa SDL_SetAudioDeviceGain
876 */
877extern SDL_DECLSPEC float SDLCALL SDL_GetAudioDeviceGain(SDL_AudioDeviceID devid);
878
879/**
880 * Change the gain of an audio device.
881 *
882 * The gain of a device is its volume; a larger gain means a louder output,
883 * with a gain of zero being silence.
884 *
885 * Audio devices default to a gain of 1.0f (no change in output).
886 *
887 * Physical devices may not have their gain changed, only logical devices, and
888 * this function will always return false when used on physical devices. While
889 * it might seem attractive to adjust several logical devices at once in this
890 * way, it would allow an app or library to interfere with another portion of
891 * the program's otherwise-isolated devices.
892 *
893 * This is applied, along with any per-audiostream gain, during playback to
894 * the hardware, and can be continuously changed to create various effects. On
895 * recording devices, this will adjust the gain before passing the data into
896 * an audiostream; that recording audiostream can then adjust its gain further
897 * when outputting the data elsewhere, if it likes, but that second gain is
898 * not applied until the data leaves the audiostream again.
899 *
900 * \param devid the audio device on which to change gain.
901 * \param gain the gain. 1.0f is no change, 0.0f is silence.
902 * \returns true on success or false on failure; call SDL_GetError() for more
903 * information.
904 *
905 * \threadsafety It is safe to call this function from any thread, as it holds
906 * a stream-specific mutex while running.
907 *
908 * \since This function is available since SDL 3.2.0.
909 *
910 * \sa SDL_GetAudioDeviceGain
911 */
912extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioDeviceGain(SDL_AudioDeviceID devid, float gain);
913
914/**
915 * Close a previously-opened audio device.
916 *
917 * The application should close open audio devices once they are no longer
918 * needed.
919 *
920 * This function may block briefly while pending audio data is played by the
921 * hardware, so that applications don't drop the last buffer of data they
922 * supplied if terminating immediately afterwards.
923 *
924 * \param devid an audio device id previously returned by
925 * SDL_OpenAudioDevice().
926 *
927 * \threadsafety It is safe to call this function from any thread.
928 *
929 * \since This function is available since SDL 3.2.0.
930 *
931 * \sa SDL_OpenAudioDevice
932 */
933extern SDL_DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID devid);
934
935/**
936 * Bind a list of audio streams to an audio device.
937 *
938 * Audio data will flow through any bound streams. For a playback device, data
939 * for all bound streams will be mixed together and fed to the device. For a
940 * recording device, a copy of recorded data will be provided to each bound
941 * stream.
942 *
943 * Audio streams can only be bound to an open device. This operation is
944 * atomic--all streams bound in the same call will start processing at the
945 * same time, so they can stay in sync. Also: either all streams will be bound
946 * or none of them will be.
947 *
948 * It is an error to bind an already-bound stream; it must be explicitly
949 * unbound first.
950 *
951 * Binding a stream to a device will set its output format for playback
952 * devices, and its input format for recording devices, so they match the
953 * device's settings. The caller is welcome to change the other end of the
954 * stream's format at any time with SDL_SetAudioStreamFormat(). If the other
955 * end of the stream's format has never been set (the audio stream was created
956 * with a NULL audio spec), this function will set it to match the device
957 * end's format.
958 *
959 * \param devid an audio device to bind a stream to.
960 * \param streams an array of audio streams to bind.
961 * \param num_streams number streams listed in the `streams` array.
962 * \returns true on success or false on failure; call SDL_GetError() for more
963 * information.
964 *
965 * \threadsafety It is safe to call this function from any thread.
966 *
967 * \since This function is available since SDL 3.2.0.
968 *
969 * \sa SDL_BindAudioStreams
970 * \sa SDL_UnbindAudioStream
971 * \sa SDL_GetAudioStreamDevice
972 */
973extern SDL_DECLSPEC bool SDLCALL SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream * const *streams, int num_streams);
974
975/**
976 * Bind a single audio stream to an audio device.
977 *
978 * This is a convenience function, equivalent to calling
979 * `SDL_BindAudioStreams(devid, &stream, 1)`.
980 *
981 * \param devid an audio device to bind a stream to.
982 * \param stream an audio stream to bind to a device.
983 * \returns true on success or false on failure; call SDL_GetError() for more
984 * information.
985 *
986 * \threadsafety It is safe to call this function from any thread.
987 *
988 * \since This function is available since SDL 3.2.0.
989 *
990 * \sa SDL_BindAudioStreams
991 * \sa SDL_UnbindAudioStream
992 * \sa SDL_GetAudioStreamDevice
993 */
994extern SDL_DECLSPEC bool SDLCALL SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream);
995
996/**
997 * Unbind a list of audio streams from their audio devices.
998 *
999 * The streams being unbound do not all have to be on the same device. All
1000 * streams on the same device will be unbound atomically (data will stop
1001 * flowing through all unbound streams on the same device at the same time).
1002 *
1003 * Unbinding a stream that isn't bound to a device is a legal no-op.
1004 *
1005 * \param streams an array of audio streams to unbind. Can be NULL or contain
1006 * NULL.
1007 * \param num_streams number streams listed in the `streams` array.
1008 *
1009 * \threadsafety It is safe to call this function from any thread.
1010 *
1011 * \since This function is available since SDL 3.2.0.
1012 *
1013 * \sa SDL_BindAudioStreams
1014 */
1015extern SDL_DECLSPEC void SDLCALL SDL_UnbindAudioStreams(SDL_AudioStream * const *streams, int num_streams);
1016
1017/**
1018 * Unbind a single audio stream from its audio device.
1019 *
1020 * This is a convenience function, equivalent to calling
1021 * `SDL_UnbindAudioStreams(&stream, 1)`.
1022 *
1023 * \param stream an audio stream to unbind from a device. Can be NULL.
1024 *
1025 * \threadsafety It is safe to call this function from any thread.
1026 *
1027 * \since This function is available since SDL 3.2.0.
1028 *
1029 * \sa SDL_BindAudioStream
1030 */
1031extern SDL_DECLSPEC void SDLCALL SDL_UnbindAudioStream(SDL_AudioStream *stream);
1032
1033/**
1034 * Query an audio stream for its currently-bound device.
1035 *
1036 * This reports the logical audio device that an audio stream is currently
1037 * bound to.
1038 *
1039 * If not bound, or invalid, this returns zero, which is not a valid device
1040 * ID.
1041 *
1042 * \param stream the audio stream to query.
1043 * \returns the bound audio device, or 0 if not bound or invalid.
1044 *
1045 * \threadsafety It is safe to call this function from any thread.
1046 *
1047 * \since This function is available since SDL 3.2.0.
1048 *
1049 * \sa SDL_BindAudioStream
1050 * \sa SDL_BindAudioStreams
1051 */
1052extern SDL_DECLSPEC SDL_AudioDeviceID SDLCALL SDL_GetAudioStreamDevice(SDL_AudioStream *stream);
1053
1054/**
1055 * Create a new audio stream.
1056 *
1057 * Note that `src_spec` or `dst_spec` may be NULL, but any attempts to
1058 * put or get data from an audio stream will fail until it has valid
1059 * specs assigned to both ends of the stream. Specs can be assigned later
1060 * through SDL_SetAudioStreamFormat(), or binding the stream to an audio
1061 * device (which will set the format of only the input or output,
1062 * depending on what kind of device the stream was bound to).
1063 *
1064 * \param src_spec the format details of the input audio. May be NULL.
1065 * \param dst_spec the format details of the output audio. May be NULL.
1066 * \returns a new audio stream on success or NULL on failure; call
1067 * SDL_GetError() for more information.
1068 *
1069 * \threadsafety It is safe to call this function from any thread.
1070 *
1071 * \since This function is available since SDL 3.2.0.
1072 *
1073 * \sa SDL_PutAudioStreamData
1074 * \sa SDL_GetAudioStreamData
1075 * \sa SDL_GetAudioStreamAvailable
1076 * \sa SDL_FlushAudioStream
1077 * \sa SDL_ClearAudioStream
1078 * \sa SDL_SetAudioStreamFormat
1079 * \sa SDL_DestroyAudioStream
1080 */
1081extern SDL_DECLSPEC SDL_AudioStream * SDLCALL SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
1082
1083/**
1084 * Get the properties associated with an audio stream.
1085 *
1086 * The application can hang any data it wants here, but the following
1087 * properties are understood by SDL:
1088 *
1089 * - `SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN`: if true (the default), the
1090 * stream be automatically cleaned up when the audio subsystem quits. If set
1091 * to false, the streams will persist beyond that. This property is ignored
1092 * for streams created through SDL_OpenAudioDeviceStream(), and will always
1093 * be cleaned up. Streams that are not cleaned up will still be unbound from
1094 * devices when the audio subsystem quits. This property was added in SDL
1095 * 3.4.0.
1096 *
1097 * \param stream the SDL_AudioStream to query.
1098 * \returns a valid property ID on success or 0 on failure; call
1099 * SDL_GetError() for more information.
1100 *
1101 * \threadsafety It is safe to call this function from any thread.
1102 *
1103 * \since This function is available since SDL 3.2.0.
1104 */
1105extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetAudioStreamProperties(SDL_AudioStream *stream);
1106
1107#define SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN "SDL.audiostream.auto_cleanup"
1108
1109
1110/**
1111 * Query the current format of an audio stream.
1112 *
1113 * \param stream the SDL_AudioStream to query.
1114 * \param src_spec where to store the input audio format; ignored if NULL.
1115 * \param dst_spec where to store the output audio format; ignored if NULL.
1116 * \returns true on success or false on failure; call SDL_GetError() for more
1117 * information.
1118 *
1119 * \threadsafety It is safe to call this function from any thread, as it holds
1120 * a stream-specific mutex while running.
1121 *
1122 * \since This function is available since SDL 3.2.0.
1123 *
1124 * \sa SDL_SetAudioStreamFormat
1125 */
1126extern SDL_DECLSPEC bool SDLCALL SDL_GetAudioStreamFormat(SDL_AudioStream *stream, SDL_AudioSpec *src_spec, SDL_AudioSpec *dst_spec);
1127
1128/**
1129 * Change the input and output formats of an audio stream.
1130 *
1131 * Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData
1132 * will reflect the new format, and future calls to SDL_PutAudioStreamData
1133 * must provide data in the new input formats.
1134 *
1135 * Data that was previously queued in the stream will still be operated on in
1136 * the format that was current when it was added, which is to say you can put
1137 * the end of a sound file in one format to a stream, change formats for the
1138 * next sound file, and start putting that new data while the previous sound
1139 * file is still queued, and everything will still play back correctly.
1140 *
1141 * If a stream is bound to a device, then the format of the side of the stream
1142 * bound to a device cannot be changed (src_spec for recording devices,
1143 * dst_spec for playback devices). Attempts to make a change to this side will
1144 * be ignored, but this will not report an error. The other side's format can
1145 * be changed.
1146 *
1147 * `src_spec` and `dst_spec` may each be NULL; a NULL spec signals not to
1148 * change the current format for that side of the stream.
1149 *
1150 * \param stream the stream the format is being changed.
1151 * \param src_spec the new format of the audio input; if NULL, it is not
1152 * changed.
1153 * \param dst_spec the new format of the audio output; if NULL, it is not
1154 * changed.
1155 * \returns true on success or false on failure; call SDL_GetError() for more
1156 * information.
1157 *
1158 * \threadsafety It is safe to call this function from any thread, as it holds
1159 * a stream-specific mutex while running.
1160 *
1161 * \since This function is available since SDL 3.2.0.
1162 *
1163 * \sa SDL_GetAudioStreamFormat
1164 * \sa SDL_SetAudioStreamFrequencyRatio
1165 */
1166extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream, const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
1167
1168/**
1169 * Get the frequency ratio of an audio stream.
1170 *
1171 * \param stream the SDL_AudioStream to query.
1172 * \returns the frequency ratio of the stream or 0.0 on failure; call
1173 * SDL_GetError() for more information.
1174 *
1175 * \threadsafety It is safe to call this function from any thread, as it holds
1176 * a stream-specific mutex while running.
1177 *
1178 * \since This function is available since SDL 3.2.0.
1179 *
1180 * \sa SDL_SetAudioStreamFrequencyRatio
1181 */
1182extern SDL_DECLSPEC float SDLCALL SDL_GetAudioStreamFrequencyRatio(SDL_AudioStream *stream);
1183
1184/**
1185 * Change the frequency ratio of an audio stream.
1186 *
1187 * The frequency ratio is used to adjust the rate at which input data is
1188 * consumed. Changing this effectively modifies the speed and pitch of the
1189 * audio. A value greater than 1.0f will play the audio faster, and at a
1190 * higher pitch. A value less than 1.0f will play the audio slower, and at a
1191 * lower pitch. 1.0f means play at normal speed.
1192 *
1193 * This is applied during SDL_GetAudioStreamData, and can be continuously
1194 * changed to create various effects.
1195 *
1196 * \param stream the stream on which the frequency ratio is being changed.
1197 * \param ratio the frequency ratio. 1.0 is normal speed. Must be between 0.01
1198 * and 100.
1199 * \returns true on success or false on failure; call SDL_GetError() for more
1200 * information.
1201 *
1202 * \threadsafety It is safe to call this function from any thread, as it holds
1203 * a stream-specific mutex while running.
1204 *
1205 * \since This function is available since SDL 3.2.0.
1206 *
1207 * \sa SDL_GetAudioStreamFrequencyRatio
1208 * \sa SDL_SetAudioStreamFormat
1209 */
1210extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamFrequencyRatio(SDL_AudioStream *stream, float ratio);
1211
1212/**
1213 * Get the gain of an audio stream.
1214 *
1215 * The gain of a stream is its volume; a larger gain means a louder output,
1216 * with a gain of zero being silence.
1217 *
1218 * Audio streams default to a gain of 1.0f (no change in output).
1219 *
1220 * \param stream the SDL_AudioStream to query.
1221 * \returns the gain of the stream or -1.0f on failure; call SDL_GetError()
1222 * for more information.
1223 *
1224 * \threadsafety It is safe to call this function from any thread, as it holds
1225 * a stream-specific mutex while running.
1226 *
1227 * \since This function is available since SDL 3.2.0.
1228 *
1229 * \sa SDL_SetAudioStreamGain
1230 */
1231extern SDL_DECLSPEC float SDLCALL SDL_GetAudioStreamGain(SDL_AudioStream *stream);
1232
1233/**
1234 * Change the gain of an audio stream.
1235 *
1236 * The gain of a stream is its volume; a larger gain means a louder output,
1237 * with a gain of zero being silence.
1238 *
1239 * Audio streams default to a gain of 1.0f (no change in output).
1240 *
1241 * This is applied during SDL_GetAudioStreamData, and can be continuously
1242 * changed to create various effects.
1243 *
1244 * \param stream the stream on which the gain is being changed.
1245 * \param gain the gain. 1.0f is no change, 0.0f is silence.
1246 * \returns true on success or false on failure; call SDL_GetError() for more
1247 * information.
1248 *
1249 * \threadsafety It is safe to call this function from any thread, as it holds
1250 * a stream-specific mutex while running.
1251 *
1252 * \since This function is available since SDL 3.2.0.
1253 *
1254 * \sa SDL_GetAudioStreamGain
1255 */
1256extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamGain(SDL_AudioStream *stream, float gain);
1257
1258/**
1259 * Get the current input channel map of an audio stream.
1260 *
1261 * Channel maps are optional; most things do not need them, instead passing
1262 * data in the [order that SDL expects](CategoryAudio#channel-layouts).
1263 *
1264 * Audio streams default to no remapping applied. This is represented by
1265 * returning NULL, and does not signify an error.
1266 *
1267 * \param stream the SDL_AudioStream to query.
1268 * \param count On output, set to number of channels in the map. Can be NULL.
1269 * \returns an array of the current channel mapping, with as many elements as
1270 * the current output spec's channels, or NULL if default. This
1271 * should be freed with SDL_free() when it is no longer needed.
1272 *
1273 * \threadsafety It is safe to call this function from any thread, as it holds
1274 * a stream-specific mutex while running.
1275 *
1276 * \since This function is available since SDL 3.2.0.
1277 *
1278 * \sa SDL_SetAudioStreamInputChannelMap
1279 */
1280extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioStreamInputChannelMap(SDL_AudioStream *stream, int *count);
1281
1282/**
1283 * Get the current output channel map of an audio stream.
1284 *
1285 * Channel maps are optional; most things do not need them, instead passing
1286 * data in the [order that SDL expects](CategoryAudio#channel-layouts).
1287 *
1288 * Audio streams default to no remapping applied. This is represented by
1289 * returning NULL, and does not signify an error.
1290 *
1291 * \param stream the SDL_AudioStream to query.
1292 * \param count On output, set to number of channels in the map. Can be NULL.
1293 * \returns an array of the current channel mapping, with as many elements as
1294 * the current output spec's channels, or NULL if default. This
1295 * should be freed with SDL_free() when it is no longer needed.
1296 *
1297 * \threadsafety It is safe to call this function from any thread, as it holds
1298 * a stream-specific mutex while running.
1299 *
1300 * \since This function is available since SDL 3.2.0.
1301 *
1302 * \sa SDL_SetAudioStreamInputChannelMap
1303 */
1304extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioStreamOutputChannelMap(SDL_AudioStream *stream, int *count);
1305
1306/**
1307 * Set the current input channel map of an audio stream.
1308 *
1309 * Channel maps are optional; most things do not need them, instead passing
1310 * data in the [order that SDL expects](CategoryAudio#channel-layouts).
1311 *
1312 * The input channel map reorders data that is added to a stream via
1313 * SDL_PutAudioStreamData. Future calls to SDL_PutAudioStreamData must provide
1314 * data in the new channel order.
1315 *
1316 * Each item in the array represents an input channel, and its value is the
1317 * channel that it should be remapped to. To reverse a stereo signal's left
1318 * and right values, you'd have an array of `{ 1, 0 }`. It is legal to remap
1319 * multiple channels to the same thing, so `{ 1, 1 }` would duplicate the
1320 * right channel to both channels of a stereo signal. An element in the
1321 * channel map set to -1 instead of a valid channel will mute that channel,
1322 * setting it to a silence value.
1323 *
1324 * You cannot change the number of channels through a channel map, just
1325 * reorder/mute them.
1326 *
1327 * Data that was previously queued in the stream will still be operated on in
1328 * the order that was current when it was added, which is to say you can put
1329 * the end of a sound file in one order to a stream, change orders for the
1330 * next sound file, and start putting that new data while the previous sound
1331 * file is still queued, and everything will still play back correctly.
1332 *
1333 * Audio streams default to no remapping applied. Passing a NULL channel map
1334 * is legal, and turns off remapping.
1335 *
1336 * SDL will copy the channel map; the caller does not have to save this array
1337 * after this call.
1338 *
1339 * If `count` is not equal to the current number of channels in the audio
1340 * stream's format, this will fail. This is a safety measure to make sure a
1341 * race condition hasn't changed the format while this call is setting the
1342 * channel map.
1343 *
1344 * Unlike attempting to change the stream's format, the input channel map on a
1345 * stream bound to a recording device is permitted to change at any time; any
1346 * data added to the stream from the device after this call will have the new
1347 * mapping, but previously-added data will still have the prior mapping.
1348 *
1349 * \param stream the SDL_AudioStream to change.
1350 * \param chmap the new channel map, NULL to reset to default.
1351 * \param count The number of channels in the map.
1352 * \returns true on success or false on failure; call SDL_GetError() for more
1353 * information.
1354 *
1355 * \threadsafety It is safe to call this function from any thread, as it holds
1356 * a stream-specific mutex while running. Don't change the
1357 * stream's format to have a different number of channels from a
1358 * different thread at the same time, though!
1359 *
1360 * \since This function is available since SDL 3.2.0.
1361 *
1362 * \sa SDL_SetAudioStreamOutputChannelMap
1363 */
1364extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamInputChannelMap(SDL_AudioStream *stream, const int *chmap, int count);
1365
1366/**
1367 * Set the current output channel map of an audio stream.
1368 *
1369 * Channel maps are optional; most things do not need them, instead passing
1370 * data in the [order that SDL expects](CategoryAudio#channel-layouts).
1371 *
1372 * The output channel map reorders data that is leaving a stream via
1373 * SDL_GetAudioStreamData.
1374 *
1375 * Each item in the array represents an input channel, and its value is the
1376 * channel that it should be remapped to. To reverse a stereo signal's left
1377 * and right values, you'd have an array of `{ 1, 0 }`. It is legal to remap
1378 * multiple channels to the same thing, so `{ 1, 1 }` would duplicate the
1379 * right channel to both channels of a stereo signal. An element in the
1380 * channel map set to -1 instead of a valid channel will mute that channel,
1381 * setting it to a silence value.
1382 *
1383 * You cannot change the number of channels through a channel map, just
1384 * reorder/mute them.
1385 *
1386 * The output channel map can be changed at any time, as output remapping is
1387 * applied during SDL_GetAudioStreamData.
1388 *
1389 * Audio streams default to no remapping applied. Passing a NULL channel map
1390 * is legal, and turns off remapping.
1391 *
1392 * SDL will copy the channel map; the caller does not have to save this array
1393 * after this call.
1394 *
1395 * If `count` is not equal to the current number of channels in the audio
1396 * stream's format, this will fail. This is a safety measure to make sure a
1397 * race condition hasn't changed the format while this call is setting the
1398 * channel map.
1399 *
1400 * Unlike attempting to change the stream's format, the output channel map on
1401 * a stream bound to a recording device is permitted to change at any time;
1402 * any data added to the stream after this call will have the new mapping, but
1403 * previously-added data will still have the prior mapping. When the channel
1404 * map doesn't match the hardware's channel layout, SDL will convert the data
1405 * before feeding it to the device for playback.
1406 *
1407 * \param stream the SDL_AudioStream to change.
1408 * \param chmap the new channel map, NULL to reset to default.
1409 * \param count The number of channels in the map.
1410 * \returns true on success or false on failure; call SDL_GetError() for more
1411 * information.
1412 *
1413 * \threadsafety It is safe to call this function from any thread, as it holds
1414 * a stream-specific mutex while running. Don't change the
1415 * stream's format to have a different number of channels from a
1416 * a different thread at the same time, though!
1417 *
1418 * \since This function is available since SDL 3.2.0.
1419 *
1420 * \sa SDL_SetAudioStreamInputChannelMap
1421 */
1422extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamOutputChannelMap(SDL_AudioStream *stream, const int *chmap, int count);
1423
1424/**
1425 * Add data to the stream.
1426 *
1427 * This data must match the format/channels/samplerate specified in the latest
1428 * call to SDL_SetAudioStreamFormat, or the format specified when creating the
1429 * stream if it hasn't been changed.
1430 *
1431 * Note that this call simply copies the unconverted data for later. This is
1432 * different than SDL2, where data was converted during the Put call and the
1433 * Get call would just dequeue the previously-converted data.
1434 *
1435 * \param stream the stream the audio data is being added to.
1436 * \param buf a pointer to the audio data to add.
1437 * \param len the number of bytes to write to the stream.
1438 * \returns true on success or false on failure; call SDL_GetError() for more
1439 * information.
1440 *
1441 * \threadsafety It is safe to call this function from any thread, but if the
1442 * stream has a callback set, the caller might need to manage
1443 * extra locking.
1444 *
1445 * \since This function is available since SDL 3.2.0.
1446 *
1447 * \sa SDL_ClearAudioStream
1448 * \sa SDL_FlushAudioStream
1449 * \sa SDL_GetAudioStreamData
1450 * \sa SDL_GetAudioStreamQueued
1451 */
1452extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
1453
1454/**
1455 * A callback that fires for completed SDL_PutAudioStreamDataNoCopy() data.
1456 *
1457 * When using SDL_PutAudioStreamDataNoCopy() to provide data to an
1458 * SDL_AudioStream, it's not safe to dispose of the data until the stream has
1459 * completely consumed it. Often times it's difficult to know exactly when
1460 * this has happened.
1461 *
1462 * This callback fires once when the stream no longer needs the buffer,
1463 * allowing the app to easily free or reuse it.
1464 *
1465 * \param userdata an opaque pointer provided by the app for their personal
1466 * use.
1467 * \param buf the pointer provided to SDL_PutAudioStreamDataNoCopy().
1468 * \param buflen the size of buffer, in bytes, provided to
1469 * SDL_PutAudioStreamDataNoCopy().
1470 *
1471 * \threadsafety This callbacks may run from any thread, so if you need to
1472 * protect shared data, you should use SDL_LockAudioStream to
1473 * serialize access; this lock will be held before your callback
1474 * is called, so your callback does not need to manage the lock
1475 * explicitly.
1476 *
1477 * \since This datatype is available since SDL 3.4.0.
1478 *
1479 * \sa SDL_SetAudioStreamGetCallback
1480 * \sa SDL_SetAudioStreamPutCallback
1481 */
1482typedef void (SDLCALL *SDL_AudioStreamDataCompleteCallback)(void *userdata, const void *buf, int buflen);
1483
1484/**
1485 * Add external data to an audio stream without copying it.
1486 *
1487 * Unlike SDL_PutAudioStreamData(), this function does not make a copy of the
1488 * provided data, instead storing the provided pointer. This means that the
1489 * put operation does not need to allocate and copy the data, but the original
1490 * data must remain available until the stream is done with it, either by
1491 * being read from the stream in its entirety, or a call to
1492 * SDL_ClearAudioStream() or SDL_DestroyAudioStream().
1493 *
1494 * The data must match the format/channels/samplerate specified in the latest
1495 * call to SDL_SetAudioStreamFormat, or the format specified when creating the
1496 * stream if it hasn't been changed.
1497 *
1498 * An optional callback may be provided, which is called when the stream no
1499 * longer needs the data. Once this callback fires, the stream will not access
1500 * the data again. This callback will fire for any reason the data is no
1501 * longer needed, including clearing or destroying the stream.
1502 *
1503 * Note that there is still an allocation to store tracking information, so
1504 * this function is more efficient for larger blocks of data. If you're
1505 * planning to put a few samples at a time, it will be more efficient to use
1506 * SDL_PutAudioStreamData(), which allocates and buffers in blocks.
1507 *
1508 * \param stream the stream the audio data is being added to.
1509 * \param buf a pointer to the audio data to add.
1510 * \param len the number of bytes to add to the stream.
1511 * \param callback the callback function to call when the data is no longer
1512 * needed by the stream. May be NULL.
1513 * \param userdata an opaque pointer provided to the callback for its own
1514 * personal use.
1515 * \returns true on success or false on failure; call SDL_GetError() for more
1516 * information.
1517 *
1518 * \threadsafety It is safe to call this function from any thread, but if the
1519 * stream has a callback set, the caller might need to manage
1520 * extra locking.
1521 *
1522 * \since This function is available since SDL 3.4.0.
1523 *
1524 * \sa SDL_ClearAudioStream
1525 * \sa SDL_FlushAudioStream
1526 * \sa SDL_GetAudioStreamData
1527 * \sa SDL_GetAudioStreamQueued
1528 */
1529extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamDataNoCopy(SDL_AudioStream *stream, const void *buf, int len, SDL_AudioStreamDataCompleteCallback callback, void *userdata);
1530
1531/**
1532 * Add data to the stream with each channel in a separate array.
1533 *
1534 * This data must match the format/channels/samplerate specified in the latest
1535 * call to SDL_SetAudioStreamFormat, or the format specified when creating the
1536 * stream if it hasn't been changed.
1537 *
1538 * The data will be interleaved and queued. Note that SDL_AudioStream only
1539 * operates on interleaved data, so this is simply a convenience function for
1540 * easily queueing data from sources that provide separate arrays. There is no
1541 * equivalent function to retrieve planar data.
1542 *
1543 * The arrays in `channel_buffers` are ordered as they are to be interleaved;
1544 * the first array will be the first sample in the interleaved data. Any
1545 * individual array may be NULL; in this case, silence will be interleaved for
1546 * that channel.
1547 *
1548 * `num_channels` specifies how many arrays are in `channel_buffers`. This can
1549 * be used as a safety to prevent overflow, in case the stream format has
1550 * changed elsewhere. If more channels are specified than the current input
1551 * spec, they are ignored. If less channels are specified, the missing arrays
1552 * are treated as if they are NULL (silence is written to those channels). If
1553 * the count is -1, SDL will assume the array count matches the current input
1554 * spec.
1555 *
1556 * Note that `num_samples` is the number of _samples per array_. This can also
1557 * be thought of as the number of _sample frames_ to be queued. A value of 1
1558 * with stereo arrays will queue two samples to the stream. This is different
1559 * than SDL_PutAudioStreamData, which wants the size of a single array in
1560 * bytes.
1561 *
1562 * \param stream the stream the audio data is being added to.
1563 * \param channel_buffers a pointer to an array of arrays, one array per
1564 * channel.
1565 * \param num_channels the number of arrays in `channel_buffers` or -1.
1566 * \param num_samples the number of _samples_ per array to write to the
1567 * stream.
1568 * \returns true on success or false on failure; call SDL_GetError() for more
1569 * information.
1570 *
1571 * \threadsafety It is safe to call this function from any thread, but if the
1572 * stream has a callback set, the caller might need to manage
1573 * extra locking.
1574 *
1575 * \since This function is available since SDL 3.4.0.
1576 *
1577 * \sa SDL_ClearAudioStream
1578 * \sa SDL_FlushAudioStream
1579 * \sa SDL_GetAudioStreamData
1580 * \sa SDL_GetAudioStreamQueued
1581 */
1582extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void * const *channel_buffers, int num_channels, int num_samples);
1583
1584/**
1585 * Get converted/resampled data from the stream.
1586 *
1587 * The input/output data format/channels/samplerate is specified when creating
1588 * the stream, and can be changed after creation by calling
1589 * SDL_SetAudioStreamFormat.
1590 *
1591 * Note that any conversion and resampling necessary is done during this call,
1592 * and SDL_PutAudioStreamData simply queues unconverted data for later. This
1593 * is different than SDL2, where that work was done while inputting new data
1594 * to the stream and requesting the output just copied the converted data.
1595 *
1596 * \param stream the stream the audio is being requested from.
1597 * \param buf a buffer to fill with audio data.
1598 * \param len the maximum number of bytes to fill.
1599 * \returns the number of bytes read from the stream or -1 on failure; call
1600 * SDL_GetError() for more information.
1601 *
1602 * \threadsafety It is safe to call this function from any thread, but if the
1603 * stream has a callback set, the caller might need to manage
1604 * extra locking.
1605 *
1606 * \since This function is available since SDL 3.2.0.
1607 *
1608 * \sa SDL_ClearAudioStream
1609 * \sa SDL_GetAudioStreamAvailable
1610 * \sa SDL_PutAudioStreamData
1611 */
1612extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
1613
1614/**
1615 * Get the number of converted/resampled bytes available.
1616 *
1617 * The stream may be buffering data behind the scenes until it has enough to
1618 * resample correctly, so this number might be lower than what you expect, or
1619 * even be zero. Add more data or flush the stream if you need the data now.
1620 *
1621 * If the stream has so much data that it would overflow an int, the return
1622 * value is clamped to a maximum value, but no queued data is lost; if there
1623 * are gigabytes of data queued, the app might need to read some of it with
1624 * SDL_GetAudioStreamData before this function's return value is no longer
1625 * clamped.
1626 *
1627 * \param stream the audio stream to query.
1628 * \returns the number of converted/resampled bytes available or -1 on
1629 * failure; call SDL_GetError() for more information.
1630 *
1631 * \threadsafety It is safe to call this function from any thread.
1632 *
1633 * \since This function is available since SDL 3.2.0.
1634 *
1635 * \sa SDL_GetAudioStreamData
1636 * \sa SDL_PutAudioStreamData
1637 */
1638extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
1639
1640
1641/**
1642 * Get the number of bytes currently queued.
1643 *
1644 * This is the number of bytes put into a stream as input, not the number that
1645 * can be retrieved as output. Because of several details, it's not possible
1646 * to calculate one number directly from the other. If you need to know how
1647 * much usable data can be retrieved right now, you should use
1648 * SDL_GetAudioStreamAvailable() and not this function.
1649 *
1650 * Note that audio streams can change their input format at any time, even if
1651 * there is still data queued in a different format, so the returned byte
1652 * count will not necessarily match the number of _sample frames_ available.
1653 * Users of this API should be aware of format changes they make when feeding
1654 * a stream and plan accordingly.
1655 *
1656 * Queued data is not converted until it is consumed by
1657 * SDL_GetAudioStreamData, so this value should be representative of the exact
1658 * data that was put into the stream.
1659 *
1660 * If the stream has so much data that it would overflow an int, the return
1661 * value is clamped to a maximum value, but no queued data is lost; if there
1662 * are gigabytes of data queued, the app might need to read some of it with
1663 * SDL_GetAudioStreamData before this function's return value is no longer
1664 * clamped.
1665 *
1666 * \param stream the audio stream to query.
1667 * \returns the number of bytes queued or -1 on failure; call SDL_GetError()
1668 * for more information.
1669 *
1670 * \threadsafety It is safe to call this function from any thread.
1671 *
1672 * \since This function is available since SDL 3.2.0.
1673 *
1674 * \sa SDL_PutAudioStreamData
1675 * \sa SDL_ClearAudioStream
1676 */
1677extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamQueued(SDL_AudioStream *stream);
1678
1679
1680/**
1681 * Tell the stream that you're done sending data, and anything being buffered
1682 * should be converted/resampled and made available immediately.
1683 *
1684 * It is legal to add more data to a stream after flushing, but there may be
1685 * audio gaps in the output. Generally this is intended to signal the end of
1686 * input, so the complete output becomes available.
1687 *
1688 * \param stream the audio stream to flush.
1689 * \returns true on success or false on failure; call SDL_GetError() for more
1690 * information.
1691 *
1692 * \threadsafety It is safe to call this function from any thread.
1693 *
1694 * \since This function is available since SDL 3.2.0.
1695 *
1696 * \sa SDL_PutAudioStreamData
1697 */
1698extern SDL_DECLSPEC bool SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
1699
1700/**
1701 * Clear any pending data in the stream.
1702 *
1703 * This drops any queued data, so there will be nothing to read from the
1704 * stream until more is added.
1705 *
1706 * \param stream the audio stream to clear.
1707 * \returns true on success or false on failure; call SDL_GetError() for more
1708 * information.
1709 *
1710 * \threadsafety It is safe to call this function from any thread.
1711 *
1712 * \since This function is available since SDL 3.2.0.
1713 *
1714 * \sa SDL_GetAudioStreamAvailable
1715 * \sa SDL_GetAudioStreamData
1716 * \sa SDL_GetAudioStreamQueued
1717 * \sa SDL_PutAudioStreamData
1718 */
1719extern SDL_DECLSPEC bool SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
1720
1721/**
1722 * Use this function to pause audio playback on the audio device associated
1723 * with an audio stream.
1724 *
1725 * This function pauses audio processing for a given device. Any bound audio
1726 * streams will not progress, and no audio will be generated. Pausing one
1727 * device does not prevent other unpaused devices from running.
1728 *
1729 * Pausing a device can be useful to halt all audio without unbinding all the
1730 * audio streams. This might be useful while a game is paused, or a level is
1731 * loading, etc.
1732 *
1733 * \param stream the audio stream associated with the audio device to pause.
1734 * \returns true on success or false on failure; call SDL_GetError() for more
1735 * information.
1736 *
1737 * \threadsafety It is safe to call this function from any thread.
1738 *
1739 * \since This function is available since SDL 3.2.0.
1740 *
1741 * \sa SDL_ResumeAudioStreamDevice
1742 */
1743extern SDL_DECLSPEC bool SDLCALL SDL_PauseAudioStreamDevice(SDL_AudioStream *stream);
1744
1745/**
1746 * Use this function to unpause audio playback on the audio device associated
1747 * with an audio stream.
1748 *
1749 * This function unpauses audio processing for a given device that has
1750 * previously been paused. Once unpaused, any bound audio streams will begin
1751 * to progress again, and audio can be generated.
1752 *
1753 * SDL_OpenAudioDeviceStream opens audio devices in a paused state, so this
1754 * function call is required for audio playback to begin on such devices.
1755 *
1756 * \param stream the audio stream associated with the audio device to resume.
1757 * \returns true on success or false on failure; call SDL_GetError() for more
1758 * information.
1759 *
1760 * \threadsafety It is safe to call this function from any thread.
1761 *
1762 * \since This function is available since SDL 3.2.0.
1763 *
1764 * \sa SDL_PauseAudioStreamDevice
1765 */
1766extern SDL_DECLSPEC bool SDLCALL SDL_ResumeAudioStreamDevice(SDL_AudioStream *stream);
1767
1768/**
1769 * Use this function to query if an audio device associated with a stream is
1770 * paused.
1771 *
1772 * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
1773 * has to bind a stream before any audio will flow.
1774 *
1775 * \param stream the audio stream associated with the audio device to query.
1776 * \returns true if device is valid and paused, false otherwise.
1777 *
1778 * \threadsafety It is safe to call this function from any thread.
1779 *
1780 * \since This function is available since SDL 3.2.0.
1781 *
1782 * \sa SDL_PauseAudioStreamDevice
1783 * \sa SDL_ResumeAudioStreamDevice
1784 */
1785extern SDL_DECLSPEC bool SDLCALL SDL_AudioStreamDevicePaused(SDL_AudioStream *stream);
1786
1787
1788/**
1789 * Lock an audio stream for serialized access.
1790 *
1791 * Each SDL_AudioStream has an internal mutex it uses to protect its data
1792 * structures from threading conflicts. This function allows an app to lock
1793 * that mutex, which could be useful if registering callbacks on this stream.
1794 *
1795 * One does not need to lock a stream to use in it most cases, as the stream
1796 * manages this lock internally. However, this lock is held during callbacks,
1797 * which may run from arbitrary threads at any time, so if an app needs to
1798 * protect shared data during those callbacks, locking the stream guarantees
1799 * that the callback is not running while the lock is held.
1800 *
1801 * As this is just a wrapper over SDL_LockMutex for an internal lock; it has
1802 * all the same attributes (recursive locks are allowed, etc).
1803 *
1804 * \param stream the audio stream to lock.
1805 * \returns true on success or false on failure; call SDL_GetError() for more
1806 * information.
1807 *
1808 * \threadsafety It is safe to call this function from any thread.
1809 *
1810 * \since This function is available since SDL 3.2.0.
1811 *
1812 * \sa SDL_UnlockAudioStream
1813 */
1814extern SDL_DECLSPEC bool SDLCALL SDL_LockAudioStream(SDL_AudioStream *stream);
1815
1816
1817/**
1818 * Unlock an audio stream for serialized access.
1819 *
1820 * This unlocks an audio stream after a call to SDL_LockAudioStream.
1821 *
1822 * \param stream the audio stream to unlock.
1823 * \returns true on success or false on failure; call SDL_GetError() for more
1824 * information.
1825 *
1826 * \threadsafety You should only call this from the same thread that
1827 * previously called SDL_LockAudioStream.
1828 *
1829 * \since This function is available since SDL 3.2.0.
1830 *
1831 * \sa SDL_LockAudioStream
1832 */
1833extern SDL_DECLSPEC bool SDLCALL SDL_UnlockAudioStream(SDL_AudioStream *stream);
1834
1835/**
1836 * A callback that fires when data passes through an SDL_AudioStream.
1837 *
1838 * Apps can (optionally) register a callback with an audio stream that is
1839 * called when data is added with SDL_PutAudioStreamData, or requested with
1840 * SDL_GetAudioStreamData.
1841 *
1842 * Two values are offered here: one is the amount of additional data needed to
1843 * satisfy the immediate request (which might be zero if the stream already
1844 * has enough data queued) and the other is the total amount being requested.
1845 * In a Get call triggering a Put callback, these values can be different. In
1846 * a Put call triggering a Get callback, these values are always the same.
1847 *
1848 * Byte counts might be slightly overestimated due to buffering or resampling,
1849 * and may change from call to call.
1850 *
1851 * This callback is not required to do anything. Generally this is useful for
1852 * adding/reading data on demand, and the app will often put/get data as
1853 * appropriate, but the system goes on with the data currently available to it
1854 * if this callback does nothing.
1855 *
1856 * \param stream the SDL audio stream associated with this callback.
1857 * \param additional_amount the amount of data, in bytes, that is needed right
1858 * now.
1859 * \param total_amount the total amount of data requested, in bytes, that is
1860 * requested or available.
1861 * \param userdata an opaque pointer provided by the app for their personal
1862 * use.
1863 *
1864 * \threadsafety This callbacks may run from any thread, so if you need to
1865 * protect shared data, you should use SDL_LockAudioStream to
1866 * serialize access; this lock will be held before your callback
1867 * is called, so your callback does not need to manage the lock
1868 * explicitly.
1869 *
1870 * \since This datatype is available since SDL 3.2.0.
1871 *
1872 * \sa SDL_SetAudioStreamGetCallback
1873 * \sa SDL_SetAudioStreamPutCallback
1874 */
1875typedef void (SDLCALL *SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount);
1876
1877/**
1878 * Set a callback that runs when data is requested from an audio stream.
1879 *
1880 * This callback is called _before_ data is obtained from the stream, giving
1881 * the callback the chance to add more on-demand.
1882 *
1883 * The callback can (optionally) call SDL_PutAudioStreamData() to add more
1884 * audio to the stream during this call; if needed, the request that triggered
1885 * this callback will obtain the new data immediately.
1886 *
1887 * The callback's `additional_amount` argument is roughly how many bytes of
1888 * _unconverted_ data (in the stream's input format) is needed by the caller,
1889 * although this may overestimate a little for safety. This takes into account
1890 * how much is already in the stream and only asks for any extra necessary to
1891 * resolve the request, which means the callback may be asked for zero bytes,
1892 * and a different amount on each call.
1893 *
1894 * The callback is not required to supply exact amounts; it is allowed to
1895 * supply too much or too little or none at all. The caller will get what's
1896 * available, up to the amount they requested, regardless of this callback's
1897 * outcome.
1898 *
1899 * Clearing or flushing an audio stream does not call this callback.
1900 *
1901 * This function obtains the stream's lock, which means any existing callback
1902 * (get or put) in progress will finish running before setting the new
1903 * callback.
1904 *
1905 * Setting a NULL function turns off the callback.
1906 *
1907 * \param stream the audio stream to set the new callback on.
1908 * \param callback the new callback function to call when data is requested
1909 * from the stream.
1910 * \param userdata an opaque pointer provided to the callback for its own
1911 * personal use.
1912 * \returns true on success or false on failure; call SDL_GetError() for more
1913 * information. This only fails if `stream` is NULL.
1914 *
1915 * \threadsafety It is safe to call this function from any thread.
1916 *
1917 * \since This function is available since SDL 3.2.0.
1918 *
1919 * \sa SDL_SetAudioStreamPutCallback
1920 */
1921extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
1922
1923/**
1924 * Set a callback that runs when data is added to an audio stream.
1925 *
1926 * This callback is called _after_ the data is added to the stream, giving the
1927 * callback the chance to obtain it immediately.
1928 *
1929 * The callback can (optionally) call SDL_GetAudioStreamData() to obtain audio
1930 * from the stream during this call.
1931 *
1932 * The callback's `additional_amount` argument is how many bytes of
1933 * _converted_ data (in the stream's output format) was provided by the
1934 * caller, although this may underestimate a little for safety. This value
1935 * might be less than what is currently available in the stream, if data was
1936 * already there, and might be less than the caller provided if the stream
1937 * needs to keep a buffer to aid in resampling. Which means the callback may
1938 * be provided with zero bytes, and a different amount on each call.
1939 *
1940 * The callback may call SDL_GetAudioStreamAvailable to see the total amount
1941 * currently available to read from the stream, instead of the total provided
1942 * by the current call.
1943 *
1944 * The callback is not required to obtain all data. It is allowed to read less
1945 * or none at all. Anything not read now simply remains in the stream for
1946 * later access.
1947 *
1948 * Clearing or flushing an audio stream does not call this callback.
1949 *
1950 * This function obtains the stream's lock, which means any existing callback
1951 * (get or put) in progress will finish running before setting the new
1952 * callback.
1953 *
1954 * Setting a NULL function turns off the callback.
1955 *
1956 * \param stream the audio stream to set the new callback on.
1957 * \param callback the new callback function to call when data is added to the
1958 * stream.
1959 * \param userdata an opaque pointer provided to the callback for its own
1960 * personal use.
1961 * \returns true on success or false on failure; call SDL_GetError() for more
1962 * information. This only fails if `stream` is NULL.
1963 *
1964 * \threadsafety It is safe to call this function from any thread.
1965 *
1966 * \since This function is available since SDL 3.2.0.
1967 *
1968 * \sa SDL_SetAudioStreamGetCallback
1969 */
1970extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
1971
1972
1973/**
1974 * Free an audio stream.
1975 *
1976 * This will release all allocated data, including any audio that is still
1977 * queued. You do not need to manually clear the stream first.
1978 *
1979 * If this stream was bound to an audio device, it is unbound during this
1980 * call. If this stream was created with SDL_OpenAudioDeviceStream, the audio
1981 * device that was opened alongside this stream's creation will be closed,
1982 * too.
1983 *
1984 * \param stream the audio stream to destroy.
1985 *
1986 * \threadsafety It is safe to call this function from any thread.
1987 *
1988 * \since This function is available since SDL 3.2.0.
1989 *
1990 * \sa SDL_CreateAudioStream
1991 */
1992extern SDL_DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
1993
1994
1995/**
1996 * Convenience function for straightforward audio init for the common case.
1997 *
1998 * If all your app intends to do is provide a single source of PCM audio, this
1999 * function allows you to do all your audio setup in a single call.
2000 *
2001 * This is also intended to be a clean means to migrate apps from SDL2.
2002 *
2003 * This function will open an audio device, create a stream and bind it.
2004 * Unlike other methods of setup, the audio device will be closed when this
2005 * stream is destroyed, so the app can treat the returned SDL_AudioStream as
2006 * the only object needed to manage audio playback.
2007 *
2008 * Also unlike other functions, the audio device begins paused. This is to map
2009 * more closely to SDL2-style behavior, since there is no extra step here to
2010 * bind a stream to begin audio flowing. The audio device should be resumed
2011 * with SDL_ResumeAudioStreamDevice().
2012 *
2013 * This function works with both playback and recording devices.
2014 *
2015 * The `spec` parameter represents the app's side of the audio stream. That
2016 * is, for recording audio, this will be the output format, and for playing
2017 * audio, this will be the input format. If spec is NULL, the system will
2018 * choose the format, and the app can use SDL_GetAudioStreamFormat() to obtain
2019 * this information later.
2020 *
2021 * If you don't care about opening a specific audio device, you can (and
2022 * probably _should_), use SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK for playback and
2023 * SDL_AUDIO_DEVICE_DEFAULT_RECORDING for recording.
2024 *
2025 * One can optionally provide a callback function; if NULL, the app is
2026 * expected to queue audio data for playback (or unqueue audio data if
2027 * capturing). Otherwise, the callback will begin to fire once the device is
2028 * unpaused.
2029 *
2030 * Destroying the returned stream with SDL_DestroyAudioStream will also close
2031 * the audio device associated with this stream.
2032 *
2033 * \param devid an audio device to open, or SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK
2034 * or SDL_AUDIO_DEVICE_DEFAULT_RECORDING.
2035 * \param spec the audio stream's data format. Can be NULL.
2036 * \param callback a callback where the app will provide new data for
2037 * playback, or receive new data for recording. Can be NULL,
2038 * in which case the app will need to call
2039 * SDL_PutAudioStreamData or SDL_GetAudioStreamData as
2040 * necessary.
2041 * \param userdata app-controlled pointer passed to callback. Can be NULL.
2042 * Ignored if callback is NULL.
2043 * \returns an audio stream on success, ready to use, or NULL on failure; call
2044 * SDL_GetError() for more information. When done with this stream,
2045 * call SDL_DestroyAudioStream to free resources and close the
2046 * device.
2047 *
2048 * \threadsafety It is safe to call this function from any thread.
2049 *
2050 * \since This function is available since SDL 3.2.0.
2051 *
2052 * \sa SDL_GetAudioStreamDevice
2053 * \sa SDL_ResumeAudioStreamDevice
2054 */
2055extern SDL_DECLSPEC SDL_AudioStream * SDLCALL SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata);
2056
2057/**
2058 * A callback that fires when data is about to be fed to an audio device.
2059 *
2060 * This is useful for accessing the final mix, perhaps for writing a
2061 * visualizer or applying a final effect to the audio data before playback.
2062 *
2063 * This callback should run as quickly as possible and not block for any
2064 * significant time, as this callback delays submission of data to the audio
2065 * device, which can cause audio playback problems.
2066 *
2067 * The postmix callback _must_ be able to handle any audio data format
2068 * specified in `spec`, which can change between callbacks if the audio device
2069 * changed. However, this only covers frequency and channel count; data is
2070 * always provided here in SDL_AUDIO_F32 format.
2071 *
2072 * The postmix callback runs _after_ logical device gain and audiostream gain
2073 * have been applied, which is to say you can make the output data louder at
2074 * this point than the gain settings would suggest.
2075 *
2076 * \param userdata a pointer provided by the app through
2077 * SDL_SetAudioPostmixCallback, for its own use.
2078 * \param spec the current format of audio that is to be submitted to the
2079 * audio device.
2080 * \param buffer the buffer of audio samples to be submitted. The callback can
2081 * inspect and/or modify this data.
2082 * \param buflen the size of `buffer` in bytes.
2083 *
2084 * \threadsafety This will run from a background thread owned by SDL. The
2085 * application is responsible for locking resources the callback
2086 * touches that need to be protected.
2087 *
2088 * \since This datatype is available since SDL 3.2.0.
2089 *
2090 * \sa SDL_SetAudioPostmixCallback
2091 */
2092typedef void (SDLCALL *SDL_AudioPostmixCallback)(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen);
2093
2094/**
2095 * Set a callback that fires when data is about to be fed to an audio device.
2096 *
2097 * This is useful for accessing the final mix, perhaps for writing a
2098 * visualizer or applying a final effect to the audio data before playback.
2099 *
2100 * The buffer is the final mix of all bound audio streams on an opened device;
2101 * this callback will fire regularly for any device that is both opened and
2102 * unpaused. If there is no new data to mix, either because no streams are
2103 * bound to the device or all the streams are empty, this callback will still
2104 * fire with the entire buffer set to silence.
2105 *
2106 * This callback is allowed to make changes to the data; the contents of the
2107 * buffer after this call is what is ultimately passed along to the hardware.
2108 *
2109 * The callback is always provided the data in float format (values from -1.0f
2110 * to 1.0f), but the number of channels or sample rate may be different than
2111 * the format the app requested when opening the device; SDL might have had to
2112 * manage a conversion behind the scenes, or the playback might have jumped to
2113 * new physical hardware when a system default changed, etc. These details may
2114 * change between calls. Accordingly, the size of the buffer might change
2115 * between calls as well.
2116 *
2117 * This callback can run at any time, and from any thread; if you need to
2118 * serialize access to your app's data, you should provide and use a mutex or
2119 * other synchronization device.
2120 *
2121 * All of this to say: there are specific needs this callback can fulfill, but
2122 * it is not the simplest interface. Apps should generally provide audio in
2123 * their preferred format through an SDL_AudioStream and let SDL handle the
2124 * difference.
2125 *
2126 * This function is extremely time-sensitive; the callback should do the least
2127 * amount of work possible and return as quickly as it can. The longer the
2128 * callback runs, the higher the risk of audio dropouts or other problems.
2129 *
2130 * This function will block until the audio device is in between iterations,
2131 * so any existing callback that might be running will finish before this
2132 * function sets the new callback and returns.
2133 *
2134 * Setting a NULL callback function disables any previously-set callback.
2135 *
2136 * \param devid the ID of an opened audio device.
2137 * \param callback a callback function to be called. Can be NULL.
2138 * \param userdata app-controlled pointer passed to callback. Can be NULL.
2139 * \returns true on success or false on failure; call SDL_GetError() for more
2140 * information.
2141 *
2142 * \threadsafety It is safe to call this function from any thread.
2143 *
2144 * \since This function is available since SDL 3.2.0.
2145 */
2146extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata);
2147
2148
2149/**
2150 * Load the audio data of a WAVE file into memory.
2151 *
2152 * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
2153 * be valid pointers. The entire data portion of the file is then loaded into
2154 * memory and decoded if necessary.
2155 *
2156 * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
2157 * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
2158 * A-law and mu-law (8 bits). Other formats are currently unsupported and
2159 * cause an error.
2160 *
2161 * If this function succeeds, the return value is zero and the pointer to the
2162 * audio data allocated by the function is written to `audio_buf` and its
2163 * length in bytes to `audio_len`. The SDL_AudioSpec members `freq`,
2164 * `channels`, and `format` are set to the values of the audio data in the
2165 * buffer.
2166 *
2167 * It's necessary to use SDL_free() to free the audio data returned in
2168 * `audio_buf` when it is no longer used.
2169 *
2170 * Because of the underspecification of the .WAV format, there are many
2171 * problematic files in the wild that cause issues with strict decoders. To
2172 * provide compatibility with these files, this decoder is lenient in regards
2173 * to the truncation of the file, the fact chunk, and the size of the RIFF
2174 * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
2175 * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
2176 * tune the behavior of the loading process.
2177 *
2178 * Any file that is invalid (due to truncation, corruption, or wrong values in
2179 * the headers), too big, or unsupported causes an error. Additionally, any
2180 * critical I/O error from the data source will terminate the loading process
2181 * with an error. The function returns NULL on error and in all cases (with
2182 * the exception of `src` being NULL), an appropriate error message will be
2183 * set.
2184 *
2185 * It is required that the data source supports seeking.
2186 *
2187 * Example:
2188 *
2189 * ```c
2190 * SDL_LoadWAV_IO(SDL_IOFromFile("sample.wav", "rb"), true, &spec, &buf, &len);
2191 * ```
2192 *
2193 * Note that the SDL_LoadWAV function does this same thing for you, but in a
2194 * less messy way:
2195 *
2196 * ```c
2197 * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
2198 * ```
2199 *
2200 * \param src the data source for the WAVE data.
2201 * \param closeio if true, calls SDL_CloseIO() on `src` before returning, even
2202 * in the case of an error.
2203 * \param spec a pointer to an SDL_AudioSpec that will be set to the WAVE
2204 * data's format details on successful return.
2205 * \param audio_buf a pointer filled with the audio data, allocated by the
2206 * function.
2207 * \param audio_len a pointer filled with the length of the audio data buffer
2208 * in bytes.
2209 * \returns true on success. `audio_buf` will be filled with a pointer to an
2210 * allocated buffer containing the audio data, and `audio_len` is
2211 * filled with the length of that audio buffer in bytes.
2212 *
2213 * This function returns false if the .WAV file cannot be opened,
2214 * uses an unknown data format, or is corrupt; call SDL_GetError()
2215 * for more information.
2216 *
2217 * When the application is done with the data returned in
2218 * `audio_buf`, it should call SDL_free() to dispose of it.
2219 *
2220 * \threadsafety It is safe to call this function from any thread.
2221 *
2222 * \since This function is available since SDL 3.2.0.
2223 *
2224 * \sa SDL_free
2225 * \sa SDL_LoadWAV
2226 */
2227extern SDL_DECLSPEC bool SDLCALL SDL_LoadWAV_IO(SDL_IOStream *src, bool closeio, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);
2228
2229/**
2230 * Loads a WAV from a file path.
2231 *
2232 * This is a convenience function that is effectively the same as:
2233 *
2234 * ```c
2235 * SDL_LoadWAV_IO(SDL_IOFromFile(path, "rb"), true, spec, audio_buf, audio_len);
2236 * ```
2237 *
2238 * \param path the file path of the WAV file to open.
2239 * \param spec a pointer to an SDL_AudioSpec that will be set to the WAVE
2240 * data's format details on successful return.
2241 * \param audio_buf a pointer filled with the audio data, allocated by the
2242 * function.
2243 * \param audio_len a pointer filled with the length of the audio data buffer
2244 * in bytes.
2245 * \returns true on success. `audio_buf` will be filled with a pointer to an
2246 * allocated buffer containing the audio data, and `audio_len` is
2247 * filled with the length of that audio buffer in bytes.
2248 *
2249 * This function returns false if the .WAV file cannot be opened,
2250 * uses an unknown data format, or is corrupt; call SDL_GetError()
2251 * for more information.
2252 *
2253 * When the application is done with the data returned in
2254 * `audio_buf`, it should call SDL_free() to dispose of it.
2255 *
2256 * \threadsafety It is safe to call this function from any thread.
2257 *
2258 * \since This function is available since SDL 3.2.0.
2259 *
2260 * \sa SDL_free
2261 * \sa SDL_LoadWAV_IO
2262 */
2263extern SDL_DECLSPEC bool SDLCALL SDL_LoadWAV(const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);
2264
2265/**
2266 * Mix audio data in a specified format.
2267 *
2268 * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
2269 * it into `dst`, performing addition, volume adjustment, and overflow
2270 * clipping. The buffer pointed to by `dst` must also be `len` bytes of
2271 * `format` data.
2272 *
2273 * This is provided for convenience -- you can mix your own audio data.
2274 *
2275 * Do not use this function for mixing together more than two streams of
2276 * sample data. The output from repeated application of this function may be
2277 * distorted by clipping, because there is no accumulator with greater range
2278 * than the input (not to mention this being an inefficient way of doing it).
2279 *
2280 * It is a common misconception that this function is required to write audio
2281 * data to an output stream in an audio callback. While you can do that,
2282 * SDL_MixAudio() is really only needed when you're mixing a single audio
2283 * stream with a volume adjustment.
2284 *
2285 * \param dst the destination for the mixed audio.
2286 * \param src the source audio buffer to be mixed.
2287 * \param format the SDL_AudioFormat structure representing the desired audio
2288 * format.
2289 * \param len the length of the audio buffer in bytes.
2290 * \param volume ranges from 0.0 - 1.0, and should be set to 1.0 for full
2291 * audio volume.
2292 * \returns true on success or false on failure; call SDL_GetError() for more
2293 * information.
2294 *
2295 * \threadsafety It is safe to call this function from any thread.
2296 *
2297 * \since This function is available since SDL 3.2.0.
2298 */
2299extern SDL_DECLSPEC bool SDLCALL SDL_MixAudio(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, float volume);
2300
2301/**
2302 * Convert some audio data of one format to another format.
2303 *
2304 * Please note that this function is for convenience, but should not be used
2305 * to resample audio in blocks, as it will introduce audio artifacts on the
2306 * boundaries. You should only use this function if you are converting audio
2307 * data in its entirety in one call. If you want to convert audio in smaller
2308 * chunks, use an SDL_AudioStream, which is designed for this situation.
2309 *
2310 * Internally, this function creates and destroys an SDL_AudioStream on each
2311 * use, so it's also less efficient than using one directly, if you need to
2312 * convert multiple times.
2313 *
2314 * \param src_spec the format details of the input audio.
2315 * \param src_data the audio data to be converted.
2316 * \param src_len the len of src_data.
2317 * \param dst_spec the format details of the output audio.
2318 * \param dst_data will be filled with a pointer to converted audio data,
2319 * which should be freed with SDL_free(). On error, it will be
2320 * NULL.
2321 * \param dst_len will be filled with the len of dst_data.
2322 * \returns true on success or false on failure; call SDL_GetError() for more
2323 * information.
2324 *
2325 * \threadsafety It is safe to call this function from any thread.
2326 *
2327 * \since This function is available since SDL 3.2.0.
2328 */
2329extern SDL_DECLSPEC bool SDLCALL SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec, const Uint8 *src_data, int src_len, const SDL_AudioSpec *dst_spec, Uint8 **dst_data, int *dst_len);
2330
2331/**
2332 * Get the human readable name of an audio format.
2333 *
2334 * \param format the audio format to query.
2335 * \returns the human readable name of the specified audio format or
2336 * "SDL_AUDIO_UNKNOWN" if the format isn't recognized.
2337 *
2338 * \threadsafety It is safe to call this function from any thread.
2339 *
2340 * \since This function is available since SDL 3.2.0.
2341 */
2342extern SDL_DECLSPEC const char * SDLCALL SDL_GetAudioFormatName(SDL_AudioFormat format);
2343
2344/**
2345 * Get the appropriate memset value for silencing an audio format.
2346 *
2347 * The value returned by this function can be used as the second argument to
2348 * memset (or SDL_memset) to set an audio buffer in a specific format to
2349 * silence.
2350 *
2351 * \param format the audio data format to query.
2352 * \returns a byte value that can be passed to memset.
2353 *
2354 * \threadsafety It is safe to call this function from any thread.
2355 *
2356 * \since This function is available since SDL 3.2.0.
2357 */
2358extern SDL_DECLSPEC int SDLCALL SDL_GetSilenceValueForFormat(SDL_AudioFormat format);
2359
2360
2361/* Ends C function definitions when using C++ */
2362#ifdef __cplusplus
2363}
2364#endif
2365#include <SDL3/SDL_close_code.h>
2366
2367#endif /* SDL_audio_h_ */
bool SDL_MixAudio(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, float volume)
bool SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void *const *channel_buffers, int num_channels, int num_samples)
const char * SDL_GetAudioDeviceName(SDL_AudioDeviceID devid)
bool SDL_SetAudioStreamFormat(SDL_AudioStream *stream, const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec)
SDL_AudioDeviceID * SDL_GetAudioRecordingDevices(int *count)
bool SDL_UnlockAudioStream(SDL_AudioStream *stream)
int * SDL_GetAudioStreamInputChannelMap(SDL_AudioStream *stream, int *count)
const char * SDL_GetAudioDriver(int index)
SDL_AudioStream * SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec)
void SDL_UnbindAudioStream(SDL_AudioStream *stream)
bool SDL_IsAudioDevicePhysical(SDL_AudioDeviceID devid)
void SDL_UnbindAudioStreams(SDL_AudioStream *const *streams, int num_streams)
float SDL_GetAudioDeviceGain(SDL_AudioDeviceID devid)
bool SDL_SetAudioStreamGain(SDL_AudioStream *stream, float gain)
struct SDL_AudioStream SDL_AudioStream
Definition SDL_audio.h:452
bool SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata)
bool SDL_IsAudioDevicePlayback(SDL_AudioDeviceID devid)
SDL_AudioDeviceID * SDL_GetAudioPlaybackDevices(int *count)
bool SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata)
void(* SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount)
Definition SDL_audio.h:1876
bool SDL_SetAudioStreamInputChannelMap(SDL_AudioStream *stream, const int *chmap, int count)
bool SDL_AudioDevicePaused(SDL_AudioDeviceID devid)
bool SDL_SetAudioStreamFrequencyRatio(SDL_AudioStream *stream, float ratio)
int SDL_GetNumAudioDrivers(void)
bool SDL_LockAudioStream(SDL_AudioStream *stream)
float SDL_GetAudioStreamGain(SDL_AudioStream *stream)
Uint32 SDL_AudioDeviceID
Definition SDL_audio.h:374
int SDL_GetAudioStreamQueued(SDL_AudioStream *stream)
bool SDL_SetAudioDeviceGain(SDL_AudioDeviceID devid, float gain)
int SDL_GetSilenceValueForFormat(SDL_AudioFormat format)
const char * SDL_GetCurrentAudioDriver(void)
SDL_PropertiesID SDL_GetAudioStreamProperties(SDL_AudioStream *stream)
SDL_AudioStream * SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata)
bool SDL_ResumeAudioStreamDevice(SDL_AudioStream *stream)
void(* SDL_AudioPostmixCallback)(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen)
Definition SDL_audio.h:2093
bool SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec, int *sample_frames)
bool SDL_SetAudioStreamOutputChannelMap(SDL_AudioStream *stream, const int *chmap, int count)
bool SDL_PauseAudioStreamDevice(SDL_AudioStream *stream)
float SDL_GetAudioStreamFrequencyRatio(SDL_AudioStream *stream)
int SDL_GetAudioStreamAvailable(SDL_AudioStream *stream)
SDL_AudioFormat
Definition SDL_audio.h:222
@ SDL_AUDIO_S32LE
Definition SDL_audio.h:232
@ SDL_AUDIO_F32
Definition SDL_audio.h:245
@ SDL_AUDIO_S16LE
Definition SDL_audio.h:228
@ SDL_AUDIO_S16
Definition SDL_audio.h:243
@ SDL_AUDIO_U8
Definition SDL_audio.h:224
@ SDL_AUDIO_S8
Definition SDL_audio.h:226
@ SDL_AUDIO_S32
Definition SDL_audio.h:244
@ SDL_AUDIO_F32LE
Definition SDL_audio.h:236
@ SDL_AUDIO_S32BE
Definition SDL_audio.h:234
@ SDL_AUDIO_UNKNOWN
Definition SDL_audio.h:223
@ SDL_AUDIO_S16BE
Definition SDL_audio.h:230
@ SDL_AUDIO_F32BE
Definition SDL_audio.h:238
bool SDL_LoadWAV(const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
bool SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec, const Uint8 *src_data, int src_len, const SDL_AudioSpec *dst_spec, Uint8 **dst_data, int *dst_len)
bool SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream *const *streams, int num_streams)
int * SDL_GetAudioStreamOutputChannelMap(SDL_AudioStream *stream, int *count)
bool SDL_ClearAudioStream(SDL_AudioStream *stream)
void SDL_DestroyAudioStream(SDL_AudioStream *stream)
void(* SDL_AudioStreamDataCompleteCallback)(void *userdata, const void *buf, int buflen)
Definition SDL_audio.h:1483
void SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
int SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len)
bool SDL_AudioStreamDevicePaused(SDL_AudioStream *stream)
const char * SDL_GetAudioFormatName(SDL_AudioFormat format)
bool SDL_GetAudioStreamFormat(SDL_AudioStream *stream, SDL_AudioSpec *src_spec, SDL_AudioSpec *dst_spec)
SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec)
bool SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream)
bool SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata)
bool SDL_FlushAudioStream(SDL_AudioStream *stream)
int * SDL_GetAudioDeviceChannelMap(SDL_AudioDeviceID devid, int *count)
bool SDL_ResumeAudioDevice(SDL_AudioDeviceID devid)
bool SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len)
bool SDL_PutAudioStreamDataNoCopy(SDL_AudioStream *stream, const void *buf, int len, SDL_AudioStreamDataCompleteCallback callback, void *userdata)
SDL_AudioDeviceID SDL_GetAudioStreamDevice(SDL_AudioStream *stream)
bool SDL_PauseAudioDevice(SDL_AudioDeviceID devid)
bool SDL_LoadWAV_IO(SDL_IOStream *src, bool closeio, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
struct SDL_IOStream SDL_IOStream
Uint32 SDL_PropertiesID
uint8_t Uint8
Definition SDL_stdinc.h:446
uint32_t Uint32
Definition SDL_stdinc.h:482
SDL_AudioFormat format
Definition SDL_audio.h:407

◆ SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN

#define SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN   "SDL.audiostream.auto_cleanup"

Definition at line 1108 of file SDL_audio.h.

Typedef Documentation

◆ SDL_AudioDeviceID

SDL Audio Device instance IDs.

Zero is used to signify an invalid/null device.

Since
This datatype is available since SDL 3.2.0.

Definition at line 374 of file SDL_audio.h.

◆ SDL_AudioPostmixCallback

typedef void(* SDL_AudioPostmixCallback) (void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen)

A callback that fires when data is about to be fed to an audio device.

This is useful for accessing the final mix, perhaps for writing a visualizer or applying a final effect to the audio data before playback.

This callback should run as quickly as possible and not block for any significant time, as this callback delays submission of data to the audio device, which can cause audio playback problems.

The postmix callback must be able to handle any audio data format specified in spec, which can change between callbacks if the audio device changed. However, this only covers frequency and channel count; data is always provided here in SDL_AUDIO_F32 format.

The postmix callback runs after logical device gain and audiostream gain have been applied, which is to say you can make the output data louder at this point than the gain settings would suggest.

Parameters
userdataa pointer provided by the app through SDL_SetAudioPostmixCallback, for its own use.
specthe current format of audio that is to be submitted to the audio device.
bufferthe buffer of audio samples to be submitted. The callback can inspect and/or modify this data.
buflenthe size of buffer in bytes.

\threadsafety This will run from a background thread owned by SDL. The application is responsible for locking resources the callback touches that need to be protected.

Since
This datatype is available since SDL 3.2.0.
See also
SDL_SetAudioPostmixCallback

Definition at line 2093 of file SDL_audio.h.

◆ SDL_AudioStream

The opaque handle that represents an audio stream.

SDL_AudioStream is an audio conversion interface.

  • It can handle resampling data in chunks without generating artifacts, when it doesn't have the complete buffer available.
  • It can handle incoming data in any variable size.
  • It can handle input/output format changes on the fly.
  • It can remap audio channels between inputs and outputs.
  • You push data as you have it, and pull it when you need it; the stream will buffer data as needed.
  • It can also function as a basic audio data queue even if you just have sound that needs to pass from one place to another.
  • You can hook callbacks up to them when more data is added or requested, to manage data on-the-fly.

Audio streams are the core of the SDL3 audio interface. You create one or more of them, bind them to an opened audio device, and feed data to them (or for recording, consume data from them).

Since
This struct is available since SDL 3.2.0.
See also
SDL_CreateAudioStream

Definition at line 452 of file SDL_audio.h.

◆ SDL_AudioStreamCallback

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

A callback that fires when data passes through an SDL_AudioStream.

Apps can (optionally) register a callback with an audio stream that is called when data is added with SDL_PutAudioStreamData, or requested with SDL_GetAudioStreamData.

Two values are offered here: one is the amount of additional data needed to satisfy the immediate request (which might be zero if the stream already has enough data queued) and the other is the total amount being requested. 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.

This callback is not required to do anything. 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.

Parameters
streamthe SDL audio stream associated with this callback.
additional_amountthe amount of data, in bytes, that is needed right now.
total_amountthe total amount of data requested, in bytes, that is requested or available.
userdataan opaque pointer provided by the app for their personal use.

\threadsafety This callbacks may run from any thread, so if you need to protect shared data, you should use SDL_LockAudioStream to serialize access; this lock will be held before your callback is called, so your callback does not need to manage the lock explicitly.

Since
This datatype is available since SDL 3.2.0.
See also
SDL_SetAudioStreamGetCallback
SDL_SetAudioStreamPutCallback

Definition at line 1876 of file SDL_audio.h.

◆ SDL_AudioStreamDataCompleteCallback

typedef void(* SDL_AudioStreamDataCompleteCallback) (void *userdata, const void *buf, int buflen)

A callback that fires for completed SDL_PutAudioStreamDataNoCopy() data.

When using SDL_PutAudioStreamDataNoCopy() to provide data to an SDL_AudioStream, it's not safe to dispose of the data until the stream has completely consumed it. Often times it's difficult to know exactly when this has happened.

This callback fires once when the stream no longer needs the buffer, allowing the app to easily free or reuse it.

Parameters
userdataan opaque pointer provided by the app for their personal use.
bufthe pointer provided to SDL_PutAudioStreamDataNoCopy().
buflenthe size of buffer, in bytes, provided to SDL_PutAudioStreamDataNoCopy().

\threadsafety This callbacks may run from any thread, so if you need to protect shared data, you should use SDL_LockAudioStream to serialize access; this lock will be held before your callback is called, so your callback does not need to manage the lock explicitly.

Since
This datatype is available since SDL 3.4.0.
See also
SDL_SetAudioStreamGetCallback
SDL_SetAudioStreamPutCallback

Definition at line 1483 of file SDL_audio.h.

Enumeration Type Documentation

◆ SDL_AudioFormat

Audio format.

Since
This enum is available since SDL 3.2.0.
See also
SDL_AUDIO_BITSIZE
SDL_AUDIO_BYTESIZE
SDL_AUDIO_ISINT
SDL_AUDIO_ISFLOAT
SDL_AUDIO_ISBIGENDIAN
SDL_AUDIO_ISLITTLEENDIAN
SDL_AUDIO_ISSIGNED
SDL_AUDIO_ISUNSIGNED
Enumerator
SDL_AUDIO_UNKNOWN 

Unspecified audio format

SDL_AUDIO_U8 

Unsigned 8-bit samples

SDL_AUDIO_S8 

Signed 8-bit samples

SDL_AUDIO_S16LE 

Signed 16-bit samples

SDL_AUDIO_S16BE 

As above, but big-endian byte order

SDL_AUDIO_S32LE 

32-bit integer samples

SDL_AUDIO_S32BE 

As above, but big-endian byte order

SDL_AUDIO_F32LE 

32-bit floating point samples

SDL_AUDIO_F32BE 

As above, but big-endian byte order

SDL_AUDIO_S16 
SDL_AUDIO_S32 
SDL_AUDIO_F32 

Definition at line 221 of file SDL_audio.h.

222{
223 SDL_AUDIO_UNKNOWN = 0x0000u, /**< Unspecified audio format */
224 SDL_AUDIO_U8 = 0x0008u, /**< Unsigned 8-bit samples */
225 /* SDL_DEFINE_AUDIO_FORMAT(0, 0, 0, 8), */
226 SDL_AUDIO_S8 = 0x8008u, /**< Signed 8-bit samples */
227 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 8), */
228 SDL_AUDIO_S16LE = 0x8010u, /**< Signed 16-bit samples */
229 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 16), */
230 SDL_AUDIO_S16BE = 0x9010u, /**< As above, but big-endian byte order */
231 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 16), */
232 SDL_AUDIO_S32LE = 0x8020u, /**< 32-bit integer samples */
233 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 32), */
234 SDL_AUDIO_S32BE = 0x9020u, /**< As above, but big-endian byte order */
235 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 32), */
236 SDL_AUDIO_F32LE = 0x8120u, /**< 32-bit floating point samples */
237 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 1, 32), */
238 SDL_AUDIO_F32BE = 0x9120u, /**< As above, but big-endian byte order */
239 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 1, 32), */
240
241 /* These represent the current system's byteorder. */
242 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
246 #else
250 #endif

Function Documentation

◆ SDL_AudioDevicePaused()

bool SDL_AudioDevicePaused ( SDL_AudioDeviceID  devid)
extern

Use this function to query if an audio device is paused.

Unlike in SDL2, audio devices start in an unpaused state, since an app has to bind a stream before any audio will flow.

Physical devices can not be paused or unpaused, only logical devices created through SDL_OpenAudioDevice() can be. Physical and invalid device IDs will report themselves as unpaused here.

Parameters
devida device opened by SDL_OpenAudioDevice().
Returns
true if device is valid and paused, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_PauseAudioDevice
SDL_ResumeAudioDevice

◆ SDL_AudioStreamDevicePaused()

bool SDL_AudioStreamDevicePaused ( SDL_AudioStream stream)
extern

Use this function to query if an audio device associated with a stream is paused.

Unlike in SDL2, audio devices start in an unpaused state, since an app has to bind a stream before any audio will flow.

Parameters
streamthe audio stream associated with the audio device to query.
Returns
true if device is valid and paused, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_PauseAudioStreamDevice
SDL_ResumeAudioStreamDevice

◆ SDL_BindAudioStream()

bool SDL_BindAudioStream ( SDL_AudioDeviceID  devid,
SDL_AudioStream stream 
)
extern

Bind a single audio stream to an audio device.

This is a convenience function, equivalent to calling SDL_BindAudioStreams(devid, &stream, 1).

Parameters
devidan audio device to bind a stream to.
streaman audio stream to bind to a device.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_BindAudioStreams
SDL_UnbindAudioStream
SDL_GetAudioStreamDevice

◆ SDL_BindAudioStreams()

bool SDL_BindAudioStreams ( SDL_AudioDeviceID  devid,
SDL_AudioStream *const *  streams,
int  num_streams 
)
extern

Bind a list of audio streams to an audio device.

Audio data will flow through any bound streams. For a playback device, data for all bound streams will be mixed together and fed to the device. For a recording device, a copy of recorded data will be provided to each bound stream.

Audio streams can only be bound to an open device. This operation is atomic–all streams bound in the same call will start processing at the same time, so they can stay in sync. Also: either all streams will be bound or none of them will be.

It is an error to bind an already-bound stream; it must be explicitly unbound first.

Binding a stream to a device will set its output format for playback devices, and its input format for recording devices, so they match the device's settings. The caller is welcome to change the other end of the stream's format at any time with SDL_SetAudioStreamFormat(). If the other end of the stream's format has never been set (the audio stream was created with a NULL audio spec), this function will set it to match the device end's format.

Parameters
devidan audio device to bind a stream to.
streamsan array of audio streams to bind.
num_streamsnumber streams listed in the streams array.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_BindAudioStreams
SDL_UnbindAudioStream
SDL_GetAudioStreamDevice

◆ SDL_ClearAudioStream()

bool SDL_ClearAudioStream ( SDL_AudioStream stream)
extern

Clear any pending data in the stream.

This drops any queued data, so there will be nothing to read from the stream until more is added.

Parameters
streamthe audio stream to clear.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_GetAudioStreamAvailable
SDL_GetAudioStreamData
SDL_GetAudioStreamQueued
SDL_PutAudioStreamData

◆ SDL_CloseAudioDevice()

void SDL_CloseAudioDevice ( SDL_AudioDeviceID  devid)
extern

Close a previously-opened audio device.

The application should close open audio devices once they are no longer needed.

This function may block briefly while pending audio data is played by the hardware, so that applications don't drop the last buffer of data they supplied if terminating immediately afterwards.

Parameters
devidan audio device id previously returned by SDL_OpenAudioDevice().

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_OpenAudioDevice

◆ SDL_ConvertAudioSamples()

bool SDL_ConvertAudioSamples ( const SDL_AudioSpec src_spec,
const Uint8 src_data,
int  src_len,
const SDL_AudioSpec dst_spec,
Uint8 **  dst_data,
int *  dst_len 
)
extern

Convert some audio data of one format to another format.

Please note that this function is for convenience, but should not be used to resample audio in blocks, as it will introduce audio artifacts on the boundaries. You should only use this function if you are converting audio data in its entirety in one call. If you want to convert audio in smaller chunks, use an SDL_AudioStream, which is designed for this situation.

Internally, this function creates and destroys an SDL_AudioStream on each use, so it's also less efficient than using one directly, if you need to convert multiple times.

Parameters
src_specthe format details of the input audio.
src_datathe audio data to be converted.
src_lenthe len of src_data.
dst_specthe format details of the output audio.
dst_datawill be filled with a pointer to converted audio data, which should be freed with SDL_free(). On error, it will be NULL.
dst_lenwill be filled with the len of dst_data.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

◆ SDL_CreateAudioStream()

SDL_AudioStream * SDL_CreateAudioStream ( const SDL_AudioSpec src_spec,
const SDL_AudioSpec dst_spec 
)
extern

Create a new audio stream.

Note that src_spec or dst_spec may be NULL, but any attempts to put or get data from an audio stream will fail until it has valid specs assigned to both ends of the stream. Specs can be assigned later through SDL_SetAudioStreamFormat(), or binding the stream to an audio device (which will set the format of only the input or output, depending on what kind of device the stream was bound to).

Parameters
src_specthe format details of the input audio. May be NULL.
dst_specthe format details of the output audio. May be NULL.
Returns
a new audio stream on success or NULL on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_PutAudioStreamData
SDL_GetAudioStreamData
SDL_GetAudioStreamAvailable
SDL_FlushAudioStream
SDL_ClearAudioStream
SDL_SetAudioStreamFormat
SDL_DestroyAudioStream

◆ SDL_DestroyAudioStream()

void SDL_DestroyAudioStream ( SDL_AudioStream stream)
extern

Free an audio stream.

This will release all allocated data, including any audio that is still queued. You do not need to manually clear the stream first.

If this stream was bound to an audio device, it is unbound during this call. If this stream was created with SDL_OpenAudioDeviceStream, the audio device that was opened alongside this stream's creation will be closed, too.

Parameters
streamthe audio stream to destroy.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_CreateAudioStream

◆ SDL_FlushAudioStream()

bool SDL_FlushAudioStream ( SDL_AudioStream stream)
extern

Tell the stream that you're done sending data, and anything being buffered should be converted/resampled and made available immediately.

It is legal to add more data to a stream after flushing, but there may be audio gaps in the output. Generally this is intended to signal the end of input, so the complete output becomes available.

Parameters
streamthe audio stream to flush.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_PutAudioStreamData

◆ SDL_GetAudioDeviceChannelMap()

int * SDL_GetAudioDeviceChannelMap ( SDL_AudioDeviceID  devid,
int *  count 
)
extern

Get the current channel map of an audio device.

Channel maps are optional; most things do not need them, instead passing data in the order that SDL expects.

Audio devices usually have no remapping applied. This is represented by returning NULL, and does not signify an error.

Parameters
devidthe instance ID of the device to query.
countOn output, set to number of channels in the map. Can be NULL.
Returns
an array of the current channel mapping, with as many elements as the current output spec's channels, or NULL if default. This should be freed with SDL_free() when it is no longer needed.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_SetAudioStreamInputChannelMap

◆ SDL_GetAudioDeviceFormat()

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

Get the current audio format of a specific audio device.

For an opened device, this will report the format the device is currently using. If the device isn't yet opened, this will report the device's preferred format (or a reasonable default if this can't be determined).

You may also specify SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or SDL_AUDIO_DEVICE_DEFAULT_RECORDING here, which is useful for getting a reasonable recommendation before opening the system-recommended default device.

You can also use this to request the current device buffer size. This is specified in sample frames and represents the amount of data SDL will feed to the physical hardware in each chunk. This can be converted to milliseconds of audio with the following equation:

ms = (int) ((((Sint64) frames) * 1000) / spec.freq);

Buffer size is only important if you need low-level control over the audio playback timing. Most apps do not need this.

Parameters
devidthe instance ID of the device to query.
specon return, will be filled with device details.
sample_framespointer to store device buffer size, in sample frames. Can be NULL.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

◆ SDL_GetAudioDeviceGain()

float SDL_GetAudioDeviceGain ( SDL_AudioDeviceID  devid)
extern

Get the gain of an audio device.

The gain of a device is its volume; a larger gain means a louder output, with a gain of zero being silence.

Audio devices default to a gain of 1.0f (no change in output).

Physical devices may not have their gain changed, only logical devices, and this function will always return -1.0f when used on physical devices.

Parameters
devidthe audio device to query.
Returns
the gain of the device or -1.0f on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_SetAudioDeviceGain

◆ SDL_GetAudioDeviceName()

const char * SDL_GetAudioDeviceName ( SDL_AudioDeviceID  devid)
extern

Get the human-readable name of a specific audio device.

WARNING: this function will work with SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK and SDL_AUDIO_DEVICE_DEFAULT_RECORDING, returning the current default physical devices' names. However, as the default device may change at any time, it is likely better to show a generic name to the user, like "System default audio device" or perhaps "default [currently %s]". Do not store this name to disk to reidentify the device in a later run of the program, as the default might change in general, and the string will be the name of a specific device and not the abstract system default.

Parameters
devidthe instance ID of the device to query.
Returns
the name of the audio device, or NULL on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_GetAudioPlaybackDevices
SDL_GetAudioRecordingDevices

◆ SDL_GetAudioDriver()

const char * SDL_GetAudioDriver ( int  index)
extern

Use this function to get the name of a built in audio driver.

The list of audio drivers is given in the order that they are normally initialized by default; the drivers that seem more reasonable to choose first (as far as the SDL developers believe) are earlier in the list.

The names of drivers are all simple, low-ASCII identifiers, like "alsa", "coreaudio" or "wasapi". These never have Unicode characters, and are not meant to be proper names.

Parameters
indexthe index of the audio driver; the value ranges from 0 to SDL_GetNumAudioDrivers() - 1.
Returns
the name of the audio driver at the requested index, or NULL if an invalid index was specified.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_GetNumAudioDrivers

◆ SDL_GetAudioFormatName()

const char * SDL_GetAudioFormatName ( SDL_AudioFormat  format)
extern

Get the human readable name of an audio format.

Parameters
formatthe audio format to query.
Returns
the human readable name of the specified audio format or "SDL_AUDIO_UNKNOWN" if the format isn't recognized.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

◆ SDL_GetAudioPlaybackDevices()

SDL_AudioDeviceID * SDL_GetAudioPlaybackDevices ( int *  count)
extern

Get a list of currently-connected audio playback devices.

This returns of list of available devices that play sound, perhaps to speakers or headphones ("playback" devices). If you want devices that record audio, like a microphone ("recording" devices), use SDL_GetAudioRecordingDevices() instead.

This only returns a list of physical devices; it will not have any device IDs returned by SDL_OpenAudioDevice().

If this function returns NULL, to signify an error, *count will be set to zero.

Parameters
counta pointer filled in with the number of devices returned, may be NULL.
Returns
a 0 terminated array of device instance IDs or NULL on error; call SDL_GetError() for more information. This should be freed with SDL_free() when it is no longer needed.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_OpenAudioDevice
SDL_GetAudioRecordingDevices

◆ SDL_GetAudioRecordingDevices()

SDL_AudioDeviceID * SDL_GetAudioRecordingDevices ( int *  count)
extern

Get a list of currently-connected audio recording devices.

This returns of list of available devices that record audio, like a microphone ("recording" devices). If you want devices that play sound, perhaps to speakers or headphones ("playback" devices), use SDL_GetAudioPlaybackDevices() instead.

This only returns a list of physical devices; it will not have any device IDs returned by SDL_OpenAudioDevice().

If this function returns NULL, to signify an error, *count will be set to zero.

Parameters
counta pointer filled in with the number of devices returned, may be NULL.
Returns
a 0 terminated array of device instance IDs, or NULL on failure; call SDL_GetError() for more information. This should be freed with SDL_free() when it is no longer needed.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_OpenAudioDevice
SDL_GetAudioPlaybackDevices

◆ SDL_GetAudioStreamAvailable()

int SDL_GetAudioStreamAvailable ( SDL_AudioStream stream)
extern

Get the number of converted/resampled bytes available.

The stream may be buffering data behind the scenes until it has enough to resample correctly, so this number might be lower than what you expect, or even be zero. Add more data or flush the stream if you need the data now.

If the stream has so much data that it would overflow an int, the return value is clamped to a maximum value, but no queued data is lost; if there are gigabytes of data queued, the app might need to read some of it with SDL_GetAudioStreamData before this function's return value is no longer clamped.

Parameters
streamthe audio stream to query.
Returns
the number of converted/resampled bytes available or -1 on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_GetAudioStreamData
SDL_PutAudioStreamData

◆ SDL_GetAudioStreamData()

int SDL_GetAudioStreamData ( SDL_AudioStream stream,
void *  buf,
int  len 
)
extern

Get converted/resampled data from the stream.

The input/output data format/channels/samplerate is specified when creating the stream, and can be changed after creation by calling SDL_SetAudioStreamFormat.

Note that any conversion and resampling necessary is done during this call, and SDL_PutAudioStreamData simply queues unconverted data for later. This is different than SDL2, where that work was done while inputting new data to the stream and requesting the output just copied the converted data.

Parameters
streamthe stream the audio is being requested from.
bufa buffer to fill with audio data.
lenthe maximum number of bytes to fill.
Returns
the number of bytes read from the stream or -1 on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, but if the stream has a callback set, the caller might need to manage extra locking.

Since
This function is available since SDL 3.2.0.
See also
SDL_ClearAudioStream
SDL_GetAudioStreamAvailable
SDL_PutAudioStreamData

◆ SDL_GetAudioStreamDevice()

SDL_AudioDeviceID SDL_GetAudioStreamDevice ( SDL_AudioStream stream)
extern

Query an audio stream for its currently-bound device.

This reports the logical audio device that an audio stream is currently bound to.

If not bound, or invalid, this returns zero, which is not a valid device ID.

Parameters
streamthe audio stream to query.
Returns
the bound audio device, or 0 if not bound or invalid.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_BindAudioStream
SDL_BindAudioStreams

◆ SDL_GetAudioStreamFormat()

bool SDL_GetAudioStreamFormat ( SDL_AudioStream stream,
SDL_AudioSpec src_spec,
SDL_AudioSpec dst_spec 
)
extern

Query the current format of an audio stream.

Parameters
streamthe SDL_AudioStream to query.
src_specwhere to store the input audio format; ignored if NULL.
dst_specwhere to store the output audio format; ignored if NULL.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, as it holds a stream-specific mutex while running.

Since
This function is available since SDL 3.2.0.
See also
SDL_SetAudioStreamFormat

◆ SDL_GetAudioStreamFrequencyRatio()

float SDL_GetAudioStreamFrequencyRatio ( SDL_AudioStream stream)
extern

Get the frequency ratio of an audio stream.

Parameters
streamthe SDL_AudioStream to query.
Returns
the frequency ratio of the stream or 0.0 on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, as it holds a stream-specific mutex while running.

Since
This function is available since SDL 3.2.0.
See also
SDL_SetAudioStreamFrequencyRatio

◆ SDL_GetAudioStreamGain()

float SDL_GetAudioStreamGain ( SDL_AudioStream stream)
extern

Get the gain of an audio stream.

The gain of a stream is its volume; a larger gain means a louder output, with a gain of zero being silence.

Audio streams default to a gain of 1.0f (no change in output).

Parameters
streamthe SDL_AudioStream to query.
Returns
the gain of the stream or -1.0f on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, as it holds a stream-specific mutex while running.

Since
This function is available since SDL 3.2.0.
See also
SDL_SetAudioStreamGain

◆ SDL_GetAudioStreamInputChannelMap()

int * SDL_GetAudioStreamInputChannelMap ( SDL_AudioStream stream,
int *  count 
)
extern

Get the current input channel map of an audio stream.

Channel maps are optional; most things do not need them, instead passing data in the order that SDL expects.

Audio streams default to no remapping applied. This is represented by returning NULL, and does not signify an error.

Parameters
streamthe SDL_AudioStream to query.
countOn output, set to number of channels in the map. Can be NULL.
Returns
an array of the current channel mapping, with as many elements as the current output spec's channels, or NULL if default. This should be freed with SDL_free() when it is no longer needed.

\threadsafety It is safe to call this function from any thread, as it holds a stream-specific mutex while running.

Since
This function is available since SDL 3.2.0.
See also
SDL_SetAudioStreamInputChannelMap

◆ SDL_GetAudioStreamOutputChannelMap()

int * SDL_GetAudioStreamOutputChannelMap ( SDL_AudioStream stream,
int *  count 
)
extern

Get the current output channel map of an audio stream.

Channel maps are optional; most things do not need them, instead passing data in the order that SDL expects.

Audio streams default to no remapping applied. This is represented by returning NULL, and does not signify an error.

Parameters
streamthe SDL_AudioStream to query.
countOn output, set to number of channels in the map. Can be NULL.
Returns
an array of the current channel mapping, with as many elements as the current output spec's channels, or NULL if default. This should be freed with SDL_free() when it is no longer needed.

\threadsafety It is safe to call this function from any thread, as it holds a stream-specific mutex while running.

Since
This function is available since SDL 3.2.0.
See also
SDL_SetAudioStreamInputChannelMap

◆ SDL_GetAudioStreamProperties()

SDL_PropertiesID SDL_GetAudioStreamProperties ( SDL_AudioStream stream)
extern

Get the properties associated with an audio stream.

The application can hang any data it wants here, but the following properties are understood by SDL:

  • SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN: if true (the default), the stream be automatically cleaned up when the audio subsystem quits. If set to false, the streams will persist beyond that. This property is ignored for streams created through SDL_OpenAudioDeviceStream(), and will always be cleaned up. Streams that are not cleaned up will still be unbound from devices when the audio subsystem quits. This property was added in SDL 3.4.0.
Parameters
streamthe SDL_AudioStream to query.
Returns
a valid property ID on success or 0 on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

◆ SDL_GetAudioStreamQueued()

int SDL_GetAudioStreamQueued ( SDL_AudioStream stream)
extern

Get the number of bytes currently queued.

This is the number of bytes put into a stream as input, not the number that can be retrieved as output. Because of several details, it's not possible to calculate one number directly from the other. If you need to know how much usable data can be retrieved right now, you should use SDL_GetAudioStreamAvailable() and not this function.

Note that audio streams can change their input format at any time, even if there is still data queued in a different format, so the returned byte count will not necessarily match the number of sample frames available. Users of this API should be aware of format changes they make when feeding a stream and plan accordingly.

Queued data is not converted until it is consumed by SDL_GetAudioStreamData, so this value should be representative of the exact data that was put into the stream.

If the stream has so much data that it would overflow an int, the return value is clamped to a maximum value, but no queued data is lost; if there are gigabytes of data queued, the app might need to read some of it with SDL_GetAudioStreamData before this function's return value is no longer clamped.

Parameters
streamthe audio stream to query.
Returns
the number of bytes queued or -1 on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_PutAudioStreamData
SDL_ClearAudioStream

◆ SDL_GetCurrentAudioDriver()

const char * SDL_GetCurrentAudioDriver ( void  )
extern

Get the name of the current audio driver.

The names of drivers are all simple, low-ASCII identifiers, like "alsa", "coreaudio" or "wasapi". These never have Unicode characters, and are not meant to be proper names.

Returns
the name of the current audio driver or NULL if no driver has been initialized.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

◆ SDL_GetNumAudioDrivers()

int SDL_GetNumAudioDrivers ( void  )
extern

Use this function to get the number of built-in audio drivers.

This function returns a hardcoded number. This never returns a negative value; if there are no drivers compiled into this build of SDL, this function returns zero. The presence of a driver in this list does not mean it will function, it just means SDL is capable of interacting with that interface. For example, a build of SDL might have esound support, but if there's no esound server available, SDL's esound driver would fail if used.

By default, SDL tries all drivers, in its preferred order, until one is found to be usable.

Returns
the number of built-in audio drivers.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_GetAudioDriver

◆ SDL_GetSilenceValueForFormat()

int SDL_GetSilenceValueForFormat ( SDL_AudioFormat  format)
extern

Get the appropriate memset value for silencing an audio format.

The value returned by this function can be used as the second argument to memset (or SDL_memset) to set an audio buffer in a specific format to silence.

Parameters
formatthe audio data format to query.
Returns
a byte value that can be passed to memset.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

◆ SDL_IsAudioDevicePhysical()

bool SDL_IsAudioDevicePhysical ( SDL_AudioDeviceID  devid)
extern

Determine if an audio device is physical (instead of logical).

An SDL_AudioDeviceID that represents physical hardware is a physical device; there is one for each piece of hardware that SDL can see. Logical devices are created by calling SDL_OpenAudioDevice or SDL_OpenAudioDeviceStream, and while each is associated with a physical device, there can be any number of logical devices on one physical device.

For the most part, logical and physical IDs are interchangeable–if you try to open a logical device, SDL understands to assign that effort to the underlying physical device, etc. However, it might be useful to know if an arbitrary device ID is physical or logical. This function reports which.

This function may return either true or false for invalid device IDs.

Parameters
devidthe device ID to query.
Returns
true if devid is a physical device, false if it is logical.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

◆ SDL_IsAudioDevicePlayback()

bool SDL_IsAudioDevicePlayback ( SDL_AudioDeviceID  devid)
extern

Determine if an audio device is a playback device (instead of recording).

This function may return either true or false for invalid device IDs.

Parameters
devidthe device ID to query.
Returns
true if devid is a playback device, false if it is recording.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

◆ SDL_LoadWAV()

bool SDL_LoadWAV ( const char *  path,
SDL_AudioSpec spec,
Uint8 **  audio_buf,
Uint32 audio_len 
)
extern

Loads a WAV from a file path.

This is a convenience function that is effectively the same as:

SDL_LoadWAV_IO(SDL_IOFromFile(path, "rb"), true, spec, audio_buf, audio_len);
SDL_IOStream * SDL_IOFromFile(const char *file, const char *mode)
Parameters
paththe file path of the WAV file to open.
speca pointer to an SDL_AudioSpec that will be set to the WAVE data's format details on successful return.
audio_bufa pointer filled with the audio data, allocated by the function.
audio_lena pointer filled with the length of the audio data buffer in bytes.
Returns
true on success. audio_buf will be filled with a pointer to an allocated buffer containing the audio data, and audio_len is filled with the length of that audio buffer in bytes.

This function returns false if the .WAV file cannot be opened, uses an unknown data format, or is corrupt; call SDL_GetError() for more information.

When the application is done with the data returned in audio_buf, it should call SDL_free() to dispose of it.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_free
SDL_LoadWAV_IO

◆ SDL_LoadWAV_IO()

bool SDL_LoadWAV_IO ( SDL_IOStream src,
bool  closeio,
SDL_AudioSpec spec,
Uint8 **  audio_buf,
Uint32 audio_len 
)
extern

Load the audio data of a WAVE file into memory.

Loading a WAVE file requires src, spec, audio_buf and audio_len to be valid pointers. The entire data portion of the file is then loaded into memory and decoded if necessary.

Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and A-law and mu-law (8 bits). Other formats are currently unsupported and cause an error.

If this function succeeds, the return value is zero and the pointer to the audio data allocated by the function is written to audio_buf and its length in bytes to audio_len. The SDL_AudioSpec members freq, channels, and format are set to the values of the audio data in the buffer.

It's necessary to use SDL_free() to free the audio data returned in audio_buf when it is no longer used.

Because of the underspecification of the .WAV format, there are many problematic files in the wild that cause issues with strict decoders. To provide compatibility with these files, this decoder is lenient in regards to the truncation of the file, the fact chunk, and the size of the RIFF chunk. The hints SDL_HINT_WAVE_RIFF_CHUNK_SIZE, SDL_HINT_WAVE_TRUNCATION, and SDL_HINT_WAVE_FACT_CHUNK can be used to tune the behavior of the loading process.

Any file that is invalid (due to truncation, corruption, or wrong values in the headers), too big, or unsupported causes an error. Additionally, any critical I/O error from the data source will terminate the loading process with an error. The function returns NULL on error and in all cases (with the exception of src being NULL), an appropriate error message will be set.

It is required that the data source supports seeking.

Example:

SDL_LoadWAV_IO(SDL_IOFromFile("sample.wav", "rb"), true, &spec, &buf, &len);

Note that the SDL_LoadWAV function does this same thing for you, but in a less messy way:

SDL_LoadWAV("sample.wav", &spec, &buf, &len);
Parameters
srcthe data source for the WAVE data.
closeioif true, calls SDL_CloseIO() on src before returning, even in the case of an error.
speca pointer to an SDL_AudioSpec that will be set to the WAVE data's format details on successful return.
audio_bufa pointer filled with the audio data, allocated by the function.
audio_lena pointer filled with the length of the audio data buffer in bytes.
Returns
true on success. audio_buf will be filled with a pointer to an allocated buffer containing the audio data, and audio_len is filled with the length of that audio buffer in bytes.

This function returns false if the .WAV file cannot be opened, uses an unknown data format, or is corrupt; call SDL_GetError() for more information.

When the application is done with the data returned in audio_buf, it should call SDL_free() to dispose of it.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_free
SDL_LoadWAV

◆ SDL_LockAudioStream()

bool SDL_LockAudioStream ( SDL_AudioStream stream)
extern

Lock an audio stream for serialized access.

Each SDL_AudioStream has an internal mutex it uses to protect its data structures from threading conflicts. This function allows an app to lock that mutex, which could be useful if registering callbacks on this stream.

One does not need to lock a stream to use in it most cases, as the stream manages this lock internally. However, this lock is held during callbacks, which may run from arbitrary threads at any time, so if an app needs to protect shared data during those callbacks, locking the stream guarantees that the callback is not running while the lock is held.

As this is just a wrapper over SDL_LockMutex for an internal lock; it has all the same attributes (recursive locks are allowed, etc).

Parameters
streamthe audio stream to lock.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_UnlockAudioStream

◆ SDL_MixAudio()

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

Mix audio data in a specified format.

This takes an audio buffer src of len bytes of format data and mixes it into dst, performing addition, volume adjustment, and overflow clipping. The buffer pointed to by dst must also be len bytes of format data.

This is provided for convenience – you can mix your own audio data.

Do not use this function for mixing together more than two streams of sample data. The output from repeated application of this function may be distorted by clipping, because there is no accumulator with greater range than the input (not to mention this being an inefficient way of doing it).

It is a common misconception that this function is required to write audio data to an output stream in an audio callback. While you can do that, SDL_MixAudio() is really only needed when you're mixing a single audio stream with a volume adjustment.

Parameters
dstthe destination for the mixed audio.
srcthe source audio buffer to be mixed.
formatthe SDL_AudioFormat structure representing the desired audio format.
lenthe length of the audio buffer in bytes.
volumeranges from 0.0 - 1.0, and should be set to 1.0 for full audio volume.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

◆ SDL_OpenAudioDevice()

SDL_AudioDeviceID SDL_OpenAudioDevice ( SDL_AudioDeviceID  devid,
const SDL_AudioSpec spec 
)
extern

Open a specific audio device.

You can open both playback and recording devices through this function. Playback devices will take data from bound audio streams, mix it, and send it to the hardware. Recording devices will feed any bound audio streams with a copy of any incoming data.

An opened audio device starts out with no audio streams bound. To start audio playing, bind a stream and supply audio data to it. Unlike SDL2, there is no audio callback; you only bind audio streams and make sure they have data flowing into them (however, you can simulate SDL2's semantics fairly closely by using SDL_OpenAudioDeviceStream instead of this function).

If you don't care about opening a specific device, pass a devid of either SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or SDL_AUDIO_DEVICE_DEFAULT_RECORDING. In this case, SDL will try to pick the most reasonable default, and may also switch between physical devices seamlessly later, if the most reasonable default changes during the lifetime of this opened device (user changed the default in the OS's system preferences, the default got unplugged so the system jumped to a new default, the user plugged in headphones on a mobile device, etc). Unless you have a good reason to choose a specific device, this is probably what you want.

You may request a specific format for the audio device, but there is no promise the device will honor that request for several reasons. As such, it's only meant to be a hint as to what data your app will provide. Audio streams will accept data in whatever format you specify and manage conversion for you as appropriate. SDL_GetAudioDeviceFormat can tell you the preferred format for the device before opening and the actual format the device is using after opening.

It's legal to open the same device ID more than once; each successful open will generate a new logical SDL_AudioDeviceID that is managed separately from others on the same physical device. This allows libraries to open a device separately from the main app and bind its own streams without conflicting.

It is also legal to open a device ID returned by a previous call to this function; doing so just creates another logical device on the same physical device. This may be useful for making logical groupings of audio streams.

This function returns the opened device ID on success. This is a new, unique SDL_AudioDeviceID that represents a logical device.

Some backends might offer arbitrary devices (for example, a networked audio protocol that can connect to an arbitrary server). For these, as a change from SDL2, you should open a default device ID and use an SDL hint to specify the target if you care, or otherwise let the backend figure out a reasonable default. Most backends don't offer anything like this, and often this would be an end user setting an environment variable for their custom need, and not something an application should specifically manage.

When done with an audio device, possibly at the end of the app's life, one should call SDL_CloseAudioDevice() on the returned device id.

Parameters
devidthe device instance id to open, or SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or SDL_AUDIO_DEVICE_DEFAULT_RECORDING for the most reasonable default device.
specthe requested device configuration. Can be NULL to use reasonable defaults.
Returns
the device ID on success or 0 on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_CloseAudioDevice
SDL_GetAudioDeviceFormat

◆ SDL_OpenAudioDeviceStream()

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

Convenience function for straightforward audio init for the common case.

If all your app intends to do is provide a single source of PCM audio, this function allows you to do all your audio setup in a single call.

This is also intended to be a clean means to migrate apps from SDL2.

This function will open an audio device, create a stream and bind it. Unlike other methods of setup, the audio device will be closed when this stream is destroyed, so the app can treat the returned SDL_AudioStream as the only object needed to manage audio playback.

Also unlike other functions, the audio device begins paused. This is to map more closely to SDL2-style behavior, since there is no extra step here to bind a stream to begin audio flowing. The audio device should be resumed with SDL_ResumeAudioStreamDevice().

This function works with both playback and recording devices.

The spec parameter represents the app's side of the audio stream. That is, for recording audio, this will be the output format, and for playing audio, this will be the input format. If spec is NULL, the system will choose the format, and the app can use SDL_GetAudioStreamFormat() to obtain this information later.

If you don't care about opening a specific audio device, you can (and probably should), use SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK for playback and SDL_AUDIO_DEVICE_DEFAULT_RECORDING for recording.

One can optionally provide a callback function; if NULL, the app is expected to queue audio data for playback (or unqueue audio data if capturing). Otherwise, the callback will begin to fire once the device is unpaused.

Destroying the returned stream with SDL_DestroyAudioStream will also close the audio device associated with this stream.

Parameters
devidan audio device to open, or SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or SDL_AUDIO_DEVICE_DEFAULT_RECORDING.
specthe audio stream's data format. Can be NULL.
callbacka callback where the app will provide new data for playback, or receive new data for recording. Can be NULL, in which case the app will need to call SDL_PutAudioStreamData or SDL_GetAudioStreamData as necessary.
userdataapp-controlled pointer passed to callback. Can be NULL. Ignored if callback is NULL.
Returns
an audio stream on success, ready to use, or NULL on failure; call SDL_GetError() for more information. When done with this stream, call SDL_DestroyAudioStream to free resources and close the device.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_GetAudioStreamDevice
SDL_ResumeAudioStreamDevice

◆ SDL_PauseAudioDevice()

bool SDL_PauseAudioDevice ( SDL_AudioDeviceID  devid)
extern

Use this function to pause audio playback on a specified device.

This function pauses audio processing for a given device. Any bound audio streams will not progress, and no audio will be generated. Pausing one device does not prevent other unpaused devices from running.

Unlike in SDL2, audio devices start in an unpaused state, since an app has to bind a stream before any audio will flow. Pausing a paused device is a legal no-op.

Pausing a device can be useful to halt all audio without unbinding all the audio streams. This might be useful while a game is paused, or a level is loading, etc.

Physical devices can not be paused or unpaused, only logical devices created through SDL_OpenAudioDevice() can be.

Parameters
devida device opened by SDL_OpenAudioDevice().
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_ResumeAudioDevice
SDL_AudioDevicePaused

◆ SDL_PauseAudioStreamDevice()

bool SDL_PauseAudioStreamDevice ( SDL_AudioStream stream)
extern

Use this function to pause audio playback on the audio device associated with an audio stream.

This function pauses audio processing for a given device. Any bound audio streams will not progress, and no audio will be generated. Pausing one device does not prevent other unpaused devices from running.

Pausing a device can be useful to halt all audio without unbinding all the audio streams. This might be useful while a game is paused, or a level is loading, etc.

Parameters
streamthe audio stream associated with the audio device to pause.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_ResumeAudioStreamDevice

◆ SDL_PutAudioStreamData()

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

Add data to the stream.

This data must match the format/channels/samplerate specified in the latest call to SDL_SetAudioStreamFormat, or the format specified when creating the stream if it hasn't been changed.

Note that this call simply copies the unconverted data for later. This is different than SDL2, where data was converted during the Put call and the Get call would just dequeue the previously-converted data.

Parameters
streamthe stream the audio data is being added to.
bufa pointer to the audio data to add.
lenthe number of bytes to write to the stream.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, but if the stream has a callback set, the caller might need to manage extra locking.

Since
This function is available since SDL 3.2.0.
See also
SDL_ClearAudioStream
SDL_FlushAudioStream
SDL_GetAudioStreamData
SDL_GetAudioStreamQueued

◆ SDL_PutAudioStreamDataNoCopy()

bool SDL_PutAudioStreamDataNoCopy ( SDL_AudioStream stream,
const void *  buf,
int  len,
SDL_AudioStreamDataCompleteCallback  callback,
void *  userdata 
)
extern

Add external data to an audio stream without copying it.

Unlike SDL_PutAudioStreamData(), this function does not make a copy of the provided data, instead storing the provided pointer. This means that the put operation does not need to allocate and copy the data, but the original data must remain available until the stream is done with it, either by being read from the stream in its entirety, or a call to SDL_ClearAudioStream() or SDL_DestroyAudioStream().

The data must match the format/channels/samplerate specified in the latest call to SDL_SetAudioStreamFormat, or the format specified when creating the stream if it hasn't been changed.

An optional callback may be provided, which is called when the stream no longer needs the data. Once this callback fires, the stream will not access the data again. This callback will fire for any reason the data is no longer needed, including clearing or destroying the stream.

Note that there is still an allocation to store tracking information, so this function is more efficient for larger blocks of data. If you're planning to put a few samples at a time, it will be more efficient to use SDL_PutAudioStreamData(), which allocates and buffers in blocks.

Parameters
streamthe stream the audio data is being added to.
bufa pointer to the audio data to add.
lenthe number of bytes to add to the stream.
callbackthe callback function to call when the data is no longer needed by the stream. May be NULL.
userdataan opaque pointer provided to the callback for its own personal use.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, but if the stream has a callback set, the caller might need to manage extra locking.

Since
This function is available since SDL 3.4.0.
See also
SDL_ClearAudioStream
SDL_FlushAudioStream
SDL_GetAudioStreamData
SDL_GetAudioStreamQueued

◆ SDL_PutAudioStreamPlanarData()

bool SDL_PutAudioStreamPlanarData ( SDL_AudioStream stream,
const void *const *  channel_buffers,
int  num_channels,
int  num_samples 
)
extern

Add data to the stream with each channel in a separate array.

This data must match the format/channels/samplerate specified in the latest call to SDL_SetAudioStreamFormat, or the format specified when creating the stream if it hasn't been changed.

The data will be interleaved and queued. Note that SDL_AudioStream only operates on interleaved data, so this is simply a convenience function for easily queueing data from sources that provide separate arrays. There is no equivalent function to retrieve planar data.

The arrays in channel_buffers are ordered as they are to be interleaved; the first array will be the first sample in the interleaved data. Any individual array may be NULL; in this case, silence will be interleaved for that channel.

num_channels specifies how many arrays are in channel_buffers. This can be used as a safety to prevent overflow, in case the stream format has changed elsewhere. If more channels are specified than the current input spec, they are ignored. If less channels are specified, the missing arrays are treated as if they are NULL (silence is written to those channels). If the count is -1, SDL will assume the array count matches the current input spec.

Note that num_samples is the number of samples per array. This can also be thought of as the number of sample frames to be queued. A value of 1 with stereo arrays will queue two samples to the stream. This is different than SDL_PutAudioStreamData, which wants the size of a single array in bytes.

Parameters
streamthe stream the audio data is being added to.
channel_buffersa pointer to an array of arrays, one array per channel.
num_channelsthe number of arrays in channel_buffers or -1.
num_samplesthe number of samples per array to write to the stream.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, but if the stream has a callback set, the caller might need to manage extra locking.

Since
This function is available since SDL 3.4.0.
See also
SDL_ClearAudioStream
SDL_FlushAudioStream
SDL_GetAudioStreamData
SDL_GetAudioStreamQueued

◆ SDL_ResumeAudioDevice()

bool SDL_ResumeAudioDevice ( SDL_AudioDeviceID  devid)
extern

Use this function to unpause audio playback on a specified device.

This function unpauses audio processing for a given device that has previously been paused with SDL_PauseAudioDevice(). Once unpaused, any bound audio streams will begin to progress again, and audio can be generated.

Unlike in SDL2, audio devices start in an unpaused state, since an app has to bind a stream before any audio will flow. Unpausing an unpaused device is a legal no-op.

Physical devices can not be paused or unpaused, only logical devices created through SDL_OpenAudioDevice() can be.

Parameters
devida device opened by SDL_OpenAudioDevice().
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_AudioDevicePaused
SDL_PauseAudioDevice

◆ SDL_ResumeAudioStreamDevice()

bool SDL_ResumeAudioStreamDevice ( SDL_AudioStream stream)
extern

Use this function to unpause audio playback on the audio device associated with an audio stream.

This function unpauses audio processing for a given device that has previously been paused. Once unpaused, any bound audio streams will begin to progress again, and audio can be generated.

SDL_OpenAudioDeviceStream opens audio devices in a paused state, so this function call is required for audio playback to begin on such devices.

Parameters
streamthe audio stream associated with the audio device to resume.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_PauseAudioStreamDevice

◆ SDL_SetAudioDeviceGain()

bool SDL_SetAudioDeviceGain ( SDL_AudioDeviceID  devid,
float  gain 
)
extern

Change the gain of an audio device.

The gain of a device is its volume; a larger gain means a louder output, with a gain of zero being silence.

Audio devices default to a gain of 1.0f (no change in output).

Physical devices may not have their gain changed, only logical devices, and this function will always return false when used on physical devices. While it might seem attractive to adjust several logical devices at once in this way, it would allow an app or library to interfere with another portion of the program's otherwise-isolated devices.

This is applied, along with any per-audiostream gain, during playback to the hardware, and can be continuously changed to create various effects. On recording devices, this will adjust the gain before passing the data into an audiostream; that recording audiostream can then adjust its gain further when outputting the data elsewhere, if it likes, but that second gain is not applied until the data leaves the audiostream again.

Parameters
devidthe audio device on which to change gain.
gainthe gain. 1.0f is no change, 0.0f is silence.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, as it holds a stream-specific mutex while running.

Since
This function is available since SDL 3.2.0.
See also
SDL_GetAudioDeviceGain

◆ SDL_SetAudioPostmixCallback()

bool SDL_SetAudioPostmixCallback ( SDL_AudioDeviceID  devid,
SDL_AudioPostmixCallback  callback,
void *  userdata 
)
extern

Set a callback that fires when data is about to be fed to an audio device.

This is useful for accessing the final mix, perhaps for writing a visualizer or applying a final effect to the audio data before playback.

The buffer is the final mix of all bound audio streams on an opened device; this callback will fire regularly for any device that is both opened and unpaused. If there is no new data to mix, either because no streams are bound to the device or all the streams are empty, this callback will still fire with the entire buffer set to silence.

This callback is allowed to make changes to the data; the contents of the buffer after this call is what is ultimately passed along to the hardware.

The callback is always provided the data in float format (values from -1.0f to 1.0f), but the number of channels or sample rate may be different than the format the app requested when opening the device; SDL might have had to manage a conversion behind the scenes, or the playback might have jumped to new physical hardware when a system default changed, etc. These details may change between calls. Accordingly, the size of the buffer might change between calls as well.

This callback can run at any time, and from any thread; if you need to serialize access to your app's data, you should provide and use a mutex or other synchronization device.

All of this to say: there are specific needs this callback can fulfill, but it is not the simplest interface. Apps should generally provide audio in their preferred format through an SDL_AudioStream and let SDL handle the difference.

This function is extremely time-sensitive; the callback should do the least amount of work possible and return as quickly as it can. The longer the callback runs, the higher the risk of audio dropouts or other problems.

This function will block until the audio device is in between iterations, so any existing callback that might be running will finish before this function sets the new callback and returns.

Setting a NULL callback function disables any previously-set callback.

Parameters
devidthe ID of an opened audio device.
callbacka callback function to be called. Can be NULL.
userdataapp-controlled pointer passed to callback. Can be NULL.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

◆ SDL_SetAudioStreamFormat()

bool SDL_SetAudioStreamFormat ( SDL_AudioStream stream,
const SDL_AudioSpec src_spec,
const SDL_AudioSpec dst_spec 
)
extern

Change the input and output formats of an audio stream.

Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData will reflect the new format, and future calls to SDL_PutAudioStreamData must provide data in the new input formats.

Data that was previously queued in the stream will still be operated on in the format that was current when it was added, which is to say you can put the end of a sound file in one format to a stream, change formats for the next sound file, and start putting that new data while the previous sound file is still queued, and everything will still play back correctly.

If a stream is bound to a device, then the format of the side of the stream bound to a device cannot be changed (src_spec for recording devices, dst_spec for playback devices). Attempts to make a change to this side will be ignored, but this will not report an error. The other side's format can be changed.

src_spec and dst_spec may each be NULL; a NULL spec signals not to change the current format for that side of the stream.

Parameters
streamthe stream the format is being changed.
src_specthe new format of the audio input; if NULL, it is not changed.
dst_specthe new format of the audio output; if NULL, it is not changed.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, as it holds a stream-specific mutex while running.

Since
This function is available since SDL 3.2.0.
See also
SDL_GetAudioStreamFormat
SDL_SetAudioStreamFrequencyRatio

◆ SDL_SetAudioStreamFrequencyRatio()

bool SDL_SetAudioStreamFrequencyRatio ( SDL_AudioStream stream,
float  ratio 
)
extern

Change the frequency ratio of an audio stream.

The frequency ratio is used to adjust the rate at which input data is consumed. Changing this effectively modifies the speed and pitch of the audio. A value greater than 1.0f will play the audio faster, and at a higher pitch. A value less than 1.0f will play the audio slower, and at a lower pitch. 1.0f means play at normal speed.

This is applied during SDL_GetAudioStreamData, and can be continuously changed to create various effects.

Parameters
streamthe stream on which the frequency ratio is being changed.
ratiothe frequency ratio. 1.0 is normal speed. Must be between 0.01 and 100.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, as it holds a stream-specific mutex while running.

Since
This function is available since SDL 3.2.0.
See also
SDL_GetAudioStreamFrequencyRatio
SDL_SetAudioStreamFormat

◆ SDL_SetAudioStreamGain()

bool SDL_SetAudioStreamGain ( SDL_AudioStream stream,
float  gain 
)
extern

Change the gain of an audio stream.

The gain of a stream is its volume; a larger gain means a louder output, with a gain of zero being silence.

Audio streams default to a gain of 1.0f (no change in output).

This is applied during SDL_GetAudioStreamData, and can be continuously changed to create various effects.

Parameters
streamthe stream on which the gain is being changed.
gainthe gain. 1.0f is no change, 0.0f is silence.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, as it holds a stream-specific mutex while running.

Since
This function is available since SDL 3.2.0.
See also
SDL_GetAudioStreamGain

◆ SDL_SetAudioStreamGetCallback()

bool SDL_SetAudioStreamGetCallback ( SDL_AudioStream stream,
SDL_AudioStreamCallback  callback,
void *  userdata 
)
extern

Set a callback that runs when data is requested from an audio stream.

This callback is called before data is obtained from the stream, giving the callback the chance to add more on-demand.

The callback can (optionally) call SDL_PutAudioStreamData() to add more audio to the stream during this call; if needed, the request that triggered this callback will obtain the new data immediately.

The callback's additional_amount argument is roughly how many bytes of unconverted data (in the stream's input format) is needed by the caller, although this may overestimate a little for safety. This takes into account how much is already in the stream and only asks for any extra necessary to resolve the request, which means the callback may be asked for zero bytes, and a different amount on each call.

The callback is not required to supply exact amounts; it is allowed to supply too much or too little or none at all. The caller will get what's available, up to the amount they requested, regardless of this callback's outcome.

Clearing or flushing an audio stream does not call this callback.

This function obtains the stream's lock, which means any existing callback (get or put) in progress will finish running before setting the new callback.

Setting a NULL function turns off the callback.

Parameters
streamthe audio stream to set the new callback on.
callbackthe new callback function to call when data is requested from the stream.
userdataan opaque pointer provided to the callback for its own personal use.
Returns
true on success or false on failure; call SDL_GetError() for more information. This only fails if stream is NULL.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_SetAudioStreamPutCallback

◆ SDL_SetAudioStreamInputChannelMap()

bool SDL_SetAudioStreamInputChannelMap ( SDL_AudioStream stream,
const int *  chmap,
int  count 
)
extern

Set the current input channel map of an audio stream.

Channel maps are optional; most things do not need them, instead passing data in the order that SDL expects.

The input channel map reorders data that is added to a stream via SDL_PutAudioStreamData. Future calls to SDL_PutAudioStreamData must provide data in the new channel order.

Each item in the array represents an input channel, and its value is the channel that it should be remapped to. To reverse a stereo signal's left and right values, you'd have an array of { 1, 0 }. It is legal to remap multiple channels to the same thing, so { 1, 1 } would duplicate the right channel to both channels of a stereo signal. An element in the channel map set to -1 instead of a valid channel will mute that channel, setting it to a silence value.

You cannot change the number of channels through a channel map, just reorder/mute them.

Data that was previously queued in the stream will still be operated on in the order that was current when it was added, which is to say you can put the end of a sound file in one order to a stream, change orders for the next sound file, and start putting that new data while the previous sound file is still queued, and everything will still play back correctly.

Audio streams default to no remapping applied. Passing a NULL channel map is legal, and turns off remapping.

SDL will copy the channel map; the caller does not have to save this array after this call.

If count is not equal to the current number of channels in the audio stream's format, this will fail. This is a safety measure to make sure a race condition hasn't changed the format while this call is setting the channel map.

Unlike attempting to change the stream's format, the input channel map on a stream bound to a recording device is permitted to change at any time; any data added to the stream from the device after this call will have the new mapping, but previously-added data will still have the prior mapping.

Parameters
streamthe SDL_AudioStream to change.
chmapthe new channel map, NULL to reset to default.
countThe number of channels in the map.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, as it holds a stream-specific mutex while running. Don't change the stream's format to have a different number of channels from a different thread at the same time, though!

Since
This function is available since SDL 3.2.0.
See also
SDL_SetAudioStreamOutputChannelMap

◆ SDL_SetAudioStreamOutputChannelMap()

bool SDL_SetAudioStreamOutputChannelMap ( SDL_AudioStream stream,
const int *  chmap,
int  count 
)
extern

Set the current output channel map of an audio stream.

Channel maps are optional; most things do not need them, instead passing data in the order that SDL expects.

The output channel map reorders data that is leaving a stream via SDL_GetAudioStreamData.

Each item in the array represents an input channel, and its value is the channel that it should be remapped to. To reverse a stereo signal's left and right values, you'd have an array of { 1, 0 }. It is legal to remap multiple channels to the same thing, so { 1, 1 } would duplicate the right channel to both channels of a stereo signal. An element in the channel map set to -1 instead of a valid channel will mute that channel, setting it to a silence value.

You cannot change the number of channels through a channel map, just reorder/mute them.

The output channel map can be changed at any time, as output remapping is applied during SDL_GetAudioStreamData.

Audio streams default to no remapping applied. Passing a NULL channel map is legal, and turns off remapping.

SDL will copy the channel map; the caller does not have to save this array after this call.

If count is not equal to the current number of channels in the audio stream's format, this will fail. This is a safety measure to make sure a race condition hasn't changed the format while this call is setting the channel map.

Unlike attempting to change the stream's format, the output channel map on a stream bound to a recording device is permitted to change at any time; any data added to the stream after this call will have the new mapping, but previously-added data will still have the prior mapping. When the channel map doesn't match the hardware's channel layout, SDL will convert the data before feeding it to the device for playback.

Parameters
streamthe SDL_AudioStream to change.
chmapthe new channel map, NULL to reset to default.
countThe number of channels in the map.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread, as it holds a stream-specific mutex while running. Don't change the stream's format to have a different number of channels from a a different thread at the same time, though!

Since
This function is available since SDL 3.2.0.
See also
SDL_SetAudioStreamInputChannelMap

◆ SDL_SetAudioStreamPutCallback()

bool SDL_SetAudioStreamPutCallback ( SDL_AudioStream stream,
SDL_AudioStreamCallback  callback,
void *  userdata 
)
extern

Set a callback that runs when data is added to an audio stream.

This callback is called after the data is added to the stream, giving the callback the chance to obtain it immediately.

The callback can (optionally) call SDL_GetAudioStreamData() to obtain audio from the stream during this call.

The callback's additional_amount argument is how many bytes of converted data (in the stream's output format) was provided by the caller, although this may underestimate a little for safety. This value might be less than what is currently available in the stream, if data was already there, and might be less than the caller provided if the stream needs to keep a buffer to aid in resampling. Which means the callback may be provided with zero bytes, and a different amount on each call.

The callback may call SDL_GetAudioStreamAvailable to see the total amount currently available to read from the stream, instead of the total provided by the current call.

The callback is not required to obtain all data. It is allowed to read less or none at all. Anything not read now simply remains in the stream for later access.

Clearing or flushing an audio stream does not call this callback.

This function obtains the stream's lock, which means any existing callback (get or put) in progress will finish running before setting the new callback.

Setting a NULL function turns off the callback.

Parameters
streamthe audio stream to set the new callback on.
callbackthe new callback function to call when data is added to the stream.
userdataan opaque pointer provided to the callback for its own personal use.
Returns
true on success or false on failure; call SDL_GetError() for more information. This only fails if stream is NULL.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_SetAudioStreamGetCallback

◆ SDL_UnbindAudioStream()

void SDL_UnbindAudioStream ( SDL_AudioStream stream)
extern

Unbind a single audio stream from its audio device.

This is a convenience function, equivalent to calling SDL_UnbindAudioStreams(&stream, 1).

Parameters
streaman audio stream to unbind from a device. Can be NULL.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_BindAudioStream

◆ SDL_UnbindAudioStreams()

void SDL_UnbindAudioStreams ( SDL_AudioStream *const *  streams,
int  num_streams 
)
extern

Unbind a list of audio streams from their audio devices.

The streams being unbound do not all have to be on the same device. All streams on the same device will be unbound atomically (data will stop flowing through all unbound streams on the same device at the same time).

Unbinding a stream that isn't bound to a device is a legal no-op.

Parameters
streamsan array of audio streams to unbind. Can be NULL or contain NULL.
num_streamsnumber streams listed in the streams array.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_BindAudioStreams

◆ SDL_UnlockAudioStream()

bool SDL_UnlockAudioStream ( SDL_AudioStream stream)
extern

Unlock an audio stream for serialized access.

This unlocks an audio stream after a call to SDL_LockAudioStream.

Parameters
streamthe audio stream to unlock.
Returns
true on success or false on failure; call SDL_GetError() for more information.

\threadsafety You should only call this from the same thread that previously called SDL_LockAudioStream.

Since
This function is available since SDL 3.2.0.
See also
SDL_LockAudioStream