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_S16   SDL_AUDIO_S16LE
 
#define SDL_AUDIO_S32   SDL_AUDIO_S32LE
 
#define SDL_AUDIO_F32   SDL_AUDIO_F32LE
 
#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
}
 

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)
 
SDL_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)
 
SDL_bool SDL_PauseAudioDevice (SDL_AudioDeviceID dev)
 
SDL_bool SDL_ResumeAudioDevice (SDL_AudioDeviceID dev)
 
SDL_bool SDL_AudioDevicePaused (SDL_AudioDeviceID dev)
 
float SDL_GetAudioDeviceGain (SDL_AudioDeviceID devid)
 
SDL_bool SDL_SetAudioDeviceGain (SDL_AudioDeviceID devid, float gain)
 
void SDL_CloseAudioDevice (SDL_AudioDeviceID devid)
 
SDL_bool SDL_BindAudioStreams (SDL_AudioDeviceID devid, SDL_AudioStream **streams, int num_streams)
 
SDL_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)
 
SDL_bool SDL_GetAudioStreamFormat (SDL_AudioStream *stream, SDL_AudioSpec *src_spec, SDL_AudioSpec *dst_spec)
 
SDL_bool SDL_SetAudioStreamFormat (SDL_AudioStream *stream, const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec)
 
float SDL_GetAudioStreamFrequencyRatio (SDL_AudioStream *stream)
 
SDL_bool SDL_SetAudioStreamFrequencyRatio (SDL_AudioStream *stream, float ratio)
 
float SDL_GetAudioStreamGain (SDL_AudioStream *stream)
 
SDL_bool SDL_SetAudioStreamGain (SDL_AudioStream *stream, float gain)
 
int * SDL_GetAudioStreamInputChannelMap (SDL_AudioStream *stream, int *count)
 
int * SDL_GetAudioStreamOutputChannelMap (SDL_AudioStream *stream, int *count)
 
SDL_bool SDL_SetAudioStreamInputChannelMap (SDL_AudioStream *stream, const int *chmap, int count)
 
SDL_bool SDL_SetAudioStreamOutputChannelMap (SDL_AudioStream *stream, const int *chmap, int count)
 
SDL_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)
 
SDL_bool SDL_FlushAudioStream (SDL_AudioStream *stream)
 
SDL_bool SDL_ClearAudioStream (SDL_AudioStream *stream)
 
SDL_bool SDL_PauseAudioStreamDevice (SDL_AudioStream *stream)
 
SDL_bool SDL_ResumeAudioStreamDevice (SDL_AudioStream *stream)
 
SDL_bool SDL_LockAudioStream (SDL_AudioStream *stream)
 
SDL_bool SDL_UnlockAudioStream (SDL_AudioStream *stream)
 
SDL_bool SDL_SetAudioStreamGetCallback (SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata)
 
SDL_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)
 
SDL_bool SDL_SetAudioPostmixCallback (SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata)
 
SDL_bool SDL_LoadWAV_IO (SDL_IOStream *src, SDL_bool closeio, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
 
SDL_bool SDL_LoadWAV (const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
 
SDL_bool SDL_MixAudio (Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, float volume)
 
SDL_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.0.0.

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

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

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

Definition at line 302 of file SDL_audio.h.

◆ SDL_AUDIO_F32

#define SDL_AUDIO_F32   SDL_AUDIO_F32LE

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

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

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

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

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

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

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

Definition at line 270 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_BIG_ENDIAN

#define SDL_AUDIO_MASK_BIG_ENDIAN   (1u<<12)

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

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 BL, BR)
  • 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 106 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_FLOAT

#define SDL_AUDIO_MASK_FLOAT   (1u<<8)

Definition at line 107 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_SIGNED

#define SDL_AUDIO_MASK_SIGNED   (1u<<15)

Definition at line 109 of file SDL_audio.h.

◆ SDL_AUDIO_S16

#define SDL_AUDIO_S16   SDL_AUDIO_S16LE

Definition at line 150 of file SDL_audio.h.

◆ SDL_AUDIO_S32

#define SDL_AUDIO_S32   SDL_AUDIO_S32LE

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

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

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

Definition at line 280 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.0.0.
See also
SDL_SetAudioPostmixCallback

Definition at line 1744 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.0.0.
See also
SDL_CreateAudioStream

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

Definition at line 1527 of file SDL_audio.h.

Enumeration Type Documentation

◆ SDL_AudioFormat

Audio format.

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

Unspecified audio format

SDL_AUDIO_U8 

Unsigned 8-bit samples

SDL_AUDIO_S8 

Signed 8-bit samples

SDL_AUDIO_S16LE 

Signed 16-bit samples

SDL_AUDIO_S16BE 

As above, but big-endian byte order

SDL_AUDIO_S32LE 

32-bit integer samples

SDL_AUDIO_S32BE 

As above, but big-endian byte order

SDL_AUDIO_F32LE 

32-bit floating point samples

SDL_AUDIO_F32BE 

As above, but big-endian byte order

Definition at line 128 of file SDL_audio.h.

129{
130 SDL_AUDIO_UNKNOWN = 0x0000u, /**< Unspecified audio format */
131 SDL_AUDIO_U8 = 0x0008u, /**< Unsigned 8-bit samples */
132 /* SDL_DEFINE_AUDIO_FORMAT(0, 0, 0, 8), */
133 SDL_AUDIO_S8 = 0x8008u, /**< Signed 8-bit samples */
134 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 8), */
135 SDL_AUDIO_S16LE = 0x8010u, /**< Signed 16-bit samples */
136 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 16), */
137 SDL_AUDIO_S16BE = 0x9010u, /**< As above, but big-endian byte order */
138 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 16), */
139 SDL_AUDIO_S32LE = 0x8020u, /**< 32-bit integer samples */
140 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 32), */
141 SDL_AUDIO_S32BE = 0x9020u, /**< As above, but big-endian byte order */
142 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 32), */
143 SDL_AUDIO_F32LE = 0x8120u, /**< 32-bit floating point samples */
144 /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 1, 32), */
145 SDL_AUDIO_F32BE = 0x9120u /**< As above, but big-endian byte order */
146 /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 1, 32), */

