SDL_audio.h 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /* !!! FIXME: several functions in here need Doxygen comments. */
  19. /**
  20. * \file SDL_audio.h
  21. *
  22. * Access to the raw audio mixing buffer for the SDL library.
  23. */
  24. #ifndef SDL_audio_h_
  25. #define SDL_audio_h_
  26. #include "SDL_stdinc.h"
  27. #include "SDL_error.h"
  28. #include "SDL_endian.h"
  29. #include "SDL_mutex.h"
  30. #include "SDL_thread.h"
  31. #include "SDL_rwops.h"
  32. #include "begin_code.h"
  33. /* Set up for C function definitions, even when using C++ */
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * \brief Audio format flags.
  39. *
  40. * These are what the 16 bits in SDL_AudioFormat currently mean...
  41. * (Unspecified bits are always zero).
  42. *
  43. * \verbatim
  44. ++-----------------------sample is signed if set
  45. ||
  46. || ++-----------sample is bigendian if set
  47. || ||
  48. || || ++---sample is float if set
  49. || || ||
  50. || || || +---sample bit size---+
  51. || || || | |
  52. 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
  53. \endverbatim
  54. *
  55. * There are macros in SDL 2.0 and later to query these bits.
  56. */
  57. typedef Uint16 SDL_AudioFormat;
  58. /**
  59. * \name Audio flags
  60. */
  61. /* @{ */
  62. #define SDL_AUDIO_MASK_BITSIZE (0xFF)
  63. #define SDL_AUDIO_MASK_DATATYPE (1<<8)
  64. #define SDL_AUDIO_MASK_ENDIAN (1<<12)
  65. #define SDL_AUDIO_MASK_SIGNED (1<<15)
  66. #define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
  67. #define SDL_AUDIO_ISFLOAT(x) (x & SDL_AUDIO_MASK_DATATYPE)
  68. #define SDL_AUDIO_ISBIGENDIAN(x) (x & SDL_AUDIO_MASK_ENDIAN)
  69. #define SDL_AUDIO_ISSIGNED(x) (x & SDL_AUDIO_MASK_SIGNED)
  70. #define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))
  71. #define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))
  72. #define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))
  73. /**
  74. * \name Audio format flags
  75. *
  76. * Defaults to LSB byte order.
  77. */
  78. /* @{ */
  79. #define AUDIO_U8 0x0008 /**< Unsigned 8-bit samples */
  80. #define AUDIO_S8 0x8008 /**< Signed 8-bit samples */
  81. #define AUDIO_U16LSB 0x0010 /**< Unsigned 16-bit samples */
  82. #define AUDIO_S16LSB 0x8010 /**< Signed 16-bit samples */
  83. #define AUDIO_U16MSB 0x1010 /**< As above, but big-endian byte order */
  84. #define AUDIO_S16MSB 0x9010 /**< As above, but big-endian byte order */
  85. #define AUDIO_U16 AUDIO_U16LSB
  86. #define AUDIO_S16 AUDIO_S16LSB
  87. /* @} */
  88. /**
  89. * \name int32 support
  90. */
  91. /* @{ */
  92. #define AUDIO_S32LSB 0x8020 /**< 32-bit integer samples */
  93. #define AUDIO_S32MSB 0x9020 /**< As above, but big-endian byte order */
  94. #define AUDIO_S32 AUDIO_S32LSB
  95. /* @} */
  96. /**
  97. * \name float32 support
  98. */
  99. /* @{ */
  100. #define AUDIO_F32LSB 0x8120 /**< 32-bit floating point samples */
  101. #define AUDIO_F32MSB 0x9120 /**< As above, but big-endian byte order */
  102. #define AUDIO_F32 AUDIO_F32LSB
  103. /* @} */
  104. /**
  105. * \name Native audio byte ordering
  106. */
  107. /* @{ */
  108. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  109. #define AUDIO_U16SYS AUDIO_U16LSB
  110. #define AUDIO_S16SYS AUDIO_S16LSB
  111. #define AUDIO_S32SYS AUDIO_S32LSB
  112. #define AUDIO_F32SYS AUDIO_F32LSB
  113. #else
  114. #define AUDIO_U16SYS AUDIO_U16MSB
  115. #define AUDIO_S16SYS AUDIO_S16MSB
  116. #define AUDIO_S32SYS AUDIO_S32MSB
  117. #define AUDIO_F32SYS AUDIO_F32MSB
  118. #endif
  119. /* @} */
  120. /**
  121. * \name Allow change flags
  122. *
  123. * Which audio format changes are allowed when opening a device.
  124. */
  125. /* @{ */
  126. #define SDL_AUDIO_ALLOW_FREQUENCY_CHANGE 0x00000001
  127. #define SDL_AUDIO_ALLOW_FORMAT_CHANGE 0x00000002
  128. #define SDL_AUDIO_ALLOW_CHANNELS_CHANGE 0x00000004
  129. #define SDL_AUDIO_ALLOW_SAMPLES_CHANGE 0x00000008
  130. #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)
  131. /* @} */
  132. /* @} *//* Audio flags */
  133. /**
  134. * This function is called when the audio device needs more data.
  135. *
  136. * \param userdata An application-specific parameter saved in
  137. * the SDL_AudioSpec structure
  138. * \param stream A pointer to the audio data buffer.
  139. * \param len The length of that buffer in bytes.
  140. *
  141. * Once the callback returns, the buffer will no longer be valid.
  142. * Stereo samples are stored in a LRLRLR ordering.
  143. *
  144. * You can choose to avoid callbacks and use SDL_QueueAudio() instead, if
  145. * you like. Just open your audio device with a NULL callback.
  146. */
  147. typedef void (SDLCALL * SDL_AudioCallback) (void *userdata, Uint8 * stream,
  148. int len);
  149. /**
  150. * The calculated values in this structure are calculated by SDL_OpenAudio().
  151. *
  152. * For multi-channel audio, the default SDL channel mapping is:
  153. * 2: FL FR (stereo)
  154. * 3: FL FR LFE (2.1 surround)
  155. * 4: FL FR BL BR (quad)
  156. * 5: FL FR FC BL BR (quad + center)
  157. * 6: FL FR FC LFE SL SR (5.1 surround - last two can also be BL BR)
  158. * 7: FL FR FC LFE BC SL SR (6.1 surround)
  159. * 8: FL FR FC LFE BL BR SL SR (7.1 surround)
  160. */
  161. typedef struct SDL_AudioSpec
  162. {
  163. int freq; /**< DSP frequency -- samples per second */
  164. SDL_AudioFormat format; /**< Audio data format */
  165. Uint8 channels; /**< Number of channels: 1 mono, 2 stereo */
  166. Uint8 silence; /**< Audio buffer silence value (calculated) */
  167. Uint16 samples; /**< Audio buffer size in sample FRAMES (total samples divided by channel count) */
  168. Uint16 padding; /**< Necessary for some compile environments */
  169. Uint32 size; /**< Audio buffer size in bytes (calculated) */
  170. SDL_AudioCallback callback; /**< Callback that feeds the audio device (NULL to use SDL_QueueAudio()). */
  171. void *userdata; /**< Userdata passed to callback (ignored for NULL callbacks). */
  172. } SDL_AudioSpec;
  173. struct SDL_AudioCVT;
  174. typedef void (SDLCALL * SDL_AudioFilter) (struct SDL_AudioCVT * cvt,
  175. SDL_AudioFormat format);
  176. /**
  177. * \brief Upper limit of filters in SDL_AudioCVT
  178. *
  179. * The maximum number of SDL_AudioFilter functions in SDL_AudioCVT is
  180. * currently limited to 9. The SDL_AudioCVT.filters array has 10 pointers,
  181. * one of which is the terminating NULL pointer.
  182. */
  183. #define SDL_AUDIOCVT_MAX_FILTERS 9
  184. /**
  185. * \struct SDL_AudioCVT
  186. * \brief A structure to hold a set of audio conversion filters and buffers.
  187. *
  188. * Note that various parts of the conversion pipeline can take advantage
  189. * of SIMD operations (like SSE2, for example). SDL_AudioCVT doesn't require
  190. * you to pass it aligned data, but can possibly run much faster if you
  191. * set both its (buf) field to a pointer that is aligned to 16 bytes, and its
  192. * (len) field to something that's a multiple of 16, if possible.
  193. */
  194. #ifdef __GNUC__
  195. /* This structure is 84 bytes on 32-bit architectures, make sure GCC doesn't
  196. pad it out to 88 bytes to guarantee ABI compatibility between compilers.
  197. vvv
  198. The next time we rev the ABI, make sure to size the ints and add padding.
  199. */
  200. #define SDL_AUDIOCVT_PACKED __attribute__((packed))
  201. #else
  202. #define SDL_AUDIOCVT_PACKED
  203. #endif
  204. /* */
  205. typedef struct SDL_AudioCVT
  206. {
  207. int needed; /**< Set to 1 if conversion possible */
  208. SDL_AudioFormat src_format; /**< Source audio format */
  209. SDL_AudioFormat dst_format; /**< Target audio format */
  210. double rate_incr; /**< Rate conversion increment */
  211. Uint8 *buf; /**< Buffer to hold entire audio data */
  212. int len; /**< Length of original audio buffer */
  213. int len_cvt; /**< Length of converted audio buffer */
  214. int len_mult; /**< buffer must be len*len_mult big */
  215. double len_ratio; /**< Given len, final size is len*len_ratio */
  216. SDL_AudioFilter filters[SDL_AUDIOCVT_MAX_FILTERS + 1]; /**< NULL-terminated list of filter functions */
  217. int filter_index; /**< Current audio conversion function */
  218. } SDL_AUDIOCVT_PACKED SDL_AudioCVT;
  219. /* Function prototypes */
  220. /**
  221. * \name Driver discovery functions
  222. *
  223. * These functions return the list of built in audio drivers, in the
  224. * order that they are normally initialized by default.
  225. */
  226. /* @{ */
  227. extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
  228. extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
  229. /* @} */
  230. /**
  231. * \name Initialization and cleanup
  232. *
  233. * \internal These functions are used internally, and should not be used unless
  234. * you have a specific need to specify the audio driver you want to
  235. * use. You should normally use SDL_Init() or SDL_InitSubSystem().
  236. */
  237. /* @{ */
  238. extern DECLSPEC int SDLCALL SDL_AudioInit(const char *driver_name);
  239. extern DECLSPEC void SDLCALL SDL_AudioQuit(void);
  240. /* @} */
  241. /**
  242. * Get the name of the current audio driver.
  243. *
  244. * The returned string points to internal static memory and thus never becomes
  245. * invalid, even if you quit the audio subsystem and initialize a new driver
  246. * (although such a case would return a different static string from another
  247. * call to this function, of course). As such, you should not modify or free
  248. * the returned string.
  249. *
  250. * \returns the name of the current audio driver or NULL if no driver has been
  251. * initialized.
  252. *
  253. * \since This function is available since SDL 2.0.0.
  254. *
  255. * \sa SDL_AudioInit
  256. */
  257. extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
  258. /**
  259. * This function is a legacy means of opening the audio device.
  260. *
  261. * This function remains for compatibility with SDL 1.2, but also because it's
  262. * slightly easier to use than the new functions in SDL 2.0. The new, more
  263. * powerful, and preferred way to do this is SDL_OpenAudioDevice().
  264. *
  265. * This function is roughly equivalent to:
  266. *
  267. * ```c++
  268. * SDL_OpenAudioDevice(NULL, 0, desired, obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
  269. * ```
  270. *
  271. * With two notable exceptions:
  272. *
  273. * - If `obtained` is NULL, we use `desired` (and allow no changes), which
  274. * means desired will be modified to have the correct values for silence,
  275. * etc, and SDL will convert any differences between your app's specific
  276. * request and the hardware behind the scenes.
  277. * - The return value is always success or failure, and not a device ID, which
  278. * means you can only have one device open at a time with this function.
  279. *
  280. * \param desired an SDL_AudioSpec structure representing the desired output
  281. * format. Please refer to the SDL_OpenAudioDevice documentation
  282. * for details on how to prepare this structure.
  283. * \param obtained an SDL_AudioSpec structure filled in with the actual
  284. * parameters, or NULL.
  285. * \returns This function opens the audio device with the desired parameters,
  286. * and returns 0 if successful, placing the actual hardware
  287. * parameters in the structure pointed to by `obtained`.
  288. *
  289. * If `obtained` is NULL, the audio data passed to the callback
  290. * function will be guaranteed to be in the requested format, and
  291. * will be automatically converted to the actual hardware audio
  292. * format if necessary. If `obtained` is NULL, `desired` will
  293. * have fields modified.
  294. *
  295. * This function returns a negative error code on failure to open the
  296. * audio device or failure to set up the audio thread; call
  297. * SDL_GetError() for more information.
  298. *
  299. * \sa SDL_CloseAudio
  300. * \sa SDL_LockAudio
  301. * \sa SDL_PauseAudio
  302. * \sa SDL_UnlockAudio
  303. */
  304. extern DECLSPEC int SDLCALL SDL_OpenAudio(SDL_AudioSpec * desired,
  305. SDL_AudioSpec * obtained);
  306. /**
  307. * SDL Audio Device IDs.
  308. *
  309. * A successful call to SDL_OpenAudio() is always device id 1, and legacy
  310. * SDL audio APIs assume you want this device ID. SDL_OpenAudioDevice() calls
  311. * always returns devices >= 2 on success. The legacy calls are good both
  312. * for backwards compatibility and when you don't care about multiple,
  313. * specific, or capture devices.
  314. */
  315. typedef Uint32 SDL_AudioDeviceID;
  316. /**
  317. * Get the number of built-in audio devices.
  318. *
  319. * This function is only valid after successfully initializing the audio
  320. * subsystem.
  321. *
  322. * Note that audio capture support is not implemented as of SDL 2.0.4, so the
  323. * `iscapture` parameter is for future expansion and should always be zero
  324. * for now.
  325. *
  326. * This function will return -1 if an explicit list of devices can't be
  327. * determined. Returning -1 is not an error. For example, if SDL is set up to
  328. * talk to a remote audio server, it can't list every one available on the
  329. * Internet, but it will still allow a specific host to be specified in
  330. * SDL_OpenAudioDevice().
  331. *
  332. * In many common cases, when this function returns a value <= 0, it can still
  333. * successfully open the default device (NULL for first argument of
  334. * SDL_OpenAudioDevice()).
  335. *
  336. * This function may trigger a complete redetect of available hardware. It
  337. * should not be called for each iteration of a loop, but rather once at the
  338. * start of a loop:
  339. *
  340. * ```c++
  341. * // Don't do this:
  342. * for (int i = 0; i < SDL_GetNumAudioDevices(0); i++)
  343. *
  344. * // do this instead:
  345. * const int count = SDL_GetNumAudioDevices(0);
  346. * for (int i = 0; i < count; ++i) { do_something_here(); }
  347. * ```
  348. *
  349. * \param iscapture zero to request playback devices, non-zero to request
  350. * recording devices
  351. * \returns the number of available devices exposed by the current driver or
  352. * -1 if an explicit list of devices can't be determined. A return
  353. * value of -1 does not necessarily mean an error condition.
  354. *
  355. * \since This function is available since SDL 2.0.0.
  356. *
  357. * \sa SDL_GetAudioDeviceName
  358. * \sa SDL_OpenAudioDevice
  359. */
  360. extern DECLSPEC int SDLCALL SDL_GetNumAudioDevices(int iscapture);
  361. /**
  362. * Get the human-readable name of a specific audio device.
  363. *
  364. * This function is only valid after successfully initializing the audio
  365. * subsystem. The values returned by this function reflect the latest call to
  366. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  367. * hardware.
  368. *
  369. * The string returned by this function is UTF-8 encoded, read-only, and
  370. * managed internally. You are not to free it. If you need to keep the string
  371. * for any length of time, you should make your own copy of it, as it will be
  372. * invalid next time any of several other SDL functions are called.
  373. *
  374. * \param index the index of the audio device; valid values range from 0 to
  375. * SDL_GetNumAudioDevices() - 1
  376. * \param iscapture non-zero to query the list of recording devices, zero to
  377. * query the list of output devices.
  378. * \returns the name of the audio device at the requested index, or NULL on
  379. * error.
  380. *
  381. * \sa SDL_GetNumAudioDevices
  382. */
  383. extern DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(int index,
  384. int iscapture);
  385. /**
  386. * Get the preferred audio format of a specific audio device.
  387. *
  388. * This function is only valid after a successfully initializing the audio
  389. * subsystem. The values returned by this function reflect the latest call to
  390. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  391. * hardware.
  392. *
  393. * `spec` will be filled with the sample rate, sample format, and channel
  394. * count. All other values in the structure are filled with 0. When the
  395. * supported struct members are 0, SDL was unable to get the property from the
  396. * backend.
  397. *
  398. * \param index the index of the audio device; valid values range from 0 to
  399. * SDL_GetNumAudioDevices() - 1
  400. * \param iscapture non-zero to query the list of recording devices, zero to
  401. * query the list of output devices.
  402. * \param spec The SDL_AudioSpec to be initialized by this function.
  403. * \returns 0 on success, nonzero on error
  404. *
  405. * \sa SDL_GetNumAudioDevices
  406. */
  407. extern DECLSPEC int SDLCALL SDL_GetAudioDeviceSpec(int index,
  408. int iscapture,
  409. SDL_AudioSpec *spec);
  410. /**
  411. * Open a specific audio device.
  412. *
  413. * SDL_OpenAudio(), unlike this function, always acts on device ID 1. As such,
  414. * this function will never return a 1 so as not to conflict with the legacy
  415. * function.
  416. *
  417. * Please note that SDL 2.0 before 2.0.5 did not support recording; as such,
  418. * this function would fail if `iscapture` was not zero. Starting with SDL
  419. * 2.0.5, recording is implemented and this value can be non-zero.
  420. *
  421. * Passing in a `device` name of NULL requests the most reasonable default
  422. * (and is equivalent to what SDL_OpenAudio() does to choose a device). The
  423. * `device` name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but
  424. * some drivers allow arbitrary and driver-specific strings, such as a
  425. * hostname/IP address for a remote audio server, or a filename in the
  426. * diskaudio driver.
  427. *
  428. * When filling in the desired audio spec structure:
  429. *
  430. * - `desired->freq` should be the frequency in sample-frames-per-second (Hz).
  431. * - `desired->format` should be the audio format (`AUDIO_S16SYS`, etc).
  432. * - `desired->samples` is the desired size of the audio buffer, in
  433. * _sample frames_ (with stereo output, two samples--left and right--would
  434. * make a single sample frame). This number should be a power of two, and
  435. * may be adjusted by the audio driver to a value more suitable for the
  436. * hardware. Good values seem to range between 512 and 8096 inclusive,
  437. * depending on the application and CPU speed. Smaller values reduce
  438. * latency, but can lead to underflow if the application is doing heavy
  439. * processing and cannot fill the audio buffer in time. Note that the
  440. * number of sample frames is directly related to time by the following
  441. * formula: `ms = (sampleframes*1000)/freq`
  442. * - `desired->size` is the size in _bytes_ of the audio buffer, and is
  443. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  444. * - `desired->silence` is the value used to set the buffer to silence,
  445. * and is calculated by SDL_OpenAudioDevice(). You don't initialize this.
  446. * - `desired->callback` should be set to a function that will be called
  447. * when the audio device is ready for more data. It is passed a pointer
  448. * to the audio buffer, and the length in bytes of the audio buffer.
  449. * This function usually runs in a separate thread, and so you should
  450. * protect data structures that it accesses by calling SDL_LockAudioDevice()
  451. * and SDL_UnlockAudioDevice() in your code. Alternately, you may pass a NULL
  452. * pointer here, and call SDL_QueueAudio() with some frequency, to queue
  453. * more audio samples to be played (or for capture devices, call
  454. * SDL_DequeueAudio() with some frequency, to obtain audio samples).
  455. * - `desired->userdata` is passed as the first parameter to your callback
  456. * function. If you passed a NULL callback, this value is ignored.
  457. *
  458. * `allowed_changes` can have the following flags OR'd together:
  459. *
  460. * - `SDL_AUDIO_ALLOW_FREQUENCY_CHANGE`
  461. * - `SDL_AUDIO_ALLOW_FORMAT_CHANGE`
  462. * - `SDL_AUDIO_ALLOW_CHANNELS_CHANGE`
  463. * - `SDL_AUDIO_ALLOW_ANY_CHANGE`
  464. *
  465. * These flags specify how SDL should behave when a device cannot offer a
  466. * specific feature. If the application requests a feature that the hardware
  467. * doesn't offer, SDL will always try to get the closest equivalent.
  468. *
  469. * For example, if you ask for float32 audio format, but the sound card only
  470. * supports int16, SDL will set the hardware to int16. If you had set
  471. * SDL_AUDIO_ALLOW_FORMAT_CHANGE, SDL will change the format in the
  472. * `obtained` structure. If that flag was *not* set, SDL will prepare to
  473. * convert your callback's float32 audio to int16 before feeding it to the
  474. * hardware and will keep the originally requested format in the `obtained`
  475. * structure.
  476. *
  477. * If your application can only handle one specific data format, pass a zero
  478. * for `allowed_changes` and let SDL transparently handle any differences.
  479. *
  480. * An opened audio device starts out paused, and should be enabled for playing
  481. * by calling SDL_PauseAudioDevice(devid, 0) when you are ready for your audio
  482. * callback function to be called. Since the audio driver may modify the
  483. * requested size of the audio buffer, you should allocate any local mixing
  484. * buffers after you open the audio device.
  485. *
  486. * The audio callback runs in a separate thread in most cases; you can prevent
  487. * race conditions between your callback and other threads without fully
  488. * pausing playback with SDL_LockAudioDevice(). For more information about the
  489. * callback, see SDL_AudioSpec.
  490. *
  491. * \param device a UTF-8 string reported by SDL_GetAudioDeviceName() or a
  492. * driver-specific name as appropriate. NULL requests the most
  493. * reasonable default device.
  494. * \param iscapture non-zero to specify a device should be opened for
  495. * recording, not playback
  496. * \param desired an SDL_AudioSpec structure representing the desired output
  497. * format; see SDL_OpenAudio() for more information
  498. * \param obtained an SDL_AudioSpec structure filled in with the actual output
  499. * format; see SDL_OpenAudio() for more information
  500. * \param allowed_changes 0, or one or more flags OR'd together
  501. * \returns a valid device ID that is > 0 on success or 0 on failure; call
  502. * SDL_GetError() for more information.
  503. *
  504. * For compatibility with SDL 1.2, this will never return 1, since
  505. * SDL reserves that ID for the legacy SDL_OpenAudio() function.
  506. *
  507. * \since This function is available since SDL 2.0.0.
  508. *
  509. * \sa SDL_CloseAudioDevice
  510. * \sa SDL_GetAudioDeviceName
  511. * \sa SDL_LockAudioDevice
  512. * \sa SDL_OpenAudio
  513. * \sa SDL_PauseAudioDevice
  514. * \sa SDL_UnlockAudioDevice
  515. */
  516. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(
  517. const char *device,
  518. int iscapture,
  519. const SDL_AudioSpec *desired,
  520. SDL_AudioSpec *obtained,
  521. int allowed_changes);
  522. /**
  523. * \name Audio state
  524. *
  525. * Get the current audio state.
  526. */
  527. /* @{ */
  528. typedef enum
  529. {
  530. SDL_AUDIO_STOPPED = 0,
  531. SDL_AUDIO_PLAYING,
  532. SDL_AUDIO_PAUSED
  533. } SDL_AudioStatus;
  534. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioStatus(void);
  535. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev);
  536. /* @} *//* Audio State */
  537. /**
  538. * \name Pause audio functions
  539. *
  540. * These functions pause and unpause the audio callback processing.
  541. * They should be called with a parameter of 0 after opening the audio
  542. * device to start playing sound. This is so you can safely initialize
  543. * data for your callback function after opening the audio device.
  544. * Silence will be written to the audio device during the pause.
  545. */
  546. /* @{ */
  547. extern DECLSPEC void SDLCALL SDL_PauseAudio(int pause_on);
  548. extern DECLSPEC void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev,
  549. int pause_on);
  550. /* @} *//* Pause audio functions */
  551. /**
  552. * Load the audio data of a WAVE file into memory.
  553. *
  554. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len`
  555. * to be valid pointers. The entire data portion of the file is then loaded
  556. * into memory and decoded if necessary.
  557. *
  558. * If `freesrc` is non-zero, the data source gets automatically closed and
  559. * freed before the function returns.
  560. *
  561. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  562. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits),
  563. * and A-law and mu-law (8 bits). Other formats are currently unsupported and
  564. * cause an error.
  565. *
  566. * If this function succeeds, the pointer returned by it is equal to `spec`
  567. * and the pointer to the audio data allocated by the function is written to
  568. * `audio_buf` and its length in bytes to `audio_len`. The SDL_AudioSpec
  569. * members `freq`, `channels`, and `format` are set to the values of the
  570. * audio data in the buffer. The `samples` member is set to a sane default
  571. * and all others are set to zero.
  572. *
  573. * It's necessary to use SDL_FreeWAV() to free the audio data returned in
  574. * `audio_buf` when it is no longer used.
  575. *
  576. * Because of the underspecification of the .WAV format, there are many
  577. * problematic files in the wild that cause issues with strict decoders. To
  578. * provide compatibility with these files, this decoder is lenient in regards
  579. * to the truncation of the file, the fact chunk, and the size of the RIFF
  580. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`, `SDL_HINT_WAVE_TRUNCATION`,
  581. * and `SDL_HINT_WAVE_FACT_CHUNK` can be used to tune the behavior of the
  582. * loading process.
  583. *
  584. * Any file that is invalid (due to truncation, corruption, or wrong values in
  585. * the headers), too big, or unsupported causes an error. Additionally, any
  586. * critical I/O error from the data source will terminate the loading process
  587. * with an error. The function returns NULL on error and in all cases (with the
  588. * exception of `src` being NULL), an appropriate error message will be set.
  589. *
  590. * It is required that the data source supports seeking.
  591. *
  592. * Example:
  593. * ```c++
  594. * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
  595. * ```
  596. *
  597. * Note that the SDL_LoadWAV macro does this same thing for you, but in a less
  598. * messy way:
  599. *
  600. * ```c++
  601. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  602. * ```
  603. *
  604. * \param src The data source for the WAVE data
  605. * \param freesrc If non-zero, SDL will _always_ free the data source
  606. * \param spec An SDL_AudioSpec that will be filled in with the wave file's
  607. * format details
  608. * \param audio_buf A pointer filled with the audio data, allocated by the function.
  609. * \param audio_len A pointer filled with the length of the audio data buffer in bytes
  610. * \returns This function, if successfully called, returns `spec`, which will
  611. * be filled with the audio data format of the wave source data.
  612. * `audio_buf` will be filled with a pointer to an allocated buffer
  613. * containing the audio data, and `audio_len` is filled with the
  614. * length of that audio buffer in bytes.
  615. *
  616. * This function returns NULL if the .WAV file cannot be opened, uses
  617. * an unknown data format, or is corrupt; call SDL_GetError() for
  618. * more information.
  619. *
  620. * When the application is done with the data returned in
  621. * `audio_buf`, it should call SDL_FreeWAV() to dispose of it.
  622. *
  623. * \sa SDL_FreeWAV
  624. * \sa SDL_LoadWAV
  625. */
  626. extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
  627. int freesrc,
  628. SDL_AudioSpec * spec,
  629. Uint8 ** audio_buf,
  630. Uint32 * audio_len);
  631. /**
  632. * Loads a WAV from a file.
  633. * Compatibility convenience function.
  634. */
  635. #define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
  636. SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
  637. /**
  638. * Free data previously allocated with SDL_LoadWAV() or SDL_LoadWAV_RW().
  639. *
  640. * After a WAVE file has been opened with SDL_LoadWAV() or SDL_LoadWAV_RW()
  641. * its data can eventually be freed with SDL_FreeWAV(). It is safe to call
  642. * this function with a NULL pointer.
  643. *
  644. * \param audio_buf a pointer to the buffer created by SDL_LoadWAV() or
  645. * SDL_LoadWAV_RW()
  646. *
  647. * \sa SDL_LoadWAV
  648. * \sa SDL_LoadWAV_RW
  649. */
  650. extern DECLSPEC void SDLCALL SDL_FreeWAV(Uint8 * audio_buf);
  651. /**
  652. * Initialize an SDL_AudioCVT structure for conversion.
  653. *
  654. * Before an SDL_AudioCVT structure can be used to convert audio data it must
  655. * be initialized with source and destination information.
  656. *
  657. * This function will zero out every field of the SDL_AudioCVT, so it must be
  658. * called before the application fills in the final buffer information.
  659. *
  660. * Once this function has returned successfully, and reported that a
  661. * conversion is necessary, the application fills in the rest of the fields in
  662. * SDL_AudioCVT, now that it knows how large a buffer it needs to allocate,
  663. * and then can call SDL_ConvertAudio() to complete the conversion.
  664. *
  665. * \param cvt an SDL_AudioCVT structure filled in with audio conversion
  666. * information
  667. * \param src_format the source format of the audio data; for more info see
  668. * SDL_AudioFormat
  669. * \param src_channels the number of channels in the source
  670. * \param src_rate the frequency (sample-frames-per-second) of the source
  671. * \param dst_format the destination format of the audio data; for more info
  672. * see SDL_AudioFormat
  673. * \param dst_channels the number of channels in the destination
  674. * \param dst_rate the frequency (sample-frames-per-second) of the
  675. * destination
  676. * \returns 1 if the audio filter is prepared, 0 if no conversion is needed,
  677. * or a negative error code on failure; call SDL_GetError() for more
  678. * information.
  679. *
  680. * \sa SDL_ConvertAudio
  681. */
  682. extern DECLSPEC int SDLCALL SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
  683. SDL_AudioFormat src_format,
  684. Uint8 src_channels,
  685. int src_rate,
  686. SDL_AudioFormat dst_format,
  687. Uint8 dst_channels,
  688. int dst_rate);
  689. /**
  690. * Convert audio data to a desired audio format.
  691. *
  692. * This function does the actual audio data conversion, after the application
  693. * has called SDL_BuildAudioCVT() to prepare the conversion information and
  694. * then filled in the buffer details.
  695. *
  696. * Once the application has initialized the `cvt` structure using
  697. * SDL_BuildAudioCVT(), allocated an audio buffer and filled it with audio
  698. * data in the source format, this function will convert the buffer, in-place,
  699. * to the desired format.
  700. *
  701. * The data conversion may go through several passes; any given pass may
  702. * possibly temporarily increase the size of the data. For example, SDL might
  703. * expand 16-bit data to 32 bits before resampling to a lower frequency,
  704. * shrinking the data size after having grown it briefly. Since the supplied
  705. * buffer will be both the source and destination, converting as necessary
  706. * in-place, the application must allocate a buffer that will fully contain
  707. * the data during its largest conversion pass. After SDL_BuildAudioCVT()
  708. * returns, the application should set the `cvt->len` field to the size, in
  709. * bytes, of the source data, and allocate a buffer that is
  710. * `cvt->len * cvt->len_mult` bytes long for the `buf` field.
  711. *
  712. * The source data should be copied into this buffer before the call to
  713. * SDL_ConvertAudio(). Upon successful return, this buffer will contain the
  714. * converted audio, and `cvt->len_cvt` will be the size of the converted data,
  715. * in bytes. Any bytes in the buffer past `cvt->len_cvt` are undefined once
  716. * this function returns.
  717. *
  718. * \param cvt an SDL_AudioCVT structure that was previously set up by
  719. * SDL_BuildAudioCVT().
  720. * \returns 0 if the conversion was completed successfully or a negative error
  721. * code on failure; call SDL_GetError() for more information.
  722. *
  723. * \sa SDL_BuildAudioCVT
  724. */
  725. extern DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT * cvt);
  726. /* SDL_AudioStream is a new audio conversion interface.
  727. The benefits vs SDL_AudioCVT:
  728. - it can handle resampling data in chunks without generating
  729. artifacts, when it doesn't have the complete buffer available.
  730. - it can handle incoming data in any variable size.
  731. - You push data as you have it, and pull it when you need it
  732. */
  733. /* this is opaque to the outside world. */
  734. struct _SDL_AudioStream;
  735. typedef struct _SDL_AudioStream SDL_AudioStream;
  736. /**
  737. * Create a new audio stream.
  738. *
  739. * \param src_format The format of the source audio
  740. * \param src_channels The number of channels of the source audio
  741. * \param src_rate The sampling rate of the source audio
  742. * \param dst_format The format of the desired audio output
  743. * \param dst_channels The number of channels of the desired audio output
  744. * \param dst_rate The sampling rate of the desired audio output
  745. * \returns 0 on success, or -1 on error.
  746. *
  747. * \sa SDL_AudioStreamPut
  748. * \sa SDL_AudioStreamGet
  749. * \sa SDL_AudioStreamAvailable
  750. * \sa SDL_AudioStreamFlush
  751. * \sa SDL_AudioStreamClear
  752. * \sa SDL_FreeAudioStream
  753. */
  754. extern DECLSPEC SDL_AudioStream * SDLCALL SDL_NewAudioStream(const SDL_AudioFormat src_format,
  755. const Uint8 src_channels,
  756. const int src_rate,
  757. const SDL_AudioFormat dst_format,
  758. const Uint8 dst_channels,
  759. const int dst_rate);
  760. /**
  761. * Add data to be converted/resampled to the stream.
  762. *
  763. * \param stream The stream the audio data is being added to
  764. * \param buf A pointer to the audio data to add
  765. * \param len The number of bytes to write to the stream
  766. * \returns 0 on success, or -1 on error.
  767. *
  768. * \sa SDL_NewAudioStream
  769. * \sa SDL_AudioStreamGet
  770. * \sa SDL_AudioStreamAvailable
  771. * \sa SDL_AudioStreamFlush
  772. * \sa SDL_AudioStreamClear
  773. * \sa SDL_FreeAudioStream
  774. */
  775. extern DECLSPEC int SDLCALL SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len);
  776. /**
  777. * Get converted/resampled data from the stream
  778. *
  779. * \param stream The stream the audio is being requested from
  780. * \param buf A buffer to fill with audio data
  781. * \param len The maximum number of bytes to fill
  782. * \returns the number of bytes read from the stream, or -1 on error
  783. *
  784. * \sa SDL_NewAudioStream
  785. * \sa SDL_AudioStreamPut
  786. * \sa SDL_AudioStreamAvailable
  787. * \sa SDL_AudioStreamFlush
  788. * \sa SDL_AudioStreamClear
  789. * \sa SDL_FreeAudioStream
  790. */
  791. extern DECLSPEC int SDLCALL SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len);
  792. /**
  793. * Get the number of converted/resampled bytes available. The stream may be
  794. * buffering data behind the scenes until it has enough to resample
  795. * correctly, so this number might be lower than what you expect, or even
  796. * be zero. Add more data or flush the stream if you need the data now.
  797. *
  798. * \sa SDL_NewAudioStream
  799. * \sa SDL_AudioStreamPut
  800. * \sa SDL_AudioStreamGet
  801. * \sa SDL_AudioStreamFlush
  802. * \sa SDL_AudioStreamClear
  803. * \sa SDL_FreeAudioStream
  804. */
  805. extern DECLSPEC int SDLCALL SDL_AudioStreamAvailable(SDL_AudioStream *stream);
  806. /**
  807. * Tell the stream that you're done sending data, and anything being buffered
  808. * should be converted/resampled and made available immediately.
  809. *
  810. * It is legal to add more data to a stream after flushing, but there will
  811. * be audio gaps in the output. Generally this is intended to signal the
  812. * end of input, so the complete output becomes available.
  813. *
  814. * \sa SDL_NewAudioStream
  815. * \sa SDL_AudioStreamPut
  816. * \sa SDL_AudioStreamGet
  817. * \sa SDL_AudioStreamAvailable
  818. * \sa SDL_AudioStreamClear
  819. * \sa SDL_FreeAudioStream
  820. */
  821. extern DECLSPEC int SDLCALL SDL_AudioStreamFlush(SDL_AudioStream *stream);
  822. /**
  823. * Clear any pending data in the stream without converting it
  824. *
  825. * \sa SDL_NewAudioStream
  826. * \sa SDL_AudioStreamPut
  827. * \sa SDL_AudioStreamGet
  828. * \sa SDL_AudioStreamAvailable
  829. * \sa SDL_AudioStreamFlush
  830. * \sa SDL_FreeAudioStream
  831. */
  832. extern DECLSPEC void SDLCALL SDL_AudioStreamClear(SDL_AudioStream *stream);
  833. /**
  834. * Free an audio stream
  835. *
  836. * \sa SDL_NewAudioStream
  837. * \sa SDL_AudioStreamPut
  838. * \sa SDL_AudioStreamGet
  839. * \sa SDL_AudioStreamAvailable
  840. * \sa SDL_AudioStreamFlush
  841. * \sa SDL_AudioStreamClear
  842. */
  843. extern DECLSPEC void SDLCALL SDL_FreeAudioStream(SDL_AudioStream *stream);
  844. #define SDL_MIX_MAXVOLUME 128
  845. /**
  846. * This function is a legacy means of mixing audio.
  847. *
  848. * This function is equivalent to calling
  849. *
  850. * ```c++
  851. * SDL_MixAudioFormat(dst, src, format, len, volume);
  852. * ```
  853. *
  854. * where `format` is the obtained format of the audio device from the legacy
  855. * SDL_OpenAudio() function.
  856. *
  857. * \param dst the destination for the mixed audio
  858. * \param src the source audio buffer to be mixed
  859. * \param len the length of the audio buffer in bytes
  860. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  861. * for full audio volume
  862. *
  863. * \sa SDL_MixAudioFormat
  864. */
  865. extern DECLSPEC void SDLCALL SDL_MixAudio(Uint8 * dst, const Uint8 * src,
  866. Uint32 len, int volume);
  867. /**
  868. * Mix audio data in a specified format.
  869. *
  870. * This takes an audio buffer `src` of `len` bytes of `format` data and
  871. * mixes it into `dst`, performing addition, volume adjustment, and overflow
  872. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  873. * `format` data.
  874. *
  875. * This is provided for convenience -- you can mix your own audio data.
  876. *
  877. * Do not use this function for mixing together more than two streams of
  878. * sample data. The output from repeated application of this function may be
  879. * distorted by clipping, because there is no accumulator with greater range
  880. * than the input (not to mention this being an inefficient way of doing it).
  881. *
  882. * It is a common misconception that this function is required to write audio
  883. * data to an output stream in an audio callback. While you can do that,
  884. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  885. * audio stream with a volume adjustment.
  886. *
  887. * \param dst the destination for the mixed audio
  888. * \param src the source audio buffer to be mixed
  889. * \param format the SDL_AudioFormat structure representing the desired audio
  890. * format
  891. * \param len the length of the audio buffer in bytes
  892. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  893. * for full audio volume
  894. */
  895. extern DECLSPEC void SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  896. const Uint8 * src,
  897. SDL_AudioFormat format,
  898. Uint32 len, int volume);
  899. /**
  900. * Queue more audio on non-callback devices.
  901. *
  902. * If you are looking to retrieve queued audio from a non-callback capture
  903. * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
  904. * -1 to signify an error if you use it with capture devices.
  905. *
  906. * SDL offers two ways to feed audio to the device: you can either supply a
  907. * callback that SDL triggers with some frequency to obtain more audio (pull
  908. * method), or you can supply no callback, and then SDL will expect you to
  909. * supply data at regular intervals (push method) with this function.
  910. *
  911. * There are no limits on the amount of data you can queue, short of
  912. * exhaustion of address space. Queued data will drain to the device as
  913. * necessary without further intervention from you. If the device needs audio
  914. * but there is not enough queued, it will play silence to make up the
  915. * difference. This means you will have skips in your audio playback if you
  916. * aren't routinely queueing sufficient data.
  917. *
  918. * This function copies the supplied data, so you are safe to free it when the
  919. * function returns. This function is thread-safe, but queueing to the same
  920. * device from two threads at once does not promise which buffer will be
  921. * queued first.
  922. *
  923. * You may not queue audio on a device that is using an application-supplied
  924. * callback; doing so returns an error. You have to use the audio callback or
  925. * queue audio with this function, but not both.
  926. *
  927. * You should not call SDL_LockAudio() on the device before queueing; SDL
  928. * handles locking internally for this function.
  929. *
  930. * \param dev the device ID to which we will queue audio
  931. * \param data the data to queue to the device for later playback
  932. * \param len the number of bytes (not samples!) to which `data` points
  933. * \returns 0 on success or a negative error code on failure; call
  934. * SDL_GetError() for more information.
  935. *
  936. * \since This function is available since SDL 2.0.4.
  937. *
  938. * \sa SDL_ClearQueuedAudio
  939. * \sa SDL_GetQueuedAudioSize
  940. */
  941. extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
  942. /**
  943. * Dequeue more audio on non-callback devices.
  944. *
  945. * If you are looking to queue audio for output on a non-callback playback
  946. * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
  947. * return 0 if you use it with playback devices.
  948. *
  949. * SDL offers two ways to retrieve audio from a capture device: you can either
  950. * supply a callback that SDL triggers with some frequency as the device
  951. * records more audio data, (push method), or you can supply no callback, and
  952. * then SDL will expect you to retrieve data at regular intervals (pull
  953. * method) with this function.
  954. *
  955. * There are no limits on the amount of data you can queue, short of
  956. * exhaustion of address space. Data from the device will keep queuing as
  957. * necessary without further intervention from you. This means you will
  958. * eventually run out of memory if you aren't routinely dequeueing data.
  959. *
  960. * Capture devices will not queue data when paused; if you are expecting to
  961. * not need captured audio for some length of time, use SDL_PauseAudioDevice()
  962. * to stop the capture device from queueing more data. This can be useful
  963. * during, say, level loading times. When unpaused, capture devices will start
  964. * queueing data from that point, having flushed any capturable data available
  965. * while paused.
  966. *
  967. * This function is thread-safe, but dequeueing from the same device from two
  968. * threads at once does not promise which thread will dequeue data first.
  969. *
  970. * You may not dequeue audio from a device that is using an
  971. * application-supplied callback; doing so returns an error. You have to use
  972. * the audio callback, or dequeue audio with this function, but not both.
  973. *
  974. * You should not call SDL_LockAudio() on the device before dequeueing; SDL
  975. * handles locking internally for this function.
  976. *
  977. * \param dev the device ID from which we will dequeue audio
  978. * \param data a pointer into where audio data should be copied
  979. * \param len the number of bytes (not samples!) to which (data) points
  980. * \returns number of bytes dequeued, which could be less than requested; call
  981. * SDL_GetError() for more information.
  982. *
  983. * \since This function is available since SDL 2.0.5.
  984. *
  985. * \sa SDL_ClearQueuedAudio
  986. * \sa SDL_GetQueuedAudioSize
  987. */
  988. extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
  989. /**
  990. * Get the number of bytes of still-queued audio.
  991. *
  992. * For playback devices: this is the number of bytes that have been queued
  993. * for playback with SDL_QueueAudio(), but have not yet been sent to the
  994. * hardware.
  995. *
  996. * Once we've sent it to the hardware, this function can not decide the exact
  997. * byte boundary of what has been played. It's possible that we just gave the
  998. * hardware several kilobytes right before you called this function, but it
  999. * hasn't played any of it yet, or maybe half of it, etc.
  1000. *
  1001. * For capture devices, this is the number of bytes that have been captured by
  1002. * the device and are waiting for you to dequeue. This number may grow at any
  1003. * time, so this only informs of the lower-bound of available data.
  1004. *
  1005. * You may not queue or dequeue audio on a device that is using an
  1006. * application-supplied callback; calling this function on such a device
  1007. * always returns 0. You have to use the audio callback or queue audio, but
  1008. * not both.
  1009. *
  1010. * You should not call SDL_LockAudio() on the device before querying; SDL
  1011. * handles locking internally for this function.
  1012. *
  1013. * \param dev the device ID of which we will query queued audio size
  1014. * \returns the number of bytes (not samples!) of queued audio.
  1015. *
  1016. * \since This function is available since SDL 2.0.4.
  1017. *
  1018. * \sa SDL_ClearQueuedAudio
  1019. * \sa SDL_QueueAudio
  1020. * \sa SDL_DequeueAudio
  1021. */
  1022. extern DECLSPEC Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev);
  1023. /**
  1024. * Drop any queued audio data waiting to be sent to the hardware.
  1025. *
  1026. * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
  1027. * output devices, the hardware will start playing silence if more audio isn't
  1028. * queued. For capture devices, the hardware will start filling the empty
  1029. * queue with new data if the capture device isn't paused.
  1030. *
  1031. * This will not prevent playback of queued audio that's already been sent to
  1032. * the hardware, as we can not undo that, so expect there to be some fraction
  1033. * of a second of audio that might still be heard. This can be useful if you
  1034. * want to, say, drop any pending music or any unprocessed microphone input
  1035. * during a level change in your game.
  1036. *
  1037. * You may not queue or dequeue audio on a device that is using an
  1038. * application-supplied callback; calling this function on such a device
  1039. * always returns 0. You have to use the audio callback or queue audio, but
  1040. * not both.
  1041. *
  1042. * You should not call SDL_LockAudio() on the device before clearing the
  1043. * queue; SDL handles locking internally for this function.
  1044. *
  1045. * This function always succeeds and thus returns void.
  1046. *
  1047. * \param dev the device ID of which to clear the audio queue
  1048. *
  1049. * \since This function is available since SDL 2.0.4.
  1050. *
  1051. * \sa SDL_GetQueuedAudioSize
  1052. * \sa SDL_QueueAudio
  1053. * \sa SDL_DequeueAudio
  1054. */
  1055. extern DECLSPEC void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
  1056. /**
  1057. * \name Audio lock functions
  1058. *
  1059. * The lock manipulated by these functions protects the callback function.
  1060. * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
  1061. * the callback function is not running. Do not call these from the callback
  1062. * function or you will cause deadlock.
  1063. */
  1064. /* @{ */
  1065. extern DECLSPEC void SDLCALL SDL_LockAudio(void);
  1066. extern DECLSPEC void SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
  1067. extern DECLSPEC void SDLCALL SDL_UnlockAudio(void);
  1068. extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
  1069. /* @} *//* Audio lock functions */
  1070. /**
  1071. * This function is a legacy means of closing the audio device.
  1072. *
  1073. * This function is equivalent to calling
  1074. *
  1075. * ```c++
  1076. * SDL_CloseAudioDevice(1);
  1077. * ```
  1078. *
  1079. * and is only useful if you used the legacy SDL_OpenAudio() function.
  1080. *
  1081. * \sa SDL_OpenAudio
  1082. */
  1083. extern DECLSPEC void SDLCALL SDL_CloseAudio(void);
  1084. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
  1085. /* Ends C function definitions when using C++ */
  1086. #ifdef __cplusplus
  1087. }
  1088. #endif
  1089. #include "close_code.h"
  1090. #endif /* SDL_audio_h_ */
  1091. /* vi: set ts=4 sw=4 expandtab: */