SDL_audio.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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. /**
  19. * \file SDL_audio.h
  20. *
  21. * \brief Audio functionality for the SDL library.
  22. */
  23. #ifndef SDL_audio_h_
  24. #define SDL_audio_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. #include <SDL3/SDL_endian.h>
  28. #include <SDL3/SDL_mutex.h>
  29. #include <SDL3/SDL_thread.h>
  30. #include <SDL3/SDL_rwops.h>
  31. #include <SDL3/SDL_begin_code.h>
  32. /* Set up for C function definitions, even when using C++ */
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /*
  37. * For multi-channel audio, the default SDL channel mapping is:
  38. * 2: FL FR (stereo)
  39. * 3: FL FR LFE (2.1 surround)
  40. * 4: FL FR BL BR (quad)
  41. * 5: FL FR LFE BL BR (4.1 surround)
  42. * 6: FL FR FC LFE SL SR (5.1 surround - last two can also be BL BR)
  43. * 7: FL FR FC LFE BC SL SR (6.1 surround)
  44. * 8: FL FR FC LFE BL BR SL SR (7.1 surround)
  45. */
  46. /**
  47. * \brief Audio format flags.
  48. *
  49. * These are what the 16 bits in SDL_AudioFormat currently mean...
  50. * (Unspecified bits are always zero).
  51. *
  52. * \verbatim
  53. ++-----------------------sample is signed if set
  54. ||
  55. || ++-----------sample is bigendian if set
  56. || ||
  57. || || ++---sample is float if set
  58. || || ||
  59. || || || +---sample bit size---+
  60. || || || | |
  61. 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
  62. \endverbatim
  63. *
  64. * There are macros in SDL 2.0 and later to query these bits.
  65. */
  66. typedef Uint16 SDL_AudioFormat;
  67. /**
  68. * \name Audio flags
  69. */
  70. /* @{ */
  71. #define SDL_AUDIO_MASK_BITSIZE (0xFF)
  72. #define SDL_AUDIO_MASK_DATATYPE (1<<8)
  73. #define SDL_AUDIO_MASK_ENDIAN (1<<12)
  74. #define SDL_AUDIO_MASK_SIGNED (1<<15)
  75. #define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
  76. #define SDL_AUDIO_ISFLOAT(x) (x & SDL_AUDIO_MASK_DATATYPE)
  77. #define SDL_AUDIO_ISBIGENDIAN(x) (x & SDL_AUDIO_MASK_ENDIAN)
  78. #define SDL_AUDIO_ISSIGNED(x) (x & SDL_AUDIO_MASK_SIGNED)
  79. #define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))
  80. #define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))
  81. #define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))
  82. /**
  83. * \name Audio format flags
  84. *
  85. * Defaults to LSB byte order.
  86. */
  87. /* @{ */
  88. #define SDL_AUDIO_U8 0x0008 /**< Unsigned 8-bit samples */
  89. #define SDL_AUDIO_S8 0x8008 /**< Signed 8-bit samples */
  90. #define SDL_AUDIO_S16LSB 0x8010 /**< Signed 16-bit samples */
  91. #define SDL_AUDIO_S16MSB 0x9010 /**< As above, but big-endian byte order */
  92. #define SDL_AUDIO_S16 SDL_AUDIO_S16LSB
  93. /* @} */
  94. /**
  95. * \name int32 support
  96. */
  97. /* @{ */
  98. #define SDL_AUDIO_S32LSB 0x8020 /**< 32-bit integer samples */
  99. #define SDL_AUDIO_S32MSB 0x9020 /**< As above, but big-endian byte order */
  100. #define SDL_AUDIO_S32 SDL_AUDIO_S32LSB
  101. /* @} */
  102. /**
  103. * \name float32 support
  104. */
  105. /* @{ */
  106. #define SDL_AUDIO_F32LSB 0x8120 /**< 32-bit floating point samples */
  107. #define SDL_AUDIO_F32MSB 0x9120 /**< As above, but big-endian byte order */
  108. #define SDL_AUDIO_F32 SDL_AUDIO_F32LSB
  109. /* @} */
  110. /**
  111. * \name Native audio byte ordering
  112. */
  113. /* @{ */
  114. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  115. #define SDL_AUDIO_S16SYS SDL_AUDIO_S16LSB
  116. #define SDL_AUDIO_S32SYS SDL_AUDIO_S32LSB
  117. #define SDL_AUDIO_F32SYS SDL_AUDIO_F32LSB
  118. #else
  119. #define SDL_AUDIO_S16SYS SDL_AUDIO_S16MSB
  120. #define SDL_AUDIO_S32SYS SDL_AUDIO_S32MSB
  121. #define SDL_AUDIO_F32SYS SDL_AUDIO_F32MSB
  122. #endif
  123. /* @} */
  124. /* @} *//* Audio flags */
  125. /**
  126. * SDL Audio Device instance IDs.
  127. */
  128. typedef Uint32 SDL_AudioDeviceID;
  129. #define SDL_AUDIO_DEVICE_DEFAULT_OUTPUT ((SDL_AudioDeviceID) 0xFFFFFFFF)
  130. #define SDL_AUDIO_DEVICE_DEFAULT_CAPTURE ((SDL_AudioDeviceID) 0xFFFFFFFE)
  131. typedef struct SDL_AudioSpec
  132. {
  133. SDL_AudioFormat format; /**< Audio data format */
  134. int channels; /**< Number of channels: 1 mono, 2 stereo, etc */
  135. int freq; /**< sample rate: sample frames per second */
  136. } SDL_AudioSpec;
  137. /* SDL_AudioStream is an audio conversion interface.
  138. - It can handle resampling data in chunks without generating
  139. artifacts, when it doesn't have the complete buffer available.
  140. - It can handle incoming data in any variable size.
  141. - It can handle input/output format changes on the fly.
  142. - You push data as you have it, and pull it when you need it
  143. - It can also function as a basic audio data queue even if you
  144. just have sound that needs to pass from one place to another.
  145. - You can hook callbacks up to them when more data is added or
  146. requested, to manage data on-the-fly.
  147. */
  148. struct SDL_AudioStream; /* this is opaque to the outside world. */
  149. typedef struct SDL_AudioStream SDL_AudioStream;
  150. /* Function prototypes */
  151. /**
  152. * \name Driver discovery functions
  153. *
  154. * These functions return the list of built in audio drivers, in the
  155. * order that they are normally initialized by default.
  156. */
  157. /* @{ */
  158. /**
  159. * Use this function to get the number of built-in audio drivers.
  160. *
  161. * This function returns a hardcoded number. This never returns a negative
  162. * value; if there are no drivers compiled into this build of SDL, this
  163. * function returns zero. The presence of a driver in this list does not mean
  164. * it will function, it just means SDL is capable of interacting with that
  165. * interface. For example, a build of SDL might have esound support, but if
  166. * there's no esound server available, SDL's esound driver would fail if used.
  167. *
  168. * By default, SDL tries all drivers, in its preferred order, until one is
  169. * found to be usable.
  170. *
  171. * \returns the number of built-in audio drivers.
  172. *
  173. * \threadsafety It is safe to call this function from any thread.
  174. *
  175. * \since This function is available since SDL 3.0.0.
  176. *
  177. * \sa SDL_GetAudioDriver
  178. */
  179. extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
  180. /**
  181. * Use this function to get the name of a built in audio driver.
  182. *
  183. * The list of audio drivers is given in the order that they are normally
  184. * initialized by default; the drivers that seem more reasonable to choose
  185. * first (as far as the SDL developers believe) are earlier in the list.
  186. *
  187. * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
  188. * "coreaudio" or "xaudio2". These never have Unicode characters, and are not
  189. * meant to be proper names.
  190. *
  191. * \param index the index of the audio driver; the value ranges from 0 to
  192. * SDL_GetNumAudioDrivers() - 1
  193. * \returns the name of the audio driver at the requested index, or NULL if an
  194. * invalid index was specified.
  195. *
  196. * \threadsafety It is safe to call this function from any thread.
  197. *
  198. * \since This function is available since SDL 3.0.0.
  199. *
  200. * \sa SDL_GetNumAudioDrivers
  201. */
  202. extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
  203. /* @} */
  204. /**
  205. * Get the name of the current audio driver.
  206. *
  207. * The returned string points to internal static memory and thus never becomes
  208. * invalid, even if you quit the audio subsystem and initialize a new driver
  209. * (although such a case would return a different static string from another
  210. * call to this function, of course). As such, you should not modify or free
  211. * the returned string.
  212. *
  213. * \returns the name of the current audio driver or NULL if no driver has been
  214. * initialized.
  215. *
  216. * \threadsafety It is safe to call this function from any thread.
  217. *
  218. * \since This function is available since SDL 3.0.0.
  219. */
  220. extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
  221. /**
  222. * Get a list of currently-connected audio output devices.
  223. *
  224. * This returns of list of available devices that play sound, perhaps
  225. * to speakers or headphones ("output" devices). If you want devices
  226. * that record audio, like a microphone ("capture" devices), use
  227. * SDL_GetAudioCaptureDevices() instead.
  228. *
  229. * This only returns a list of physical devices; it will not have any
  230. * device IDs returned by SDL_OpenAudioDevice().
  231. *
  232. * \param count a pointer filled in with the number of devices returned
  233. * \returns a 0 terminated array of device instance IDs which should be
  234. * freed with SDL_free(), or NULL on error; call SDL_GetError() for
  235. * more details.
  236. *
  237. * \threadsafety It is safe to call this function from any thread.
  238. *
  239. * \since This function is available since SDL 3.0.0.
  240. *
  241. * \sa SDL_OpenAudioDevice
  242. * \sa SDL_GetAudioCaptureDevices
  243. */
  244. extern DECLSPEC SDL_AudioDeviceID *SDLCALL SDL_GetAudioOutputDevices(int *count);
  245. /**
  246. * Get a list of currently-connected audio capture devices.
  247. *
  248. * This returns of list of available devices that record audio, like a
  249. * microphone ("capture" devices). If you want devices
  250. * that play sound, perhaps to speakers or headphones ("output" devices),
  251. * use SDL_GetAudioOutputDevices() instead.
  252. *
  253. * This only returns a list of physical devices; it will not have any
  254. * device IDs returned by SDL_OpenAudioDevice().
  255. *
  256. * \param count a pointer filled in with the number of devices returned
  257. * \returns a 0 terminated array of device instance IDs which should be
  258. * freed with SDL_free(), or NULL on error; call SDL_GetError() for
  259. * more details.
  260. *
  261. * \threadsafety It is safe to call this function from any thread.
  262. *
  263. * \since This function is available since SDL 3.0.0.
  264. *
  265. * \sa SDL_OpenAudioDevice
  266. * \sa SDL_GetAudioOutputDevices
  267. */
  268. extern DECLSPEC SDL_AudioDeviceID *SDLCALL SDL_GetAudioCaptureDevices(int *count);
  269. /**
  270. * Get the human-readable name of a specific audio device.
  271. *
  272. * The string returned by this function is UTF-8 encoded. The caller should
  273. * call SDL_free on the return value when done with it.
  274. *
  275. * \param devid the instance ID of the device to query.
  276. * \returns the name of the audio device, or NULL on error.
  277. *
  278. * \threadsafety It is safe to call this function from any thread.
  279. *
  280. * \since This function is available since SDL 3.0.0.
  281. *
  282. * \sa SDL_GetNumAudioDevices
  283. * \sa SDL_GetDefaultAudioInfo
  284. */
  285. extern DECLSPEC char *SDLCALL SDL_GetAudioDeviceName(SDL_AudioDeviceID devid);
  286. /**
  287. * Get the current audio format of a specific audio device.
  288. *
  289. * For an opened device, this will report the format the device is
  290. * currently using. If the device isn't yet opened, this will report
  291. * the device's preferred format (or a reasonable default if this
  292. * can't be determined).
  293. *
  294. * You may also specify SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
  295. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE here, which is useful for getting
  296. * a reasonable recommendation before opening the system-recommended
  297. * default device.
  298. *
  299. * \param devid the instance ID of the device to query.
  300. * \param spec On return, will be filled with device details.
  301. * \returns 0 on success or a negative error code on failure; call
  302. * SDL_GetError() for more information.
  303. *
  304. * \threadsafety It is safe to call this function from any thread.
  305. *
  306. * \since This function is available since SDL 3.0.0.
  307. */
  308. extern DECLSPEC int SDLCALL SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec);
  309. /**
  310. * Open a specific audio device.
  311. *
  312. * You can open both output and capture devices through this function.
  313. * Output devices will take data from bound audio streams, mix it, and
  314. * send it to the hardware. Capture devices will feed any bound audio
  315. * streams with a copy of any incoming data.
  316. *
  317. * An opened audio device starts out with no audio streams bound. To
  318. * start audio playing, bind a stream and supply audio data to it. Unlike
  319. * SDL2, there is no audio callback; you only bind audio streams and
  320. * make sure they have data flowing into them (although, as an optional
  321. * feature, each audio stream may have its own callback, which can be
  322. * used to simulate SDL2's semantics).
  323. *
  324. * If you don't care about opening a specific device, pass a `devid`
  325. * of either `SDL_AUDIO_DEVICE_DEFAULT_OUTPUT` or
  326. * `SDL_AUDIO_DEVICE_DEFAULT_CAPTURE`. In this case, SDL will try to
  327. * pick the most reasonable default, and may also switch between
  328. * physical devices seamlessly later, if the most reasonable default
  329. * changes during the lifetime of this opened device (user changed
  330. * the default in the OS's system preferences, the default got
  331. * unplugged so the system jumped to a new default, the user plugged
  332. * in headphones on a mobile device, etc). Unless you have a good
  333. * reason to choose a specific device, this is probably what you want.
  334. *
  335. * You may request a specific format for the audio device, but there is
  336. * no promise the device will honor that request for several reasons. As
  337. * such, it's only meant to be a hint as to what data your app will
  338. * provide. Audio streams will accept data in whatever format you specify and
  339. * manage conversion for you as appropriate. SDL_GetAudioDeviceFormat can
  340. * tell you the preferred format for the device before opening and the
  341. * actual format the device is using after opening.
  342. *
  343. * It's legal to open the same device ID more than once; each successful
  344. * open will generate a new logical SDL_AudioDeviceID that is managed
  345. * separately from others on the same physical device. This allows
  346. * libraries to open a device separately from the main app and bind its own
  347. * streams without conflicting.
  348. *
  349. * It is also legal to open a device ID returned by a previous call to
  350. * this function; doing so just creates another logical device on the same
  351. * physical device. This may be useful for making logical groupings of
  352. * audio streams.
  353. *
  354. * This function returns the opened device ID on success. This is a new,
  355. * unique SDL_AudioDeviceID that represents a logical device.
  356. *
  357. * Some backends might offer arbitrary devices (for example, a networked
  358. * audio protocol that can connect to an arbitrary server). For these, as
  359. * a change from SDL2, you should open a default device ID and use an SDL
  360. * hint to specify the target if you care, or otherwise let the backend
  361. * figure out a reasonable default. Most backends don't offer anything like
  362. * this, and often this would be an end user setting an environment
  363. * variable for their custom need, and not something an application should
  364. * specifically manage.
  365. *
  366. * When done with an audio device, possibly at the end of the app's life,
  367. * one should call SDL_CloseAudioDevice() on the returned device id.
  368. *
  369. * \param devid the device instance id to open, or SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
  370. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE for the most reasonable default device.
  371. * \param spec the requested device configuration. Can be NULL to use reasonable defaults.
  372. * \returns The device ID on success, 0 on error; call SDL_GetError() for more information.
  373. *
  374. * \threadsafety It is safe to call this function from any thread.
  375. *
  376. * \since This function is available since SDL 3.0.0.
  377. *
  378. * \sa SDL_CloseAudioDevice
  379. * \sa SDL_GetAudioDeviceFormat
  380. */
  381. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec);
  382. /**
  383. * Use this function to pause audio playback on a specified device.
  384. *
  385. * This function pauses audio processing for a given device. Any bound
  386. * audio streams will not progress, and no audio will be generated.
  387. * Pausing one device does not prevent other unpaused devices from running.
  388. *
  389. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  390. * has to bind a stream before any audio will flow. Pausing a paused
  391. * device is a legal no-op.
  392. *
  393. * Pausing a device can be useful to halt all audio without unbinding all
  394. * the audio streams. This might be useful while a game is paused, or
  395. * a level is loading, etc.
  396. *
  397. * Physical devices can not be paused or unpaused, only logical devices
  398. * created through SDL_OpenAudioDevice() can be.
  399. *
  400. * \param dev a device opened by SDL_OpenAudioDevice()
  401. * \returns 0 on success or a negative error code on failure; call
  402. * SDL_GetError() for more information.
  403. *
  404. * \threadsafety It is safe to call this function from any thread.
  405. *
  406. * \since This function is available since SDL 3.0.0.
  407. *
  408. * \sa SDL_UnpauseAudioDevice
  409. * \sa SDL_IsAudioDevicePaused
  410. */
  411. extern DECLSPEC int SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev);
  412. /**
  413. * Use this function to unpause audio playback on a specified device.
  414. *
  415. * This function unpauses audio processing for a given device that has
  416. * previously been paused with SDL_PauseAudioDevice(). Once unpaused, any
  417. * bound audio streams will begin to progress again, and audio can be
  418. * generated.
  419. *
  420. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  421. * has to bind a stream before any audio will flow. Unpausing an unpaused
  422. * device is a legal no-op.
  423. *
  424. * Physical devices can not be paused or unpaused, only logical devices
  425. * created through SDL_OpenAudioDevice() can be.
  426. *
  427. * \param dev a device opened by SDL_OpenAudioDevice()
  428. * \returns 0 on success or a negative error code on failure; call
  429. * SDL_GetError() for more information.
  430. *
  431. * \threadsafety It is safe to call this function from any thread.
  432. *
  433. * \since This function is available since SDL 3.0.0.
  434. *
  435. * \sa SDL_UnpauseAudioDevice
  436. * \sa SDL_IsAudioDevicePaused
  437. */
  438. extern DECLSPEC int SDLCALL SDL_UnpauseAudioDevice(SDL_AudioDeviceID dev);
  439. /**
  440. * Use this function to query if an audio device is paused.
  441. *
  442. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  443. * has to bind a stream before any audio will flow.
  444. *
  445. * Physical devices can not be paused or unpaused, only logical devices
  446. * created through SDL_OpenAudioDevice() can be. Physical and invalid
  447. * device IDs will report themselves as unpaused here.
  448. *
  449. * \param dev a device opened by SDL_OpenAudioDevice()
  450. * \returns SDL_TRUE if device is valid and paused, SDL_FALSE otherwise.
  451. *
  452. * \threadsafety It is safe to call this function from any thread.
  453. *
  454. * \since This function is available since SDL 3.0.0.
  455. *
  456. * \sa SDL_PauseAudioDevice
  457. * \sa SDL_UnpauseAudioDevice
  458. * \sa SDL_IsAudioDevicePaused
  459. */
  460. extern DECLSPEC SDL_bool SDLCALL SDL_IsAudioDevicePaused(SDL_AudioDeviceID dev);
  461. /**
  462. * Close a previously-opened audio device.
  463. *
  464. * The application should close open audio devices once they are no longer
  465. * needed.
  466. *
  467. * This function may block briefly while pending audio data is played by the
  468. * hardware, so that applications don't drop the last buffer of data they
  469. * supplied if terminating immediately afterwards.
  470. *
  471. * \param devid an audio device id previously returned by SDL_OpenAudioDevice()
  472. *
  473. * \threadsafety It is safe to call this function from any thread.
  474. *
  475. * \since This function is available since SDL 3.0.0.
  476. *
  477. * \sa SDL_OpenAudioDevice
  478. */
  479. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID devid);
  480. /**
  481. * Bind a list of audio streams to an audio device.
  482. *
  483. * Audio data will flow through any bound streams. For an output device, data
  484. * for all bound streams will be mixed together and fed to the device. For a
  485. * capture device, a copy of recorded data will be provided to each bound
  486. * stream.
  487. *
  488. * Audio streams can only be bound to an open device. This operation is
  489. * atomic--all streams bound in the same call will start processing at the same
  490. * time, so they can stay in sync. Also: either all streams will be bound or
  491. * none of them will be.
  492. *
  493. * It is an error to bind an already-bound stream; it must be explicitly unbound
  494. * first.
  495. *
  496. * Binding a stream to a device will set its output format for output devices,
  497. * and its input format for capture devices, so they match the device's
  498. * settings. The caller is welcome to change the other end of the stream's
  499. * format at any time.
  500. *
  501. * \param devid an audio device to bind a stream to.
  502. * \param streams an array of audio streams to unbind.
  503. * \param num_streams Number streams listed in the `streams` array.
  504. * \returns 0 on success, -1 on error; call SDL_GetError() for more information.
  505. *
  506. * \threadsafety It is safe to call this function from any thread.
  507. *
  508. * \since This function is available since SDL 3.0.0.
  509. *
  510. * \sa SDL_BindAudioStreams
  511. * \sa SDL_UnbindAudioStreams
  512. * \sa SDL_UnbindAudioStream
  513. * \sa SDL_GetAudioStreamBinding
  514. */
  515. extern DECLSPEC int SDLCALL SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream **streams, int num_streams);
  516. /**
  517. * Bind a single audio stream to an audio device.
  518. *
  519. * This is a convenience function, equivalent to calling
  520. * `SDL_BindAudioStreams(devid, &stream, 1)`.
  521. *
  522. * \param devid an audio device to bind a stream to.
  523. * \param stream an audio stream to bind to a device.
  524. * \returns 0 on success, -1 on error; call SDL_GetError() for more information.
  525. *
  526. * \threadsafety It is safe to call this function from any thread.
  527. *
  528. * \since This function is available since SDL 3.0.0.
  529. *
  530. * \sa SDL_BindAudioStreams
  531. * \sa SDL_UnbindAudioStreams
  532. * \sa SDL_UnbindAudioStream
  533. * \sa SDL_GetAudioStreamBinding
  534. */
  535. extern DECLSPEC int SDLCALL SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream);
  536. /**
  537. * Unbind a list of audio streams from their audio devices.
  538. *
  539. * The streams being unbound do not all have to be on the same device.
  540. * All streams on the same device will be unbound atomically (data will stop flowing
  541. * through them all unbound streams on the same device at the same time).
  542. *
  543. * Unbinding a stream that isn't bound to a device is a legal no-op.
  544. *
  545. * \param streams an array of audio streams to unbind.
  546. * \param num_streams Number streams listed in the `streams` array.
  547. *
  548. * \threadsafety It is safe to call this function from any thread.
  549. *
  550. * \since This function is available since SDL 3.0.0.
  551. *
  552. * \sa SDL_BindAudioStreams
  553. * \sa SDL_BindAudioStream
  554. * \sa SDL_UnbindAudioStream
  555. * \sa SDL_GetAudioStreamBinding
  556. */
  557. extern DECLSPEC void SDLCALL SDL_UnbindAudioStreams(SDL_AudioStream **streams, int num_streams);
  558. /**
  559. * Unbind a single audio stream from its audio device.
  560. *
  561. * This is a convenience function, equivalent to calling
  562. * `SDL_UnbindAudioStreams(&stream, 1)`.
  563. *
  564. * \param stream an audio stream to unbind from a device.
  565. *
  566. * \threadsafety It is safe to call this function from any thread.
  567. *
  568. * \since This function is available since SDL 3.0.0.
  569. *
  570. * \sa SDL_BindAudioStream
  571. * \sa SDL_BindAudioStreams
  572. * \sa SDL_UnbindAudioStreams
  573. * \sa SDL_GetAudioStreamBinding
  574. */
  575. extern DECLSPEC void SDLCALL SDL_UnbindAudioStream(SDL_AudioStream *stream);
  576. /**
  577. * Query an audio stream for its currently-bound device.
  578. *
  579. * This reports the audio device that an audio stream is currently bound to.
  580. *
  581. * If not bound, or invalid, this returns zero, which is not a valid device ID.
  582. *
  583. * \param stream the audio stream to query.
  584. * \returns The bound audio device, or 0 if not bound or invalid.
  585. *
  586. * \threadsafety It is safe to call this function from any thread.
  587. *
  588. * \since This function is available since SDL 3.0.0.
  589. *
  590. * \sa SDL_BindAudioStream
  591. * \sa SDL_BindAudioStreams
  592. * \sa SDL_UnbindAudioStream
  593. * \sa SDL_UnbindAudioStreams
  594. */
  595. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_GetAudioStreamBinding(SDL_AudioStream *stream);
  596. /**
  597. * Create a new audio stream.
  598. *
  599. * \param src_spec The format details of the input audio
  600. * \param dst_spec The format details of the output audio
  601. * \returns 0 on success, or -1 on error.
  602. *
  603. * \threadsafety It is safe to call this function from any thread.
  604. *
  605. * \since This function is available since SDL 3.0.0.
  606. *
  607. * \sa SDL_PutAudioStreamData
  608. * \sa SDL_GetAudioStreamData
  609. * \sa SDL_GetAudioStreamAvailable
  610. * \sa SDL_FlushAudioStream
  611. * \sa SDL_ClearAudioStream
  612. * \sa SDL_ChangeAudioStreamOutput
  613. * \sa SDL_DestroyAudioStream
  614. */
  615. extern DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
  616. /**
  617. * Query the current format of an audio stream.
  618. *
  619. * \param stream the SDL_AudioStream to query.
  620. * \param src_spec Where to store the input audio format; ignored if NULL.
  621. * \param dst_spec Where to store the output audio format; ignored if NULL.
  622. * \returns 0 on success, or -1 on error.
  623. *
  624. * \threadsafety It is safe to call this function from any thread, as it holds
  625. * a stream-specific mutex while running.
  626. *
  627. * \since This function is available since SDL 3.0.0.
  628. */
  629. extern DECLSPEC int SDLCALL SDL_GetAudioStreamFormat(SDL_AudioStream *stream,
  630. SDL_AudioSpec *src_spec,
  631. SDL_AudioSpec *dst_spec);
  632. /**
  633. * Change the input and output formats of an audio stream.
  634. *
  635. * Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData
  636. * will reflect the new format, and future calls to SDL_PutAudioStreamData
  637. * must provide data in the new input formats.
  638. *
  639. * \param stream The stream the format is being changed
  640. * \param src_spec The new format of the audio input; if NULL, it is not changed.
  641. * \param dst_spec The new format of the audio output; if NULL, it is not changed.
  642. * \returns 0 on success, or -1 on error.
  643. *
  644. * \threadsafety It is safe to call this function from any thread, as it holds
  645. * a stream-specific mutex while running.
  646. *
  647. * \since This function is available since SDL 3.0.0.
  648. *
  649. * \sa SDL_GetAudioStreamFormat
  650. * \sa SDL_PutAudioStreamData
  651. * \sa SDL_GetAudioStreamData
  652. * \sa SDL_GetAudioStreamAvailable
  653. */
  654. extern DECLSPEC int SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream,
  655. const SDL_AudioSpec *src_spec,
  656. const SDL_AudioSpec *dst_spec);
  657. /**
  658. * Add data to be converted/resampled to the stream.
  659. *
  660. * This data must match the format/channels/samplerate specified in the latest
  661. * call to SDL_SetAudioStreamFormat, or the format specified when creating the
  662. * stream if it hasn't been changed.
  663. *
  664. * Note that this call simply queues unconverted data for later. This is
  665. * different than SDL2, where data was converted during the Put call and the
  666. * Get call would just dequeue the previously-converted data.
  667. *
  668. * \param stream The stream the audio data is being added to
  669. * \param buf A pointer to the audio data to add
  670. * \param len The number of bytes to write to the stream
  671. * \returns 0 on success or a negative error code on failure; call
  672. * SDL_GetError() for more information.
  673. *
  674. * \threadsafety It is safe to call this function from any thread, but if the
  675. * stream has a callback set, the caller might need to manage
  676. * extra locking.
  677. *
  678. * \since This function is available since SDL 3.0.0.
  679. *
  680. * \sa SDL_CreateAudioStream
  681. * \sa SDL_GetAudioStreamData
  682. * \sa SDL_GetAudioStreamAvailable
  683. * \sa SDL_FlushAudioStream
  684. * \sa SDL_ClearAudioStream
  685. * \sa SDL_DestroyAudioStream
  686. */
  687. extern DECLSPEC int SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
  688. /**
  689. * Get converted/resampled data from the stream.
  690. *
  691. * The input/output data format/channels/samplerate is specified when creating
  692. * the stream, and can be changed after creation by calling
  693. * SDL_SetAudioStreamFormat.
  694. *
  695. * Note that any conversion and resampling necessary is done during this call,
  696. * and SDL_PutAudioStreamData simply queues unconverted data for later. This
  697. * is different than SDL2, where that work was done while inputting new data
  698. * to the stream and requesting the output just copied the converted data.
  699. *
  700. * \param stream The stream the audio is being requested from
  701. * \param buf A buffer to fill with audio data
  702. * \param len The maximum number of bytes to fill
  703. * \returns the number of bytes read from the stream, or -1 on error
  704. *
  705. * \threadsafety It is safe to call this function from any thread, but if the
  706. * stream has a callback set, the caller might need to manage
  707. * extra locking.
  708. *
  709. * \since This function is available since SDL 3.0.0.
  710. *
  711. * \sa SDL_CreateAudioStream
  712. * \sa SDL_PutAudioStreamData
  713. * \sa SDL_GetAudioStreamAvailable
  714. * \sa SDL_SetAudioStreamFormat
  715. * \sa SDL_FlushAudioStream
  716. * \sa SDL_ClearAudioStream
  717. * \sa SDL_DestroyAudioStream
  718. */
  719. extern DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
  720. /**
  721. * Get the number of converted/resampled bytes available.
  722. *
  723. * The stream may be buffering data behind the scenes until it has enough to
  724. * resample correctly, so this number might be lower than what you expect, or
  725. * even be zero. Add more data or flush the stream if you need the data now.
  726. *
  727. * If the stream has so much data that it would overflow an int, the return
  728. * value is clamped to a maximum value, but no queued data is lost; if there
  729. * are gigabytes of data queued, the app might need to read some of it with
  730. * SDL_GetAudioStreamData before this function's return value is no longer
  731. * clamped.
  732. *
  733. * \param stream The audio stream to query
  734. * \returns the number of converted/resampled bytes available.
  735. *
  736. * \threadsafety It is safe to call this function from any thread.
  737. *
  738. * \since This function is available since SDL 3.0.0.
  739. *
  740. * \sa SDL_CreateAudioStream
  741. * \sa SDL_PutAudioStreamData
  742. * \sa SDL_GetAudioStreamData
  743. * \sa SDL_FlushAudioStream
  744. * \sa SDL_ClearAudioStream
  745. * \sa SDL_DestroyAudioStream
  746. */
  747. extern DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
  748. /**
  749. * Tell the stream that you're done sending data, and anything being buffered
  750. * should be converted/resampled and made available immediately.
  751. *
  752. * It is legal to add more data to a stream after flushing, but there will be
  753. * audio gaps in the output. Generally this is intended to signal the end of
  754. * input, so the complete output becomes available.
  755. *
  756. * \param stream The audio stream to flush
  757. * \returns 0 on success or a negative error code on failure; call
  758. * SDL_GetError() for more information.
  759. *
  760. * \threadsafety It is safe to call this function from any thread.
  761. *
  762. * \since This function is available since SDL 3.0.0.
  763. *
  764. * \sa SDL_CreateAudioStream
  765. * \sa SDL_PutAudioStreamData
  766. * \sa SDL_GetAudioStreamData
  767. * \sa SDL_GetAudioStreamAvailable
  768. * \sa SDL_ClearAudioStream
  769. * \sa SDL_DestroyAudioStream
  770. */
  771. extern DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
  772. /**
  773. * Clear any pending data in the stream without converting it
  774. *
  775. * \param stream The audio stream to clear
  776. * \returns 0 on success or a negative error code on failure; call
  777. * SDL_GetError() for more information.
  778. *
  779. * \threadsafety It is safe to call this function from any thread.
  780. *
  781. * \since This function is available since SDL 3.0.0.
  782. *
  783. * \sa SDL_CreateAudioStream
  784. * \sa SDL_PutAudioStreamData
  785. * \sa SDL_GetAudioStreamData
  786. * \sa SDL_GetAudioStreamAvailable
  787. * \sa SDL_FlushAudioStream
  788. * \sa SDL_DestroyAudioStream
  789. */
  790. extern DECLSPEC int SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
  791. /**
  792. * Lock an audio stream for serialized access.
  793. *
  794. * Each SDL_AudioStream has an internal mutex it uses to
  795. * protect its data structures from threading conflicts. This function
  796. * allows an app to lock that mutex, which could be useful if
  797. * registering callbacks on this stream.
  798. *
  799. * One does not need to lock a stream to use in it most cases,
  800. * as the stream manages this lock internally. However, this lock
  801. * is held during callbacks, which may run from arbitrary threads
  802. * at any time, so if an app needs to protect shared data during
  803. * those callbacks, locking the stream guarantees that the
  804. * callback is not running while the lock is held.
  805. *
  806. * As this is just a wrapper over SDL_LockMutex for an internal
  807. * lock, it has all the same attributes (recursive locks are
  808. * allowed, etc).
  809. *
  810. * \param stream The audio stream to lock.
  811. * \returns 0 on success or a negative error code on failure; call
  812. * SDL_GetError() for more information.
  813. *
  814. * \threadsafety It is safe to call this function from any thread.
  815. *
  816. * \since This function is available since SDL 3.0.0.
  817. *
  818. * \sa SDL_UnlockAudioStream
  819. * \sa SDL_SetAudioStreamPutCallback
  820. * \sa SDL_SetAudioStreamGetCallback
  821. */
  822. extern DECLSPEC int SDLCALL SDL_LockAudioStream(SDL_AudioStream *stream);
  823. /**
  824. * Unlock an audio stream for serialized access.
  825. *
  826. * This unlocks an audio stream after a call to SDL_LockAudioStream.
  827. *
  828. * \param stream The audio stream to unlock.
  829. * \returns 0 on success or a negative error code on failure; call
  830. * SDL_GetError() for more information.
  831. *
  832. * \threadsafety You should only call this from the same thread that
  833. * previously called SDL_LockAudioStream.
  834. *
  835. * \since This function is available since SDL 3.0.0.
  836. *
  837. * \sa SDL_LockAudioStream
  838. * \sa SDL_SetAudioStreamPutCallback
  839. * \sa SDL_SetAudioStreamGetCallback
  840. */
  841. extern DECLSPEC int SDLCALL SDL_UnlockAudioStream(SDL_AudioStream *stream);
  842. /**
  843. * A callback that fires when data passes through an SDL_AudioStream.
  844. *
  845. * Apps can (optionally) register a callback with an audio stream that
  846. * is called when data is added with SDL_PutAudioStreamData, or requested
  847. * with SDL_GetAudioStreamData. These callbacks may run from any
  848. * thread, so if you need to protect shared data, you should use
  849. * SDL_LockAudioStream to serialize access; this lock will be held by
  850. * before your callback is called, so your callback does not need to
  851. * manage the lock explicitly.
  852. *
  853. * \param stream The SDL audio stream associated with this callback.
  854. * \param approx_request The _approximate_ amout of data, in bytes, that is requested.
  855. * This might be slightly overestimated due to buffering or
  856. * resampling, and may change from call to call anyhow.
  857. * \param userdata An opaque pointer provided by the app for their personal use.
  858. */
  859. typedef void (SDLCALL *SDL_AudioStreamRequestCallback)(SDL_AudioStream *stream, int approx_request, void *userdata);
  860. /**
  861. * Set a callback that runs when data is requested from an audio stream.
  862. *
  863. * This callback is called _before_ data is obtained from the stream,
  864. * giving the callback the chance to add more on-demand.
  865. *
  866. * The callback can (optionally) call SDL_PutAudioStreamData() to add
  867. * more audio to the stream during this call; if needed, the request
  868. * that triggered this callback will obtain the new data immediately.
  869. *
  870. * The callback's `approx_request` argument is roughly how many bytes
  871. * of _unconverted_ data (in the stream's input format) is needed by
  872. * the caller, although this may overestimate a little for safety.
  873. * This takes into account how much is already in the stream and only
  874. * asks for any extra necessary to resolve the request, which means
  875. * the callback may be asked for zero bytes, and a different amount
  876. * on each call.
  877. *
  878. * The callback is not required to supply exact amounts; it is allowed
  879. * to supply too much or too little or none at all. The caller will
  880. * get what's available, up to the amount they requested, regardless
  881. * of this callback's outcome.
  882. *
  883. * Clearing or flushing an audio stream does not call this callback.
  884. *
  885. * This function obtains the stream's lock, which means any existing
  886. * callback (get or put) in progress will finish running before setting
  887. * the new callback.
  888. *
  889. * Setting a NULL function turns off the callback.
  890. *
  891. * \param stream the audio stream to set the new callback on.
  892. * \param callback the new callback function to call when data is added to the stream.
  893. * \param userdata an opaque pointer provided to the callback for its own personal use.
  894. * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
  895. *
  896. * \threadsafety It is safe to call this function from any thread.
  897. *
  898. * \since This function is available since SDL 3.0.0.
  899. *
  900. * \sa SDL_SetAudioStreamPutCallback
  901. */
  902. extern DECLSPEC int SDLCALL SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamRequestCallback callback, void *userdata);
  903. /**
  904. * Set a callback that runs when data is added to an audio stream.
  905. *
  906. * This callback is called _after_ the data is added to the stream,
  907. * giving the callback the chance to obtain it immediately.
  908. *
  909. * The callback can (optionally) call SDL_GetAudioStreamData() to
  910. * obtain audio from the stream during this call.
  911. *
  912. * The callback's `approx_request` argument is how many bytes
  913. * of _converted_ data (in the stream's output format) was provided
  914. * by the caller, although this may underestimate a little for safety.
  915. * This value might be less than what is currently available in the
  916. * stream, if data was already there, and might be less than the
  917. * caller provided if the stream needs to keep a buffer to aid in
  918. * resampling. Which means the callback may be provided with zero
  919. * bytes, and a different amount on each call.
  920. *
  921. * The callback may call SDL_GetAudioStreamAvailable to see the
  922. * total amount currently available to read from the stream, instead
  923. * of the total provided by the current call.
  924. *
  925. * The callback is not required to obtain all data. It is allowed
  926. * to read less or none at all. Anything not read now simply remains
  927. * in the stream for later access.
  928. *
  929. * Clearing or flushing an audio stream does not call this callback.
  930. *
  931. * This function obtains the stream's lock, which means any existing
  932. * callback (get or put) in progress will finish running before setting
  933. * the new callback.
  934. *
  935. * Setting a NULL function turns off the callback.
  936. *
  937. * \param stream the audio stream to set the new callback on.
  938. * \param callback the new callback function to call when data is added to the stream.
  939. * \param userdata an opaque pointer provided to the callback for its own personal use.
  940. * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
  941. *
  942. * \threadsafety It is safe to call this function from any thread.
  943. *
  944. * \since This function is available since SDL 3.0.0.
  945. *
  946. * \sa SDL_SetAudioStreamGetCallback
  947. */
  948. extern DECLSPEC int SDLCALL SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamRequestCallback callback, void *userdata);
  949. /**
  950. * Free an audio stream
  951. *
  952. * \param stream The audio stream to free
  953. *
  954. * \threadsafety It is safe to call this function from any thread.
  955. *
  956. * \since This function is available since SDL 3.0.0.
  957. *
  958. * \sa SDL_CreateAudioStream
  959. * \sa SDL_PutAudioStreamData
  960. * \sa SDL_GetAudioStreamData
  961. * \sa SDL_GetAudioStreamAvailable
  962. * \sa SDL_FlushAudioStream
  963. * \sa SDL_ClearAudioStream
  964. */
  965. extern DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
  966. /**
  967. * Convenience function to create and bind an audio stream in one step.
  968. *
  969. * This manages the creation of an audio stream, and setting its format
  970. * correctly to match both the app and the audio device's needs. This is
  971. * optional, but slightly less cumbersome to set up for a common use case.
  972. *
  973. * The `spec` parameter represents the app's side of the audio stream. That
  974. * is, for recording audio, this will be the output format, and for playing
  975. * audio, this will be the input format. This function will set the other
  976. * side of the audio stream to the device's format.
  977. *
  978. * \param devid an audio device to bind a stream to. This must be an opened device, and can not be zero.
  979. * \param spec the audio stream's input format
  980. * \returns a bound audio stream on success, ready to use. NULL on error; call SDL_GetError() for more information.
  981. *
  982. * \threadsafety It is safe to call this function from any thread.
  983. *
  984. * \since This function is available since SDL 3.0.0.
  985. *
  986. * \sa SDL_BindAudioStreams
  987. * \sa SDL_UnbindAudioStreams
  988. * \sa SDL_UnbindAudioStream
  989. */
  990. extern DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAndBindAudioStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec);
  991. /**
  992. * Load the audio data of a WAVE file into memory.
  993. *
  994. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  995. * be valid pointers. The entire data portion of the file is then loaded into
  996. * memory and decoded if necessary.
  997. *
  998. * If `freesrc` is non-zero, the data source gets automatically closed and
  999. * freed before the function returns.
  1000. *
  1001. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  1002. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  1003. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  1004. * cause an error.
  1005. *
  1006. * If this function succeeds, the return value is zero and the pointer to the
  1007. * audio data allocated by the function is written to `audio_buf` and its
  1008. * length in bytes to `audio_len`. The SDL_AudioSpec members `freq`,
  1009. * `channels`, and `format` are set to the values of the audio data in the
  1010. * buffer. The `samples` member is set to a sane default and all
  1011. * others are set to zero.
  1012. *
  1013. * It's necessary to use SDL_free() to free the audio data returned in
  1014. * `audio_buf` when it is no longer used.
  1015. *
  1016. * Because of the underspecification of the .WAV format, there are many
  1017. * problematic files in the wild that cause issues with strict decoders. To
  1018. * provide compatibility with these files, this decoder is lenient in regards
  1019. * to the truncation of the file, the fact chunk, and the size of the RIFF
  1020. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  1021. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  1022. * tune the behavior of the loading process.
  1023. *
  1024. * Any file that is invalid (due to truncation, corruption, or wrong values in
  1025. * the headers), too big, or unsupported causes an error. Additionally, any
  1026. * critical I/O error from the data source will terminate the loading process
  1027. * with an error. The function returns NULL on error and in all cases (with
  1028. * the exception of `src` being NULL), an appropriate error message will be
  1029. * set.
  1030. *
  1031. * It is required that the data source supports seeking.
  1032. *
  1033. * Example:
  1034. *
  1035. * ```c
  1036. * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
  1037. * ```
  1038. *
  1039. * Note that the SDL_LoadWAV function does this same thing for you, but in a
  1040. * less messy way:
  1041. *
  1042. * ```c
  1043. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  1044. * ```
  1045. *
  1046. * \param src The data source for the WAVE data
  1047. * \param freesrc If non-zero, SDL will _always_ free the data source
  1048. * \param spec A pointer to an SDL_AudioSpec that will be set to the
  1049. * WAVE data's format details on successful return.
  1050. * \param audio_buf A pointer filled with the audio data, allocated by the
  1051. * function.
  1052. * \param audio_len A pointer filled with the length of the audio data buffer
  1053. * in bytes
  1054. * \returns This function, if successfully called, returns 0. `audio_buf`
  1055. * will be filled with a pointer to an allocated buffer
  1056. * containing the audio data, and `audio_len` is filled with the
  1057. * length of that audio buffer in bytes.
  1058. *
  1059. * This function returns -1 if the .WAV file cannot be opened, uses
  1060. * an unknown data format, or is corrupt; call SDL_GetError() for
  1061. * more information.
  1062. *
  1063. * When the application is done with the data returned in
  1064. * `audio_buf`, it should call SDL_free() to dispose of it.
  1065. *
  1066. * \threadsafety It is safe to call this function from any thread.
  1067. *
  1068. * \since This function is available since SDL 3.0.0.
  1069. *
  1070. * \sa SDL_free
  1071. * \sa SDL_LoadWAV
  1072. */
  1073. extern DECLSPEC int SDLCALL SDL_LoadWAV_RW(SDL_RWops * src, int freesrc,
  1074. SDL_AudioSpec * spec, Uint8 ** audio_buf,
  1075. Uint32 * audio_len);
  1076. /**
  1077. * Loads a WAV from a file path.
  1078. *
  1079. * This is a convenience function that is effectively the same as:
  1080. *
  1081. * ```c
  1082. * SDL_LoadWAV_RW(SDL_RWFromFile(path, "rb"), 1, spec, audio_buf, audio_len);
  1083. * ```
  1084. *
  1085. * Note that in SDL2, this was a preprocessor macro and not a real function.
  1086. *
  1087. * \param path The file path of the WAV file to open.
  1088. * \param spec A pointer to an SDL_AudioSpec that will be set to the
  1089. * WAVE data's format details on successful return.
  1090. * \param audio_buf A pointer filled with the audio data, allocated by the
  1091. * function.
  1092. * \param audio_len A pointer filled with the length of the audio data buffer
  1093. * in bytes
  1094. * \returns This function, if successfully called, returns 0. `audio_buf`
  1095. * will be filled with a pointer to an allocated buffer
  1096. * containing the audio data, and `audio_len` is filled with the
  1097. * length of that audio buffer in bytes.
  1098. *
  1099. * This function returns -1 if the .WAV file cannot be opened, uses
  1100. * an unknown data format, or is corrupt; call SDL_GetError() for
  1101. * more information.
  1102. *
  1103. * When the application is done with the data returned in
  1104. * `audio_buf`, it should call SDL_free() to dispose of it.
  1105. *
  1106. * \threadsafety It is safe to call this function from any thread.
  1107. *
  1108. * \since This function is available since SDL 3.0.0.
  1109. *
  1110. * \sa SDL_free
  1111. * \sa SDL_LoadWAV_RW
  1112. */
  1113. extern DECLSPEC int SDLCALL SDL_LoadWAV(const char *path, SDL_AudioSpec * spec,
  1114. Uint8 ** audio_buf, Uint32 * audio_len);
  1115. #define SDL_MIX_MAXVOLUME 128
  1116. /**
  1117. * Mix audio data in a specified format.
  1118. *
  1119. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  1120. * it into `dst`, performing addition, volume adjustment, and overflow
  1121. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  1122. * `format` data.
  1123. *
  1124. * This is provided for convenience -- you can mix your own audio data.
  1125. *
  1126. * Do not use this function for mixing together more than two streams of
  1127. * sample data. The output from repeated application of this function may be
  1128. * distorted by clipping, because there is no accumulator with greater range
  1129. * than the input (not to mention this being an inefficient way of doing it).
  1130. *
  1131. * It is a common misconception that this function is required to write audio
  1132. * data to an output stream in an audio callback. While you can do that,
  1133. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  1134. * audio stream with a volume adjustment.
  1135. *
  1136. * \param dst the destination for the mixed audio
  1137. * \param src the source audio buffer to be mixed
  1138. * \param format the SDL_AudioFormat structure representing the desired audio
  1139. * format
  1140. * \param len the length of the audio buffer in bytes
  1141. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  1142. * for full audio volume
  1143. * \returns 0 on success or a negative error code on failure; call
  1144. * SDL_GetError() for more information.
  1145. *
  1146. * \threadsafety It is safe to call this function from any thread.
  1147. *
  1148. * \since This function is available since SDL 3.0.0.
  1149. */
  1150. extern DECLSPEC int SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  1151. const Uint8 * src,
  1152. SDL_AudioFormat format,
  1153. Uint32 len, int volume);
  1154. /**
  1155. * Convert some audio data of one format to another format.
  1156. *
  1157. * Please note that this function is for convenience, but should not be used
  1158. * to resample audio in blocks, as it will introduce audio artifacts on the
  1159. * boundaries. You should only use this function if you are converting audio
  1160. * data in its entirety in one call. If you want to convert audio in smaller
  1161. * chunks, use an SDL_AudioStream, which is designed for this situation.
  1162. *
  1163. * Internally, this function creates and destroys an SDL_AudioStream on each
  1164. * use, so it's also less efficient than using one directly, if you need to
  1165. * convert multiple times.
  1166. *
  1167. * \param src_spec The format details of the input audio
  1168. * \param src_data The audio data to be converted
  1169. * \param src_len The len of src_data
  1170. * \param dst_spec The format details of the output audio
  1171. * \param dst_data Will be filled with a pointer to converted audio data,
  1172. * which should be freed with SDL_free(). On error, it will be
  1173. * NULL.
  1174. * \param dst_len Will be filled with the len of dst_data
  1175. * \returns 0 on success or a negative error code on failure; call
  1176. * SDL_GetError() for more information.
  1177. *
  1178. * \threadsafety It is safe to call this function from any thread.
  1179. *
  1180. * \since This function is available since SDL 3.0.0.
  1181. *
  1182. * \sa SDL_CreateAudioStream
  1183. */
  1184. extern DECLSPEC int SDLCALL SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec,
  1185. const Uint8 *src_data,
  1186. int src_len,
  1187. const SDL_AudioSpec *dst_spec,
  1188. Uint8 **dst_data,
  1189. int *dst_len);
  1190. /**
  1191. * Get the appropriate memset value for silencing an audio format.
  1192. *
  1193. * The value returned by this function can be used as the second
  1194. * argument to memset (or SDL_memset) to set an audio buffer in
  1195. * a specific format to silence.
  1196. *
  1197. * \param format the audio data format to query.
  1198. * \returns A byte value that can be passed to memset.
  1199. *
  1200. * \threadsafety It is safe to call this function from any thread.
  1201. *
  1202. * \since This function is available since SDL 3.0.0.
  1203. */
  1204. extern DECLSPEC int SDLCALL SDL_GetSilenceValueForFormat(SDL_AudioFormat format);
  1205. /* Ends C function definitions when using C++ */
  1206. #ifdef __cplusplus
  1207. }
  1208. #endif
  1209. #include <SDL3/SDL_close_code.h>
  1210. #endif /* SDL_audio_h_ */