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 * Tell the stream that you're done sending data, and anything being buffered
863 * should be converted/resampled and made available immediately.
864 *
865 * It is legal to add more data to a stream after flushing, but there will be
866 * audio gaps in the output. Generally this is intended to signal the end of
867 * input, so the complete output becomes available.
868 *
869 * \param stream The audio stream to flush
870 * \returns 0 on success or a negative error code on failure; call
871 * SDL_GetError() for more information.
872 *
873 * \threadsafety It is safe to call this function from any thread.
874 *
875 * \since This function is available since SDL 3.0.0.
876 *
877 * \sa SDL_CreateAudioStream
878 * \sa SDL_PutAudioStreamData
879 * \sa SDL_GetAudioStreamData
880 * \sa SDL_GetAudioStreamAvailable
881 * \sa SDL_ClearAudioStream
882 * \sa SDL_DestroyAudioStream
883 */
884extern DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
885
886/**
887 * Clear any pending data in the stream without converting it
888 *
889 * \param stream The audio stream to clear
890 * \returns 0 on success or a negative error code on failure; call
891 * SDL_GetError() for more information.
892 *
893 * \threadsafety It is safe to call this function from any thread.
894 *
895 * \since This function is available since SDL 3.0.0.
896 *
897 * \sa SDL_CreateAudioStream
898 * \sa SDL_PutAudioStreamData
899 * \sa SDL_GetAudioStreamData
900 * \sa SDL_GetAudioStreamAvailable
901 * \sa SDL_FlushAudioStream
902 * \sa SDL_DestroyAudioStream
903 */
904extern DECLSPEC int SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
905
906/**
907 * Lock an audio stream for serialized access.
908 *
909 * Each SDL_AudioStream has an internal mutex it uses to protect its data
910 * structures from threading conflicts. This function allows an app to lock
911 * that mutex, which could be useful if registering callbacks on this stream.
912 *
913 * One does not need to lock a stream to use in it most cases, as the stream
914 * manages this lock internally. However, this lock is held during callbacks,
915 * which may run from arbitrary threads at any time, so if an app needs to
916 * protect shared data during those callbacks, locking the stream guarantees
917 * that the callback is not running while the lock is held.
918 *
919 * As this is just a wrapper over SDL_LockMutex for an internal lock, it has
920 * all the same attributes (recursive locks are allowed, etc).
921 *
922 * \param stream The audio stream to lock.
923 * \returns 0 on success or a negative error code on failure; call
924 * SDL_GetError() for more information.
925 *
926 * \threadsafety It is safe to call this function from any thread.
927 *
928 * \since This function is available since SDL 3.0.0.
929 *
930 * \sa SDL_UnlockAudioStream
931 * \sa SDL_SetAudioStreamPutCallback
932 * \sa SDL_SetAudioStreamGetCallback
933 */
934extern DECLSPEC int SDLCALL SDL_LockAudioStream(SDL_AudioStream *stream);
935
936
937/**
938 * Unlock an audio stream for serialized access.
939 *
940 * This unlocks an audio stream after a call to SDL_LockAudioStream.
941 *
942 * \param stream The audio stream to unlock.
943 * \returns 0 on success or a negative error code on failure; call
944 * SDL_GetError() for more information.
945 *
946 * \threadsafety You should only call this from the same thread that
947 * previously called SDL_LockAudioStream.
948 *
949 * \since This function is available since SDL 3.0.0.
950 *
951 * \sa SDL_LockAudioStream
952 * \sa SDL_SetAudioStreamPutCallback
953 * \sa SDL_SetAudioStreamGetCallback
954 */
955extern DECLSPEC int SDLCALL SDL_UnlockAudioStream(SDL_AudioStream *stream);
956
957/**
958 * A callback that fires when data passes through an SDL_AudioStream.
959 *
960 * Apps can (optionally) register a callback with an audio stream that
961 * is called when data is added with SDL_PutAudioStreamData, or requested
962 * with SDL_GetAudioStreamData. These callbacks may run from any
963 * thread, so if you need to protect shared data, you should use
964 * SDL_LockAudioStream to serialize access; this lock will be held by
965 * before your callback is called, so your callback does not need to
966 * manage the lock explicitly.
967 *
968 * Two values are offered here: one is the amount of additional data needed
969 * to satisfy the immediate request (which might be zero if the stream
970 * already has enough data queued) and the other is the total amount
971 * being requested. In a Get call triggering a Put callback, these
972 * values can be different. In a Put call triggering a Get callback,
973 * these values are always the same.
974 *
975 * Byte counts might be slightly overestimated due to buffering or
976 * resampling, and may change from call to call.
977 *
978 * \param stream The SDL audio stream associated with this callback.
979 * \param additional_amount The amount of data, in bytes, that is needed right now.
980 * \param total_amount The total amount of data requested, in bytes, that is requested or available.
981 * \param userdata An opaque pointer provided by the app for their personal use.
982 */
983typedef void (SDLCALL *SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount);
984
985/**
986 * Set a callback that runs when data is requested from an audio stream.
987 *
988 * This callback is called _before_ data is obtained from the stream, giving
989 * the callback the chance to add more on-demand.
990 *
991 * The callback can (optionally) call SDL_PutAudioStreamData() to add more
992 * audio to the stream during this call; if needed, the request that triggered
993 * this callback will obtain the new data immediately.
994 *
995 * The callback's `approx_request` argument is roughly how many bytes of
996 * _unconverted_ data (in the stream's input format) is needed by the caller,
997 * although this may overestimate a little for safety. This takes into account
998 * how much is already in the stream and only asks for any extra necessary to
999 * resolve the request, which means the callback may be asked for zero bytes,
1000 * and a different amount on each call.
1001 *
1002 * The callback is not required to supply exact amounts; it is allowed to
1003 * supply too much or too little or none at all. The caller will get what's
1004 * available, up to the amount they requested, regardless of this callback's
1005 * outcome.
1006 *
1007 * Clearing or flushing an audio stream does not call this callback.
1008 *
1009 * This function obtains the stream's lock, which means any existing callback
1010 * (get or put) in progress will finish running before setting the new
1011 * callback.
1012 *
1013 * Setting a NULL function turns off the callback.
1014 *
1015 * \param stream the audio stream to set the new callback on.
1016 * \param callback the new callback function to call when data is added to the
1017 * stream.
1018 * \param userdata an opaque pointer provided to the callback for its own
1019 * personal use.
1020 * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
1021 *
1022 * \threadsafety It is safe to call this function from any thread.
1023 *
1024 * \since This function is available since SDL 3.0.0.
1025 *
1026 * \sa SDL_SetAudioStreamPutCallback
1027 */
1028extern DECLSPEC int SDLCALL SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
1029
1030/**
1031 * Set a callback that runs when data is added to an audio stream.
1032 *
1033 * This callback is called _after_ the data is added to the stream, giving the
1034 * callback the chance to obtain it immediately.
1035 *
1036 * The callback can (optionally) call SDL_GetAudioStreamData() to obtain audio
1037 * from the stream during this call.
1038 *
1039 * The callback's `approx_request` argument is how many bytes of _converted_
1040 * data (in the stream's output format) was provided by the caller, although
1041 * this may underestimate a little for safety. This value might be less than
1042 * what is currently available in the stream, if data was already there, and
1043 * might be less than the caller provided if the stream needs to keep a buffer
1044 * to aid in resampling. Which means the callback may be provided with zero
1045 * bytes, and a different amount on each call.
1046 *
1047 * The callback may call SDL_GetAudioStreamAvailable to see the total amount
1048 * currently available to read from the stream, instead of the total provided
1049 * by the current call.
1050 *
1051 * The callback is not required to obtain all data. It is allowed to read less
1052 * or none at all. Anything not read now simply remains in the stream for
1053 * later access.
1054 *
1055 * Clearing or flushing an audio stream does not call this callback.
1056 *
1057 * This function obtains the stream's lock, which means any existing callback
1058 * (get or put) in progress will finish running before setting the new
1059 * callback.
1060 *
1061 * Setting a NULL function turns off the callback.
1062 *
1063 * \param stream the audio stream to set the new callback on.
1064 * \param callback the new callback function to call when data is added to the
1065 * stream.
1066 * \param userdata an opaque pointer provided to the callback for its own
1067 * personal use.
1068 * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
1069 *
1070 * \threadsafety It is safe to call this function from any thread.
1071 *
1072 * \since This function is available since SDL 3.0.0.
1073 *
1074 * \sa SDL_SetAudioStreamGetCallback
1075 */
1076extern DECLSPEC int SDLCALL SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
1077
1078
1079/**
1080 * Free an audio stream
1081 *
1082 * \param stream The audio stream to free
1083 *
1084 * \threadsafety It is safe to call this function from any thread.
1085 *
1086 * \since This function is available since SDL 3.0.0.
1087 *
1088 * \sa SDL_CreateAudioStream
1089 * \sa SDL_PutAudioStreamData
1090 * \sa SDL_GetAudioStreamData
1091 * \sa SDL_GetAudioStreamAvailable
1092 * \sa SDL_FlushAudioStream
1093 * \sa SDL_ClearAudioStream
1094 */
1095extern DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
1096
1097
1098/**
1099 * Convenience function for straightforward audio init for the common case.
1100 *
1101 * If all your app intends to do is provide a single source of PCM audio, this
1102 * function allows you to do all your audio setup in a single call.
1103 *
1104 * This is intended to be a clean means to migrate apps from SDL2.
1105 *
1106 * This function will open an audio device, create a stream and bind it.
1107 * Unlike other methods of setup, the audio device will be closed when this
1108 * stream is destroyed, so the app can treat the returned SDL_AudioStream as
1109 * the only object needed to manage audio playback.
1110 *
1111 * Also unlike other functions, the audio device begins paused. This is to map
1112 * more closely to SDL2-style behavior, and since there is no extra step here
1113 * to bind a stream to begin audio flowing. The audio device should be resumed
1114 * with SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream));
1115 *
1116 * This function works with both playback and capture devices.
1117 *
1118 * The `spec` parameter represents the app's side of the audio stream. That
1119 * is, for recording audio, this will be the output format, and for playing
1120 * audio, this will be the input format.
1121 *
1122 * If you don't care about opening a specific audio device, you can (and
1123 * probably _should_), use SDL_AUDIO_DEVICE_DEFAULT_OUTPUT for playback and
1124 * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE for recording.
1125 *
1126 * One can optionally provide a callback function; if NULL, the app is
1127 * expected to queue audio data for playback (or unqueue audio data if
1128 * capturing). Otherwise, the callback will begin to fire once the device is
1129 * unpaused.
1130 *
1131 * \param devid an audio device to open, or SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
1132 * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE.
1133 * \param spec the audio stream's data format. Required.
1134 * \param callback A callback where the app will provide new data for
1135 * playback, or receive new data for capture. Can be NULL, in
1136 * which case the app will need to call SDL_PutAudioStreamData
1137 * or SDL_GetAudioStreamData as necessary.
1138 * \param userdata App-controlled pointer passed to callback. Can be NULL.
1139 * Ignored if callback is NULL.
1140 * \returns an audio stream on success, ready to use. NULL on error; call
1141 * SDL_GetError() for more information. When done with this stream,
1142 * call SDL_DestroyAudioStream to free resources and close the
1143 * device.
1144 *
1145 * \threadsafety It is safe to call this function from any thread.
1146 *
1147 * \since This function is available since SDL 3.0.0.
1148 *
1149 * \sa SDL_GetAudioStreamDevice
1150 * \sa SDL_ResumeAudioDevice
1151 */
1152extern DECLSPEC SDL_AudioStream *SDLCALL SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata);
1153
1154
1155/**
1156 * A callback that fires when data is about to be fed to an audio device.
1157 *
1158 * This is useful for accessing the final mix, perhaps for writing a
1159 * visualizer or applying a final effect to the audio data before playback.
1160 *
1161 * \sa SDL_SetAudioDevicePostmixCallback
1162 */
1163typedef void (SDLCALL *SDL_AudioPostmixCallback)(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen);
1164
1165/**
1166 * Set a callback that fires when data is about to be fed to an audio device.
1167 *
1168 * This is useful for accessing the final mix, perhaps for writing a
1169 * visualizer or applying a final effect to the audio data before playback.
1170 *
1171 * The buffer is the final mix of all bound audio streams on an opened device;
1172 * this callback will fire regularly for any device that is both opened and
1173 * unpaused. If there is no new data to mix, either because no streams are
1174 * bound to the device or all the streams are empty, this callback will still
1175 * fire with the entire buffer set to silence.
1176 *
1177 * This callback is allowed to make changes to the data; the contents of the
1178 * buffer after this call is what is ultimately passed along to the hardware.
1179 *
1180 * The callback is always provided the data in float format (values from -1.0f
1181 * to 1.0f), but the number of channels or sample rate may be different than
1182 * the format the app requested when opening the device; SDL might have had to
1183 * manage a conversion behind the scenes, or the playback might have jumped to
1184 * new physical hardware when a system default changed, etc. These details may
1185 * change between calls. Accordingly, the size of the buffer might change
1186 * between calls as well.
1187 *
1188 * This callback can run at any time, and from any thread; if you need to
1189 * serialize access to your app's data, you should provide and use a mutex or
1190 * other synchronization device.
1191 *
1192 * All of this to say: there are specific needs this callback can fulfill, but
1193 * it is not the simplest interface. Apps should generally provide audio in
1194 * their preferred format through an SDL_AudioStream and let SDL handle the
1195 * difference.
1196 *
1197 * This function is extremely time-sensitive; the callback should do the least
1198 * amount of work possible and return as quickly as it can. The longer the
1199 * callback runs, the higher the risk of audio dropouts or other problems.
1200 *
1201 * This function will block until the audio device is in between iterations,
1202 * so any existing callback that might be running will finish before this
1203 * function sets the new callback and returns.
1204 *
1205 * Setting a NULL callback function disables any previously-set callback.
1206 *
1207 * \param devid The ID of an opened audio device.
1208 * \param callback A callback function to be called. Can be NULL.
1209 * \param userdata App-controlled pointer passed to callback. Can be NULL.
1210 * \returns zero on success, -1 on error; call SDL_GetError() for more
1211 * information.
1212 *
1213 * \threadsafety It is safe to call this function from any thread.
1214 *
1215 * \since This function is available since SDL 3.0.0.
1216 */
1217extern DECLSPEC int SDLCALL SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata);
1218
1219
1220/**
1221 * Load the audio data of a WAVE file into memory.
1222 *
1223 * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
1224 * be valid pointers. The entire data portion of the file is then loaded into
1225 * memory and decoded if necessary.
1226 *
1227 * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
1228 * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
1229 * A-law and mu-law (8 bits). Other formats are currently unsupported and
1230 * cause an error.
1231 *
1232 * If this function succeeds, the return value is zero and the pointer to the
1233 * audio data allocated by the function is written to `audio_buf` and its
1234 * length in bytes to `audio_len`. The SDL_AudioSpec members `freq`,
1235 * `channels`, and `format` are set to the values of the audio data in the
1236 * buffer. The `samples` member is set to a sane default and all others are
1237 * set to zero.
1238 *
1239 * It's necessary to use SDL_free() to free the audio data returned in
1240 * `audio_buf` when it is no longer used.
1241 *
1242 * Because of the underspecification of the .WAV format, there are many
1243 * problematic files in the wild that cause issues with strict decoders. To
1244 * provide compatibility with these files, this decoder is lenient in regards
1245 * to the truncation of the file, the fact chunk, and the size of the RIFF
1246 * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
1247 * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
1248 * tune the behavior of the loading process.
1249 *
1250 * Any file that is invalid (due to truncation, corruption, or wrong values in
1251 * the headers), too big, or unsupported causes an error. Additionally, any
1252 * critical I/O error from the data source will terminate the loading process
1253 * with an error. The function returns NULL on error and in all cases (with
1254 * the exception of `src` being NULL), an appropriate error message will be
1255 * set.
1256 *
1257 * It is required that the data source supports seeking.
1258 *
1259 * Example:
1260 *
1261 * ```c
1262 * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
1263 * ```
1264 *
1265 * Note that the SDL_LoadWAV function does this same thing for you, but in a
1266 * less messy way:
1267 *
1268 * ```c
1269 * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
1270 * ```
1271 *
1272 * \param src The data source for the WAVE data
1273 * \param freesrc If SDL_TRUE, calls SDL_RWclose() on `src` before returning,
1274 * even in the case of an error
1275 * \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE
1276 * data's format details on successful return
1277 * \param audio_buf A pointer filled with the audio data, allocated by the
1278 * function
1279 * \param audio_len A pointer filled with the length of the audio data buffer
1280 * in bytes
1281 * \returns This function, if successfully called, returns 0. `audio_buf` will
1282 * be filled with a pointer to an allocated buffer containing the
1283 * audio data, and `audio_len` is filled with the length of that
1284 * audio buffer in bytes.
1285 *
1286 * This function returns -1 if the .WAV file cannot be opened, uses
1287 * an unknown data format, or is corrupt; call SDL_GetError() for
1288 * more information.
1289 *
1290 * When the application is done with the data returned in
1291 * `audio_buf`, it should call SDL_free() to dispose of it.
1292 *
1293 * \threadsafety It is safe to call this function from any thread.
1294 *
1295 * \since This function is available since SDL 3.0.0.
1296 *
1297 * \sa SDL_free
1298 * \sa SDL_LoadWAV
1299 */
1300extern DECLSPEC int SDLCALL SDL_LoadWAV_RW(SDL_RWops * src, SDL_bool freesrc,
1301 SDL_AudioSpec * spec, Uint8 ** audio_buf,
1302 Uint32 * audio_len);
1303
1304/**
1305 * Loads a WAV from a file path.
1306 *
1307 * This is a convenience function that is effectively the same as:
1308 *
1309 * ```c
1310 * SDL_LoadWAV_RW(SDL_RWFromFile(path, "rb"), 1, spec, audio_buf, audio_len);
1311 * ```
1312 *
1313 * Note that in SDL2, this was a preprocessor macro and not a real function.
1314 *
1315 * \param path The file path of the WAV file to open.
1316 * \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE
1317 * data's format details on successful return.
1318 * \param audio_buf A pointer filled with the audio data, allocated by the
1319 * function.
1320 * \param audio_len A pointer filled with the length of the audio data buffer
1321 * in bytes
1322 * \returns This function, if successfully called, returns 0. `audio_buf` will
1323 * be filled with a pointer to an allocated buffer containing the
1324 * audio data, and `audio_len` is filled with the length of that
1325 * audio buffer in bytes.
1326 *
1327 * This function returns -1 if the .WAV file cannot be opened, uses
1328 * an unknown data format, or is corrupt; call SDL_GetError() for
1329 * more information.
1330 *
1331 * When the application is done with the data returned in
1332 * `audio_buf`, it should call SDL_free() to dispose of it.
1333 *
1334 * \threadsafety It is safe to call this function from any thread.
1335 *
1336 * \since This function is available since SDL 3.0.0.
1337 *
1338 * \sa SDL_free
1339 * \sa SDL_LoadWAV_RW
1340 */
1341extern DECLSPEC int SDLCALL SDL_LoadWAV(const char *path, SDL_AudioSpec * spec,
1342 Uint8 ** audio_buf, Uint32 * audio_len);
1343
1344
1345
1346#define SDL_MIX_MAXVOLUME 128
1347
1348/**
1349 * Mix audio data in a specified format.
1350 *
1351 * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
1352 * it into `dst`, performing addition, volume adjustment, and overflow
1353 * clipping. The buffer pointed to by `dst` must also be `len` bytes of
1354 * `format` data.
1355 *
1356 * This is provided for convenience -- you can mix your own audio data.
1357 *
1358 * Do not use this function for mixing together more than two streams of
1359 * sample data. The output from repeated application of this function may be
1360 * distorted by clipping, because there is no accumulator with greater range
1361 * than the input (not to mention this being an inefficient way of doing it).
1362 *
1363 * It is a common misconception that this function is required to write audio
1364 * data to an output stream in an audio callback. While you can do that,
1365 * SDL_MixAudioFormat() is really only needed when you're mixing a single
1366 * audio stream with a volume adjustment.
1367 *
1368 * \param dst the destination for the mixed audio
1369 * \param src the source audio buffer to be mixed
1370 * \param format the SDL_AudioFormat structure representing the desired audio
1371 * format
1372 * \param len the length of the audio buffer in bytes
1373 * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
1374 * for full audio volume
1375 * \returns 0 on success or a negative error code on failure; call
1376 * SDL_GetError() for more information.
1377 *
1378 * \threadsafety It is safe to call this function from any thread.
1379 *
1380 * \since This function is available since SDL 3.0.0.
1381 */
1382extern DECLSPEC int SDLCALL SDL_MixAudioFormat(Uint8 * dst,
1383 const Uint8 * src,
1384 SDL_AudioFormat format,
1385 Uint32 len, int volume);
1386
1387/**
1388 * Convert some audio data of one format to another format.
1389 *
1390 * Please note that this function is for convenience, but should not be used
1391 * to resample audio in blocks, as it will introduce audio artifacts on the
1392 * boundaries. You should only use this function if you are converting audio
1393 * data in its entirety in one call. If you want to convert audio in smaller
1394 * chunks, use an SDL_AudioStream, which is designed for this situation.
1395 *
1396 * Internally, this function creates and destroys an SDL_AudioStream on each
1397 * use, so it's also less efficient than using one directly, if you need to
1398 * convert multiple times.
1399 *
1400 * \param src_spec The format details of the input audio
1401 * \param src_data The audio data to be converted
1402 * \param src_len The len of src_data
1403 * \param dst_spec The format details of the output audio
1404 * \param dst_data Will be filled with a pointer to converted audio data,
1405 * which should be freed with SDL_free(). On error, it will be
1406 * NULL.
1407 * \param dst_len Will be filled with the len of dst_data
1408 * \returns 0 on success or a negative error code on failure; call
1409 * SDL_GetError() for more information.
1410 *
1411 * \threadsafety It is safe to call this function from any thread.
1412 *
1413 * \since This function is available since SDL 3.0.0.
1414 *
1415 * \sa SDL_CreateAudioStream
1416 */
1417extern DECLSPEC int SDLCALL SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec,
1418 const Uint8 *src_data,
1419 int src_len,
1420 const SDL_AudioSpec *dst_spec,
1421 Uint8 **dst_data,
1422 int *dst_len);
1423
1424
1425/**
1426 * Get the appropriate memset value for silencing an audio format.
1427 *
1428 * The value returned by this function can be used as the second argument to
1429 * memset (or SDL_memset) to set an audio buffer in a specific format to
1430 * silence.
1431 *
1432 * \param format the audio data format to query.
1433 * \returns A byte value that can be passed to memset.
1434 *
1435 * \threadsafety It is safe to call this function from any thread.
1436 *
1437 * \since This function is available since SDL 3.0.0.
1438 */
1439extern DECLSPEC int SDLCALL SDL_GetSilenceValueForFormat(SDL_AudioFormat format);
1440
1441
1442/* Ends C function definitions when using C++ */
1443#ifdef __cplusplus
1444}
1445#endif
1446#include <SDL3/SDL_close_code.h>
1447
1448#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:983
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_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:1163
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