SDL_audio.h 46 KB

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