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