SDL_audio.h 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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 <SDL3/SDL_stdinc.h>
  27. #include <SDL3/SDL_error.h>
  28. #include <SDL3/SDL_endian.h>
  29. #include <SDL3/SDL_mutex.h>
  30. #include <SDL3/SDL_thread.h>
  31. #include <SDL3/SDL_rwops.h>
  32. #include <SDL3/SDL_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_OpenAudioDevice().
  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 LFE BL BR (4.1 surround)
  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. #if defined(__GNUC__) && !defined(__CHERI_PURE_CAPABILITY__)
  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. This is not a concern on CHERI architectures, where pointers must be stored
  198. at aligned locations otherwise they will become invalid, and thus structs
  199. containing pointers cannot be packed without giving a warning or error.
  200. vvv
  201. The next time we rev the ABI, make sure to size the ints and add padding.
  202. */
  203. #define SDL_AUDIOCVT_PACKED __attribute__((packed))
  204. #else
  205. #define SDL_AUDIOCVT_PACKED
  206. #endif
  207. /* */
  208. typedef struct SDL_AudioCVT
  209. {
  210. int needed; /**< Set to 1 if conversion possible */
  211. SDL_AudioFormat src_format; /**< Source audio format */
  212. SDL_AudioFormat dst_format; /**< Target audio format */
  213. double rate_incr; /**< Rate conversion increment */
  214. Uint8 *buf; /**< Buffer to hold entire audio data */
  215. int len; /**< Length of original audio buffer */
  216. int len_cvt; /**< Length of converted audio buffer */
  217. int len_mult; /**< buffer must be len*len_mult big */
  218. double len_ratio; /**< Given len, final size is len*len_ratio */
  219. SDL_AudioFilter filters[SDL_AUDIOCVT_MAX_FILTERS + 1]; /**< NULL-terminated list of filter functions */
  220. int filter_index; /**< Current audio conversion function */
  221. } SDL_AUDIOCVT_PACKED SDL_AudioCVT;
  222. /* Function prototypes */
  223. /**
  224. * \name Driver discovery functions
  225. *
  226. * These functions return the list of built in audio drivers, in the
  227. * order that they are normally initialized by default.
  228. */
  229. /* @{ */
  230. /**
  231. * Use this function to get the number of built-in audio drivers.
  232. *
  233. * This function returns a hardcoded number. This never returns a negative
  234. * value; if there are no drivers compiled into this build of SDL, this
  235. * function returns zero. The presence of a driver in this list does not mean
  236. * it will function, it just means SDL is capable of interacting with that
  237. * interface. For example, a build of SDL might have esound support, but if
  238. * there's no esound server available, SDL's esound driver would fail if used.
  239. *
  240. * By default, SDL tries all drivers, in its preferred order, until one is
  241. * found to be usable.
  242. *
  243. * \returns the number of built-in audio drivers.
  244. *
  245. * \since This function is available since SDL 3.0.0.
  246. *
  247. * \sa SDL_GetAudioDriver
  248. */
  249. extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
  250. /**
  251. * Use this function to get the name of a built in audio driver.
  252. *
  253. * The list of audio drivers is given in the order that they are normally
  254. * initialized by default; the drivers that seem more reasonable to choose
  255. * first (as far as the SDL developers believe) are earlier in the list.
  256. *
  257. * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
  258. * "coreaudio" or "xaudio2". These never have Unicode characters, and are not
  259. * meant to be proper names.
  260. *
  261. * \param index the index of the audio driver; the value ranges from 0 to
  262. * SDL_GetNumAudioDrivers() - 1
  263. * \returns the name of the audio driver at the requested index, or NULL if an
  264. * invalid index was specified.
  265. *
  266. * \since This function is available since SDL 3.0.0.
  267. *
  268. * \sa SDL_GetNumAudioDrivers
  269. */
  270. extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
  271. /* @} */
  272. /**
  273. * Get the name of the current audio driver.
  274. *
  275. * The returned string points to internal static memory and thus never becomes
  276. * invalid, even if you quit the audio subsystem and initialize a new driver
  277. * (although such a case would return a different static string from another
  278. * call to this function, of course). As such, you should not modify or free
  279. * the returned string.
  280. *
  281. * \returns the name of the current audio driver or NULL if no driver has been
  282. * initialized.
  283. *
  284. * \since This function is available since SDL 3.0.0.
  285. */
  286. extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
  287. /**
  288. * SDL Audio Device IDs.
  289. */
  290. typedef Uint32 SDL_AudioDeviceID;
  291. /**
  292. * Get the number of built-in audio devices.
  293. *
  294. * This function is only valid after successfully initializing the audio
  295. * subsystem.
  296. *
  297. * Note that audio capture support is not implemented as of SDL 2.0.4, so the
  298. * `iscapture` parameter is for future expansion and should always be zero for
  299. * now.
  300. *
  301. * This function will return -1 if an explicit list of devices can't be
  302. * determined. Returning -1 is not an error. For example, if SDL is set up to
  303. * talk to a remote audio server, it can't list every one available on the
  304. * Internet, but it will still allow a specific host to be specified in
  305. * SDL_OpenAudioDevice().
  306. *
  307. * In many common cases, when this function returns a value <= 0, it can still
  308. * successfully open the default device (NULL for first argument of
  309. * SDL_OpenAudioDevice()).
  310. *
  311. * This function may trigger a complete redetect of available hardware. It
  312. * should not be called for each iteration of a loop, but rather once at the
  313. * start of a loop:
  314. *
  315. * ```c
  316. * // Don't do this:
  317. * for (int i = 0; i < SDL_GetNumAudioDevices(0); i++)
  318. *
  319. * // do this instead:
  320. * const int count = SDL_GetNumAudioDevices(0);
  321. * for (int i = 0; i < count; ++i) { do_something_here(); }
  322. * ```
  323. *
  324. * \param iscapture zero to request playback devices, non-zero to request
  325. * recording devices
  326. * \returns the number of available devices exposed by the current driver or
  327. * -1 if an explicit list of devices can't be determined. A return
  328. * value of -1 does not necessarily mean an error condition.
  329. *
  330. * \since This function is available since SDL 3.0.0.
  331. *
  332. * \sa SDL_GetAudioDeviceName
  333. * \sa SDL_OpenAudioDevice
  334. */
  335. extern DECLSPEC int SDLCALL SDL_GetNumAudioDevices(int iscapture);
  336. /**
  337. * Get the human-readable name of a specific audio device.
  338. *
  339. * This function is only valid after successfully initializing the audio
  340. * subsystem. The values returned by this function reflect the latest call to
  341. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  342. * hardware.
  343. *
  344. * The string returned by this function is UTF-8 encoded, read-only, and
  345. * managed internally. You are not to free it. If you need to keep the string
  346. * for any length of time, you should make your own copy of it, as it will be
  347. * invalid next time any of several other SDL functions are called.
  348. *
  349. * \param index the index of the audio device; valid values range from 0 to
  350. * SDL_GetNumAudioDevices() - 1
  351. * \param iscapture non-zero to query the list of recording devices, zero to
  352. * query the list of output devices.
  353. * \returns the name of the audio device at the requested index, or NULL on
  354. * error.
  355. *
  356. * \since This function is available since SDL 3.0.0.
  357. *
  358. * \sa SDL_GetNumAudioDevices
  359. * \sa SDL_GetDefaultAudioInfo
  360. */
  361. extern DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(int index,
  362. int iscapture);
  363. /**
  364. * Get the preferred audio format of a specific audio device.
  365. *
  366. * This function is only valid after a successfully initializing the audio
  367. * subsystem. The values returned by this function reflect the latest call to
  368. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  369. * hardware.
  370. *
  371. * `spec` will be filled with the sample rate, sample format, and channel
  372. * count.
  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. * \param spec The SDL_AudioSpec to be initialized by this function.
  379. * \returns 0 on success, nonzero on error
  380. *
  381. * \since This function is available since SDL 3.0.0.
  382. *
  383. * \sa SDL_GetNumAudioDevices
  384. * \sa SDL_GetDefaultAudioInfo
  385. */
  386. extern DECLSPEC int SDLCALL SDL_GetAudioDeviceSpec(int index,
  387. int iscapture,
  388. SDL_AudioSpec *spec);
  389. /**
  390. * Get the name and preferred format of the default audio device.
  391. *
  392. * Some (but not all!) platforms have an isolated mechanism to get information
  393. * about the "default" device. This can actually be a completely different
  394. * device that's not in the list you get from SDL_GetAudioDeviceSpec(). It can
  395. * even be a network address! (This is discussed in SDL_OpenAudioDevice().)
  396. *
  397. * As a result, this call is not guaranteed to be performant, as it can query
  398. * the sound server directly every time, unlike the other query functions. You
  399. * should call this function sparingly!
  400. *
  401. * `spec` will be filled with the sample rate, sample format, and channel
  402. * count, if a default device exists on the system. If `name` is provided,
  403. * will be filled with either a dynamically-allocated UTF-8 string or NULL.
  404. *
  405. * \param name A pointer to be filled with the name of the default device (can
  406. * be NULL). Please call SDL_free() when you are done with this
  407. * pointer!
  408. * \param spec The SDL_AudioSpec to be initialized by this function.
  409. * \param iscapture non-zero to query the default recording device, zero to
  410. * query the default output device.
  411. * \returns 0 on success, nonzero on error
  412. *
  413. * \since This function is available since SDL 3.0.0.
  414. *
  415. * \sa SDL_GetAudioDeviceName
  416. * \sa SDL_GetAudioDeviceSpec
  417. * \sa SDL_OpenAudioDevice
  418. */
  419. extern DECLSPEC int SDLCALL SDL_GetDefaultAudioInfo(char **name,
  420. SDL_AudioSpec *spec,
  421. int iscapture);
  422. /**
  423. * Open a specific audio device.
  424. *
  425. * Passing in a `device` name of NULL requests the most reasonable default.
  426. * The `device` name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but
  427. * some drivers allow arbitrary and driver-specific strings, such as a
  428. * hostname/IP address for a remote audio server, or a filename in the
  429. * diskaudio driver.
  430. *
  431. * An opened audio device starts out paused, and should be enabled for playing
  432. * by calling SDL_PlayAudioDevice(devid) when you are ready for your audio
  433. * callback function to be called. Since the audio driver may modify the
  434. * requested size of the audio buffer, you should allocate any local mixing
  435. * buffers after you open the audio device.
  436. *
  437. * The audio callback runs in a separate thread in most cases; you can prevent
  438. * race conditions between your callback and other threads without fully
  439. * pausing playback with SDL_LockAudioDevice(). For more information about the
  440. * callback, see SDL_AudioSpec.
  441. *
  442. * Managing the audio spec via 'desired' and 'obtained':
  443. *
  444. * When filling in the desired audio spec structure:
  445. *
  446. * - `desired->freq` should be the frequency in sample-frames-per-second (Hz).
  447. * - `desired->format` should be the audio format (`AUDIO_S16SYS`, etc).
  448. * - `desired->samples` is the desired size of the audio buffer, in _sample
  449. * frames_ (with stereo output, two samples--left and right--would make a
  450. * single sample frame). This number should be a power of two, and may be
  451. * adjusted by the audio driver to a value more suitable for the hardware.
  452. * Good values seem to range between 512 and 8096 inclusive, depending on
  453. * the application and CPU speed. Smaller values reduce latency, but can
  454. * lead to underflow if the application is doing heavy processing and cannot
  455. * fill the audio buffer in time. Note that the number of sample frames is
  456. * directly related to time by the following formula: `ms =
  457. * (sampleframes*1000)/freq`
  458. * - `desired->size` is the size in _bytes_ of the audio buffer, and is
  459. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  460. * - `desired->silence` is the value used to set the buffer to silence, and is
  461. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  462. * - `desired->callback` should be set to a function that will be called when
  463. * the audio device is ready for more data. It is passed a pointer to the
  464. * audio buffer, and the length in bytes of the audio buffer. This function
  465. * usually runs in a separate thread, and so you should protect data
  466. * structures that it accesses by calling SDL_LockAudioDevice() and
  467. * SDL_UnlockAudioDevice() in your code. Alternately, you may pass a NULL
  468. * pointer here, and call SDL_QueueAudio() with some frequency, to queue
  469. * more audio samples to be played (or for capture devices, call
  470. * SDL_DequeueAudio() with some frequency, to obtain audio samples).
  471. * - `desired->userdata` is passed as the first parameter to your callback
  472. * function. If you passed a NULL callback, this value is ignored.
  473. *
  474. * `allowed_changes` can have the following flags OR'd together:
  475. *
  476. * - `SDL_AUDIO_ALLOW_FREQUENCY_CHANGE`
  477. * - `SDL_AUDIO_ALLOW_FORMAT_CHANGE`
  478. * - `SDL_AUDIO_ALLOW_CHANNELS_CHANGE`
  479. * - `SDL_AUDIO_ALLOW_SAMPLES_CHANGE`
  480. * - `SDL_AUDIO_ALLOW_ANY_CHANGE`
  481. *
  482. * These flags specify how SDL should behave when a device cannot offer a
  483. * specific feature. If the application requests a feature that the hardware
  484. * doesn't offer, SDL will always try to get the closest equivalent.
  485. *
  486. * For example, if you ask for float32 audio format, but the sound card only
  487. * supports int16, SDL will set the hardware to int16. If you had set
  488. * SDL_AUDIO_ALLOW_FORMAT_CHANGE, SDL will change the format in the `obtained`
  489. * structure. If that flag was *not* set, SDL will prepare to convert your
  490. * callback's float32 audio to int16 before feeding it to the hardware and
  491. * will keep the originally requested format in the `obtained` structure.
  492. *
  493. * The resulting audio specs, varying depending on hardware and on what
  494. * changes were allowed, will then be written back to `obtained`.
  495. *
  496. * If your application can only handle one specific data format, pass a zero
  497. * for `allowed_changes` and let SDL transparently handle any differences.
  498. *
  499. * \param device a UTF-8 string reported by SDL_GetAudioDeviceName() or a
  500. * driver-specific name as appropriate. NULL requests the most
  501. * reasonable default device.
  502. * \param iscapture non-zero to specify a device should be opened for
  503. * recording, not playback
  504. * \param desired an SDL_AudioSpec structure representing the desired output
  505. * format
  506. * \param obtained an SDL_AudioSpec structure filled in with the actual output
  507. * format
  508. * \param allowed_changes 0, or one or more flags OR'd together
  509. * \returns a valid device ID that is > 0 on success or 0 on failure; call
  510. * SDL_GetError() for more information.
  511. *
  512. * For compatibility with SDL 1.2, this will never return 1, since
  513. * SDL reserves that ID for the legacy SDL_OpenAudio() function.
  514. *
  515. * \since This function is available since SDL 3.0.0.
  516. *
  517. * \sa SDL_CloseAudioDevice
  518. * \sa SDL_GetAudioDeviceName
  519. * \sa SDL_LockAudioDevice
  520. * \sa SDL_PlayAudioDevice
  521. * \sa SDL_PauseAudioDevice
  522. * \sa SDL_UnlockAudioDevice
  523. */
  524. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(
  525. const char *device,
  526. int iscapture,
  527. const SDL_AudioSpec *desired,
  528. SDL_AudioSpec *obtained,
  529. int allowed_changes);
  530. /**
  531. * \name Audio state
  532. *
  533. * Get the current audio state.
  534. */
  535. /* @{ */
  536. typedef enum
  537. {
  538. SDL_AUDIO_STOPPED = 0,
  539. SDL_AUDIO_PLAYING,
  540. SDL_AUDIO_PAUSED
  541. } SDL_AudioStatus;
  542. /**
  543. * Use this function to get the current audio state of an audio device.
  544. *
  545. * \param dev the ID of an audio device previously opened with
  546. * SDL_OpenAudioDevice()
  547. * \returns the SDL_AudioStatus of the specified audio device.
  548. *
  549. * \since This function is available since SDL 3.0.0.
  550. *
  551. * \sa SDL_PlayAudioDevice
  552. * \sa SDL_PauseAudioDevice
  553. */
  554. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev);
  555. /* @} *//* Audio State */
  556. /**
  557. * Use this function to play audio on a specified device.
  558. *
  559. * Newly-opened audio devices start in the paused state, so you must
  560. * call this function after opening the specified audio
  561. * device to start playing sound. This allows you to safely initialize data
  562. * for your callback function after opening the audio device. Silence will be
  563. * written to the audio device while paused, and the audio callback is
  564. * guaranteed to not be called. Pausing one device does not prevent other
  565. * unpaused devices from running their callbacks.
  566. *
  567. * \param dev a device opened by SDL_OpenAudioDevice()
  568. *
  569. * \since This function is available since SDL 3.0.0.
  570. *
  571. * \sa SDL_LockAudioDevice
  572. * \sa SDL_PauseAudioDevice
  573. */
  574. extern DECLSPEC void SDLCALL SDL_PlayAudioDevice(SDL_AudioDeviceID dev);
  575. /**
  576. * Use this function to pause audio playback on a specified device.
  577. *
  578. * This function pauses the audio callback processing for a given
  579. * device. Silence will be written to the audio device while paused, and
  580. * the audio callback is guaranteed to not be called.
  581. * Pausing one device does not prevent other unpaused devices from running
  582. * their callbacks.
  583. *
  584. * If you just need to protect a few variables from race conditions vs your
  585. * callback, you shouldn't pause the audio device, as it will lead to dropouts
  586. * in the audio playback. Instead, you should use SDL_LockAudioDevice().
  587. *
  588. * \param dev a device opened by SDL_OpenAudioDevice()
  589. *
  590. * \since This function is available since SDL 3.0.0.
  591. *
  592. * \sa SDL_LockAudioDevice
  593. * \sa SDL_PlayAudioDevice
  594. */
  595. extern DECLSPEC void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev);
  596. /**
  597. * Load the audio data of a WAVE file into memory.
  598. *
  599. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  600. * be valid pointers. The entire data portion of the file is then loaded into
  601. * memory and decoded if necessary.
  602. *
  603. * If `freesrc` is non-zero, the data source gets automatically closed and
  604. * freed before the function returns.
  605. *
  606. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  607. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  608. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  609. * cause an error.
  610. *
  611. * If this function succeeds, the pointer returned by it is equal to `spec`
  612. * and the pointer to the audio data allocated by the function is written to
  613. * `audio_buf` and its length in bytes to `audio_len`. The SDL_AudioSpec
  614. * members `freq`, `channels`, and `format` are set to the values of the audio
  615. * data in the buffer. The `samples` member is set to a sane default and all
  616. * others are set to zero.
  617. *
  618. * It's necessary to use SDL_free() to free the audio data returned in
  619. * `audio_buf` when it is no longer used.
  620. *
  621. * Because of the underspecification of the .WAV format, there are many
  622. * problematic files in the wild that cause issues with strict decoders. To
  623. * provide compatibility with these files, this decoder is lenient in regards
  624. * to the truncation of the file, the fact chunk, and the size of the RIFF
  625. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  626. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  627. * tune the behavior of the loading process.
  628. *
  629. * Any file that is invalid (due to truncation, corruption, or wrong values in
  630. * the headers), too big, or unsupported causes an error. Additionally, any
  631. * critical I/O error from the data source will terminate the loading process
  632. * with an error. The function returns NULL on error and in all cases (with
  633. * the exception of `src` being NULL), an appropriate error message will be
  634. * set.
  635. *
  636. * It is required that the data source supports seeking.
  637. *
  638. * Example:
  639. *
  640. * ```c
  641. * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
  642. * ```
  643. *
  644. * Note that the SDL_LoadWAV macro does this same thing for you, but in a less
  645. * messy way:
  646. *
  647. * ```c
  648. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  649. * ```
  650. *
  651. * \param src The data source for the WAVE data
  652. * \param freesrc If non-zero, SDL will _always_ free the data source
  653. * \param spec An SDL_AudioSpec that will be filled in with the wave file's
  654. * format details
  655. * \param audio_buf A pointer filled with the audio data, allocated by the
  656. * function.
  657. * \param audio_len A pointer filled with the length of the audio data buffer
  658. * in bytes
  659. * \returns This function, if successfully called, returns `spec`, which will
  660. * be filled with the audio data format of the wave source data.
  661. * `audio_buf` will be filled with a pointer to an allocated buffer
  662. * containing the audio data, and `audio_len` is filled with the
  663. * length of that audio buffer in bytes.
  664. *
  665. * This function returns NULL if the .WAV file cannot be opened, uses
  666. * an unknown data format, or is corrupt; call SDL_GetError() for
  667. * more information.
  668. *
  669. * When the application is done with the data returned in
  670. * `audio_buf`, it should call SDL_free() to dispose of it.
  671. *
  672. * \since This function is available since SDL 3.0.0.
  673. *
  674. * \sa SDL_free
  675. * \sa SDL_LoadWAV
  676. */
  677. extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
  678. int freesrc,
  679. SDL_AudioSpec * spec,
  680. Uint8 ** audio_buf,
  681. Uint32 * audio_len);
  682. /**
  683. * Loads a WAV from a file.
  684. * Compatibility convenience function.
  685. */
  686. #define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
  687. SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
  688. /**
  689. * Initialize an SDL_AudioCVT structure for conversion.
  690. *
  691. * Before an SDL_AudioCVT structure can be used to convert audio data it must
  692. * be initialized with source and destination information.
  693. *
  694. * This function will zero out every field of the SDL_AudioCVT, so it must be
  695. * called before the application fills in the final buffer information.
  696. *
  697. * Once this function has returned successfully, and reported that a
  698. * conversion is necessary, the application fills in the rest of the fields in
  699. * SDL_AudioCVT, now that it knows how large a buffer it needs to allocate,
  700. * and then can call SDL_ConvertAudio() to complete the conversion.
  701. *
  702. * \param cvt an SDL_AudioCVT structure filled in with audio conversion
  703. * information
  704. * \param src_format the source format of the audio data; for more info see
  705. * SDL_AudioFormat
  706. * \param src_channels the number of channels in the source
  707. * \param src_rate the frequency (sample-frames-per-second) of the source
  708. * \param dst_format the destination format of the audio data; for more info
  709. * see SDL_AudioFormat
  710. * \param dst_channels the number of channels in the destination
  711. * \param dst_rate the frequency (sample-frames-per-second) of the destination
  712. * \returns 1 if the audio filter is prepared, 0 if no conversion is needed,
  713. * or a negative error code on failure; call SDL_GetError() for more
  714. * information.
  715. *
  716. * \since This function is available since SDL 3.0.0.
  717. *
  718. * \sa SDL_ConvertAudio
  719. */
  720. extern DECLSPEC int SDLCALL SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
  721. SDL_AudioFormat src_format,
  722. Uint8 src_channels,
  723. int src_rate,
  724. SDL_AudioFormat dst_format,
  725. Uint8 dst_channels,
  726. int dst_rate);
  727. /**
  728. * Convert audio data to a desired audio format.
  729. *
  730. * This function does the actual audio data conversion, after the application
  731. * has called SDL_BuildAudioCVT() to prepare the conversion information and
  732. * then filled in the buffer details.
  733. *
  734. * Once the application has initialized the `cvt` structure using
  735. * SDL_BuildAudioCVT(), allocated an audio buffer and filled it with audio
  736. * data in the source format, this function will convert the buffer, in-place,
  737. * to the desired format.
  738. *
  739. * The data conversion may go through several passes; any given pass may
  740. * possibly temporarily increase the size of the data. For example, SDL might
  741. * expand 16-bit data to 32 bits before resampling to a lower frequency,
  742. * shrinking the data size after having grown it briefly. Since the supplied
  743. * buffer will be both the source and destination, converting as necessary
  744. * in-place, the application must allocate a buffer that will fully contain
  745. * the data during its largest conversion pass. After SDL_BuildAudioCVT()
  746. * returns, the application should set the `cvt->len` field to the size, in
  747. * bytes, of the source data, and allocate a buffer that is `cvt->len *
  748. * cvt->len_mult` bytes long for the `buf` field.
  749. *
  750. * The source data should be copied into this buffer before the call to
  751. * SDL_ConvertAudio(). Upon successful return, this buffer will contain the
  752. * converted audio, and `cvt->len_cvt` will be the size of the converted data,
  753. * in bytes. Any bytes in the buffer past `cvt->len_cvt` are undefined once
  754. * this function returns.
  755. *
  756. * \param cvt an SDL_AudioCVT structure that was previously set up by
  757. * SDL_BuildAudioCVT().
  758. * \returns 0 if the conversion was completed successfully or a negative error
  759. * code on failure; call SDL_GetError() for more information.
  760. *
  761. * \since This function is available since SDL 3.0.0.
  762. *
  763. * \sa SDL_BuildAudioCVT
  764. */
  765. extern DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT * cvt);
  766. /* SDL_AudioStream is a new audio conversion interface.
  767. The benefits vs SDL_AudioCVT:
  768. - it can handle resampling data in chunks without generating
  769. artifacts, when it doesn't have the complete buffer available.
  770. - it can handle incoming data in any variable size.
  771. - You push data as you have it, and pull it when you need it
  772. */
  773. /* this is opaque to the outside world. */
  774. struct SDL_AudioStream;
  775. typedef struct SDL_AudioStream SDL_AudioStream;
  776. /**
  777. * Create a new audio stream.
  778. *
  779. * \param src_format The format of the source audio
  780. * \param src_channels The number of channels of the source audio
  781. * \param src_rate The sampling rate of the source audio
  782. * \param dst_format The format of the desired audio output
  783. * \param dst_channels The number of channels of the desired audio output
  784. * \param dst_rate The sampling rate of the desired audio output
  785. * \returns 0 on success, or -1 on error.
  786. *
  787. * \since This function is available since SDL 3.0.0.
  788. *
  789. * \sa SDL_PutAudioStreamData
  790. * \sa SDL_GetAudioStreamData
  791. * \sa SDL_GetAudioStreamAvailable
  792. * \sa SDL_FlushAudioStream
  793. * \sa SDL_ClearAudioStream
  794. * \sa SDL_DestroyAudioStream
  795. */
  796. extern DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAudioStream(SDL_AudioFormat src_format,
  797. Uint8 src_channels,
  798. int src_rate,
  799. SDL_AudioFormat dst_format,
  800. Uint8 dst_channels,
  801. int dst_rate);
  802. /**
  803. * Add data to be converted/resampled to the stream.
  804. *
  805. * \param stream The stream the audio data is being added to
  806. * \param buf A pointer to the audio data to add
  807. * \param len The number of bytes to write to the stream
  808. * \returns 0 on success, or -1 on error.
  809. *
  810. * \since This function is available since SDL 3.0.0.
  811. *
  812. * \sa SDL_CreateAudioStream
  813. * \sa SDL_GetAudioStreamData
  814. * \sa SDL_GetAudioStreamAvailable
  815. * \sa SDL_FlushAudioStream
  816. * \sa SDL_ClearAudioStream
  817. * \sa SDL_DestroyAudioStream
  818. */
  819. extern DECLSPEC int SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
  820. /**
  821. * Get converted/resampled data from the stream
  822. *
  823. * \param stream The stream the audio is being requested from
  824. * \param buf A buffer to fill with audio data
  825. * \param len The maximum number of bytes to fill
  826. * \returns the number of bytes read from the stream, or -1 on error
  827. *
  828. * \since This function is available since SDL 3.0.0.
  829. *
  830. * \sa SDL_CreateAudioStream
  831. * \sa SDL_PutAudioStreamData
  832. * \sa SDL_GetAudioStreamAvailable
  833. * \sa SDL_FlushAudioStream
  834. * \sa SDL_ClearAudioStream
  835. * \sa SDL_DestroyAudioStream
  836. */
  837. extern DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
  838. /**
  839. * Get the number of converted/resampled bytes available.
  840. *
  841. * The stream may be buffering data behind the scenes until it has enough to
  842. * resample correctly, so this number might be lower than what you expect, or
  843. * even be zero. Add more data or flush the stream if you need the data now.
  844. *
  845. * \since This function is available since SDL 3.0.0.
  846. *
  847. * \sa SDL_CreateAudioStream
  848. * \sa SDL_PutAudioStreamData
  849. * \sa SDL_GetAudioStreamData
  850. * \sa SDL_FlushAudioStream
  851. * \sa SDL_ClearAudioStream
  852. * \sa SDL_DestroyAudioStream
  853. */
  854. extern DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
  855. /**
  856. * Tell the stream that you're done sending data, and anything being buffered
  857. * should be converted/resampled and made available immediately.
  858. *
  859. * It is legal to add more data to a stream after flushing, but there will be
  860. * audio gaps in the output. Generally this is intended to signal the end of
  861. * input, so the complete output becomes available.
  862. *
  863. * \since This function is available since SDL 3.0.0.
  864. *
  865. * \sa SDL_CreateAudioStream
  866. * \sa SDL_PutAudioStreamData
  867. * \sa SDL_GetAudioStreamData
  868. * \sa SDL_GetAudioStreamAvailable
  869. * \sa SDL_ClearAudioStream
  870. * \sa SDL_DestroyAudioStream
  871. */
  872. extern DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
  873. /**
  874. * Clear any pending data in the stream without converting it
  875. *
  876. * \since This function is available since SDL 3.0.0.
  877. *
  878. * \sa SDL_CreateAudioStream
  879. * \sa SDL_PutAudioStreamData
  880. * \sa SDL_GetAudioStreamData
  881. * \sa SDL_GetAudioStreamAvailable
  882. * \sa SDL_FlushAudioStream
  883. * \sa SDL_DestroyAudioStream
  884. */
  885. extern DECLSPEC void SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
  886. /**
  887. * Free an audio stream
  888. *
  889. * \since This function is available since SDL 3.0.0.
  890. *
  891. * \sa SDL_CreateAudioStream
  892. * \sa SDL_PutAudioStreamData
  893. * \sa SDL_GetAudioStreamData
  894. * \sa SDL_GetAudioStreamAvailable
  895. * \sa SDL_FlushAudioStream
  896. * \sa SDL_ClearAudioStream
  897. */
  898. extern DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
  899. #define SDL_MIX_MAXVOLUME 128
  900. /**
  901. * Mix audio data in a specified format.
  902. *
  903. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  904. * it into `dst`, performing addition, volume adjustment, and overflow
  905. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  906. * `format` data.
  907. *
  908. * This is provided for convenience -- you can mix your own audio data.
  909. *
  910. * Do not use this function for mixing together more than two streams of
  911. * sample data. The output from repeated application of this function may be
  912. * distorted by clipping, because there is no accumulator with greater range
  913. * than the input (not to mention this being an inefficient way of doing it).
  914. *
  915. * It is a common misconception that this function is required to write audio
  916. * data to an output stream in an audio callback. While you can do that,
  917. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  918. * audio stream with a volume adjustment.
  919. *
  920. * \param dst the destination for the mixed audio
  921. * \param src the source audio buffer to be mixed
  922. * \param format the SDL_AudioFormat structure representing the desired audio
  923. * format
  924. * \param len the length of the audio buffer in bytes
  925. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  926. * for full audio volume
  927. *
  928. * \since This function is available since SDL 3.0.0.
  929. */
  930. extern DECLSPEC void SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  931. const Uint8 * src,
  932. SDL_AudioFormat format,
  933. Uint32 len, int volume);
  934. /**
  935. * Queue more audio on non-callback devices.
  936. *
  937. * If you are looking to retrieve queued audio from a non-callback capture
  938. * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
  939. * -1 to signify an error if you use it with capture devices.
  940. *
  941. * SDL offers two ways to feed audio to the device: you can either supply a
  942. * callback that SDL triggers with some frequency to obtain more audio (pull
  943. * method), or you can supply no callback, and then SDL will expect you to
  944. * supply data at regular intervals (push method) with this function.
  945. *
  946. * There are no limits on the amount of data you can queue, short of
  947. * exhaustion of address space. Queued data will drain to the device as
  948. * necessary without further intervention from you. If the device needs audio
  949. * but there is not enough queued, it will play silence to make up the
  950. * difference. This means you will have skips in your audio playback if you
  951. * aren't routinely queueing sufficient data.
  952. *
  953. * This function copies the supplied data, so you are safe to free it when the
  954. * function returns. This function is thread-safe, but queueing to the same
  955. * device from two threads at once does not promise which buffer will be
  956. * queued first.
  957. *
  958. * You may not queue audio on a device that is using an application-supplied
  959. * callback; doing so returns an error. You have to use the audio callback or
  960. * queue audio with this function, but not both.
  961. *
  962. * You should not call SDL_LockAudio() on the device before queueing; SDL
  963. * handles locking internally for this function.
  964. *
  965. * Note that SDL does not support planar audio. You will need to resample from
  966. * planar audio formats into a non-planar one (see SDL_AudioFormat) before
  967. * queuing audio.
  968. *
  969. * \param dev the device ID to which we will queue audio
  970. * \param data the data to queue to the device for later playback
  971. * \param len the number of bytes (not samples!) to which `data` points
  972. * \returns 0 on success or a negative error code on failure; call
  973. * SDL_GetError() for more information.
  974. *
  975. * \since This function is available since SDL 3.0.0.
  976. *
  977. * \sa SDL_ClearQueuedAudio
  978. * \sa SDL_GetQueuedAudioSize
  979. */
  980. extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
  981. /**
  982. * Dequeue more audio on non-callback devices.
  983. *
  984. * If you are looking to queue audio for output on a non-callback playback
  985. * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
  986. * return 0 if you use it with playback devices.
  987. *
  988. * SDL offers two ways to retrieve audio from a capture device: you can either
  989. * supply a callback that SDL triggers with some frequency as the device
  990. * records more audio data, (push method), or you can supply no callback, and
  991. * then SDL will expect you to retrieve data at regular intervals (pull
  992. * method) with this function.
  993. *
  994. * There are no limits on the amount of data you can queue, short of
  995. * exhaustion of address space. Data from the device will keep queuing as
  996. * necessary without further intervention from you. This means you will
  997. * eventually run out of memory if you aren't routinely dequeueing data.
  998. *
  999. * Capture devices will not queue data when paused; if you are expecting to
  1000. * not need captured audio for some length of time, use SDL_PauseAudioDevice()
  1001. * to stop the capture device from queueing more data. This can be useful
  1002. * during, say, level loading times. When unpaused, capture devices will start
  1003. * queueing data from that point, having flushed any capturable data available
  1004. * while paused.
  1005. *
  1006. * This function is thread-safe, but dequeueing from the same device from two
  1007. * threads at once does not promise which thread will dequeue data first.
  1008. *
  1009. * You may not dequeue audio from a device that is using an
  1010. * application-supplied callback; doing so returns an error. You have to use
  1011. * the audio callback, or dequeue audio with this function, but not both.
  1012. *
  1013. * You should not call SDL_LockAudio() on the device before dequeueing; SDL
  1014. * handles locking internally for this function.
  1015. *
  1016. * \param dev the device ID from which we will dequeue audio
  1017. * \param data a pointer into where audio data should be copied
  1018. * \param len the number of bytes (not samples!) to which (data) points
  1019. * \returns the number of bytes dequeued, which could be less than requested;
  1020. * call SDL_GetError() for more information.
  1021. *
  1022. * \since This function is available since SDL 3.0.0.
  1023. *
  1024. * \sa SDL_ClearQueuedAudio
  1025. * \sa SDL_GetQueuedAudioSize
  1026. */
  1027. extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
  1028. /**
  1029. * Get the number of bytes of still-queued audio.
  1030. *
  1031. * For playback devices: this is the number of bytes that have been queued for
  1032. * playback with SDL_QueueAudio(), but have not yet been sent to the hardware.
  1033. *
  1034. * Once we've sent it to the hardware, this function can not decide the exact
  1035. * byte boundary of what has been played. It's possible that we just gave the
  1036. * hardware several kilobytes right before you called this function, but it
  1037. * hasn't played any of it yet, or maybe half of it, etc.
  1038. *
  1039. * For capture devices, this is the number of bytes that have been captured by
  1040. * the device and are waiting for you to dequeue. This number may grow at any
  1041. * time, so this only informs of the lower-bound of available data.
  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 querying; SDL
  1049. * handles locking internally for this function.
  1050. *
  1051. * \param dev the device ID of which we will query queued audio size
  1052. * \returns the number of bytes (not samples!) of queued audio.
  1053. *
  1054. * \since This function is available since SDL 3.0.0.
  1055. *
  1056. * \sa SDL_ClearQueuedAudio
  1057. * \sa SDL_QueueAudio
  1058. * \sa SDL_DequeueAudio
  1059. */
  1060. extern DECLSPEC Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev);
  1061. /**
  1062. * Drop any queued audio data waiting to be sent to the hardware.
  1063. *
  1064. * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
  1065. * output devices, the hardware will start playing silence if more audio isn't
  1066. * queued. For capture devices, the hardware will start filling the empty
  1067. * queue with new data if the capture device isn't paused.
  1068. *
  1069. * This will not prevent playback of queued audio that's already been sent to
  1070. * the hardware, as we can not undo that, so expect there to be some fraction
  1071. * of a second of audio that might still be heard. This can be useful if you
  1072. * want to, say, drop any pending music or any unprocessed microphone input
  1073. * during a level change in your game.
  1074. *
  1075. * You may not queue or dequeue audio on a device that is using an
  1076. * application-supplied callback; calling this function on such a device
  1077. * always returns 0. You have to use the audio callback or queue audio, but
  1078. * not both.
  1079. *
  1080. * You should not call SDL_LockAudio() on the device before clearing the
  1081. * queue; SDL handles locking internally for this function.
  1082. *
  1083. * This function always succeeds and thus returns void.
  1084. *
  1085. * \param dev the device ID of which to clear the audio queue
  1086. *
  1087. * \since This function is available since SDL 3.0.0.
  1088. *
  1089. * \sa SDL_GetQueuedAudioSize
  1090. * \sa SDL_QueueAudio
  1091. * \sa SDL_DequeueAudio
  1092. */
  1093. extern DECLSPEC void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
  1094. /**
  1095. * \name Audio lock functions
  1096. *
  1097. * The lock manipulated by these functions protects the callback function.
  1098. * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
  1099. * the callback function is not running. Do not call these from the callback
  1100. * function or you will cause deadlock.
  1101. */
  1102. /* @{ */
  1103. /**
  1104. * Use this function to lock out the audio callback function for a specified
  1105. * device.
  1106. *
  1107. * The lock manipulated by these functions protects the audio callback
  1108. * function specified in SDL_OpenAudioDevice(). During a
  1109. * SDL_LockAudioDevice()/SDL_UnlockAudioDevice() pair, you can be guaranteed
  1110. * that the callback function for that device is not running, even if the
  1111. * device is not paused. While a device is locked, any other unpaused,
  1112. * unlocked devices may still run their callbacks.
  1113. *
  1114. * Calling this function from inside your audio callback is unnecessary. SDL
  1115. * obtains this lock before calling your function, and releases it when the
  1116. * function returns.
  1117. *
  1118. * You should not hold the lock longer than absolutely necessary. If you hold
  1119. * it too long, you'll experience dropouts in your audio playback. Ideally,
  1120. * your application locks the device, sets a few variables and unlocks again.
  1121. * Do not do heavy work while holding the lock for a device.
  1122. *
  1123. * It is safe to lock the audio device multiple times, as long as you unlock
  1124. * it an equivalent number of times. The callback will not run until the
  1125. * device has been unlocked completely in this way. If your application fails
  1126. * to unlock the device appropriately, your callback will never run, you might
  1127. * hear repeating bursts of audio, and SDL_CloseAudioDevice() will probably
  1128. * deadlock.
  1129. *
  1130. * Internally, the audio device lock is a mutex; if you lock from two threads
  1131. * at once, not only will you block the audio callback, you'll block the other
  1132. * thread.
  1133. *
  1134. * \param dev the ID of the device to be locked
  1135. *
  1136. * \since This function is available since SDL 3.0.0.
  1137. *
  1138. * \sa SDL_UnlockAudioDevice
  1139. */
  1140. extern DECLSPEC void SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
  1141. /**
  1142. * Use this function to unlock the audio callback function for a specified
  1143. * device.
  1144. *
  1145. * This function should be paired with a previous SDL_LockAudioDevice() call.
  1146. *
  1147. * \param dev the ID of the device to be unlocked
  1148. *
  1149. * \since This function is available since SDL 3.0.0.
  1150. *
  1151. * \sa SDL_LockAudioDevice
  1152. */
  1153. extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
  1154. /* @} *//* Audio lock functions */
  1155. /**
  1156. * Use this function to shut down audio processing and close the audio device.
  1157. *
  1158. * The application should close open audio devices once they are no longer
  1159. * needed. Calling this function will wait until the device's audio callback
  1160. * is not running, release the audio hardware and then clean up internal
  1161. * state. No further audio will play from this device once this function
  1162. * returns.
  1163. *
  1164. * This function may block briefly while pending audio data is played by the
  1165. * hardware, so that applications don't drop the last buffer of data they
  1166. * supplied.
  1167. *
  1168. * The device ID is invalid as soon as the device is closed, and is eligible
  1169. * for reuse in a new SDL_OpenAudioDevice() call immediately.
  1170. *
  1171. * \param dev an audio device previously opened with SDL_OpenAudioDevice()
  1172. *
  1173. * \since This function is available since SDL 3.0.0.
  1174. *
  1175. * \sa SDL_OpenAudioDevice
  1176. */
  1177. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
  1178. /* Ends C function definitions when using C++ */
  1179. #ifdef __cplusplus
  1180. }
  1181. #endif
  1182. #include <SDL3/SDL_close_code.h>
  1183. #endif /* SDL_audio_h_ */