SDL_audio.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. /* 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(),
  378. * but 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 call this
  511. * function after opening the specified audio device to start playing sound.
  512. * This allows you to safely initialize data for your callback function after
  513. * opening the audio device. Silence will be written to the audio device while
  514. * paused, and the audio callback is guaranteed to not be called. Pausing one
  515. * device does not prevent other unpaused devices from running their
  516. * callbacks.
  517. *
  518. * \param dev a device opened by SDL_OpenAudioDevice()
  519. * \returns 0 on success or a negative error code on failure; call
  520. * SDL_GetError() for more information.
  521. *
  522. * \since This function is available since SDL 3.0.0.
  523. *
  524. * \sa SDL_LockAudioDevice
  525. * \sa SDL_PauseAudioDevice
  526. */
  527. extern DECLSPEC int SDLCALL SDL_PlayAudioDevice(SDL_AudioDeviceID dev);
  528. /**
  529. * Use this function to pause audio playback on a specified device.
  530. *
  531. * This function pauses the audio callback processing for a given device.
  532. * Silence will be written to the audio device while paused, and the audio
  533. * callback is guaranteed to not be called. Pausing one device does not
  534. * prevent other unpaused devices from running their callbacks.
  535. *
  536. * If you just need to protect a few variables from race conditions vs your
  537. * callback, you shouldn't pause the audio device, as it will lead to dropouts
  538. * in the audio playback. Instead, you should use SDL_LockAudioDevice().
  539. *
  540. * \param dev a device opened by SDL_OpenAudioDevice()
  541. * \returns 0 on success or a negative error code on failure; call
  542. * SDL_GetError() for more information.
  543. *
  544. * \since This function is available since SDL 3.0.0.
  545. *
  546. * \sa SDL_LockAudioDevice
  547. * \sa SDL_PlayAudioDevice
  548. */
  549. extern DECLSPEC int SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev);
  550. /**
  551. * Load the audio data of a WAVE file into memory.
  552. *
  553. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  554. * be valid pointers. The entire data portion of the file is then loaded into
  555. * memory and decoded if necessary.
  556. *
  557. * If `freesrc` is non-zero, the data source gets automatically closed and
  558. * freed before the function returns.
  559. *
  560. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  561. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  562. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  563. * cause an error.
  564. *
  565. * If this function succeeds, the pointer returned by it is equal to `spec`
  566. * and the pointer to the audio data allocated by the function is written to
  567. * `audio_buf` and its length in bytes to `audio_len`. The SDL_AudioSpec
  568. * members `freq`, `channels`, and `format` are set to the values of the audio
  569. * data in the buffer. The `samples` member is set to a sane default and all
  570. * others are set to zero.
  571. *
  572. * It's necessary to use SDL_free() to free the audio data returned in
  573. * `audio_buf` when it is no longer used.
  574. *
  575. * Because of the underspecification of the .WAV format, there are many
  576. * problematic files in the wild that cause issues with strict decoders. To
  577. * provide compatibility with these files, this decoder is lenient in regards
  578. * to the truncation of the file, the fact chunk, and the size of the RIFF
  579. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  580. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  581. * tune the behavior of the loading process.
  582. *
  583. * Any file that is invalid (due to truncation, corruption, or wrong values in
  584. * the headers), too big, or unsupported causes an error. Additionally, any
  585. * critical I/O error from the data source will terminate the loading process
  586. * with an error. The function returns NULL on error and in all cases (with
  587. * the exception of `src` being NULL), an appropriate error message will be
  588. * set.
  589. *
  590. * It is required that the data source supports seeking.
  591. *
  592. * Example:
  593. *
  594. * ```c
  595. * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
  596. * ```
  597. *
  598. * Note that the SDL_LoadWAV macro does this same thing for you, but in a less
  599. * messy way:
  600. *
  601. * ```c
  602. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  603. * ```
  604. *
  605. * \param src The data source for the WAVE data
  606. * \param freesrc If non-zero, SDL will _always_ free the data source
  607. * \param spec An SDL_AudioSpec that will be filled in with the wave file's
  608. * format details
  609. * \param audio_buf A pointer filled with the audio data, allocated by the
  610. * function.
  611. * \param audio_len A pointer filled with the length of the audio data buffer
  612. * in bytes
  613. * \returns This function, if successfully called, returns `spec`, which will
  614. * be filled with the audio data format of the wave source data.
  615. * `audio_buf` will be filled with a pointer to an allocated buffer
  616. * containing the audio data, and `audio_len` is filled with the
  617. * length of that audio buffer in bytes.
  618. *
  619. * This function returns NULL if the .WAV file cannot be opened, uses
  620. * an unknown data format, or is corrupt; call SDL_GetError() for
  621. * more information.
  622. *
  623. * When the application is done with the data returned in
  624. * `audio_buf`, it should call SDL_free() to dispose of it.
  625. *
  626. * \since This function is available since SDL 3.0.0.
  627. *
  628. * \sa SDL_free
  629. * \sa SDL_LoadWAV
  630. */
  631. extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
  632. int freesrc,
  633. SDL_AudioSpec * spec,
  634. Uint8 ** audio_buf,
  635. Uint32 * audio_len);
  636. /**
  637. * Loads a WAV from a file.
  638. * Compatibility convenience function.
  639. */
  640. #define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
  641. SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
  642. /* SDL_AudioStream is a new audio conversion interface.
  643. The benefits vs SDL_AudioCVT:
  644. - it can handle resampling data in chunks without generating
  645. artifacts, when it doesn't have the complete buffer available.
  646. - it can handle incoming data in any variable size.
  647. - You push data as you have it, and pull it when you need it
  648. */
  649. /* this is opaque to the outside world. */
  650. struct SDL_AudioStream;
  651. typedef struct SDL_AudioStream SDL_AudioStream;
  652. /**
  653. * Create a new audio stream.
  654. *
  655. * \param src_format The format of the source audio
  656. * \param src_channels The number of channels of the source audio
  657. * \param src_rate The sampling rate of the source audio
  658. * \param dst_format The format of the desired audio output
  659. * \param dst_channels The number of channels of the desired audio output
  660. * \param dst_rate The sampling rate of the desired audio output
  661. * \returns 0 on success, or -1 on error.
  662. *
  663. * \since This function is available since SDL 3.0.0.
  664. *
  665. * \sa SDL_PutAudioStreamData
  666. * \sa SDL_GetAudioStreamData
  667. * \sa SDL_GetAudioStreamAvailable
  668. * \sa SDL_FlushAudioStream
  669. * \sa SDL_ClearAudioStream
  670. * \sa SDL_DestroyAudioStream
  671. */
  672. extern DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAudioStream(SDL_AudioFormat src_format,
  673. Uint8 src_channels,
  674. int src_rate,
  675. SDL_AudioFormat dst_format,
  676. Uint8 dst_channels,
  677. int dst_rate);
  678. /**
  679. * Add data to be converted/resampled to the stream.
  680. *
  681. * \param stream The stream the audio data is being added to
  682. * \param buf A pointer to the audio data to add
  683. * \param len The number of bytes to write to the stream
  684. * \returns 0 on success, or -1 on error.
  685. *
  686. * \since This function is available since SDL 3.0.0.
  687. *
  688. * \sa SDL_CreateAudioStream
  689. * \sa SDL_GetAudioStreamData
  690. * \sa SDL_GetAudioStreamAvailable
  691. * \sa SDL_FlushAudioStream
  692. * \sa SDL_ClearAudioStream
  693. * \sa SDL_DestroyAudioStream
  694. */
  695. extern DECLSPEC int SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
  696. /**
  697. * Get converted/resampled data from the stream
  698. *
  699. * \param stream The stream the audio is being requested from
  700. * \param buf A buffer to fill with audio data
  701. * \param len The maximum number of bytes to fill
  702. * \returns the number of bytes read from the stream, or -1 on error
  703. *
  704. * \since This function is available since SDL 3.0.0.
  705. *
  706. * \sa SDL_CreateAudioStream
  707. * \sa SDL_PutAudioStreamData
  708. * \sa SDL_GetAudioStreamAvailable
  709. * \sa SDL_FlushAudioStream
  710. * \sa SDL_ClearAudioStream
  711. * \sa SDL_DestroyAudioStream
  712. */
  713. extern DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
  714. /**
  715. * Get the number of converted/resampled bytes available.
  716. *
  717. * The stream may be buffering data behind the scenes until it has enough to
  718. * resample correctly, so this number might be lower than what you expect, or
  719. * even be zero. Add more data or flush the stream if you need the data now.
  720. *
  721. * \since This function is available since SDL 3.0.0.
  722. *
  723. * \sa SDL_CreateAudioStream
  724. * \sa SDL_PutAudioStreamData
  725. * \sa SDL_GetAudioStreamData
  726. * \sa SDL_FlushAudioStream
  727. * \sa SDL_ClearAudioStream
  728. * \sa SDL_DestroyAudioStream
  729. */
  730. extern DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
  731. /**
  732. * Tell the stream that you're done sending data, and anything being buffered
  733. * should be converted/resampled and made available immediately.
  734. *
  735. * It is legal to add more data to a stream after flushing, but there will be
  736. * audio gaps in the output. Generally this is intended to signal the end of
  737. * input, so the complete output becomes available.
  738. *
  739. * \since This function is available since SDL 3.0.0.
  740. *
  741. * \sa SDL_CreateAudioStream
  742. * \sa SDL_PutAudioStreamData
  743. * \sa SDL_GetAudioStreamData
  744. * \sa SDL_GetAudioStreamAvailable
  745. * \sa SDL_ClearAudioStream
  746. * \sa SDL_DestroyAudioStream
  747. */
  748. extern DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
  749. /**
  750. * Clear any pending data in the stream without converting it
  751. *
  752. * \param stream The audio stream to clear
  753. * \returns 0 on success or a negative error code on failure; call
  754. * SDL_GetError() for more information.
  755. *
  756. * \since This function is available since SDL 3.0.0.
  757. *
  758. * \sa SDL_CreateAudioStream
  759. * \sa SDL_PutAudioStreamData
  760. * \sa SDL_GetAudioStreamData
  761. * \sa SDL_GetAudioStreamAvailable
  762. * \sa SDL_FlushAudioStream
  763. * \sa SDL_DestroyAudioStream
  764. */
  765. extern DECLSPEC int SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
  766. /**
  767. * Free an audio stream
  768. *
  769. * \since This function is available since SDL 3.0.0.
  770. *
  771. * \sa SDL_CreateAudioStream
  772. * \sa SDL_PutAudioStreamData
  773. * \sa SDL_GetAudioStreamData
  774. * \sa SDL_GetAudioStreamAvailable
  775. * \sa SDL_FlushAudioStream
  776. * \sa SDL_ClearAudioStream
  777. */
  778. extern DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
  779. #define SDL_MIX_MAXVOLUME 128
  780. /**
  781. * Mix audio data in a specified format.
  782. *
  783. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  784. * it into `dst`, performing addition, volume adjustment, and overflow
  785. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  786. * `format` data.
  787. *
  788. * This is provided for convenience -- you can mix your own audio data.
  789. *
  790. * Do not use this function for mixing together more than two streams of
  791. * sample data. The output from repeated application of this function may be
  792. * distorted by clipping, because there is no accumulator with greater range
  793. * than the input (not to mention this being an inefficient way of doing it).
  794. *
  795. * It is a common misconception that this function is required to write audio
  796. * data to an output stream in an audio callback. While you can do that,
  797. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  798. * audio stream with a volume adjustment.
  799. *
  800. * \param dst the destination for the mixed audio
  801. * \param src the source audio buffer to be mixed
  802. * \param format the SDL_AudioFormat structure representing the desired audio
  803. * format
  804. * \param len the length of the audio buffer in bytes
  805. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  806. * for full audio volume
  807. * \returns 0 on success or a negative error code on failure; call
  808. * SDL_GetError() for more information.
  809. *
  810. * \since This function is available since SDL 3.0.0.
  811. */
  812. extern DECLSPEC int SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  813. const Uint8 * src,
  814. SDL_AudioFormat format,
  815. Uint32 len, int volume);
  816. /**
  817. * Queue more audio on non-callback devices.
  818. *
  819. * If you are looking to retrieve queued audio from a non-callback capture
  820. * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
  821. * -1 to signify an error if you use it with capture devices.
  822. *
  823. * SDL offers two ways to feed audio to the device: you can either supply a
  824. * callback that SDL triggers with some frequency to obtain more audio (pull
  825. * method), or you can supply no callback, and then SDL will expect you to
  826. * supply data at regular intervals (push method) with this function.
  827. *
  828. * There are no limits on the amount of data you can queue, short of
  829. * exhaustion of address space. Queued data will drain to the device as
  830. * necessary without further intervention from you. If the device needs audio
  831. * but there is not enough queued, it will play silence to make up the
  832. * difference. This means you will have skips in your audio playback if you
  833. * aren't routinely queueing sufficient data.
  834. *
  835. * This function copies the supplied data, so you are safe to free it when the
  836. * function returns. This function is thread-safe, but queueing to the same
  837. * device from two threads at once does not promise which buffer will be
  838. * queued first.
  839. *
  840. * You may not queue audio on a device that is using an application-supplied
  841. * callback; doing so returns an error. You have to use the audio callback or
  842. * queue audio with this function, but not both.
  843. *
  844. * You should not call SDL_LockAudio() on the device before queueing; SDL
  845. * handles locking internally for this function.
  846. *
  847. * Note that SDL does not support planar audio. You will need to resample from
  848. * planar audio formats into a non-planar one (see SDL_AudioFormat) before
  849. * queuing audio.
  850. *
  851. * \param dev the device ID to which we will queue audio
  852. * \param data the data to queue to the device for later playback
  853. * \param len the number of bytes (not samples!) to which `data` points
  854. * \returns 0 on success or a negative error code on failure; call
  855. * SDL_GetError() for more information.
  856. *
  857. * \since This function is available since SDL 3.0.0.
  858. *
  859. * \sa SDL_ClearQueuedAudio
  860. * \sa SDL_GetQueuedAudioSize
  861. */
  862. extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
  863. /**
  864. * Dequeue more audio on non-callback devices.
  865. *
  866. * If you are looking to queue audio for output on a non-callback playback
  867. * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
  868. * return 0 if you use it with playback devices.
  869. *
  870. * SDL offers two ways to retrieve audio from a capture device: you can either
  871. * supply a callback that SDL triggers with some frequency as the device
  872. * records more audio data, (push method), or you can supply no callback, and
  873. * then SDL will expect you to retrieve data at regular intervals (pull
  874. * method) with this function.
  875. *
  876. * There are no limits on the amount of data you can queue, short of
  877. * exhaustion of address space. Data from the device will keep queuing as
  878. * necessary without further intervention from you. This means you will
  879. * eventually run out of memory if you aren't routinely dequeueing data.
  880. *
  881. * Capture devices will not queue data when paused; if you are expecting to
  882. * not need captured audio for some length of time, use SDL_PauseAudioDevice()
  883. * to stop the capture device from queueing more data. This can be useful
  884. * during, say, level loading times. When unpaused, capture devices will start
  885. * queueing data from that point, having flushed any capturable data available
  886. * while paused.
  887. *
  888. * This function is thread-safe, but dequeueing from the same device from two
  889. * threads at once does not promise which thread will dequeue data first.
  890. *
  891. * You may not dequeue audio from a device that is using an
  892. * application-supplied callback; doing so returns an error. You have to use
  893. * the audio callback, or dequeue audio with this function, but not both.
  894. *
  895. * You should not call SDL_LockAudio() on the device before dequeueing; SDL
  896. * handles locking internally for this function.
  897. *
  898. * \param dev the device ID from which we will dequeue audio
  899. * \param data a pointer into where audio data should be copied
  900. * \param len the number of bytes (not samples!) to which (data) points
  901. * \returns the number of bytes dequeued, which could be less than requested;
  902. * call SDL_GetError() for more information.
  903. *
  904. * \since This function is available since SDL 3.0.0.
  905. *
  906. * \sa SDL_ClearQueuedAudio
  907. * \sa SDL_GetQueuedAudioSize
  908. */
  909. extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
  910. /**
  911. * Get the number of bytes of still-queued audio.
  912. *
  913. * For playback devices: this is the number of bytes that have been queued for
  914. * playback with SDL_QueueAudio(), but have not yet been sent to the hardware.
  915. *
  916. * Once we've sent it to the hardware, this function can not decide the exact
  917. * byte boundary of what has been played. It's possible that we just gave the
  918. * hardware several kilobytes right before you called this function, but it
  919. * hasn't played any of it yet, or maybe half of it, etc.
  920. *
  921. * For capture devices, this is the number of bytes that have been captured by
  922. * the device and are waiting for you to dequeue. This number may grow at any
  923. * time, so this only informs of the lower-bound of available data.
  924. *
  925. * You may not queue or dequeue audio on a device that is using an
  926. * application-supplied callback; calling this function on such a device
  927. * always returns 0. You have to use the audio callback or queue audio, but
  928. * not both.
  929. *
  930. * You should not call SDL_LockAudio() on the device before querying; SDL
  931. * handles locking internally for this function.
  932. *
  933. * \param dev the device ID of which we will query queued audio size
  934. * \returns the number of bytes (not samples!) of queued audio.
  935. *
  936. * \since This function is available since SDL 3.0.0.
  937. *
  938. * \sa SDL_ClearQueuedAudio
  939. * \sa SDL_QueueAudio
  940. * \sa SDL_DequeueAudio
  941. */
  942. extern DECLSPEC Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev);
  943. /**
  944. * Drop any queued audio data waiting to be sent to the hardware.
  945. *
  946. * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
  947. * output devices, the hardware will start playing silence if more audio isn't
  948. * queued. For capture devices, the hardware will start filling the empty
  949. * queue with new data if the capture device isn't paused.
  950. *
  951. * This will not prevent playback of queued audio that's already been sent to
  952. * the hardware, as we can not undo that, so expect there to be some fraction
  953. * of a second of audio that might still be heard. This can be useful if you
  954. * want to, say, drop any pending music or any unprocessed microphone input
  955. * during a level change in your game.
  956. *
  957. * You may not queue or dequeue audio on a device that is using an
  958. * application-supplied callback; calling this function on such a device
  959. * always returns 0. You have to use the audio callback or queue audio, but
  960. * not both.
  961. *
  962. * You should not call SDL_LockAudio() on the device before clearing the
  963. * queue; SDL handles locking internally for this function.
  964. *
  965. * This function always succeeds and thus returns void.
  966. *
  967. * \param dev the device ID of which to clear the audio queue
  968. * \returns 0 on success or a negative error code on failure; call
  969. * SDL_GetError() for more information.
  970. *
  971. * \since This function is available since SDL 3.0.0.
  972. *
  973. * \sa SDL_GetQueuedAudioSize
  974. * \sa SDL_QueueAudio
  975. * \sa SDL_DequeueAudio
  976. */
  977. extern DECLSPEC int SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
  978. /**
  979. * \name Audio lock functions
  980. *
  981. * The lock manipulated by these functions protects the callback function.
  982. * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
  983. * the callback function is not running. Do not call these from the callback
  984. * function or you will cause deadlock.
  985. */
  986. /* @{ */
  987. /**
  988. * Use this function to lock out the audio callback function for a specified
  989. * device.
  990. *
  991. * The lock manipulated by these functions protects the audio callback
  992. * function specified in SDL_OpenAudioDevice(). During a
  993. * SDL_LockAudioDevice()/SDL_UnlockAudioDevice() pair, you can be guaranteed
  994. * that the callback function for that device is not running, even if the
  995. * device is not paused. While a device is locked, any other unpaused,
  996. * unlocked devices may still run their callbacks.
  997. *
  998. * Calling this function from inside your audio callback is unnecessary. SDL
  999. * obtains this lock before calling your function, and releases it when the
  1000. * function returns.
  1001. *
  1002. * You should not hold the lock longer than absolutely necessary. If you hold
  1003. * it too long, you'll experience dropouts in your audio playback. Ideally,
  1004. * your application locks the device, sets a few variables and unlocks again.
  1005. * Do not do heavy work while holding the lock for a device.
  1006. *
  1007. * It is safe to lock the audio device multiple times, as long as you unlock
  1008. * it an equivalent number of times. The callback will not run until the
  1009. * device has been unlocked completely in this way. If your application fails
  1010. * to unlock the device appropriately, your callback will never run, you might
  1011. * hear repeating bursts of audio, and SDL_CloseAudioDevice() will probably
  1012. * deadlock.
  1013. *
  1014. * Internally, the audio device lock is a mutex; if you lock from two threads
  1015. * at once, not only will you block the audio callback, you'll block the other
  1016. * thread.
  1017. *
  1018. * \param dev the ID of the device to be locked
  1019. * \returns 0 on success or a negative error code on failure; call
  1020. * SDL_GetError() for more information.
  1021. *
  1022. * \since This function is available since SDL 3.0.0.
  1023. *
  1024. * \sa SDL_UnlockAudioDevice
  1025. */
  1026. extern DECLSPEC int SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
  1027. /**
  1028. * Use this function to unlock the audio callback function for a specified
  1029. * device.
  1030. *
  1031. * This function should be paired with a previous SDL_LockAudioDevice() call.
  1032. *
  1033. * \param dev the ID of the device to be unlocked
  1034. *
  1035. * \since This function is available since SDL 3.0.0.
  1036. *
  1037. * \sa SDL_LockAudioDevice
  1038. */
  1039. extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
  1040. /* @} *//* Audio lock functions */
  1041. /**
  1042. * Use this function to shut down audio processing and close the audio device.
  1043. *
  1044. * The application should close open audio devices once they are no longer
  1045. * needed. Calling this function will wait until the device's audio callback
  1046. * is not running, release the audio hardware and then clean up internal
  1047. * state. No further audio will play from this device once this function
  1048. * returns.
  1049. *
  1050. * This function may block briefly while pending audio data is played by the
  1051. * hardware, so that applications don't drop the last buffer of data they
  1052. * supplied.
  1053. *
  1054. * The device ID is invalid as soon as the device is closed, and is eligible
  1055. * for reuse in a new SDL_OpenAudioDevice() call immediately.
  1056. *
  1057. * \param dev an audio device previously opened with SDL_OpenAudioDevice()
  1058. *
  1059. * \since This function is available since SDL 3.0.0.
  1060. *
  1061. * \sa SDL_OpenAudioDevice
  1062. */
  1063. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
  1064. /**
  1065. * Convert some audio data of one format to another format.
  1066. *
  1067. * \param src_format The format of the source audio
  1068. * \param src_channels The number of channels of the source audio
  1069. * \param src_rate The sampling rate of the source audio
  1070. * \param src_len The len of src_data
  1071. * \param src_data The audio data to be converted
  1072. * \param dst_format The format of the desired audio output
  1073. * \param dst_channels The number of channels of the desired audio output
  1074. * \param dst_rate The sampling rate of the desired audio output
  1075. * \param dst_len Will be filled with the len of dst_data
  1076. * \param dst_data Will be filled with a pointer to converted audio data,
  1077. * which should be freed with SDL_free().
  1078. * \returns 0 on success or a negative error code on failure. On error,
  1079. * *dst_data will be NULL and so not allocated.
  1080. *
  1081. * \since This function is available since SDL 3.0.0.
  1082. *
  1083. * \sa SDL_CreateAudioStream
  1084. */
  1085. extern DECLSPEC int SDLCALL SDL_ConvertAudioSamples(SDL_AudioFormat src_format,
  1086. Uint8 src_channels,
  1087. int src_rate,
  1088. int src_len,
  1089. Uint8 *src_data,
  1090. SDL_AudioFormat dst_format,
  1091. Uint8 dst_channels,
  1092. int dst_rate,
  1093. int *dst_len,
  1094. Uint8 **dst_data);
  1095. /* Ends C function definitions when using C++ */
  1096. #ifdef __cplusplus
  1097. }
  1098. #endif
  1099. #include <SDL3/SDL_close_code.h>
  1100. #endif /* SDL_audio_h_ */