SDL_audio.h 52 KB

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