SDL_audio.h 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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 stream The stream the format is being changed
  712. * \param src_format The format of the audio input
  713. * \param src_channels The number of channels of the audio input
  714. * \param src_rate The sampling rate of the audio input
  715. * \param dst_format The format of the desired audio output
  716. * \param dst_channels The number of channels of the desired audio output
  717. * \param dst_rate The sampling rate of the desired audio output
  718. * \returns 0 on success, or -1 on error.
  719. *
  720. * \threadsafety It is safe to call this function from any thread, as it holds
  721. * a stream-specific mutex while running.
  722. *
  723. * \since This function is available since SDL 3.0.0.
  724. *
  725. * \sa SDL_GetAudioStreamFormat
  726. * \sa SDL_PutAudioStreamData
  727. * \sa SDL_GetAudioStreamData
  728. * \sa SDL_GetAudioStreamAvailable
  729. */
  730. extern DECLSPEC int SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream,
  731. SDL_AudioFormat src_format,
  732. int src_channels,
  733. int src_rate,
  734. SDL_AudioFormat dst_format,
  735. int dst_channels,
  736. int dst_rate);
  737. /**
  738. * Add data to be converted/resampled to the stream.
  739. *
  740. * This data must match the format/channels/samplerate specified in the latest
  741. * call to SDL_SetAudioStreamFormat, or the format specified when creating the
  742. * stream if it hasn't been changed.
  743. *
  744. * Note that this call simply queues unconverted data for later. This is
  745. * different than SDL2, where data was converted during the Put call and the
  746. * Get call would just dequeue the previously-converted data.
  747. *
  748. * \param stream The stream the audio data is being added to
  749. * \param buf A pointer to the audio data to add
  750. * \param len The number of bytes to write to the stream
  751. * \returns 0 on success or a negative error code on failure; call
  752. * SDL_GetError() for more information.
  753. *
  754. * \since This function is available since SDL 3.0.0.
  755. *
  756. * \sa SDL_CreateAudioStream
  757. * \sa SDL_GetAudioStreamData
  758. * \sa SDL_GetAudioStreamAvailable
  759. * \sa SDL_FlushAudioStream
  760. * \sa SDL_ClearAudioStream
  761. * \sa SDL_DestroyAudioStream
  762. */
  763. extern DECLSPEC int SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
  764. /**
  765. * Get converted/resampled data from the stream.
  766. *
  767. * The input/output data format/channels/samplerate is specified when creating
  768. * the stream, and can be changed after creation by calling
  769. * SDL_SetAudioStreamFormat.
  770. *
  771. * Note that any conversion and resampling necessary is done during this call,
  772. * and SDL_PutAudioStreamData simply queues unconverted data for later. This
  773. * is different than SDL2, where that work was done while inputting new data
  774. * to the stream and requesting the output just copied the converted data.
  775. *
  776. * \param stream The stream the audio is being requested from
  777. * \param buf A buffer to fill with audio data
  778. * \param len The maximum number of bytes to fill
  779. * \returns the number of bytes read from the stream, or -1 on error
  780. *
  781. * \since This function is available since SDL 3.0.0.
  782. *
  783. * \sa SDL_CreateAudioStream
  784. * \sa SDL_PutAudioStreamData
  785. * \sa SDL_GetAudioStreamAvailable
  786. * \sa SDL_SetAudioStreamFormat
  787. * \sa SDL_FlushAudioStream
  788. * \sa SDL_ClearAudioStream
  789. * \sa SDL_DestroyAudioStream
  790. */
  791. extern DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
  792. /**
  793. * Get the number of converted/resampled bytes available.
  794. *
  795. * The stream may be buffering data behind the scenes until it has enough to
  796. * resample correctly, so this number might be lower than what you expect, or
  797. * even be zero. Add more data or flush the stream if you need the data now.
  798. *
  799. * If the stream has so much data that it would overflow an int, the return
  800. * value is clamped to a maximum value, but no queued data is lost; if there
  801. * are gigabytes of data queued, the app might need to read some of it with
  802. * SDL_GetAudioStreamData before this function's return value is no longer
  803. * clamped.
  804. *
  805. * \param stream The audio stream to query
  806. * \returns the number of converted/resampled bytes available.
  807. *
  808. * \since This function is available since SDL 3.0.0.
  809. *
  810. * \sa SDL_CreateAudioStream
  811. * \sa SDL_PutAudioStreamData
  812. * \sa SDL_GetAudioStreamData
  813. * \sa SDL_FlushAudioStream
  814. * \sa SDL_ClearAudioStream
  815. * \sa SDL_DestroyAudioStream
  816. */
  817. extern DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
  818. /**
  819. * Tell the stream that you're done sending data, and anything being buffered
  820. * should be converted/resampled and made available immediately.
  821. *
  822. * It is legal to add more data to a stream after flushing, but there will be
  823. * audio gaps in the output. Generally this is intended to signal the end of
  824. * input, so the complete output becomes available.
  825. *
  826. * \param stream The audio stream to flush
  827. * \returns 0 on success or a negative error code on failure; call
  828. * SDL_GetError() for more information.
  829. *
  830. * \since This function is available since SDL 3.0.0.
  831. *
  832. * \sa SDL_CreateAudioStream
  833. * \sa SDL_PutAudioStreamData
  834. * \sa SDL_GetAudioStreamData
  835. * \sa SDL_GetAudioStreamAvailable
  836. * \sa SDL_ClearAudioStream
  837. * \sa SDL_DestroyAudioStream
  838. */
  839. extern DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
  840. /**
  841. * Clear any pending data in the stream without converting it
  842. *
  843. * \param stream The audio stream to clear
  844. * \returns 0 on success or a negative error code on failure; call
  845. * SDL_GetError() for more information.
  846. *
  847. * \since This function is available since SDL 3.0.0.
  848. *
  849. * \sa SDL_CreateAudioStream
  850. * \sa SDL_PutAudioStreamData
  851. * \sa SDL_GetAudioStreamData
  852. * \sa SDL_GetAudioStreamAvailable
  853. * \sa SDL_FlushAudioStream
  854. * \sa SDL_DestroyAudioStream
  855. */
  856. extern DECLSPEC int SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
  857. /**
  858. * Free an audio stream
  859. *
  860. * \param stream The audio stream to free
  861. *
  862. * \since This function is available since SDL 3.0.0.
  863. *
  864. * \sa SDL_CreateAudioStream
  865. * \sa SDL_PutAudioStreamData
  866. * \sa SDL_GetAudioStreamData
  867. * \sa SDL_GetAudioStreamAvailable
  868. * \sa SDL_FlushAudioStream
  869. * \sa SDL_ClearAudioStream
  870. */
  871. extern DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
  872. #define SDL_MIX_MAXVOLUME 128
  873. /**
  874. * Mix audio data in a specified format.
  875. *
  876. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  877. * it into `dst`, performing addition, volume adjustment, and overflow
  878. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  879. * `format` data.
  880. *
  881. * This is provided for convenience -- you can mix your own audio data.
  882. *
  883. * Do not use this function for mixing together more than two streams of
  884. * sample data. The output from repeated application of this function may be
  885. * distorted by clipping, because there is no accumulator with greater range
  886. * than the input (not to mention this being an inefficient way of doing it).
  887. *
  888. * It is a common misconception that this function is required to write audio
  889. * data to an output stream in an audio callback. While you can do that,
  890. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  891. * audio stream with a volume adjustment.
  892. *
  893. * \param dst the destination for the mixed audio
  894. * \param src the source audio buffer to be mixed
  895. * \param format the SDL_AudioFormat structure representing the desired audio
  896. * format
  897. * \param len the length of the audio buffer in bytes
  898. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  899. * for full audio volume
  900. * \returns 0 on success or a negative error code on failure; call
  901. * SDL_GetError() for more information.
  902. *
  903. * \since This function is available since SDL 3.0.0.
  904. */
  905. extern DECLSPEC int SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  906. const Uint8 * src,
  907. SDL_AudioFormat format,
  908. Uint32 len, int volume);
  909. /**
  910. * Queue more audio on non-callback devices.
  911. *
  912. * If you are looking to retrieve queued audio from a non-callback capture
  913. * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
  914. * -1 to signify an error if you use it with capture devices.
  915. *
  916. * SDL offers two ways to feed audio to the device: you can either supply a
  917. * callback that SDL triggers with some frequency to obtain more audio (pull
  918. * method), or you can supply no callback, and then SDL will expect you to
  919. * supply data at regular intervals (push method) with this function.
  920. *
  921. * There are no limits on the amount of data you can queue, short of
  922. * exhaustion of address space. Queued data will drain to the device as
  923. * necessary without further intervention from you. If the device needs audio
  924. * but there is not enough queued, it will play silence to make up the
  925. * difference. This means you will have skips in your audio playback if you
  926. * aren't routinely queueing sufficient data.
  927. *
  928. * This function copies the supplied data, so you are safe to free it when the
  929. * function returns. This function is thread-safe, but queueing to the same
  930. * device from two threads at once does not promise which buffer will be
  931. * queued first.
  932. *
  933. * You may not queue audio on a device that is using an application-supplied
  934. * callback; doing so returns an error. You have to use the audio callback or
  935. * queue audio with this function, but not both.
  936. *
  937. * You should not call SDL_LockAudio() on the device before queueing; SDL
  938. * handles locking internally for this function.
  939. *
  940. * Note that SDL does not support planar audio. You will need to resample from
  941. * planar audio formats into a non-planar one (see SDL_AudioFormat) before
  942. * queuing audio.
  943. *
  944. * \param dev the device ID to which we will queue audio
  945. * \param data the data to queue to the device for later playback
  946. * \param len the number of bytes (not samples!) to which `data` points
  947. * \returns 0 on success or a negative error code on failure; call
  948. * SDL_GetError() for more information.
  949. *
  950. * \since This function is available since SDL 3.0.0.
  951. *
  952. * \sa SDL_ClearQueuedAudio
  953. * \sa SDL_GetQueuedAudioSize
  954. */
  955. extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
  956. /**
  957. * Dequeue more audio on non-callback devices.
  958. *
  959. * If you are looking to queue audio for output on a non-callback playback
  960. * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
  961. * return 0 if you use it with playback devices.
  962. *
  963. * SDL offers two ways to retrieve audio from a capture device: you can either
  964. * supply a callback that SDL triggers with some frequency as the device
  965. * records more audio data, (push method), or you can supply no callback, and
  966. * then SDL will expect you to retrieve data at regular intervals (pull
  967. * method) with this function.
  968. *
  969. * There are no limits on the amount of data you can queue, short of
  970. * exhaustion of address space. Data from the device will keep queuing as
  971. * necessary without further intervention from you. This means you will
  972. * eventually run out of memory if you aren't routinely dequeueing data.
  973. *
  974. * Capture devices will not queue data when paused; if you are expecting to
  975. * not need captured audio for some length of time, use SDL_PauseAudioDevice()
  976. * to stop the capture device from queueing more data. This can be useful
  977. * during, say, level loading times. When unpaused, capture devices will start
  978. * queueing data from that point, having flushed any capturable data available
  979. * while paused.
  980. *
  981. * This function is thread-safe, but dequeueing from the same device from two
  982. * threads at once does not promise which thread will dequeue data first.
  983. *
  984. * You may not dequeue audio from a device that is using an
  985. * application-supplied callback; doing so returns an error. You have to use
  986. * the audio callback, or dequeue audio with this function, but not both.
  987. *
  988. * You should not call SDL_LockAudio() on the device before dequeueing; SDL
  989. * handles locking internally for this function.
  990. *
  991. * \param dev the device ID from which we will dequeue audio
  992. * \param data a pointer into where audio data should be copied
  993. * \param len the number of bytes (not samples!) to which (data) points
  994. * \returns the number of bytes dequeued, which could be less than requested;
  995. * call SDL_GetError() for more information.
  996. *
  997. * \since This function is available since SDL 3.0.0.
  998. *
  999. * \sa SDL_ClearQueuedAudio
  1000. * \sa SDL_GetQueuedAudioSize
  1001. */
  1002. extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
  1003. /**
  1004. * Get the number of bytes of still-queued audio.
  1005. *
  1006. * For playback devices: this is the number of bytes that have been queued for
  1007. * playback with SDL_QueueAudio(), but have not yet been sent to the hardware.
  1008. *
  1009. * Once we've sent it to the hardware, this function can not decide the exact
  1010. * byte boundary of what has been played. It's possible that we just gave the
  1011. * hardware several kilobytes right before you called this function, but it
  1012. * hasn't played any of it yet, or maybe half of it, etc.
  1013. *
  1014. * For capture devices, this is the number of bytes that have been captured by
  1015. * the device and are waiting for you to dequeue. This number may grow at any
  1016. * time, so this only informs of the lower-bound of available data.
  1017. *
  1018. * You may not queue or dequeue audio on a device that is using an
  1019. * application-supplied callback; calling this function on such a device
  1020. * always returns 0. You have to use the audio callback or queue audio, but
  1021. * not both.
  1022. *
  1023. * You should not call SDL_LockAudio() on the device before querying; SDL
  1024. * handles locking internally for this function.
  1025. *
  1026. * \param dev the device ID of which we will query queued audio size
  1027. * \returns the number of bytes (not samples!) of queued audio.
  1028. *
  1029. * \since This function is available since SDL 3.0.0.
  1030. *
  1031. * \sa SDL_ClearQueuedAudio
  1032. * \sa SDL_QueueAudio
  1033. * \sa SDL_DequeueAudio
  1034. */
  1035. extern DECLSPEC Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev);
  1036. /**
  1037. * Drop any queued audio data waiting to be sent to the hardware.
  1038. *
  1039. * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
  1040. * output devices, the hardware will start playing silence if more audio isn't
  1041. * queued. For capture devices, the hardware will start filling the empty
  1042. * queue with new data if the capture device isn't paused.
  1043. *
  1044. * This will not prevent playback of queued audio that's already been sent to
  1045. * the hardware, as we can not undo that, so expect there to be some fraction
  1046. * of a second of audio that might still be heard. This can be useful if you
  1047. * want to, say, drop any pending music or any unprocessed microphone input
  1048. * during a level change in your game.
  1049. *
  1050. * You may not queue or dequeue audio on a device that is using an
  1051. * application-supplied callback; calling this function on such a device
  1052. * always returns 0. You have to use the audio callback or queue audio, but
  1053. * not both.
  1054. *
  1055. * You should not call SDL_LockAudio() on the device before clearing the
  1056. * queue; SDL handles locking internally for this function.
  1057. *
  1058. * This function always succeeds and thus returns void.
  1059. *
  1060. * \param dev the device ID of which to clear the audio queue
  1061. * \returns 0 on success or a negative error code on failure; call
  1062. * SDL_GetError() for more information.
  1063. *
  1064. * \since This function is available since SDL 3.0.0.
  1065. *
  1066. * \sa SDL_GetQueuedAudioSize
  1067. * \sa SDL_QueueAudio
  1068. * \sa SDL_DequeueAudio
  1069. */
  1070. extern DECLSPEC int SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
  1071. /**
  1072. * \name Audio lock functions
  1073. *
  1074. * The lock manipulated by these functions protects the callback function.
  1075. * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
  1076. * the callback function is not running. Do not call these from the callback
  1077. * function or you will cause deadlock.
  1078. */
  1079. /* @{ */
  1080. /**
  1081. * Use this function to lock out the audio callback function for a specified
  1082. * device.
  1083. *
  1084. * The lock manipulated by these functions protects the audio callback
  1085. * function specified in SDL_OpenAudioDevice(). During a
  1086. * SDL_LockAudioDevice()/SDL_UnlockAudioDevice() pair, you can be guaranteed
  1087. * that the callback function for that device is not running, even if the
  1088. * device is not paused. While a device is locked, any other unpaused,
  1089. * unlocked devices may still run their callbacks.
  1090. *
  1091. * Calling this function from inside your audio callback is unnecessary. SDL
  1092. * obtains this lock before calling your function, and releases it when the
  1093. * function returns.
  1094. *
  1095. * You should not hold the lock longer than absolutely necessary. If you hold
  1096. * it too long, you'll experience dropouts in your audio playback. Ideally,
  1097. * your application locks the device, sets a few variables and unlocks again.
  1098. * Do not do heavy work while holding the lock for a device.
  1099. *
  1100. * It is safe to lock the audio device multiple times, as long as you unlock
  1101. * it an equivalent number of times. The callback will not run until the
  1102. * device has been unlocked completely in this way. If your application fails
  1103. * to unlock the device appropriately, your callback will never run, you might
  1104. * hear repeating bursts of audio, and SDL_CloseAudioDevice() will probably
  1105. * deadlock.
  1106. *
  1107. * Internally, the audio device lock is a mutex; if you lock from two threads
  1108. * at once, not only will you block the audio callback, you'll block the other
  1109. * thread.
  1110. *
  1111. * \param dev the ID of the device to be locked
  1112. * \returns 0 on success or a negative error code on failure; call
  1113. * SDL_GetError() for more information.
  1114. *
  1115. * \since This function is available since SDL 3.0.0.
  1116. *
  1117. * \sa SDL_UnlockAudioDevice
  1118. */
  1119. extern DECLSPEC int SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
  1120. /**
  1121. * Use this function to unlock the audio callback function for a specified
  1122. * device.
  1123. *
  1124. * This function should be paired with a previous SDL_LockAudioDevice() call.
  1125. *
  1126. * \param dev the ID of the device to be unlocked
  1127. *
  1128. * \since This function is available since SDL 3.0.0.
  1129. *
  1130. * \sa SDL_LockAudioDevice
  1131. */
  1132. extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
  1133. /* @} *//* Audio lock functions */
  1134. /**
  1135. * Use this function to shut down audio processing and close the audio device.
  1136. *
  1137. * The application should close open audio devices once they are no longer
  1138. * needed. Calling this function will wait until the device's audio callback
  1139. * is not running, release the audio hardware and then clean up internal
  1140. * state. No further audio will play from this device once this function
  1141. * returns.
  1142. *
  1143. * This function may block briefly while pending audio data is played by the
  1144. * hardware, so that applications don't drop the last buffer of data they
  1145. * supplied.
  1146. *
  1147. * The device ID is invalid as soon as the device is closed, and is eligible
  1148. * for reuse in a new SDL_OpenAudioDevice() call immediately.
  1149. *
  1150. * \param dev an audio device previously opened with SDL_OpenAudioDevice()
  1151. *
  1152. * \since This function is available since SDL 3.0.0.
  1153. *
  1154. * \sa SDL_OpenAudioDevice
  1155. */
  1156. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
  1157. /**
  1158. * Convert some audio data of one format to another format.
  1159. *
  1160. * Please note that this function is for convenience, but should not be used
  1161. * to resample audio in blocks, as it will introduce audio artifacts on the
  1162. * boundaries. You should only use this function if you are converting audio
  1163. * data in its entirety in one call. If you want to convert audio in smaller
  1164. * chunks, use an SDL_AudioStream, which is designed for this situation.
  1165. *
  1166. * Internally, this function creates and destroys an SDL_AudioStream on each
  1167. * use, so it's also less efficient than using one directly, if you need to
  1168. * convert multiple times.
  1169. *
  1170. * \param src_format The format of the source audio
  1171. * \param src_channels The number of channels of the source audio
  1172. * \param src_rate The sampling rate of the source audio
  1173. * \param src_data The audio data to be converted
  1174. * \param src_len The len of src_data
  1175. * \param dst_format The format of the desired audio output
  1176. * \param dst_channels The number of channels of the desired audio output
  1177. * \param dst_rate The sampling rate of the desired audio output
  1178. * \param dst_data Will be filled with a pointer to converted audio data,
  1179. * which should be freed with SDL_free(). On error, it will be
  1180. * NULL.
  1181. * \param dst_len Will be filled with the len of dst_data
  1182. * \returns 0 on success or a negative error code on failure; call
  1183. * SDL_GetError() for more information.
  1184. *
  1185. * \since This function is available since SDL 3.0.0.
  1186. *
  1187. * \sa SDL_CreateAudioStream
  1188. */
  1189. extern DECLSPEC int SDLCALL SDL_ConvertAudioSamples(SDL_AudioFormat src_format,
  1190. Uint8 src_channels,
  1191. int src_rate,
  1192. const Uint8 *src_data,
  1193. int src_len,
  1194. SDL_AudioFormat dst_format,
  1195. Uint8 dst_channels,
  1196. int dst_rate,
  1197. Uint8 **dst_data,
  1198. int *dst_len);
  1199. /* Ends C function definitions when using C++ */
  1200. #ifdef __cplusplus
  1201. }
  1202. #endif
  1203. #include <SDL3/SDL_close_code.h>
  1204. #endif /* SDL_audio_h_ */