SDL_audio.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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 to
  225. * speakers or headphones ("output" devices). If you want devices that record
  226. * 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 device
  230. * 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 freed
  234. * with SDL_free(), or NULL on error; call SDL_GetError() for more
  235. * 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 that play sound,
  250. * perhaps to speakers or headphones ("output" devices), use
  251. * SDL_GetAudioOutputDevices() instead.
  252. *
  253. * This only returns a list of physical devices; it will not have any device
  254. * 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 freed
  258. * with SDL_free(), or NULL on error; call SDL_GetError() for more
  259. * 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 currently
  290. * using. If the device isn't yet opened, this will report the device's
  291. * preferred format (or a reasonable default if this can't be determined).
  292. *
  293. * You may also specify SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
  294. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE here, which is useful for getting a
  295. * reasonable recommendation before opening the system-recommended default
  296. * device.
  297. *
  298. * \param devid the instance ID of the device to query.
  299. * \param spec On return, will be filled with device details.
  300. * \returns 0 on success or a negative error code on failure; call
  301. * SDL_GetError() for more information.
  302. *
  303. * \threadsafety It is safe to call this function from any thread.
  304. *
  305. * \since This function is available since SDL 3.0.0.
  306. */
  307. extern DECLSPEC int SDLCALL SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec);
  308. /**
  309. * Open a specific audio device.
  310. *
  311. * You can open both output and capture devices through this function. Output
  312. * devices will take data from bound audio streams, mix it, and send it to the
  313. * hardware. Capture devices will feed any bound audio streams with a copy of
  314. * any incoming data.
  315. *
  316. * An opened audio device starts out with no audio streams bound. To start
  317. * audio playing, bind a stream and supply audio data to it. Unlike SDL2,
  318. * there is no audio callback; you only bind audio streams and make sure they
  319. * have data flowing into them (although, as an optional feature, each audio
  320. * stream may have its own callback, which can be used to simulate SDL2's
  321. * semantics).
  322. *
  323. * If you don't care about opening a specific device, pass a `devid` of either
  324. * `SDL_AUDIO_DEVICE_DEFAULT_OUTPUT` or `SDL_AUDIO_DEVICE_DEFAULT_CAPTURE`. In
  325. * this case, SDL will try to pick the most reasonable default, and may also
  326. * switch between physical devices seamlessly later, if the most reasonable
  327. * default changes during the lifetime of this opened device (user changed the
  328. * default in the OS's system preferences, the default got unplugged so the
  329. * system jumped to a new default, the user plugged in headphones on a mobile
  330. * device, etc). Unless you have a good reason to choose a specific device,
  331. * this is probably what you want.
  332. *
  333. * You may request a specific format for the audio device, but there is no
  334. * promise the device will honor that request for several reasons. As such,
  335. * it's only meant to be a hint as to what data your app will provide. Audio
  336. * streams will accept data in whatever format you specify and manage
  337. * conversion for you as appropriate. SDL_GetAudioDeviceFormat can tell you
  338. * the preferred format for the device before opening and the actual format
  339. * the device is using after opening.
  340. *
  341. * It's legal to open the same device ID more than once; each successful open
  342. * will generate a new logical SDL_AudioDeviceID that is managed separately
  343. * from others on the same physical device. This allows libraries to open a
  344. * device separately from the main app and bind its own streams without
  345. * conflicting.
  346. *
  347. * It is also legal to open a device ID returned by a previous call to this
  348. * function; doing so just creates another logical device on the same physical
  349. * device. This may be useful for making logical groupings of audio streams.
  350. *
  351. * This function returns the opened device ID on success. This is a new,
  352. * unique SDL_AudioDeviceID that represents a logical device.
  353. *
  354. * Some backends might offer arbitrary devices (for example, a networked audio
  355. * protocol that can connect to an arbitrary server). For these, as a change
  356. * from SDL2, you should open a default device ID and use an SDL hint to
  357. * specify the target if you care, or otherwise let the backend figure out a
  358. * reasonable default. Most backends don't offer anything like this, and often
  359. * this would be an end user setting an environment variable for their custom
  360. * need, and not something an application should specifically manage.
  361. *
  362. * When done with an audio device, possibly at the end of the app's life, one
  363. * should call SDL_CloseAudioDevice() on the returned device id.
  364. *
  365. * \param devid the device instance id to open, or
  366. * SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
  367. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE for the most reasonable
  368. * default device.
  369. * \param spec the requested device configuration. Can be NULL to use
  370. * reasonable defaults.
  371. * \returns The device ID on success, 0 on error; call SDL_GetError() for more
  372. * 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 audio
  386. * streams will not progress, and no audio will be generated. Pausing one
  387. * 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 device is
  391. * a legal no-op.
  392. *
  393. * Pausing a device can be useful to halt all audio without unbinding all the
  394. * audio streams. This might be useful while a game is paused, or a level is
  395. * 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 device
  447. * 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
  472. * SDL_OpenAudioDevice()
  473. *
  474. * \threadsafety It is safe to call this function from any thread.
  475. *
  476. * \since This function is available since SDL 3.0.0.
  477. *
  478. * \sa SDL_OpenAudioDevice
  479. */
  480. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID devid);
  481. /**
  482. * Bind a list of audio streams to an audio device.
  483. *
  484. * Audio data will flow through any bound streams. For an output device, data
  485. * for all bound streams will be mixed together and fed to the device. For a
  486. * capture device, a copy of recorded data will be provided to each bound
  487. * stream.
  488. *
  489. * Audio streams can only be bound to an open device. This operation is
  490. * atomic--all streams bound in the same call will start processing at the
  491. * same time, so they can stay in sync. Also: either all streams will be bound
  492. * or none of them will be.
  493. *
  494. * It is an error to bind an already-bound stream; it must be explicitly
  495. * unbound first.
  496. *
  497. * Binding a stream to a device will set its output format for output devices,
  498. * and its input format for capture devices, so they match the device's
  499. * settings. The caller is welcome to change the other end of the stream's
  500. * format at any time.
  501. *
  502. * \param devid an audio device to bind a stream to.
  503. * \param streams an array of audio streams to unbind.
  504. * \param num_streams Number streams listed in the `streams` array.
  505. * \returns 0 on success, -1 on error; call SDL_GetError() for more
  506. * information.
  507. *
  508. * \threadsafety It is safe to call this function from any thread.
  509. *
  510. * \since This function is available since SDL 3.0.0.
  511. *
  512. * \sa SDL_BindAudioStreams
  513. * \sa SDL_UnbindAudioStreams
  514. * \sa SDL_UnbindAudioStream
  515. * \sa SDL_GetAudioStreamBinding
  516. */
  517. extern DECLSPEC int SDLCALL SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream **streams, int num_streams);
  518. /**
  519. * Bind a single audio stream to an audio device.
  520. *
  521. * This is a convenience function, equivalent to calling
  522. * `SDL_BindAudioStreams(devid, &stream, 1)`.
  523. *
  524. * \param devid an audio device to bind a stream to.
  525. * \param stream an audio stream to bind to a device.
  526. * \returns 0 on success, -1 on error; call SDL_GetError() for more
  527. * information.
  528. *
  529. * \threadsafety It is safe to call this function from any thread.
  530. *
  531. * \since This function is available since SDL 3.0.0.
  532. *
  533. * \sa SDL_BindAudioStreams
  534. * \sa SDL_UnbindAudioStreams
  535. * \sa SDL_UnbindAudioStream
  536. * \sa SDL_GetAudioStreamBinding
  537. */
  538. extern DECLSPEC int SDLCALL SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream);
  539. /**
  540. * Unbind a list of audio streams from their audio devices.
  541. *
  542. * The streams being unbound do not all have to be on the same device. All
  543. * streams on the same device will be unbound atomically (data will stop
  544. * flowing through them all unbound streams on the same device at the same
  545. * time).
  546. *
  547. * Unbinding a stream that isn't bound to a device is a legal no-op.
  548. *
  549. * \param streams an array of audio streams to unbind.
  550. * \param num_streams Number streams listed in the `streams` array.
  551. *
  552. * \threadsafety It is safe to call this function from any thread.
  553. *
  554. * \since This function is available since SDL 3.0.0.
  555. *
  556. * \sa SDL_BindAudioStreams
  557. * \sa SDL_BindAudioStream
  558. * \sa SDL_UnbindAudioStream
  559. * \sa SDL_GetAudioStreamBinding
  560. */
  561. extern DECLSPEC void SDLCALL SDL_UnbindAudioStreams(SDL_AudioStream **streams, int num_streams);
  562. /**
  563. * Unbind a single audio stream from its audio device.
  564. *
  565. * This is a convenience function, equivalent to calling
  566. * `SDL_UnbindAudioStreams(&stream, 1)`.
  567. *
  568. * \param stream an audio stream to unbind from a device.
  569. *
  570. * \threadsafety It is safe to call this function from any thread.
  571. *
  572. * \since This function is available since SDL 3.0.0.
  573. *
  574. * \sa SDL_BindAudioStream
  575. * \sa SDL_BindAudioStreams
  576. * \sa SDL_UnbindAudioStreams
  577. * \sa SDL_GetAudioStreamBinding
  578. */
  579. extern DECLSPEC void SDLCALL SDL_UnbindAudioStream(SDL_AudioStream *stream);
  580. /**
  581. * Query an audio stream for its currently-bound device.
  582. *
  583. * This reports the audio device that an audio stream is currently bound to.
  584. *
  585. * If not bound, or invalid, this returns zero, which is not a valid device
  586. * ID.
  587. *
  588. * \param stream the audio stream to query.
  589. * \returns The bound audio device, or 0 if not bound or invalid.
  590. *
  591. * \threadsafety It is safe to call this function from any thread.
  592. *
  593. * \since This function is available since SDL 3.0.0.
  594. *
  595. * \sa SDL_BindAudioStream
  596. * \sa SDL_BindAudioStreams
  597. * \sa SDL_UnbindAudioStream
  598. * \sa SDL_UnbindAudioStreams
  599. */
  600. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_GetAudioStreamBinding(SDL_AudioStream *stream);
  601. /**
  602. * Create a new audio stream.
  603. *
  604. * \param src_spec The format details of the input audio
  605. * \param dst_spec The format details of the output audio
  606. * \returns 0 on success, or -1 on error.
  607. *
  608. * \threadsafety It is safe to call this function from any thread.
  609. *
  610. * \since This function is available since SDL 3.0.0.
  611. *
  612. * \sa SDL_PutAudioStreamData
  613. * \sa SDL_GetAudioStreamData
  614. * \sa SDL_GetAudioStreamAvailable
  615. * \sa SDL_FlushAudioStream
  616. * \sa SDL_ClearAudioStream
  617. * \sa SDL_ChangeAudioStreamOutput
  618. * \sa SDL_DestroyAudioStream
  619. */
  620. extern DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
  621. /**
  622. * Query the current format of an audio stream.
  623. *
  624. * \param stream the SDL_AudioStream to query.
  625. * \param src_spec Where to store the input audio format; ignored if NULL.
  626. * \param dst_spec Where to store the output audio format; ignored if NULL.
  627. * \returns 0 on success, or -1 on error.
  628. *
  629. * \threadsafety It is safe to call this function from any thread, as it holds
  630. * a stream-specific mutex while running.
  631. *
  632. * \since This function is available since SDL 3.0.0.
  633. */
  634. extern DECLSPEC int SDLCALL SDL_GetAudioStreamFormat(SDL_AudioStream *stream,
  635. SDL_AudioSpec *src_spec,
  636. SDL_AudioSpec *dst_spec);
  637. /**
  638. * Change the input and output formats of an audio stream.
  639. *
  640. * Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData
  641. * will reflect the new format, and future calls to SDL_PutAudioStreamData
  642. * must provide data in the new input formats.
  643. *
  644. * \param stream The stream the format is being changed
  645. * \param src_spec The new format of the audio input; if NULL, it is not
  646. * changed.
  647. * \param dst_spec The new format of the audio output; if NULL, it is not
  648. * changed.
  649. * \returns 0 on success, or -1 on error.
  650. *
  651. * \threadsafety It is safe to call this function from any thread, as it holds
  652. * a stream-specific mutex while running.
  653. *
  654. * \since This function is available since SDL 3.0.0.
  655. *
  656. * \sa SDL_GetAudioStreamFormat
  657. * \sa SDL_PutAudioStreamData
  658. * \sa SDL_GetAudioStreamData
  659. * \sa SDL_GetAudioStreamAvailable
  660. */
  661. extern DECLSPEC int SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream,
  662. const SDL_AudioSpec *src_spec,
  663. const SDL_AudioSpec *dst_spec);
  664. /**
  665. * Add data to be converted/resampled to the stream.
  666. *
  667. * This data must match the format/channels/samplerate specified in the latest
  668. * call to SDL_SetAudioStreamFormat, or the format specified when creating the
  669. * stream if it hasn't been changed.
  670. *
  671. * Note that this call simply queues unconverted data for later. This is
  672. * different than SDL2, where data was converted during the Put call and the
  673. * Get call would just dequeue the previously-converted data.
  674. *
  675. * \param stream The stream the audio data is being added to
  676. * \param buf A pointer to the audio data to add
  677. * \param len The number of bytes to write to the stream
  678. * \returns 0 on success or a negative error code on failure; call
  679. * SDL_GetError() for more information.
  680. *
  681. * \threadsafety It is safe to call this function from any thread, but if the
  682. * stream has a callback set, the caller might need to manage
  683. * extra locking.
  684. *
  685. * \since This function is available since SDL 3.0.0.
  686. *
  687. * \sa SDL_CreateAudioStream
  688. * \sa SDL_GetAudioStreamData
  689. * \sa SDL_GetAudioStreamAvailable
  690. * \sa SDL_FlushAudioStream
  691. * \sa SDL_ClearAudioStream
  692. * \sa SDL_DestroyAudioStream
  693. */
  694. extern DECLSPEC int SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
  695. /**
  696. * Get converted/resampled data from the stream.
  697. *
  698. * The input/output data format/channels/samplerate is specified when creating
  699. * the stream, and can be changed after creation by calling
  700. * SDL_SetAudioStreamFormat.
  701. *
  702. * Note that any conversion and resampling necessary is done during this call,
  703. * and SDL_PutAudioStreamData simply queues unconverted data for later. This
  704. * is different than SDL2, where that work was done while inputting new data
  705. * to the stream and requesting the output just copied the converted data.
  706. *
  707. * \param stream The stream the audio is being requested from
  708. * \param buf A buffer to fill with audio data
  709. * \param len The maximum number of bytes to fill
  710. * \returns the number of bytes read from the stream, or -1 on error
  711. *
  712. * \threadsafety It is safe to call this function from any thread, but if the
  713. * stream has a callback set, the caller might need to manage
  714. * extra locking.
  715. *
  716. * \since This function is available since SDL 3.0.0.
  717. *
  718. * \sa SDL_CreateAudioStream
  719. * \sa SDL_PutAudioStreamData
  720. * \sa SDL_GetAudioStreamAvailable
  721. * \sa SDL_SetAudioStreamFormat
  722. * \sa SDL_FlushAudioStream
  723. * \sa SDL_ClearAudioStream
  724. * \sa SDL_DestroyAudioStream
  725. */
  726. extern DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
  727. /**
  728. * Get the number of converted/resampled bytes available.
  729. *
  730. * The stream may be buffering data behind the scenes until it has enough to
  731. * resample correctly, so this number might be lower than what you expect, or
  732. * even be zero. Add more data or flush the stream if you need the data now.
  733. *
  734. * If the stream has so much data that it would overflow an int, the return
  735. * value is clamped to a maximum value, but no queued data is lost; if there
  736. * are gigabytes of data queued, the app might need to read some of it with
  737. * SDL_GetAudioStreamData before this function's return value is no longer
  738. * clamped.
  739. *
  740. * \param stream The audio stream to query
  741. * \returns the number of converted/resampled bytes available.
  742. *
  743. * \threadsafety It is safe to call this function from any thread.
  744. *
  745. * \since This function is available since SDL 3.0.0.
  746. *
  747. * \sa SDL_CreateAudioStream
  748. * \sa SDL_PutAudioStreamData
  749. * \sa SDL_GetAudioStreamData
  750. * \sa SDL_FlushAudioStream
  751. * \sa SDL_ClearAudioStream
  752. * \sa SDL_DestroyAudioStream
  753. */
  754. extern DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
  755. /**
  756. * Tell the stream that you're done sending data, and anything being buffered
  757. * should be converted/resampled and made available immediately.
  758. *
  759. * It is legal to add more data to a stream after flushing, but there will be
  760. * audio gaps in the output. Generally this is intended to signal the end of
  761. * input, so the complete output becomes available.
  762. *
  763. * \param stream The audio stream to flush
  764. * \returns 0 on success or a negative error code on failure; call
  765. * SDL_GetError() for more information.
  766. *
  767. * \threadsafety It is safe to call this function from any thread.
  768. *
  769. * \since This function is available since SDL 3.0.0.
  770. *
  771. * \sa SDL_CreateAudioStream
  772. * \sa SDL_PutAudioStreamData
  773. * \sa SDL_GetAudioStreamData
  774. * \sa SDL_GetAudioStreamAvailable
  775. * \sa SDL_ClearAudioStream
  776. * \sa SDL_DestroyAudioStream
  777. */
  778. extern DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
  779. /**
  780. * Clear any pending data in the stream without converting it
  781. *
  782. * \param stream The audio stream to clear
  783. * \returns 0 on success or a negative error code on failure; call
  784. * SDL_GetError() for more information.
  785. *
  786. * \threadsafety It is safe to call this function from any thread.
  787. *
  788. * \since This function is available since SDL 3.0.0.
  789. *
  790. * \sa SDL_CreateAudioStream
  791. * \sa SDL_PutAudioStreamData
  792. * \sa SDL_GetAudioStreamData
  793. * \sa SDL_GetAudioStreamAvailable
  794. * \sa SDL_FlushAudioStream
  795. * \sa SDL_DestroyAudioStream
  796. */
  797. extern DECLSPEC int SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
  798. /**
  799. * Lock an audio stream for serialized access.
  800. *
  801. * Each SDL_AudioStream has an internal mutex it uses to protect its data
  802. * structures from threading conflicts. This function allows an app to lock
  803. * that mutex, which could be useful if registering callbacks on this stream.
  804. *
  805. * One does not need to lock a stream to use in it most cases, as the stream
  806. * manages this lock internally. However, this lock is held during callbacks,
  807. * which may run from arbitrary threads at any time, so if an app needs to
  808. * protect shared data during those callbacks, locking the stream guarantees
  809. * that the callback is not running while the lock is held.
  810. *
  811. * As this is just a wrapper over SDL_LockMutex for an internal lock, it has
  812. * all the same attributes (recursive locks are allowed, etc).
  813. *
  814. * \param stream The audio stream to lock.
  815. * \returns 0 on success or a negative error code on failure; call
  816. * SDL_GetError() for more information.
  817. *
  818. * \threadsafety It is safe to call this function from any thread.
  819. *
  820. * \since This function is available since SDL 3.0.0.
  821. *
  822. * \sa SDL_UnlockAudioStream
  823. * \sa SDL_SetAudioStreamPutCallback
  824. * \sa SDL_SetAudioStreamGetCallback
  825. */
  826. extern DECLSPEC int SDLCALL SDL_LockAudioStream(SDL_AudioStream *stream);
  827. /**
  828. * Unlock an audio stream for serialized access.
  829. *
  830. * This unlocks an audio stream after a call to SDL_LockAudioStream.
  831. *
  832. * \param stream The audio stream to unlock.
  833. * \returns 0 on success or a negative error code on failure; call
  834. * SDL_GetError() for more information.
  835. *
  836. * \threadsafety You should only call this from the same thread that
  837. * previously called SDL_LockAudioStream.
  838. *
  839. * \since This function is available since SDL 3.0.0.
  840. *
  841. * \sa SDL_LockAudioStream
  842. * \sa SDL_SetAudioStreamPutCallback
  843. * \sa SDL_SetAudioStreamGetCallback
  844. */
  845. extern DECLSPEC int SDLCALL SDL_UnlockAudioStream(SDL_AudioStream *stream);
  846. /**
  847. * A callback that fires when data passes through an SDL_AudioStream.
  848. *
  849. * Apps can (optionally) register a callback with an audio stream that
  850. * is called when data is added with SDL_PutAudioStreamData, or requested
  851. * with SDL_GetAudioStreamData. These callbacks may run from any
  852. * thread, so if you need to protect shared data, you should use
  853. * SDL_LockAudioStream to serialize access; this lock will be held by
  854. * before your callback is called, so your callback does not need to
  855. * manage the lock explicitly.
  856. *
  857. * \param stream The SDL audio stream associated with this callback.
  858. * \param approx_request The _approximate_ amout of data, in bytes, that is requested.
  859. * This might be slightly overestimated due to buffering or
  860. * resampling, and may change from call to call anyhow.
  861. * \param userdata An opaque pointer provided by the app for their personal use.
  862. */
  863. typedef void (SDLCALL *SDL_AudioStreamRequestCallback)(SDL_AudioStream *stream, int approx_request, void *userdata);
  864. /**
  865. * Set a callback that runs when data is requested from an audio stream.
  866. *
  867. * This callback is called _before_ data is obtained from the stream, giving
  868. * the callback the chance to add more on-demand.
  869. *
  870. * The callback can (optionally) call SDL_PutAudioStreamData() to add more
  871. * audio to the stream during this call; if needed, the request that triggered
  872. * this callback will obtain the new data immediately.
  873. *
  874. * The callback's `approx_request` argument is roughly how many bytes of
  875. * _unconverted_ data (in the stream's input format) is needed by the caller,
  876. * although this may overestimate a little for safety. This takes into account
  877. * how much is already in the stream and only asks for any extra necessary to
  878. * resolve the request, which means the callback may be asked for zero bytes,
  879. * and a different amount on each call.
  880. *
  881. * The callback is not required to supply exact amounts; it is allowed to
  882. * supply too much or too little or none at all. The caller will get what's
  883. * available, up to the amount they requested, regardless of this callback's
  884. * outcome.
  885. *
  886. * Clearing or flushing an audio stream does not call this callback.
  887. *
  888. * This function obtains the stream's lock, which means any existing callback
  889. * (get or put) in progress will finish running before setting the new
  890. * callback.
  891. *
  892. * Setting a NULL function turns off the callback.
  893. *
  894. * \param stream the audio stream to set the new callback on.
  895. * \param callback the new callback function to call when data is added to the
  896. * stream.
  897. * \param userdata an opaque pointer provided to the callback for its own
  898. * personal use.
  899. * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
  900. *
  901. * \threadsafety It is safe to call this function from any thread.
  902. *
  903. * \since This function is available since SDL 3.0.0.
  904. *
  905. * \sa SDL_SetAudioStreamPutCallback
  906. */
  907. extern DECLSPEC int SDLCALL SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamRequestCallback callback, void *userdata);
  908. /**
  909. * Set a callback that runs when data is added to an audio stream.
  910. *
  911. * This callback is called _after_ the data is added to the stream, giving the
  912. * callback the chance to obtain it immediately.
  913. *
  914. * The callback can (optionally) call SDL_GetAudioStreamData() to obtain audio
  915. * from the stream during this call.
  916. *
  917. * The callback's `approx_request` argument is how many bytes of _converted_
  918. * data (in the stream's output format) was provided by the caller, although
  919. * this may underestimate a little for safety. This value might be less than
  920. * what is currently available in the stream, if data was already there, and
  921. * might be less than the caller provided if the stream needs to keep a buffer
  922. * to aid in resampling. Which means the callback may be provided with zero
  923. * bytes, and a different amount on each call.
  924. *
  925. * The callback may call SDL_GetAudioStreamAvailable to see the total amount
  926. * currently available to read from the stream, instead of the total provided
  927. * by the current call.
  928. *
  929. * The callback is not required to obtain all data. It is allowed to read less
  930. * or none at all. Anything not read now simply remains in the stream for
  931. * later access.
  932. *
  933. * Clearing or flushing an audio stream does not call this callback.
  934. *
  935. * This function obtains the stream's lock, which means any existing callback
  936. * (get or put) in progress will finish running before setting the new
  937. * callback.
  938. *
  939. * Setting a NULL function turns off the callback.
  940. *
  941. * \param stream the audio stream to set the new callback on.
  942. * \param callback the new callback function to call when data is added to the
  943. * stream.
  944. * \param userdata an opaque pointer provided to the callback for its own
  945. * personal use.
  946. * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
  947. *
  948. * \threadsafety It is safe to call this function from any thread.
  949. *
  950. * \since This function is available since SDL 3.0.0.
  951. *
  952. * \sa SDL_SetAudioStreamGetCallback
  953. */
  954. extern DECLSPEC int SDLCALL SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamRequestCallback callback, void *userdata);
  955. /**
  956. * Free an audio stream
  957. *
  958. * \param stream The audio stream to free
  959. *
  960. * \threadsafety It is safe to call this function from any thread.
  961. *
  962. * \since This function is available since SDL 3.0.0.
  963. *
  964. * \sa SDL_CreateAudioStream
  965. * \sa SDL_PutAudioStreamData
  966. * \sa SDL_GetAudioStreamData
  967. * \sa SDL_GetAudioStreamAvailable
  968. * \sa SDL_FlushAudioStream
  969. * \sa SDL_ClearAudioStream
  970. */
  971. extern DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
  972. /**
  973. * Convenience function to create and bind an audio stream in one step.
  974. *
  975. * This manages the creation of an audio stream, and setting its format
  976. * correctly to match both the app and the audio device's needs. This is
  977. * optional, but slightly less cumbersome to set up for a common use case.
  978. *
  979. * The `spec` parameter represents the app's side of the audio stream. That
  980. * is, for recording audio, this will be the output format, and for playing
  981. * audio, this will be the input format. This function will set the other side
  982. * of the audio stream to the device's format.
  983. *
  984. * \param devid an audio device to bind a stream to. This must be an opened
  985. * device, and can not be zero.
  986. * \param spec the audio stream's input format
  987. * \returns a bound audio stream on success, ready to use. NULL on error; call
  988. * SDL_GetError() for more information.
  989. *
  990. * \threadsafety It is safe to call this function from any thread.
  991. *
  992. * \since This function is available since SDL 3.0.0.
  993. *
  994. * \sa SDL_BindAudioStreams
  995. * \sa SDL_UnbindAudioStreams
  996. * \sa SDL_UnbindAudioStream
  997. */
  998. extern DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAndBindAudioStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec);
  999. /**
  1000. * Load the audio data of a WAVE file into memory.
  1001. *
  1002. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  1003. * be valid pointers. The entire data portion of the file is then loaded into
  1004. * memory and decoded if necessary.
  1005. *
  1006. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  1007. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  1008. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  1009. * cause an error.
  1010. *
  1011. * If this function succeeds, the return value is zero and the pointer to the
  1012. * audio data allocated by the function is written to `audio_buf` and its
  1013. * length in bytes to `audio_len`. The SDL_AudioSpec members `freq`,
  1014. * `channels`, and `format` are set to the values of the audio data in the
  1015. * buffer. The `samples` member is set to a sane default and all others are
  1016. * set to zero.
  1017. *
  1018. * It's necessary to use SDL_free() to free the audio data returned in
  1019. * `audio_buf` when it is no longer used.
  1020. *
  1021. * Because of the underspecification of the .WAV format, there are many
  1022. * problematic files in the wild that cause issues with strict decoders. To
  1023. * provide compatibility with these files, this decoder is lenient in regards
  1024. * to the truncation of the file, the fact chunk, and the size of the RIFF
  1025. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  1026. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  1027. * tune the behavior of the loading process.
  1028. *
  1029. * Any file that is invalid (due to truncation, corruption, or wrong values in
  1030. * the headers), too big, or unsupported causes an error. Additionally, any
  1031. * critical I/O error from the data source will terminate the loading process
  1032. * with an error. The function returns NULL on error and in all cases (with
  1033. * the exception of `src` being NULL), an appropriate error message will be
  1034. * set.
  1035. *
  1036. * It is required that the data source supports seeking.
  1037. *
  1038. * Example:
  1039. *
  1040. * ```c
  1041. * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
  1042. * ```
  1043. *
  1044. * Note that the SDL_LoadWAV function does this same thing for you, but in a
  1045. * less messy way:
  1046. *
  1047. * ```c
  1048. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  1049. * ```
  1050. *
  1051. * \param src The data source for the WAVE data
  1052. * \param freesrc If SDL_TRUE, calls SDL_RWclose() on `src` before returning,
  1053. * even in the case of an error
  1054. * \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE
  1055. * data's format details on successful return
  1056. * \param audio_buf A pointer filled with the audio data, allocated by the
  1057. * function
  1058. * \param audio_len A pointer filled with the length of the audio data buffer
  1059. * in bytes
  1060. * \returns This function, if successfully called, returns 0. `audio_buf` will
  1061. * be filled with a pointer to an allocated buffer containing the
  1062. * audio data, and `audio_len` is filled with the length of that
  1063. * audio buffer in bytes.
  1064. *
  1065. * This function returns -1 if the .WAV file cannot be opened, uses
  1066. * an unknown data format, or is corrupt; call SDL_GetError() for
  1067. * more information.
  1068. *
  1069. * When the application is done with the data returned in
  1070. * `audio_buf`, it should call SDL_free() to dispose of it.
  1071. *
  1072. * \threadsafety It is safe to call this function from any thread.
  1073. *
  1074. * \since This function is available since SDL 3.0.0.
  1075. *
  1076. * \sa SDL_free
  1077. * \sa SDL_LoadWAV
  1078. */
  1079. extern DECLSPEC int SDLCALL SDL_LoadWAV_RW(SDL_RWops * src, SDL_bool freesrc,
  1080. SDL_AudioSpec * spec, Uint8 ** audio_buf,
  1081. Uint32 * audio_len);
  1082. /**
  1083. * Loads a WAV from a file path.
  1084. *
  1085. * This is a convenience function that is effectively the same as:
  1086. *
  1087. * ```c
  1088. * SDL_LoadWAV_RW(SDL_RWFromFile(path, "rb"), 1, spec, audio_buf, audio_len);
  1089. * ```
  1090. *
  1091. * Note that in SDL2, this was a preprocessor macro and not a real function.
  1092. *
  1093. * \param path The file path of the WAV file to open.
  1094. * \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE
  1095. * data's format details on successful return.
  1096. * \param audio_buf A pointer filled with the audio data, allocated by the
  1097. * function.
  1098. * \param audio_len A pointer filled with the length of the audio data buffer
  1099. * in bytes
  1100. * \returns This function, if successfully called, returns 0. `audio_buf` will
  1101. * be filled with a pointer to an allocated buffer containing the
  1102. * audio data, and `audio_len` is filled with the length of that
  1103. * audio buffer in bytes.
  1104. *
  1105. * This function returns -1 if the .WAV file cannot be opened, uses
  1106. * an unknown data format, or is corrupt; call SDL_GetError() for
  1107. * more information.
  1108. *
  1109. * When the application is done with the data returned in
  1110. * `audio_buf`, it should call SDL_free() to dispose of it.
  1111. *
  1112. * \threadsafety It is safe to call this function from any thread.
  1113. *
  1114. * \since This function is available since SDL 3.0.0.
  1115. *
  1116. * \sa SDL_free
  1117. * \sa SDL_LoadWAV_RW
  1118. */
  1119. extern DECLSPEC int SDLCALL SDL_LoadWAV(const char *path, SDL_AudioSpec * spec,
  1120. Uint8 ** audio_buf, Uint32 * audio_len);
  1121. #define SDL_MIX_MAXVOLUME 128
  1122. /**
  1123. * Mix audio data in a specified format.
  1124. *
  1125. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  1126. * it into `dst`, performing addition, volume adjustment, and overflow
  1127. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  1128. * `format` data.
  1129. *
  1130. * This is provided for convenience -- you can mix your own audio data.
  1131. *
  1132. * Do not use this function for mixing together more than two streams of
  1133. * sample data. The output from repeated application of this function may be
  1134. * distorted by clipping, because there is no accumulator with greater range
  1135. * than the input (not to mention this being an inefficient way of doing it).
  1136. *
  1137. * It is a common misconception that this function is required to write audio
  1138. * data to an output stream in an audio callback. While you can do that,
  1139. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  1140. * audio stream with a volume adjustment.
  1141. *
  1142. * \param dst the destination for the mixed audio
  1143. * \param src the source audio buffer to be mixed
  1144. * \param format the SDL_AudioFormat structure representing the desired audio
  1145. * format
  1146. * \param len the length of the audio buffer in bytes
  1147. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  1148. * for full audio volume
  1149. * \returns 0 on success or a negative error code on failure; call
  1150. * SDL_GetError() for more information.
  1151. *
  1152. * \threadsafety It is safe to call this function from any thread.
  1153. *
  1154. * \since This function is available since SDL 3.0.0.
  1155. */
  1156. extern DECLSPEC int SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  1157. const Uint8 * src,
  1158. SDL_AudioFormat format,
  1159. Uint32 len, int volume);
  1160. /**
  1161. * Convert some audio data of one format to another format.
  1162. *
  1163. * Please note that this function is for convenience, but should not be used
  1164. * to resample audio in blocks, as it will introduce audio artifacts on the
  1165. * boundaries. You should only use this function if you are converting audio
  1166. * data in its entirety in one call. If you want to convert audio in smaller
  1167. * chunks, use an SDL_AudioStream, which is designed for this situation.
  1168. *
  1169. * Internally, this function creates and destroys an SDL_AudioStream on each
  1170. * use, so it's also less efficient than using one directly, if you need to
  1171. * convert multiple times.
  1172. *
  1173. * \param src_spec The format details of the input audio
  1174. * \param src_data The audio data to be converted
  1175. * \param src_len The len of src_data
  1176. * \param dst_spec The format details of the output audio
  1177. * \param dst_data Will be filled with a pointer to converted audio data,
  1178. * which should be freed with SDL_free(). On error, it will be
  1179. * NULL.
  1180. * \param dst_len Will be filled with the len of dst_data
  1181. * \returns 0 on success or a negative error code on failure; call
  1182. * SDL_GetError() for more information.
  1183. *
  1184. * \threadsafety It is safe to call this function from any thread.
  1185. *
  1186. * \since This function is available since SDL 3.0.0.
  1187. *
  1188. * \sa SDL_CreateAudioStream
  1189. */
  1190. extern DECLSPEC int SDLCALL SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec,
  1191. const Uint8 *src_data,
  1192. int src_len,
  1193. const SDL_AudioSpec *dst_spec,
  1194. Uint8 **dst_data,
  1195. int *dst_len);
  1196. /**
  1197. * Get the appropriate memset value for silencing an audio format.
  1198. *
  1199. * The value returned by this function can be used as the second argument to
  1200. * memset (or SDL_memset) to set an audio buffer in a specific format to
  1201. * silence.
  1202. *
  1203. * \param format the audio data format to query.
  1204. * \returns A byte value that can be passed to memset.
  1205. *
  1206. * \threadsafety It is safe to call this function from any thread.
  1207. *
  1208. * \since This function is available since SDL 3.0.0.
  1209. */
  1210. extern DECLSPEC int SDLCALL SDL_GetSilenceValueForFormat(SDL_AudioFormat format);
  1211. /* Ends C function definitions when using C++ */
  1212. #ifdef __cplusplus
  1213. }
  1214. #endif
  1215. #include <SDL3/SDL_close_code.h>
  1216. #endif /* SDL_audio_h_ */