SDL_audio.h 47 KB

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