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