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/* !!! FIXME: several functions in here need Doxygen comments. */
23
24/**
25 * \file SDL_audio.h
26 *
27 * \brief Access to the raw audio mixing buffer for the SDL library.
28 */
29
30#ifndef SDL_audio_h_
31#define SDL_audio_h_
32
33#include <SDL3/SDL_stdinc.h>
34#include <SDL3/SDL_error.h>
35#include <SDL3/SDL_endian.h>
36#include <SDL3/SDL_mutex.h>
37#include <SDL3/SDL_thread.h>
38#include <SDL3/SDL_rwops.h>
39
40#include <SDL3/SDL_begin_code.h>
41/* Set up for C function definitions, even when using C++ */
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/**
47 * \brief Audio format flags.
48 *
49 * These are what the 16 bits in SDL_AudioFormat currently mean...
50 * (Unspecified bits are always zero).
51 *
52 * \verbatim
53 ++-----------------------sample is signed if set
54 ||
55 || ++-----------sample is bigendian if set
56 || ||
57 || || ++---sample is float if set
58 || || ||
59 || || || +---sample bit size---+
60 || || || | |
61 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
62 \endverbatim
63 *
64 * There are macros in SDL 2.0 and later to query these bits.
65 */
67
68/**
69 * \name Audio flags
70 */
71/* @{ */
72
73#define SDL_AUDIO_MASK_BITSIZE (0xFF)
74#define SDL_AUDIO_MASK_DATATYPE (1<<8)
75#define SDL_AUDIO_MASK_ENDIAN (1<<12)
76#define SDL_AUDIO_MASK_SIGNED (1<<15)
77#define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
78#define SDL_AUDIO_ISFLOAT(x) (x & SDL_AUDIO_MASK_DATATYPE)
79#define SDL_AUDIO_ISBIGENDIAN(x) (x & SDL_AUDIO_MASK_ENDIAN)
80#define SDL_AUDIO_ISSIGNED(x) (x & SDL_AUDIO_MASK_SIGNED)
81#define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))
82#define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))
83#define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))
84
85/**
86 * \name Audio format flags
87 *
88 * Defaults to LSB byte order.
89 */
90/* @{ */
91#define SDL_AUDIO_U8 0x0008 /**< Unsigned 8-bit samples */
92#define SDL_AUDIO_S8 0x8008 /**< Signed 8-bit samples */
93#define SDL_AUDIO_S16LSB 0x8010 /**< Signed 16-bit samples */
94#define SDL_AUDIO_S16MSB 0x9010 /**< As above, but big-endian byte order */
95#define SDL_AUDIO_S16 SDL_AUDIO_S16LSB
96/* @} */
97
98/**
99 * \name int32 support
100 */
101/* @{ */
102#define SDL_AUDIO_S32LSB 0x8020 /**< 32-bit integer samples */
103#define SDL_AUDIO_S32MSB 0x9020 /**< As above, but big-endian byte order */
104#define SDL_AUDIO_S32 SDL_AUDIO_S32LSB
105/* @} */
106
107/**
108 * \name float32 support
109 */
110/* @{ */
111#define SDL_AUDIO_F32LSB 0x8120 /**< 32-bit floating point samples */
112#define SDL_AUDIO_F32MSB 0x9120 /**< As above, but big-endian byte order */
113#define SDL_AUDIO_F32 SDL_AUDIO_F32LSB
114/* @} */
115
116/**
117 * \name Native audio byte ordering
118 */
119/* @{ */
120#if SDL_BYTEORDER == SDL_LIL_ENDIAN
121#define SDL_AUDIO_S16SYS SDL_AUDIO_S16LSB
122#define SDL_AUDIO_S32SYS SDL_AUDIO_S32LSB
123#define SDL_AUDIO_F32SYS SDL_AUDIO_F32LSB
124#else
125#define SDL_AUDIO_S16SYS SDL_AUDIO_S16MSB
126#define SDL_AUDIO_S32SYS SDL_AUDIO_S32MSB
127#define SDL_AUDIO_F32SYS SDL_AUDIO_F32MSB
128#endif
129/* @} */
130
131/**
132 * \name Allow change flags
133 *
134 * Which audio format changes are allowed when opening a device.
135 */
136/* @{ */
137#define SDL_AUDIO_ALLOW_FREQUENCY_CHANGE 0x00000001
138#define SDL_AUDIO_ALLOW_FORMAT_CHANGE 0x00000002
139#define SDL_AUDIO_ALLOW_CHANNELS_CHANGE 0x00000004
140#define SDL_AUDIO_ALLOW_SAMPLES_CHANGE 0x00000008
141#define SDL_AUDIO_ALLOW_ANY_CHANGE (SDL_AUDIO_ALLOW_FREQUENCY_CHANGE|SDL_AUDIO_ALLOW_FORMAT_CHANGE|SDL_AUDIO_ALLOW_CHANNELS_CHANGE|SDL_AUDIO_ALLOW_SAMPLES_CHANGE)
142/* @} */
143
144/* @} *//* Audio flags */
145
146/**
147 * This function is called when the audio device needs more data.
148 *
149 * \param userdata An application-specific parameter saved in
150 * the SDL_AudioSpec structure
151 * \param stream A pointer to the audio data buffer.
152 * \param len The length of that buffer in bytes.
153 *
154 * Once the callback returns, the buffer will no longer be valid.
155 * Stereo samples are stored in a LRLRLR ordering.
156 *
157 * You can choose to avoid callbacks and use SDL_QueueAudio() instead, if
158 * you like. Just open your audio device with a NULL callback.
159 */
160typedef void (SDLCALL * SDL_AudioCallback) (void *userdata, Uint8 * stream,
161 int len);
162
163/**
164 * The calculated values in this structure are calculated by SDL_OpenAudioDevice().
165 *
166 * For multi-channel audio, the default SDL channel mapping is:
167 * 2: FL FR (stereo)
168 * 3: FL FR LFE (2.1 surround)
169 * 4: FL FR BL BR (quad)
170 * 5: FL FR LFE BL BR (4.1 surround)
171 * 6: FL FR FC LFE SL SR (5.1 surround - last two can also be BL BR)
172 * 7: FL FR FC LFE BC SL SR (6.1 surround)
173 * 8: FL FR FC LFE BL BR SL SR (7.1 surround)
174 */
175typedef struct SDL_AudioSpec
176{
177 int freq; /**< DSP frequency -- samples per second */
178 SDL_AudioFormat format; /**< Audio data format */
179 Uint8 channels; /**< Number of channels: 1 mono, 2 stereo */
180 Uint8 silence; /**< Audio buffer silence value (calculated) */
181 Uint16 samples; /**< Audio buffer size in sample FRAMES (total samples divided by channel count) */
182 Uint16 padding; /**< Necessary for some compile environments */
183 Uint32 size; /**< Audio buffer size in bytes (calculated) */
184 SDL_AudioCallback callback; /**< Callback that feeds the audio device (NULL to use SDL_QueueAudio()). */
185 void *userdata; /**< Userdata passed to callback (ignored for NULL callbacks). */
187
188
189/* Function prototypes */
190
191/**
192 * \name Driver discovery functions
193 *
194 * These functions return the list of built in audio drivers, in the
195 * order that they are normally initialized by default.
196 */
197/* @{ */
198
199/**
200 * Use this function to get the number of built-in audio drivers.
201 *
202 * This function returns a hardcoded number. This never returns a negative
203 * value; if there are no drivers compiled into this build of SDL, this
204 * function returns zero. The presence of a driver in this list does not mean
205 * it will function, it just means SDL is capable of interacting with that
206 * interface. For example, a build of SDL might have esound support, but if
207 * there's no esound server available, SDL's esound driver would fail if used.
208 *
209 * By default, SDL tries all drivers, in its preferred order, until one is
210 * found to be usable.
211 *
212 * \returns the number of built-in audio drivers.
213 *
214 * \since This function is available since SDL 3.0.0.
215 *
216 * \sa SDL_GetAudioDriver
217 */
218extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
219
220/**
221 * Use this function to get the name of a built in audio driver.
222 *
223 * The list of audio drivers is given in the order that they are normally
224 * initialized by default; the drivers that seem more reasonable to choose
225 * first (as far as the SDL developers believe) are earlier in the list.
226 *
227 * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
228 * "coreaudio" or "xaudio2". These never have Unicode characters, and are not
229 * meant to be proper names.
230 *
231 * \param index the index of the audio driver; the value ranges from 0 to
232 * SDL_GetNumAudioDrivers() - 1
233 * \returns the name of the audio driver at the requested index, or NULL if an
234 * invalid index was specified.
235 *
236 * \since This function is available since SDL 3.0.0.
237 *
238 * \sa SDL_GetNumAudioDrivers
239 */
240extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
241/* @} */
242
243/**
244 * Get the name of the current audio driver.
245 *
246 * The returned string points to internal static memory and thus never becomes
247 * invalid, even if you quit the audio subsystem and initialize a new driver
248 * (although such a case would return a different static string from another
249 * call to this function, of course). As such, you should not modify or free
250 * the returned string.
251 *
252 * \returns the name of the current audio driver or NULL if no driver has been
253 * initialized.
254 *
255 * \since This function is available since SDL 3.0.0.
256 */
257extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
258
259/**
260 * SDL Audio Device IDs.
261 */
263
264/**
265 * Get the number of built-in audio devices.
266 *
267 * This function is only valid after successfully initializing the audio
268 * subsystem.
269 *
270 * Note that audio capture support is not implemented as of SDL 2.0.4, so the
271 * `iscapture` parameter is for future expansion and should always be zero for
272 * now.
273 *
274 * This function will return -1 if an explicit list of devices can't be
275 * determined. Returning -1 is not an error. For example, if SDL is set up to
276 * talk to a remote audio server, it can't list every one available on the
277 * Internet, but it will still allow a specific host to be specified in
278 * SDL_OpenAudioDevice().
279 *
280 * In many common cases, when this function returns a value <= 0, it can still
281 * successfully open the default device (NULL for first argument of
282 * SDL_OpenAudioDevice()).
283 *
284 * This function may trigger a complete redetect of available hardware. It
285 * should not be called for each iteration of a loop, but rather once at the
286 * start of a loop:
287 *
288 * ```c
289 * // Don't do this:
290 * for (int i = 0; i < SDL_GetNumAudioDevices(0); i++)
291 *
292 * // do this instead:
293 * const int count = SDL_GetNumAudioDevices(0);
294 * for (int i = 0; i < count; ++i) { do_something_here(); }
295 * ```
296 *
297 * \param iscapture zero to request playback devices, non-zero to request
298 * recording devices
299 * \returns the number of available devices exposed by the current driver or
300 * -1 if an explicit list of devices can't be determined. A return
301 * value of -1 does not necessarily mean an error condition.
302 *
303 * \since This function is available since SDL 3.0.0.
304 *
305 * \sa SDL_GetAudioDeviceName
306 * \sa SDL_OpenAudioDevice
307 */
308extern DECLSPEC int SDLCALL SDL_GetNumAudioDevices(int iscapture);
309
310/**
311 * Get the human-readable name of a specific audio device.
312 *
313 * This function is only valid after successfully initializing the audio
314 * subsystem. The values returned by this function reflect the latest call to
315 * SDL_GetNumAudioDevices(); re-call that function to redetect available
316 * hardware.
317 *
318 * The string returned by this function is UTF-8 encoded, read-only, and
319 * managed internally. You are not to free it. If you need to keep the string
320 * for any length of time, you should make your own copy of it, as it will be
321 * invalid next time any of several other SDL functions are called.
322 *
323 * \param index the index of the audio device; valid values range from 0 to
324 * SDL_GetNumAudioDevices() - 1
325 * \param iscapture non-zero to query the list of recording devices, zero to
326 * query the list of output devices.
327 * \returns the name of the audio device at the requested index, or NULL on
328 * error.
329 *
330 * \since This function is available since SDL 3.0.0.
331 *
332 * \sa SDL_GetNumAudioDevices
333 * \sa SDL_GetDefaultAudioInfo
334 */
335extern DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(int index,
336 int iscapture);
337
338/**
339 * Get the preferred audio format of a specific audio device.
340 *
341 * This function is only valid after a successfully initializing the audio
342 * subsystem. The values returned by this function reflect the latest call to
343 * SDL_GetNumAudioDevices(); re-call that function to redetect available
344 * hardware.
345 *
346 * `spec` will be filled with the sample rate, sample format, and channel
347 * count.
348 *
349 * \param index the index of the audio device; valid values range from 0 to
350 * SDL_GetNumAudioDevices() - 1
351 * \param iscapture non-zero to query the list of recording devices, zero to
352 * query the list of output devices.
353 * \param spec The SDL_AudioSpec to be initialized by this function.
354 * \returns 0 on success or a negative error code on failure; call
355 * SDL_GetError() for more information.
356 *
357 * \since This function is available since SDL 3.0.0.
358 *
359 * \sa SDL_GetNumAudioDevices
360 * \sa SDL_GetDefaultAudioInfo
361 */
362extern DECLSPEC int SDLCALL SDL_GetAudioDeviceSpec(int index,
363 int iscapture,
364 SDL_AudioSpec *spec);
365
366
367/**
368 * Get the name and preferred format of the default audio device.
369 *
370 * Some (but not all!) platforms have an isolated mechanism to get information
371 * about the "default" device. This can actually be a completely different
372 * device that's not in the list you get from SDL_GetAudioDeviceSpec(). It can
373 * even be a network address! (This is discussed in SDL_OpenAudioDevice().)
374 *
375 * As a result, this call is not guaranteed to be performant, as it can query
376 * the sound server directly every time, unlike the other query functions. You
377 * should call this function sparingly!
378 *
379 * `spec` will be filled with the sample rate, sample format, and channel
380 * count, if a default device exists on the system. If `name` is provided,
381 * will be filled with either a dynamically-allocated UTF-8 string or NULL.
382 *
383 * \param name A pointer to be filled with the name of the default device (can
384 * be NULL). Please call SDL_free() when you are done with this
385 * pointer!
386 * \param spec The SDL_AudioSpec to be initialized by this function.
387 * \param iscapture non-zero to query the default recording device, zero to
388 * query the default output device.
389 * \returns 0 on success or a negative error code on failure; call
390 * SDL_GetError() for more information.
391 *
392 * \since This function is available since SDL 3.0.0.
393 *
394 * \sa SDL_GetAudioDeviceName
395 * \sa SDL_GetAudioDeviceSpec
396 * \sa SDL_OpenAudioDevice
397 */
398extern DECLSPEC int SDLCALL SDL_GetDefaultAudioInfo(char **name,
399 SDL_AudioSpec *spec,
400 int iscapture);
401
402
403/**
404 * Open a specific audio device.
405 *
406 * Passing in a `device` name of NULL requests the most reasonable default.
407 * The `device` name is a UTF-8 string reported by SDL_GetAudioDeviceName(),
408 * but some drivers allow arbitrary and driver-specific strings, such as a
409 * hostname/IP address for a remote audio server, or a filename in the
410 * diskaudio driver.
411 *
412 * An opened audio device starts out paused, and should be enabled for playing
413 * by calling SDL_PlayAudioDevice(devid) when you are ready for your audio
414 * callback function to be called. Since the audio driver may modify the
415 * requested size of the audio buffer, you should allocate any local mixing
416 * buffers after you open the audio device.
417 *
418 * The audio callback runs in a separate thread in most cases; you can prevent
419 * race conditions between your callback and other threads without fully
420 * pausing playback with SDL_LockAudioDevice(). For more information about the
421 * callback, see SDL_AudioSpec.
422 *
423 * Managing the audio spec via 'desired' and 'obtained':
424 *
425 * When filling in the desired audio spec structure:
426 *
427 * - `desired->freq` should be the frequency in sample-frames-per-second (Hz).
428 * - `desired->format` should be the audio format (`SDL_AUDIO_S16SYS`, etc).
429 * - `desired->samples` is the desired size of the audio buffer, in _sample
430 * frames_ (with stereo output, two samples--left and right--would make a
431 * single sample frame). This number should be a power of two, and may be
432 * adjusted by the audio driver to a value more suitable for the hardware.
433 * Good values seem to range between 512 and 8096 inclusive, depending on
434 * the application and CPU speed. Smaller values reduce latency, but can
435 * lead to underflow if the application is doing heavy processing and cannot
436 * fill the audio buffer in time. Note that the number of sample frames is
437 * directly related to time by the following formula: `ms =
438 * (sampleframes*1000)/freq`
439 * - `desired->size` is the size in _bytes_ of the audio buffer, and is
440 * calculated by SDL_OpenAudioDevice(). You don't initialize this.
441 * - `desired->silence` is the value used to set the buffer to silence, and is
442 * calculated by SDL_OpenAudioDevice(). You don't initialize this.
443 * - `desired->callback` should be set to a function that will be called when
444 * the audio device is ready for more data. It is passed a pointer to the
445 * audio buffer, and the length in bytes of the audio buffer. This function
446 * usually runs in a separate thread, and so you should protect data
447 * structures that it accesses by calling SDL_LockAudioDevice() and
448 * SDL_UnlockAudioDevice() in your code. Alternately, you may pass a NULL
449 * pointer here, and call SDL_QueueAudio() with some frequency, to queue
450 * more audio samples to be played (or for capture devices, call
451 * SDL_DequeueAudio() with some frequency, to obtain audio samples).
452 * - `desired->userdata` is passed as the first parameter to your callback
453 * function. If you passed a NULL callback, this value is ignored.
454 *
455 * `allowed_changes` can have the following flags OR'd together:
456 *
457 * - `SDL_AUDIO_ALLOW_FREQUENCY_CHANGE`
458 * - `SDL_AUDIO_ALLOW_FORMAT_CHANGE`
459 * - `SDL_AUDIO_ALLOW_CHANNELS_CHANGE`
460 * - `SDL_AUDIO_ALLOW_SAMPLES_CHANGE`
461 * - `SDL_AUDIO_ALLOW_ANY_CHANGE`
462 *
463 * These flags specify how SDL should behave when a device cannot offer a
464 * specific feature. If the application requests a feature that the hardware
465 * doesn't offer, SDL will always try to get the closest equivalent.
466 *
467 * For example, if you ask for float32 audio format, but the sound card only
468 * supports int16, SDL will set the hardware to int16. If you had set
469 * SDL_AUDIO_ALLOW_FORMAT_CHANGE, SDL will change the format in the `obtained`
470 * structure. If that flag was *not* set, SDL will prepare to convert your
471 * callback's float32 audio to int16 before feeding it to the hardware and
472 * will keep the originally requested format in the `obtained` structure.
473 *
474 * The resulting audio specs, varying depending on hardware and on what
475 * changes were allowed, will then be written back to `obtained`.
476 *
477 * If your application can only handle one specific data format, pass a zero
478 * for `allowed_changes` and let SDL transparently handle any differences.
479 *
480 * \param device a UTF-8 string reported by SDL_GetAudioDeviceName() or a
481 * driver-specific name as appropriate. NULL requests the most
482 * reasonable default device.
483 * \param iscapture non-zero to specify a device should be opened for
484 * recording, not playback
485 * \param desired an SDL_AudioSpec structure representing the desired output
486 * format
487 * \param obtained an SDL_AudioSpec structure filled in with the actual output
488 * format
489 * \param allowed_changes 0, or one or more flags OR'd together
490 * \returns a valid device ID that is > 0 on success or 0 on failure; call
491 * SDL_GetError() for more information.
492 *
493 * For compatibility with SDL 1.2, this will never return 1, since
494 * SDL reserves that ID for the legacy SDL_OpenAudio() function.
495 *
496 * \since This function is available since SDL 3.0.0.
497 *
498 * \sa SDL_CloseAudioDevice
499 * \sa SDL_GetAudioDeviceName
500 * \sa SDL_LockAudioDevice
501 * \sa SDL_PlayAudioDevice
502 * \sa SDL_PauseAudioDevice
503 * \sa SDL_UnlockAudioDevice
504 */
506 const char *device,
507 int iscapture,
508 const SDL_AudioSpec *desired,
509 SDL_AudioSpec *obtained,
510 int allowed_changes);
511
512
513
514/**
515 * \name Audio state
516 *
517 * Get the current audio state.
518 */
519/* @{ */
520typedef enum
521{
526
527/**
528 * Use this function to get the current audio state of an audio device.
529 *
530 * \param dev the ID of an audio device previously opened with
531 * SDL_OpenAudioDevice()
532 * \returns the SDL_AudioStatus of the specified audio device.
533 *
534 * \since This function is available since SDL 3.0.0.
535 *
536 * \sa SDL_PlayAudioDevice
537 * \sa SDL_PauseAudioDevice
538 */
540/* @} *//* Audio State */
541
542/**
543 * Use this function to play audio on a specified device.
544 *
545 * Newly-opened audio devices start in the paused state, so you must call this
546 * function after opening the specified audio device to start playing sound.
547 * This allows you to safely initialize data for your callback function after
548 * opening the audio device. Silence will be written to the audio device while
549 * paused, and the audio callback is guaranteed to not be called. Pausing one
550 * device does not prevent other unpaused devices from running their
551 * callbacks.
552 *
553 * \param dev a device opened by SDL_OpenAudioDevice()
554 * \returns 0 on success or a negative error code on failure; call
555 * SDL_GetError() for more information.
556 *
557 * \since This function is available since SDL 3.0.0.
558 *
559 * \sa SDL_LockAudioDevice
560 * \sa SDL_PauseAudioDevice
561 */
562extern DECLSPEC int SDLCALL SDL_PlayAudioDevice(SDL_AudioDeviceID dev);
563
564
565
566/**
567 * Use this function to pause audio playback on a specified device.
568 *
569 * This function pauses the audio callback processing for a given device.
570 * Silence will be written to the audio device while paused, and the audio
571 * callback is guaranteed to not be called. Pausing one device does not
572 * prevent other unpaused devices from running their callbacks.
573 *
574 * If you just need to protect a few variables from race conditions vs your
575 * callback, you shouldn't pause the audio device, as it will lead to dropouts
576 * in the audio playback. Instead, you should use SDL_LockAudioDevice().
577 *
578 * \param dev a device opened by SDL_OpenAudioDevice()
579 * \returns 0 on success or a negative error code on failure; call
580 * SDL_GetError() for more information.
581 *
582 * \since This function is available since SDL 3.0.0.
583 *
584 * \sa SDL_LockAudioDevice
585 * \sa SDL_PlayAudioDevice
586 */
587extern DECLSPEC int SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev);
588
589
590/**
591 * Load the audio data of a WAVE file into memory.
592 *
593 * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
594 * be valid pointers. The entire data portion of the file is then loaded into
595 * memory and decoded if necessary.
596 *
597 * If `freesrc` is non-zero, the data source gets automatically closed and
598 * freed before the function returns.
599 *
600 * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
601 * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
602 * A-law and mu-law (8 bits). Other formats are currently unsupported and
603 * cause an error.
604 *
605 * If this function succeeds, the pointer returned by it is equal to `spec`
606 * and the pointer to the audio data allocated by the function is written to
607 * `audio_buf` and its length in bytes to `audio_len`. The SDL_AudioSpec
608 * members `freq`, `channels`, and `format` are set to the values of the audio
609 * data in the buffer. The `samples` member is set to a sane default and all
610 * others are set to zero.
611 *
612 * It's necessary to use SDL_free() to free the audio data returned in
613 * `audio_buf` when it is no longer used.
614 *
615 * Because of the underspecification of the .WAV format, there are many
616 * problematic files in the wild that cause issues with strict decoders. To
617 * provide compatibility with these files, this decoder is lenient in regards
618 * to the truncation of the file, the fact chunk, and the size of the RIFF
619 * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
620 * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
621 * tune the behavior of the loading process.
622 *
623 * Any file that is invalid (due to truncation, corruption, or wrong values in
624 * the headers), too big, or unsupported causes an error. Additionally, any
625 * critical I/O error from the data source will terminate the loading process
626 * with an error. The function returns NULL on error and in all cases (with
627 * the exception of `src` being NULL), an appropriate error message will be
628 * set.
629 *
630 * It is required that the data source supports seeking.
631 *
632 * Example:
633 *
634 * ```c
635 * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
636 * ```
637 *
638 * Note that the SDL_LoadWAV macro does this same thing for you, but in a less
639 * messy way:
640 *
641 * ```c
642 * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
643 * ```
644 *
645 * \param src The data source for the WAVE data
646 * \param freesrc If non-zero, SDL will _always_ free the data source
647 * \param spec An SDL_AudioSpec that will be filled in with the wave file's
648 * format details
649 * \param audio_buf A pointer filled with the audio data, allocated by the
650 * function.
651 * \param audio_len A pointer filled with the length of the audio data buffer
652 * in bytes
653 * \returns This function, if successfully called, returns `spec`, which will
654 * be filled with the audio data format of the wave source data.
655 * `audio_buf` will be filled with a pointer to an allocated buffer
656 * containing the audio data, and `audio_len` is filled with the
657 * length of that audio buffer in bytes.
658 *
659 * This function returns NULL if the .WAV file cannot be opened, uses
660 * an unknown data format, or is corrupt; call SDL_GetError() for
661 * more information.
662 *
663 * When the application is done with the data returned in
664 * `audio_buf`, it should call SDL_free() to dispose of it.
665 *
666 * \since This function is available since SDL 3.0.0.
667 *
668 * \sa SDL_free
669 * \sa SDL_LoadWAV
670 */
671extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
672 int freesrc,
673 SDL_AudioSpec * spec,
674 Uint8 ** audio_buf,
675 Uint32 * audio_len);
676
677/**
678 * Loads a WAV from a file.
679 * Compatibility convenience function.
680 */
681#define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
682 SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
683
684
685/* SDL_AudioStream is an audio conversion interface.
686 - It can handle resampling data in chunks without generating
687 artifacts, when it doesn't have the complete buffer available.
688 - It can handle incoming data in any variable size.
689 - You push data as you have it, and pull it when you need it
690 - It can also function as a basic audio data queue even if you
691 just have sound that needs to pass from one place to another.
692 */
693struct SDL_AudioStream; /* this is opaque to the outside world. */
695
696/**
697 * Create a new audio stream.
698 *
699 * \param src_format The format of the source audio
700 * \param src_channels The number of channels of the source audio
701 * \param src_rate The sampling rate of the source audio
702 * \param dst_format The format of the desired audio output
703 * \param dst_channels The number of channels of the desired audio output
704 * \param dst_rate The sampling rate of the desired audio output
705 * \returns 0 on success, or -1 on error.
706 *
707 * \threadsafety It is safe to call this function from any thread.
708 *
709 * \since This function is available since SDL 3.0.0.
710 *
711 * \sa SDL_PutAudioStreamData
712 * \sa SDL_GetAudioStreamData
713 * \sa SDL_GetAudioStreamAvailable
714 * \sa SDL_FlushAudioStream
715 * \sa SDL_ClearAudioStream
716 * \sa SDL_ChangeAudioStreamOutput
717 * \sa SDL_DestroyAudioStream
718 */
719extern DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAudioStream(SDL_AudioFormat src_format,
720 int src_channels,
721 int src_rate,
722 SDL_AudioFormat dst_format,
723 int dst_channels,
724 int dst_rate);
725
726
727/**
728 * Query the current format of an audio stream.
729 *
730 * \param stream the SDL_AudioStream to query.
731 * \param src_format Where to store the input audio format; ignored if NULL.
732 * \param src_channels Where to store the input channel count; ignored if
733 * NULL.
734 * \param src_rate Where to store the input sample rate; ignored if NULL.
735 * \param dst_format Where to store the output audio format; ignored if NULL.
736 * \param dst_channels Where to store the output channel count; ignored if
737 * NULL.
738 * \param dst_rate Where to store the output sample rate; ignored if NULL.
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 */
746extern DECLSPEC int SDLCALL SDL_GetAudioStreamFormat(SDL_AudioStream *stream,
747 SDL_AudioFormat *src_format,
748 int *src_channels,
749 int *src_rate,
750 SDL_AudioFormat *dst_format,
751 int *dst_channels,
752 int *dst_rate);
753
754/**
755 * Change the input and output formats of an audio stream.
756 *
757 * Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData
758 * will reflect the new format, and future calls to SDL_PutAudioStreamData
759 * must provide data in the new input formats.
760 *
761 * \param src_format The format of the audio input
762 * \param src_channels The number of channels of the audio input
763 * \param src_rate The sampling rate of the audio input
764 * \param dst_format The format of the desired audio output
765 * \param dst_channels The number of channels of the desired audio output
766 * \param dst_rate The sampling rate of the desired audio output
767 * \returns 0 on success, or -1 on error.
768 *
769 * \threadsafety It is safe to call this function from any thread, as it holds
770 * a stream-specific mutex while running.
771 *
772 * \since This function is available since SDL 3.0.0.
773 *
774 * \sa SDL_GetAudioStreamFormat
775 * \sa SDL_PutAudioStreamData
776 * \sa SDL_GetAudioStreamData
777 * \sa SDL_GetAudioStreamAvailable
778 */
779extern DECLSPEC int SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream,
780 SDL_AudioFormat src_format,
781 int src_channels,
782 int src_rate,
783 SDL_AudioFormat dst_format,
784 int dst_channels,
785 int dst_rate);
786
787/**
788 * Add data to be converted/resampled to the stream.
789 *
790 * This data must match the format/channels/samplerate specified in the latest
791 * call to SDL_SetAudioStreamFormat, or the format specified when creating the
792 * stream if it hasn't been changed.
793 *
794 * Note that this call simply queues unconverted data for later. This is
795 * different than SDL2, where data was converted during the Put call and the
796 * Get call would just dequeue the previously-converted data.
797 *
798 * \param stream The stream the audio data is being added to
799 * \param buf A pointer to the audio data to add
800 * \param len The number of bytes to write to the stream
801 * \returns 0 on success or a negative error code on failure; call
802 * SDL_GetError() for more information.
803 *
804 * \since This function is available since SDL 3.0.0.
805 *
806 * \sa SDL_CreateAudioStream
807 * \sa SDL_GetAudioStreamData
808 * \sa SDL_GetAudioStreamAvailable
809 * \sa SDL_FlushAudioStream
810 * \sa SDL_ClearAudioStream
811 * \sa SDL_DestroyAudioStream
812 */
813extern DECLSPEC int SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
814
815/**
816 * Get converted/resampled data from the stream.
817 *
818 * The input/output data format/channels/samplerate is specified when creating
819 * the stream, and can be changed after creation by calling
820 * SDL_SetAudioStreamFormat.
821 *
822 * Note that any conversion and resampling necessary is done during this call,
823 * and SDL_PutAudioStreamData simply queues unconverted data for later. This
824 * is different than SDL2, where that work was done while inputting new data
825 * to the stream and requesting the output just copied the converted data.
826 *
827 * \param stream The stream the audio is being requested from
828 * \param buf A buffer to fill with audio data
829 * \param len The maximum number of bytes to fill
830 * \returns the number of bytes read from the stream, or -1 on error
831 *
832 * \since This function is available since SDL 3.0.0.
833 *
834 * \sa SDL_CreateAudioStream
835 * \sa SDL_PutAudioStreamData
836 * \sa SDL_GetAudioStreamAvailable
837 * \sa SDL_SetAudioStreamFormat
838 * \sa SDL_FlushAudioStream
839 * \sa SDL_ClearAudioStream
840 * \sa SDL_DestroyAudioStream
841 */
842extern DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
843
844/**
845 * Get the number of converted/resampled bytes available.
846 *
847 * The stream may be buffering data behind the scenes until it has enough to
848 * resample correctly, so this number might be lower than what you expect, or
849 * even be zero. Add more data or flush the stream if you need the data now.
850 *
851 * If the stream has so much data that it would overflow an int, the return
852 * value is clamped to a maximum value, but no queued data is lost; if there
853 * are gigabytes of data queued, the app might need to read some of it with
854 * SDL_GetAudioStreamData before this function's return value is no longer
855 * clamped.
856 *
857 * \param stream The audio stream to query
858 * \returns the number of converted/resampled bytes available.
859 *
860 * \since This function is available since SDL 3.0.0.
861 *
862 * \sa SDL_CreateAudioStream
863 * \sa SDL_PutAudioStreamData
864 * \sa SDL_GetAudioStreamData
865 * \sa SDL_FlushAudioStream
866 * \sa SDL_ClearAudioStream
867 * \sa SDL_DestroyAudioStream
868 */
869extern DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
870
871/**
872 * Tell the stream that you're done sending data, and anything being buffered
873 * should be converted/resampled and made available immediately.
874 *
875 * It is legal to add more data to a stream after flushing, but there will be
876 * audio gaps in the output. Generally this is intended to signal the end of
877 * input, so the complete output becomes available.
878 *
879 * \param stream The audio stream to flush
880 * \returns 0 on success or a negative error code on failure; call
881 * SDL_GetError() for more information.
882 *
883 * \since This function is available since SDL 3.0.0.
884 *
885 * \sa SDL_CreateAudioStream
886 * \sa SDL_PutAudioStreamData
887 * \sa SDL_GetAudioStreamData
888 * \sa SDL_GetAudioStreamAvailable
889 * \sa SDL_ClearAudioStream
890 * \sa SDL_DestroyAudioStream
891 */
892extern DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
893
894/**
895 * Clear any pending data in the stream without converting it
896 *
897 * \param stream The audio stream to clear
898 * \returns 0 on success or a negative error code on failure; call
899 * SDL_GetError() for more information.
900 *
901 * \since This function is available since SDL 3.0.0.
902 *
903 * \sa SDL_CreateAudioStream
904 * \sa SDL_PutAudioStreamData
905 * \sa SDL_GetAudioStreamData
906 * \sa SDL_GetAudioStreamAvailable
907 * \sa SDL_FlushAudioStream
908 * \sa SDL_DestroyAudioStream
909 */
910extern DECLSPEC int SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
911
912/**
913 * Free an audio stream
914 *
915 * \param stream The audio stream to free
916 *
917 * \since This function is available since SDL 3.0.0.
918 *
919 * \sa SDL_CreateAudioStream
920 * \sa SDL_PutAudioStreamData
921 * \sa SDL_GetAudioStreamData
922 * \sa SDL_GetAudioStreamAvailable
923 * \sa SDL_FlushAudioStream
924 * \sa SDL_ClearAudioStream
925 */
926extern DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
927
928#define SDL_MIX_MAXVOLUME 128
929
930/**
931 * Mix audio data in a specified format.
932 *
933 * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
934 * it into `dst`, performing addition, volume adjustment, and overflow
935 * clipping. The buffer pointed to by `dst` must also be `len` bytes of
936 * `format` data.
937 *
938 * This is provided for convenience -- you can mix your own audio data.
939 *
940 * Do not use this function for mixing together more than two streams of
941 * sample data. The output from repeated application of this function may be
942 * distorted by clipping, because there is no accumulator with greater range
943 * than the input (not to mention this being an inefficient way of doing it).
944 *
945 * It is a common misconception that this function is required to write audio
946 * data to an output stream in an audio callback. While you can do that,
947 * SDL_MixAudioFormat() is really only needed when you're mixing a single
948 * audio stream with a volume adjustment.
949 *
950 * \param dst the destination for the mixed audio
951 * \param src the source audio buffer to be mixed
952 * \param format the SDL_AudioFormat structure representing the desired audio
953 * format
954 * \param len the length of the audio buffer in bytes
955 * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
956 * for full audio volume
957 * \returns 0 on success or a negative error code on failure; call
958 * SDL_GetError() for more information.
959 *
960 * \since This function is available since SDL 3.0.0.
961 */
962extern DECLSPEC int SDLCALL SDL_MixAudioFormat(Uint8 * dst,
963 const Uint8 * src,
964 SDL_AudioFormat format,
965 Uint32 len, int volume);
966
967/**
968 * Queue more audio on non-callback devices.
969 *
970 * If you are looking to retrieve queued audio from a non-callback capture
971 * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
972 * -1 to signify an error if you use it with capture devices.
973 *
974 * SDL offers two ways to feed audio to the device: you can either supply a
975 * callback that SDL triggers with some frequency to obtain more audio (pull
976 * method), or you can supply no callback, and then SDL will expect you to
977 * supply data at regular intervals (push method) with this function.
978 *
979 * There are no limits on the amount of data you can queue, short of
980 * exhaustion of address space. Queued data will drain to the device as
981 * necessary without further intervention from you. If the device needs audio
982 * but there is not enough queued, it will play silence to make up the
983 * difference. This means you will have skips in your audio playback if you
984 * aren't routinely queueing sufficient data.
985 *
986 * This function copies the supplied data, so you are safe to free it when the
987 * function returns. This function is thread-safe, but queueing to the same
988 * device from two threads at once does not promise which buffer will be
989 * queued first.
990 *
991 * You may not queue audio on a device that is using an application-supplied
992 * callback; doing so returns an error. You have to use the audio callback or
993 * queue audio with this function, but not both.
994 *
995 * You should not call SDL_LockAudio() on the device before queueing; SDL
996 * handles locking internally for this function.
997 *
998 * Note that SDL does not support planar audio. You will need to resample from
999 * planar audio formats into a non-planar one (see SDL_AudioFormat) before
1000 * queuing audio.
1001 *
1002 * \param dev the device ID to which we will queue audio
1003 * \param data the data to queue to the device for later playback
1004 * \param len the number of bytes (not samples!) to which `data` points
1005 * \returns 0 on success or a negative error code on failure; call
1006 * SDL_GetError() for more information.
1007 *
1008 * \since This function is available since SDL 3.0.0.
1009 *
1010 * \sa SDL_ClearQueuedAudio
1011 * \sa SDL_GetQueuedAudioSize
1012 */
1013extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
1014
1015/**
1016 * Dequeue more audio on non-callback devices.
1017 *
1018 * If you are looking to queue audio for output on a non-callback playback
1019 * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
1020 * return 0 if you use it with playback devices.
1021 *
1022 * SDL offers two ways to retrieve audio from a capture device: you can either
1023 * supply a callback that SDL triggers with some frequency as the device
1024 * records more audio data, (push method), or you can supply no callback, and
1025 * then SDL will expect you to retrieve data at regular intervals (pull
1026 * method) with this function.
1027 *
1028 * There are no limits on the amount of data you can queue, short of
1029 * exhaustion of address space. Data from the device will keep queuing as
1030 * necessary without further intervention from you. This means you will
1031 * eventually run out of memory if you aren't routinely dequeueing data.
1032 *
1033 * Capture devices will not queue data when paused; if you are expecting to
1034 * not need captured audio for some length of time, use SDL_PauseAudioDevice()
1035 * to stop the capture device from queueing more data. This can be useful
1036 * during, say, level loading times. When unpaused, capture devices will start
1037 * queueing data from that point, having flushed any capturable data available
1038 * while paused.
1039 *
1040 * This function is thread-safe, but dequeueing from the same device from two
1041 * threads at once does not promise which thread will dequeue data first.
1042 *
1043 * You may not dequeue audio from a device that is using an
1044 * application-supplied callback; doing so returns an error. You have to use
1045 * the audio callback, or dequeue audio with this function, but not both.
1046 *
1047 * You should not call SDL_LockAudio() on the device before dequeueing; SDL
1048 * handles locking internally for this function.
1049 *
1050 * \param dev the device ID from which we will dequeue audio
1051 * \param data a pointer into where audio data should be copied
1052 * \param len the number of bytes (not samples!) to which (data) points
1053 * \returns the number of bytes dequeued, which could be less than requested;
1054 * call SDL_GetError() for more information.
1055 *
1056 * \since This function is available since SDL 3.0.0.
1057 *
1058 * \sa SDL_ClearQueuedAudio
1059 * \sa SDL_GetQueuedAudioSize
1060 */
1061extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
1062
1063/**
1064 * Get the number of bytes of still-queued audio.
1065 *
1066 * For playback devices: this is the number of bytes that have been queued for
1067 * playback with SDL_QueueAudio(), but have not yet been sent to the hardware.
1068 *
1069 * Once we've sent it to the hardware, this function can not decide the exact
1070 * byte boundary of what has been played. It's possible that we just gave the
1071 * hardware several kilobytes right before you called this function, but it
1072 * hasn't played any of it yet, or maybe half of it, etc.
1073 *
1074 * For capture devices, this is the number of bytes that have been captured by
1075 * the device and are waiting for you to dequeue. This number may grow at any
1076 * time, so this only informs of the lower-bound of available data.
1077 *
1078 * You may not queue or dequeue audio on a device that is using an
1079 * application-supplied callback; calling this function on such a device
1080 * always returns 0. You have to use the audio callback or queue audio, but
1081 * not both.
1082 *
1083 * You should not call SDL_LockAudio() on the device before querying; SDL
1084 * handles locking internally for this function.
1085 *
1086 * \param dev the device ID of which we will query queued audio size
1087 * \returns the number of bytes (not samples!) of queued audio.
1088 *
1089 * \since This function is available since SDL 3.0.0.
1090 *
1091 * \sa SDL_ClearQueuedAudio
1092 * \sa SDL_QueueAudio
1093 * \sa SDL_DequeueAudio
1094 */
1096
1097/**
1098 * Drop any queued audio data waiting to be sent to the hardware.
1099 *
1100 * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
1101 * output devices, the hardware will start playing silence if more audio isn't
1102 * queued. For capture devices, the hardware will start filling the empty
1103 * queue with new data if the capture device isn't paused.
1104 *
1105 * This will not prevent playback of queued audio that's already been sent to
1106 * the hardware, as we can not undo that, so expect there to be some fraction
1107 * of a second of audio that might still be heard. This can be useful if you
1108 * want to, say, drop any pending music or any unprocessed microphone input
1109 * during a level change in your game.
1110 *
1111 * You may not queue or dequeue audio on a device that is using an
1112 * application-supplied callback; calling this function on such a device
1113 * always returns 0. You have to use the audio callback or queue audio, but
1114 * not both.
1115 *
1116 * You should not call SDL_LockAudio() on the device before clearing the
1117 * queue; SDL handles locking internally for this function.
1118 *
1119 * This function always succeeds and thus returns void.
1120 *
1121 * \param dev the device ID of which to clear the audio queue
1122 * \returns 0 on success or a negative error code on failure; call
1123 * SDL_GetError() for more information.
1124 *
1125 * \since This function is available since SDL 3.0.0.
1126 *
1127 * \sa SDL_GetQueuedAudioSize
1128 * \sa SDL_QueueAudio
1129 * \sa SDL_DequeueAudio
1130 */
1131extern DECLSPEC int SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
1132
1133
1134/**
1135 * \name Audio lock functions
1136 *
1137 * The lock manipulated by these functions protects the callback function.
1138 * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
1139 * the callback function is not running. Do not call these from the callback
1140 * function or you will cause deadlock.
1141 */
1142/* @{ */
1143
1144/**
1145 * Use this function to lock out the audio callback function for a specified
1146 * device.
1147 *
1148 * The lock manipulated by these functions protects the audio callback
1149 * function specified in SDL_OpenAudioDevice(). During a
1150 * SDL_LockAudioDevice()/SDL_UnlockAudioDevice() pair, you can be guaranteed
1151 * that the callback function for that device is not running, even if the
1152 * device is not paused. While a device is locked, any other unpaused,
1153 * unlocked devices may still run their callbacks.
1154 *
1155 * Calling this function from inside your audio callback is unnecessary. SDL
1156 * obtains this lock before calling your function, and releases it when the
1157 * function returns.
1158 *
1159 * You should not hold the lock longer than absolutely necessary. If you hold
1160 * it too long, you'll experience dropouts in your audio playback. Ideally,
1161 * your application locks the device, sets a few variables and unlocks again.
1162 * Do not do heavy work while holding the lock for a device.
1163 *
1164 * It is safe to lock the audio device multiple times, as long as you unlock
1165 * it an equivalent number of times. The callback will not run until the
1166 * device has been unlocked completely in this way. If your application fails
1167 * to unlock the device appropriately, your callback will never run, you might
1168 * hear repeating bursts of audio, and SDL_CloseAudioDevice() will probably
1169 * deadlock.
1170 *
1171 * Internally, the audio device lock is a mutex; if you lock from two threads
1172 * at once, not only will you block the audio callback, you'll block the other
1173 * thread.
1174 *
1175 * \param dev the ID of the device to be locked
1176 * \returns 0 on success or a negative error code on failure; call
1177 * SDL_GetError() for more information.
1178 *
1179 * \since This function is available since SDL 3.0.0.
1180 *
1181 * \sa SDL_UnlockAudioDevice
1182 */
1183extern DECLSPEC int SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
1184
1185/**
1186 * Use this function to unlock the audio callback function for a specified
1187 * device.
1188 *
1189 * This function should be paired with a previous SDL_LockAudioDevice() call.
1190 *
1191 * \param dev the ID of the device to be unlocked
1192 *
1193 * \since This function is available since SDL 3.0.0.
1194 *
1195 * \sa SDL_LockAudioDevice
1196 */
1197extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
1198/* @} *//* Audio lock functions */
1199
1200/**
1201 * Use this function to shut down audio processing and close the audio device.
1202 *
1203 * The application should close open audio devices once they are no longer
1204 * needed. Calling this function will wait until the device's audio callback
1205 * is not running, release the audio hardware and then clean up internal
1206 * state. No further audio will play from this device once this function
1207 * returns.
1208 *
1209 * This function may block briefly while pending audio data is played by the
1210 * hardware, so that applications don't drop the last buffer of data they
1211 * supplied.
1212 *
1213 * The device ID is invalid as soon as the device is closed, and is eligible
1214 * for reuse in a new SDL_OpenAudioDevice() call immediately.
1215 *
1216 * \param dev an audio device previously opened with SDL_OpenAudioDevice()
1217 *
1218 * \since This function is available since SDL 3.0.0.
1219 *
1220 * \sa SDL_OpenAudioDevice
1221 */
1222extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
1223
1224/**
1225 * Convert some audio data of one format to another format.
1226 *
1227 * Please note that this function is for convenience, but should not be used
1228 * to resample audio in blocks, as it will introduce audio artifacts on the
1229 * boundaries. You should only use this function if you are converting audio
1230 * data in its entirety in one call. If you want to convert audio in smaller
1231 * chunks, use an SDL_AudioStream, which is designed for this situation.
1232 *
1233 * Internally, this function creates and destroys an SDL_AudioStream on each
1234 * use, so it's also less efficient than using one directly, if you need to
1235 * convert multiple times.
1236 *
1237 * \param src_format The format of the source audio
1238 * \param src_channels The number of channels of the source audio
1239 * \param src_rate The sampling rate of the source audio
1240 * \param src_data The audio data to be converted
1241 * \param src_len The len of src_data
1242 * \param dst_format The format of the desired audio output
1243 * \param dst_channels The number of channels of the desired audio output
1244 * \param dst_rate The sampling rate of the desired audio output
1245 * \param dst_data Will be filled with a pointer to converted audio data,
1246 * which should be freed with SDL_free(). On error, it will be
1247 * NULL.
1248 * \param dst_len Will be filled with the len of dst_data
1249 * \returns 0 on success or a negative error code on failure; call
1250 * SDL_GetError() for more information.
1251 *
1252 * \since This function is available since SDL 3.0.0.
1253 *
1254 * \sa SDL_CreateAudioStream
1255 */
1256extern DECLSPEC int SDLCALL SDL_ConvertAudioSamples(SDL_AudioFormat src_format,
1257 Uint8 src_channels,
1258 int src_rate,
1259 const Uint8 *src_data,
1260 int src_len,
1261 SDL_AudioFormat dst_format,
1262 Uint8 dst_channels,
1263 int dst_rate,
1264 Uint8 **dst_data,
1265 int *dst_len);
1266
1267/* Ends C function definitions when using C++ */
1268#ifdef __cplusplus
1269}
1270#endif
1271#include <SDL3/SDL_close_code.h>
1272
1273#endif /* SDL_audio_h_ */
void SDL_CloseAudioDevice(SDL_AudioDeviceID dev)
SDL_AudioSpec * SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
SDL_AudioDeviceID SDL_OpenAudioDevice(const char *device, int iscapture, const SDL_AudioSpec *desired, SDL_AudioSpec *obtained, int allowed_changes)
int SDL_GetNumAudioDevices(int iscapture)
int SDL_SetAudioStreamFormat(SDL_AudioStream *stream, SDL_AudioFormat src_format, int src_channels, int src_rate, SDL_AudioFormat dst_format, int dst_channels, int dst_rate)
const char * SDL_GetAudioDriver(int index)
SDL_AudioStatus
Definition: SDL_audio.h:521
@ SDL_AUDIO_STOPPED
Definition: SDL_audio.h:522
@ SDL_AUDIO_PLAYING
Definition: SDL_audio.h:523
@ SDL_AUDIO_PAUSED
Definition: SDL_audio.h:524
int SDL_PlayAudioDevice(SDL_AudioDeviceID dev)
struct SDL_AudioStream SDL_AudioStream
Definition: SDL_audio.h:694
const char * SDL_GetAudioDeviceName(int index, int iscapture)
Uint16 SDL_AudioFormat
Audio format flags.
Definition: SDL_audio.h:66
int SDL_FlushAudioStream(SDL_AudioStream *stream)
Uint32 SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len)
int SDL_GetNumAudioDrivers(void)
Uint32 SDL_AudioDeviceID
Definition: SDL_audio.h:262
const char * SDL_GetCurrentAudioDriver(void)
int SDL_GetAudioStreamFormat(SDL_AudioStream *stream, SDL_AudioFormat *src_format, int *src_channels, int *src_rate, SDL_AudioFormat *dst_format, int *dst_channels, int *dst_rate)
int SDL_PauseAudioDevice(SDL_AudioDeviceID dev)
int SDL_GetAudioStreamAvailable(SDL_AudioStream *stream)
int SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len)
int SDL_ConvertAudioSamples(SDL_AudioFormat src_format, Uint8 src_channels, int src_rate, const Uint8 *src_data, int src_len, SDL_AudioFormat dst_format, Uint8 dst_channels, int dst_rate, Uint8 **dst_data, int *dst_len)
int SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len)
int SDL_GetDefaultAudioInfo(char **name, SDL_AudioSpec *spec, int iscapture)
void SDL_DestroyAudioStream(SDL_AudioStream *stream)
SDL_AudioStream * SDL_CreateAudioStream(SDL_AudioFormat src_format, int src_channels, int src_rate, SDL_AudioFormat dst_format, int dst_channels, int dst_rate)
int SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len)
void(* SDL_AudioCallback)(void *userdata, Uint8 *stream, int len)
Definition: SDL_audio.h:160
Uint32 SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev)
int SDL_ClearAudioStream(SDL_AudioStream *stream)
int SDL_ClearQueuedAudio(SDL_AudioDeviceID dev)
int SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, int volume)
int SDL_GetAudioDeviceSpec(int index, int iscapture, SDL_AudioSpec *spec)
SDL_AudioStatus SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev)
int SDL_LockAudioDevice(SDL_AudioDeviceID dev)
void SDL_UnlockAudioDevice(SDL_AudioDeviceID dev)
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
uint32_t Uint32
Definition: SDL_stdinc.h:171
Header for the SDL thread management routines.
Uint32 size
Definition: SDL_audio.h:183
SDL_AudioCallback callback
Definition: SDL_audio.h:184
Uint16 samples
Definition: SDL_audio.h:181
Uint8 channels
Definition: SDL_audio.h:179
Uint16 padding
Definition: SDL_audio.h:182
Uint8 silence
Definition: SDL_audio.h:180
SDL_AudioFormat format
Definition: SDL_audio.h:178
void * userdata
Definition: SDL_audio.h:185