SDL 2.0
SDL_audio.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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 * # CategoryAudio
26 *
27 * 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 "SDL_stdinc.h"
34#include "SDL_error.h"
35#include "SDL_endian.h"
36#include "SDL_mutex.h"
37#include "SDL_thread.h"
38#include "SDL_rwops.h"
39
40#include "begin_code.h"
41/* Set up for C function definitions, even when using C++ */
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/**
47 * Audio format flags.
48 *
49 * These are what the 16 bits in SDL_AudioFormat currently mean...
50 * (Unspecified bits are always zero).
51 *
52 * ```
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 * ```
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 AUDIO_U8 0x0008 /**< Unsigned 8-bit samples */
92#define AUDIO_S8 0x8008 /**< Signed 8-bit samples */
93#define AUDIO_U16LSB 0x0010 /**< Unsigned 16-bit samples */
94#define AUDIO_S16LSB 0x8010 /**< Signed 16-bit samples */
95#define AUDIO_U16MSB 0x1010 /**< As above, but big-endian byte order */
96#define AUDIO_S16MSB 0x9010 /**< As above, but big-endian byte order */
97#define AUDIO_U16 AUDIO_U16LSB
98#define AUDIO_S16 AUDIO_S16LSB
99/* @} */
100
101/**
102 * \name int32 support
103 */
104/* @{ */
105#define AUDIO_S32LSB 0x8020 /**< 32-bit integer samples */
106#define AUDIO_S32MSB 0x9020 /**< As above, but big-endian byte order */
107#define AUDIO_S32 AUDIO_S32LSB
108/* @} */
109
110/**
111 * \name float32 support
112 */
113/* @{ */
114#define AUDIO_F32LSB 0x8120 /**< 32-bit floating point samples */
115#define AUDIO_F32MSB 0x9120 /**< As above, but big-endian byte order */
116#define AUDIO_F32 AUDIO_F32LSB
117/* @} */
118
119/**
120 * \name Native audio byte ordering
121 */
122/* @{ */
123#if SDL_BYTEORDER == SDL_LIL_ENDIAN
124#define AUDIO_U16SYS AUDIO_U16LSB
125#define AUDIO_S16SYS AUDIO_S16LSB
126#define AUDIO_S32SYS AUDIO_S32LSB
127#define AUDIO_F32SYS AUDIO_F32LSB
128#else
129#define AUDIO_U16SYS AUDIO_U16MSB
130#define AUDIO_S16SYS AUDIO_S16MSB
131#define AUDIO_S32SYS AUDIO_S32MSB
132#define AUDIO_F32SYS AUDIO_F32MSB
133#endif
134/* @} */
135
136/**
137 * \name Allow change flags
138 *
139 * Which audio format changes are allowed when opening a device.
140 */
141/* @{ */
142#define SDL_AUDIO_ALLOW_FREQUENCY_CHANGE 0x00000001
143#define SDL_AUDIO_ALLOW_FORMAT_CHANGE 0x00000002
144#define SDL_AUDIO_ALLOW_CHANNELS_CHANGE 0x00000004
145#define SDL_AUDIO_ALLOW_SAMPLES_CHANGE 0x00000008
146#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)
147/* @} */
148
149/* @} *//* Audio flags */
150
151/**
152 * This function is called when the audio device needs more data.
153 *
154 * \param userdata An application-specific parameter saved in the
155 * SDL_AudioSpec structure.
156 * \param stream A pointer to the audio data buffer.
157 * \param len Length of **stream** in bytes.
158 */
159typedef void (SDLCALL * SDL_AudioCallback) (void *userdata, Uint8 * stream,
160 int len);
161
162/**
163 * The calculated values in this structure are calculated by SDL_OpenAudio().
164 *
165 * For multi-channel audio, the default SDL channel mapping is:
166 *
167 * ```
168 * 2: FL FR (stereo)
169 * 3: FL FR LFE (2.1 surround)
170 * 4: FL FR BL BR (quad)
171 * 5: FL FR LFE BL BR (4.1 surround)
172 * 6: FL FR FC LFE SL SR (5.1 surround - last two can also be BL BR)
173 * 7: FL FR FC LFE BC SL SR (6.1 surround)
174 * 8: FL FR FC LFE BL BR SL SR (7.1 surround)
175 * ```
176 */
177typedef struct SDL_AudioSpec
178{
179 int freq; /**< DSP frequency -- samples per second */
180 SDL_AudioFormat format; /**< Audio data format */
181 Uint8 channels; /**< Number of channels: 1 mono, 2 stereo */
182 Uint8 silence; /**< Audio buffer silence value (calculated) */
183 Uint16 samples; /**< Audio buffer size in sample FRAMES (total samples divided by channel count) */
184 Uint16 padding; /**< Necessary for some compile environments */
185 Uint32 size; /**< Audio buffer size in bytes (calculated) */
186 SDL_AudioCallback callback; /**< Callback that feeds the audio device (NULL to use SDL_QueueAudio()). */
187 void *userdata; /**< Userdata passed to callback (ignored for NULL callbacks). */
189
190
191struct SDL_AudioCVT;
192typedef void (SDLCALL * SDL_AudioFilter) (struct SDL_AudioCVT * cvt,
193 SDL_AudioFormat format);
194
195/**
196 * Upper limit of filters in SDL_AudioCVT
197 *
198 * The maximum number of SDL_AudioFilter functions in SDL_AudioCVT is
199 * currently limited to 9. The SDL_AudioCVT.filters array has 10 pointers, one
200 * of which is the terminating NULL pointer.
201 */
202#define SDL_AUDIOCVT_MAX_FILTERS 9
203
204/**
205 * \struct SDL_AudioCVT
206 * \brief A structure to hold a set of audio conversion filters and buffers.
207 *
208 * Note that various parts of the conversion pipeline can take advantage
209 * of SIMD operations (like SSE2, for example). SDL_AudioCVT doesn't require
210 * you to pass it aligned data, but can possibly run much faster if you
211 * set both its (buf) field to a pointer that is aligned to 16 bytes, and its
212 * (len) field to something that's a multiple of 16, if possible.
213 */
214#if defined(__GNUC__) && !defined(__CHERI_PURE_CAPABILITY__)
215/* This structure is 84 bytes on 32-bit architectures, make sure GCC doesn't
216 pad it out to 88 bytes to guarantee ABI compatibility between compilers.
217 This is not a concern on CHERI architectures, where pointers must be stored
218 at aligned locations otherwise they will become invalid, and thus structs
219 containing pointers cannot be packed without giving a warning or error.
220 vvv
221 The next time we rev the ABI, make sure to size the ints and add padding.
222*/
223#define SDL_AUDIOCVT_PACKED __attribute__((packed))
224#else
225#define SDL_AUDIOCVT_PACKED
226#endif
227/* */
228typedef struct SDL_AudioCVT
229{
230 int needed; /**< Set to 1 if conversion possible */
231 SDL_AudioFormat src_format; /**< Source audio format */
232 SDL_AudioFormat dst_format; /**< Target audio format */
233 double rate_incr; /**< Rate conversion increment */
234 Uint8 *buf; /**< Buffer to hold entire audio data */
235 int len; /**< Length of original audio buffer */
236 int len_cvt; /**< Length of converted audio buffer */
237 int len_mult; /**< buffer must be len*len_mult big */
238 double len_ratio; /**< Given len, final size is len*len_ratio */
239 SDL_AudioFilter filters[SDL_AUDIOCVT_MAX_FILTERS + 1]; /**< NULL-terminated list of filter functions */
240 int filter_index; /**< Current audio conversion function */
242
243
244/* Function prototypes */
245
246/**
247 * \name Driver discovery functions
248 *
249 * These functions return the list of built in audio drivers, in the
250 * order that they are normally initialized by default.
251 */
252/* @{ */
253
254/**
255 * Use this function to get the number of built-in audio drivers.
256 *
257 * This function returns a hardcoded number. This never returns a negative
258 * value; if there are no drivers compiled into this build of SDL, this
259 * function returns zero. The presence of a driver in this list does not mean
260 * it will function, it just means SDL is capable of interacting with that
261 * interface. For example, a build of SDL might have esound support, but if
262 * there's no esound server available, SDL's esound driver would fail if used.
263 *
264 * By default, SDL tries all drivers, in its preferred order, until one is
265 * found to be usable.
266 *
267 * \returns the number of built-in audio drivers.
268 *
269 * \since This function is available since SDL 2.0.0.
270 *
271 * \sa SDL_GetAudioDriver
272 */
273extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
274
275/**
276 * Use this function to get the name of a built in audio driver.
277 *
278 * The list of audio drivers is given in the order that they are normally
279 * initialized by default; the drivers that seem more reasonable to choose
280 * first (as far as the SDL developers believe) are earlier in the list.
281 *
282 * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
283 * "coreaudio" or "xaudio2". These never have Unicode characters, and are not
284 * meant to be proper names.
285 *
286 * \param index the index of the audio driver; the value ranges from 0 to
287 * SDL_GetNumAudioDrivers() - 1.
288 * \returns the name of the audio driver at the requested index, or NULL if an
289 * invalid index was specified.
290 *
291 * \since This function is available since SDL 2.0.0.
292 *
293 * \sa SDL_GetNumAudioDrivers
294 */
295extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
296/* @} */
297
298/**
299 * \name Initialization and cleanup
300 *
301 * \internal These functions are used internally, and should not be used unless
302 * you have a specific need to specify the audio driver you want to
303 * use. You should normally use SDL_Init() or SDL_InitSubSystem().
304 */
305/* @{ */
306
307/**
308 * Use this function to initialize a particular audio driver.
309 *
310 * This function is used internally, and should not be used unless you have a
311 * specific need to designate the audio driver you want to use. You should
312 * normally use SDL_Init() or SDL_InitSubSystem().
313 *
314 * \param driver_name the name of the desired audio driver.
315 * \returns 0 on success or a negative error code on failure; call
316 * SDL_GetError() for more information.
317 *
318 * \since This function is available since SDL 2.0.0.
319 *
320 * \sa SDL_AudioQuit
321 */
322extern DECLSPEC int SDLCALL SDL_AudioInit(const char *driver_name);
323
324/**
325 * Use this function to shut down audio if you initialized it with
326 * SDL_AudioInit().
327 *
328 * This function is used internally, and should not be used unless you have a
329 * specific need to specify the audio driver you want to use. You should
330 * normally use SDL_Quit() or SDL_QuitSubSystem().
331 *
332 * \since This function is available since SDL 2.0.0.
333 *
334 * \sa SDL_AudioInit
335 */
336extern DECLSPEC void SDLCALL SDL_AudioQuit(void);
337/* @} */
338
339/**
340 * Get the name of the current audio driver.
341 *
342 * The returned string points to internal static memory and thus never becomes
343 * invalid, even if you quit the audio subsystem and initialize a new driver
344 * (although such a case would return a different static string from another
345 * call to this function, of course). As such, you should not modify or free
346 * the returned string.
347 *
348 * \returns the name of the current audio driver or NULL if no driver has been
349 * initialized.
350 *
351 * \since This function is available since SDL 2.0.0.
352 *
353 * \sa SDL_AudioInit
354 */
355extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
356
357/**
358 * This function is a legacy means of opening the audio device.
359 *
360 * This function remains for compatibility with SDL 1.2, but also because it's
361 * slightly easier to use than the new functions in SDL 2.0. The new, more
362 * powerful, and preferred way to do this is SDL_OpenAudioDevice().
363 *
364 * This function is roughly equivalent to:
365 *
366 * ```c
367 * SDL_OpenAudioDevice(NULL, 0, desired, obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
368 * ```
369 *
370 * With two notable exceptions:
371 *
372 * - If `obtained` is NULL, we use `desired` (and allow no changes), which
373 * means desired will be modified to have the correct values for silence,
374 * etc, and SDL will convert any differences between your app's specific
375 * request and the hardware behind the scenes.
376 * - The return value is always success or failure, and not a device ID, which
377 * means you can only have one device open at a time with this function.
378 *
379 * \param desired an SDL_AudioSpec structure representing the desired output
380 * format. Please refer to the SDL_OpenAudioDevice
381 * documentation for details on how to prepare this structure.
382 * \param obtained an SDL_AudioSpec structure filled in with the actual
383 * parameters, or NULL.
384 * \returns 0 if successful, placing the actual hardware parameters in the
385 * structure pointed to by `obtained`.
386 *
387 * If `obtained` is NULL, the audio data passed to the callback
388 * function will be guaranteed to be in the requested format, and
389 * will be automatically converted to the actual hardware audio
390 * format if necessary. If `obtained` is NULL, `desired` will have
391 * fields modified.
392 *
393 * This function returns a negative error code on failure to open the
394 * audio device or failure to set up the audio thread; call
395 * SDL_GetError() for more information.
396 *
397 * \since This function is available since SDL 2.0.0.
398 *
399 * \sa SDL_CloseAudio
400 * \sa SDL_LockAudio
401 * \sa SDL_PauseAudio
402 * \sa SDL_UnlockAudio
403 */
404extern DECLSPEC int SDLCALL SDL_OpenAudio(SDL_AudioSpec * desired,
405 SDL_AudioSpec * obtained);
406
407/**
408 * SDL Audio Device IDs.
409 *
410 * A successful call to SDL_OpenAudio() is always device id 1, and legacy SDL
411 * audio APIs assume you want this device ID. SDL_OpenAudioDevice() calls
412 * always returns devices >= 2 on success. The legacy calls are good both for
413 * backwards compatibility and when you don't care about multiple, specific,
414 * or capture devices.
415 */
417
418/**
419 * Get the number of built-in audio devices.
420 *
421 * This function is only valid after successfully initializing the audio
422 * subsystem.
423 *
424 * Note that audio capture support is not implemented as of SDL 2.0.4, so the
425 * `iscapture` parameter is for future expansion and should always be zero for
426 * now.
427 *
428 * This function will return -1 if an explicit list of devices can't be
429 * determined. Returning -1 is not an error. For example, if SDL is set up to
430 * talk to a remote audio server, it can't list every one available on the
431 * Internet, but it will still allow a specific host to be specified in
432 * SDL_OpenAudioDevice().
433 *
434 * In many common cases, when this function returns a value <= 0, it can still
435 * successfully open the default device (NULL for first argument of
436 * SDL_OpenAudioDevice()).
437 *
438 * This function may trigger a complete redetect of available hardware. It
439 * should not be called for each iteration of a loop, but rather once at the
440 * start of a loop:
441 *
442 * ```c
443 * // Don't do this:
444 * for (int i = 0; i < SDL_GetNumAudioDevices(0); i++)
445 *
446 * // do this instead:
447 * const int count = SDL_GetNumAudioDevices(0);
448 * for (int i = 0; i < count; ++i) { do_something_here(); }
449 * ```
450 *
451 * \param iscapture zero to request playback devices, non-zero to request
452 * recording devices.
453 * \returns the number of available devices exposed by the current driver or
454 * -1 if an explicit list of devices can't be determined. A return
455 * value of -1 does not necessarily mean an error condition.
456 *
457 * \since This function is available since SDL 2.0.0.
458 *
459 * \sa SDL_GetAudioDeviceName
460 * \sa SDL_OpenAudioDevice
461 */
462extern DECLSPEC int SDLCALL SDL_GetNumAudioDevices(int iscapture);
463
464/**
465 * Get the human-readable name of a specific audio device.
466 *
467 * This function is only valid after successfully initializing the audio
468 * subsystem. The values returned by this function reflect the latest call to
469 * SDL_GetNumAudioDevices(); re-call that function to redetect available
470 * hardware.
471 *
472 * The string returned by this function is UTF-8 encoded, read-only, and
473 * managed internally. You are not to free it. If you need to keep the string
474 * for any length of time, you should make your own copy of it, as it will be
475 * invalid next time any of several other SDL functions are called.
476 *
477 * \param index the index of the audio device; valid values range from 0 to
478 * SDL_GetNumAudioDevices() - 1.
479 * \param iscapture non-zero to query the list of recording devices, zero to
480 * query the list of output devices.
481 * \returns the name of the audio device at the requested index, or NULL on
482 * error.
483 *
484 * \since This function is available since SDL 2.0.0.
485 *
486 * \sa SDL_GetNumAudioDevices
487 * \sa SDL_GetDefaultAudioInfo
488 */
489extern DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(int index,
490 int iscapture);
491
492/**
493 * Get the preferred audio format of a specific audio device.
494 *
495 * This function is only valid after a successfully initializing the audio
496 * subsystem. The values returned by this function reflect the latest call to
497 * SDL_GetNumAudioDevices(); re-call that function to redetect available
498 * hardware.
499 *
500 * `spec` will be filled with the sample rate, sample format, and channel
501 * count.
502 *
503 * \param index the index of the audio device; valid values range from 0 to
504 * SDL_GetNumAudioDevices() - 1.
505 * \param iscapture non-zero to query the list of recording devices, zero to
506 * query the list of output devices.
507 * \param spec The SDL_AudioSpec to be initialized by this function.
508 * \returns 0 on success, nonzero on error.
509 *
510 * \since This function is available since SDL 2.0.16.
511 *
512 * \sa SDL_GetNumAudioDevices
513 * \sa SDL_GetDefaultAudioInfo
514 */
515extern DECLSPEC int SDLCALL SDL_GetAudioDeviceSpec(int index,
516 int iscapture,
517 SDL_AudioSpec *spec);
518
519
520/**
521 * Get the name and preferred format of the default audio device.
522 *
523 * Some (but not all!) platforms have an isolated mechanism to get information
524 * about the "default" device. This can actually be a completely different
525 * device that's not in the list you get from SDL_GetAudioDeviceSpec(). It can
526 * even be a network address! (This is discussed in SDL_OpenAudioDevice().)
527 *
528 * As a result, this call is not guaranteed to be performant, as it can query
529 * the sound server directly every time, unlike the other query functions. You
530 * should call this function sparingly!
531 *
532 * `spec` will be filled with the sample rate, sample format, and channel
533 * count, if a default device exists on the system. If `name` is provided,
534 * will be filled with either a dynamically-allocated UTF-8 string or NULL.
535 *
536 * \param name A pointer to be filled with the name of the default device (can
537 * be NULL). Please call SDL_free() when you are done with this
538 * pointer!
539 * \param spec The SDL_AudioSpec to be initialized by this function.
540 * \param iscapture non-zero to query the default recording device, zero to
541 * query the default output device.
542 * \returns 0 on success, nonzero on error.
543 *
544 * \since This function is available since SDL 2.24.0.
545 *
546 * \sa SDL_GetAudioDeviceName
547 * \sa SDL_GetAudioDeviceSpec
548 * \sa SDL_OpenAudioDevice
549 */
550extern DECLSPEC int SDLCALL SDL_GetDefaultAudioInfo(char **name,
551 SDL_AudioSpec *spec,
552 int iscapture);
553
554
555/**
556 * Open a specific audio device.
557 *
558 * SDL_OpenAudio(), unlike this function, always acts on device ID 1. As such,
559 * this function will never return a 1 so as not to conflict with the legacy
560 * function.
561 *
562 * Please note that SDL 2.0 before 2.0.5 did not support recording; as such,
563 * this function would fail if `iscapture` was not zero. Starting with SDL
564 * 2.0.5, recording is implemented and this value can be non-zero.
565 *
566 * Passing in a `device` name of NULL requests the most reasonable default
567 * (and is equivalent to what SDL_OpenAudio() does to choose a device). The
568 * `device` name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but
569 * some drivers allow arbitrary and driver-specific strings, such as a
570 * hostname/IP address for a remote audio server, or a filename in the
571 * diskaudio driver.
572 *
573 * An opened audio device starts out paused, and should be enabled for playing
574 * by calling SDL_PauseAudioDevice(devid, 0) when you are ready for your audio
575 * callback function to be called. Since the audio driver may modify the
576 * requested size of the audio buffer, you should allocate any local mixing
577 * buffers after you open the audio device.
578 *
579 * The audio callback runs in a separate thread in most cases; you can prevent
580 * race conditions between your callback and other threads without fully
581 * pausing playback with SDL_LockAudioDevice(). For more information about the
582 * callback, see SDL_AudioSpec.
583 *
584 * Managing the audio spec via 'desired' and 'obtained':
585 *
586 * When filling in the desired audio spec structure:
587 *
588 * - `desired->freq` should be the frequency in sample-frames-per-second (Hz).
589 * - `desired->format` should be the audio format (`AUDIO_S16SYS`, etc).
590 * - `desired->samples` is the desired size of the audio buffer, in _sample
591 * frames_ (with stereo output, two samples--left and right--would make a
592 * single sample frame). This number should be a power of two, and may be
593 * adjusted by the audio driver to a value more suitable for the hardware.
594 * Good values seem to range between 512 and 4096 inclusive, depending on
595 * the application and CPU speed. Smaller values reduce latency, but can
596 * lead to underflow if the application is doing heavy processing and cannot
597 * fill the audio buffer in time. Note that the number of sample frames is
598 * directly related to time by the following formula: `ms =
599 * (sampleframes*1000)/freq`
600 * - `desired->size` is the size in _bytes_ of the audio buffer, and is
601 * calculated by SDL_OpenAudioDevice(). You don't initialize this.
602 * - `desired->silence` is the value used to set the buffer to silence, and is
603 * calculated by SDL_OpenAudioDevice(). You don't initialize this.
604 * - `desired->callback` should be set to a function that will be called when
605 * the audio device is ready for more data. It is passed a pointer to the
606 * audio buffer, and the length in bytes of the audio buffer. This function
607 * usually runs in a separate thread, and so you should protect data
608 * structures that it accesses by calling SDL_LockAudioDevice() and
609 * SDL_UnlockAudioDevice() in your code. Alternately, you may pass a NULL
610 * pointer here, and call SDL_QueueAudio() with some frequency, to queue
611 * more audio samples to be played (or for capture devices, call
612 * SDL_DequeueAudio() with some frequency, to obtain audio samples).
613 * - `desired->userdata` is passed as the first parameter to your callback
614 * function. If you passed a NULL callback, this value is ignored.
615 *
616 * `allowed_changes` can have the following flags OR'd together:
617 *
618 * - `SDL_AUDIO_ALLOW_FREQUENCY_CHANGE`
619 * - `SDL_AUDIO_ALLOW_FORMAT_CHANGE`
620 * - `SDL_AUDIO_ALLOW_CHANNELS_CHANGE`
621 * - `SDL_AUDIO_ALLOW_SAMPLES_CHANGE`
622 * - `SDL_AUDIO_ALLOW_ANY_CHANGE`
623 *
624 * These flags specify how SDL should behave when a device cannot offer a
625 * specific feature. If the application requests a feature that the hardware
626 * doesn't offer, SDL will always try to get the closest equivalent.
627 *
628 * For example, if you ask for float32 audio format, but the sound card only
629 * supports int16, SDL will set the hardware to int16. If you had set
630 * SDL_AUDIO_ALLOW_FORMAT_CHANGE, SDL will change the format in the `obtained`
631 * structure. If that flag was *not* set, SDL will prepare to convert your
632 * callback's float32 audio to int16 before feeding it to the hardware and
633 * will keep the originally requested format in the `obtained` structure.
634 *
635 * The resulting audio specs, varying depending on hardware and on what
636 * changes were allowed, will then be written back to `obtained`.
637 *
638 * If your application can only handle one specific data format, pass a zero
639 * for `allowed_changes` and let SDL transparently handle any differences.
640 *
641 * \param device a UTF-8 string reported by SDL_GetAudioDeviceName() or a
642 * driver-specific name as appropriate. NULL requests the most
643 * reasonable default device.
644 * \param iscapture non-zero to specify a device should be opened for
645 * recording, not playback.
646 * \param desired an SDL_AudioSpec structure representing the desired output
647 * format; see SDL_OpenAudio() for more information.
648 * \param obtained an SDL_AudioSpec structure filled in with the actual output
649 * format; see SDL_OpenAudio() for more information.
650 * \param allowed_changes 0, or one or more flags OR'd together.
651 * \returns a valid device ID that is > 0 on success or 0 on failure; call
652 * SDL_GetError() for more information.
653 *
654 * For compatibility with SDL 1.2, this will never return 1, since
655 * SDL reserves that ID for the legacy SDL_OpenAudio() function.
656 *
657 * \since This function is available since SDL 2.0.0.
658 *
659 * \sa SDL_CloseAudioDevice
660 * \sa SDL_GetAudioDeviceName
661 * \sa SDL_LockAudioDevice
662 * \sa SDL_OpenAudio
663 * \sa SDL_PauseAudioDevice
664 * \sa SDL_UnlockAudioDevice
665 */
667 const char *device,
668 int iscapture,
669 const SDL_AudioSpec *desired,
670 SDL_AudioSpec *obtained,
671 int allowed_changes);
672
673
674
675/**
676 * \name Audio state
677 *
678 * Get the current audio state.
679 */
680/* @{ */
687
688/**
689 * This function is a legacy means of querying the audio device.
690 *
691 * New programs might want to use SDL_GetAudioDeviceStatus() instead. This
692 * function is equivalent to calling...
693 *
694 * ```c
695 * SDL_GetAudioDeviceStatus(1);
696 * ```
697 *
698 * ...and is only useful if you used the legacy SDL_OpenAudio() function.
699 *
700 * \returns the SDL_AudioStatus of the audio device opened by SDL_OpenAudio().
701 *
702 * \since This function is available since SDL 2.0.0.
703 *
704 * \sa SDL_GetAudioDeviceStatus
705 */
706extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioStatus(void);
707
708/**
709 * Use this function to get the current audio state of an audio device.
710 *
711 * \param dev the ID of an audio device previously opened with
712 * SDL_OpenAudioDevice().
713 * \returns the SDL_AudioStatus of the specified audio device.
714 *
715 * \since This function is available since SDL 2.0.0.
716 *
717 * \sa SDL_PauseAudioDevice
718 */
720/* @} *//* Audio State */
721
722/**
723 * \name Pause audio functions
724 *
725 * These functions pause and unpause the audio callback processing.
726 * They should be called with a parameter of 0 after opening the audio
727 * device to start playing sound. This is so you can safely initialize
728 * data for your callback function after opening the audio device.
729 * Silence will be written to the audio device during the pause.
730 */
731/* @{ */
732
733/**
734 * This function is a legacy means of pausing the audio device.
735 *
736 * New programs might want to use SDL_PauseAudioDevice() instead. This
737 * function is equivalent to calling...
738 *
739 * ```c
740 * SDL_PauseAudioDevice(1, pause_on);
741 * ```
742 *
743 * ...and is only useful if you used the legacy SDL_OpenAudio() function.
744 *
745 * \param pause_on non-zero to pause, 0 to unpause.
746 *
747 * \since This function is available since SDL 2.0.0.
748 *
749 * \sa SDL_GetAudioStatus
750 * \sa SDL_PauseAudioDevice
751 */
752extern DECLSPEC void SDLCALL SDL_PauseAudio(int pause_on);
753
754/**
755 * Use this function to pause and unpause audio playback on a specified
756 * device.
757 *
758 * This function pauses and unpauses the audio callback processing for a given
759 * device. Newly-opened audio devices start in the paused state, so you must
760 * call this function with **pause_on**=0 after opening the specified audio
761 * device to start playing sound. This allows you to safely initialize data
762 * for your callback function after opening the audio device. Silence will be
763 * written to the audio device while paused, and the audio callback is
764 * guaranteed to not be called. Pausing one device does not prevent other
765 * unpaused devices from running their callbacks.
766 *
767 * Pausing state does not stack; even if you pause a device several times, a
768 * single unpause will start the device playing again, and vice versa. This is
769 * different from how SDL_LockAudioDevice() works.
770 *
771 * If you just need to protect a few variables from race conditions vs your
772 * callback, you shouldn't pause the audio device, as it will lead to dropouts
773 * in the audio playback. Instead, you should use SDL_LockAudioDevice().
774 *
775 * \param dev a device opened by SDL_OpenAudioDevice().
776 * \param pause_on non-zero to pause, 0 to unpause.
777 *
778 * \since This function is available since SDL 2.0.0.
779 *
780 * \sa SDL_LockAudioDevice
781 */
782extern DECLSPEC void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev,
783 int pause_on);
784/* @} *//* Pause audio functions */
785
786/**
787 * Load the audio data of a WAVE file into memory.
788 *
789 * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
790 * be valid pointers. The entire data portion of the file is then loaded into
791 * memory and decoded if necessary.
792 *
793 * If `freesrc` is non-zero, the data source gets automatically closed and
794 * freed before the function returns.
795 *
796 * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
797 * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
798 * A-law and mu-law (8 bits). Other formats are currently unsupported and
799 * cause an error.
800 *
801 * If this function succeeds, the pointer returned by it is equal to `spec`
802 * and the pointer to the audio data allocated by the function is written to
803 * `audio_buf` and its length in bytes to `audio_len`. The SDL_AudioSpec
804 * members `freq`, `channels`, and `format` are set to the values of the audio
805 * data in the buffer. The `samples` member is set to a sane default and all
806 * others are set to zero.
807 *
808 * It's necessary to use SDL_FreeWAV() to free the audio data returned in
809 * `audio_buf` when it is no longer used.
810 *
811 * Because of the underspecification of the .WAV format, there are many
812 * problematic files in the wild that cause issues with strict decoders. To
813 * provide compatibility with these files, this decoder is lenient in regards
814 * to the truncation of the file, the fact chunk, and the size of the RIFF
815 * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
816 * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
817 * tune the behavior of the loading process.
818 *
819 * Any file that is invalid (due to truncation, corruption, or wrong values in
820 * the headers), too big, or unsupported causes an error. Additionally, any
821 * critical I/O error from the data source will terminate the loading process
822 * with an error. The function returns NULL on error and in all cases (with
823 * the exception of `src` being NULL), an appropriate error message will be
824 * set.
825 *
826 * It is required that the data source supports seeking.
827 *
828 * Example:
829 *
830 * ```c
831 * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
832 * ```
833 *
834 * Note that the SDL_LoadWAV macro does this same thing for you, but in a less
835 * messy way:
836 *
837 * ```c
838 * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
839 * ```
840 *
841 * \param src The data source for the WAVE data.
842 * \param freesrc If non-zero, SDL will _always_ free the data source.
843 * \param spec An SDL_AudioSpec that will be filled in with the wave file's
844 * format details.
845 * \param audio_buf A pointer filled with the audio data, allocated by the
846 * function.
847 * \param audio_len A pointer filled with the length of the audio data buffer
848 * in bytes.
849 * \returns This function, if successfully called, returns `spec`, which will
850 * be filled with the audio data format of the wave source data.
851 * `audio_buf` will be filled with a pointer to an allocated buffer
852 * containing the audio data, and `audio_len` is filled with the
853 * length of that audio buffer in bytes.
854 *
855 * This function returns NULL if the .WAV file cannot be opened, uses
856 * an unknown data format, or is corrupt; call SDL_GetError() for
857 * more information.
858 *
859 * When the application is done with the data returned in
860 * `audio_buf`, it should call SDL_FreeWAV() to dispose of it.
861 *
862 * \since This function is available since SDL 2.0.0.
863 *
864 * \sa SDL_FreeWAV
865 * \sa SDL_LoadWAV
866 */
867extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
868 int freesrc,
869 SDL_AudioSpec * spec,
870 Uint8 ** audio_buf,
871 Uint32 * audio_len);
872
873/**
874 * Loads a WAV from a file.
875 *
876 * Compatibility convenience function.
877 */
878#define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
879 SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
880
881/**
882 * Free data previously allocated with SDL_LoadWAV() or SDL_LoadWAV_RW().
883 *
884 * After a WAVE file has been opened with SDL_LoadWAV() or SDL_LoadWAV_RW()
885 * its data can eventually be freed with SDL_FreeWAV(). It is safe to call
886 * this function with a NULL pointer.
887 *
888 * \param audio_buf a pointer to the buffer created by SDL_LoadWAV() or
889 * SDL_LoadWAV_RW().
890 *
891 * \since This function is available since SDL 2.0.0.
892 *
893 * \sa SDL_LoadWAV
894 * \sa SDL_LoadWAV_RW
895 */
896extern DECLSPEC void SDLCALL SDL_FreeWAV(Uint8 * audio_buf);
897
898/**
899 * Initialize an SDL_AudioCVT structure for conversion.
900 *
901 * Before an SDL_AudioCVT structure can be used to convert audio data it must
902 * be initialized with source and destination information.
903 *
904 * This function will zero out every field of the SDL_AudioCVT, so it must be
905 * called before the application fills in the final buffer information.
906 *
907 * Once this function has returned successfully, and reported that a
908 * conversion is necessary, the application fills in the rest of the fields in
909 * SDL_AudioCVT, now that it knows how large a buffer it needs to allocate,
910 * and then can call SDL_ConvertAudio() to complete the conversion.
911 *
912 * \param cvt an SDL_AudioCVT structure filled in with audio conversion
913 * information.
914 * \param src_format the source format of the audio data; for more info see
915 * SDL_AudioFormat.
916 * \param src_channels the number of channels in the source.
917 * \param src_rate the frequency (sample-frames-per-second) of the source.
918 * \param dst_format the destination format of the audio data; for more info
919 * see SDL_AudioFormat.
920 * \param dst_channels the number of channels in the destination.
921 * \param dst_rate the frequency (sample-frames-per-second) of the
922 * destination.
923 * \returns 1 if the audio filter is prepared, 0 if no conversion is needed,
924 * or a negative error code on failure; call SDL_GetError() for more
925 * information.
926 *
927 * \since This function is available since SDL 2.0.0.
928 *
929 * \sa SDL_ConvertAudio
930 */
931extern DECLSPEC int SDLCALL SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
932 SDL_AudioFormat src_format,
933 Uint8 src_channels,
934 int src_rate,
935 SDL_AudioFormat dst_format,
936 Uint8 dst_channels,
937 int dst_rate);
938
939/**
940 * Convert audio data to a desired audio format.
941 *
942 * This function does the actual audio data conversion, after the application
943 * has called SDL_BuildAudioCVT() to prepare the conversion information and
944 * then filled in the buffer details.
945 *
946 * Once the application has initialized the `cvt` structure using
947 * SDL_BuildAudioCVT(), allocated an audio buffer and filled it with audio
948 * data in the source format, this function will convert the buffer, in-place,
949 * to the desired format.
950 *
951 * The data conversion may go through several passes; any given pass may
952 * possibly temporarily increase the size of the data. For example, SDL might
953 * expand 16-bit data to 32 bits before resampling to a lower frequency,
954 * shrinking the data size after having grown it briefly. Since the supplied
955 * buffer will be both the source and destination, converting as necessary
956 * in-place, the application must allocate a buffer that will fully contain
957 * the data during its largest conversion pass. After SDL_BuildAudioCVT()
958 * returns, the application should set the `cvt->len` field to the size, in
959 * bytes, of the source data, and allocate a buffer that is `cvt->len *
960 * cvt->len_mult` bytes long for the `buf` field.
961 *
962 * The source data should be copied into this buffer before the call to
963 * SDL_ConvertAudio(). Upon successful return, this buffer will contain the
964 * converted audio, and `cvt->len_cvt` will be the size of the converted data,
965 * in bytes. Any bytes in the buffer past `cvt->len_cvt` are undefined once
966 * this function returns.
967 *
968 * \param cvt an SDL_AudioCVT structure that was previously set up by
969 * SDL_BuildAudioCVT().
970 * \returns 0 if the conversion was completed successfully or a negative error
971 * code on failure; call SDL_GetError() for more information.
972 *
973 * \since This function is available since SDL 2.0.0.
974 *
975 * \sa SDL_BuildAudioCVT
976 */
977extern DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT * cvt);
978
979/* SDL_AudioStream is a new audio conversion interface.
980 The benefits vs SDL_AudioCVT:
981 - it can handle resampling data in chunks without generating
982 artifacts, when it doesn't have the complete buffer available.
983 - it can handle incoming data in any variable size.
984 - You push data as you have it, and pull it when you need it
985 */
986/* this is opaque to the outside world. */
987struct _SDL_AudioStream;
988typedef struct _SDL_AudioStream SDL_AudioStream;
989
990/**
991 * Create a new audio stream.
992 *
993 * \param src_format The format of the source audio.
994 * \param src_channels The number of channels of the source audio.
995 * \param src_rate The sampling rate of the source audio.
996 * \param dst_format The format of the desired audio output.
997 * \param dst_channels The number of channels of the desired audio output.
998 * \param dst_rate The sampling rate of the desired audio output.
999 * \returns 0 on success, or -1 on error.
1000 *
1001 * \since This function is available since SDL 2.0.7.
1002 *
1003 * \sa SDL_AudioStreamPut
1004 * \sa SDL_AudioStreamGet
1005 * \sa SDL_AudioStreamAvailable
1006 * \sa SDL_AudioStreamFlush
1007 * \sa SDL_AudioStreamClear
1008 * \sa SDL_FreeAudioStream
1009 */
1010extern DECLSPEC SDL_AudioStream * SDLCALL SDL_NewAudioStream(const SDL_AudioFormat src_format,
1011 const Uint8 src_channels,
1012 const int src_rate,
1013 const SDL_AudioFormat dst_format,
1014 const Uint8 dst_channels,
1015 const int dst_rate);
1016
1017/**
1018 * Add data to be converted/resampled to the stream.
1019 *
1020 * \param stream The stream the audio data is being added to.
1021 * \param buf A pointer to the audio data to add.
1022 * \param len The number of bytes to write to the stream.
1023 * \returns 0 on success, or -1 on error.
1024 *
1025 * \since This function is available since SDL 2.0.7.
1026 *
1027 * \sa SDL_NewAudioStream
1028 * \sa SDL_AudioStreamGet
1029 * \sa SDL_AudioStreamAvailable
1030 * \sa SDL_AudioStreamFlush
1031 * \sa SDL_AudioStreamClear
1032 * \sa SDL_FreeAudioStream
1033 */
1034extern DECLSPEC int SDLCALL SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len);
1035
1036/**
1037 * Get converted/resampled data from the stream
1038 *
1039 * \param stream The stream the audio is being requested from.
1040 * \param buf A buffer to fill with audio data.
1041 * \param len The maximum number of bytes to fill.
1042 * \returns the number of bytes read from the stream, or -1 on error.
1043 *
1044 * \since This function is available since SDL 2.0.7.
1045 *
1046 * \sa SDL_NewAudioStream
1047 * \sa SDL_AudioStreamPut
1048 * \sa SDL_AudioStreamAvailable
1049 * \sa SDL_AudioStreamFlush
1050 * \sa SDL_AudioStreamClear
1051 * \sa SDL_FreeAudioStream
1052 */
1053extern DECLSPEC int SDLCALL SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len);
1054
1055/**
1056 * Get the number of converted/resampled bytes available.
1057 *
1058 * The stream may be buffering data behind the scenes until it has enough to
1059 * resample correctly, so this number might be lower than what you expect, or
1060 * even be zero. Add more data or flush the stream if you need the data now.
1061 *
1062 * \since This function is available since SDL 2.0.7.
1063 *
1064 * \sa SDL_NewAudioStream
1065 * \sa SDL_AudioStreamPut
1066 * \sa SDL_AudioStreamGet
1067 * \sa SDL_AudioStreamFlush
1068 * \sa SDL_AudioStreamClear
1069 * \sa SDL_FreeAudioStream
1070 */
1071extern DECLSPEC int SDLCALL SDL_AudioStreamAvailable(SDL_AudioStream *stream);
1072
1073/**
1074 * Tell the stream that you're done sending data, and anything being buffered
1075 * should be converted/resampled and made available immediately.
1076 *
1077 * It is legal to add more data to a stream after flushing, but there will be
1078 * audio gaps in the output. Generally this is intended to signal the end of
1079 * input, so the complete output becomes available.
1080 *
1081 * \since This function is available since SDL 2.0.7.
1082 *
1083 * \sa SDL_NewAudioStream
1084 * \sa SDL_AudioStreamPut
1085 * \sa SDL_AudioStreamGet
1086 * \sa SDL_AudioStreamAvailable
1087 * \sa SDL_AudioStreamClear
1088 * \sa SDL_FreeAudioStream
1089 */
1090extern DECLSPEC int SDLCALL SDL_AudioStreamFlush(SDL_AudioStream *stream);
1091
1092/**
1093 * Clear any pending data in the stream without converting it
1094 *
1095 * \since This function is available since SDL 2.0.7.
1096 *
1097 * \sa SDL_NewAudioStream
1098 * \sa SDL_AudioStreamPut
1099 * \sa SDL_AudioStreamGet
1100 * \sa SDL_AudioStreamAvailable
1101 * \sa SDL_AudioStreamFlush
1102 * \sa SDL_FreeAudioStream
1103 */
1104extern DECLSPEC void SDLCALL SDL_AudioStreamClear(SDL_AudioStream *stream);
1105
1106/**
1107 * Free an audio stream
1108 *
1109 * \since This function is available since SDL 2.0.7.
1110 *
1111 * \sa SDL_NewAudioStream
1112 * \sa SDL_AudioStreamPut
1113 * \sa SDL_AudioStreamGet
1114 * \sa SDL_AudioStreamAvailable
1115 * \sa SDL_AudioStreamFlush
1116 * \sa SDL_AudioStreamClear
1117 */
1118extern DECLSPEC void SDLCALL SDL_FreeAudioStream(SDL_AudioStream *stream);
1119
1120/**
1121 * Maximum volume allowed in calls to SDL_MixAudio and SDL_MixAudioFormat.
1122 */
1123#define SDL_MIX_MAXVOLUME 128
1124
1125/**
1126 * This function is a legacy means of mixing audio.
1127 *
1128 * This function is equivalent to calling...
1129 *
1130 * ```c
1131 * SDL_MixAudioFormat(dst, src, format, len, volume);
1132 * ```
1133 *
1134 * ...where `format` is the obtained format of the audio device from the
1135 * legacy SDL_OpenAudio() function.
1136 *
1137 * \param dst the destination for the mixed audio.
1138 * \param src the source audio buffer to be mixed.
1139 * \param len the length of the audio buffer in bytes.
1140 * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
1141 * for full audio volume.
1142 *
1143 * \since This function is available since SDL 2.0.0.
1144 *
1145 * \sa SDL_MixAudioFormat
1146 */
1147extern DECLSPEC void SDLCALL SDL_MixAudio(Uint8 * dst, const Uint8 * src,
1148 Uint32 len, int volume);
1149
1150/**
1151 * Mix audio data in a specified format.
1152 *
1153 * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
1154 * it into `dst`, performing addition, volume adjustment, and overflow
1155 * clipping. The buffer pointed to by `dst` must also be `len` bytes of
1156 * `format` data.
1157 *
1158 * This is provided for convenience -- you can mix your own audio data.
1159 *
1160 * Do not use this function for mixing together more than two streams of
1161 * sample data. The output from repeated application of this function may be
1162 * distorted by clipping, because there is no accumulator with greater range
1163 * than the input (not to mention this being an inefficient way of doing it).
1164 *
1165 * It is a common misconception that this function is required to write audio
1166 * data to an output stream in an audio callback. While you can do that,
1167 * SDL_MixAudioFormat() is really only needed when you're mixing a single
1168 * audio stream with a volume adjustment.
1169 *
1170 * \param dst the destination for the mixed audio.
1171 * \param src the source audio buffer to be mixed.
1172 * \param format the SDL_AudioFormat structure representing the desired audio
1173 * format.
1174 * \param len the length of the audio buffer in bytes.
1175 * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
1176 * for full audio volume.
1177 *
1178 * \since This function is available since SDL 2.0.0.
1179 */
1180extern DECLSPEC void SDLCALL SDL_MixAudioFormat(Uint8 * dst,
1181 const Uint8 * src,
1182 SDL_AudioFormat format,
1183 Uint32 len, int volume);
1184
1185/**
1186 * Queue more audio on non-callback devices.
1187 *
1188 * If you are looking to retrieve queued audio from a non-callback capture
1189 * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
1190 * -1 to signify an error if you use it with capture devices.
1191 *
1192 * SDL offers two ways to feed audio to the device: you can either supply a
1193 * callback that SDL triggers with some frequency to obtain more audio (pull
1194 * method), or you can supply no callback, and then SDL will expect you to
1195 * supply data at regular intervals (push method) with this function.
1196 *
1197 * There are no limits on the amount of data you can queue, short of
1198 * exhaustion of address space. Queued data will drain to the device as
1199 * necessary without further intervention from you. If the device needs audio
1200 * but there is not enough queued, it will play silence to make up the
1201 * difference. This means you will have skips in your audio playback if you
1202 * aren't routinely queueing sufficient data.
1203 *
1204 * This function copies the supplied data, so you are safe to free it when the
1205 * function returns. This function is thread-safe, but queueing to the same
1206 * device from two threads at once does not promise which buffer will be
1207 * queued first.
1208 *
1209 * You may not queue audio on a device that is using an application-supplied
1210 * callback; doing so returns an error. You have to use the audio callback or
1211 * queue audio with this function, but not both.
1212 *
1213 * You should not call SDL_LockAudio() on the device before queueing; SDL
1214 * handles locking internally for this function.
1215 *
1216 * Note that SDL2 does not support planar audio. You will need to resample
1217 * from planar audio formats into a non-planar one (see SDL_AudioFormat)
1218 * before queuing audio.
1219 *
1220 * \param dev the device ID to which we will queue audio.
1221 * \param data the data to queue to the device for later playback.
1222 * \param len the number of bytes (not samples!) to which `data` points.
1223 * \returns 0 on success or a negative error code on failure; call
1224 * SDL_GetError() for more information.
1225 *
1226 * \since This function is available since SDL 2.0.4.
1227 *
1228 * \sa SDL_ClearQueuedAudio
1229 * \sa SDL_GetQueuedAudioSize
1230 */
1231extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
1232
1233/**
1234 * Dequeue more audio on non-callback devices.
1235 *
1236 * If you are looking to queue audio for output on a non-callback playback
1237 * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
1238 * return 0 if you use it with playback devices.
1239 *
1240 * SDL offers two ways to retrieve audio from a capture device: you can either
1241 * supply a callback that SDL triggers with some frequency as the device
1242 * records more audio data, (push method), or you can supply no callback, and
1243 * then SDL will expect you to retrieve data at regular intervals (pull
1244 * method) with this function.
1245 *
1246 * There are no limits on the amount of data you can queue, short of
1247 * exhaustion of address space. Data from the device will keep queuing as
1248 * necessary without further intervention from you. This means you will
1249 * eventually run out of memory if you aren't routinely dequeueing data.
1250 *
1251 * Capture devices will not queue data when paused; if you are expecting to
1252 * not need captured audio for some length of time, use SDL_PauseAudioDevice()
1253 * to stop the capture device from queueing more data. This can be useful
1254 * during, say, level loading times. When unpaused, capture devices will start
1255 * queueing data from that point, having flushed any capturable data available
1256 * while paused.
1257 *
1258 * This function is thread-safe, but dequeueing from the same device from two
1259 * threads at once does not promise which thread will dequeue data first.
1260 *
1261 * You may not dequeue audio from a device that is using an
1262 * application-supplied callback; doing so returns an error. You have to use
1263 * the audio callback, or dequeue audio with this function, but not both.
1264 *
1265 * You should not call SDL_LockAudio() on the device before dequeueing; SDL
1266 * handles locking internally for this function.
1267 *
1268 * \param dev the device ID from which we will dequeue audio.
1269 * \param data a pointer into where audio data should be copied.
1270 * \param len the number of bytes (not samples!) to which (data) points.
1271 * \returns the number of bytes dequeued, which could be less than requested;
1272 * call SDL_GetError() for more information.
1273 *
1274 * \since This function is available since SDL 2.0.5.
1275 *
1276 * \sa SDL_ClearQueuedAudio
1277 * \sa SDL_GetQueuedAudioSize
1278 */
1279extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
1280
1281/**
1282 * Get the number of bytes of still-queued audio.
1283 *
1284 * For playback devices: this is the number of bytes that have been queued for
1285 * playback with SDL_QueueAudio(), but have not yet been sent to the hardware.
1286 *
1287 * Once we've sent it to the hardware, this function can not decide the exact
1288 * byte boundary of what has been played. It's possible that we just gave the
1289 * hardware several kilobytes right before you called this function, but it
1290 * hasn't played any of it yet, or maybe half of it, etc.
1291 *
1292 * For capture devices, this is the number of bytes that have been captured by
1293 * the device and are waiting for you to dequeue. This number may grow at any
1294 * time, so this only informs of the lower-bound of available data.
1295 *
1296 * You may not queue or dequeue audio on a device that is using an
1297 * application-supplied callback; calling this function on such a device
1298 * always returns 0. You have to use the audio callback or queue audio, but
1299 * not both.
1300 *
1301 * You should not call SDL_LockAudio() on the device before querying; SDL
1302 * handles locking internally for this function.
1303 *
1304 * \param dev the device ID of which we will query queued audio size.
1305 * \returns the number of bytes (not samples!) of queued audio.
1306 *
1307 * \since This function is available since SDL 2.0.4.
1308 *
1309 * \sa SDL_ClearQueuedAudio
1310 * \sa SDL_QueueAudio
1311 * \sa SDL_DequeueAudio
1312 */
1314
1315/**
1316 * Drop any queued audio data waiting to be sent to the hardware.
1317 *
1318 * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
1319 * output devices, the hardware will start playing silence if more audio isn't
1320 * queued. For capture devices, the hardware will start filling the empty
1321 * queue with new data if the capture device isn't paused.
1322 *
1323 * This will not prevent playback of queued audio that's already been sent to
1324 * the hardware, as we can not undo that, so expect there to be some fraction
1325 * of a second of audio that might still be heard. This can be useful if you
1326 * want to, say, drop any pending music or any unprocessed microphone input
1327 * during a level change in your game.
1328 *
1329 * You may not queue or dequeue audio on a device that is using an
1330 * application-supplied callback; calling this function on such a device
1331 * always returns 0. You have to use the audio callback or queue audio, but
1332 * not both.
1333 *
1334 * You should not call SDL_LockAudio() on the device before clearing the
1335 * queue; SDL handles locking internally for this function.
1336 *
1337 * This function always succeeds and thus returns void.
1338 *
1339 * \param dev the device ID of which to clear the audio queue.
1340 *
1341 * \since This function is available since SDL 2.0.4.
1342 *
1343 * \sa SDL_GetQueuedAudioSize
1344 * \sa SDL_QueueAudio
1345 * \sa SDL_DequeueAudio
1346 */
1347extern DECLSPEC void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
1348
1349
1350/**
1351 * \name Audio lock functions
1352 *
1353 * The lock manipulated by these functions protects the callback function.
1354 * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
1355 * the callback function is not running. Do not call these from the callback
1356 * function or you will cause deadlock.
1357 */
1358/* @{ */
1359
1360/**
1361 * This function is a legacy means of locking the audio device.
1362 *
1363 * New programs might want to use SDL_LockAudioDevice() instead. This function
1364 * is equivalent to calling...
1365 *
1366 * ```c
1367 * SDL_LockAudioDevice(1);
1368 * ```
1369 *
1370 * ...and is only useful if you used the legacy SDL_OpenAudio() function.
1371 *
1372 * \since This function is available since SDL 2.0.0.
1373 *
1374 * \sa SDL_LockAudioDevice
1375 * \sa SDL_UnlockAudio
1376 * \sa SDL_UnlockAudioDevice
1377 */
1378extern DECLSPEC void SDLCALL SDL_LockAudio(void);
1379
1380/**
1381 * Use this function to lock out the audio callback function for a specified
1382 * device.
1383 *
1384 * The lock manipulated by these functions protects the audio callback
1385 * function specified in SDL_OpenAudioDevice(). During a
1386 * SDL_LockAudioDevice()/SDL_UnlockAudioDevice() pair, you can be guaranteed
1387 * that the callback function for that device is not running, even if the
1388 * device is not paused. While a device is locked, any other unpaused,
1389 * unlocked devices may still run their callbacks.
1390 *
1391 * Calling this function from inside your audio callback is unnecessary. SDL
1392 * obtains this lock before calling your function, and releases it when the
1393 * function returns.
1394 *
1395 * You should not hold the lock longer than absolutely necessary. If you hold
1396 * it too long, you'll experience dropouts in your audio playback. Ideally,
1397 * your application locks the device, sets a few variables and unlocks again.
1398 * Do not do heavy work while holding the lock for a device.
1399 *
1400 * It is safe to lock the audio device multiple times, as long as you unlock
1401 * it an equivalent number of times. The callback will not run until the
1402 * device has been unlocked completely in this way. If your application fails
1403 * to unlock the device appropriately, your callback will never run, you might
1404 * hear repeating bursts of audio, and SDL_CloseAudioDevice() will probably
1405 * deadlock.
1406 *
1407 * Internally, the audio device lock is a mutex; if you lock from two threads
1408 * at once, not only will you block the audio callback, you'll block the other
1409 * thread.
1410 *
1411 * \param dev the ID of the device to be locked.
1412 *
1413 * \since This function is available since SDL 2.0.0.
1414 *
1415 * \sa SDL_UnlockAudioDevice
1416 */
1417extern DECLSPEC void SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
1418
1419/**
1420 * This function is a legacy means of unlocking the audio device.
1421 *
1422 * New programs might want to use SDL_UnlockAudioDevice() instead. This
1423 * function is equivalent to calling...
1424 *
1425 * ```c
1426 * SDL_UnlockAudioDevice(1);
1427 * ```
1428 *
1429 * ...and is only useful if you used the legacy SDL_OpenAudio() function.
1430 *
1431 * \since This function is available since SDL 2.0.0.
1432 *
1433 * \sa SDL_LockAudio
1434 * \sa SDL_UnlockAudioDevice
1435 */
1436extern DECLSPEC void SDLCALL SDL_UnlockAudio(void);
1437
1438/**
1439 * Use this function to unlock the audio callback function for a specified
1440 * device.
1441 *
1442 * This function should be paired with a previous SDL_LockAudioDevice() call.
1443 *
1444 * \param dev the ID of the device to be unlocked.
1445 *
1446 * \since This function is available since SDL 2.0.0.
1447 *
1448 * \sa SDL_LockAudioDevice
1449 */
1450extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
1451/* @} *//* Audio lock functions */
1452
1453/**
1454 * This function is a legacy means of closing the audio device.
1455 *
1456 * This function is equivalent to calling...
1457 *
1458 * ```c
1459 * SDL_CloseAudioDevice(1);
1460 * ```
1461 *
1462 * ...and is only useful if you used the legacy SDL_OpenAudio() function.
1463 *
1464 * \since This function is available since SDL 2.0.0.
1465 *
1466 * \sa SDL_OpenAudio
1467 */
1468extern DECLSPEC void SDLCALL SDL_CloseAudio(void);
1469
1470/**
1471 * Use this function to shut down audio processing and close the audio device.
1472 *
1473 * The application should close open audio devices once they are no longer
1474 * needed. Calling this function will wait until the device's audio callback
1475 * is not running, release the audio hardware and then clean up internal
1476 * state. No further audio will play from this device once this function
1477 * returns.
1478 *
1479 * This function may block briefly while pending audio data is played by the
1480 * hardware, so that applications don't drop the last buffer of data they
1481 * supplied.
1482 *
1483 * The device ID is invalid as soon as the device is closed, and is eligible
1484 * for reuse in a new SDL_OpenAudioDevice() call immediately.
1485 *
1486 * \param dev an audio device previously opened with SDL_OpenAudioDevice().
1487 *
1488 * \since This function is available since SDL 2.0.0.
1489 *
1490 * \sa SDL_OpenAudioDevice
1491 */
1492extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
1493
1494/* Ends C function definitions when using C++ */
1495#ifdef __cplusplus
1496}
1497#endif
1498#include "close_code.h"
1499
1500#endif /* SDL_audio_h_ */
1501
1502/* vi: set ts=4 sw=4 expandtab: */
int SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len)
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)
void SDL_AudioQuit(void)
SDL_AudioDeviceID SDL_OpenAudioDevice(const char *device, int iscapture, const SDL_AudioSpec *desired, SDL_AudioSpec *obtained, int allowed_changes)
int SDL_GetNumAudioDevices(int iscapture)
struct _SDL_AudioStream SDL_AudioStream
Definition SDL_audio.h:988
const char * SDL_GetAudioDriver(int index)
SDL_AudioStream * SDL_NewAudioStream(const SDL_AudioFormat src_format, const Uint8 src_channels, const int src_rate, const SDL_AudioFormat dst_format, const Uint8 dst_channels, const int dst_rate)
SDL_AudioStatus
Definition SDL_audio.h:682
@ SDL_AUDIO_STOPPED
Definition SDL_audio.h:683
@ SDL_AUDIO_PLAYING
Definition SDL_audio.h:684
@ SDL_AUDIO_PAUSED
Definition SDL_audio.h:685
void SDL_AudioStreamClear(SDL_AudioStream *stream)
#define SDL_AUDIOCVT_MAX_FILTERS
Definition SDL_audio.h:202
void SDL_CloseAudio(void)
void(* SDL_AudioFilter)(struct SDL_AudioCVT *cvt, SDL_AudioFormat format)
Definition SDL_audio.h:192
const char * SDL_GetAudioDeviceName(int index, int iscapture)
Uint16 SDL_AudioFormat
Definition SDL_audio.h:66
Uint32 SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len)
int SDL_GetNumAudioDrivers(void)
int SDL_AudioStreamFlush(SDL_AudioStream *stream)
void SDL_PauseAudioDevice(SDL_AudioDeviceID dev, int pause_on)
Uint32 SDL_AudioDeviceID
Definition SDL_audio.h:416
const char * SDL_GetCurrentAudioDriver(void)
void SDL_UnlockAudio(void)
int SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len)
void SDL_MixAudio(Uint8 *dst, const Uint8 *src, Uint32 len, int volume)
void SDL_ClearQueuedAudio(SDL_AudioDeviceID dev)
int SDL_GetDefaultAudioInfo(char **name, SDL_AudioSpec *spec, int iscapture)
void SDL_LockAudio(void)
void SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, int volume)
int SDL_AudioInit(const char *driver_name)
int SDL_BuildAudioCVT(SDL_AudioCVT *cvt, SDL_AudioFormat src_format, Uint8 src_channels, int src_rate, SDL_AudioFormat dst_format, Uint8 dst_channels, int dst_rate)
void(* SDL_AudioCallback)(void *userdata, Uint8 *stream, int len)
Definition SDL_audio.h:159
void SDL_PauseAudio(int pause_on)
SDL_AudioStatus SDL_GetAudioStatus(void)
Uint32 SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev)
#define SDL_AUDIOCVT_PACKED
Definition SDL_audio.h:225
int SDL_ConvertAudio(SDL_AudioCVT *cvt)
int SDL_GetAudioDeviceSpec(int index, int iscapture, SDL_AudioSpec *spec)
int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained)
SDL_AudioStatus SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev)
int SDL_AudioStreamAvailable(SDL_AudioStream *stream)
int SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len)
void SDL_LockAudioDevice(SDL_AudioDeviceID dev)
void SDL_FreeAudioStream(SDL_AudioStream *stream)
void SDL_UnlockAudioDevice(SDL_AudioDeviceID dev)
void SDL_FreeWAV(Uint8 *audio_buf)
uint8_t Uint8
Definition SDL_stdinc.h:203
uint16_t Uint16
Definition SDL_stdinc.h:217
uint32_t Uint32
Definition SDL_stdinc.h:231
A structure to hold a set of audio conversion filters and buffers.
Definition SDL_audio.h:229
Uint8 * buf
Definition SDL_audio.h:234
double len_ratio
Definition SDL_audio.h:238
SDL_AudioFormat src_format
Definition SDL_audio.h:231
SDL_AudioFormat dst_format
Definition SDL_audio.h:232
double rate_incr
Definition SDL_audio.h:233
SDL_AudioFilter filters[SDL_AUDIOCVT_MAX_FILTERS+1]
Definition SDL_audio.h:239
SDL_AudioCallback callback
Definition SDL_audio.h:186
Uint16 samples
Definition SDL_audio.h:183
Uint16 padding
Definition SDL_audio.h:184
SDL_AudioFormat format
Definition SDL_audio.h:180
void * userdata
Definition SDL_audio.h:187