SDL 3.0
SDL_audio.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2023 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 * \file SDL_audio.h
24 *
25 * \brief Audio functionality for the SDL library.
26 */
27
28#ifndef SDL_audio_h_
29#define SDL_audio_h_
30
31#include <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33#include <SDL3/SDL_endian.h>
34#include <SDL3/SDL_mutex.h>
35#include <SDL3/SDL_thread.h>
36#include <SDL3/SDL_rwops.h>
37
38#include <SDL3/SDL_begin_code.h>
39/* Set up for C function definitions, even when using C++ */
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/*
45 * For multi-channel audio, the default SDL channel mapping is:
46 * 2: FL FR (stereo)
47 * 3: FL FR LFE (2.1 surround)
48 * 4: FL FR BL BR (quad)
49 * 5: FL FR LFE BL BR (4.1 surround)
50 * 6: FL FR FC LFE SL SR (5.1 surround - last two can also be BL BR)
51 * 7: FL FR FC LFE BC SL SR (6.1 surround)
52 * 8: FL FR FC LFE BL BR SL SR (7.1 surround)
53 */
54
55/**
56 * \brief Audio format flags.
57 *
58 * These are what the 16 bits in SDL_AudioFormat currently mean...
59 * (Unspecified bits are always zero).
60 *
61 * \verbatim
62 ++-----------------------sample is signed if set
63 ||
64 || ++-----------sample is bigendian if set
65 || ||
66 || || ++---sample is float if set
67 || || ||
68 || || || +---sample bit size---+
69 || || || | |
70 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
71 \endverbatim
72 *
73 * There are macros in SDL 2.0 and later to query these bits.
74 */
76
77/**
78 * \name Audio flags
79 */
80/* @{ */
81
82#define SDL_AUDIO_MASK_BITSIZE (0xFF)
83#define SDL_AUDIO_MASK_FLOAT (1<<8)
84#define SDL_AUDIO_MASK_BIG_ENDIAN (1<<12)
85#define SDL_AUDIO_MASK_SIGNED (1<<15)
86#define SDL_AUDIO_BITSIZE(x) ((x) & SDL_AUDIO_MASK_BITSIZE)
87#define SDL_AUDIO_BYTESIZE(x) (SDL_AUDIO_BITSIZE(x) / 8)
88#define SDL_AUDIO_ISFLOAT(x) ((x) & SDL_AUDIO_MASK_FLOAT)
89#define SDL_AUDIO_ISBIGENDIAN(x) ((x) & SDL_AUDIO_MASK_BIG_ENDIAN)
90#define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))
91#define SDL_AUDIO_ISSIGNED(x) ((x) & SDL_AUDIO_MASK_SIGNED)
92#define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))
93#define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))
94
95/**
96 * \name Audio format flags
97 *
98 * Defaults to LSB byte order.
99 */
100/* @{ */
101#define SDL_AUDIO_U8 0x0008 /**< Unsigned 8-bit samples */
102#define SDL_AUDIO_S8 0x8008 /**< Signed 8-bit samples */
103#define SDL_AUDIO_S16LE 0x8010 /**< Signed 16-bit samples */
104#define SDL_AUDIO_S16BE 0x9010 /**< As above, but big-endian byte order */
105/* @} */
106
107/**
108 * \name int32 support
109 */
110/* @{ */
111#define SDL_AUDIO_S32LE 0x8020 /**< 32-bit integer samples */
112#define SDL_AUDIO_S32BE 0x9020 /**< As above, but big-endian byte order */
113/* @} */
114
115/**
116 * \name float32 support
117 */
118/* @{ */
119#define SDL_AUDIO_F32LE 0x8120 /**< 32-bit floating point samples */
120#define SDL_AUDIO_F32BE 0x9120 /**< As above, but big-endian byte order */
121/* @} */
122
123/**
124 * \name Native audio byte ordering
125 */
126/* @{ */
127#if SDL_BYTEORDER == SDL_LIL_ENDIAN
128#define SDL_AUDIO_S16 SDL_AUDIO_S16LE
129#define SDL_AUDIO_S32 SDL_AUDIO_S32LE
130#define SDL_AUDIO_F32 SDL_AUDIO_F32LE
131#else
132#define SDL_AUDIO_S16 SDL_AUDIO_S16BE
133#define SDL_AUDIO_S32 SDL_AUDIO_S32BE
134#define SDL_AUDIO_F32 SDL_AUDIO_F32BE
135#endif
136/* @} */
137
138/* @} *//* Audio flags */
139
140/**
141 * SDL Audio Device instance IDs.
142 */
144
145#define SDL_AUDIO_DEVICE_DEFAULT_OUTPUT ((SDL_AudioDeviceID) 0xFFFFFFFF)
146#define SDL_AUDIO_DEVICE_DEFAULT_CAPTURE ((SDL_AudioDeviceID) 0xFFFFFFFE)
147
148typedef struct SDL_AudioSpec
149{
150 SDL_AudioFormat format; /**< Audio data format */
151 int channels; /**< Number of channels: 1 mono, 2 stereo, etc */
152 int freq; /**< sample rate: sample frames per second */
154
155/* Calculate the size of each audio frame (in bytes) */
156#define SDL_AUDIO_FRAMESIZE(x) (SDL_AUDIO_BYTESIZE((x).format) * (x).channels)
157
158/* SDL_AudioStream is an audio conversion interface.
159 - It can handle resampling data in chunks without generating
160 artifacts, when it doesn't have the complete buffer available.
161 - It can handle incoming data in any variable size.
162 - It can handle input/output format changes on the fly.
163 - You push data as you have it, and pull it when you need it
164 - It can also function as a basic audio data queue even if you
165 just have sound that needs to pass from one place to another.
166 - You can hook callbacks up to them when more data is added or
167 requested, to manage data on-the-fly.
168 */
169struct SDL_AudioStream; /* this is opaque to the outside world. */
171
172
173/* Function prototypes */
174
175/**
176 * \name Driver discovery functions
177 *
178 * These functions return the list of built in audio drivers, in the
179 * order that they are normally initialized by default.
180 */
181/* @{ */
182
183/**
184 * Use this function to get the number of built-in audio drivers.
185 *
186 * This function returns a hardcoded number. This never returns a negative
187 * value; if there are no drivers compiled into this build of SDL, this
188 * function returns zero. The presence of a driver in this list does not mean
189 * it will function, it just means SDL is capable of interacting with that
190 * interface. For example, a build of SDL might have esound support, but if
191 * there's no esound server available, SDL's esound driver would fail if used.
192 *
193 * By default, SDL tries all drivers, in its preferred order, until one is
194 * found to be usable.
195 *
196 * \returns the number of built-in audio drivers.
197 *
198 * \threadsafety It is safe to call this function from any thread.
199 *
200 * \since This function is available since SDL 3.0.0.
201 *
202 * \sa SDL_GetAudioDriver
203 */
204extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
205
206/**
207 * Use this function to get the name of a built in audio driver.
208 *
209 * The list of audio drivers is given in the order that they are normally
210 * initialized by default; the drivers that seem more reasonable to choose
211 * first (as far as the SDL developers believe) are earlier in the list.
212 *
213 * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
214 * "coreaudio" or "xaudio2". These never have Unicode characters, and are not
215 * meant to be proper names.
216 *
217 * \param index the index of the audio driver; the value ranges from 0 to
218 * SDL_GetNumAudioDrivers() - 1
219 * \returns the name of the audio driver at the requested index, or NULL if an
220 * invalid index was specified.
221 *
222 * \threadsafety It is safe to call this function from any thread.
223 *
224 * \since This function is available since SDL 3.0.0.
225 *
226 * \sa SDL_GetNumAudioDrivers
227 */
228extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
229/* @} */
230
231/**
232 * Get the name of the current audio driver.
233 *
234 * The returned string points to internal static memory and thus never becomes
235 * invalid, even if you quit the audio subsystem and initialize a new driver
236 * (although such a case would return a different static string from another
237 * call to this function, of course). As such, you should not modify or free
238 * the returned string.
239 *
240 * \returns the name of the current audio driver or NULL if no driver has been
241 * initialized.
242 *
243 * \threadsafety It is safe to call this function from any thread.
244 *
245 * \since This function is available since SDL 3.0.0.
246 */
247extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
248
249/**
250 * Get a list of currently-connected audio output devices.
251 *
252 * This returns of list of available devices that play sound, perhaps to
253 * speakers or headphones ("output" devices). If you want devices that record
254 * audio, like a microphone ("capture" devices), use
255 * SDL_GetAudioCaptureDevices() instead.
256 *
257 * This only returns a list of physical devices; it will not have any device
258 * IDs returned by SDL_OpenAudioDevice().
259 *
260 * \param count a pointer filled in with the number of devices returned
261 * \returns a 0 terminated array of device instance IDs which should be freed
262 * with SDL_free(), or NULL on error; call SDL_GetError() for more
263 * details.
264 *
265 * \threadsafety It is safe to call this function from any thread.
266 *
267 * \since This function is available since SDL 3.0.0.
268 *
269 * \sa SDL_OpenAudioDevice
270 * \sa SDL_GetAudioCaptureDevices
271 */
272extern DECLSPEC SDL_AudioDeviceID *SDLCALL SDL_GetAudioOutputDevices(int *count);
273
274/**
275 * Get a list of currently-connected audio capture devices.
276 *
277 * This returns of list of available devices that record audio, like a
278 * microphone ("capture" devices). If you want devices that play sound,
279 * perhaps to speakers or headphones ("output" devices), use
280 * SDL_GetAudioOutputDevices() instead.
281 *
282 * This only returns a list of physical devices; it will not have any device
283 * IDs returned by SDL_OpenAudioDevice().
284 *
285 * \param count a pointer filled in with the number of devices returned
286 * \returns a 0 terminated array of device instance IDs which should be freed
287 * with SDL_free(), or NULL on error; call SDL_GetError() for more
288 * details.
289 *
290 * \threadsafety It is safe to call this function from any thread.
291 *
292 * \since This function is available since SDL 3.0.0.
293 *
294 * \sa SDL_OpenAudioDevice
295 * \sa SDL_GetAudioOutputDevices
296 */
297extern DECLSPEC SDL_AudioDeviceID *SDLCALL SDL_GetAudioCaptureDevices(int *count);
298
299/**
300 * Get the human-readable name of a specific audio device.
301 *
302 * The string returned by this function is UTF-8 encoded. The caller should
303 * call SDL_free on the return value when done with it.
304 *
305 * \param devid the instance ID of the device to query.
306 * \returns the name of the audio device, or NULL on error.
307 *
308 * \threadsafety It is safe to call this function from any thread.
309 *
310 * \since This function is available since SDL 3.0.0.
311 *
312 * \sa SDL_GetAudioOutputDevices
313 * \sa SDL_GetAudioCaptureDevices
314 * \sa SDL_GetDefaultAudioInfo
315 */
316extern DECLSPEC char *SDLCALL SDL_GetAudioDeviceName(SDL_AudioDeviceID devid);
317
318/**
319 * Get the current audio format of a specific audio device.
320 *
321 * For an opened device, this will report the format the device is currently
322 * using. If the device isn't yet opened, this will report the device's
323 * preferred format (or a reasonable default if this can't be determined).
324 *
325 * You may also specify SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
326 * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE here, which is useful for getting a
327 * reasonable recommendation before opening the system-recommended default
328 * device.
329 *
330 * You can also use this to request the current device buffer size. This is
331 * specified in sample frames and represents the amount of data SDL will feed
332 * to the physical hardware in each chunk. This can be converted to
333 * milliseconds of audio with the following equation:
334 *
335 * `ms = (int) ((((Sint64) frames) * 1000) / spec.freq);`
336 *
337 * Buffer size is only important if you need low-level control over the audio
338 * playback timing. Most apps do not need this.
339 *
340 * \param devid the instance ID of the device to query.
341 * \param spec On return, will be filled with device details.
342 * \param sample_frames Pointer to store device buffer size, in sample frames.
343 * Can be NULL.
344 * \returns 0 on success or a negative error code on failure; call
345 * SDL_GetError() for more information.
346 *
347 * \threadsafety It is safe to call this function from any thread.
348 *
349 * \since This function is available since SDL 3.0.0.
350 */
351extern DECLSPEC int SDLCALL SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec, int *sample_frames);
352
353
354/**
355 * Open a specific audio device.
356 *
357 * You can open both output and capture devices through this function. Output
358 * devices will take data from bound audio streams, mix it, and send it to the
359 * hardware. Capture devices will feed any bound audio streams with a copy of
360 * any incoming data.
361 *
362 * An opened audio device starts out with no audio streams bound. To start
363 * audio playing, bind a stream and supply audio data to it. Unlike SDL2,
364 * there is no audio callback; you only bind audio streams and make sure they
365 * have data flowing into them (however, you can simulate SDL2's semantics
366 * fairly closely by using SDL_OpenAudioDeviceStream instead of this
367 * function).
368 *
369 * If you don't care about opening a specific device, pass a `devid` of either
370 * `SDL_AUDIO_DEVICE_DEFAULT_OUTPUT` or `SDL_AUDIO_DEVICE_DEFAULT_CAPTURE`. In
371 * this case, SDL will try to pick the most reasonable default, and may also
372 * switch between physical devices seamlessly later, if the most reasonable
373 * default changes during the lifetime of this opened device (user changed the
374 * default in the OS's system preferences, the default got unplugged so the
375 * system jumped to a new default, the user plugged in headphones on a mobile
376 * device, etc). Unless you have a good reason to choose a specific device,
377 * this is probably what you want.
378 *
379 * You may request a specific format for the audio device, but there is no
380 * promise the device will honor that request for several reasons. As such,
381 * it's only meant to be a hint as to what data your app will provide. Audio
382 * streams will accept data in whatever format you specify and manage
383 * conversion for you as appropriate. SDL_GetAudioDeviceFormat can tell you
384 * the preferred format for the device before opening and the actual format
385 * the device is using after opening.
386 *
387 * It's legal to open the same device ID more than once; each successful open
388 * will generate a new logical SDL_AudioDeviceID that is managed separately
389 * from others on the same physical device. This allows libraries to open a
390 * device separately from the main app and bind its own streams without
391 * conflicting.
392 *
393 * It is also legal to open a device ID returned by a previous call to this
394 * function; doing so just creates another logical device on the same physical
395 * device. This may be useful for making logical groupings of audio streams.
396 *
397 * This function returns the opened device ID on success. This is a new,
398 * unique SDL_AudioDeviceID that represents a logical device.
399 *
400 * Some backends might offer arbitrary devices (for example, a networked audio
401 * protocol that can connect to an arbitrary server). For these, as a change
402 * from SDL2, you should open a default device ID and use an SDL hint to
403 * specify the target if you care, or otherwise let the backend figure out a
404 * reasonable default. Most backends don't offer anything like this, and often
405 * this would be an end user setting an environment variable for their custom
406 * need, and not something an application should specifically manage.
407 *
408 * When done with an audio device, possibly at the end of the app's life, one
409 * should call SDL_CloseAudioDevice() on the returned device id.
410 *
411 * \param devid the device instance id to open, or
412 * SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
413 * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE for the most reasonable
414 * default device.
415 * \param spec the requested device configuration. Can be NULL to use
416 * reasonable defaults.
417 * \returns The device ID on success, 0 on error; call SDL_GetError() for more
418 * information.
419 *
420 * \threadsafety It is safe to call this function from any thread.
421 *
422 * \since This function is available since SDL 3.0.0.
423 *
424 * \sa SDL_CloseAudioDevice
425 * \sa SDL_GetAudioDeviceFormat
426 */
427extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec);
428
429/**
430 * Use this function to pause audio playback on a specified device.
431 *
432 * This function pauses audio processing for a given device. Any bound audio
433 * streams will not progress, and no audio will be generated. Pausing one
434 * device does not prevent other unpaused devices from running.
435 *
436 * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
437 * has to bind a stream before any audio will flow. Pausing a paused device is
438 * a legal no-op.
439 *
440 * Pausing a device can be useful to halt all audio without unbinding all the
441 * audio streams. This might be useful while a game is paused, or a level is
442 * loading, etc.
443 *
444 * Physical devices can not be paused or unpaused, only logical devices
445 * created through SDL_OpenAudioDevice() can be.
446 *
447 * \param dev a device opened by SDL_OpenAudioDevice()
448 * \returns 0 on success or a negative error code on failure; call
449 * SDL_GetError() for more information.
450 *
451 * \threadsafety It is safe to call this function from any thread.
452 *
453 * \since This function is available since SDL 3.0.0.
454 *
455 * \sa SDL_ResumeAudioDevice
456 * \sa SDL_AudioDevicePaused
457 */
458extern DECLSPEC int SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev);
459
460/**
461 * Use this function to unpause audio playback on a specified device.
462 *
463 * This function unpauses audio processing for a given device that has
464 * previously been paused with SDL_PauseAudioDevice(). Once unpaused, any
465 * bound audio streams will begin to progress again, and audio can be
466 * generated.
467 *
468 * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
469 * has to bind a stream before any audio will flow. Unpausing an unpaused
470 * device is a legal no-op.
471 *
472 * Physical devices can not be paused or unpaused, only logical devices
473 * created through SDL_OpenAudioDevice() can be.
474 *
475 * \param dev a device opened by SDL_OpenAudioDevice()
476 * \returns 0 on success or a negative error code on failure; call
477 * SDL_GetError() for more information.
478 *
479 * \threadsafety It is safe to call this function from any thread.
480 *
481 * \since This function is available since SDL 3.0.0.
482 *
483 * \sa SDL_AudioDevicePaused
484 * \sa SDL_PauseAudioDevice
485 */
486extern DECLSPEC int SDLCALL SDL_ResumeAudioDevice(SDL_AudioDeviceID dev);
487
488/**
489 * Use this function to query if an audio device is paused.
490 *
491 * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
492 * has to bind a stream before any audio will flow.
493 *
494 * Physical devices can not be paused or unpaused, only logical devices
495 * created through SDL_OpenAudioDevice() can be. Physical and invalid device
496 * IDs will report themselves as unpaused here.
497 *
498 * \param dev a device opened by SDL_OpenAudioDevice()
499 * \returns SDL_TRUE if device is valid and paused, SDL_FALSE otherwise.
500 *
501 * \threadsafety It is safe to call this function from any thread.
502 *
503 * \since This function is available since SDL 3.0.0.
504 *
505 * \sa SDL_PauseAudioDevice
506 * \sa SDL_ResumeAudioDevice
507 */
509
510/**
511 * Close a previously-opened audio device.
512 *
513 * The application should close open audio devices once they are no longer
514 * needed.
515 *
516 * This function may block briefly while pending audio data is played by the
517 * hardware, so that applications don't drop the last buffer of data they
518 * supplied if terminating immediately afterwards.
519 *
520 * \param devid an audio device id previously returned by
521 * SDL_OpenAudioDevice()
522 *
523 * \threadsafety It is safe to call this function from any thread.
524 *
525 * \since This function is available since SDL 3.0.0.
526 *
527 * \sa SDL_OpenAudioDevice
528 */
529extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID devid);
530
531/**
532 * Bind a list of audio streams to an audio device.
533 *
534 * Audio data will flow through any bound streams. For an output device, data
535 * for all bound streams will be mixed together and fed to the device. For a
536 * capture device, a copy of recorded data will be provided to each bound
537 * stream.
538 *
539 * Audio streams can only be bound to an open device. This operation is
540 * atomic--all streams bound in the same call will start processing at the
541 * same time, so they can stay in sync. Also: either all streams will be bound
542 * or none of them will be.
543 *
544 * It is an error to bind an already-bound stream; it must be explicitly
545 * unbound first.
546 *
547 * Binding a stream to a device will set its output format for output devices,
548 * and its input format for capture devices, so they match the device's
549 * settings. The caller is welcome to change the other end of the stream's
550 * format at any time.
551 *
552 * \param devid an audio device to bind a stream to.
553 * \param streams an array of audio streams to unbind.
554 * \param num_streams Number streams listed in the `streams` array.
555 * \returns 0 on success, -1 on error; call SDL_GetError() for more
556 * information.
557 *
558 * \threadsafety It is safe to call this function from any thread.
559 *
560 * \since This function is available since SDL 3.0.0.
561 *
562 * \sa SDL_BindAudioStreams
563 * \sa SDL_UnbindAudioStreams
564 * \sa SDL_UnbindAudioStream
565 * \sa SDL_GetAudioStreamDevice
566 */
567extern DECLSPEC int SDLCALL SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream **streams, int num_streams);
568
569/**
570 * Bind a single audio stream to an audio device.
571 *
572 * This is a convenience function, equivalent to calling
573 * `SDL_BindAudioStreams(devid, &stream, 1)`.
574 *
575 * \param devid an audio device to bind a stream to.
576 * \param stream an audio stream to bind to a device.
577 * \returns 0 on success, -1 on error; call SDL_GetError() for more
578 * information.
579 *
580 * \threadsafety It is safe to call this function from any thread.
581 *
582 * \since This function is available since SDL 3.0.0.
583 *
584 * \sa SDL_BindAudioStreams
585 * \sa SDL_UnbindAudioStreams
586 * \sa SDL_UnbindAudioStream
587 * \sa SDL_GetAudioStreamDevice
588 */
589extern DECLSPEC int SDLCALL SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream);
590
591/**
592 * Unbind a list of audio streams from their audio devices.
593 *
594 * The streams being unbound do not all have to be on the same device. All
595 * streams on the same device will be unbound atomically (data will stop
596 * flowing through them all unbound streams on the same device at the same
597 * time).
598 *
599 * Unbinding a stream that isn't bound to a device is a legal no-op.
600 *
601 * \param streams an array of audio streams to unbind.
602 * \param num_streams Number streams listed in the `streams` array.
603 *
604 * \threadsafety It is safe to call this function from any thread.
605 *
606 * \since This function is available since SDL 3.0.0.
607 *
608 * \sa SDL_BindAudioStreams
609 * \sa SDL_BindAudioStream
610 * \sa SDL_UnbindAudioStream
611 * \sa SDL_GetAudioStreamDevice
612 */
613extern DECLSPEC void SDLCALL SDL_UnbindAudioStreams(SDL_AudioStream **streams, int num_streams);
614
615/**
616 * Unbind a single audio stream from its audio device.
617 *
618 * This is a convenience function, equivalent to calling
619 * `SDL_UnbindAudioStreams(&stream, 1)`.
620 *
621 * \param stream an audio stream to unbind from a device.
622 *
623 * \threadsafety It is safe to call this function from any thread.
624 *
625 * \since This function is available since SDL 3.0.0.
626 *
627 * \sa SDL_BindAudioStream
628 * \sa SDL_BindAudioStreams
629 * \sa SDL_UnbindAudioStreams
630 * \sa SDL_GetAudioStreamDevice
631 */
632extern DECLSPEC void SDLCALL SDL_UnbindAudioStream(SDL_AudioStream *stream);
633
634/**
635 * Query an audio stream for its currently-bound device.
636 *
637 * This reports the audio device that an audio stream is currently bound to.
638 *
639 * If not bound, or invalid, this returns zero, which is not a valid device
640 * ID.
641 *
642 * \param stream the audio stream to query.
643 * \returns The bound audio device, or 0 if not bound or invalid.
644 *
645 * \threadsafety It is safe to call this function from any thread.
646 *
647 * \since This function is available since SDL 3.0.0.
648 *
649 * \sa SDL_BindAudioStream
650 * \sa SDL_BindAudioStreams
651 * \sa SDL_UnbindAudioStream
652 * \sa SDL_UnbindAudioStreams
653 */
655
656
657/**
658 * Create a new audio stream.
659 *
660 * \param src_spec The format details of the input audio
661 * \param dst_spec The format details of the output audio
662 * \returns 0 on success, or -1 on error.
663 *
664 * \threadsafety It is safe to call this function from any thread.
665 *
666 * \since This function is available since SDL 3.0.0.
667 *
668 * \sa SDL_PutAudioStreamData
669 * \sa SDL_GetAudioStreamData
670 * \sa SDL_GetAudioStreamAvailable
671 * \sa SDL_FlushAudioStream
672 * \sa SDL_ClearAudioStream
673 * \sa SDL_ChangeAudioStreamOutput
674 * \sa SDL_DestroyAudioStream
675 */
676extern DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
677
678
679/**
680 * Query the current format of an audio stream.
681 *
682 * \param stream the SDL_AudioStream to query.
683 * \param src_spec Where to store the input audio format; ignored if NULL.
684 * \param dst_spec Where to store the output audio format; ignored if NULL.
685 * \returns 0 on success, or -1 on error.
686 *
687 * \threadsafety It is safe to call this function from any thread, as it holds
688 * a stream-specific mutex while running.
689 *
690 * \since This function is available since SDL 3.0.0.
691 */
692extern DECLSPEC int SDLCALL SDL_GetAudioStreamFormat(SDL_AudioStream *stream,
693 SDL_AudioSpec *src_spec,
694 SDL_AudioSpec *dst_spec);
695
696/**
697 * Change the input and output formats of an audio stream.
698 *
699 * Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData
700 * will reflect the new format, and future calls to SDL_PutAudioStreamData
701 * must provide data in the new input formats.
702 *
703 * \param stream The stream the format is being changed
704 * \param src_spec The new format of the audio input; if NULL, it is not
705 * changed.
706 * \param dst_spec The new format of the audio output; if NULL, it is not
707 * changed.
708 * \returns 0 on success, or -1 on error.
709 *
710 * \threadsafety It is safe to call this function from any thread, as it holds
711 * a stream-specific mutex while running.
712 *
713 * \since This function is available since SDL 3.0.0.
714 *
715 * \sa SDL_GetAudioStreamFormat
716 * \sa SDL_PutAudioStreamData
717 * \sa SDL_GetAudioStreamData
718 * \sa SDL_GetAudioStreamAvailable
719 * \sa SDL_SetAudioStreamFrequencyRatio
720 */
721extern DECLSPEC int SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream,
722 const SDL_AudioSpec *src_spec,
723 const SDL_AudioSpec *dst_spec);
724
725/**
726 * Get the frequency ratio of an audio stream.
727 *
728 * \param stream the SDL_AudioStream to query.
729 * \returns the frequency ratio of the stream, or 0.0 on error
730 *
731 * \threadsafety It is safe to call this function from any thread, as it holds
732 * a stream-specific mutex while running.
733 *
734 * \since This function is available since SDL 3.0.0.
735 *
736 * \sa SDL_SetAudioStreamFrequencyRatio
737 */
738extern DECLSPEC float SDLCALL SDL_GetAudioStreamFrequencyRatio(SDL_AudioStream *stream);
739
740/**
741 * Change the frequency ratio of an audio stream.
742 *
743 * The frequency ratio is used to adjust the rate at which input data is
744 * consumed. Changing this effectively modifies the speed and pitch of the
745 * audio. A value greater than 1.0 will play the audio faster, and at a higher
746 * pitch. A value less than 1.0 will play the audio slower, and at a lower
747 * pitch.
748 *
749 * This is applied during SDL_GetAudioStreamData, and can be continuously
750 * changed to create various effects.
751 *
752 * \param stream The stream the frequency ratio is being changed
753 * \param ratio The frequency ratio. 1.0 is normal speed. Must be between 0.01
754 * and 100.
755 * \returns 0 on success, or -1 on error.
756 *
757 * \threadsafety It is safe to call this function from any thread, as it holds
758 * a stream-specific mutex while running.
759 *
760 * \since This function is available since SDL 3.0.0.
761 *
762 * \sa SDL_GetAudioStreamFrequencyRatio
763 * \sa SDL_SetAudioStreamFormat
764 */
765extern DECLSPEC int SDLCALL SDL_SetAudioStreamFrequencyRatio(SDL_AudioStream *stream, float ratio);
766
767/**
768 * Add data to be converted/resampled to the stream.
769 *
770 * This data must match the format/channels/samplerate specified in the latest
771 * call to SDL_SetAudioStreamFormat, or the format specified when creating the
772 * stream if it hasn't been changed.
773 *
774 * Note that this call simply queues unconverted data for later. This is
775 * different than SDL2, where data was converted during the Put call and the
776 * Get call would just dequeue the previously-converted data.
777 *
778 * \param stream The stream the audio data is being added to
779 * \param buf A pointer to the audio data to add
780 * \param len The number of bytes to write to the stream
781 * \returns 0 on success or a negative error code on failure; call
782 * SDL_GetError() for more information.
783 *
784 * \threadsafety It is safe to call this function from any thread, but if the
785 * stream has a callback set, the caller might need to manage
786 * extra locking.
787 *
788 * \since This function is available since SDL 3.0.0.
789 *
790 * \sa SDL_CreateAudioStream
791 * \sa SDL_GetAudioStreamData
792 * \sa SDL_GetAudioStreamAvailable
793 * \sa SDL_FlushAudioStream
794 * \sa SDL_ClearAudioStream
795 * \sa SDL_DestroyAudioStream
796 */
797extern DECLSPEC int SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
798
799/**
800 * Get converted/resampled data from the stream.
801 *
802 * The input/output data format/channels/samplerate is specified when creating
803 * the stream, and can be changed after creation by calling
804 * SDL_SetAudioStreamFormat.
805 *
806 * Note that any conversion and resampling necessary is done during this call,
807 * and SDL_PutAudioStreamData simply queues unconverted data for later. This
808 * is different than SDL2, where that work was done while inputting new data
809 * to the stream and requesting the output just copied the converted data.
810 *
811 * \param stream The stream the audio is being requested from
812 * \param buf A buffer to fill with audio data
813 * \param len The maximum number of bytes to fill
814 * \returns the number of bytes read from the stream, or -1 on error
815 *
816 * \threadsafety It is safe to call this function from any thread, but if the
817 * stream has a callback set, the caller might need to manage
818 * extra locking.
819 *
820 * \since This function is available since SDL 3.0.0.
821 *
822 * \sa SDL_CreateAudioStream
823 * \sa SDL_PutAudioStreamData
824 * \sa SDL_GetAudioStreamAvailable
825 * \sa SDL_SetAudioStreamFormat
826 * \sa SDL_FlushAudioStream
827 * \sa SDL_ClearAudioStream
828 * \sa SDL_DestroyAudioStream
829 */
830extern DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
831
832/**
833 * Get the number of converted/resampled bytes available.
834 *
835 * The stream may be buffering data behind the scenes until it has enough to
836 * resample correctly, so this number might be lower than what you expect, or
837 * even be zero. Add more data or flush the stream if you need the data now.
838 *
839 * If the stream has so much data that it would overflow an int, the return
840 * value is clamped to a maximum value, but no queued data is lost; if there
841 * are gigabytes of data queued, the app might need to read some of it with
842 * SDL_GetAudioStreamData before this function's return value is no longer
843 * clamped.
844 *
845 * \param stream The audio stream to query
846 * \returns the number of converted/resampled bytes available.
847 *
848 * \threadsafety It is safe to call this function from any thread.
849 *
850 * \since This function is available since SDL 3.0.0.
851 *
852 * \sa SDL_CreateAudioStream
853 * \sa SDL_PutAudioStreamData
854 * \sa SDL_GetAudioStreamData
855 * \sa SDL_FlushAudioStream
856 * \sa SDL_ClearAudioStream
857 * \sa SDL_DestroyAudioStream
858 */
859extern DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
860
861
862/**
863 * Get the number of bytes currently queued.
864 *
865 * Note that audio streams can change their input format at any time, even if
866 * there is still data queued in a different format, so the returned byte
867 * count will not necessarily match the number of _sample frames_ available.
868 * Users of this API should be aware of format changes they make when feeding
869 * a stream and plan accordingly.
870 *
871 * Queued data is not converted until it is consumed by
872 * SDL_GetAudioStreamData, so this value should be representative of the exact
873 * data that was put into the stream.
874 *
875 * If the stream has so much data that it would overflow an int, the return
876 * value is clamped to a maximum value, but no queued data is lost; if there
877 * are gigabytes of data queued, the app might need to read some of it with
878 * SDL_GetAudioStreamData before this function's return value is no longer
879 * clamped.
880 *
881 * \param stream The audio stream to query
882 * \returns the number of bytes queued.
883 *
884 * \threadsafety It is safe to call this function from any thread.
885 *
886 * \since This function is available since SDL 3.0.0.
887 *
888 * \sa SDL_PutAudioStreamData
889 * \sa SDL_GetAudioStreamData
890 * \sa SDL_ClearAudioStream
891 */
892extern DECLSPEC int SDLCALL SDL_GetAudioStreamQueued(SDL_AudioStream *stream);
893
894
895/**
896 * Tell the stream that you're done sending data, and anything being buffered
897 * should be converted/resampled and made available immediately.
898 *
899 * It is legal to add more data to a stream after flushing, but there will be
900 * audio gaps in the output. Generally this is intended to signal the end of
901 * input, so the complete output becomes available.
902 *
903 * \param stream The audio stream to flush
904 * \returns 0 on success or a negative error code on failure; call
905 * SDL_GetError() for more information.
906 *
907 * \threadsafety It is safe to call this function from any thread.
908 *
909 * \since This function is available since SDL 3.0.0.
910 *
911 * \sa SDL_CreateAudioStream
912 * \sa SDL_PutAudioStreamData
913 * \sa SDL_GetAudioStreamData
914 * \sa SDL_GetAudioStreamAvailable
915 * \sa SDL_ClearAudioStream
916 * \sa SDL_DestroyAudioStream
917 */
918extern DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
919
920/**
921 * Clear any pending data in the stream without converting it
922 *
923 * \param stream The audio stream to clear
924 * \returns 0 on success or a negative error code on failure; call
925 * SDL_GetError() for more information.
926 *
927 * \threadsafety It is safe to call this function from any thread.
928 *
929 * \since This function is available since SDL 3.0.0.
930 *
931 * \sa SDL_CreateAudioStream
932 * \sa SDL_PutAudioStreamData
933 * \sa SDL_GetAudioStreamData
934 * \sa SDL_GetAudioStreamAvailable
935 * \sa SDL_FlushAudioStream
936 * \sa SDL_DestroyAudioStream
937 */
938extern DECLSPEC int SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
939
940/**
941 * Lock an audio stream for serialized access.
942 *
943 * Each SDL_AudioStream has an internal mutex it uses to protect its data
944 * structures from threading conflicts. This function allows an app to lock
945 * that mutex, which could be useful if registering callbacks on this stream.
946 *
947 * One does not need to lock a stream to use in it most cases, as the stream
948 * manages this lock internally. However, this lock is held during callbacks,
949 * which may run from arbitrary threads at any time, so if an app needs to
950 * protect shared data during those callbacks, locking the stream guarantees
951 * that the callback is not running while the lock is held.
952 *
953 * As this is just a wrapper over SDL_LockMutex for an internal lock, it has
954 * all the same attributes (recursive locks are allowed, etc).
955 *
956 * \param stream The audio stream to lock.
957 * \returns 0 on success or a negative error code on failure; call
958 * SDL_GetError() for more information.
959 *
960 * \threadsafety It is safe to call this function from any thread.
961 *
962 * \since This function is available since SDL 3.0.0.
963 *
964 * \sa SDL_UnlockAudioStream
965 * \sa SDL_SetAudioStreamPutCallback
966 * \sa SDL_SetAudioStreamGetCallback
967 */
968extern DECLSPEC int SDLCALL SDL_LockAudioStream(SDL_AudioStream *stream);
969
970
971/**
972 * Unlock an audio stream for serialized access.
973 *
974 * This unlocks an audio stream after a call to SDL_LockAudioStream.
975 *
976 * \param stream The audio stream to unlock.
977 * \returns 0 on success or a negative error code on failure; call
978 * SDL_GetError() for more information.
979 *
980 * \threadsafety You should only call this from the same thread that
981 * previously called SDL_LockAudioStream.
982 *
983 * \since This function is available since SDL 3.0.0.
984 *
985 * \sa SDL_LockAudioStream
986 * \sa SDL_SetAudioStreamPutCallback
987 * \sa SDL_SetAudioStreamGetCallback
988 */
989extern DECLSPEC int SDLCALL SDL_UnlockAudioStream(SDL_AudioStream *stream);
990
991/**
992 * A callback that fires when data passes through an SDL_AudioStream.
993 *
994 * Apps can (optionally) register a callback with an audio stream that
995 * is called when data is added with SDL_PutAudioStreamData, or requested
996 * with SDL_GetAudioStreamData. These callbacks may run from any
997 * thread, so if you need to protect shared data, you should use
998 * SDL_LockAudioStream to serialize access; this lock will be held by
999 * before your callback is called, so your callback does not need to
1000 * manage the lock explicitly.
1001 *
1002 * Two values are offered here: one is the amount of additional data needed
1003 * to satisfy the immediate request (which might be zero if the stream
1004 * already has enough data queued) and the other is the total amount
1005 * being requested. In a Get call triggering a Put callback, these
1006 * values can be different. In a Put call triggering a Get callback,
1007 * these values are always the same.
1008 *
1009 * Byte counts might be slightly overestimated due to buffering or
1010 * resampling, and may change from call to call.
1011 *
1012 * \param stream The SDL audio stream associated with this callback.
1013 * \param additional_amount The amount of data, in bytes, that is needed right now.
1014 * \param total_amount The total amount of data requested, in bytes, that is requested or available.
1015 * \param userdata An opaque pointer provided by the app for their personal use.
1016 */
1017typedef void (SDLCALL *SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount);
1018
1019/**
1020 * Set a callback that runs when data is requested from an audio stream.
1021 *
1022 * This callback is called _before_ data is obtained from the stream, giving
1023 * the callback the chance to add more on-demand.
1024 *
1025 * The callback can (optionally) call SDL_PutAudioStreamData() to add more
1026 * audio to the stream during this call; if needed, the request that triggered
1027 * this callback will obtain the new data immediately.
1028 *
1029 * The callback's `approx_request` argument is roughly how many bytes of
1030 * _unconverted_ data (in the stream's input format) is needed by the caller,
1031 * although this may overestimate a little for safety. This takes into account
1032 * how much is already in the stream and only asks for any extra necessary to
1033 * resolve the request, which means the callback may be asked for zero bytes,
1034 * and a different amount on each call.
1035 *
1036 * The callback is not required to supply exact amounts; it is allowed to
1037 * supply too much or too little or none at all. The caller will get what's
1038 * available, up to the amount they requested, regardless of this callback's
1039 * outcome.
1040 *
1041 * Clearing or flushing an audio stream does not call this callback.
1042 *
1043 * This function obtains the stream's lock, which means any existing callback
1044 * (get or put) in progress will finish running before setting the new
1045 * callback.
1046 *
1047 * Setting a NULL function turns off the callback.
1048 *
1049 * \param stream the audio stream to set the new callback on.
1050 * \param callback the new callback function to call when data is added to the
1051 * stream.
1052 * \param userdata an opaque pointer provided to the callback for its own
1053 * personal use.
1054 * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
1055 *
1056 * \threadsafety It is safe to call this function from any thread.
1057 *
1058 * \since This function is available since SDL 3.0.0.
1059 *
1060 * \sa SDL_SetAudioStreamPutCallback
1061 */
1062extern DECLSPEC int SDLCALL SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
1063
1064/**
1065 * Set a callback that runs when data is added to an audio stream.
1066 *
1067 * This callback is called _after_ the data is added to the stream, giving the
1068 * callback the chance to obtain it immediately.
1069 *
1070 * The callback can (optionally) call SDL_GetAudioStreamData() to obtain audio
1071 * from the stream during this call.
1072 *
1073 * The callback's `approx_request` argument is how many bytes of _converted_
1074 * data (in the stream's output format) was provided by the caller, although
1075 * this may underestimate a little for safety. This value might be less than
1076 * what is currently available in the stream, if data was already there, and
1077 * might be less than the caller provided if the stream needs to keep a buffer
1078 * to aid in resampling. Which means the callback may be provided with zero
1079 * bytes, and a different amount on each call.
1080 *
1081 * The callback may call SDL_GetAudioStreamAvailable to see the total amount
1082 * currently available to read from the stream, instead of the total provided
1083 * by the current call.
1084 *
1085 * The callback is not required to obtain all data. It is allowed to read less
1086 * or none at all. Anything not read now simply remains in the stream for
1087 * later access.
1088 *
1089 * Clearing or flushing an audio stream does not call this callback.
1090 *
1091 * This function obtains the stream's lock, which means any existing callback
1092 * (get or put) in progress will finish running before setting the new
1093 * callback.
1094 *
1095 * Setting a NULL function turns off the callback.
1096 *
1097 * \param stream the audio stream to set the new callback on.
1098 * \param callback the new callback function to call when data is added to the
1099 * stream.
1100 * \param userdata an opaque pointer provided to the callback for its own
1101 * personal use.
1102 * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
1103 *
1104 * \threadsafety It is safe to call this function from any thread.
1105 *
1106 * \since This function is available since SDL 3.0.0.
1107 *
1108 * \sa SDL_SetAudioStreamGetCallback
1109 */
1110extern DECLSPEC int SDLCALL SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
1111
1112
1113/**
1114 * Free an audio stream
1115 *
1116 * \param stream The audio stream to free
1117 *
1118 * \threadsafety It is safe to call this function from any thread.
1119 *
1120 * \since This function is available since SDL 3.0.0.
1121 *
1122 * \sa SDL_CreateAudioStream
1123 * \sa SDL_PutAudioStreamData
1124 * \sa SDL_GetAudioStreamData
1125 * \sa SDL_GetAudioStreamAvailable
1126 * \sa SDL_FlushAudioStream
1127 * \sa SDL_ClearAudioStream
1128 */
1129extern DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
1130
1131
1132/**
1133 * Convenience function for straightforward audio init for the common case.
1134 *
1135 * If all your app intends to do is provide a single source of PCM audio, this
1136 * function allows you to do all your audio setup in a single call.
1137 *
1138 * This is intended to be a clean means to migrate apps from SDL2.
1139 *
1140 * This function will open an audio device, create a stream and bind it.
1141 * Unlike other methods of setup, the audio device will be closed when this
1142 * stream is destroyed, so the app can treat the returned SDL_AudioStream as
1143 * the only object needed to manage audio playback.
1144 *
1145 * Also unlike other functions, the audio device begins paused. This is to map
1146 * more closely to SDL2-style behavior, and since there is no extra step here
1147 * to bind a stream to begin audio flowing. The audio device should be resumed
1148 * with SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream));
1149 *
1150 * This function works with both playback and capture devices.
1151 *
1152 * The `spec` parameter represents the app's side of the audio stream. That
1153 * is, for recording audio, this will be the output format, and for playing
1154 * audio, this will be the input format.
1155 *
1156 * If you don't care about opening a specific audio device, you can (and
1157 * probably _should_), use SDL_AUDIO_DEVICE_DEFAULT_OUTPUT for playback and
1158 * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE for recording.
1159 *
1160 * One can optionally provide a callback function; if NULL, the app is
1161 * expected to queue audio data for playback (or unqueue audio data if
1162 * capturing). Otherwise, the callback will begin to fire once the device is
1163 * unpaused.
1164 *
1165 * \param devid an audio device to open, or SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
1166 * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE.
1167 * \param spec the audio stream's data format. Required.
1168 * \param callback A callback where the app will provide new data for
1169 * playback, or receive new data for capture. Can be NULL, in
1170 * which case the app will need to call SDL_PutAudioStreamData
1171 * or SDL_GetAudioStreamData as necessary.
1172 * \param userdata App-controlled pointer passed to callback. Can be NULL.
1173 * Ignored if callback is NULL.
1174 * \returns an audio stream on success, ready to use. NULL on error; call
1175 * SDL_GetError() for more information. When done with this stream,
1176 * call SDL_DestroyAudioStream to free resources and close the
1177 * device.
1178 *
1179 * \threadsafety It is safe to call this function from any thread.
1180 *
1181 * \since This function is available since SDL 3.0.0.
1182 *
1183 * \sa SDL_GetAudioStreamDevice
1184 * \sa SDL_ResumeAudioDevice
1185 */
1186extern DECLSPEC SDL_AudioStream *SDLCALL SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata);
1187
1188
1189/**
1190 * A callback that fires when data is about to be fed to an audio device.
1191 *
1192 * This is useful for accessing the final mix, perhaps for writing a
1193 * visualizer or applying a final effect to the audio data before playback.
1194 *
1195 * \sa SDL_SetAudioDevicePostmixCallback
1196 */
1197typedef void (SDLCALL *SDL_AudioPostmixCallback)(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen);
1198
1199/**
1200 * Set a callback that fires when data is about to be fed to an audio device.
1201 *
1202 * This is useful for accessing the final mix, perhaps for writing a
1203 * visualizer or applying a final effect to the audio data before playback.
1204 *
1205 * The buffer is the final mix of all bound audio streams on an opened device;
1206 * this callback will fire regularly for any device that is both opened and
1207 * unpaused. If there is no new data to mix, either because no streams are
1208 * bound to the device or all the streams are empty, this callback will still
1209 * fire with the entire buffer set to silence.
1210 *
1211 * This callback is allowed to make changes to the data; the contents of the
1212 * buffer after this call is what is ultimately passed along to the hardware.
1213 *
1214 * The callback is always provided the data in float format (values from -1.0f
1215 * to 1.0f), but the number of channels or sample rate may be different than
1216 * the format the app requested when opening the device; SDL might have had to
1217 * manage a conversion behind the scenes, or the playback might have jumped to
1218 * new physical hardware when a system default changed, etc. These details may
1219 * change between calls. Accordingly, the size of the buffer might change
1220 * between calls as well.
1221 *
1222 * This callback can run at any time, and from any thread; if you need to
1223 * serialize access to your app's data, you should provide and use a mutex or
1224 * other synchronization device.
1225 *
1226 * All of this to say: there are specific needs this callback can fulfill, but
1227 * it is not the simplest interface. Apps should generally provide audio in
1228 * their preferred format through an SDL_AudioStream and let SDL handle the
1229 * difference.
1230 *
1231 * This function is extremely time-sensitive; the callback should do the least
1232 * amount of work possible and return as quickly as it can. The longer the
1233 * callback runs, the higher the risk of audio dropouts or other problems.
1234 *
1235 * This function will block until the audio device is in between iterations,
1236 * so any existing callback that might be running will finish before this
1237 * function sets the new callback and returns.
1238 *
1239 * Setting a NULL callback function disables any previously-set callback.
1240 *
1241 * \param devid The ID of an opened audio device.
1242 * \param callback A callback function to be called. Can be NULL.
1243 * \param userdata App-controlled pointer passed to callback. Can be NULL.
1244 * \returns zero on success, -1 on error; call SDL_GetError() for more
1245 * information.
1246 *
1247 * \threadsafety It is safe to call this function from any thread.
1248 *
1249 * \since This function is available since SDL 3.0.0.
1250 */
1251extern DECLSPEC int SDLCALL SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata);
1252
1253
1254/**
1255 * Load the audio data of a WAVE file into memory.
1256 *
1257 * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
1258 * be valid pointers. The entire data portion of the file is then loaded into
1259 * memory and decoded if necessary.
1260 *
1261 * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
1262 * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
1263 * A-law and mu-law (8 bits). Other formats are currently unsupported and
1264 * cause an error.
1265 *
1266 * If this function succeeds, the return value is zero and the pointer to the
1267 * audio data allocated by the function is written to `audio_buf` and its
1268 * length in bytes to `audio_len`. The SDL_AudioSpec members `freq`,
1269 * `channels`, and `format` are set to the values of the audio data in the
1270 * buffer. The `samples` member is set to a sane default and all others are
1271 * set to zero.
1272 *
1273 * It's necessary to use SDL_free() to free the audio data returned in
1274 * `audio_buf` when it is no longer used.
1275 *
1276 * Because of the underspecification of the .WAV format, there are many
1277 * problematic files in the wild that cause issues with strict decoders. To
1278 * provide compatibility with these files, this decoder is lenient in regards
1279 * to the truncation of the file, the fact chunk, and the size of the RIFF
1280 * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
1281 * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
1282 * tune the behavior of the loading process.
1283 *
1284 * Any file that is invalid (due to truncation, corruption, or wrong values in
1285 * the headers), too big, or unsupported causes an error. Additionally, any
1286 * critical I/O error from the data source will terminate the loading process
1287 * with an error. The function returns NULL on error and in all cases (with
1288 * the exception of `src` being NULL), an appropriate error message will be
1289 * set.
1290 *
1291 * It is required that the data source supports seeking.
1292 *
1293 * Example:
1294 *
1295 * ```c
1296 * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
1297 * ```
1298 *
1299 * Note that the SDL_LoadWAV function does this same thing for you, but in a
1300 * less messy way:
1301 *
1302 * ```c
1303 * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
1304 * ```
1305 *
1306 * \param src The data source for the WAVE data
1307 * \param freesrc If SDL_TRUE, calls SDL_RWclose() on `src` before returning,
1308 * even in the case of an error
1309 * \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE
1310 * data's format details on successful return
1311 * \param audio_buf A pointer filled with the audio data, allocated by the
1312 * function
1313 * \param audio_len A pointer filled with the length of the audio data buffer
1314 * in bytes
1315 * \returns This function, if successfully called, returns 0. `audio_buf` will
1316 * be filled with a pointer to an allocated buffer containing the
1317 * audio data, and `audio_len` is filled with the length of that
1318 * audio buffer in bytes.
1319 *
1320 * This function returns -1 if the .WAV file cannot be opened, uses
1321 * an unknown data format, or is corrupt; call SDL_GetError() for
1322 * more information.
1323 *
1324 * When the application is done with the data returned in
1325 * `audio_buf`, it should call SDL_free() to dispose of it.
1326 *
1327 * \threadsafety It is safe to call this function from any thread.
1328 *
1329 * \since This function is available since SDL 3.0.0.
1330 *
1331 * \sa SDL_free
1332 * \sa SDL_LoadWAV
1333 */
1334extern DECLSPEC int SDLCALL SDL_LoadWAV_RW(SDL_RWops * src, SDL_bool freesrc,
1335 SDL_AudioSpec * spec, Uint8 ** audio_buf,
1336 Uint32 * audio_len);
1337
1338/**
1339 * Loads a WAV from a file path.
1340 *
1341 * This is a convenience function that is effectively the same as:
1342 *
1343 * ```c
1344 * SDL_LoadWAV_RW(SDL_RWFromFile(path, "rb"), 1, spec, audio_buf, audio_len);
1345 * ```
1346 *
1347 * Note that in SDL2, this was a preprocessor macro and not a real function.
1348 *
1349 * \param path The file path of the WAV file to open.
1350 * \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE
1351 * data's format details on successful return.
1352 * \param audio_buf A pointer filled with the audio data, allocated by the
1353 * function.
1354 * \param audio_len A pointer filled with the length of the audio data buffer
1355 * in bytes
1356 * \returns This function, if successfully called, returns 0. `audio_buf` will
1357 * be filled with a pointer to an allocated buffer containing the
1358 * audio data, and `audio_len` is filled with the length of that
1359 * audio buffer in bytes.
1360 *
1361 * This function returns -1 if the .WAV file cannot be opened, uses
1362 * an unknown data format, or is corrupt; call SDL_GetError() for
1363 * more information.
1364 *
1365 * When the application is done with the data returned in
1366 * `audio_buf`, it should call SDL_free() to dispose of it.
1367 *
1368 * \threadsafety It is safe to call this function from any thread.
1369 *
1370 * \since This function is available since SDL 3.0.0.
1371 *
1372 * \sa SDL_free
1373 * \sa SDL_LoadWAV_RW
1374 */
1375extern DECLSPEC int SDLCALL SDL_LoadWAV(const char *path, SDL_AudioSpec * spec,
1376 Uint8 ** audio_buf, Uint32 * audio_len);
1377
1378
1379
1380#define SDL_MIX_MAXVOLUME 128
1381
1382/**
1383 * Mix audio data in a specified format.
1384 *
1385 * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
1386 * it into `dst`, performing addition, volume adjustment, and overflow
1387 * clipping. The buffer pointed to by `dst` must also be `len` bytes of
1388 * `format` data.
1389 *
1390 * This is provided for convenience -- you can mix your own audio data.
1391 *
1392 * Do not use this function for mixing together more than two streams of
1393 * sample data. The output from repeated application of this function may be
1394 * distorted by clipping, because there is no accumulator with greater range
1395 * than the input (not to mention this being an inefficient way of doing it).
1396 *
1397 * It is a common misconception that this function is required to write audio
1398 * data to an output stream in an audio callback. While you can do that,
1399 * SDL_MixAudioFormat() is really only needed when you're mixing a single
1400 * audio stream with a volume adjustment.
1401 *
1402 * \param dst the destination for the mixed audio
1403 * \param src the source audio buffer to be mixed
1404 * \param format the SDL_AudioFormat structure representing the desired audio
1405 * format
1406 * \param len the length of the audio buffer in bytes
1407 * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
1408 * for full audio volume
1409 * \returns 0 on success or a negative error code on failure; call
1410 * SDL_GetError() for more information.
1411 *
1412 * \threadsafety It is safe to call this function from any thread.
1413 *
1414 * \since This function is available since SDL 3.0.0.
1415 */
1416extern DECLSPEC int SDLCALL SDL_MixAudioFormat(Uint8 * dst,
1417 const Uint8 * src,
1418 SDL_AudioFormat format,
1419 Uint32 len, int volume);
1420
1421/**
1422 * Convert some audio data of one format to another format.
1423 *
1424 * Please note that this function is for convenience, but should not be used
1425 * to resample audio in blocks, as it will introduce audio artifacts on the
1426 * boundaries. You should only use this function if you are converting audio
1427 * data in its entirety in one call. If you want to convert audio in smaller
1428 * chunks, use an SDL_AudioStream, which is designed for this situation.
1429 *
1430 * Internally, this function creates and destroys an SDL_AudioStream on each
1431 * use, so it's also less efficient than using one directly, if you need to
1432 * convert multiple times.
1433 *
1434 * \param src_spec The format details of the input audio
1435 * \param src_data The audio data to be converted
1436 * \param src_len The len of src_data
1437 * \param dst_spec The format details of the output audio
1438 * \param dst_data Will be filled with a pointer to converted audio data,
1439 * which should be freed with SDL_free(). On error, it will be
1440 * NULL.
1441 * \param dst_len Will be filled with the len of dst_data
1442 * \returns 0 on success or a negative error code on failure; call
1443 * SDL_GetError() for more information.
1444 *
1445 * \threadsafety It is safe to call this function from any thread.
1446 *
1447 * \since This function is available since SDL 3.0.0.
1448 *
1449 * \sa SDL_CreateAudioStream
1450 */
1451extern DECLSPEC int SDLCALL SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec,
1452 const Uint8 *src_data,
1453 int src_len,
1454 const SDL_AudioSpec *dst_spec,
1455 Uint8 **dst_data,
1456 int *dst_len);
1457
1458
1459/**
1460 * Get the appropriate memset value for silencing an audio format.
1461 *
1462 * The value returned by this function can be used as the second argument to
1463 * memset (or SDL_memset) to set an audio buffer in a specific format to
1464 * silence.
1465 *
1466 * \param format the audio data format to query.
1467 * \returns A byte value that can be passed to memset.
1468 *
1469 * \threadsafety It is safe to call this function from any thread.
1470 *
1471 * \since This function is available since SDL 3.0.0.
1472 */
1473extern DECLSPEC int SDLCALL SDL_GetSilenceValueForFormat(SDL_AudioFormat format);
1474
1475
1476/* Ends C function definitions when using C++ */
1477#ifdef __cplusplus
1478}
1479#endif
1480#include <SDL3/SDL_close_code.h>
1481
1482#endif /* SDL_audio_h_ */
int SDL_UnlockAudioStream(SDL_AudioStream *stream)
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)
int SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata)
int SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata)
struct SDL_AudioStream SDL_AudioStream
Definition SDL_audio.h:170
int SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata)
void(* SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount)
Definition SDL_audio.h:1017
Uint16 SDL_AudioFormat
Audio format flags.
Definition SDL_audio.h:75
int SDL_FlushAudioStream(SDL_AudioStream *stream)
int SDL_GetNumAudioDrivers(void)
int SDL_SetAudioStreamFrequencyRatio(SDL_AudioStream *stream, float ratio)
char * SDL_GetAudioDeviceName(SDL_AudioDeviceID devid)
Uint32 SDL_AudioDeviceID
Definition SDL_audio.h:143
int SDL_GetAudioStreamQueued(SDL_AudioStream *stream)
int 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)
int SDL_GetSilenceValueForFormat(SDL_AudioFormat format)
const char * SDL_GetCurrentAudioDriver(void)
int SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream)
SDL_AudioStream * SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata)
void(* SDL_AudioPostmixCallback)(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen)
Definition SDL_audio.h:1197
int SDL_PauseAudioDevice(SDL_AudioDeviceID dev)
float SDL_GetAudioStreamFrequencyRatio(SDL_AudioStream *stream)
int SDL_GetAudioStreamAvailable(SDL_AudioStream *stream)
SDL_AudioDeviceID * SDL_GetAudioCaptureDevices(int *count)
int SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec, int *sample_frames)
int SDL_LoadWAV(const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
int SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len)
int SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream **streams, int num_streams)
SDL_bool SDL_AudioDevicePaused(SDL_AudioDeviceID dev)
void SDL_UnbindAudioStreams(SDL_AudioStream **streams, int num_streams)
void SDL_DestroyAudioStream(SDL_AudioStream *stream)
int SDL_LockAudioStream(SDL_AudioStream *stream)
void SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
int SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len)
int SDL_LoadWAV_RW(SDL_RWops *src, SDL_bool freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
int SDL_ResumeAudioDevice(SDL_AudioDeviceID dev)
int SDL_ClearAudioStream(SDL_AudioStream *stream)
SDL_AudioDeviceID * SDL_GetAudioOutputDevices(int *count)
int SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, int volume)
SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec)
int SDL_SetAudioStreamFormat(SDL_AudioStream *stream, const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec)
int SDL_GetAudioStreamFormat(SDL_AudioStream *stream, SDL_AudioSpec *src_spec, SDL_AudioSpec *dst_spec)
SDL_AudioDeviceID SDL_GetAudioStreamDevice(SDL_AudioStream *stream)
Functions for reading and writing endian-specific values.
Functions to provide thread synchronization primitives.
This is a general header that includes C language support.
uint8_t Uint8
Definition SDL_stdinc.h:147
uint16_t Uint16
Definition SDL_stdinc.h:159
SDL_bool
Definition SDL_stdinc.h:130
uint32_t Uint32
Definition SDL_stdinc.h:171
Header for the SDL thread management routines.
SDL_AudioFormat format
Definition SDL_audio.h:150