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, float, size)    (((Uint16)(signed) << 15) | ((Uint16)(bigendian) << 12) | ((Uint16)(float) << 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)
 

Typedefs

typedef Uint32 SDL_AudioDeviceID
 
typedef struct SDL_AudioStream SDL_AudioStream
 

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
}
 

Driver discovery functions

These functions return the list of built in audio drivers, in the order that they are normally initialized by default.

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)
 
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 dev)
 
bool SDL_ResumeAudioDevice (SDL_AudioDeviceID dev)
 
bool SDL_AudioDevicePaused (SDL_AudioDeviceID dev)
 
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 **streams, int num_streams)
 
bool SDL_BindAudioStream (SDL_AudioDeviceID devid, SDL_AudioStream *stream)
 
void SDL_UnbindAudioStreams (SDL_AudioStream **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)
 
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_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.1.3.

Definition at line 212 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.1.3.

Definition at line 226 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.1.3.

Definition at line 331 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.1.3.

Definition at line 342 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.1.3.

Definition at line 371 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.1.3.

Definition at line 254 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.1.3.

Definition at line 240 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.1.3.

Definition at line 296 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.1.3.

Definition at line 268 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.1.3.

Definition at line 282 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.1.3.

Definition at line 310 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_BIG_ENDIAN

#define SDL_AUDIO_MASK_BIG_ENDIAN   (1u<<12)

Definition at line 147 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.

Definition at line 145 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_FLOAT

#define SDL_AUDIO_MASK_FLOAT   (1u<<8)

Definition at line 146 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_SIGNED

#define SDL_AUDIO_MASK_SIGNED   (1u<<15)

Definition at line 148 of file SDL_audio.h.

◆ SDL_DEFINE_AUDIO_FORMAT

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

Definition at line 150 of file SDL_audio.h.

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

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.1.3.

Definition at line 320 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.1.3.
See also
SDL_SetAudioPostmixCallback

Definition at line 1838 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
  • 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.1.3.
See also
SDL_CreateAudioStream

Definition at line 397 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.1.3.
See also
SDL_SetAudioStreamGetCallback
SDL_SetAudioStreamPutCallback

Definition at line 1621 of file SDL_audio.h.

Enumeration Type Documentation

◆ SDL_AudioFormat

Audio format.

Since
This enum is available since SDL 3.1.3.
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 167 of file SDL_audio.h.

168{
169 SDL_AUDIO_UNKNOWN = 0x0000u, /**< Unspecified audio format */
170 SDL_AUDIO_U8 = 0x0008u, /**< Unsigned 8-bit samples */
171 /* SDL_DEFINE_AUDIO_FORMAT(0, 0, 0, 8), */
172 SDL_AUDIO_S8 = 0x8008u, /**< Signed 8-bit samples */
173 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 8), */
174 SDL_AUDIO_S16LE = 0x8010u, /**< Signed 16-bit samples */
175 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 16), */
176 SDL_AUDIO_S16BE = 0x9010u, /**< As above, but big-endian byte order */
177 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 16), */
178 SDL_AUDIO_S32LE = 0x8020u, /**< 32-bit integer samples */
179 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 32), */
180 SDL_AUDIO_S32BE = 0x9020u, /**< As above, but big-endian byte order */
181 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 32), */
182 SDL_AUDIO_F32LE = 0x8120u, /**< 32-bit floating point samples */
183 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 1, 32), */
184 SDL_AUDIO_F32BE = 0x9120u, /**< As above, but big-endian byte order */
185 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 1, 32), */
186
187 /* These represent the current system's byteorder. */
188 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
192 #else
196 #endif

Function Documentation

◆ SDL_AudioDevicePaused()

bool SDL_AudioDevicePaused ( SDL_AudioDeviceID  dev)
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
deva 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.1.3.
See also
SDL_PauseAudioDevice
SDL_ResumeAudioDevice

◆ 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.1.3.
See also
SDL_BindAudioStreams
SDL_UnbindAudioStream
SDL_GetAudioStreamDevice

◆ SDL_BindAudioStreams()

bool SDL_BindAudioStreams ( SDL_AudioDeviceID  devid,
SDL_AudioStream **  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.

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.1.3.
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.1.3.
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.1.3.
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.1.3.

◆ SDL_CreateAudioStream()

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

Create a new audio stream.

Parameters
src_specthe format details of the input audio.
dst_specthe format details of the output audio.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.

◆ 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.1.3.
See also
SDL_SetAudioDeviceGain

◆ SDL_GetAudioDeviceName()

const char * SDL_GetAudioDeviceName ( SDL_AudioDeviceID  devid)
extern

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

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.1.3.
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.1.3.
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.1.3.

◆ 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.1.3.
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.1.3.
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.1.3.
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.1.3.
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 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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
See also
SDL_SetAudioStreamInputChannelMap

◆ SDL_GetAudioStreamProperties()

SDL_PropertiesID SDL_GetAudioStreamProperties ( SDL_AudioStream stream)
extern

Get the properties associated with an audio stream.

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.1.3.

◆ 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.1.3.
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.1.3.

◆ 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.1.3.
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.1.3.

◆ 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.1.3.
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.1.3.
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.1.3.
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.1.3.

◆ 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.1.3.
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(stream);

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.1.3.
See also
SDL_GetAudioStreamDevice
SDL_ResumeAudioStreamDevice

◆ SDL_PauseAudioDevice()

bool SDL_PauseAudioDevice ( SDL_AudioDeviceID  dev)
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
deva 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.1.3.
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.1.3.
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.1.3.
See also
SDL_ClearAudioStream
SDL_FlushAudioStream
SDL_GetAudioStreamData
SDL_GetAudioStreamQueued

◆ SDL_ResumeAudioDevice()

bool SDL_ResumeAudioDevice ( SDL_AudioDeviceID  dev)
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
deva 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.1.3.
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.

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.1.3.
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.1.3.
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.1.3.

◆ 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.

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.1.3.
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.0 will play the audio faster, and at a higher pitch. A value less than 1.0 will play the audio slower, and at a lower pitch.

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

Parameters
streamthe stream 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.1.3.
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.1.3.
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 approx_request 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.1.3.
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.

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.1.3.
See also
SDL_SetAudioStreamInputChannelMap

◆ 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 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.

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.1.3.
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 approx_request 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.1.3.
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.

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

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

◆ SDL_UnbindAudioStreams()

void SDL_UnbindAudioStreams ( SDL_AudioStream **  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.
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.1.3.
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.1.3.
See also
SDL_LockAudioStream