SDL_audio.h 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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
  282. * documentation 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 0 if successful, placing the actual hardware parameters in the
  286. * structure pointed to by `obtained`.
  287. *
  288. * If `obtained` is NULL, the audio data passed to the callback
  289. * function will be guaranteed to be in the requested format, and
  290. * will be automatically converted to the actual hardware audio
  291. * format if necessary. If `obtained` is NULL, `desired` will have
  292. * fields modified.
  293. *
  294. * This function returns a negative error code on failure to open the
  295. * audio device or failure to set up the audio thread; call
  296. * SDL_GetError() for more information.
  297. *
  298. * \sa SDL_CloseAudio
  299. * \sa SDL_LockAudio
  300. * \sa SDL_PauseAudio
  301. * \sa SDL_UnlockAudio
  302. */
  303. extern DECLSPEC int SDLCALL SDL_OpenAudio(SDL_AudioSpec * desired,
  304. SDL_AudioSpec * obtained);
  305. /**
  306. * SDL Audio Device IDs.
  307. *
  308. * A successful call to SDL_OpenAudio() is always device id 1, and legacy
  309. * SDL audio APIs assume you want this device ID. SDL_OpenAudioDevice() calls
  310. * always returns devices >= 2 on success. The legacy calls are good both
  311. * for backwards compatibility and when you don't care about multiple,
  312. * specific, or capture devices.
  313. */
  314. typedef Uint32 SDL_AudioDeviceID;
  315. /**
  316. * Get the number of built-in audio devices.
  317. *
  318. * This function is only valid after successfully initializing the audio
  319. * subsystem.
  320. *
  321. * Note that audio capture support is not implemented as of SDL 2.0.4, so the
  322. * `iscapture` parameter is for future expansion and should always be zero for
  323. * now.
  324. *
  325. * This function will return -1 if an explicit list of devices can't be
  326. * determined. Returning -1 is not an error. For example, if SDL is set up to
  327. * talk to a remote audio server, it can't list every one available on the
  328. * Internet, but it will still allow a specific host to be specified in
  329. * SDL_OpenAudioDevice().
  330. *
  331. * In many common cases, when this function returns a value <= 0, it can still
  332. * successfully open the default device (NULL for first argument of
  333. * SDL_OpenAudioDevice()).
  334. *
  335. * This function may trigger a complete redetect of available hardware. It
  336. * should not be called for each iteration of a loop, but rather once at the
  337. * start of a loop:
  338. *
  339. * ```c++
  340. * // Don't do this:
  341. * for (int i = 0; i < SDL_GetNumAudioDevices(0); i++)
  342. *
  343. * // do this instead:
  344. * const int count = SDL_GetNumAudioDevices(0);
  345. * for (int i = 0; i < count; ++i) { do_something_here(); }
  346. * ```
  347. *
  348. * \param iscapture zero to request playback devices, non-zero to request
  349. * recording devices
  350. * \returns the number of available devices exposed by the current driver or
  351. * -1 if an explicit list of devices can't be determined. A return
  352. * value of -1 does not necessarily mean an error condition.
  353. *
  354. * \since This function is available since SDL 2.0.0.
  355. *
  356. * \sa SDL_GetAudioDeviceName
  357. * \sa SDL_OpenAudioDevice
  358. */
  359. extern DECLSPEC int SDLCALL SDL_GetNumAudioDevices(int iscapture);
  360. /**
  361. * Get the human-readable name of a specific audio device.
  362. *
  363. * This function is only valid after successfully initializing the audio
  364. * subsystem. The values returned by this function reflect the latest call to
  365. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  366. * hardware.
  367. *
  368. * The string returned by this function is UTF-8 encoded, read-only, and
  369. * managed internally. You are not to free it. If you need to keep the string
  370. * for any length of time, you should make your own copy of it, as it will be
  371. * invalid next time any of several other SDL functions are called.
  372. *
  373. * \param index the index of the audio device; valid values range from 0 to
  374. * SDL_GetNumAudioDevices() - 1
  375. * \param iscapture non-zero to query the list of recording devices, zero to
  376. * query the list of output devices.
  377. * \returns the name of the audio device at the requested index, or NULL on
  378. * error.
  379. *
  380. * \sa SDL_GetNumAudioDevices
  381. */
  382. extern DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(int index,
  383. int iscapture);
  384. /**
  385. * Get the preferred audio format of a specific audio device.
  386. *
  387. * This function is only valid after a successfully initializing the audio
  388. * subsystem. The values returned by this function reflect the latest call to
  389. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  390. * hardware.
  391. *
  392. * `spec` will be filled with the sample rate, sample format, and channel
  393. * count. All other values in the structure are filled with 0. When the
  394. * supported struct members are 0, SDL was unable to get the property from the
  395. * backend.
  396. *
  397. * \param index the index of the audio device; valid values range from 0 to
  398. * SDL_GetNumAudioDevices() - 1
  399. * \param iscapture non-zero to query the list of recording devices, zero to
  400. * query the list of output devices.
  401. * \param spec The SDL_AudioSpec to be initialized by this function.
  402. * \returns 0 on success, nonzero on error
  403. *
  404. * \sa SDL_GetNumAudioDevices
  405. */
  406. extern DECLSPEC int SDLCALL SDL_GetAudioDeviceSpec(int index,
  407. int iscapture,
  408. SDL_AudioSpec *spec);
  409. /**
  410. * Open a specific audio device.
  411. *
  412. * SDL_OpenAudio(), unlike this function, always acts on device ID 1. As such,
  413. * this function will never return a 1 so as not to conflict with the legacy
  414. * function.
  415. *
  416. * Please note that SDL 2.0 before 2.0.5 did not support recording; as such,
  417. * this function would fail if `iscapture` was not zero. Starting with SDL
  418. * 2.0.5, recording is implemented and this value can be non-zero.
  419. *
  420. * Passing in a `device` name of NULL requests the most reasonable default
  421. * (and is equivalent to what SDL_OpenAudio() does to choose a device). The
  422. * `device` name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but
  423. * some drivers allow arbitrary and driver-specific strings, such as a
  424. * hostname/IP address for a remote audio server, or a filename in the
  425. * diskaudio driver.
  426. *
  427. * When filling in the desired audio spec structure:
  428. *
  429. * - `desired->freq` should be the frequency in sample-frames-per-second (Hz).
  430. * - `desired->format` should be the audio format (`AUDIO_S16SYS`, etc).
  431. * - `desired->samples` is the desired size of the audio buffer, in _sample
  432. * frames_ (with stereo output, two samples--left and right--would make a
  433. * single sample frame). This number should be a power of two, and may be
  434. * adjusted by the audio driver to a value more suitable for the hardware.
  435. * Good values seem to range between 512 and 8096 inclusive, depending on
  436. * the application and CPU speed. Smaller values reduce latency, but can
  437. * lead to underflow if the application is doing heavy processing and cannot
  438. * fill the audio buffer in time. Note that the number of sample frames is
  439. * directly related to time by the following formula: `ms =
  440. * (sampleframes*1000)/freq`
  441. * - `desired->size` is the size in _bytes_ of the audio buffer, and is
  442. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  443. * - `desired->silence` is the value used to set the buffer to silence, and is
  444. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  445. * - `desired->callback` should be set to a function that will be called when
  446. * the audio device is ready for more data. It is passed a pointer to the
  447. * audio buffer, and the length in bytes of the audio buffer. This function
  448. * usually runs in a separate thread, and so you should protect data
  449. * structures that it accesses by calling SDL_LockAudioDevice() and
  450. * SDL_UnlockAudioDevice() in your code. Alternately, you may pass a NULL
  451. * pointer here, and call SDL_QueueAudio() with some frequency, to queue
  452. * more audio samples to be played (or for capture devices, call
  453. * SDL_DequeueAudio() with some frequency, to obtain audio samples).
  454. * - `desired->userdata` is passed as the first parameter to your callback
  455. * function. If you passed a NULL callback, this value is ignored.
  456. *
  457. * `allowed_changes` can have the following flags OR'd together:
  458. *
  459. * - `SDL_AUDIO_ALLOW_FREQUENCY_CHANGE`
  460. * - `SDL_AUDIO_ALLOW_FORMAT_CHANGE`
  461. * - `SDL_AUDIO_ALLOW_CHANNELS_CHANGE`
  462. * - `SDL_AUDIO_ALLOW_ANY_CHANGE`
  463. *
  464. * These flags specify how SDL should behave when a device cannot offer a
  465. * specific feature. If the application requests a feature that the hardware
  466. * doesn't offer, SDL will always try to get the closest equivalent.
  467. *
  468. * For example, if you ask for float32 audio format, but the sound card only
  469. * supports int16, SDL will set the hardware to int16. If you had set
  470. * SDL_AUDIO_ALLOW_FORMAT_CHANGE, SDL will change the format in the `obtained`
  471. * structure. If that flag was *not* set, SDL will prepare to convert your
  472. * callback's float32 audio to int16 before feeding it to the hardware and
  473. * will keep the originally requested format in the `obtained` structure.
  474. *
  475. * If your application can only handle one specific data format, pass a zero
  476. * for `allowed_changes` and let SDL transparently handle any differences.
  477. *
  478. * An opened audio device starts out paused, and should be enabled for playing
  479. * by calling SDL_PauseAudioDevice(devid, 0) when you are ready for your audio
  480. * callback function to be called. Since the audio driver may modify the
  481. * requested size of the audio buffer, you should allocate any local mixing
  482. * buffers after you open the audio device.
  483. *
  484. * The audio callback runs in a separate thread in most cases; you can prevent
  485. * race conditions between your callback and other threads without fully
  486. * pausing playback with SDL_LockAudioDevice(). For more information about the
  487. * callback, see SDL_AudioSpec.
  488. *
  489. * \param device a UTF-8 string reported by SDL_GetAudioDeviceName() or a
  490. * driver-specific name as appropriate. NULL requests the most
  491. * reasonable default device.
  492. * \param iscapture non-zero to specify a device should be opened for
  493. * recording, not playback
  494. * \param desired an SDL_AudioSpec structure representing the desired output
  495. * format; see SDL_OpenAudio() for more information
  496. * \param obtained an SDL_AudioSpec structure filled in with the actual output
  497. * format; see SDL_OpenAudio() for more information
  498. * \param allowed_changes 0, or one or more flags OR'd together
  499. * \returns a valid device ID that is > 0 on success or 0 on failure; call
  500. * SDL_GetError() for more information.
  501. *
  502. * For compatibility with SDL 1.2, this will never return 1, since
  503. * SDL reserves that ID for the legacy SDL_OpenAudio() function.
  504. *
  505. * \since This function is available since SDL 2.0.0.
  506. *
  507. * \sa SDL_CloseAudioDevice
  508. * \sa SDL_GetAudioDeviceName
  509. * \sa SDL_LockAudioDevice
  510. * \sa SDL_OpenAudio
  511. * \sa SDL_PauseAudioDevice
  512. * \sa SDL_UnlockAudioDevice
  513. */
  514. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(
  515. const char *device,
  516. int iscapture,
  517. const SDL_AudioSpec *desired,
  518. SDL_AudioSpec *obtained,
  519. int allowed_changes);
  520. /**
  521. * \name Audio state
  522. *
  523. * Get the current audio state.
  524. */
  525. /* @{ */
  526. typedef enum
  527. {
  528. SDL_AUDIO_STOPPED = 0,
  529. SDL_AUDIO_PLAYING,
  530. SDL_AUDIO_PAUSED
  531. } SDL_AudioStatus;
  532. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioStatus(void);
  533. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev);
  534. /* @} *//* Audio State */
  535. /**
  536. * \name Pause audio functions
  537. *
  538. * These functions pause and unpause the audio callback processing.
  539. * They should be called with a parameter of 0 after opening the audio
  540. * device to start playing sound. This is so you can safely initialize
  541. * data for your callback function after opening the audio device.
  542. * Silence will be written to the audio device during the pause.
  543. */
  544. /* @{ */
  545. extern DECLSPEC void SDLCALL SDL_PauseAudio(int pause_on);
  546. extern DECLSPEC void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev,
  547. int pause_on);
  548. /* @} *//* Pause audio functions */
  549. /**
  550. * Load the audio data of a WAVE file into memory.
  551. *
  552. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  553. * be valid pointers. The entire data portion of the file is then loaded into
  554. * memory and decoded if necessary.
  555. *
  556. * If `freesrc` is non-zero, the data source gets automatically closed and
  557. * freed before the function returns.
  558. *
  559. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  560. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  561. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  562. * cause an error.
  563. *
  564. * If this function succeeds, the pointer returned by it is equal to `spec`
  565. * and the pointer to the audio data allocated by the function is written to
  566. * `audio_buf` and its length in bytes to `audio_len`. The SDL_AudioSpec
  567. * members `freq`, `channels`, and `format` are set to the values of the audio
  568. * data in the buffer. The `samples` member is set to a sane default and all
  569. * others are set to zero.
  570. *
  571. * It's necessary to use SDL_FreeWAV() to free the audio data returned in
  572. * `audio_buf` when it is no longer used.
  573. *
  574. * Because of the underspecification of the .WAV format, there are many
  575. * problematic files in the wild that cause issues with strict decoders. To
  576. * provide compatibility with these files, this decoder is lenient in regards
  577. * to the truncation of the file, the fact chunk, and the size of the RIFF
  578. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  579. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  580. * tune the behavior of the loading process.
  581. *
  582. * Any file that is invalid (due to truncation, corruption, or wrong values in
  583. * the headers), too big, or unsupported causes an error. Additionally, any
  584. * critical I/O error from the data source will terminate the loading process
  585. * with an error. The function returns NULL on error and in all cases (with
  586. * the exception of `src` being NULL), an appropriate error message will be
  587. * set.
  588. *
  589. * It is required that the data source supports seeking.
  590. *
  591. * Example:
  592. *
  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
  609. * function.
  610. * \param audio_len A pointer filled with the length of the audio data buffer
  611. * in bytes
  612. * \returns This function, if successfully called, returns `spec`, which will
  613. * be filled with the audio data format of the wave source data.
  614. * `audio_buf` will be filled with a pointer to an allocated buffer
  615. * containing the audio data, and `audio_len` is filled with the
  616. * length of that audio buffer in bytes.
  617. *
  618. * This function returns NULL if the .WAV file cannot be opened, uses
  619. * an unknown data format, or is corrupt; call SDL_GetError() for
  620. * more information.
  621. *
  622. * When the application is done with the data returned in
  623. * `audio_buf`, it should call SDL_FreeWAV() to dispose of it.
  624. *
  625. * \sa SDL_FreeWAV
  626. * \sa SDL_LoadWAV
  627. */
  628. extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
  629. int freesrc,
  630. SDL_AudioSpec * spec,
  631. Uint8 ** audio_buf,
  632. Uint32 * audio_len);
  633. /**
  634. * Loads a WAV from a file.
  635. * Compatibility convenience function.
  636. */
  637. #define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
  638. SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
  639. /**
  640. * Free data previously allocated with SDL_LoadWAV() or SDL_LoadWAV_RW().
  641. *
  642. * After a WAVE file has been opened with SDL_LoadWAV() or SDL_LoadWAV_RW()
  643. * its data can eventually be freed with SDL_FreeWAV(). It is safe to call
  644. * this function with a NULL pointer.
  645. *
  646. * \param audio_buf a pointer to the buffer created by SDL_LoadWAV() or
  647. * SDL_LoadWAV_RW()
  648. *
  649. * \sa SDL_LoadWAV
  650. * \sa SDL_LoadWAV_RW
  651. */
  652. extern DECLSPEC void SDLCALL SDL_FreeWAV(Uint8 * audio_buf);
  653. /**
  654. * Initialize an SDL_AudioCVT structure for conversion.
  655. *
  656. * Before an SDL_AudioCVT structure can be used to convert audio data it must
  657. * be initialized with source and destination information.
  658. *
  659. * This function will zero out every field of the SDL_AudioCVT, so it must be
  660. * called before the application fills in the final buffer information.
  661. *
  662. * Once this function has returned successfully, and reported that a
  663. * conversion is necessary, the application fills in the rest of the fields in
  664. * SDL_AudioCVT, now that it knows how large a buffer it needs to allocate,
  665. * and then can call SDL_ConvertAudio() to complete the conversion.
  666. *
  667. * \param cvt an SDL_AudioCVT structure filled in with audio conversion
  668. * information
  669. * \param src_format the source format of the audio data; for more info see
  670. * SDL_AudioFormat
  671. * \param src_channels the number of channels in the source
  672. * \param src_rate the frequency (sample-frames-per-second) of the source
  673. * \param dst_format the destination format of the audio data; for more info
  674. * see SDL_AudioFormat
  675. * \param dst_channels the number of channels in the destination
  676. * \param dst_rate the frequency (sample-frames-per-second) of the destination
  677. * \returns 1 if the audio filter is prepared, 0 if no conversion is needed,
  678. * or a negative error code on failure; call SDL_GetError() for more
  679. * information.
  680. *
  681. * \sa SDL_ConvertAudio
  682. */
  683. extern DECLSPEC int SDLCALL SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
  684. SDL_AudioFormat src_format,
  685. Uint8 src_channels,
  686. int src_rate,
  687. SDL_AudioFormat dst_format,
  688. Uint8 dst_channels,
  689. int dst_rate);
  690. /**
  691. * Convert audio data to a desired audio format.
  692. *
  693. * This function does the actual audio data conversion, after the application
  694. * has called SDL_BuildAudioCVT() to prepare the conversion information and
  695. * then filled in the buffer details.
  696. *
  697. * Once the application has initialized the `cvt` structure using
  698. * SDL_BuildAudioCVT(), allocated an audio buffer and filled it with audio
  699. * data in the source format, this function will convert the buffer, in-place,
  700. * to the desired format.
  701. *
  702. * The data conversion may go through several passes; any given pass may
  703. * possibly temporarily increase the size of the data. For example, SDL might
  704. * expand 16-bit data to 32 bits before resampling to a lower frequency,
  705. * shrinking the data size after having grown it briefly. Since the supplied
  706. * buffer will be both the source and destination, converting as necessary
  707. * in-place, the application must allocate a buffer that will fully contain
  708. * the data during its largest conversion pass. After SDL_BuildAudioCVT()
  709. * returns, the application should set the `cvt->len` field to the size, in
  710. * bytes, of the source data, and allocate a buffer that is `cvt->len *
  711. * cvt->len_mult` bytes long for the `buf` field.
  712. *
  713. * The source data should be copied into this buffer before the call to
  714. * SDL_ConvertAudio(). Upon successful return, this buffer will contain the
  715. * converted audio, and `cvt->len_cvt` will be the size of the converted data,
  716. * in bytes. Any bytes in the buffer past `cvt->len_cvt` are undefined once
  717. * this function returns.
  718. *
  719. * \param cvt an SDL_AudioCVT structure that was previously set up by
  720. * SDL_BuildAudioCVT().
  721. * \returns 0 if the conversion was completed successfully or a negative error
  722. * code on failure; call SDL_GetError() for more information.
  723. *
  724. * \sa SDL_BuildAudioCVT
  725. */
  726. extern DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT * cvt);
  727. /* SDL_AudioStream is a new audio conversion interface.
  728. The benefits vs SDL_AudioCVT:
  729. - it can handle resampling data in chunks without generating
  730. artifacts, when it doesn't have the complete buffer available.
  731. - it can handle incoming data in any variable size.
  732. - You push data as you have it, and pull it when you need it
  733. */
  734. /* this is opaque to the outside world. */
  735. struct _SDL_AudioStream;
  736. typedef struct _SDL_AudioStream SDL_AudioStream;
  737. /**
  738. * Create a new audio stream.
  739. *
  740. * \param src_format The format of the source audio
  741. * \param src_channels The number of channels of the source audio
  742. * \param src_rate The sampling rate of the source audio
  743. * \param dst_format The format of the desired audio output
  744. * \param dst_channels The number of channels of the desired audio output
  745. * \param dst_rate The sampling rate of the desired audio output
  746. * \returns 0 on success, or -1 on error.
  747. *
  748. * \sa SDL_AudioStreamPut
  749. * \sa SDL_AudioStreamGet
  750. * \sa SDL_AudioStreamAvailable
  751. * \sa SDL_AudioStreamFlush
  752. * \sa SDL_AudioStreamClear
  753. * \sa SDL_FreeAudioStream
  754. */
  755. extern DECLSPEC SDL_AudioStream * SDLCALL SDL_NewAudioStream(const SDL_AudioFormat src_format,
  756. const Uint8 src_channels,
  757. const int src_rate,
  758. const SDL_AudioFormat dst_format,
  759. const Uint8 dst_channels,
  760. const int dst_rate);
  761. /**
  762. * Add data to be converted/resampled to the stream.
  763. *
  764. * \param stream The stream the audio data is being added to
  765. * \param buf A pointer to the audio data to add
  766. * \param len The number of bytes to write to the stream
  767. * \returns 0 on success, or -1 on error.
  768. *
  769. * \sa SDL_NewAudioStream
  770. * \sa SDL_AudioStreamGet
  771. * \sa SDL_AudioStreamAvailable
  772. * \sa SDL_AudioStreamFlush
  773. * \sa SDL_AudioStreamClear
  774. * \sa SDL_FreeAudioStream
  775. */
  776. extern DECLSPEC int SDLCALL SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len);
  777. /**
  778. * Get converted/resampled data from the stream
  779. *
  780. * \param stream The stream the audio is being requested from
  781. * \param buf A buffer to fill with audio data
  782. * \param len The maximum number of bytes to fill
  783. * \returns the number of bytes read from the stream, or -1 on error
  784. *
  785. * \sa SDL_NewAudioStream
  786. * \sa SDL_AudioStreamPut
  787. * \sa SDL_AudioStreamAvailable
  788. * \sa SDL_AudioStreamFlush
  789. * \sa SDL_AudioStreamClear
  790. * \sa SDL_FreeAudioStream
  791. */
  792. extern DECLSPEC int SDLCALL SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len);
  793. /**
  794. * Get the number of converted/resampled bytes available.
  795. *
  796. * The stream may be buffering data behind the scenes until it has enough to
  797. * resample correctly, so this number might be lower than what you expect, or
  798. * even be zero. Add more data or flush the stream if you need the data now.
  799. *
  800. * \sa SDL_NewAudioStream
  801. * \sa SDL_AudioStreamPut
  802. * \sa SDL_AudioStreamGet
  803. * \sa SDL_AudioStreamFlush
  804. * \sa SDL_AudioStreamClear
  805. * \sa SDL_FreeAudioStream
  806. */
  807. extern DECLSPEC int SDLCALL SDL_AudioStreamAvailable(SDL_AudioStream *stream);
  808. /**
  809. * Tell the stream that you're done sending data, and anything being buffered
  810. * should be converted/resampled and made available immediately.
  811. *
  812. * It is legal to add more data to a stream after flushing, but there will be
  813. * audio gaps in the output. Generally this is intended to signal the end of
  814. * input, so the complete output becomes available.
  815. *
  816. * \sa SDL_NewAudioStream
  817. * \sa SDL_AudioStreamPut
  818. * \sa SDL_AudioStreamGet
  819. * \sa SDL_AudioStreamAvailable
  820. * \sa SDL_AudioStreamClear
  821. * \sa SDL_FreeAudioStream
  822. */
  823. extern DECLSPEC int SDLCALL SDL_AudioStreamFlush(SDL_AudioStream *stream);
  824. /**
  825. * Clear any pending data in the stream without converting it
  826. *
  827. * \sa SDL_NewAudioStream
  828. * \sa SDL_AudioStreamPut
  829. * \sa SDL_AudioStreamGet
  830. * \sa SDL_AudioStreamAvailable
  831. * \sa SDL_AudioStreamFlush
  832. * \sa SDL_FreeAudioStream
  833. */
  834. extern DECLSPEC void SDLCALL SDL_AudioStreamClear(SDL_AudioStream *stream);
  835. /**
  836. * Free an audio stream
  837. *
  838. * \sa SDL_NewAudioStream
  839. * \sa SDL_AudioStreamPut
  840. * \sa SDL_AudioStreamGet
  841. * \sa SDL_AudioStreamAvailable
  842. * \sa SDL_AudioStreamFlush
  843. * \sa SDL_AudioStreamClear
  844. */
  845. extern DECLSPEC void SDLCALL SDL_FreeAudioStream(SDL_AudioStream *stream);
  846. #define SDL_MIX_MAXVOLUME 128
  847. /**
  848. * This function is a legacy means of mixing audio.
  849. *
  850. * This function is equivalent to calling
  851. *
  852. * ```c++
  853. * SDL_MixAudioFormat(dst, src, format, len, volume);
  854. * ```
  855. *
  856. * where `format` is the obtained format of the audio device from the legacy
  857. * SDL_OpenAudio() function.
  858. *
  859. * \param dst the destination for the mixed audio
  860. * \param src the source audio buffer to be mixed
  861. * \param len the length of the audio buffer in bytes
  862. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  863. * for full audio volume
  864. *
  865. * \sa SDL_MixAudioFormat
  866. */
  867. extern DECLSPEC void SDLCALL SDL_MixAudio(Uint8 * dst, const Uint8 * src,
  868. Uint32 len, int volume);
  869. /**
  870. * Mix audio data in a specified format.
  871. *
  872. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  873. * it into `dst`, performing addition, volume adjustment, and overflow
  874. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  875. * `format` data.
  876. *
  877. * This is provided for convenience -- you can mix your own audio data.
  878. *
  879. * Do not use this function for mixing together more than two streams of
  880. * sample data. The output from repeated application of this function may be
  881. * distorted by clipping, because there is no accumulator with greater range
  882. * than the input (not to mention this being an inefficient way of doing it).
  883. *
  884. * It is a common misconception that this function is required to write audio
  885. * data to an output stream in an audio callback. While you can do that,
  886. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  887. * audio stream with a volume adjustment.
  888. *
  889. * \param dst the destination for the mixed audio
  890. * \param src the source audio buffer to be mixed
  891. * \param format the SDL_AudioFormat structure representing the desired audio
  892. * format
  893. * \param len the length of the audio buffer in bytes
  894. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  895. * for full audio volume
  896. */
  897. extern DECLSPEC void SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  898. const Uint8 * src,
  899. SDL_AudioFormat format,
  900. Uint32 len, int volume);
  901. /**
  902. * Queue more audio on non-callback devices.
  903. *
  904. * If you are looking to retrieve queued audio from a non-callback capture
  905. * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
  906. * -1 to signify an error if you use it with capture devices.
  907. *
  908. * SDL offers two ways to feed audio to the device: you can either supply a
  909. * callback that SDL triggers with some frequency to obtain more audio (pull
  910. * method), or you can supply no callback, and then SDL will expect you to
  911. * supply data at regular intervals (push method) with this function.
  912. *
  913. * There are no limits on the amount of data you can queue, short of
  914. * exhaustion of address space. Queued data will drain to the device as
  915. * necessary without further intervention from you. If the device needs audio
  916. * but there is not enough queued, it will play silence to make up the
  917. * difference. This means you will have skips in your audio playback if you
  918. * aren't routinely queueing sufficient data.
  919. *
  920. * This function copies the supplied data, so you are safe to free it when the
  921. * function returns. This function is thread-safe, but queueing to the same
  922. * device from two threads at once does not promise which buffer will be
  923. * queued first.
  924. *
  925. * You may not queue audio on a device that is using an application-supplied
  926. * callback; doing so returns an error. You have to use the audio callback or
  927. * queue audio with this function, but not both.
  928. *
  929. * You should not call SDL_LockAudio() on the device before queueing; SDL
  930. * handles locking internally for this function.
  931. *
  932. * Note that SDL2
  933. * [https://discourse.libsdl.org/t/sdl2-support-for-planar-audio/31263/3 does
  934. * not support planar audio]. You will need to resample from planar audio
  935. * formats into a non-planar one (see SDL_AudioFormat) before queuing audio.
  936. *
  937. * \param dev the device ID to which we will queue audio
  938. * \param data the data to queue to the device for later playback
  939. * \param len the number of bytes (not samples!) to which `data` points
  940. * \returns 0 on success or a negative error code on failure; call
  941. * SDL_GetError() for more information.
  942. *
  943. * \since This function is available since SDL 2.0.4.
  944. *
  945. * \sa SDL_ClearQueuedAudio
  946. * \sa SDL_GetQueuedAudioSize
  947. */
  948. extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
  949. /**
  950. * Dequeue more audio on non-callback devices.
  951. *
  952. * If you are looking to queue audio for output on a non-callback playback
  953. * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
  954. * return 0 if you use it with playback devices.
  955. *
  956. * SDL offers two ways to retrieve audio from a capture device: you can either
  957. * supply a callback that SDL triggers with some frequency as the device
  958. * records more audio data, (push method), or you can supply no callback, and
  959. * then SDL will expect you to retrieve data at regular intervals (pull
  960. * method) with this function.
  961. *
  962. * There are no limits on the amount of data you can queue, short of
  963. * exhaustion of address space. Data from the device will keep queuing as
  964. * necessary without further intervention from you. This means you will
  965. * eventually run out of memory if you aren't routinely dequeueing data.
  966. *
  967. * Capture devices will not queue data when paused; if you are expecting to
  968. * not need captured audio for some length of time, use SDL_PauseAudioDevice()
  969. * to stop the capture device from queueing more data. This can be useful
  970. * during, say, level loading times. When unpaused, capture devices will start
  971. * queueing data from that point, having flushed any capturable data available
  972. * while paused.
  973. *
  974. * This function is thread-safe, but dequeueing from the same device from two
  975. * threads at once does not promise which thread will dequeue data first.
  976. *
  977. * You may not dequeue audio from a device that is using an
  978. * application-supplied callback; doing so returns an error. You have to use
  979. * the audio callback, or dequeue audio with this function, but not both.
  980. *
  981. * You should not call SDL_LockAudio() on the device before dequeueing; SDL
  982. * handles locking internally for this function.
  983. *
  984. * \param dev the device ID from which we will dequeue audio
  985. * \param data a pointer into where audio data should be copied
  986. * \param len the number of bytes (not samples!) to which (data) points
  987. * \returns the number of bytes dequeued, which could be less than requested;
  988. * call SDL_GetError() for more information.
  989. *
  990. * \since This function is available since SDL 2.0.5.
  991. *
  992. * \sa SDL_ClearQueuedAudio
  993. * \sa SDL_GetQueuedAudioSize
  994. */
  995. extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
  996. /**
  997. * Get the number of bytes of still-queued audio.
  998. *
  999. * For playback devices: this is the number of bytes that have been queued for
  1000. * playback with SDL_QueueAudio(), but have not yet been sent to the hardware.
  1001. *
  1002. * Once we've sent it to the hardware, this function can not decide the exact
  1003. * byte boundary of what has been played. It's possible that we just gave the
  1004. * hardware several kilobytes right before you called this function, but it
  1005. * hasn't played any of it yet, or maybe half of it, etc.
  1006. *
  1007. * For capture devices, this is the number of bytes that have been captured by
  1008. * the device and are waiting for you to dequeue. This number may grow at any
  1009. * time, so this only informs of the lower-bound of available data.
  1010. *
  1011. * You may not queue or dequeue audio on a device that is using an
  1012. * application-supplied callback; calling this function on such a device
  1013. * always returns 0. You have to use the audio callback or queue audio, but
  1014. * not both.
  1015. *
  1016. * You should not call SDL_LockAudio() on the device before querying; SDL
  1017. * handles locking internally for this function.
  1018. *
  1019. * \param dev the device ID of which we will query queued audio size
  1020. * \returns the number of bytes (not samples!) of queued audio.
  1021. *
  1022. * \since This function is available since SDL 2.0.4.
  1023. *
  1024. * \sa SDL_ClearQueuedAudio
  1025. * \sa SDL_QueueAudio
  1026. * \sa SDL_DequeueAudio
  1027. */
  1028. extern DECLSPEC Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev);
  1029. /**
  1030. * Drop any queued audio data waiting to be sent to the hardware.
  1031. *
  1032. * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
  1033. * output devices, the hardware will start playing silence if more audio isn't
  1034. * queued. For capture devices, the hardware will start filling the empty
  1035. * queue with new data if the capture device isn't paused.
  1036. *
  1037. * This will not prevent playback of queued audio that's already been sent to
  1038. * the hardware, as we can not undo that, so expect there to be some fraction
  1039. * of a second of audio that might still be heard. This can be useful if you
  1040. * want to, say, drop any pending music or any unprocessed microphone input
  1041. * during a level change in your game.
  1042. *
  1043. * You may not queue or dequeue audio on a device that is using an
  1044. * application-supplied callback; calling this function on such a device
  1045. * always returns 0. You have to use the audio callback or queue audio, but
  1046. * not both.
  1047. *
  1048. * You should not call SDL_LockAudio() on the device before clearing the
  1049. * queue; SDL handles locking internally for this function.
  1050. *
  1051. * This function always succeeds and thus returns void.
  1052. *
  1053. * \param dev the device ID of which to clear the audio queue
  1054. *
  1055. * \since This function is available since SDL 2.0.4.
  1056. *
  1057. * \sa SDL_GetQueuedAudioSize
  1058. * \sa SDL_QueueAudio
  1059. * \sa SDL_DequeueAudio
  1060. */
  1061. extern DECLSPEC void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
  1062. /**
  1063. * \name Audio lock functions
  1064. *
  1065. * The lock manipulated by these functions protects the callback function.
  1066. * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
  1067. * the callback function is not running. Do not call these from the callback
  1068. * function or you will cause deadlock.
  1069. */
  1070. /* @{ */
  1071. extern DECLSPEC void SDLCALL SDL_LockAudio(void);
  1072. extern DECLSPEC void SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
  1073. extern DECLSPEC void SDLCALL SDL_UnlockAudio(void);
  1074. extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
  1075. /* @} *//* Audio lock functions */
  1076. /**
  1077. * This function is a legacy means of closing the audio device.
  1078. *
  1079. * This function is equivalent to calling
  1080. *
  1081. * ```c++
  1082. * SDL_CloseAudioDevice(1);
  1083. * ```
  1084. *
  1085. * and is only useful if you used the legacy SDL_OpenAudio() function.
  1086. *
  1087. * \sa SDL_OpenAudio
  1088. */
  1089. extern DECLSPEC void SDLCALL SDL_CloseAudio(void);
  1090. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
  1091. /* Ends C function definitions when using C++ */
  1092. #ifdef __cplusplus
  1093. }
  1094. #endif
  1095. #include "close_code.h"
  1096. #endif /* SDL_audio_h_ */
  1097. /* vi: set ts=4 sw=4 expandtab: */