Function Documentation

◆ SDL_AudioDevicePaused()

SDL_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
SDL_TRUE if device is valid and paused, SDL_FALSE otherwise.

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

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

◆ SDL_BindAudioStream()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_BindAudioStreams
SDL_UnbindAudioStream
SDL_GetAudioStreamDevice

◆ SDL_BindAudioStreams()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_BindAudioStreams
SDL_UnbindAudioStream
SDL_GetAudioStreamDevice

◆ SDL_ClearAudioStream()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_GetAudioStreamAvailable
SDL_GetAudioStreamData
SDL_GetAudioStreamQueued
SDL_PutAudioStreamData

◆ SDL_CloseAudioDevice()

void SDL_CloseAudioDevice ( SDL_AudioDeviceID  devid)
extern

Close a previously-opened audio device.

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

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

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

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

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

◆ SDL_ConvertAudioSamples()

SDL_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
SDL_TRUE on success or SDL_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.0.0.

◆ 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.0.0.
See also
SDL_PutAudioStreamData
SDL_GetAudioStreamData
SDL_GetAudioStreamAvailable
SDL_FlushAudioStream
SDL_ClearAudioStream
SDL_SetAudioStreamFormat
SDL_DestroyAudioStream

◆ SDL_DestroyAudioStream()

void SDL_DestroyAudioStream ( SDL_AudioStream stream)
extern

Free an audio stream.

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

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

Parameters
streamthe audio stream to destroy.

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

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

◆ SDL_FlushAudioStream()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_PutAudioStreamData

◆ SDL_GetAudioDeviceChannelMap()

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

Get the current channel map of an audio device.

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

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

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

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

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

◆ SDL_GetAudioDeviceFormat()

SDL_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
SDL_TRUE on success or SDL_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.0.0.

◆ SDL_GetAudioDeviceGain()

float SDL_GetAudioDeviceGain ( SDL_AudioDeviceID  devid)
extern

Get the gain of an audio device.

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

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

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

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

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

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

◆ SDL_GetAudioDeviceName()

const char * SDL_GetAudioDeviceName ( SDL_AudioDeviceID  devid)
extern

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

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.0.0.
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.0.0.
See also
SDL_GetNumAudioDrivers

◆ SDL_GetAudioFormatName()

const char * SDL_GetAudioFormatName ( SDL_AudioFormat  format)
extern

Get the human readable name of an audio format.

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

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

Since
This function is available since SDL 3.0.0.

◆ SDL_GetAudioPlaybackDevices()

SDL_AudioDeviceID * SDL_GetAudioPlaybackDevices ( int *  count)
extern

Get a list of currently-connected audio playback devices.

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

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

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

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

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

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

◆ SDL_GetAudioRecordingDevices()

SDL_AudioDeviceID * SDL_GetAudioRecordingDevices ( int *  count)
extern

Get a list of currently-connected audio recording devices.

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

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

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

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

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

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

◆ SDL_GetAudioStreamAvailable()

int SDL_GetAudioStreamAvailable ( SDL_AudioStream stream)
extern

Get the number of converted/resampled bytes available.

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

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

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

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

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

◆ SDL_GetAudioStreamData()

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

Get converted/resampled data from the stream.

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

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

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

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

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

◆ SDL_GetAudioStreamDevice()

SDL_AudioDeviceID SDL_GetAudioStreamDevice ( SDL_AudioStream stream)
extern

Query an audio stream for its currently-bound device.

This reports the 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.0.0.
See also
SDL_BindAudioStream
SDL_BindAudioStreams

◆ SDL_GetAudioStreamFormat()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_SetAudioStreamFormat

◆ SDL_GetAudioStreamFrequencyRatio()

float SDL_GetAudioStreamFrequencyRatio ( SDL_AudioStream stream)
extern

Get the frequency ratio of an audio stream.

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

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

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

◆ SDL_GetAudioStreamGain()

