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