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