float SDL_GetAudioStreamGain ( SDL_AudioStream stream)
extern

Get the gain of an audio stream.

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

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

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

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

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

◆ SDL_GetAudioStreamInputChannelMap()

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

Get the current input channel map of an audio stream.

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

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

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

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

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

◆ SDL_GetAudioStreamOutputChannelMap()

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

Get the current output channel map of an audio stream.

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

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

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

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

Since
This function is available since SDL 3.0.0.
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.
Since
This function is available since SDL 3.0.0.

◆ SDL_GetAudioStreamQueued()

int SDL_GetAudioStreamQueued ( SDL_AudioStream stream)
extern

Get the number of bytes currently queued.

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.0.0.
See also
SDL_PutAudioStreamData
SDL_ClearAudioStream

◆ SDL_GetCurrentAudioDriver()

const char * SDL_GetCurrentAudioDriver ( void  )
extern

Get the name of the current audio driver.

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

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

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

Since
This function is available since SDL 3.0.0.

◆ SDL_GetNumAudioDrivers()

int SDL_GetNumAudioDrivers ( void  )
extern

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

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

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

Returns
the number of built-in audio drivers.

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

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

◆ SDL_GetSilenceValueForFormat()

int SDL_GetSilenceValueForFormat ( SDL_AudioFormat  format)
extern

Get the appropriate memset value for silencing an audio format.

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

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

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

Since
This function is available since SDL 3.0.0.

◆ SDL_LoadWAV()

SDL_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"), SDL_TRUE, spec, audio_buf, audio_len);
SDL_IOStream * SDL_IOFromFile(const char *file, const char *mode)
#define SDL_TRUE
Definition SDL_stdinc.h:206
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
SDL_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 SDL_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.0.0.
See also
SDL_free
SDL_LoadWAV_IO

◆ SDL_LoadWAV_IO()

SDL_bool SDL_LoadWAV_IO ( SDL_IOStream src,
SDL_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"), 1, &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 SDL_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
SDL_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 SDL_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.0.0.
See also
SDL_free
SDL_LoadWAV

◆ SDL_LockAudioStream()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_UnlockAudioStream

◆ SDL_MixAudio()

SDL_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
SDL_TRUE on success or SDL_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.0.0.

◆ SDL_OpenAudioDevice()

SDL_AudioDeviceID SDL_OpenAudioDevice ( SDL_AudioDeviceID  devid,
const SDL_AudioSpec spec 
)
extern

Open a specific audio device.

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

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

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

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

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

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

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

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

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

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

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

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

◆ SDL_OpenAudioDeviceStream()

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

Convenience function for straightforward audio init for the common case.

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

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

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

Also unlike other functions, the audio device begins paused. This is to map more closely to SDL2-style behavior, since there is no extra step here to bind a stream to begin audio flowing. The audio device should be resumed with SDL_ResumeAudioStreamDevice(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.0.0.
See also
SDL_GetAudioStreamDevice
SDL_ResumeAudioStreamDevice

◆ SDL_PauseAudioDevice()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_ResumeAudioDevice
SDL_AudioDevicePaused

◆ SDL_PauseAudioStreamDevice()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_ResumeAudioStreamDevice

◆ SDL_PutAudioStreamData()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_ClearAudioStream
SDL_FlushAudioStream
SDL_GetAudioStreamData
SDL_GetAudioStreamQueued

◆ SDL_ResumeAudioDevice()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_AudioDevicePaused
SDL_PauseAudioDevice

◆ SDL_ResumeAudioStreamDevice()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_PauseAudioStreamDevice

◆ SDL_SetAudioDeviceGain()

SDL_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 -1 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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_GetAudioDeviceGain

◆ SDL_SetAudioPostmixCallback()

SDL_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
SDL_TRUE on success or SDL_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.0.0.

◆ SDL_SetAudioStreamFormat()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_GetAudioStreamFormat
SDL_SetAudioStreamFrequencyRatio

◆ SDL_SetAudioStreamFrequencyRatio()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_GetAudioStreamFrequencyRatio
SDL_SetAudioStreamFormat

◆ SDL_SetAudioStreamGain()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_GetAudioStreamGain

◆ SDL_SetAudioStreamGetCallback()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_SetAudioStreamPutCallback

◆ SDL_SetAudioStreamInputChannelMap()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_SetAudioStreamInputChannelMap

◆ SDL_SetAudioStreamOutputChannelMap()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_SetAudioStreamInputChannelMap

◆ SDL_SetAudioStreamPutCallback()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_SetAudioStreamGetCallback

◆ SDL_UnbindAudioStream()

void SDL_UnbindAudioStream ( SDL_AudioStream stream)
extern

Unbind a single audio stream from its audio device.

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

Parameters
streaman audio stream to unbind from a device.

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

Since
This function is available since SDL 3.0.0.
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.0.0.
See also
SDL_BindAudioStreams

◆ SDL_UnlockAudioStream()

SDL_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
SDL_TRUE on success or SDL_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.0.0.
See also
SDL_LockAudioStream