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 * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
598 * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
599 * A-law and mu-law (8 bits). Other formats are currently unsupported and
600 * cause an error.
601 *
602 * If this function succeeds, the pointer returned by it is equal to `spec`
603 * and the pointer to the audio data allocated by the function is written to
604 * `audio_buf` and its length in bytes to `audio_len`. The SDL_AudioSpec
605 * members `freq`, `channels`, and `format` are set to the values of the audio
606 * data in the buffer. The `samples` member is set to a sane default and all
607 * others are set to zero.
608 *
609 * It's necessary to use SDL_free() to free the audio data returned in
610 * `audio_buf` when it is no longer used.
611 *
612 * Because of the underspecification of the .WAV format, there are many
613 * problematic files in the wild that cause issues with strict decoders. To
614 * provide compatibility with these files, this decoder is lenient in regards
615 * to the truncation of the file, the fact chunk, and the size of the RIFF
616 * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
617 * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
618 * tune the behavior of the loading process.
619 *
620 * Any file that is invalid (due to truncation, corruption, or wrong values in
621 * the headers), too big, or unsupported causes an error. Additionally, any
622 * critical I/O error from the data source will terminate the loading process
623 * with an error. The function returns NULL on error and in all cases (with
624 * the exception of `src` being NULL), an appropriate error message will be
625 * set.
626 *
627 * It is required that the data source supports seeking.
628 *
629 * Example:
630 *
631 * ```c
632 * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
633 * ```
634 *
635 * Note that the SDL_LoadWAV macro does this same thing for you, but in a less
636 * messy way:
637 *
638 * ```c
639 * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
640 * ```
641 *
642 * \param src The data source for the WAVE data
643 * \param freesrc if SDL_TRUE, calls SDL_RWclose() on `src` before returning,
644 * even in the case of an error
645 * \param spec An SDL_AudioSpec that will be filled in with the wave file's
646 * format details
647 * \param audio_buf A pointer filled with the audio data, allocated by the
648 * function
649 * \param audio_len A pointer filled with the length of the audio data buffer
650 * in bytes
651 * \returns This function, if successfully called, returns `spec`, which will
652 * be filled with the audio data format of the wave source data.
653 * `audio_buf` will be filled with a pointer to an allocated buffer
654 * containing the audio data, and `audio_len` is filled with the
655 * length of that audio buffer in bytes.
656 *
657 * This function returns NULL if the .WAV file cannot be opened, uses
658 * an unknown data format, or is corrupt; call SDL_GetError() for
659 * more information.
660 *
661 * When the application is done with the data returned in
662 * `audio_buf`, it should call SDL_free() to dispose of it.
663 *
664 * \since This function is available since SDL 3.0.0.
665 *
666 * \sa SDL_free
667 * \sa SDL_LoadWAV
668 */
669extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
670 SDL_bool freesrc,
671 SDL_AudioSpec * spec,
672 Uint8 ** audio_buf,
673 Uint32 * audio_len);
674
675/**
676 * Loads a WAV from a file.
677 * Compatibility convenience function.
678 */
679#define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
680 SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
681
682
683/* SDL_AudioStream is an audio conversion interface.
684 - It can handle resampling data in chunks without generating
685 artifacts, when it doesn't have the complete buffer available.
686 - It can handle incoming data in any variable size.
687 - You push data as you have it, and pull it when you need it
688 - It can also function as a basic audio data queue even if you
689 just have sound that needs to pass from one place to another.
690 */
691struct SDL_AudioStream; /* this is opaque to the outside world. */
693
694/**
695 * Create a new audio stream.
696 *
697 * \param src_format The format of the source audio
698 * \param src_channels The number of channels of the source audio
699 * \param src_rate The sampling rate of the source audio
700 * \param dst_format The format of the desired audio output
701 * \param dst_channels The number of channels of the desired audio output
702 * \param dst_rate The sampling rate of the desired audio output
703 * \returns 0 on success, or -1 on error.
704 *
705 * \threadsafety It is safe to call this function from any thread.
706 *
707 * \since This function is available since SDL 3.0.0.
708 *
709 * \sa SDL_PutAudioStreamData
710 * \sa SDL_GetAudioStreamData
711 * \sa SDL_GetAudioStreamAvailable
712 * \sa SDL_FlushAudioStream
713 * \sa SDL_ClearAudioStream
714 * \sa SDL_ChangeAudioStreamOutput
715 * \sa SDL_DestroyAudioStream
716 */
717extern DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAudioStream(SDL_AudioFormat src_format,
718 int src_channels,
719 int src_rate,
720 SDL_AudioFormat dst_format,
721 int dst_channels,
722 int dst_rate);
723
724
725/**
726 * Query the current format of an audio stream.
727 *
728 * \param stream the SDL_AudioStream to query.
729 * \param src_format Where to store the input audio format; ignored if NULL.
730 * \param src_channels Where to store the input channel count; ignored if
731 * NULL.
732 * \param src_rate Where to store the input sample rate; ignored if NULL.
733 * \param dst_format Where to store the output audio format; ignored if NULL.
734 * \param dst_channels Where to store the output channel count; ignored if
735 * NULL.
736 * \param dst_rate Where to store the output sample rate; ignored if NULL.
737 * \returns 0 on success, or -1 on error.
738 *
739 * \threadsafety It is safe to call this function from any thread, as it holds
740 * a stream-specific mutex while running.
741 *
742 * \since This function is available since SDL 3.0.0.
743 */
744extern DECLSPEC int SDLCALL SDL_GetAudioStreamFormat(SDL_AudioStream *stream,
745 SDL_AudioFormat *src_format,
746 int *src_channels,
747 int *src_rate,
748 SDL_AudioFormat *dst_format,
749 int *dst_channels,
750 int *dst_rate);
751
752/**
753 * Change the input and output formats of an audio stream.
754 *
755 * Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData
756 * will reflect the new format, and future calls to SDL_PutAudioStreamData
757 * must provide data in the new input formats.
758 *
759 * \param stream The stream the format is being changed
760 * \param src_format The format of the audio input
761 * \param src_channels The number of channels of the audio input
762 * \param src_rate The sampling rate of the audio input
763 * \param dst_format The format of the desired audio output
764 * \param dst_channels The number of channels of the desired audio output
765 * \param dst_rate The sampling rate of the desired audio output
766 * \returns 0 on success, or -1 on error.
767 *
768 * \threadsafety It is safe to call this function from any thread, as it holds
769 * a stream-specific mutex while running.
770 *
771 * \since This function is available since SDL 3.0.0.
772 *
773 * \sa SDL_GetAudioStreamFormat
774 * \sa SDL_PutAudioStreamData
775 * \sa SDL_GetAudioStreamData
776 * \sa SDL_GetAudioStreamAvailable
777 */
778extern DECLSPEC int SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream,
779 SDL_AudioFormat src_format,
780 int src_channels,
781 int src_rate,
782 SDL_AudioFormat dst_format,
783 int dst_channels,
784 int dst_rate);
785
786/**
787 * Add data to be converted/resampled to the stream.
788 *
789 * This data must match the format/channels/samplerate specified in the latest
790 * call to SDL_SetAudioStreamFormat, or the format specified when creating the
791 * stream if it hasn't been changed.
792 *
793 * Note that this call simply queues unconverted data for later. This is
794 * different than SDL2, where data was converted during the Put call and the
795 * Get call would just dequeue the previously-converted data.
796 *
797 * \param stream The stream the audio data is being added to
798 * \param buf A pointer to the audio data to add
799 * \param len The number of bytes to write to the stream
800 * \returns 0 on success or a negative error code on failure; call
801 * SDL_GetError() for more information.
802 *
803 * \since This function is available since SDL 3.0.0.
804 *
805 * \sa SDL_CreateAudioStream
806 * \sa SDL_GetAudioStreamData
807 * \sa SDL_GetAudioStreamAvailable
808 * \sa SDL_FlushAudioStream
809 * \sa SDL_ClearAudioStream
810 * \sa SDL_DestroyAudioStream
811 */
812extern DECLSPEC int SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
813
814/**
815 * Get converted/resampled data from the stream.
816 *
817 * The input/output data format/channels/samplerate is specified when creating
818 * the stream, and can be changed after creation by calling
819 * SDL_SetAudioStreamFormat.
820 *
821 * Note that any conversion and resampling necessary is done during this call,
822 * and SDL_PutAudioStreamData simply queues unconverted data for later. This
823 * is different than SDL2, where that work was done while inputting new data
824 * to the stream and requesting the output just copied the converted data.
825 *
826 * \param stream The stream the audio is being requested from
827 * \param buf A buffer to fill with audio data
828 * \param len The maximum number of bytes to fill
829 * \returns the number of bytes read from the stream, or -1 on error
830 *
831 * \since This function is available since SDL 3.0.0.
832 *
833 * \sa SDL_CreateAudioStream
834 * \sa SDL_PutAudioStreamData
835 * \sa SDL_GetAudioStreamAvailable
836 * \sa SDL_SetAudioStreamFormat
837 * \sa SDL_FlushAudioStream
838 * \sa SDL_ClearAudioStream
839 * \sa SDL_DestroyAudioStream
840 */
841extern DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
842
843/**
844 * Get the number of converted/resampled bytes available.
845 *
846 * The stream may be buffering data behind the scenes until it has enough to
847 * resample correctly, so this number might be lower than what you expect, or
848 * even be zero. Add more data or flush the stream if you need the data now.
849 *
850 * If the stream has so much data that it would overflow an int, the return
851 * value is clamped to a maximum value, but no queued data is lost; if there
852 * are gigabytes of data queued, the app might need to read some of it with
853 * SDL_GetAudioStreamData before this function's return value is no longer
854 * clamped.
855 *
856 * \param stream The audio stream to query
857 * \returns the number of converted/resampled bytes available.
858 *
859 * \since This function is available since SDL 3.0.0.
860 *
861 * \sa SDL_CreateAudioStream
862 * \sa SDL_PutAudioStreamData
863 * \sa SDL_GetAudioStreamData
864 * \sa SDL_FlushAudioStream
865 * \sa SDL_ClearAudioStream
866 * \sa SDL_DestroyAudioStream
867 */
868extern DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
869
870/**
871 * Tell the stream that you're done sending data, and anything being buffered
872 * should be converted/resampled and made available immediately.
873 *
874 * It is legal to add more data to a stream after flushing, but there will be
875 * audio gaps in the output. Generally this is intended to signal the end of
876 * input, so the complete output becomes available.
877 *
878 * \param stream The audio stream to flush
879 * \returns 0 on success or a negative error code on failure; call
880 * SDL_GetError() for more information.
881 *
882 * \since This function is available since SDL 3.0.0.
883 *
884 * \sa SDL_CreateAudioStream
885 * \sa SDL_PutAudioStreamData
886 * \sa SDL_GetAudioStreamData
887 * \sa SDL_GetAudioStreamAvailable
888 * \sa SDL_ClearAudioStream
889 * \sa SDL_DestroyAudioStream
890 */
891extern DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
892
893/**
894 * Clear any pending data in the stream without converting it
895 *
896 * \param stream The audio stream to clear
897 * \returns 0 on success or a negative error code on failure; call
898 * SDL_GetError() for more information.
899 *
900 * \since This function is available since SDL 3.0.0.
901 *
902 * \sa SDL_CreateAudioStream
903 * \sa SDL_PutAudioStreamData
904 * \sa SDL_GetAudioStreamData
905 * \sa SDL_GetAudioStreamAvailable
906 * \sa SDL_FlushAudioStream
907 * \sa SDL_DestroyAudioStream
908 */
909extern DECLSPEC int SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
910
911/**
912 * Free an audio stream
913 *
914 * \param stream The audio stream to free
915 *
916 * \since This function is available since SDL 3.0.0.
917 *
918 * \sa SDL_CreateAudioStream
919 * \sa SDL_PutAudioStreamData
920 * \sa SDL_GetAudioStreamData
921 * \sa SDL_GetAudioStreamAvailable
922 * \sa SDL_FlushAudioStream
923 * \sa SDL_ClearAudioStream
924 */
925extern DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
926
927#define SDL_MIX_MAXVOLUME 128
928
929/**
930 * Mix audio data in a specified format.
931 *
932 * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
933 * it into `dst`, performing addition, volume adjustment, and overflow
934 * clipping. The buffer pointed to by `dst` must also be `len` bytes of
935 * `format` data.
936 *
937 * This is provided for convenience -- you can mix your own audio data.
938 *
939 * Do not use this function for mixing together more than two streams of
940 * sample data. The output from repeated application of this function may be
941 * distorted by clipping, because there is no accumulator with greater range
942 * than the input (not to mention this being an inefficient way of doing it).
943 *
944 * It is a common misconception that this function is required to write audio
945 * data to an output stream in an audio callback. While you can do that,
946 * SDL_MixAudioFormat() is really only needed when you're mixing a single
947 * audio stream with a volume adjustment.
948 *
949 * \param dst the destination for the mixed audio
950 * \param src the source audio buffer to be mixed
951 * \param format the SDL_AudioFormat structure representing the desired audio
952 * format
953 * \param len the length of the audio buffer in bytes
954 * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
955 * for full audio volume
956 * \returns 0 on success or a negative error code on failure; call
957 * SDL_GetError() for more information.
958 *
959 * \since This function is available since SDL 3.0.0.
960 */
961extern DECLSPEC int SDLCALL SDL_MixAudioFormat(Uint8 * dst,
962 const Uint8 * src,
963 SDL_AudioFormat format,
964 Uint32 len, int volume);
965
966/**
967 * Queue more audio on non-callback devices.
968 *
969 * If you are looking to retrieve queued audio from a non-callback capture
970 * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
971 * -1 to signify an error if you use it with capture devices.
972 *
973 * SDL offers two ways to feed audio to the device: you can either supply a
974 * callback that SDL triggers with some frequency to obtain more audio (pull
975 * method), or you can supply no callback, and then SDL will expect you to
976 * supply data at regular intervals (push method) with this function.
977 *
978 * There are no limits on the amount of data you can queue, short of
979 * exhaustion of address space. Queued data will drain to the device as
980 * necessary without further intervention from you. If the device needs audio
981 * but there is not enough queued, it will play silence to make up the
982 * difference. This means you will have skips in your audio playback if you
983 * aren't routinely queueing sufficient data.
984 *
985 * This function copies the supplied data, so you are safe to free it when the
986 * function returns. This function is thread-safe, but queueing to the same
987 * device from two threads at once does not promise which buffer will be
988 * queued first.
989 *
990 * You may not queue audio on a device that is using an application-supplied
991 * callback; doing so returns an error. You have to use the audio callback or
992 * queue audio with this function, but not both.
993 *
994 * You should not call SDL_LockAudio() on the device before queueing; SDL
995 * handles locking internally for this function.
996 *
997 * Note that SDL does not support planar audio. You will need to resample from
998 * planar audio formats into a non-planar one (see SDL_AudioFormat) before
999 * queuing audio.
1000 *
1001 * \param dev the device ID to which we will queue audio
1002 * \param data the data to queue to the device for later playback
1003 * \param len the number of bytes (not samples!) to which `data` points
1004 * \returns 0 on success or a negative error code on failure; call
1005 * SDL_GetError() for more information.
1006 *
1007 * \since This function is available since SDL 3.0.0.
1008 *
1009 * \sa SDL_ClearQueuedAudio
1010 * \sa SDL_GetQueuedAudioSize
1011 */
1012extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
1013
1014/**
1015 * Dequeue more audio on non-callback devices.
1016 *
1017 * If you are looking to queue audio for output on a non-callback playback
1018 * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
1019 * return 0 if you use it with playback devices.
1020 *
1021 * SDL offers two ways to retrieve audio from a capture device: you can either
1022 * supply a callback that SDL triggers with some frequency as the device
1023 * records more audio data, (push method), or you can supply no callback, and
1024 * then SDL will expect you to retrieve data at regular intervals (pull
1025 * method) with this function.
1026 *
1027 * There are no limits on the amount of data you can queue, short of
1028 * exhaustion of address space. Data from the device will keep queuing as
1029 * necessary without further intervention from you. This means you will
1030 * eventually run out of memory if you aren't routinely dequeueing data.
1031 *
1032 * Capture devices will not queue data when paused; if you are expecting to
1033 * not need captured audio for some length of time, use SDL_PauseAudioDevice()
1034 * to stop the capture device from queueing more data. This can be useful
1035 * during, say, level loading times. When unpaused, capture devices will start
1036 * queueing data from that point, having flushed any capturable data available
1037 * while paused.
1038 *
1039 * This function is thread-safe, but dequeueing from the same device from two
1040 * threads at once does not promise which thread will dequeue data first.
1041 *
1042 * You may not dequeue audio from a device that is using an
1043 * application-supplied callback; doing so returns an error. You have to use
1044 * the audio callback, or dequeue audio with this function, but not both.
1045 *
1046 * You should not call SDL_LockAudio() on the device before dequeueing; SDL
1047 * handles locking internally for this function.
1048 *
1049 * \param dev the device ID from which we will dequeue audio
1050 * \param data a pointer into where audio data should be copied
1051 * \param len the number of bytes (not samples!) to which (data) points
1052 * \returns the number of bytes dequeued, which could be less than requested;
1053 * call SDL_GetError() for more information.
1054 *
1055 * \since This function is available since SDL 3.0.0.
1056 *
1057 * \sa SDL_ClearQueuedAudio
1058 * \sa SDL_GetQueuedAudioSize
1059 */
1060extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
1061
1062/**
1063 * Get the number of bytes of still-queued audio.
1064 *
1065 * For playback devices: this is the number of bytes that have been queued for
1066 * playback with SDL_QueueAudio(), but have not yet been sent to the hardware.
1067 *
1068 * Once we've sent it to the hardware, this function can not decide the exact
1069 * byte boundary of what has been played. It's possible that we just gave the
1070 * hardware several kilobytes right before you called this function, but it
1071 * hasn't played any of it yet, or maybe half of it, etc.
1072 *
1073 * For capture devices, this is the number of bytes that have been captured by
1074 * the device and are waiting for you to dequeue. This number may grow at any
1075 * time, so this only informs of the lower-bound of available data.
1076 *
1077 * You may not queue or dequeue audio on a device that is using an
1078 * application-supplied callback; calling this function on such a device
1079 * always returns 0. You have to use the audio callback or queue audio, but
1080 * not both.
1081 *
1082 * You should not call SDL_LockAudio() on the device before querying; SDL
1083 * handles locking internally for this function.
1084 *
1085 * \param dev the device ID of which we will query queued audio size
1086 * \returns the number of bytes (not samples!) of queued audio.
1087 *
1088 * \since This function is available since SDL 3.0.0.
1089 *
1090 * \sa SDL_ClearQueuedAudio
1091 * \sa SDL_QueueAudio
1092 * \sa SDL_DequeueAudio
1093 */
1095
1096/**
1097 * Drop any queued audio data waiting to be sent to the hardware.
1098 *
1099 * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
1100 * output devices, the hardware will start playing silence if more audio isn't
1101 * queued. For capture devices, the hardware will start filling the empty
1102 * queue with new data if the capture device isn't paused.
1103 *
1104 * This will not prevent playback of queued audio that's already been sent to
1105 * the hardware, as we can not undo that, so expect there to be some fraction
1106 * of a second of audio that might still be heard. This can be useful if you
1107 * want to, say, drop any pending music or any unprocessed microphone input
1108 * during a level change in your game.
1109 *
1110 * You may not queue or dequeue audio on a device that is using an
1111 * application-supplied callback; calling this function on such a device
1112 * always returns 0. You have to use the audio callback or queue audio, but
1113 * not both.
1114 *
1115 * You should not call SDL_LockAudio() on the device before clearing the
1116 * queue; SDL handles locking internally for this function.
1117 *
1118 * This function always succeeds and thus returns void.
1119 *
1120 * \param dev the device ID of which to clear the audio queue
1121 * \returns 0 on success or a negative error code on failure; call
1122 * SDL_GetError() for more information.
1123 *
1124 * \since This function is available since SDL 3.0.0.
1125 *
1126 * \sa SDL_GetQueuedAudioSize
1127 * \sa SDL_QueueAudio
1128 * \sa SDL_DequeueAudio
1129 */
1130extern DECLSPEC int SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
1131
1132
1133/**
1134 * \name Audio lock functions
1135 *
1136 * The lock manipulated by these functions protects the callback function.
1137 * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
1138 * the callback function is not running. Do not call these from the callback
1139 * function or you will cause deadlock.
1140 */
1141/* @{ */
1142
1143/**
1144 * Use this function to lock out the audio callback function for a specified
1145 * device.
1146 *
1147 * The lock manipulated by these functions protects the audio callback
1148 * function specified in SDL_OpenAudioDevice(). During a
1149 * SDL_LockAudioDevice()/SDL_UnlockAudioDevice() pair, you can be guaranteed
1150 * that the callback function for that device is not running, even if the
1151 * device is not paused. While a device is locked, any other unpaused,
1152 * unlocked devices may still run their callbacks.
1153 *
1154 * Calling this function from inside your audio callback is unnecessary. SDL
1155 * obtains this lock before calling your function, and releases it when the
1156 * function returns.
1157 *
1158 * You should not hold the lock longer than absolutely necessary. If you hold
1159 * it too long, you'll experience dropouts in your audio playback. Ideally,
1160 * your application locks the device, sets a few variables and unlocks again.
1161 * Do not do heavy work while holding the lock for a device.
1162 *
1163 * It is safe to lock the audio device multiple times, as long as you unlock
1164 * it an equivalent number of times. The callback will not run until the
1165 * device has been unlocked completely in this way. If your application fails
1166 * to unlock the device appropriately, your callback will never run, you might
1167 * hear repeating bursts of audio, and SDL_CloseAudioDevice() will probably
1168 * deadlock.
1169 *
1170 * Internally, the audio device lock is a mutex; if you lock from two threads
1171 * at once, not only will you block the audio callback, you'll block the other
1172 * thread.
1173 *
1174 * \param dev the ID of the device to be locked
1175 * \returns 0 on success or a negative error code on failure; call
1176 * SDL_GetError() for more information.
1177 *
1178 * \since This function is available since SDL 3.0.0.
1179 *
1180 * \sa SDL_UnlockAudioDevice
1181 */
1182extern DECLSPEC int SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
1183
1184/**
1185 * Use this function to unlock the audio callback function for a specified
1186 * device.
1187 *
1188 * This function should be paired with a previous SDL_LockAudioDevice() call.
1189 *
1190 * \param dev the ID of the device to be unlocked
1191 *
1192 * \since This function is available since SDL 3.0.0.
1193 *
1194 * \sa SDL_LockAudioDevice
1195 */
1196extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
1197/* @} *//* Audio lock functions */
1198
1199/**
1200 * Use this function to shut down audio processing and close the audio device.
1201 *
1202 * The application should close open audio devices once they are no longer
1203 * needed. Calling this function will wait until the device's audio callback
1204 * is not running, release the audio hardware and then clean up internal
1205 * state. No further audio will play from this device once this function
1206 * returns.
1207 *
1208 * This function may block briefly while pending audio data is played by the
1209 * hardware, so that applications don't drop the last buffer of data they
1210 * supplied.
1211 *
1212 * The device ID is invalid as soon as the device is closed, and is eligible
1213 * for reuse in a new SDL_OpenAudioDevice() call immediately.
1214 *
1215 * \param dev an audio device previously opened with SDL_OpenAudioDevice()
1216 *
1217 * \since This function is available since SDL 3.0.0.
1218 *
1219 * \sa SDL_OpenAudioDevice
1220 */
1221extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
1222
1223/**
1224 * Convert some audio data of one format to another format.
1225 *
1226 * Please note that this function is for convenience, but should not be used
1227 * to resample audio in blocks, as it will introduce audio artifacts on the
1228 * boundaries. You should only use this function if you are converting audio
1229 * data in its entirety in one call. If you want to convert audio in smaller
1230 * chunks, use an SDL_AudioStream, which is designed for this situation.
1231 *
1232 * Internally, this function creates and destroys an SDL_AudioStream on each
1233 * use, so it's also less efficient than using one directly, if you need to
1234 * convert multiple times.
1235 *
1236 * \param src_format The format of the source audio
1237 * \param src_channels The number of channels of the source audio
1238 * \param src_rate The sampling rate of the source audio
1239 * \param src_data The audio data to be converted
1240 * \param src_len The len of src_data
1241 * \param dst_format The format of the desired audio output
1242 * \param dst_channels The number of channels of the desired audio output
1243 * \param dst_rate The sampling rate of the desired audio output
1244 * \param dst_data Will be filled with a pointer to converted audio data,
1245 * which should be freed with SDL_free(). On error, it will be
1246 * NULL.
1247 * \param dst_len Will be filled with the len of dst_data
1248 * \returns 0 on success or a negative error code on failure; call
1249 * SDL_GetError() for more information.
1250 *
1251 * \since This function is available since SDL 3.0.0.
1252 *
1253 * \sa SDL_CreateAudioStream
1254 */
1255extern DECLSPEC int SDLCALL SDL_ConvertAudioSamples(SDL_AudioFormat src_format,
1256 Uint8 src_channels,
1257 int src_rate,
1258 const Uint8 *src_data,
1259 int src_len,
1260 SDL_AudioFormat dst_format,
1261 Uint8 dst_channels,
1262 int dst_rate,
1263 Uint8 **dst_data,
1264 int *dst_len);
1265
1266/* Ends C function definitions when using C++ */
1267#ifdef __cplusplus
1268}
1269#endif
1270#include <SDL3/SDL_close_code.h>
1271
1272#endif /* SDL_audio_h_ */
void SDL_CloseAudioDevice(SDL_AudioDeviceID dev)
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:692
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)
SDL_AudioSpec * SDL_LoadWAV_RW(SDL_RWops *src, SDL_bool freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_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
SDL_bool
Definition: SDL_stdinc.h:130
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