SDL 3.0
SDL_audio.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2026 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
246 #else
250 #endif
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 */
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 */
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; the stream
438 * will buffer data as needed.
439 * - It can also function as a basic audio data queue even if you just have
440 * sound that needs to pass from one place to another.
441 * - You can hook callbacks up to them when more data is added or requested,
442 * to manage data on-the-fly.
443 *
444 * Audio streams are the core of the SDL3 audio interface. You create one or
445 * more of them, bind them to an opened audio device, and feed data to them
446 * (or for recording, consume data from them).
447 *
448 * \since This struct is available since SDL 3.2.0.
449 *
450 * \sa SDL_CreateAudioStream
451 */
453
454
455/* Function prototypes */
456
457/**
458 * Use this function to get the number of built-in audio drivers.
459 *
460 * This function returns a hardcoded number. This never returns a negative
461 * value; if there are no drivers compiled into this build of SDL, this
462 * function returns zero. The presence of a driver in this list does not mean
463 * it will function, it just means SDL is capable of interacting with that
464 * interface. For example, a build of SDL might have esound support, but if
465 * there's no esound server available, SDL's esound driver would fail if used.
466 *
467 * By default, SDL tries all drivers, in its preferred order, until one is
468 * found to be usable.
469 *
470 * \returns the number of built-in audio drivers.
471 *
472 * \threadsafety It is safe to call this function from any thread.
473 *
474 * \since This function is available since SDL 3.2.0.
475 *
476 * \sa SDL_GetAudioDriver
477 */
478extern SDL_DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
479
480/**
481 * Use this function to get the name of a built in audio driver.
482 *
483 * The list of audio drivers is given in the order that they are normally
484 * initialized by default; the drivers that seem more reasonable to choose
485 * first (as far as the SDL developers believe) are earlier in the list.
486 *
487 * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
488 * "coreaudio" or "wasapi". These never have Unicode characters, and are not
489 * meant to be proper names.
490 *
491 * \param index the index of the audio driver; the value ranges from 0 to
492 * SDL_GetNumAudioDrivers() - 1.
493 * \returns the name of the audio driver at the requested index, or NULL if an
494 * invalid index was specified.
495 *
496 * \threadsafety It is safe to call this function from any thread.
497 *
498 * \since This function is available since SDL 3.2.0.
499 *
500 * \sa SDL_GetNumAudioDrivers
501 */
502extern SDL_DECLSPEC const char * SDLCALL SDL_GetAudioDriver(int index);
503
504/**
505 * Get the name of the current audio driver.
506 *
507 * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
508 * "coreaudio" or "wasapi". These never have Unicode characters, and are not
509 * meant to be proper names.
510 *
511 * \returns the name of the current audio driver or NULL if no driver has been
512 * initialized.
513 *
514 * \threadsafety It is safe to call this function from any thread.
515 *
516 * \since This function is available since SDL 3.2.0.
517 */
518extern SDL_DECLSPEC const char * SDLCALL SDL_GetCurrentAudioDriver(void);
519
520/**
521 * Get a list of currently-connected audio playback devices.
522 *
523 * This returns of list of available devices that play sound, perhaps to
524 * speakers or headphones ("playback" devices). If you want devices that
525 * record audio, like a microphone ("recording" devices), use
526 * SDL_GetAudioRecordingDevices() instead.
527 *
528 * This only returns a list of physical devices; it will not have any device
529 * IDs returned by SDL_OpenAudioDevice().
530 *
531 * If this function returns NULL, to signify an error, `*count` will be set to
532 * zero.
533 *
534 * \param count a pointer filled in with the number of devices returned, may
535 * be NULL.
536 * \returns a 0 terminated array of device instance IDs or NULL on error; call
537 * SDL_GetError() for more information. This should be freed with
538 * SDL_free() when it is no longer needed.
539 *
540 * \threadsafety It is safe to call this function from any thread.
541 *
542 * \since This function is available since SDL 3.2.0.
543 *
544 * \sa SDL_OpenAudioDevice
545 * \sa SDL_GetAudioRecordingDevices
546 */
547extern SDL_DECLSPEC SDL_AudioDeviceID * SDLCALL SDL_GetAudioPlaybackDevices(int *count);
548
549/**
550 * Get a list of currently-connected audio recording devices.
551 *
552 * This returns of list of available devices that record audio, like a
553 * microphone ("recording" devices). If you want devices that play sound,
554 * perhaps to speakers or headphones ("playback" devices), use
555 * SDL_GetAudioPlaybackDevices() instead.
556 *
557 * This only returns a list of physical devices; it will not have any device
558 * IDs returned by SDL_OpenAudioDevice().
559 *
560 * If this function returns NULL, to signify an error, `*count` will be set to
561 * zero.
562 *
563 * \param count a pointer filled in with the number of devices returned, may
564 * be NULL.
565 * \returns a 0 terminated array of device instance IDs, or NULL on failure;
566 * call SDL_GetError() for more information. This should be freed
567 * with SDL_free() when it is no longer needed.
568 *
569 * \threadsafety It is safe to call this function from any thread.
570 *
571 * \since This function is available since SDL 3.2.0.
572 *
573 * \sa SDL_OpenAudioDevice
574 * \sa SDL_GetAudioPlaybackDevices
575 */
576extern SDL_DECLSPEC SDL_AudioDeviceID * SDLCALL SDL_GetAudioRecordingDevices(int *count);
577
578/**
579 * Get the human-readable name of a specific audio device.
580 *
581 * **WARNING**: this function will work with SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK
582 * and SDL_AUDIO_DEVICE_DEFAULT_RECORDING, returning the current default
583 * physical devices' names. However, as the default device may change at any
584 * time, it is likely better to show a generic name to the user, like "System
585 * default audio device" or perhaps "default [currently %s]". Do not store
586 * this name to disk to reidentify the device in a later run of the program,
587 * as the default might change in general, and the string will be the name of
588 * a specific device and not the abstract system default.
589 *
590 * \param devid the instance ID of the device to query.
591 * \returns the name of the audio device, or NULL on failure; call
592 * SDL_GetError() for more information.
593 *
594 * \threadsafety It is safe to call this function from any thread.
595 *
596 * \since This function is available since SDL 3.2.0.
597 *
598 * \sa SDL_GetAudioPlaybackDevices
599 * \sa SDL_GetAudioRecordingDevices
600 */
601extern SDL_DECLSPEC const char * SDLCALL SDL_GetAudioDeviceName(SDL_AudioDeviceID devid);
602
603/**
604 * Get the current audio format of a specific audio device.
605 *
606 * For an opened device, this will report the format the device is currently
607 * using. If the device isn't yet opened, this will report the device's
608 * preferred format (or a reasonable default if this can't be determined).
609 *
610 * You may also specify SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or
611 * SDL_AUDIO_DEVICE_DEFAULT_RECORDING here, which is useful for getting a
612 * reasonable recommendation before opening the system-recommended default
613 * device.
614 *
615 * You can also use this to request the current device buffer size. This is
616 * specified in sample frames and represents the amount of data SDL will feed
617 * to the physical hardware in each chunk. This can be converted to
618 * milliseconds of audio with the following equation:
619 *
620 * `ms = (int) ((((Sint64) frames) * 1000) / spec.freq);`
621 *
622 * Buffer size is only important if you need low-level control over the audio
623 * playback timing. Most apps do not need this.
624 *
625 * \param devid the instance ID of the device to query.
626 * \param spec on return, will be filled with device details.
627 * \param sample_frames pointer to store device buffer size, in sample frames.
628 * Can be NULL.
629 * \returns true on success or false on failure; call SDL_GetError() for more
630 * information.
631 *
632 * \threadsafety It is safe to call this function from any thread.
633 *
634 * \since This function is available since SDL 3.2.0.
635 */
636extern SDL_DECLSPEC bool SDLCALL SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec, int *sample_frames);
637
638/**
639 * Get the current channel map of an audio device.
640 *
641 * Channel maps are optional; most things do not need them, instead passing
642 * data in the [order that SDL expects](CategoryAudio#channel-layouts).
643 *
644 * Audio devices usually have no remapping applied. This is represented by
645 * returning NULL, and does not signify an error.
646 *
647 * \param devid the instance ID of the device to query.
648 * \param count On output, set to number of channels in the map. Can be NULL.
649 * \returns an array of the current channel mapping, with as many elements as
650 * the current output spec's channels, or NULL if default. This
651 * should be freed with SDL_free() when it is no longer needed.
652 *
653 * \threadsafety It is safe to call this function from any thread.
654 *
655 * \since This function is available since SDL 3.2.0.
656 *
657 * \sa SDL_SetAudioStreamInputChannelMap
658 */
659extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioDeviceChannelMap(SDL_AudioDeviceID devid, int *count);
660
661/**
662 * Get the properties associated with an audio device.
663 *
664 * This can be used with both logical and physical devices. Note that while
665 * apps can hang any data they want here, physical device IDs are global; it
666 * would be better to assign data to one's own logical device so it doesn't
667 * interfere with other parts of the program that might be using the same
668 * physical device ID.
669 *
670 * Properties provided by SDL for physical devices will also be made available
671 * on their associated logical devices, unless otherwise noted.
672 *
673 * The application can hang any data it wants here, but the following
674 * properties are understood by SDL:
675 *
676 * - `SDL_PROP_AUDIO_DEVICE_UNIQUE_ID_STRING`: This identifier can be used to
677 * locate a specific device. In optimal conditions, this identifier will not
678 * change between runs of an app, hardware disconnection, and system
679 * reboots. However, depending on the hardware, operating system, and other
680 * circumstances, a device's identifier may change, so if the app cannot
681 * find a device with a previously queried identifier, the user should be
682 * prompted to choose a new device (possibly the same device, now with a new
683 * identifier). Device identifier strings have no specific format, the
684 * format may change in the future without warning, and are likely different
685 * between different operating systems on the same hardware. If the system
686 * cannot reasonably provide a unique identifier, this property will not be
687 * set. Note that property is useful for finding specific hardware again on
688 * a later run of the app, but often times it's better to just open the
689 * default device (SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or
690 * SDL_AUDIO_DEVICE_DEFAULT_RECORDING), and let the user set this up
691 * globally on their platform.
692 *
693 * \param devid the audio device instance id to query.
694 * \returns a valid property ID on success or 0 on failure; call
695 * SDL_GetError() for more information.
696 *
697 * \threadsafety It is safe to call this function from any thread.
698 *
699 * \since This function is available since SDL 3.6.0.
700 */
702
703#define SDL_PROP_AUDIO_DEVICE_UNIQUE_ID_STRING "SDL.audio.device.unique_id"
704
705
706/**
707 * Open a specific audio device.
708 *
709 * You can open both playback and recording devices through this function.
710 * Playback devices will take data from bound audio streams, mix it, and send
711 * it to the hardware. Recording devices will feed any bound audio streams
712 * with a copy of any incoming data.
713 *
714 * An opened audio device starts out with no audio streams bound. To start
715 * audio playing, bind a stream and supply audio data to it. Unlike SDL2,
716 * there is no audio callback; you only bind audio streams and make sure they
717 * have data flowing into them (however, you can simulate SDL2's semantics
718 * fairly closely by using SDL_OpenAudioDeviceStream instead of this
719 * function).
720 *
721 * If you don't care about opening a specific device, pass a `devid` of either
722 * `SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK` or
723 * `SDL_AUDIO_DEVICE_DEFAULT_RECORDING`. In this case, SDL will try to pick
724 * the most reasonable default, and may also switch between physical devices
725 * seamlessly later, if the most reasonable default changes during the
726 * lifetime of this opened device (user changed the default in the OS's system
727 * preferences, the default got unplugged so the system jumped to a new
728 * default, the user plugged in headphones on a mobile device, etc). Unless
729 * you have a good reason to choose a specific device, this is probably what
730 * you want.
731 *
732 * You may request a specific format for the audio device, but there is no
733 * promise the device will honor that request for several reasons. As such,
734 * it's only meant to be a hint as to what data your app will provide. Audio
735 * streams will accept data in whatever format you specify and manage
736 * conversion for you as appropriate. SDL_GetAudioDeviceFormat can tell you
737 * the preferred format for the device before opening and the actual format
738 * the device is using after opening.
739 *
740 * It's legal to open the same device ID more than once; each successful open
741 * will generate a new logical SDL_AudioDeviceID that is managed separately
742 * from others on the same physical device. This allows libraries to open a
743 * device separately from the main app and bind its own streams without
744 * conflicting.
745 *
746 * It is also legal to open a device ID returned by a previous call to this
747 * function; doing so just creates another logical device on the same physical
748 * device. This may be useful for making logical groupings of audio streams.
749 *
750 * This function returns the opened device ID on success. This is a new,
751 * unique SDL_AudioDeviceID that represents a logical device.
752 *
753 * Some backends might offer arbitrary devices (for example, a networked audio
754 * protocol that can connect to an arbitrary server). For these, as a change
755 * from SDL2, you should open a default device ID and use an SDL hint to
756 * specify the target if you care, or otherwise let the backend figure out a
757 * reasonable default. Most backends don't offer anything like this, and often
758 * this would be an end user setting an environment variable for their custom
759 * need, and not something an application should specifically manage.
760 *
761 * When done with an audio device, possibly at the end of the app's life, one
762 * should call SDL_CloseAudioDevice() on the returned device id.
763 *
764 * \param devid the device instance id to open, or
765 * SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or
766 * SDL_AUDIO_DEVICE_DEFAULT_RECORDING for the most reasonable
767 * default device.
768 * \param spec the requested device configuration. Can be NULL to use
769 * reasonable defaults.
770 * \returns the device ID on success or 0 on failure; call SDL_GetError() for
771 * more information.
772 *
773 * \threadsafety It is safe to call this function from any thread.
774 *
775 * \since This function is available since SDL 3.2.0.
776 *
777 * \sa SDL_CloseAudioDevice
778 * \sa SDL_GetAudioDeviceFormat
779 */
780extern SDL_DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec);
781
782/**
783 * Determine if an audio device is physical (instead of logical).
784 *
785 * An SDL_AudioDeviceID that represents physical hardware is a physical
786 * device; there is one for each piece of hardware that SDL can see. Logical
787 * devices are created by calling SDL_OpenAudioDevice or
788 * SDL_OpenAudioDeviceStream, and while each is associated with a physical
789 * device, there can be any number of logical devices on one physical device.
790 *
791 * For the most part, logical and physical IDs are interchangeable--if you try
792 * to open a logical device, SDL understands to assign that effort to the
793 * underlying physical device, etc. However, it might be useful to know if an
794 * arbitrary device ID is physical or logical. This function reports which.
795 *
796 * This function may return either true or false for invalid device IDs.
797 *
798 * \param devid the device ID to query.
799 * \returns true if devid is a physical device, false if it is logical.
800 *
801 * \threadsafety It is safe to call this function from any thread.
802 *
803 * \since This function is available since SDL 3.2.0.
804 */
805extern SDL_DECLSPEC bool SDLCALL SDL_IsAudioDevicePhysical(SDL_AudioDeviceID devid);
806
807/**
808 * Determine if an audio device is a playback device (instead of recording).
809 *
810 * This function may return either true or false for invalid device IDs.
811 *
812 * \param devid the device ID to query.
813 * \returns true if devid is a playback device, false if it is recording.
814 *
815 * \threadsafety It is safe to call this function from any thread.
816 *
817 * \since This function is available since SDL 3.2.0.
818 */
819extern SDL_DECLSPEC bool SDLCALL SDL_IsAudioDevicePlayback(SDL_AudioDeviceID devid);
820
821/**
822 * Use this function to pause audio playback on a specified device.
823 *
824 * This function pauses audio processing for a given device. Any bound audio
825 * streams will not progress, and no audio will be generated. Pausing one
826 * device does not prevent other unpaused devices from running.
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. Pausing a paused device is
830 * a legal no-op.
831 *
832 * Pausing a device can be useful to halt all audio without unbinding all the
833 * audio streams. This might be useful while a game is paused, or a level is
834 * loading, etc.
835 *
836 * Physical devices can not be paused or unpaused, only logical devices
837 * created through SDL_OpenAudioDevice() can be.
838 *
839 * \param devid a device opened by SDL_OpenAudioDevice().
840 * \returns true on success or false on failure; call SDL_GetError() for more
841 * information.
842 *
843 * \threadsafety It is safe to call this function from any thread.
844 *
845 * \since This function is available since SDL 3.2.0.
846 *
847 * \sa SDL_ResumeAudioDevice
848 * \sa SDL_AudioDevicePaused
849 */
850extern SDL_DECLSPEC bool SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID devid);
851
852/**
853 * Use this function to unpause audio playback on a specified device.
854 *
855 * This function unpauses audio processing for a given device that has
856 * previously been paused with SDL_PauseAudioDevice(). Once unpaused, any
857 * bound audio streams will begin to progress again, and audio can be
858 * generated.
859 *
860 * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
861 * has to bind a stream before any audio will flow. Unpausing an unpaused
862 * device is a legal no-op.
863 *
864 * Physical devices can not be paused or unpaused, only logical devices
865 * created through SDL_OpenAudioDevice() can be.
866 *
867 * \param devid a device opened by SDL_OpenAudioDevice().
868 * \returns true on success or false on failure; call SDL_GetError() for more
869 * information.
870 *
871 * \threadsafety It is safe to call this function from any thread.
872 *
873 * \since This function is available since SDL 3.2.0.
874 *
875 * \sa SDL_AudioDevicePaused
876 * \sa SDL_PauseAudioDevice
877 */
878extern SDL_DECLSPEC bool SDLCALL SDL_ResumeAudioDevice(SDL_AudioDeviceID devid);
879
880/**
881 * Use this function to query if an audio device is paused.
882 *
883 * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
884 * has to bind a stream before any audio will flow.
885 *
886 * Physical devices can not be paused or unpaused, only logical devices
887 * created through SDL_OpenAudioDevice() can be. Physical and invalid device
888 * IDs will report themselves as unpaused here.
889 *
890 * \param devid a device opened by SDL_OpenAudioDevice().
891 * \returns true if device is valid and paused, false otherwise.
892 *
893 * \threadsafety It is safe to call this function from any thread.
894 *
895 * \since This function is available since SDL 3.2.0.
896 *
897 * \sa SDL_PauseAudioDevice
898 * \sa SDL_ResumeAudioDevice
899 */
900extern SDL_DECLSPEC bool SDLCALL SDL_AudioDevicePaused(SDL_AudioDeviceID devid);
901
902/**
903 * Get the gain of an audio device.
904 *
905 * The gain of a device is its volume; a larger gain means a louder output,
906 * with a gain of zero being silence.
907 *
908 * Audio devices default to a gain of 1.0f (no change in output).
909 *
910 * Physical devices may not have their gain changed, only logical devices, and
911 * this function will always return -1.0f when used on physical devices.
912 *
913 * \param devid the audio device to query.
914 * \returns the gain of the device or -1.0f on failure; call SDL_GetError()
915 * for more information.
916 *
917 * \threadsafety It is safe to call this function from any thread.
918 *
919 * \since This function is available since SDL 3.2.0.
920 *
921 * \sa SDL_SetAudioDeviceGain
922 */
923extern SDL_DECLSPEC float SDLCALL SDL_GetAudioDeviceGain(SDL_AudioDeviceID devid);
924
925/**
926 * Change the gain of an audio device.
927 *
928 * The gain of a device is its volume; a larger gain means a louder output,
929 * with a gain of zero being silence.
930 *
931 * Audio devices default to a gain of 1.0f (no change in output).
932 *
933 * Physical devices may not have their gain changed, only logical devices, and
934 * this function will always return false when used on physical devices. While
935 * it might seem attractive to adjust several logical devices at once in this
936 * way, it would allow an app or library to interfere with another portion of
937 * the program's otherwise-isolated devices.
938 *
939 * This is applied, along with any per-audiostream gain, during playback to
940 * the hardware, and can be continuously changed to create various effects. On
941 * recording devices, this will adjust the gain before passing the data into
942 * an audiostream; that recording audiostream can then adjust its gain further
943 * when outputting the data elsewhere, if it likes, but that second gain is
944 * not applied until the data leaves the audiostream again.
945 *
946 * \param devid the audio device on which to change gain.
947 * \param gain the gain. 1.0f is no change, 0.0f is silence.
948 * \returns true on success or false on failure; call SDL_GetError() for more
949 * information.
950 *
951 * \threadsafety It is safe to call this function from any thread, as it holds
952 * a stream-specific mutex while running.
953 *
954 * \since This function is available since SDL 3.2.0.
955 *
956 * \sa SDL_GetAudioDeviceGain
957 */
958extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioDeviceGain(SDL_AudioDeviceID devid, float gain);
959
960/**
961 * Close a previously-opened audio device.
962 *
963 * The application should close open audio devices once they are no longer
964 * needed.
965 *
966 * This function may block briefly while pending audio data is played by the
967 * hardware, so that applications don't drop the last buffer of data they
968 * supplied if terminating immediately afterwards.
969 *
970 * \param devid an audio device id previously returned by
971 * SDL_OpenAudioDevice().
972 *
973 * \threadsafety It is safe to call this function from any thread.
974 *
975 * \since This function is available since SDL 3.2.0.
976 *
977 * \sa SDL_OpenAudioDevice
978 */
979extern SDL_DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID devid);
980
981/**
982 * Bind a list of audio streams to an audio device.
983 *
984 * Audio data will flow through any bound streams. For a playback device, data
985 * for all bound streams will be mixed together and fed to the device. For a
986 * recording device, a copy of recorded data will be provided to each bound
987 * stream.
988 *
989 * Audio streams can only be bound to an open device. This operation is
990 * atomic--all streams bound in the same call will start processing at the
991 * same time, so they can stay in sync. Also: either all streams will be bound
992 * or none of them will be.
993 *
994 * It is an error to bind an already-bound stream; it must be explicitly
995 * unbound first.
996 *
997 * Binding a stream to a device will set its output format for playback
998 * devices, and its input format for recording devices, so they match the
999 * device's settings. The caller is welcome to change the other end of the
1000 * stream's format at any time with SDL_SetAudioStreamFormat(). If the other
1001 * end of the stream's format has never been set (the audio stream was created
1002 * with a NULL audio spec), this function will set it to match the device
1003 * end's format.
1004 *
1005 * \param devid an audio device to bind a stream to.
1006 * \param streams an array of audio streams to bind.
1007 * \param num_streams number streams listed in the `streams` array.
1008 * \returns true on success or false on failure; call SDL_GetError() for more
1009 * information.
1010 *
1011 * \threadsafety It is safe to call this function from any thread.
1012 *
1013 * \since This function is available since SDL 3.2.0.
1014 *
1015 * \sa SDL_BindAudioStreams
1016 * \sa SDL_UnbindAudioStream
1017 * \sa SDL_GetAudioStreamDevice
1018 */
1019extern SDL_DECLSPEC bool SDLCALL SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream * const *streams, int num_streams);
1020
1021/**
1022 * Bind a single audio stream to an audio device.
1023 *
1024 * This is a convenience function, equivalent to calling
1025 * `SDL_BindAudioStreams(devid, &stream, 1)`.
1026 *
1027 * \param devid an audio device to bind a stream to.
1028 * \param stream an audio stream to bind to a device.
1029 * \returns true on success or false on failure; call SDL_GetError() for more
1030 * information.
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_BindAudioStreams
1037 * \sa SDL_UnbindAudioStream
1038 * \sa SDL_GetAudioStreamDevice
1039 */
1040extern SDL_DECLSPEC bool SDLCALL SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream);
1041
1042/**
1043 * Unbind a list of audio streams from their audio devices.
1044 *
1045 * The streams being unbound do not all have to be on the same device. All
1046 * streams on the same device will be unbound atomically (data will stop
1047 * flowing through all unbound streams on the same device at the same time).
1048 *
1049 * Unbinding a stream that isn't bound to a device is a legal no-op.
1050 *
1051 * \param streams an array of audio streams to unbind. Can be NULL or contain
1052 * NULL.
1053 * \param num_streams number streams listed in the `streams` array.
1054 *
1055 * \threadsafety It is safe to call this function from any thread.
1056 *
1057 * \since This function is available since SDL 3.2.0.
1058 *
1059 * \sa SDL_BindAudioStreams
1060 */
1061extern SDL_DECLSPEC void SDLCALL SDL_UnbindAudioStreams(SDL_AudioStream * const *streams, int num_streams);
1062
1063/**
1064 * Unbind a single audio stream from its audio device.
1065 *
1066 * This is a convenience function, equivalent to calling
1067 * `SDL_UnbindAudioStreams(&stream, 1)`.
1068 *
1069 * \param stream an audio stream to unbind from a device. Can be NULL.
1070 *
1071 * \threadsafety It is safe to call this function from any thread.
1072 *
1073 * \since This function is available since SDL 3.2.0.
1074 *
1075 * \sa SDL_BindAudioStream
1076 */
1077extern SDL_DECLSPEC void SDLCALL SDL_UnbindAudioStream(SDL_AudioStream *stream);
1078
1079/**
1080 * Query an audio stream for its currently-bound device.
1081 *
1082 * This reports the logical audio device that an audio stream is currently
1083 * bound to.
1084 *
1085 * If not bound, or invalid, this returns zero, which is not a valid device
1086 * ID.
1087 *
1088 * \param stream the audio stream to query.
1089 * \returns the bound audio device, or 0 if not bound or invalid.
1090 *
1091 * \threadsafety It is safe to call this function from any thread.
1092 *
1093 * \since This function is available since SDL 3.2.0.
1094 *
1095 * \sa SDL_BindAudioStream
1096 * \sa SDL_BindAudioStreams
1097 */
1099
1100/**
1101 * Create a new audio stream.
1102 *
1103 * Note that `src_spec` or `dst_spec` may be NULL, but any attempts to put or
1104 * get data from an audio stream will fail until it has valid specs assigned
1105 * to both ends of the stream. Specs can be assigned later through
1106 * SDL_SetAudioStreamFormat(), or binding the stream to an audio device (which
1107 * will set the format of only the input or output, depending on what kind of
1108 * device the stream was bound to).
1109 *
1110 * \param src_spec the format details of the input audio. May be NULL.
1111 * \param dst_spec the format details of the output audio. May be NULL.
1112 * \returns a new audio stream on success or NULL on failure; call
1113 * SDL_GetError() for more information.
1114 *
1115 * \threadsafety It is safe to call this function from any thread.
1116 *
1117 * \since This function is available since SDL 3.2.0.
1118 *
1119 * \sa SDL_PutAudioStreamData
1120 * \sa SDL_GetAudioStreamData
1121 * \sa SDL_GetAudioStreamAvailable
1122 * \sa SDL_FlushAudioStream
1123 * \sa SDL_ClearAudioStream
1124 * \sa SDL_SetAudioStreamFormat
1125 * \sa SDL_DestroyAudioStream
1126 */
1127extern SDL_DECLSPEC SDL_AudioStream * SDLCALL SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
1128
1129/**
1130 * Get the properties associated with an audio stream.
1131 *
1132 * The application can hang any data it wants here, but the following
1133 * properties are understood by SDL:
1134 *
1135 * - `SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN`: if true (the default), the
1136 * stream be automatically cleaned up when the audio subsystem quits. If set
1137 * to false, the streams will persist beyond that. This property is ignored
1138 * for streams created through SDL_OpenAudioDeviceStream(), and will always
1139 * be cleaned up. Streams that are not cleaned up will still be unbound from
1140 * devices when the audio subsystem quits. This property was added in SDL
1141 * 3.4.0.
1142 *
1143 * \param stream the SDL_AudioStream to query.
1144 * \returns a valid property ID on success or 0 on failure; call
1145 * SDL_GetError() for more information.
1146 *
1147 * \threadsafety It is safe to call this function from any thread.
1148 *
1149 * \since This function is available since SDL 3.2.0.
1150 */
1152
1153#define SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN "SDL.audiostream.auto_cleanup"
1154
1155
1156/**
1157 * Query the current format of an audio stream.
1158 *
1159 * \param stream the SDL_AudioStream to query.
1160 * \param src_spec where to store the input audio format; ignored if NULL.
1161 * \param dst_spec where to store the output audio format; ignored if NULL.
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_SetAudioStreamFormat
1171 */
1172extern SDL_DECLSPEC bool SDLCALL SDL_GetAudioStreamFormat(SDL_AudioStream *stream, SDL_AudioSpec *src_spec, SDL_AudioSpec *dst_spec);
1173
1174/**
1175 * Change the input and output formats of an audio stream.
1176 *
1177 * Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData
1178 * will reflect the new format, and future calls to SDL_PutAudioStreamData
1179 * must provide data in the new input formats.
1180 *
1181 * Data that was previously queued in the stream will still be operated on in
1182 * the format that was current when it was added, which is to say you can put
1183 * the end of a sound file in one format to a stream, change formats for the
1184 * next sound file, and start putting that new data while the previous sound
1185 * file is still queued, and everything will still play back correctly.
1186 *
1187 * If a stream is bound to a device, then the format of the side of the stream
1188 * bound to a device cannot be changed (src_spec for recording devices,
1189 * dst_spec for playback devices). Attempts to make a change to this side will
1190 * be ignored, but this will not report an error. The other side's format can
1191 * be changed.
1192 *
1193 * `src_spec` and `dst_spec` may each be NULL; a NULL spec signals not to
1194 * change the current format for that side of the stream.
1195 *
1196 * \param stream the stream the format is being changed.
1197 * \param src_spec the new format of the audio input; if NULL, it is not
1198 * changed.
1199 * \param dst_spec the new format of the audio output; if NULL, it is not
1200 * changed.
1201 * \returns true on success or false on failure; call SDL_GetError() for more
1202 * information.
1203 *
1204 * \threadsafety It is safe to call this function from any thread, as it holds
1205 * a stream-specific mutex while running.
1206 *
1207 * \since This function is available since SDL 3.2.0.
1208 *
1209 * \sa SDL_GetAudioStreamFormat
1210 * \sa SDL_SetAudioStreamFrequencyRatio
1211 */
1212extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream, const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
1213
1214/**
1215 * Get the frequency ratio of an audio stream.
1216 *
1217 * \param stream the SDL_AudioStream to query.
1218 * \returns the frequency ratio of the stream or 0.0 on failure; call
1219 * SDL_GetError() for more information.
1220 *
1221 * \threadsafety It is safe to call this function from any thread, as it holds
1222 * a stream-specific mutex while running.
1223 *
1224 * \since This function is available since SDL 3.2.0.
1225 *
1226 * \sa SDL_SetAudioStreamFrequencyRatio
1227 */
1228extern SDL_DECLSPEC float SDLCALL SDL_GetAudioStreamFrequencyRatio(SDL_AudioStream *stream);
1229
1230/**
1231 * Change the frequency ratio of an audio stream.
1232 *
1233 * The frequency ratio is used to adjust the rate at which input data is
1234 * consumed. Changing this effectively modifies the speed and pitch of the
1235 * audio. A value greater than 1.0f will play the audio faster, and at a
1236 * higher pitch. A value less than 1.0f will play the audio slower, and at a
1237 * lower pitch. 1.0f means play at normal speed.
1238 *
1239 * This is applied during SDL_GetAudioStreamData, and can be continuously
1240 * changed to create various effects.
1241 *
1242 * \param stream the stream on which the frequency ratio is being changed.
1243 * \param ratio the frequency ratio. 1.0 is normal speed. Must be between 0.01
1244 * and 100.
1245 * \returns true on success or false on failure; call SDL_GetError() for more
1246 * information.
1247 *
1248 * \threadsafety It is safe to call this function from any thread, as it holds
1249 * a stream-specific mutex while running.
1250 *
1251 * \since This function is available since SDL 3.2.0.
1252 *
1253 * \sa SDL_GetAudioStreamFrequencyRatio
1254 * \sa SDL_SetAudioStreamFormat
1255 */
1256extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamFrequencyRatio(SDL_AudioStream *stream, float ratio);
1257
1258/**
1259 * Get the gain of an audio stream.
1260 *
1261 * The gain of a stream is its volume; a larger gain means a louder output,
1262 * with a gain of zero being silence.
1263 *
1264 * Audio streams default to a gain of 1.0f (no change in output).
1265 *
1266 * \param stream the SDL_AudioStream to query.
1267 * \returns the gain of the stream or -1.0f on failure; call SDL_GetError()
1268 * for more information.
1269 *
1270 * \threadsafety It is safe to call this function from any thread, as it holds
1271 * a stream-specific mutex while running.
1272 *
1273 * \since This function is available since SDL 3.2.0.
1274 *
1275 * \sa SDL_SetAudioStreamGain
1276 */
1277extern SDL_DECLSPEC float SDLCALL SDL_GetAudioStreamGain(SDL_AudioStream *stream);
1278
1279/**
1280 * Change the gain of an audio stream.
1281 *
1282 * The gain of a stream is its volume; a larger gain means a louder output,
1283 * with a gain of zero being silence.
1284 *
1285 * Audio streams default to a gain of 1.0f (no change in output).
1286 *
1287 * This is applied during SDL_GetAudioStreamData, and can be continuously
1288 * changed to create various effects.
1289 *
1290 * \param stream the stream on which the gain is being changed.
1291 * \param gain the gain. 1.0f is no change, 0.0f is silence.
1292 * \returns true on success or false on failure; call SDL_GetError() for more
1293 * information.
1294 *
1295 * \threadsafety It is safe to call this function from any thread, as it holds
1296 * a stream-specific mutex while running.
1297 *
1298 * \since This function is available since SDL 3.2.0.
1299 *
1300 * \sa SDL_GetAudioStreamGain
1301 */
1302extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamGain(SDL_AudioStream *stream, float gain);
1303
1304/**
1305 * Get the current input channel map of an audio stream.
1306 *
1307 * Channel maps are optional; most things do not need them, instead passing
1308 * data in the [order that SDL expects](CategoryAudio#channel-layouts).
1309 *
1310 * Audio streams default to no remapping applied. This is represented by
1311 * returning NULL, and does not signify an error.
1312 *
1313 * \param stream the SDL_AudioStream to query.
1314 * \param count On output, set to number of channels in the map. Can be NULL.
1315 * \returns an array of the current channel mapping, with as many elements as
1316 * the current output spec's channels, or NULL if default. This
1317 * should be freed with SDL_free() when it is no longer needed.
1318 *
1319 * \threadsafety It is safe to call this function from any thread, as it holds
1320 * a stream-specific mutex while running.
1321 *
1322 * \since This function is available since SDL 3.2.0.
1323 *
1324 * \sa SDL_SetAudioStreamInputChannelMap
1325 */
1326extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioStreamInputChannelMap(SDL_AudioStream *stream, int *count);
1327
1328/**
1329 * Get the current output channel map of an audio stream.
1330 *
1331 * Channel maps are optional; most things do not need them, instead passing
1332 * data in the [order that SDL expects](CategoryAudio#channel-layouts).
1333 *
1334 * Audio streams default to no remapping applied. This is represented by
1335 * returning NULL, and does not signify an error.
1336 *
1337 * \param stream the SDL_AudioStream to query.
1338 * \param count On output, set to number of channels in the map. Can be NULL.
1339 * \returns an array of the current channel mapping, with as many elements as
1340 * the current output spec's channels, or NULL if default. This
1341 * should be freed with SDL_free() when it is no longer needed.
1342 *
1343 * \threadsafety It is safe to call this function from any thread, as it holds
1344 * a stream-specific mutex while running.
1345 *
1346 * \since This function is available since SDL 3.2.0.
1347 *
1348 * \sa SDL_SetAudioStreamInputChannelMap
1349 */
1350extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioStreamOutputChannelMap(SDL_AudioStream *stream, int *count);
1351
1352/**
1353 * Set the current input channel map of an audio stream.
1354 *
1355 * Channel maps are optional; most things do not need them, instead passing
1356 * data in the [order that SDL expects](CategoryAudio#channel-layouts).
1357 *
1358 * The input channel map reorders data that is added to a stream via
1359 * SDL_PutAudioStreamData. Future calls to SDL_PutAudioStreamData must provide
1360 * data in the new channel order.
1361 *
1362 * Each item in the array represents an input channel, and its value is the
1363 * channel that it should be remapped to. To reverse a stereo signal's left
1364 * and right values, you'd have an array of `{ 1, 0 }`. It is legal to remap
1365 * multiple channels to the same thing, so `{ 1, 1 }` would duplicate the
1366 * right channel to both channels of a stereo signal. An element in the
1367 * channel map set to -1 instead of a valid channel will mute that channel,
1368 * setting it to a silence value.
1369 *
1370 * You cannot change the number of channels through a channel map, just
1371 * reorder/mute them.
1372 *
1373 * Data that was previously queued in the stream will still be operated on in
1374 * the order that was current when it was added, which is to say you can put
1375 * the end of a sound file in one order to a stream, change orders for the
1376 * next sound file, and start putting that new data while the previous sound
1377 * file is still queued, and everything will still play back correctly.
1378 *
1379 * Audio streams default to no remapping applied. Passing a NULL channel map
1380 * is legal, and turns off remapping.
1381 *
1382 * SDL will copy the channel map; the caller does not have to save this array
1383 * after this call.
1384 *
1385 * If `count` is not equal to the current number of channels in the audio
1386 * stream's format, this will fail. This is a safety measure to make sure a
1387 * race condition hasn't changed the format while this call is setting the
1388 * channel map.
1389 *
1390 * Unlike attempting to change the stream's format, the input channel map on a
1391 * stream bound to a recording device is permitted to change at any time; any
1392 * data added to the stream from the device after this call will have the new
1393 * mapping, but previously-added data will still have the prior mapping.
1394 *
1395 * \param stream the SDL_AudioStream to change.
1396 * \param chmap the new channel map, NULL to reset to default.
1397 * \param count The number of channels in the map.
1398 * \returns true on success or false on failure; call SDL_GetError() for more
1399 * information.
1400 *
1401 * \threadsafety It is safe to call this function from any thread, as it holds
1402 * a stream-specific mutex while running. Don't change the
1403 * stream's format to have a different number of channels from a
1404 * different thread at the same time, though!
1405 *
1406 * \since This function is available since SDL 3.2.0.
1407 *
1408 * \sa SDL_SetAudioStreamOutputChannelMap
1409 */
1410extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamInputChannelMap(SDL_AudioStream *stream, const int *chmap, int count);
1411
1412/**
1413 * Set the current output channel map of an audio stream.
1414 *
1415 * Channel maps are optional; most things do not need them, instead passing
1416 * data in the [order that SDL expects](CategoryAudio#channel-layouts).
1417 *
1418 * The output channel map reorders data that is leaving a stream via
1419 * SDL_GetAudioStreamData.
1420 *
1421 * Each item in the array represents an input channel, and its value is the
1422 * channel that it should be remapped to. To reverse a stereo signal's left
1423 * and right values, you'd have an array of `{ 1, 0 }`. It is legal to remap
1424 * multiple channels to the same thing, so `{ 1, 1 }` would duplicate the
1425 * right channel to both channels of a stereo signal. An element in the
1426 * channel map set to -1 instead of a valid channel will mute that channel,
1427 * setting it to a silence value.
1428 *
1429 * You cannot change the number of channels through a channel map, just
1430 * reorder/mute them.
1431 *
1432 * The output channel map can be changed at any time, as output remapping is
1433 * applied during SDL_GetAudioStreamData.
1434 *
1435 * Audio streams default to no remapping applied. Passing a NULL channel map
1436 * is legal, and turns off remapping.
1437 *
1438 * SDL will copy the channel map; the caller does not have to save this array
1439 * after this call.
1440 *
1441 * If `count` is not equal to the current number of channels in the audio
1442 * stream's format, this will fail. This is a safety measure to make sure a
1443 * race condition hasn't changed the format while this call is setting the
1444 * channel map.
1445 *
1446 * Unlike attempting to change the stream's format, the output channel map on
1447 * a stream bound to a recording device is permitted to change at any time;
1448 * any data added to the stream after this call will have the new mapping, but
1449 * previously-added data will still have the prior mapping. When the channel
1450 * map doesn't match the hardware's channel layout, SDL will convert the data
1451 * before feeding it to the device for playback.
1452 *
1453 * \param stream the SDL_AudioStream to change.
1454 * \param chmap the new channel map, NULL to reset to default.
1455 * \param count The number of channels in the map.
1456 * \returns true on success or false on failure; call SDL_GetError() for more
1457 * information.
1458 *
1459 * \threadsafety It is safe to call this function from any thread, as it holds
1460 * a stream-specific mutex while running. Don't change the
1461 * stream's format to have a different number of channels from a
1462 * a different thread at the same time, though!
1463 *
1464 * \since This function is available since SDL 3.2.0.
1465 *
1466 * \sa SDL_SetAudioStreamInputChannelMap
1467 */
1468extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamOutputChannelMap(SDL_AudioStream *stream, const int *chmap, int count);
1469
1470/**
1471 * Add data to the stream.
1472 *
1473 * This data must match the format/channels/samplerate specified in the latest
1474 * call to SDL_SetAudioStreamFormat, or the format specified when creating the
1475 * stream if it hasn't been changed.
1476 *
1477 * Note that this call simply copies the unconverted data for later. This is
1478 * different than SDL2, where data was converted during the Put call and the
1479 * Get call would just dequeue the previously-converted data.
1480 *
1481 * \param stream the stream the audio data is being added to.
1482 * \param buf a pointer to the audio data to add.
1483 * \param len the number of bytes to write to the stream.
1484 * \returns true on success or false on failure; call SDL_GetError() for more
1485 * information.
1486 *
1487 * \threadsafety It is safe to call this function from any thread, but if the
1488 * stream has a callback set, the caller might need to manage
1489 * extra locking.
1490 *
1491 * \since This function is available since SDL 3.2.0.
1492 *
1493 * \sa SDL_ClearAudioStream
1494 * \sa SDL_FlushAudioStream
1495 * \sa SDL_GetAudioStreamData
1496 * \sa SDL_GetAudioStreamQueued
1497 */
1498extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
1499
1500/**
1501 * A callback that fires for completed SDL_PutAudioStreamDataNoCopy() data.
1502 *
1503 * When using SDL_PutAudioStreamDataNoCopy() to provide data to an
1504 * SDL_AudioStream, it's not safe to dispose of the data until the stream has
1505 * completely consumed it. Often times it's difficult to know exactly when
1506 * this has happened.
1507 *
1508 * This callback fires once when the stream no longer needs the buffer,
1509 * allowing the app to easily free or reuse it.
1510 *
1511 * \param userdata an opaque pointer provided by the app for their personal
1512 * use.
1513 * \param buf the pointer provided to SDL_PutAudioStreamDataNoCopy().
1514 * \param buflen the size of buffer, in bytes, provided to
1515 * SDL_PutAudioStreamDataNoCopy().
1516 *
1517 * \threadsafety This callbacks may run from any thread, so if you need to
1518 * protect shared data, you should use SDL_LockAudioStream to
1519 * serialize access; this lock will be held before your callback
1520 * is called, so your callback does not need to manage the lock
1521 * explicitly.
1522 *
1523 * \since This datatype is available since SDL 3.4.0.
1524 *
1525 * \sa SDL_SetAudioStreamGetCallback
1526 * \sa SDL_SetAudioStreamPutCallback
1527 */
1528typedef void (SDLCALL *SDL_AudioStreamDataCompleteCallback)(void *userdata, const void *buf, int buflen);
1529
1530/**
1531 * Add external data to an audio stream without copying it.
1532 *
1533 * Unlike SDL_PutAudioStreamData(), this function does not make a copy of the
1534 * provided data, instead storing the provided pointer. This means that the
1535 * put operation does not need to allocate and copy the data, but the original
1536 * data must remain available until the stream is done with it, either by
1537 * being read from the stream in its entirety, or a call to
1538 * SDL_ClearAudioStream() or SDL_DestroyAudioStream().
1539 *
1540 * The data must match the format/channels/samplerate specified in the latest
1541 * call to SDL_SetAudioStreamFormat, or the format specified when creating the
1542 * stream if it hasn't been changed.
1543 *
1544 * An optional callback may be provided, which is called when the stream no
1545 * longer needs the data. Once this callback fires, the stream will not access
1546 * the data again. This callback will fire for any reason the data is no
1547 * longer needed, including clearing or destroying the stream.
1548 *
1549 * Note that there is still an allocation to store tracking information, so
1550 * this function is more efficient for larger blocks of data. If you're
1551 * planning to put a few samples at a time, it will be more efficient to use
1552 * SDL_PutAudioStreamData(), which allocates and buffers in blocks.
1553 *
1554 * \param stream the stream the audio data is being added to.
1555 * \param buf a pointer to the audio data to add.
1556 * \param len the number of bytes to add to the stream.
1557 * \param callback the callback function to call when the data is no longer
1558 * needed by the stream. May be NULL.
1559 * \param userdata an opaque pointer provided to the callback for its own
1560 * personal use.
1561 * \returns true on success or false on failure; call SDL_GetError() for more
1562 * information.
1563 *
1564 * \threadsafety It is safe to call this function from any thread, but if the
1565 * stream has a callback set, the caller might need to manage
1566 * extra locking.
1567 *
1568 * \since This function is available since SDL 3.4.0.
1569 *
1570 * \sa SDL_ClearAudioStream
1571 * \sa SDL_FlushAudioStream
1572 * \sa SDL_GetAudioStreamData
1573 * \sa SDL_GetAudioStreamQueued
1574 */
1575extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamDataNoCopy(SDL_AudioStream *stream, const void *buf, int len, SDL_AudioStreamDataCompleteCallback callback, void *userdata);
1576
1577/**
1578 * Add data to the stream with each channel in a separate array.
1579 *
1580 * This data must match the format/channels/samplerate specified in the latest
1581 * call to SDL_SetAudioStreamFormat, or the format specified when creating the
1582 * stream if it hasn't been changed.
1583 *
1584 * The data will be interleaved and queued. Note that SDL_AudioStream only
1585 * operates on interleaved data, so this is simply a convenience function for
1586 * easily queueing data from sources that provide separate arrays. There is no
1587 * equivalent function to retrieve planar data.
1588 *
1589 * The arrays in `channel_buffers` are ordered as they are to be interleaved;
1590 * the first array will be the first sample in the interleaved data. Any
1591 * individual array may be NULL; in this case, silence will be interleaved for
1592 * that channel.
1593 *
1594 * `num_channels` specifies how many arrays are in `channel_buffers`. This can
1595 * be used as a safety to prevent overflow, in case the stream format has
1596 * changed elsewhere. If more channels are specified than the current input
1597 * spec, they are ignored. If less channels are specified, the missing arrays
1598 * are treated as if they are NULL (silence is written to those channels). If
1599 * the count is -1, SDL will assume the array count matches the current input
1600 * spec.
1601 *
1602 * Note that `num_samples` is the number of _samples per array_. This can also
1603 * be thought of as the number of _sample frames_ to be queued. A value of 1
1604 * with stereo arrays will queue two samples to the stream. This is different
1605 * than SDL_PutAudioStreamData, which wants the size of a single array in
1606 * bytes.
1607 *
1608 * \param stream the stream the audio data is being added to.
1609 * \param channel_buffers a pointer to an array of arrays, one array per
1610 * channel.
1611 * \param num_channels the number of arrays in `channel_buffers` or -1.
1612 * \param num_samples the number of _samples_ per array to write to the
1613 * stream.
1614 * \returns true on success or false on failure; call SDL_GetError() for more
1615 * information.
1616 *
1617 * \threadsafety It is safe to call this function from any thread, but if the
1618 * stream has a callback set, the caller might need to manage
1619 * extra locking.
1620 *
1621 * \since This function is available since SDL 3.4.0.
1622 *
1623 * \sa SDL_ClearAudioStream
1624 * \sa SDL_FlushAudioStream
1625 * \sa SDL_GetAudioStreamData
1626 * \sa SDL_GetAudioStreamQueued
1627 */
1628extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void * const *channel_buffers, int num_channels, int num_samples);
1629
1630/**
1631 * Get converted/resampled data from the stream.
1632 *
1633 * The input/output data format/channels/samplerate is specified when creating
1634 * the stream, and can be changed after creation by calling
1635 * SDL_SetAudioStreamFormat.
1636 *
1637 * Note that any conversion and resampling necessary is done during this call,
1638 * and SDL_PutAudioStreamData simply queues unconverted data for later. This
1639 * is different than SDL2, where that work was done while inputting new data
1640 * to the stream and requesting the output just copied the converted data.
1641 *
1642 * \param stream the stream the audio is being requested from.
1643 * \param buf a buffer to fill with audio data.
1644 * \param len the maximum number of bytes to fill.
1645 * \returns the number of bytes read from the stream or -1 on failure; call
1646 * SDL_GetError() for more information.
1647 *
1648 * \threadsafety It is safe to call this function from any thread, but if the
1649 * stream has a callback set, the caller might need to manage
1650 * extra locking.
1651 *
1652 * \since This function is available since SDL 3.2.0.
1653 *
1654 * \sa SDL_ClearAudioStream
1655 * \sa SDL_GetAudioStreamAvailable
1656 * \sa SDL_PutAudioStreamData
1657 */
1658extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
1659
1660/**
1661 * Get the number of converted/resampled bytes available.
1662 *
1663 * The stream may be buffering data behind the scenes until it has enough to
1664 * resample correctly, so this number might be lower than what you expect, or
1665 * even be zero. Add more data or flush the stream if you need the data now.
1666 *
1667 * If the stream has so much data that it would overflow an int, the return
1668 * value is clamped to a maximum value, but no queued data is lost; if there
1669 * are gigabytes of data queued, the app might need to read some of it with
1670 * SDL_GetAudioStreamData before this function's return value is no longer
1671 * clamped.
1672 *
1673 * \param stream the audio stream to query.
1674 * \returns the number of converted/resampled bytes available or -1 on
1675 * failure; call SDL_GetError() for more information.
1676 *
1677 * \threadsafety It is safe to call this function from any thread.
1678 *
1679 * \since This function is available since SDL 3.2.0.
1680 *
1681 * \sa SDL_GetAudioStreamData
1682 * \sa SDL_PutAudioStreamData
1683 */
1684extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
1685
1686
1687/**
1688 * Get the number of bytes currently queued.
1689 *
1690 * This is the number of bytes put into a stream as input, not the number that
1691 * can be retrieved as output. Because of several details, it's not possible
1692 * to calculate one number directly from the other. If you need to know how
1693 * much usable data can be retrieved right now, you should use
1694 * SDL_GetAudioStreamAvailable() and not this function.
1695 *
1696 * Note that audio streams can change their input format at any time, even if
1697 * there is still data queued in a different format, so the returned byte
1698 * count will not necessarily match the number of _sample frames_ available.
1699 * Users of this API should be aware of format changes they make when feeding
1700 * a stream and plan accordingly.
1701 *
1702 * Queued data is not converted until it is consumed by
1703 * SDL_GetAudioStreamData, so this value should be representative of the exact
1704 * data that was put into the stream.
1705 *
1706 * If the stream has so much data that it would overflow an int, the return
1707 * value is clamped to a maximum value, but no queued data is lost; if there
1708 * are gigabytes of data queued, the app might need to read some of it with
1709 * SDL_GetAudioStreamData before this function's return value is no longer
1710 * clamped.
1711 *
1712 * \param stream the audio stream to query.
1713 * \returns the number of bytes queued or -1 on failure; call SDL_GetError()
1714 * for more information.
1715 *
1716 * \threadsafety It is safe to call this function from any thread.
1717 *
1718 * \since This function is available since SDL 3.2.0.
1719 *
1720 * \sa SDL_PutAudioStreamData
1721 * \sa SDL_ClearAudioStream
1722 */
1723extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamQueued(SDL_AudioStream *stream);
1724
1725
1726/**
1727 * Tell the stream that you're done sending data, and anything being buffered
1728 * should be converted/resampled and made available immediately.
1729 *
1730 * It is legal to add more data to a stream after flushing, but there may be
1731 * audio gaps in the output. Generally this is intended to signal the end of
1732 * input, so the complete output becomes available.
1733 *
1734 * \param stream the audio stream to flush.
1735 * \returns true on success or false on failure; call SDL_GetError() for more
1736 * information.
1737 *
1738 * \threadsafety It is safe to call this function from any thread.
1739 *
1740 * \since This function is available since SDL 3.2.0.
1741 *
1742 * \sa SDL_PutAudioStreamData
1743 */
1744extern SDL_DECLSPEC bool SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
1745
1746/**
1747 * Clear any pending data in the stream.
1748 *
1749 * This drops any queued data, so there will be nothing to read from the
1750 * stream until more is added.
1751 *
1752 * \param stream the audio stream to clear.
1753 * \returns true on success or false on failure; call SDL_GetError() for more
1754 * information.
1755 *
1756 * \threadsafety It is safe to call this function from any thread.
1757 *
1758 * \since This function is available since SDL 3.2.0.
1759 *
1760 * \sa SDL_GetAudioStreamAvailable
1761 * \sa SDL_GetAudioStreamData
1762 * \sa SDL_GetAudioStreamQueued
1763 * \sa SDL_PutAudioStreamData
1764 */
1765extern SDL_DECLSPEC bool SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
1766
1767/**
1768 * Use this function to pause audio playback on the audio device associated
1769 * with an audio stream.
1770 *
1771 * This function pauses audio processing for a given device. Any bound audio
1772 * streams will not progress, and no audio will be generated. Pausing one
1773 * device does not prevent other unpaused devices from running.
1774 *
1775 * Pausing a device can be useful to halt all audio without unbinding all the
1776 * audio streams. This might be useful while a game is paused, or a level is
1777 * loading, etc.
1778 *
1779 * \param stream the audio stream associated with the audio device to pause.
1780 * \returns true on success or false on failure; call SDL_GetError() for more
1781 * information.
1782 *
1783 * \threadsafety It is safe to call this function from any thread.
1784 *
1785 * \since This function is available since SDL 3.2.0.
1786 *
1787 * \sa SDL_ResumeAudioStreamDevice
1788 */
1789extern SDL_DECLSPEC bool SDLCALL SDL_PauseAudioStreamDevice(SDL_AudioStream *stream);
1790
1791/**
1792 * Use this function to unpause audio playback on the audio device associated
1793 * with an audio stream.
1794 *
1795 * This function unpauses audio processing for a given device that has
1796 * previously been paused. Once unpaused, any bound audio streams will begin
1797 * to progress again, and audio can be generated.
1798 *
1799 * SDL_OpenAudioDeviceStream opens audio devices in a paused state, so this
1800 * function call is required for audio playback to begin on such devices.
1801 *
1802 * \param stream the audio stream associated with the audio device to resume.
1803 * \returns true on success or false on failure; call SDL_GetError() for more
1804 * information.
1805 *
1806 * \threadsafety It is safe to call this function from any thread.
1807 *
1808 * \since This function is available since SDL 3.2.0.
1809 *
1810 * \sa SDL_PauseAudioStreamDevice
1811 */
1812extern SDL_DECLSPEC bool SDLCALL SDL_ResumeAudioStreamDevice(SDL_AudioStream *stream);
1813
1814/**
1815 * Use this function to query if an audio device associated with a stream is
1816 * paused.
1817 *
1818 * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
1819 * has to bind a stream before any audio will flow.
1820 *
1821 * \param stream the audio stream associated with the audio device to query.
1822 * \returns true if device is valid and paused, false otherwise.
1823 *
1824 * \threadsafety It is safe to call this function from any thread.
1825 *
1826 * \since This function is available since SDL 3.2.0.
1827 *
1828 * \sa SDL_PauseAudioStreamDevice
1829 * \sa SDL_ResumeAudioStreamDevice
1830 */
1831extern SDL_DECLSPEC bool SDLCALL SDL_AudioStreamDevicePaused(SDL_AudioStream *stream);
1832
1833
1834/**
1835 * Lock an audio stream for serialized access.
1836 *
1837 * Each SDL_AudioStream has an internal mutex it uses to protect its data
1838 * structures from threading conflicts. This function allows an app to lock
1839 * that mutex, which could be useful if registering callbacks on this stream.
1840 *
1841 * One does not need to lock a stream to use in it most cases, as the stream
1842 * manages this lock internally. However, this lock is held during callbacks,
1843 * which may run from arbitrary threads at any time, so if an app needs to
1844 * protect shared data during those callbacks, locking the stream guarantees
1845 * that the callback is not running while the lock is held.
1846 *
1847 * As this is just a wrapper over SDL_LockMutex for an internal lock; it has
1848 * all the same attributes (recursive locks are allowed, etc).
1849 *
1850 * \param stream the audio stream to lock.
1851 * \returns true on success or false on failure; call SDL_GetError() for more
1852 * information.
1853 *
1854 * \threadsafety It is safe to call this function from any thread.
1855 *
1856 * \since This function is available since SDL 3.2.0.
1857 *
1858 * \sa SDL_UnlockAudioStream
1859 */
1860extern SDL_DECLSPEC bool SDLCALL SDL_LockAudioStream(SDL_AudioStream *stream);
1861
1862
1863/**
1864 * Unlock an audio stream for serialized access.
1865 *
1866 * This unlocks an audio stream after a call to SDL_LockAudioStream.
1867 *
1868 * \param stream the audio stream to unlock.
1869 * \returns true on success or false on failure; call SDL_GetError() for more
1870 * information.
1871 *
1872 * \threadsafety You should only call this from the same thread that
1873 * previously called SDL_LockAudioStream.
1874 *
1875 * \since This function is available since SDL 3.2.0.
1876 *
1877 * \sa SDL_LockAudioStream
1878 */
1879extern SDL_DECLSPEC bool SDLCALL SDL_UnlockAudioStream(SDL_AudioStream *stream);
1880
1881/**
1882 * A callback that fires when data passes through an SDL_AudioStream.
1883 *
1884 * Apps can (optionally) register a callback with an audio stream that is
1885 * called when data is added with SDL_PutAudioStreamData, or requested with
1886 * SDL_GetAudioStreamData.
1887 *
1888 * Two values are offered here: one is the amount of additional data needed to
1889 * satisfy the immediate request (which might be zero if the stream already
1890 * has enough data queued) and the other is the total amount being requested.
1891 * In a Get call triggering a Put callback, these values can be different. In
1892 * a Put call triggering a Get callback, these values are always the same.
1893 *
1894 * Byte counts might be slightly overestimated due to buffering or resampling,
1895 * and may change from call to call.
1896 *
1897 * This callback is not required to do anything. Generally this is useful for
1898 * adding/reading data on demand, and the app will often put/get data as
1899 * appropriate, but the system goes on with the data currently available to it
1900 * if this callback does nothing.
1901 *
1902 * Do not call SDL_DestroyAudioStream() on `stream` during this callback.
1903 *
1904 * \param stream the SDL audio stream associated with this callback.
1905 * \param additional_amount the amount of data, in bytes, that is needed right
1906 * now.
1907 * \param total_amount the total amount of data requested, in bytes, that is
1908 * requested or available.
1909 * \param userdata an opaque pointer provided by the app for their personal
1910 * use.
1911 *
1912 * \threadsafety This callbacks may run from any thread, so if you need to
1913 * protect shared data, you should use SDL_LockAudioStream to
1914 * serialize access; this lock will be held before your callback
1915 * is called, so your callback does not need to manage the lock
1916 * explicitly.
1917 *
1918 * \since This datatype is available since SDL 3.2.0.
1919 *
1920 * \sa SDL_SetAudioStreamGetCallback
1921 * \sa SDL_SetAudioStreamPutCallback
1922 */
1923typedef void (SDLCALL *SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount);
1924
1925/**
1926 * Set a callback that runs when data is requested from an audio stream.
1927 *
1928 * This callback is called _before_ data is obtained from the stream, giving
1929 * the callback the chance to add more on-demand.
1930 *
1931 * The callback can (optionally) call SDL_PutAudioStreamData() to add more
1932 * audio to the stream during this call; if needed, the request that triggered
1933 * this callback will obtain the new data immediately.
1934 *
1935 * The callback's `additional_amount` argument is roughly how many bytes of
1936 * _unconverted_ data (in the stream's input format) is needed by the caller,
1937 * although this may overestimate a little for safety. This takes into account
1938 * how much is already in the stream and only asks for any extra necessary to
1939 * resolve the request, which means the callback may be asked for zero bytes,
1940 * and a different amount on each call.
1941 *
1942 * The callback is not required to supply exact amounts; it is allowed to
1943 * supply too much or too little or none at all. The caller will get what's
1944 * available, up to the amount they requested, regardless of this callback's
1945 * outcome.
1946 *
1947 * Clearing or flushing an audio stream does not call this callback.
1948 *
1949 * This function obtains the stream's lock, which means any existing callback
1950 * (get or put) in progress will finish running before setting the new
1951 * callback.
1952 *
1953 * Setting a NULL function turns off the callback.
1954 *
1955 * \param stream the audio stream to set the new callback on.
1956 * \param callback the new callback function to call when data is requested
1957 * from the stream.
1958 * \param userdata an opaque pointer provided to the callback for its own
1959 * personal use.
1960 * \returns true on success or false on failure; call SDL_GetError() for more
1961 * information. This only fails if `stream` is NULL.
1962 *
1963 * \threadsafety It is safe to call this function from any thread.
1964 *
1965 * \since This function is available since SDL 3.2.0.
1966 *
1967 * \sa SDL_SetAudioStreamPutCallback
1968 */
1969extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
1970
1971/**
1972 * Set a callback that runs when data is added to an audio stream.
1973 *
1974 * This callback is called _after_ the data is added to the stream, giving the
1975 * callback the chance to obtain it immediately.
1976 *
1977 * The callback can (optionally) call SDL_GetAudioStreamData() to obtain audio
1978 * from the stream during this call.
1979 *
1980 * The callback's `additional_amount` argument is how many bytes of
1981 * _converted_ data (in the stream's output format) was provided by the
1982 * caller, although this may underestimate a little for safety. This value
1983 * might be less than what is currently available in the stream, if data was
1984 * already there, and might be less than the caller provided if the stream
1985 * needs to keep a buffer to aid in resampling. Which means the callback may
1986 * be provided with zero bytes, and a different amount on each call.
1987 *
1988 * The callback may call SDL_GetAudioStreamAvailable to see the total amount
1989 * currently available to read from the stream, instead of the total provided
1990 * by the current call.
1991 *
1992 * The callback is not required to obtain all data. It is allowed to read less
1993 * or none at all. Anything not read now simply remains in the stream for
1994 * later access.
1995 *
1996 * Clearing or flushing an audio stream does not call this callback.
1997 *
1998 * This function obtains the stream's lock, which means any existing callback
1999 * (get or put) in progress will finish running before setting the new
2000 * callback.
2001 *
2002 * Setting a NULL function turns off the callback.
2003 *
2004 * \param stream the audio stream to set the new callback on.
2005 * \param callback the new callback function to call when data is added to the
2006 * stream.
2007 * \param userdata an opaque pointer provided to the callback for its own
2008 * personal use.
2009 * \returns true on success or false on failure; call SDL_GetError() for more
2010 * information. This only fails if `stream` is NULL.
2011 *
2012 * \threadsafety It is safe to call this function from any thread.
2013 *
2014 * \since This function is available since SDL 3.2.0.
2015 *
2016 * \sa SDL_SetAudioStreamGetCallback
2017 */
2018extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
2019
2020
2021/**
2022 * Free an audio stream.
2023 *
2024 * This will release all allocated data, including any audio that is still
2025 * queued. You do not need to manually clear the stream first.
2026 *
2027 * If this stream was bound to an audio device, it is unbound during this
2028 * call. If this stream was created with SDL_OpenAudioDeviceStream, the audio
2029 * device that was opened alongside this stream's creation will be closed,
2030 * too.
2031 *
2032 * \param stream the audio stream to destroy.
2033 *
2034 * \threadsafety It is safe to call this function from any thread.
2035 *
2036 * \since This function is available since SDL 3.2.0.
2037 *
2038 * \sa SDL_CreateAudioStream
2039 */
2040extern SDL_DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
2041
2042
2043/**
2044 * Convenience function for straightforward audio init for the common case.
2045 *
2046 * If all your app intends to do is provide a single source of PCM audio, this
2047 * function allows you to do all your audio setup in a single call.
2048 *
2049 * This is also intended to be a clean means to migrate apps from SDL2.
2050 *
2051 * This function will open an audio device, create a stream and bind it.
2052 * Unlike other methods of setup, the audio device will be closed when this
2053 * stream is destroyed, so the app can treat the returned SDL_AudioStream as
2054 * the only object needed to manage audio playback.
2055 *
2056 * Also unlike other functions, the audio device begins paused. This is to map
2057 * more closely to SDL2-style behavior, since there is no extra step here to
2058 * bind a stream to begin audio flowing. The audio device should be resumed
2059 * with SDL_ResumeAudioStreamDevice().
2060 *
2061 * This function works with both playback and recording devices.
2062 *
2063 * The `spec` parameter represents the app's side of the audio stream. That
2064 * is, for recording audio, this will be the output format, and for playing
2065 * audio, this will be the input format. If spec is NULL, the system will
2066 * choose the format, and the app can use SDL_GetAudioStreamFormat() to obtain
2067 * this information later.
2068 *
2069 * If you don't care about opening a specific audio device, you can (and
2070 * probably _should_), use SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK for playback and
2071 * SDL_AUDIO_DEVICE_DEFAULT_RECORDING for recording.
2072 *
2073 * One can optionally provide a callback function; if NULL, the app is
2074 * expected to queue audio data for playback (or unqueue audio data if
2075 * capturing). Otherwise, the callback will begin to fire once the device is
2076 * unpaused.
2077 *
2078 * Destroying the returned stream with SDL_DestroyAudioStream will also close
2079 * the audio device associated with this stream.
2080 *
2081 * \param devid an audio device to open, or SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK
2082 * or SDL_AUDIO_DEVICE_DEFAULT_RECORDING.
2083 * \param spec the audio stream's data format. Can be NULL.
2084 * \param callback a callback where the app will provide new data for
2085 * playback, or receive new data for recording. Can be NULL,
2086 * in which case the app will need to call
2087 * SDL_PutAudioStreamData or SDL_GetAudioStreamData as
2088 * necessary.
2089 * \param userdata app-controlled pointer passed to callback. Can be NULL.
2090 * Ignored if callback is NULL.
2091 * \returns an audio stream on success, ready to use, or NULL on failure; call
2092 * SDL_GetError() for more information. When done with this stream,
2093 * call SDL_DestroyAudioStream to free resources and close the
2094 * device.
2095 *
2096 * \threadsafety It is safe to call this function from any thread.
2097 *
2098 * \since This function is available since SDL 3.2.0.
2099 *
2100 * \sa SDL_GetAudioStreamDevice
2101 * \sa SDL_ResumeAudioStreamDevice
2102 */
2103extern SDL_DECLSPEC SDL_AudioStream * SDLCALL SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata);
2104
2105/**
2106 * A callback that fires when data is about to be fed to an audio device.
2107 *
2108 * This is useful for accessing the final mix, perhaps for writing a
2109 * visualizer or applying a final effect to the audio data before playback.
2110 *
2111 * This callback should run as quickly as possible and not block for any
2112 * significant time, as this callback delays submission of data to the audio
2113 * device, which can cause audio playback problems.
2114 *
2115 * The postmix callback _must_ be able to handle any audio data format
2116 * specified in `spec`, which can change between callbacks if the audio device
2117 * changed. However, this only covers frequency and channel count; data is
2118 * always provided here in SDL_AUDIO_F32 format.
2119 *
2120 * The postmix callback runs _after_ logical device gain and audiostream gain
2121 * have been applied, which is to say you can make the output data louder at
2122 * this point than the gain settings would suggest.
2123 *
2124 * \param userdata a pointer provided by the app through
2125 * SDL_SetAudioPostmixCallback, for its own use.
2126 * \param spec the current format of audio that is to be submitted to the
2127 * audio device.
2128 * \param buffer the buffer of audio samples to be submitted. The callback can
2129 * inspect and/or modify this data.
2130 * \param buflen the size of `buffer` in bytes.
2131 *
2132 * \threadsafety This will run from a background thread owned by SDL. The
2133 * application is responsible for locking resources the callback
2134 * touches that need to be protected.
2135 *
2136 * \since This datatype is available since SDL 3.2.0.
2137 *
2138 * \sa SDL_SetAudioPostmixCallback
2139 */
2140typedef void (SDLCALL *SDL_AudioPostmixCallback)(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen);
2141
2142/**
2143 * Set a callback that fires when data is about to be fed to an audio device.
2144 *
2145 * This is useful for accessing the final mix, perhaps for writing a
2146 * visualizer or applying a final effect to the audio data before playback.
2147 *
2148 * The buffer is the final mix of all bound audio streams on an opened device;
2149 * this callback will fire regularly for any device that is both opened and
2150 * unpaused. If there is no new data to mix, either because no streams are
2151 * bound to the device or all the streams are empty, this callback will still
2152 * fire with the entire buffer set to silence.
2153 *
2154 * This callback is allowed to make changes to the data; the contents of the
2155 * buffer after this call is what is ultimately passed along to the hardware.
2156 *
2157 * The callback is always provided the data in float format (values from -1.0f
2158 * to 1.0f), but the number of channels or sample rate may be different than
2159 * the format the app requested when opening the device; SDL might have had to
2160 * manage a conversion behind the scenes, or the playback might have jumped to
2161 * new physical hardware when a system default changed, etc. These details may
2162 * change between calls. Accordingly, the size of the buffer might change
2163 * between calls as well.
2164 *
2165 * This callback can run at any time, and from any thread; if you need to
2166 * serialize access to your app's data, you should provide and use a mutex or
2167 * other synchronization device.
2168 *
2169 * All of this to say: there are specific needs this callback can fulfill, but
2170 * it is not the simplest interface. Apps should generally provide audio in
2171 * their preferred format through an SDL_AudioStream and let SDL handle the
2172 * difference.
2173 *
2174 * This function is extremely time-sensitive; the callback should do the least
2175 * amount of work possible and return as quickly as it can. The longer the
2176 * callback runs, the higher the risk of audio dropouts or other problems.
2177 *
2178 * This function will block until the audio device is in between iterations,
2179 * so any existing callback that might be running will finish before this
2180 * function sets the new callback and returns.
2181 *
2182 * Setting a NULL callback function disables any previously-set callback.
2183 *
2184 * \param devid the ID of an opened audio device.
2185 * \param callback a callback function to be called. Can be NULL.
2186 * \param userdata app-controlled pointer passed to callback. Can be NULL.
2187 * \returns true on success or false on failure; call SDL_GetError() for more
2188 * information.
2189 *
2190 * \threadsafety It is safe to call this function from any thread.
2191 *
2192 * \since This function is available since SDL 3.2.0.
2193 */
2194extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata);
2195
2196
2197/**
2198 * Load the audio data of a WAVE file into memory.
2199 *
2200 * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
2201 * be valid pointers. The entire data portion of the file is then loaded into
2202 * memory and decoded if necessary.
2203 *
2204 * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
2205 * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
2206 * A-law and mu-law (8 bits). Other formats are currently unsupported and
2207 * cause an error.
2208 *
2209 * If this function succeeds, the return value is zero and the pointer to the
2210 * audio data allocated by the function is written to `audio_buf` and its
2211 * length in bytes to `audio_len`. The SDL_AudioSpec members `freq`,
2212 * `channels`, and `format` are set to the values of the audio data in the
2213 * buffer.
2214 *
2215 * It's necessary to use SDL_free() to free the audio data returned in
2216 * `audio_buf` when it is no longer used.
2217 *
2218 * Because of the underspecification of the .WAV format, there are many
2219 * problematic files in the wild that cause issues with strict decoders. To
2220 * provide compatibility with these files, this decoder is lenient in regards
2221 * to the truncation of the file, the fact chunk, and the size of the RIFF
2222 * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
2223 * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
2224 * tune the behavior of the loading process.
2225 *
2226 * Any file that is invalid (due to truncation, corruption, or wrong values in
2227 * the headers), too big, or unsupported causes an error. Additionally, any
2228 * critical I/O error from the data source will terminate the loading process
2229 * with an error. The function returns NULL on error and in all cases (with
2230 * the exception of `src` being NULL), an appropriate error message will be
2231 * set.
2232 *
2233 * It is required that the data source supports seeking.
2234 *
2235 * Example:
2236 *
2237 * ```c
2238 * SDL_LoadWAV_IO(SDL_IOFromFile("sample.wav", "rb"), true, &spec, &buf, &len);
2239 * ```
2240 *
2241 * Note that the SDL_LoadWAV function does this same thing for you, but in a
2242 * less messy way:
2243 *
2244 * ```c
2245 * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
2246 * ```
2247 *
2248 * \param src the data source for the WAVE data.
2249 * \param closeio if true, calls SDL_CloseIO() on `src` before returning, even
2250 * in the case of an error.
2251 * \param spec a pointer to an SDL_AudioSpec that will be set to the WAVE
2252 * data's format details on successful return.
2253 * \param audio_buf a pointer filled with the audio data, allocated by the
2254 * function.
2255 * \param audio_len a pointer filled with the length of the audio data buffer
2256 * in bytes.
2257 * \returns true on success. `audio_buf` will be filled with a pointer to an
2258 * allocated buffer containing the audio data, and `audio_len` is
2259 * filled with the length of that audio buffer in bytes.
2260 *
2261 * This function returns false if the .WAV file cannot be opened,
2262 * uses an unknown data format, or is corrupt; call SDL_GetError()
2263 * for more information.
2264 *
2265 * When the application is done with the data returned in
2266 * `audio_buf`, it should call SDL_free() to dispose of it.
2267 *
2268 * \threadsafety It is safe to call this function from any thread.
2269 *
2270 * \since This function is available since SDL 3.2.0.
2271 *
2272 * \sa SDL_free
2273 * \sa SDL_LoadWAV
2274 */
2275extern SDL_DECLSPEC bool SDLCALL SDL_LoadWAV_IO(SDL_IOStream *src, bool closeio, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);
2276
2277/**
2278 * Loads a WAV from a file path.
2279 *
2280 * This is a convenience function that is effectively the same as:
2281 *
2282 * ```c
2283 * SDL_LoadWAV_IO(SDL_IOFromFile(path, "rb"), true, spec, audio_buf, audio_len);
2284 * ```
2285 *
2286 * \param path the file path of the WAV file to open.
2287 * \param spec a pointer to an SDL_AudioSpec that will be set to the WAVE
2288 * data's format details on successful return.
2289 * \param audio_buf a pointer filled with the audio data, allocated by the
2290 * function.
2291 * \param audio_len a pointer filled with the length of the audio data buffer
2292 * in bytes.
2293 * \returns true on success. `audio_buf` will be filled with a pointer to an
2294 * allocated buffer containing the audio data, and `audio_len` is
2295 * filled with the length of that audio buffer in bytes.
2296 *
2297 * This function returns false if the .WAV file cannot be opened,
2298 * uses an unknown data format, or is corrupt; call SDL_GetError()
2299 * for more information.
2300 *
2301 * When the application is done with the data returned in
2302 * `audio_buf`, it should call SDL_free() to dispose of it.
2303 *
2304 * \threadsafety It is safe to call this function from any thread.
2305 *
2306 * \since This function is available since SDL 3.2.0.
2307 *
2308 * \sa SDL_free
2309 * \sa SDL_LoadWAV_IO
2310 */
2311extern SDL_DECLSPEC bool SDLCALL SDL_LoadWAV(const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);
2312
2313/**
2314 * Mix audio data in a specified format.
2315 *
2316 * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
2317 * it into `dst`, performing addition, volume adjustment, and overflow
2318 * clipping. The buffer pointed to by `dst` must also be `len` bytes of
2319 * `format` data.
2320 *
2321 * This is provided for convenience -- you can mix your own audio data.
2322 *
2323 * Do not use this function for mixing together more than two streams of
2324 * sample data. The output from repeated application of this function may be
2325 * distorted by clipping, because there is no accumulator with greater range
2326 * than the input (not to mention this being an inefficient way of doing it).
2327 *
2328 * It is a common misconception that this function is required to write audio
2329 * data to an output stream in an audio callback. While you can do that,
2330 * SDL_MixAudio() is really only needed when you're mixing a single audio
2331 * stream with a volume adjustment.
2332 *
2333 * \param dst the destination for the mixed audio.
2334 * \param src the source audio buffer to be mixed.
2335 * \param format the SDL_AudioFormat structure representing the desired audio
2336 * format.
2337 * \param len the length of the audio buffer in bytes.
2338 * \param volume ranges from 0.0 - 1.0, and should be set to 1.0 for full
2339 * audio volume.
2340 * \returns true on success or false on failure; call SDL_GetError() for more
2341 * information.
2342 *
2343 * \threadsafety It is safe to call this function from any thread.
2344 *
2345 * \since This function is available since SDL 3.2.0.
2346 */
2347extern SDL_DECLSPEC bool SDLCALL SDL_MixAudio(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, float volume);
2348
2349/**
2350 * Convert some audio data of one format to another format.
2351 *
2352 * Please note that this function is for convenience, but should not be used
2353 * to resample audio in blocks, as it will introduce audio artifacts on the
2354 * boundaries. You should only use this function if you are converting audio
2355 * data in its entirety in one call. If you want to convert audio in smaller
2356 * chunks, use an SDL_AudioStream, which is designed for this situation.
2357 *
2358 * Internally, this function creates and destroys an SDL_AudioStream on each
2359 * use, so it's also less efficient than using one directly, if you need to
2360 * convert multiple times.
2361 *
2362 * \param src_spec the format details of the input audio.
2363 * \param src_data the audio data to be converted.
2364 * \param src_len the len of src_data.
2365 * \param dst_spec the format details of the output audio.
2366 * \param dst_data will be filled with a pointer to converted audio data,
2367 * which should be freed with SDL_free(). On error, it will be
2368 * NULL.
2369 * \param dst_len will be filled with the len of dst_data.
2370 * \returns true on success or false on failure; call SDL_GetError() for more
2371 * information.
2372 *
2373 * \threadsafety It is safe to call this function from any thread.
2374 *
2375 * \since This function is available since SDL 3.2.0.
2376 */
2377extern 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);
2378
2379/**
2380 * Get the human readable name of an audio format.
2381 *
2382 * \param format the audio format to query.
2383 * \returns the human readable name of the specified audio format or
2384 * "SDL_AUDIO_UNKNOWN" if the format isn't recognized.
2385 *
2386 * \threadsafety It is safe to call this function from any thread.
2387 *
2388 * \since This function is available since SDL 3.2.0.
2389 */
2390extern SDL_DECLSPEC const char * SDLCALL SDL_GetAudioFormatName(SDL_AudioFormat format);
2391
2392/**
2393 * Get the appropriate memset value for silencing an audio format.
2394 *
2395 * The value returned by this function can be used as the second argument to
2396 * memset (or SDL_memset) to set an audio buffer in a specific format to
2397 * silence.
2398 *
2399 * \param format the audio data format to query.
2400 * \returns a byte value that can be passed to memset.
2401 *
2402 * \threadsafety It is safe to call this function from any thread.
2403 *
2404 * \since This function is available since SDL 3.2.0.
2405 */
2406extern SDL_DECLSPEC int SDLCALL SDL_GetSilenceValueForFormat(SDL_AudioFormat format);
2407
2408
2409/* Ends C function definitions when using C++ */
2410#ifdef __cplusplus
2411}
2412#endif
2413#include <SDL3/SDL_close_code.h>
2414
2415#endif /* SDL_audio_h_ */
bool SDL_MixAudio(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, float volume)
bool SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void *const *channel_buffers, int num_channels, int num_samples)
const char * SDL_GetAudioDeviceName(SDL_AudioDeviceID devid)
bool SDL_SetAudioStreamFormat(SDL_AudioStream *stream, const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec)
SDL_AudioDeviceID * SDL_GetAudioRecordingDevices(int *count)
bool SDL_UnlockAudioStream(SDL_AudioStream *stream)
int * SDL_GetAudioStreamInputChannelMap(SDL_AudioStream *stream, int *count)
const char * SDL_GetAudioDriver(int index)
SDL_AudioStream * SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec)
void SDL_UnbindAudioStream(SDL_AudioStream *stream)
bool SDL_IsAudioDevicePhysical(SDL_AudioDeviceID devid)
void SDL_UnbindAudioStreams(SDL_AudioStream *const *streams, int num_streams)
float SDL_GetAudioDeviceGain(SDL_AudioDeviceID devid)
bool SDL_SetAudioStreamGain(SDL_AudioStream *stream, float gain)
struct SDL_AudioStream SDL_AudioStream
Definition SDL_audio.h:452
bool SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata)
bool SDL_IsAudioDevicePlayback(SDL_AudioDeviceID devid)
SDL_AudioDeviceID * SDL_GetAudioPlaybackDevices(int *count)
bool SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata)
void(* SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount)
Definition SDL_audio.h:1923
bool SDL_SetAudioStreamInputChannelMap(SDL_AudioStream *stream, const int *chmap, int count)
bool SDL_AudioDevicePaused(SDL_AudioDeviceID devid)
bool SDL_SetAudioStreamFrequencyRatio(SDL_AudioStream *stream, float ratio)
int SDL_GetNumAudioDrivers(void)
bool SDL_LockAudioStream(SDL_AudioStream *stream)
float SDL_GetAudioStreamGain(SDL_AudioStream *stream)
SDL_PropertiesID SDL_GetAudioDeviceProperties(SDL_AudioDeviceID devid)
Uint32 SDL_AudioDeviceID
Definition SDL_audio.h:374
int SDL_GetAudioStreamQueued(SDL_AudioStream *stream)
bool SDL_SetAudioDeviceGain(SDL_AudioDeviceID devid, float gain)
int SDL_GetSilenceValueForFormat(SDL_AudioFormat format)
const char * SDL_GetCurrentAudioDriver(void)
SDL_PropertiesID SDL_GetAudioStreamProperties(SDL_AudioStream *stream)
SDL_AudioStream * SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata)
bool SDL_ResumeAudioStreamDevice(SDL_AudioStream *stream)
void(* SDL_AudioPostmixCallback)(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen)
Definition SDL_audio.h:2140
bool SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec, int *sample_frames)
bool SDL_SetAudioStreamOutputChannelMap(SDL_AudioStream *stream, const int *chmap, int count)
bool SDL_PauseAudioStreamDevice(SDL_AudioStream *stream)
float SDL_GetAudioStreamFrequencyRatio(SDL_AudioStream *stream)
int SDL_GetAudioStreamAvailable(SDL_AudioStream *stream)
SDL_AudioFormat
Definition SDL_audio.h:222
@ SDL_AUDIO_S32LE
Definition SDL_audio.h:232
@ SDL_AUDIO_F32
Definition SDL_audio.h:245
@ SDL_AUDIO_S16LE
Definition SDL_audio.h:228
@ SDL_AUDIO_S16
Definition SDL_audio.h:243
@ SDL_AUDIO_U8
Definition SDL_audio.h:224
@ SDL_AUDIO_S8
Definition SDL_audio.h:226
@ SDL_AUDIO_S32
Definition SDL_audio.h:244
@ SDL_AUDIO_F32LE
Definition SDL_audio.h:236
@ SDL_AUDIO_S32BE
Definition SDL_audio.h:234
@ SDL_AUDIO_UNKNOWN
Definition SDL_audio.h:223
@ SDL_AUDIO_S16BE
Definition SDL_audio.h:230
@ SDL_AUDIO_F32BE
Definition SDL_audio.h:238
bool SDL_LoadWAV(const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
bool SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec, const Uint8 *src_data, int src_len, const SDL_AudioSpec *dst_spec, Uint8 **dst_data, int *dst_len)
bool SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream *const *streams, int num_streams)
int * SDL_GetAudioStreamOutputChannelMap(SDL_AudioStream *stream, int *count)
bool SDL_ClearAudioStream(SDL_AudioStream *stream)
void SDL_DestroyAudioStream(SDL_AudioStream *stream)
void(* SDL_AudioStreamDataCompleteCallback)(void *userdata, const void *buf, int buflen)
Definition SDL_audio.h:1528
void SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
int SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len)
bool SDL_AudioStreamDevicePaused(SDL_AudioStream *stream)
const char * SDL_GetAudioFormatName(SDL_AudioFormat format)
bool SDL_GetAudioStreamFormat(SDL_AudioStream *stream, SDL_AudioSpec *src_spec, SDL_AudioSpec *dst_spec)
SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec)
bool SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream)
bool SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata)
bool SDL_FlushAudioStream(SDL_AudioStream *stream)
int * SDL_GetAudioDeviceChannelMap(SDL_AudioDeviceID devid, int *count)
bool SDL_ResumeAudioDevice(SDL_AudioDeviceID devid)
bool SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len)
bool SDL_PutAudioStreamDataNoCopy(SDL_AudioStream *stream, const void *buf, int len, SDL_AudioStreamDataCompleteCallback callback, void *userdata)
SDL_AudioDeviceID SDL_GetAudioStreamDevice(SDL_AudioStream *stream)
bool SDL_PauseAudioDevice(SDL_AudioDeviceID devid)
bool SDL_LoadWAV_IO(SDL_IOStream *src, bool closeio, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
struct SDL_IOStream SDL_IOStream
Uint32 SDL_PropertiesID
uint8_t Uint8
Definition SDL_stdinc.h:459
uint32_t Uint32
Definition SDL_stdinc.h:495
SDL_AudioFormat format
Definition SDL_audio.h:407