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