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