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_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 195 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 209 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 314 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 325 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 354 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 237 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 223 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 279 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 251 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 265 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 293 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_BIG_ENDIAN

#define SDL_AUDIO_MASK_BIG_ENDIAN   (1u<<12)

Definition at line 130 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 it as available. When the devices 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.

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 128 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_FLOAT

#define SDL_AUDIO_MASK_FLOAT   (1u<<8)

Definition at line 129 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_SIGNED

#define SDL_AUDIO_MASK_SIGNED   (1u<<15)

Definition at line 131 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 133 of file SDL_audio.h.

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

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 303 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 1775 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 380 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 1558 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 150 of file SDL_audio.h.

151{
152 SDL_AUDIO_UNKNOWN = 0x0000u, /**< Unspecified audio format */
153 SDL_AUDIO_U8 = 0x0008u, /**< Unsigned 8-bit samples */
154 /* SDL_DEFINE_AUDIO_FORMAT(0, 0, 0, 8), */
155 SDL_AUDIO_S8 = 0x8008u, /**< Signed 8-bit samples */
156 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 8), */
157 SDL_AUDIO_S16LE = 0x8010u, /**< Signed 16-bit samples */
158 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 16), */
159 SDL_AUDIO_S16BE = 0x9010u, /**< As above, but big-endian byte order */
160 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 16), */
161 SDL_AUDIO_S32LE = 0x8020u, /**< 32-bit integer samples */
162 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 32), */
163 SDL_AUDIO_S32BE = 0x9020u, /**< As above, but big-endian byte order */
164 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 32), */
165 SDL_AUDIO_F32LE = 0x8120u, /**< 32-bit floating point samples */
166 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 1, 32), */
167 SDL_AUDIO_F32BE = 0x9120u, /**< As above, but big-endian byte order */
168 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 1, 32), */
169
170 /* These represent the current system's byteorder. */
171 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
175 #else
179 #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_GetDefaultAudioInfo

◆ 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_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. You cannot change the number of channels through a channel map, just reorder 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 a race condition hasn't changed the format while you 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 output 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. You cannot change the number of channels through a channel map, just reorder 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 a race condition hasn't changed the format while you 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