SDL_audio.h 47 KB

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