SDL_audio.h 51 KB

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