SDL_audio.h 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. * Access to the raw audio mixing buffer for the SDL library.
  23. */
  24. #ifndef SDL_audio_h_
  25. #define SDL_audio_h_
  26. #include <SDL3/SDL_stdinc.h>
  27. #include <SDL3/SDL_error.h>
  28. #include <SDL3/SDL_endian.h>
  29. #include <SDL3/SDL_mutex.h>
  30. #include <SDL3/SDL_thread.h>
  31. #include <SDL3/SDL_rwops.h>
  32. #include <SDL3/SDL_begin_code.h>
  33. /* Set up for C function definitions, even when using C++ */
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * \brief Audio format flags.
  39. *
  40. * These are what the 16 bits in SDL_AudioFormat currently mean...
  41. * (Unspecified bits are always zero).
  42. *
  43. * \verbatim
  44. ++-----------------------sample is signed if set
  45. ||
  46. || ++-----------sample is bigendian if set
  47. || ||
  48. || || ++---sample is float if set
  49. || || ||
  50. || || || +---sample bit size---+
  51. || || || | |
  52. 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
  53. \endverbatim
  54. *
  55. * There are macros in SDL 2.0 and later to query these bits.
  56. */
  57. typedef Uint16 SDL_AudioFormat;
  58. /**
  59. * \name Audio flags
  60. */
  61. /* @{ */
  62. #define SDL_AUDIO_MASK_BITSIZE (0xFF)
  63. #define SDL_AUDIO_MASK_DATATYPE (1<<8)
  64. #define SDL_AUDIO_MASK_ENDIAN (1<<12)
  65. #define SDL_AUDIO_MASK_SIGNED (1<<15)
  66. #define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
  67. #define SDL_AUDIO_ISFLOAT(x) (x & SDL_AUDIO_MASK_DATATYPE)
  68. #define SDL_AUDIO_ISBIGENDIAN(x) (x & SDL_AUDIO_MASK_ENDIAN)
  69. #define SDL_AUDIO_ISSIGNED(x) (x & SDL_AUDIO_MASK_SIGNED)
  70. #define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))
  71. #define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))
  72. #define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))
  73. /**
  74. * \name Audio format flags
  75. *
  76. * Defaults to LSB byte order.
  77. */
  78. /* @{ */
  79. #define AUDIO_U8 0x0008 /**< Unsigned 8-bit samples */
  80. #define AUDIO_S8 0x8008 /**< Signed 8-bit samples */
  81. #define AUDIO_U16LSB 0x0010 /**< Unsigned 16-bit samples */
  82. #define AUDIO_S16LSB 0x8010 /**< Signed 16-bit samples */
  83. #define AUDIO_U16MSB 0x1010 /**< As above, but big-endian byte order */
  84. #define AUDIO_S16MSB 0x9010 /**< As above, but big-endian byte order */
  85. #define AUDIO_U16 AUDIO_U16LSB
  86. #define AUDIO_S16 AUDIO_S16LSB
  87. /* @} */
  88. /**
  89. * \name int32 support
  90. */
  91. /* @{ */
  92. #define AUDIO_S32LSB 0x8020 /**< 32-bit integer samples */
  93. #define AUDIO_S32MSB 0x9020 /**< As above, but big-endian byte order */
  94. #define AUDIO_S32 AUDIO_S32LSB
  95. /* @} */
  96. /**
  97. * \name float32 support
  98. */
  99. /* @{ */
  100. #define AUDIO_F32LSB 0x8120 /**< 32-bit floating point samples */
  101. #define AUDIO_F32MSB 0x9120 /**< As above, but big-endian byte order */
  102. #define AUDIO_F32 AUDIO_F32LSB
  103. /* @} */
  104. /**
  105. * \name Native audio byte ordering
  106. */
  107. /* @{ */
  108. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  109. #define AUDIO_U16SYS AUDIO_U16LSB
  110. #define AUDIO_S16SYS AUDIO_S16LSB
  111. #define AUDIO_S32SYS AUDIO_S32LSB
  112. #define AUDIO_F32SYS AUDIO_F32LSB
  113. #else
  114. #define AUDIO_U16SYS AUDIO_U16MSB
  115. #define AUDIO_S16SYS AUDIO_S16MSB
  116. #define AUDIO_S32SYS AUDIO_S32MSB
  117. #define AUDIO_F32SYS AUDIO_F32MSB
  118. #endif
  119. /* @} */
  120. /**
  121. * \name Allow change flags
  122. *
  123. * Which audio format changes are allowed when opening a device.
  124. */
  125. /* @{ */
  126. #define SDL_AUDIO_ALLOW_FREQUENCY_CHANGE 0x00000001
  127. #define SDL_AUDIO_ALLOW_FORMAT_CHANGE 0x00000002
  128. #define SDL_AUDIO_ALLOW_CHANNELS_CHANGE 0x00000004
  129. #define SDL_AUDIO_ALLOW_SAMPLES_CHANGE 0x00000008
  130. #define SDL_AUDIO_ALLOW_ANY_CHANGE (SDL_AUDIO_ALLOW_FREQUENCY_CHANGE|SDL_AUDIO_ALLOW_FORMAT_CHANGE|SDL_AUDIO_ALLOW_CHANNELS_CHANGE|SDL_AUDIO_ALLOW_SAMPLES_CHANGE)
  131. /* @} */
  132. /* @} *//* Audio flags */
  133. /**
  134. * This function is called when the audio device needs more data.
  135. *
  136. * \param userdata An application-specific parameter saved in
  137. * the SDL_AudioSpec structure
  138. * \param stream A pointer to the audio data buffer.
  139. * \param len The length of that buffer in bytes.
  140. *
  141. * Once the callback returns, the buffer will no longer be valid.
  142. * Stereo samples are stored in a LRLRLR ordering.
  143. *
  144. * You can choose to avoid callbacks and use SDL_QueueAudio() instead, if
  145. * you like. Just open your audio device with a NULL callback.
  146. */
  147. typedef void (SDLCALL * SDL_AudioCallback) (void *userdata, Uint8 * stream,
  148. int len);
  149. /**
  150. * The calculated values in this structure are calculated by SDL_OpenAudio().
  151. *
  152. * For multi-channel audio, the default SDL channel mapping is:
  153. * 2: FL FR (stereo)
  154. * 3: FL FR LFE (2.1 surround)
  155. * 4: FL FR BL BR (quad)
  156. * 5: FL FR LFE BL BR (4.1 surround)
  157. * 6: FL FR FC LFE SL SR (5.1 surround - last two can also be BL BR)
  158. * 7: FL FR FC LFE BC SL SR (6.1 surround)
  159. * 8: FL FR FC LFE BL BR SL SR (7.1 surround)
  160. */
  161. typedef struct SDL_AudioSpec
  162. {
  163. int freq; /**< DSP frequency -- samples per second */
  164. SDL_AudioFormat format; /**< Audio data format */
  165. Uint8 channels; /**< Number of channels: 1 mono, 2 stereo */
  166. Uint8 silence; /**< Audio buffer silence value (calculated) */
  167. Uint16 samples; /**< Audio buffer size in sample FRAMES (total samples divided by channel count) */
  168. Uint16 padding; /**< Necessary for some compile environments */
  169. Uint32 size; /**< Audio buffer size in bytes (calculated) */
  170. SDL_AudioCallback callback; /**< Callback that feeds the audio device (NULL to use SDL_QueueAudio()). */
  171. void *userdata; /**< Userdata passed to callback (ignored for NULL callbacks). */
  172. } SDL_AudioSpec;
  173. struct SDL_AudioCVT;
  174. typedef void (SDLCALL * SDL_AudioFilter) (struct SDL_AudioCVT * cvt,
  175. SDL_AudioFormat format);
  176. /**
  177. * \brief Upper limit of filters in SDL_AudioCVT
  178. *
  179. * The maximum number of SDL_AudioFilter functions in SDL_AudioCVT is
  180. * currently limited to 9. The SDL_AudioCVT.filters array has 10 pointers,
  181. * one of which is the terminating NULL pointer.
  182. */
  183. #define SDL_AUDIOCVT_MAX_FILTERS 9
  184. /**
  185. * \struct SDL_AudioCVT
  186. * \brief A structure to hold a set of audio conversion filters and buffers.
  187. *
  188. * Note that various parts of the conversion pipeline can take advantage
  189. * of SIMD operations (like SSE2, for example). SDL_AudioCVT doesn't require
  190. * you to pass it aligned data, but can possibly run much faster if you
  191. * set both its (buf) field to a pointer that is aligned to 16 bytes, and its
  192. * (len) field to something that's a multiple of 16, if possible.
  193. */
  194. #if defined(__GNUC__) && !defined(__CHERI_PURE_CAPABILITY__)
  195. /* This structure is 84 bytes on 32-bit architectures, make sure GCC doesn't
  196. pad it out to 88 bytes to guarantee ABI compatibility between compilers.
  197. This is not a concern on CHERI architectures, where pointers must be stored
  198. at aligned locations otherwise they will become invalid, and thus structs
  199. containing pointers cannot be packed without giving a warning or error.
  200. vvv
  201. The next time we rev the ABI, make sure to size the ints and add padding.
  202. */
  203. #define SDL_AUDIOCVT_PACKED __attribute__((packed))
  204. #else
  205. #define SDL_AUDIOCVT_PACKED
  206. #endif
  207. /* */
  208. typedef struct SDL_AudioCVT
  209. {
  210. int needed; /**< Set to 1 if conversion possible */
  211. SDL_AudioFormat src_format; /**< Source audio format */
  212. SDL_AudioFormat dst_format; /**< Target audio format */
  213. double rate_incr; /**< Rate conversion increment */
  214. Uint8 *buf; /**< Buffer to hold entire audio data */
  215. int len; /**< Length of original audio buffer */
  216. int len_cvt; /**< Length of converted audio buffer */
  217. int len_mult; /**< buffer must be len*len_mult big */
  218. double len_ratio; /**< Given len, final size is len*len_ratio */
  219. SDL_AudioFilter filters[SDL_AUDIOCVT_MAX_FILTERS + 1]; /**< NULL-terminated list of filter functions */
  220. int filter_index; /**< Current audio conversion function */
  221. } SDL_AUDIOCVT_PACKED SDL_AudioCVT;
  222. /* Function prototypes */
  223. /**
  224. * \name Driver discovery functions
  225. *
  226. * These functions return the list of built in audio drivers, in the
  227. * order that they are normally initialized by default.
  228. */
  229. /* @{ */
  230. /**
  231. * Use this function to get the number of built-in audio drivers.
  232. *
  233. * This function returns a hardcoded number. This never returns a negative
  234. * value; if there are no drivers compiled into this build of SDL, this
  235. * function returns zero. The presence of a driver in this list does not mean
  236. * it will function, it just means SDL is capable of interacting with that
  237. * interface. For example, a build of SDL might have esound support, but if
  238. * there's no esound server available, SDL's esound driver would fail if used.
  239. *
  240. * By default, SDL tries all drivers, in its preferred order, until one is
  241. * found to be usable.
  242. *
  243. * \returns the number of built-in audio drivers.
  244. *
  245. * \since This function is available since SDL 3.0.0.
  246. *
  247. * \sa SDL_GetAudioDriver
  248. */
  249. extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
  250. /**
  251. * Use this function to get the name of a built in audio driver.
  252. *
  253. * The list of audio drivers is given in the order that they are normally
  254. * initialized by default; the drivers that seem more reasonable to choose
  255. * first (as far as the SDL developers believe) are earlier in the list.
  256. *
  257. * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
  258. * "coreaudio" or "xaudio2". These never have Unicode characters, and are not
  259. * meant to be proper names.
  260. *
  261. * \param index the index of the audio driver; the value ranges from 0 to
  262. * SDL_GetNumAudioDrivers() - 1
  263. * \returns the name of the audio driver at the requested index, or NULL if an
  264. * invalid index was specified.
  265. *
  266. * \since This function is available since SDL 3.0.0.
  267. *
  268. * \sa SDL_GetNumAudioDrivers
  269. */
  270. extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
  271. /* @} */
  272. /**
  273. * Get the name of the current audio driver.
  274. *
  275. * The returned string points to internal static memory and thus never becomes
  276. * invalid, even if you quit the audio subsystem and initialize a new driver
  277. * (although such a case would return a different static string from another
  278. * call to this function, of course). As such, you should not modify or free
  279. * the returned string.
  280. *
  281. * \returns the name of the current audio driver or NULL if no driver has been
  282. * initialized.
  283. *
  284. * \since This function is available since SDL 3.0.0.
  285. */
  286. extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
  287. /**
  288. * This function is a legacy means of opening the audio device.
  289. *
  290. * This function remains for compatibility with SDL 1.2, but also because it's
  291. * slightly easier to use than the new functions in SDL 2.0. The new, more
  292. * powerful, and preferred way to do this is SDL_OpenAudioDevice().
  293. *
  294. * This function is roughly equivalent to:
  295. *
  296. * ```c
  297. * SDL_OpenAudioDevice(NULL, 0, desired, obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
  298. * ```
  299. *
  300. * With two notable exceptions:
  301. *
  302. * - If `obtained` is NULL, we use `desired` (and allow no changes), which
  303. * means desired will be modified to have the correct values for silence,
  304. * etc, and SDL will convert any differences between your app's specific
  305. * request and the hardware behind the scenes.
  306. * - The return value is always success or failure, and not a device ID, which
  307. * means you can only have one device open at a time with this function.
  308. *
  309. * \param desired an SDL_AudioSpec structure representing the desired output
  310. * format. Please refer to the SDL_OpenAudioDevice
  311. * documentation for details on how to prepare this structure.
  312. * \param obtained an SDL_AudioSpec structure filled in with the actual
  313. * parameters, or NULL.
  314. * \returns 0 if successful, placing the actual hardware parameters in the
  315. * structure pointed to by `obtained`.
  316. *
  317. * If `obtained` is NULL, the audio data passed to the callback
  318. * function will be guaranteed to be in the requested format, and
  319. * will be automatically converted to the actual hardware audio
  320. * format if necessary. If `obtained` is NULL, `desired` will have
  321. * fields modified.
  322. *
  323. * This function returns a negative error code on failure to open the
  324. * audio device or failure to set up the audio thread; call
  325. * SDL_GetError() for more information.
  326. *
  327. * \since This function is available since SDL 3.0.0.
  328. *
  329. * \sa SDL_CloseAudio
  330. * \sa SDL_LockAudio
  331. * \sa SDL_PauseAudio
  332. * \sa SDL_UnlockAudio
  333. */
  334. extern DECLSPEC int SDLCALL SDL_OpenAudio(SDL_AudioSpec * desired,
  335. SDL_AudioSpec * obtained);
  336. /**
  337. * SDL Audio Device IDs.
  338. *
  339. * A successful call to SDL_OpenAudio() is always device id 1, and legacy
  340. * SDL audio APIs assume you want this device ID. SDL_OpenAudioDevice() calls
  341. * always returns devices >= 2 on success. The legacy calls are good both
  342. * for backwards compatibility and when you don't care about multiple,
  343. * specific, or capture devices.
  344. */
  345. typedef Uint32 SDL_AudioDeviceID;
  346. /**
  347. * Get the number of built-in audio devices.
  348. *
  349. * This function is only valid after successfully initializing the audio
  350. * subsystem.
  351. *
  352. * Note that audio capture support is not implemented as of SDL 2.0.4, so the
  353. * `iscapture` parameter is for future expansion and should always be zero for
  354. * now.
  355. *
  356. * This function will return -1 if an explicit list of devices can't be
  357. * determined. Returning -1 is not an error. For example, if SDL is set up to
  358. * talk to a remote audio server, it can't list every one available on the
  359. * Internet, but it will still allow a specific host to be specified in
  360. * SDL_OpenAudioDevice().
  361. *
  362. * In many common cases, when this function returns a value <= 0, it can still
  363. * successfully open the default device (NULL for first argument of
  364. * SDL_OpenAudioDevice()).
  365. *
  366. * This function may trigger a complete redetect of available hardware. It
  367. * should not be called for each iteration of a loop, but rather once at the
  368. * start of a loop:
  369. *
  370. * ```c
  371. * // Don't do this:
  372. * for (int i = 0; i < SDL_GetNumAudioDevices(0); i++)
  373. *
  374. * // do this instead:
  375. * const int count = SDL_GetNumAudioDevices(0);
  376. * for (int i = 0; i < count; ++i) { do_something_here(); }
  377. * ```
  378. *
  379. * \param iscapture zero to request playback devices, non-zero to request
  380. * recording devices
  381. * \returns the number of available devices exposed by the current driver or
  382. * -1 if an explicit list of devices can't be determined. A return
  383. * value of -1 does not necessarily mean an error condition.
  384. *
  385. * \since This function is available since SDL 3.0.0.
  386. *
  387. * \sa SDL_GetAudioDeviceName
  388. * \sa SDL_OpenAudioDevice
  389. */
  390. extern DECLSPEC int SDLCALL SDL_GetNumAudioDevices(int iscapture);
  391. /**
  392. * Get the human-readable name of a specific audio device.
  393. *
  394. * This function is only valid after successfully initializing the audio
  395. * subsystem. The values returned by this function reflect the latest call to
  396. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  397. * hardware.
  398. *
  399. * The string returned by this function is UTF-8 encoded, read-only, and
  400. * managed internally. You are not to free it. If you need to keep the string
  401. * for any length of time, you should make your own copy of it, as it will be
  402. * invalid next time any of several other SDL functions are called.
  403. *
  404. * \param index the index of the audio device; valid values range from 0 to
  405. * SDL_GetNumAudioDevices() - 1
  406. * \param iscapture non-zero to query the list of recording devices, zero to
  407. * query the list of output devices.
  408. * \returns the name of the audio device at the requested index, or NULL on
  409. * error.
  410. *
  411. * \since This function is available since SDL 3.0.0.
  412. *
  413. * \sa SDL_GetNumAudioDevices
  414. * \sa SDL_GetDefaultAudioInfo
  415. */
  416. extern DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(int index,
  417. int iscapture);
  418. /**
  419. * Get the preferred audio format of a specific audio device.
  420. *
  421. * This function is only valid after a successfully initializing the audio
  422. * subsystem. The values returned by this function reflect the latest call to
  423. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  424. * hardware.
  425. *
  426. * `spec` will be filled with the sample rate, sample format, and channel
  427. * count.
  428. *
  429. * \param index the index of the audio device; valid values range from 0 to
  430. * SDL_GetNumAudioDevices() - 1
  431. * \param iscapture non-zero to query the list of recording devices, zero to
  432. * query the list of output devices.
  433. * \param spec The SDL_AudioSpec to be initialized by this function.
  434. * \returns 0 on success, nonzero on error
  435. *
  436. * \since This function is available since SDL 3.0.0.
  437. *
  438. * \sa SDL_GetNumAudioDevices
  439. * \sa SDL_GetDefaultAudioInfo
  440. */
  441. extern DECLSPEC int SDLCALL SDL_GetAudioDeviceSpec(int index,
  442. int iscapture,
  443. SDL_AudioSpec *spec);
  444. /**
  445. * Get the name and preferred format of the default audio device.
  446. *
  447. * Some (but not all!) platforms have an isolated mechanism to get information
  448. * about the "default" device. This can actually be a completely different
  449. * device that's not in the list you get from SDL_GetAudioDeviceSpec(). It can
  450. * even be a network address! (This is discussed in SDL_OpenAudioDevice().)
  451. *
  452. * As a result, this call is not guaranteed to be performant, as it can query
  453. * the sound server directly every time, unlike the other query functions. You
  454. * should call this function sparingly!
  455. *
  456. * `spec` will be filled with the sample rate, sample format, and channel
  457. * count, if a default device exists on the system. If `name` is provided,
  458. * will be filled with either a dynamically-allocated UTF-8 string or NULL.
  459. *
  460. * \param name A pointer to be filled with the name of the default device (can
  461. * be NULL). Please call SDL_free() when you are done with this
  462. * pointer!
  463. * \param spec The SDL_AudioSpec to be initialized by this function.
  464. * \param iscapture non-zero to query the default recording device, zero to
  465. * query the default output device.
  466. * \returns 0 on success, nonzero on error
  467. *
  468. * \since This function is available since SDL 3.0.0.
  469. *
  470. * \sa SDL_GetAudioDeviceName
  471. * \sa SDL_GetAudioDeviceSpec
  472. * \sa SDL_OpenAudioDevice
  473. */
  474. extern DECLSPEC int SDLCALL SDL_GetDefaultAudioInfo(char **name,
  475. SDL_AudioSpec *spec,
  476. int iscapture);
  477. /**
  478. * Open a specific audio device.
  479. *
  480. * SDL_OpenAudio(), unlike this function, always acts on device ID 1. As such,
  481. * this function will never return a 1 so as not to conflict with the legacy
  482. * function.
  483. *
  484. * Please note that SDL 2.0 before 2.0.5 did not support recording; as such,
  485. * this function would fail if `iscapture` was not zero. Starting with SDL
  486. * 2.0.5, recording is implemented and this value can be non-zero.
  487. *
  488. * Passing in a `device` name of NULL requests the most reasonable default
  489. * (and is equivalent to what SDL_OpenAudio() does to choose a device). The
  490. * `device` name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but
  491. * some drivers allow arbitrary and driver-specific strings, such as a
  492. * hostname/IP address for a remote audio server, or a filename in the
  493. * diskaudio driver.
  494. *
  495. * An opened audio device starts out paused, and should be enabled for playing
  496. * by calling SDL_PauseAudioDevice(devid, 0) when you are ready for your audio
  497. * callback function to be called. Since the audio driver may modify the
  498. * requested size of the audio buffer, you should allocate any local mixing
  499. * buffers after you open the audio device.
  500. *
  501. * The audio callback runs in a separate thread in most cases; you can prevent
  502. * race conditions between your callback and other threads without fully
  503. * pausing playback with SDL_LockAudioDevice(). For more information about the
  504. * callback, see SDL_AudioSpec.
  505. *
  506. * Managing the audio spec via 'desired' and 'obtained':
  507. *
  508. * When filling in the desired audio spec structure:
  509. *
  510. * - `desired->freq` should be the frequency in sample-frames-per-second (Hz).
  511. * - `desired->format` should be the audio format (`AUDIO_S16SYS`, etc).
  512. * - `desired->samples` is the desired size of the audio buffer, in _sample
  513. * frames_ (with stereo output, two samples--left and right--would make a
  514. * single sample frame). This number should be a power of two, and may be
  515. * adjusted by the audio driver to a value more suitable for the hardware.
  516. * Good values seem to range between 512 and 8096 inclusive, depending on
  517. * the application and CPU speed. Smaller values reduce latency, but can
  518. * lead to underflow if the application is doing heavy processing and cannot
  519. * fill the audio buffer in time. Note that the number of sample frames is
  520. * directly related to time by the following formula: `ms =
  521. * (sampleframes*1000)/freq`
  522. * - `desired->size` is the size in _bytes_ of the audio buffer, and is
  523. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  524. * - `desired->silence` is the value used to set the buffer to silence, and is
  525. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  526. * - `desired->callback` should be set to a function that will be called when
  527. * the audio device is ready for more data. It is passed a pointer to the
  528. * audio buffer, and the length in bytes of the audio buffer. This function
  529. * usually runs in a separate thread, and so you should protect data
  530. * structures that it accesses by calling SDL_LockAudioDevice() and
  531. * SDL_UnlockAudioDevice() in your code. Alternately, you may pass a NULL
  532. * pointer here, and call SDL_QueueAudio() with some frequency, to queue
  533. * more audio samples to be played (or for capture devices, call
  534. * SDL_DequeueAudio() with some frequency, to obtain audio samples).
  535. * - `desired->userdata` is passed as the first parameter to your callback
  536. * function. If you passed a NULL callback, this value is ignored.
  537. *
  538. * `allowed_changes` can have the following flags OR'd together:
  539. *
  540. * - `SDL_AUDIO_ALLOW_FREQUENCY_CHANGE`
  541. * - `SDL_AUDIO_ALLOW_FORMAT_CHANGE`
  542. * - `SDL_AUDIO_ALLOW_CHANNELS_CHANGE`
  543. * - `SDL_AUDIO_ALLOW_SAMPLES_CHANGE`
  544. * - `SDL_AUDIO_ALLOW_ANY_CHANGE`
  545. *
  546. * These flags specify how SDL should behave when a device cannot offer a
  547. * specific feature. If the application requests a feature that the hardware
  548. * doesn't offer, SDL will always try to get the closest equivalent.
  549. *
  550. * For example, if you ask for float32 audio format, but the sound card only
  551. * supports int16, SDL will set the hardware to int16. If you had set
  552. * SDL_AUDIO_ALLOW_FORMAT_CHANGE, SDL will change the format in the `obtained`
  553. * structure. If that flag was *not* set, SDL will prepare to convert your
  554. * callback's float32 audio to int16 before feeding it to the hardware and
  555. * will keep the originally requested format in the `obtained` structure.
  556. *
  557. * The resulting audio specs, varying depending on hardware and on what
  558. * changes were allowed, will then be written back to `obtained`.
  559. *
  560. * If your application can only handle one specific data format, pass a zero
  561. * for `allowed_changes` and let SDL transparently handle any differences.
  562. *
  563. * \param device a UTF-8 string reported by SDL_GetAudioDeviceName() or a
  564. * driver-specific name as appropriate. NULL requests the most
  565. * reasonable default device.
  566. * \param iscapture non-zero to specify a device should be opened for
  567. * recording, not playback
  568. * \param desired an SDL_AudioSpec structure representing the desired output
  569. * format; see SDL_OpenAudio() for more information
  570. * \param obtained an SDL_AudioSpec structure filled in with the actual output
  571. * format; see SDL_OpenAudio() for more information
  572. * \param allowed_changes 0, or one or more flags OR'd together
  573. * \returns a valid device ID that is > 0 on success or 0 on failure; call
  574. * SDL_GetError() for more information.
  575. *
  576. * For compatibility with SDL 1.2, this will never return 1, since
  577. * SDL reserves that ID for the legacy SDL_OpenAudio() function.
  578. *
  579. * \since This function is available since SDL 3.0.0.
  580. *
  581. * \sa SDL_CloseAudioDevice
  582. * \sa SDL_GetAudioDeviceName
  583. * \sa SDL_LockAudioDevice
  584. * \sa SDL_OpenAudio
  585. * \sa SDL_PauseAudioDevice
  586. * \sa SDL_UnlockAudioDevice
  587. */
  588. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(
  589. const char *device,
  590. int iscapture,
  591. const SDL_AudioSpec *desired,
  592. SDL_AudioSpec *obtained,
  593. int allowed_changes);
  594. /**
  595. * \name Audio state
  596. *
  597. * Get the current audio state.
  598. */
  599. /* @{ */
  600. typedef enum
  601. {
  602. SDL_AUDIO_STOPPED = 0,
  603. SDL_AUDIO_PLAYING,
  604. SDL_AUDIO_PAUSED
  605. } SDL_AudioStatus;
  606. /**
  607. * This function is a legacy means of querying the audio device.
  608. *
  609. * New programs might want to use SDL_GetAudioDeviceStatus() instead. This
  610. * function is equivalent to calling...
  611. *
  612. * ```c
  613. * SDL_GetAudioDeviceStatus(1);
  614. * ```
  615. *
  616. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  617. *
  618. * \returns the SDL_AudioStatus of the audio device opened by SDL_OpenAudio().
  619. *
  620. * \since This function is available since SDL 3.0.0.
  621. *
  622. * \sa SDL_GetAudioDeviceStatus
  623. */
  624. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioStatus(void);
  625. /**
  626. * Use this function to get the current audio state of an audio device.
  627. *
  628. * \param dev the ID of an audio device previously opened with
  629. * SDL_OpenAudioDevice()
  630. * \returns the SDL_AudioStatus of the specified audio device.
  631. *
  632. * \since This function is available since SDL 3.0.0.
  633. *
  634. * \sa SDL_PauseAudioDevice
  635. */
  636. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev);
  637. /* @} *//* Audio State */
  638. /**
  639. * \name Pause audio functions
  640. *
  641. * These functions pause and unpause the audio callback processing.
  642. * They should be called with a parameter of 0 after opening the audio
  643. * device to start playing sound. This is so you can safely initialize
  644. * data for your callback function after opening the audio device.
  645. * Silence will be written to the audio device during the pause.
  646. */
  647. /* @{ */
  648. /**
  649. * This function is a legacy means of pausing the audio device.
  650. *
  651. * New programs might want to use SDL_PauseAudioDevice() instead. This
  652. * function is equivalent to calling...
  653. *
  654. * ```c
  655. * SDL_PauseAudioDevice(1, pause_on);
  656. * ```
  657. *
  658. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  659. *
  660. * \param pause_on non-zero to pause, 0 to unpause
  661. *
  662. * \since This function is available since SDL 3.0.0.
  663. *
  664. * \sa SDL_GetAudioStatus
  665. * \sa SDL_PauseAudioDevice
  666. */
  667. extern DECLSPEC void SDLCALL SDL_PauseAudio(int pause_on);
  668. /**
  669. * Use this function to pause and unpause audio playback on a specified
  670. * device.
  671. *
  672. * This function pauses and unpauses the audio callback processing for a given
  673. * device. Newly-opened audio devices start in the paused state, so you must
  674. * call this function with **pause_on**=0 after opening the specified audio
  675. * device to start playing sound. This allows you to safely initialize data
  676. * for your callback function after opening the audio device. Silence will be
  677. * written to the audio device while paused, and the audio callback is
  678. * guaranteed to not be called. Pausing one device does not prevent other
  679. * unpaused devices from running their callbacks.
  680. *
  681. * Pausing state does not stack; even if you pause a device several times, a
  682. * single unpause will start the device playing again, and vice versa. This is
  683. * different from how SDL_LockAudioDevice() works.
  684. *
  685. * If you just need to protect a few variables from race conditions vs your
  686. * callback, you shouldn't pause the audio device, as it will lead to dropouts
  687. * in the audio playback. Instead, you should use SDL_LockAudioDevice().
  688. *
  689. * \param dev a device opened by SDL_OpenAudioDevice()
  690. * \param pause_on non-zero to pause, 0 to unpause
  691. *
  692. * \since This function is available since SDL 3.0.0.
  693. *
  694. * \sa SDL_LockAudioDevice
  695. */
  696. extern DECLSPEC void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev,
  697. int pause_on);
  698. /* @} *//* Pause audio functions */
  699. /**
  700. * Load the audio data of a WAVE file into memory.
  701. *
  702. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  703. * be valid pointers. The entire data portion of the file is then loaded into
  704. * memory and decoded if necessary.
  705. *
  706. * If `freesrc` is non-zero, the data source gets automatically closed and
  707. * freed before the function returns.
  708. *
  709. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  710. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  711. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  712. * cause an error.
  713. *
  714. * If this function succeeds, the pointer returned by it is equal to `spec`
  715. * and the pointer to the audio data allocated by the function is written to
  716. * `audio_buf` and its length in bytes to `audio_len`. The SDL_AudioSpec
  717. * members `freq`, `channels`, and `format` are set to the values of the audio
  718. * data in the buffer. The `samples` member is set to a sane default and all
  719. * others are set to zero.
  720. *
  721. * It's necessary to use SDL_free() to free the audio data returned in
  722. * `audio_buf` when it is no longer used.
  723. *
  724. * Because of the underspecification of the .WAV format, there are many
  725. * problematic files in the wild that cause issues with strict decoders. To
  726. * provide compatibility with these files, this decoder is lenient in regards
  727. * to the truncation of the file, the fact chunk, and the size of the RIFF
  728. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  729. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  730. * tune the behavior of the loading process.
  731. *
  732. * Any file that is invalid (due to truncation, corruption, or wrong values in
  733. * the headers), too big, or unsupported causes an error. Additionally, any
  734. * critical I/O error from the data source will terminate the loading process
  735. * with an error. The function returns NULL on error and in all cases (with
  736. * the exception of `src` being NULL), an appropriate error message will be
  737. * set.
  738. *
  739. * It is required that the data source supports seeking.
  740. *
  741. * Example:
  742. *
  743. * ```c
  744. * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
  745. * ```
  746. *
  747. * Note that the SDL_LoadWAV macro does this same thing for you, but in a less
  748. * messy way:
  749. *
  750. * ```c
  751. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  752. * ```
  753. *
  754. * \param src The data source for the WAVE data
  755. * \param freesrc If non-zero, SDL will _always_ free the data source
  756. * \param spec An SDL_AudioSpec that will be filled in with the wave file's
  757. * format details
  758. * \param audio_buf A pointer filled with the audio data, allocated by the
  759. * function.
  760. * \param audio_len A pointer filled with the length of the audio data buffer
  761. * in bytes
  762. * \returns This function, if successfully called, returns `spec`, which will
  763. * be filled with the audio data format of the wave source data.
  764. * `audio_buf` will be filled with a pointer to an allocated buffer
  765. * containing the audio data, and `audio_len` is filled with the
  766. * length of that audio buffer in bytes.
  767. *
  768. * This function returns NULL if the .WAV file cannot be opened, uses
  769. * an unknown data format, or is corrupt; call SDL_GetError() for
  770. * more information.
  771. *
  772. * When the application is done with the data returned in
  773. * `audio_buf`, it should call SDL_free() to dispose of it.
  774. *
  775. * \since This function is available since SDL 3.0.0.
  776. *
  777. * \sa SDL_free
  778. * \sa SDL_LoadWAV
  779. */
  780. extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
  781. int freesrc,
  782. SDL_AudioSpec * spec,
  783. Uint8 ** audio_buf,
  784. Uint32 * audio_len);
  785. /**
  786. * Loads a WAV from a file.
  787. * Compatibility convenience function.
  788. */
  789. #define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
  790. SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
  791. /**
  792. * Initialize an SDL_AudioCVT structure for conversion.
  793. *
  794. * Before an SDL_AudioCVT structure can be used to convert audio data it must
  795. * be initialized with source and destination information.
  796. *
  797. * This function will zero out every field of the SDL_AudioCVT, so it must be
  798. * called before the application fills in the final buffer information.
  799. *
  800. * Once this function has returned successfully, and reported that a
  801. * conversion is necessary, the application fills in the rest of the fields in
  802. * SDL_AudioCVT, now that it knows how large a buffer it needs to allocate,
  803. * and then can call SDL_ConvertAudio() to complete the conversion.
  804. *
  805. * \param cvt an SDL_AudioCVT structure filled in with audio conversion
  806. * information
  807. * \param src_format the source format of the audio data; for more info see
  808. * SDL_AudioFormat
  809. * \param src_channels the number of channels in the source
  810. * \param src_rate the frequency (sample-frames-per-second) of the source
  811. * \param dst_format the destination format of the audio data; for more info
  812. * see SDL_AudioFormat
  813. * \param dst_channels the number of channels in the destination
  814. * \param dst_rate the frequency (sample-frames-per-second) of the destination
  815. * \returns 1 if the audio filter is prepared, 0 if no conversion is needed,
  816. * or a negative error code on failure; call SDL_GetError() for more
  817. * information.
  818. *
  819. * \since This function is available since SDL 3.0.0.
  820. *
  821. * \sa SDL_ConvertAudio
  822. */
  823. extern DECLSPEC int SDLCALL SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
  824. SDL_AudioFormat src_format,
  825. Uint8 src_channels,
  826. int src_rate,
  827. SDL_AudioFormat dst_format,
  828. Uint8 dst_channels,
  829. int dst_rate);
  830. /**
  831. * Convert audio data to a desired audio format.
  832. *
  833. * This function does the actual audio data conversion, after the application
  834. * has called SDL_BuildAudioCVT() to prepare the conversion information and
  835. * then filled in the buffer details.
  836. *
  837. * Once the application has initialized the `cvt` structure using
  838. * SDL_BuildAudioCVT(), allocated an audio buffer and filled it with audio
  839. * data in the source format, this function will convert the buffer, in-place,
  840. * to the desired format.
  841. *
  842. * The data conversion may go through several passes; any given pass may
  843. * possibly temporarily increase the size of the data. For example, SDL might
  844. * expand 16-bit data to 32 bits before resampling to a lower frequency,
  845. * shrinking the data size after having grown it briefly. Since the supplied
  846. * buffer will be both the source and destination, converting as necessary
  847. * in-place, the application must allocate a buffer that will fully contain
  848. * the data during its largest conversion pass. After SDL_BuildAudioCVT()
  849. * returns, the application should set the `cvt->len` field to the size, in
  850. * bytes, of the source data, and allocate a buffer that is `cvt->len *
  851. * cvt->len_mult` bytes long for the `buf` field.
  852. *
  853. * The source data should be copied into this buffer before the call to
  854. * SDL_ConvertAudio(). Upon successful return, this buffer will contain the
  855. * converted audio, and `cvt->len_cvt` will be the size of the converted data,
  856. * in bytes. Any bytes in the buffer past `cvt->len_cvt` are undefined once
  857. * this function returns.
  858. *
  859. * \param cvt an SDL_AudioCVT structure that was previously set up by
  860. * SDL_BuildAudioCVT().
  861. * \returns 0 if the conversion was completed successfully or a negative error
  862. * code on failure; call SDL_GetError() for more information.
  863. *
  864. * \since This function is available since SDL 3.0.0.
  865. *
  866. * \sa SDL_BuildAudioCVT
  867. */
  868. extern DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT * cvt);
  869. /* SDL_AudioStream is a new audio conversion interface.
  870. The benefits vs SDL_AudioCVT:
  871. - it can handle resampling data in chunks without generating
  872. artifacts, when it doesn't have the complete buffer available.
  873. - it can handle incoming data in any variable size.
  874. - You push data as you have it, and pull it when you need it
  875. */
  876. /* this is opaque to the outside world. */
  877. struct _SDL_AudioStream;
  878. typedef struct _SDL_AudioStream SDL_AudioStream;
  879. /**
  880. * Create a new audio stream.
  881. *
  882. * \param src_format The format of the source audio
  883. * \param src_channels The number of channels of the source audio
  884. * \param src_rate The sampling rate of the source audio
  885. * \param dst_format The format of the desired audio output
  886. * \param dst_channels The number of channels of the desired audio output
  887. * \param dst_rate The sampling rate of the desired audio output
  888. * \returns 0 on success, or -1 on error.
  889. *
  890. * \since This function is available since SDL 3.0.0.
  891. *
  892. * \sa SDL_PutAudioStreamData
  893. * \sa SDL_GetAudioStreamData
  894. * \sa SDL_GetAudioStreamAvailable
  895. * \sa SDL_FlushAudioStream
  896. * \sa SDL_ClearAudioStream
  897. * \sa SDL_DestroyAudioStream
  898. */
  899. extern DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAudioStream(SDL_AudioFormat src_format,
  900. Uint8 src_channels,
  901. int src_rate,
  902. SDL_AudioFormat dst_format,
  903. Uint8 dst_channels,
  904. int dst_rate);
  905. /**
  906. * Add data to be converted/resampled to the stream.
  907. *
  908. * \param stream The stream the audio data is being added to
  909. * \param buf A pointer to the audio data to add
  910. * \param len The number of bytes to write to the stream
  911. * \returns 0 on success, or -1 on error.
  912. *
  913. * \since This function is available since SDL 3.0.0.
  914. *
  915. * \sa SDL_CreateAudioStream
  916. * \sa SDL_GetAudioStreamData
  917. * \sa SDL_GetAudioStreamAvailable
  918. * \sa SDL_FlushAudioStream
  919. * \sa SDL_ClearAudioStream
  920. * \sa SDL_DestroyAudioStream
  921. */
  922. extern DECLSPEC int SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
  923. /**
  924. * Get converted/resampled data from the stream
  925. *
  926. * \param stream The stream the audio is being requested from
  927. * \param buf A buffer to fill with audio data
  928. * \param len The maximum number of bytes to fill
  929. * \returns the number of bytes read from the stream, or -1 on error
  930. *
  931. * \since This function is available since SDL 3.0.0.
  932. *
  933. * \sa SDL_CreateAudioStream
  934. * \sa SDL_PutAudioStreamData
  935. * \sa SDL_GetAudioStreamAvailable
  936. * \sa SDL_FlushAudioStream
  937. * \sa SDL_ClearAudioStream
  938. * \sa SDL_DestroyAudioStream
  939. */
  940. extern DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
  941. /**
  942. * Get the number of converted/resampled bytes available.
  943. *
  944. * The stream may be buffering data behind the scenes until it has enough to
  945. * resample correctly, so this number might be lower than what you expect, or
  946. * even be zero. Add more data or flush the stream if you need the data now.
  947. *
  948. * \since This function is available since SDL 3.0.0.
  949. *
  950. * \sa SDL_CreateAudioStream
  951. * \sa SDL_PutAudioStreamData
  952. * \sa SDL_GetAudioStreamData
  953. * \sa SDL_FlushAudioStream
  954. * \sa SDL_ClearAudioStream
  955. * \sa SDL_DestroyAudioStream
  956. */
  957. extern DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
  958. /**
  959. * Tell the stream that you're done sending data, and anything being buffered
  960. * should be converted/resampled and made available immediately.
  961. *
  962. * It is legal to add more data to a stream after flushing, but there will be
  963. * audio gaps in the output. Generally this is intended to signal the end of
  964. * input, so the complete output becomes available.
  965. *
  966. * \since This function is available since SDL 3.0.0.
  967. *
  968. * \sa SDL_CreateAudioStream
  969. * \sa SDL_PutAudioStreamData
  970. * \sa SDL_GetAudioStreamData
  971. * \sa SDL_GetAudioStreamAvailable
  972. * \sa SDL_ClearAudioStream
  973. * \sa SDL_DestroyAudioStream
  974. */
  975. extern DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
  976. /**
  977. * Clear any pending data in the stream without converting it
  978. *
  979. * \since This function is available since SDL 3.0.0.
  980. *
  981. * \sa SDL_CreateAudioStream
  982. * \sa SDL_PutAudioStreamData
  983. * \sa SDL_GetAudioStreamData
  984. * \sa SDL_GetAudioStreamAvailable
  985. * \sa SDL_FlushAudioStream
  986. * \sa SDL_DestroyAudioStream
  987. */
  988. extern DECLSPEC void SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
  989. /**
  990. * Free an audio stream
  991. *
  992. * \since This function is available since SDL 3.0.0.
  993. *
  994. * \sa SDL_CreateAudioStream
  995. * \sa SDL_PutAudioStreamData
  996. * \sa SDL_GetAudioStreamData
  997. * \sa SDL_GetAudioStreamAvailable
  998. * \sa SDL_FlushAudioStream
  999. * \sa SDL_ClearAudioStream
  1000. */
  1001. extern DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
  1002. #define SDL_MIX_MAXVOLUME 128
  1003. /**
  1004. * This function is a legacy means of mixing audio.
  1005. *
  1006. * This function is equivalent to calling...
  1007. *
  1008. * ```c
  1009. * SDL_MixAudioFormat(dst, src, format, len, volume);
  1010. * ```
  1011. *
  1012. * ...where `format` is the obtained format of the audio device from the
  1013. * legacy SDL_OpenAudio() function.
  1014. *
  1015. * \param dst the destination for the mixed audio
  1016. * \param src the source audio buffer to be mixed
  1017. * \param len the length of the audio buffer in bytes
  1018. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  1019. * for full audio volume
  1020. *
  1021. * \since This function is available since SDL 3.0.0.
  1022. *
  1023. * \sa SDL_MixAudioFormat
  1024. */
  1025. extern DECLSPEC void SDLCALL SDL_MixAudio(Uint8 * dst, const Uint8 * src,
  1026. Uint32 len, int volume);
  1027. /**
  1028. * Mix audio data in a specified format.
  1029. *
  1030. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  1031. * it into `dst`, performing addition, volume adjustment, and overflow
  1032. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  1033. * `format` data.
  1034. *
  1035. * This is provided for convenience -- you can mix your own audio data.
  1036. *
  1037. * Do not use this function for mixing together more than two streams of
  1038. * sample data. The output from repeated application of this function may be
  1039. * distorted by clipping, because there is no accumulator with greater range
  1040. * than the input (not to mention this being an inefficient way of doing it).
  1041. *
  1042. * It is a common misconception that this function is required to write audio
  1043. * data to an output stream in an audio callback. While you can do that,
  1044. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  1045. * audio stream with a volume adjustment.
  1046. *
  1047. * \param dst the destination for the mixed audio
  1048. * \param src the source audio buffer to be mixed
  1049. * \param format the SDL_AudioFormat structure representing the desired audio
  1050. * format
  1051. * \param len the length of the audio buffer in bytes
  1052. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  1053. * for full audio volume
  1054. *
  1055. * \since This function is available since SDL 3.0.0.
  1056. */
  1057. extern DECLSPEC void SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  1058. const Uint8 * src,
  1059. SDL_AudioFormat format,
  1060. Uint32 len, int volume);
  1061. /**
  1062. * Queue more audio on non-callback devices.
  1063. *
  1064. * If you are looking to retrieve queued audio from a non-callback capture
  1065. * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
  1066. * -1 to signify an error if you use it with capture devices.
  1067. *
  1068. * SDL offers two ways to feed audio to the device: you can either supply a
  1069. * callback that SDL triggers with some frequency to obtain more audio (pull
  1070. * method), or you can supply no callback, and then SDL will expect you to
  1071. * supply data at regular intervals (push method) with this function.
  1072. *
  1073. * There are no limits on the amount of data you can queue, short of
  1074. * exhaustion of address space. Queued data will drain to the device as
  1075. * necessary without further intervention from you. If the device needs audio
  1076. * but there is not enough queued, it will play silence to make up the
  1077. * difference. This means you will have skips in your audio playback if you
  1078. * aren't routinely queueing sufficient data.
  1079. *
  1080. * This function copies the supplied data, so you are safe to free it when the
  1081. * function returns. This function is thread-safe, but queueing to the same
  1082. * device from two threads at once does not promise which buffer will be
  1083. * queued first.
  1084. *
  1085. * You may not queue audio on a device that is using an application-supplied
  1086. * callback; doing so returns an error. You have to use the audio callback or
  1087. * queue audio with this function, but not both.
  1088. *
  1089. * You should not call SDL_LockAudio() on the device before queueing; SDL
  1090. * handles locking internally for this function.
  1091. *
  1092. * Note that SDL does not support planar audio. You will need to resample from
  1093. * planar audio formats into a non-planar one (see SDL_AudioFormat) before
  1094. * queuing audio.
  1095. *
  1096. * \param dev the device ID to which we will queue audio
  1097. * \param data the data to queue to the device for later playback
  1098. * \param len the number of bytes (not samples!) to which `data` points
  1099. * \returns 0 on success or a negative error code on failure; call
  1100. * SDL_GetError() for more information.
  1101. *
  1102. * \since This function is available since SDL 3.0.0.
  1103. *
  1104. * \sa SDL_ClearQueuedAudio
  1105. * \sa SDL_GetQueuedAudioSize
  1106. */
  1107. extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
  1108. /**
  1109. * Dequeue more audio on non-callback devices.
  1110. *
  1111. * If you are looking to queue audio for output on a non-callback playback
  1112. * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
  1113. * return 0 if you use it with playback devices.
  1114. *
  1115. * SDL offers two ways to retrieve audio from a capture device: you can either
  1116. * supply a callback that SDL triggers with some frequency as the device
  1117. * records more audio data, (push method), or you can supply no callback, and
  1118. * then SDL will expect you to retrieve data at regular intervals (pull
  1119. * method) with this function.
  1120. *
  1121. * There are no limits on the amount of data you can queue, short of
  1122. * exhaustion of address space. Data from the device will keep queuing as
  1123. * necessary without further intervention from you. This means you will
  1124. * eventually run out of memory if you aren't routinely dequeueing data.
  1125. *
  1126. * Capture devices will not queue data when paused; if you are expecting to
  1127. * not need captured audio for some length of time, use SDL_PauseAudioDevice()
  1128. * to stop the capture device from queueing more data. This can be useful
  1129. * during, say, level loading times. When unpaused, capture devices will start
  1130. * queueing data from that point, having flushed any capturable data available
  1131. * while paused.
  1132. *
  1133. * This function is thread-safe, but dequeueing from the same device from two
  1134. * threads at once does not promise which thread will dequeue data first.
  1135. *
  1136. * You may not dequeue audio from a device that is using an
  1137. * application-supplied callback; doing so returns an error. You have to use
  1138. * the audio callback, or dequeue audio with this function, but not both.
  1139. *
  1140. * You should not call SDL_LockAudio() on the device before dequeueing; SDL
  1141. * handles locking internally for this function.
  1142. *
  1143. * \param dev the device ID from which we will dequeue audio
  1144. * \param data a pointer into where audio data should be copied
  1145. * \param len the number of bytes (not samples!) to which (data) points
  1146. * \returns the number of bytes dequeued, which could be less than requested;
  1147. * call SDL_GetError() for more information.
  1148. *
  1149. * \since This function is available since SDL 3.0.0.
  1150. *
  1151. * \sa SDL_ClearQueuedAudio
  1152. * \sa SDL_GetQueuedAudioSize
  1153. */
  1154. extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
  1155. /**
  1156. * Get the number of bytes of still-queued audio.
  1157. *
  1158. * For playback devices: this is the number of bytes that have been queued for
  1159. * playback with SDL_QueueAudio(), but have not yet been sent to the hardware.
  1160. *
  1161. * Once we've sent it to the hardware, this function can not decide the exact
  1162. * byte boundary of what has been played. It's possible that we just gave the
  1163. * hardware several kilobytes right before you called this function, but it
  1164. * hasn't played any of it yet, or maybe half of it, etc.
  1165. *
  1166. * For capture devices, this is the number of bytes that have been captured by
  1167. * the device and are waiting for you to dequeue. This number may grow at any
  1168. * time, so this only informs of the lower-bound of available data.
  1169. *
  1170. * You may not queue or dequeue audio on a device that is using an
  1171. * application-supplied callback; calling this function on such a device
  1172. * always returns 0. You have to use the audio callback or queue audio, but
  1173. * not both.
  1174. *
  1175. * You should not call SDL_LockAudio() on the device before querying; SDL
  1176. * handles locking internally for this function.
  1177. *
  1178. * \param dev the device ID of which we will query queued audio size
  1179. * \returns the number of bytes (not samples!) of queued audio.
  1180. *
  1181. * \since This function is available since SDL 3.0.0.
  1182. *
  1183. * \sa SDL_ClearQueuedAudio
  1184. * \sa SDL_QueueAudio
  1185. * \sa SDL_DequeueAudio
  1186. */
  1187. extern DECLSPEC Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev);
  1188. /**
  1189. * Drop any queued audio data waiting to be sent to the hardware.
  1190. *
  1191. * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
  1192. * output devices, the hardware will start playing silence if more audio isn't
  1193. * queued. For capture devices, the hardware will start filling the empty
  1194. * queue with new data if the capture device isn't paused.
  1195. *
  1196. * This will not prevent playback of queued audio that's already been sent to
  1197. * the hardware, as we can not undo that, so expect there to be some fraction
  1198. * of a second of audio that might still be heard. This can be useful if you
  1199. * want to, say, drop any pending music or any unprocessed microphone input
  1200. * during a level change in your game.
  1201. *
  1202. * You may not queue or dequeue audio on a device that is using an
  1203. * application-supplied callback; calling this function on such a device
  1204. * always returns 0. You have to use the audio callback or queue audio, but
  1205. * not both.
  1206. *
  1207. * You should not call SDL_LockAudio() on the device before clearing the
  1208. * queue; SDL handles locking internally for this function.
  1209. *
  1210. * This function always succeeds and thus returns void.
  1211. *
  1212. * \param dev the device ID of which to clear the audio queue
  1213. *
  1214. * \since This function is available since SDL 3.0.0.
  1215. *
  1216. * \sa SDL_GetQueuedAudioSize
  1217. * \sa SDL_QueueAudio
  1218. * \sa SDL_DequeueAudio
  1219. */
  1220. extern DECLSPEC void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
  1221. /**
  1222. * \name Audio lock functions
  1223. *
  1224. * The lock manipulated by these functions protects the callback function.
  1225. * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
  1226. * the callback function is not running. Do not call these from the callback
  1227. * function or you will cause deadlock.
  1228. */
  1229. /* @{ */
  1230. /**
  1231. * This function is a legacy means of locking the audio device.
  1232. *
  1233. * New programs might want to use SDL_LockAudioDevice() instead. This function
  1234. * is equivalent to calling...
  1235. *
  1236. * ```c
  1237. * SDL_LockAudioDevice(1);
  1238. * ```
  1239. *
  1240. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  1241. *
  1242. * \since This function is available since SDL 3.0.0.
  1243. *
  1244. * \sa SDL_LockAudioDevice
  1245. * \sa SDL_UnlockAudio
  1246. * \sa SDL_UnlockAudioDevice
  1247. */
  1248. extern DECLSPEC void SDLCALL SDL_LockAudio(void);
  1249. /**
  1250. * Use this function to lock out the audio callback function for a specified
  1251. * device.
  1252. *
  1253. * The lock manipulated by these functions protects the audio callback
  1254. * function specified in SDL_OpenAudioDevice(). During a
  1255. * SDL_LockAudioDevice()/SDL_UnlockAudioDevice() pair, you can be guaranteed
  1256. * that the callback function for that device is not running, even if the
  1257. * device is not paused. While a device is locked, any other unpaused,
  1258. * unlocked devices may still run their callbacks.
  1259. *
  1260. * Calling this function from inside your audio callback is unnecessary. SDL
  1261. * obtains this lock before calling your function, and releases it when the
  1262. * function returns.
  1263. *
  1264. * You should not hold the lock longer than absolutely necessary. If you hold
  1265. * it too long, you'll experience dropouts in your audio playback. Ideally,
  1266. * your application locks the device, sets a few variables and unlocks again.
  1267. * Do not do heavy work while holding the lock for a device.
  1268. *
  1269. * It is safe to lock the audio device multiple times, as long as you unlock
  1270. * it an equivalent number of times. The callback will not run until the
  1271. * device has been unlocked completely in this way. If your application fails
  1272. * to unlock the device appropriately, your callback will never run, you might
  1273. * hear repeating bursts of audio, and SDL_CloseAudioDevice() will probably
  1274. * deadlock.
  1275. *
  1276. * Internally, the audio device lock is a mutex; if you lock from two threads
  1277. * at once, not only will you block the audio callback, you'll block the other
  1278. * thread.
  1279. *
  1280. * \param dev the ID of the device to be locked
  1281. *
  1282. * \since This function is available since SDL 3.0.0.
  1283. *
  1284. * \sa SDL_UnlockAudioDevice
  1285. */
  1286. extern DECLSPEC void SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
  1287. /**
  1288. * This function is a legacy means of unlocking the audio device.
  1289. *
  1290. * New programs might want to use SDL_UnlockAudioDevice() instead. This
  1291. * function is equivalent to calling...
  1292. *
  1293. * ```c
  1294. * SDL_UnlockAudioDevice(1);
  1295. * ```
  1296. *
  1297. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  1298. *
  1299. * \since This function is available since SDL 3.0.0.
  1300. *
  1301. * \sa SDL_LockAudio
  1302. * \sa SDL_UnlockAudioDevice
  1303. */
  1304. extern DECLSPEC void SDLCALL SDL_UnlockAudio(void);
  1305. /**
  1306. * Use this function to unlock the audio callback function for a specified
  1307. * device.
  1308. *
  1309. * This function should be paired with a previous SDL_LockAudioDevice() call.
  1310. *
  1311. * \param dev the ID of the device to be unlocked
  1312. *
  1313. * \since This function is available since SDL 3.0.0.
  1314. *
  1315. * \sa SDL_LockAudioDevice
  1316. */
  1317. extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
  1318. /* @} *//* Audio lock functions */
  1319. /**
  1320. * This function is a legacy means of closing the audio device.
  1321. *
  1322. * This function is equivalent to calling...
  1323. *
  1324. * ```c
  1325. * SDL_CloseAudioDevice(1);
  1326. * ```
  1327. *
  1328. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  1329. *
  1330. * \since This function is available since SDL 3.0.0.
  1331. *
  1332. * \sa SDL_OpenAudio
  1333. */
  1334. extern DECLSPEC void SDLCALL SDL_CloseAudio(void);
  1335. /**
  1336. * Use this function to shut down audio processing and close the audio device.
  1337. *
  1338. * The application should close open audio devices once they are no longer
  1339. * needed. Calling this function will wait until the device's audio callback
  1340. * is not running, release the audio hardware and then clean up internal
  1341. * state. No further audio will play from this device once this function
  1342. * returns.
  1343. *
  1344. * This function may block briefly while pending audio data is played by the
  1345. * hardware, so that applications don't drop the last buffer of data they
  1346. * supplied.
  1347. *
  1348. * The device ID is invalid as soon as the device is closed, and is eligible
  1349. * for reuse in a new SDL_OpenAudioDevice() call immediately.
  1350. *
  1351. * \param dev an audio device previously opened with SDL_OpenAudioDevice()
  1352. *
  1353. * \since This function is available since SDL 3.0.0.
  1354. *
  1355. * \sa SDL_OpenAudioDevice
  1356. */
  1357. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
  1358. /* Ends C function definitions when using C++ */
  1359. #ifdef __cplusplus
  1360. }
  1361. #endif
  1362. #include <SDL3/SDL_close_code.h>
  1363. #endif /* SDL_audio_h_ */