SDL_audio.h 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  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. * # CategoryAudio
  20. *
  21. * Audio functionality for the SDL library.
  22. *
  23. * All audio in SDL3 revolves around SDL_AudioStream. Whether you want to play
  24. * or record audio, convert it, stream it, buffer it, or mix it, you're going
  25. * to be passing it through an audio stream.
  26. *
  27. * Audio streams are quite flexible; they can accept any amount of data at a
  28. * time, in any supported format, and output it as needed in any other format,
  29. * even if the data format changes on either side halfway through.
  30. *
  31. * An app opens an audio device and binds any number of audio streams to it,
  32. * feeding more data to it as available. When the devices needs more data, it
  33. * will pull it from all bound streams and mix them together for playback.
  34. *
  35. * Audio streams can also use an app-provided callback to supply data
  36. * on-demand, which maps pretty closely to the SDL2 audio model.
  37. *
  38. * SDL also provides a simple .WAV loader in SDL_LoadWAV (and SDL_LoadWAV_IO
  39. * if you aren't reading from a file) as a basic means to load sound data into
  40. * your program.
  41. *
  42. * For multi-channel audio, the default SDL channel mapping is:
  43. *
  44. * ```
  45. * 2: FL FR (stereo)
  46. * 3: FL FR LFE (2.1 surround)
  47. * 4: FL FR BL BR (quad)
  48. * 5: FL FR LFE BL BR (4.1 surround)
  49. * 6: FL FR FC LFE SL SR (5.1 surround - last two can also be BL BR)
  50. * 7: FL FR FC LFE BC SL SR (6.1 surround)
  51. * 8: FL FR FC LFE BL BR SL SR (7.1 surround)
  52. * ```
  53. */
  54. #ifndef SDL_audio_h_
  55. #define SDL_audio_h_
  56. #include <SDL3/SDL_stdinc.h>
  57. #include <SDL3/SDL_endian.h>
  58. #include <SDL3/SDL_error.h>
  59. #include <SDL3/SDL_mutex.h>
  60. #include <SDL3/SDL_properties.h>
  61. #include <SDL3/SDL_iostream.h>
  62. #include <SDL3/SDL_thread.h>
  63. #include <SDL3/SDL_begin_code.h>
  64. /* Set up for C function definitions, even when using C++ */
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68. /**
  69. * Audio format flags.
  70. *
  71. * These are what the 16 bits in SDL_AudioFormat currently mean...
  72. * (Unspecified bits are always zero).
  73. *
  74. * ```
  75. * ++-----------------------sample is signed if set
  76. * ||
  77. * || ++-----------sample is bigendian if set
  78. * || ||
  79. * || || ++---sample is float if set
  80. * || || ||
  81. * || || || +=--sample bit size--++
  82. * || || || || ||
  83. * 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
  84. * ```
  85. *
  86. * There are macros to query these bits.
  87. *
  88. * \since This datatype is available since SDL 3.0.0.
  89. *
  90. * \sa SDL_AUDIO_BITSIZE
  91. * \sa SDL_AUDIO_BYTESIZE
  92. * \sa SDL_AUDIO_ISINT
  93. * \sa SDL_AUDIO_ISFLOAT
  94. * \sa SDL_AUDIO_ISBIGENDIAN
  95. * \sa SDL_AUDIO_ISLITTLEENDIAN
  96. * \sa SDL_AUDIO_ISSIGNED
  97. * \sa SDL_AUDIO_ISUNSIGNED
  98. */
  99. typedef Uint16 SDL_AudioFormat;
  100. #define SDL_AUDIO_U8 0x0008u /**< Unsigned 8-bit samples */
  101. #define SDL_AUDIO_S8 0x8008u /**< Signed 8-bit samples */
  102. #define SDL_AUDIO_S16LE 0x8010u /**< Signed 16-bit samples */
  103. #define SDL_AUDIO_S16BE 0x9010u /**< As above, but big-endian byte order */
  104. #define SDL_AUDIO_S32LE 0x8020u /**< 32-bit integer samples */
  105. #define SDL_AUDIO_S32BE 0x9020u /**< As above, but big-endian byte order */
  106. #define SDL_AUDIO_F32LE 0x8120u /**< 32-bit floating point samples */
  107. #define SDL_AUDIO_F32BE 0x9120u /**< As above, but big-endian byte order */
  108. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  109. #define SDL_AUDIO_S16 SDL_AUDIO_S16LE
  110. #define SDL_AUDIO_S32 SDL_AUDIO_S32LE
  111. #define SDL_AUDIO_F32 SDL_AUDIO_F32LE
  112. #else
  113. #define SDL_AUDIO_S16 SDL_AUDIO_S16BE
  114. #define SDL_AUDIO_S32 SDL_AUDIO_S32BE
  115. #define SDL_AUDIO_F32 SDL_AUDIO_F32BE
  116. #endif
  117. /* masks for different parts of SDL_AudioFormat. */
  118. #define SDL_AUDIO_MASK_BITSIZE (0xFFu)
  119. #define SDL_AUDIO_MASK_FLOAT (1u<<8)
  120. #define SDL_AUDIO_MASK_BIG_ENDIAN (1u<<12)
  121. #define SDL_AUDIO_MASK_SIGNED (1u<<15)
  122. /**
  123. * Retrieve the size, in bits, from an SDL_AudioFormat.
  124. *
  125. * For example, `SDL_AUDIO_BITSIZE(SDL_AUDIO_S16)` returns 16.
  126. *
  127. * \param x an SDL_AudioFormat value
  128. * \returns data size in bits
  129. *
  130. * \threadsafety It is safe to call this macro from any thread.
  131. *
  132. * \since This macro is available since SDL 3.0.0.
  133. */
  134. #define SDL_AUDIO_BITSIZE(x) ((x) & SDL_AUDIO_MASK_BITSIZE)
  135. /**
  136. * Retrieve the size, in bytes, from an SDL_AudioFormat.
  137. *
  138. * For example, `SDL_AUDIO_BYTESIZE(SDL_AUDIO_S16)` returns 2.
  139. *
  140. * \param x an SDL_AudioFormat value
  141. * \returns data size in bytes
  142. *
  143. * \threadsafety It is safe to call this macro from any thread.
  144. *
  145. * \since This macro is available since SDL 3.0.0.
  146. */
  147. #define SDL_AUDIO_BYTESIZE(x) (SDL_AUDIO_BITSIZE(x) / 8)
  148. /**
  149. * Determine if an SDL_AudioFormat represents floating point data.
  150. *
  151. * For example, `SDL_AUDIO_ISFLOAT(SDL_AUDIO_S16)` returns 0.
  152. *
  153. * \param x an SDL_AudioFormat value
  154. * \returns non-zero if format is floating point, zero otherwise.
  155. *
  156. * \threadsafety It is safe to call this macro from any thread.
  157. *
  158. * \since This macro is available since SDL 3.0.0.
  159. */
  160. #define SDL_AUDIO_ISFLOAT(x) ((x) & SDL_AUDIO_MASK_FLOAT)
  161. /**
  162. * Determine if an SDL_AudioFormat represents bigendian data.
  163. *
  164. * For example, `SDL_AUDIO_ISBIGENDIAN(SDL_AUDIO_S16LE)` returns 0.
  165. *
  166. * \param x an SDL_AudioFormat value
  167. * \returns non-zero if format is bigendian, zero otherwise.
  168. *
  169. * \threadsafety It is safe to call this macro from any thread.
  170. *
  171. * \since This macro is available since SDL 3.0.0.
  172. */
  173. #define SDL_AUDIO_ISBIGENDIAN(x) ((x) & SDL_AUDIO_MASK_BIG_ENDIAN)
  174. /**
  175. * Determine if an SDL_AudioFormat represents littleendian data.
  176. *
  177. * For example, `SDL_AUDIO_ISLITTLEENDIAN(SDL_AUDIO_S16BE)` returns 0.
  178. *
  179. * \param x an SDL_AudioFormat value
  180. * \returns non-zero if format is littleendian, zero otherwise.
  181. *
  182. * \threadsafety It is safe to call this macro from any thread.
  183. *
  184. * \since This macro is available since SDL 3.0.0.
  185. */
  186. #define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))
  187. /**
  188. * Determine if an SDL_AudioFormat represents signed data.
  189. *
  190. * For example, `SDL_AUDIO_ISSIGNED(SDL_AUDIO_U8)` returns 0.
  191. *
  192. * \param x an SDL_AudioFormat value
  193. * \returns non-zero if format is signed, zero otherwise.
  194. *
  195. * \threadsafety It is safe to call this macro from any thread.
  196. *
  197. * \since This macro is available since SDL 3.0.0.
  198. */
  199. #define SDL_AUDIO_ISSIGNED(x) ((x) & SDL_AUDIO_MASK_SIGNED)
  200. /**
  201. * Determine if an SDL_AudioFormat represents integer data.
  202. *
  203. * For example, `SDL_AUDIO_ISINT(SDL_AUDIO_F32)` returns 0.
  204. *
  205. * \param x an SDL_AudioFormat value
  206. * \returns non-zero if format is integer, zero otherwise.
  207. *
  208. * \threadsafety It is safe to call this macro from any thread.
  209. *
  210. * \since This macro is available since SDL 3.0.0.
  211. */
  212. #define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))
  213. /**
  214. * Determine if an SDL_AudioFormat represents unsigned data.
  215. *
  216. * For example, `SDL_AUDIO_ISUNSIGNED(SDL_AUDIO_S16)` returns 0.
  217. *
  218. * \param x an SDL_AudioFormat value
  219. * \returns non-zero if format is unsigned, zero otherwise.
  220. *
  221. * \threadsafety It is safe to call this macro from any thread.
  222. *
  223. * \since This macro is available since SDL 3.0.0.
  224. */
  225. #define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))
  226. /**
  227. * SDL Audio Device instance IDs.
  228. *
  229. * Zero is used to signify an invalid/null device.
  230. *
  231. * \since This datatype is available since SDL 3.0.0.
  232. */
  233. typedef Uint32 SDL_AudioDeviceID;
  234. /**
  235. * A value used to request a default output audio device.
  236. *
  237. * Several functions that require an SDL_AudioDeviceID will accept this value
  238. * to signify the app just wants the system to choose a default device instead
  239. * of the app providing a specific one.
  240. *
  241. * \since This macro is available since SDL 3.0.0.
  242. */
  243. #define SDL_AUDIO_DEVICE_DEFAULT_OUTPUT ((SDL_AudioDeviceID) 0xFFFFFFFF)
  244. /**
  245. * A value used to request a default capture audio device.
  246. *
  247. * Several functions that require an SDL_AudioDeviceID will accept this value
  248. * to signify the app just wants the system to choose a default device instead
  249. * of the app providing a specific one.
  250. *
  251. * \since This macro is available since SDL 3.0.0.
  252. */
  253. #define SDL_AUDIO_DEVICE_DEFAULT_CAPTURE ((SDL_AudioDeviceID) 0xFFFFFFFE)
  254. /**
  255. * Format specifier for audio data.
  256. *
  257. * \since This struct is available since SDL 3.0.0.
  258. *
  259. * \sa SDL_AudioFormat
  260. */
  261. typedef struct SDL_AudioSpec
  262. {
  263. SDL_AudioFormat format; /**< Audio data format */
  264. int channels; /**< Number of channels: 1 mono, 2 stereo, etc */
  265. int freq; /**< sample rate: sample frames per second */
  266. } SDL_AudioSpec;
  267. /**
  268. * Calculate the size of each audio frame (in bytes) from an SDL_AudioSpec.
  269. *
  270. * This reports on the size of an audio sample frame: stereo Sint16 data (2
  271. * channels of 2 bytes each) would be 4 bytes per frame, for example.
  272. *
  273. * \param x an SDL_AudioSpec to query.
  274. * \returns the number of bytes used per sample frame.
  275. *
  276. * \threadsafety It is safe to call this macro from any thread.
  277. *
  278. * \since This macro is available since SDL 3.0.0.
  279. */
  280. #define SDL_AUDIO_FRAMESIZE(x) (SDL_AUDIO_BYTESIZE((x).format) * (x).channels)
  281. /**
  282. * The opaque handle that represents an audio stream.
  283. *
  284. * SDL_AudioStream is an audio conversion interface.
  285. *
  286. * - It can handle resampling data in chunks without generating artifacts,
  287. * when it doesn't have the complete buffer available.
  288. * - It can handle incoming data in any variable size.
  289. * - It can handle input/output format changes on the fly.
  290. * - You push data as you have it, and pull it when you need it
  291. * - It can also function as a basic audio data queue even if you just have
  292. * sound that needs to pass from one place to another.
  293. * - You can hook callbacks up to them when more data is added or requested,
  294. * to manage data on-the-fly.
  295. *
  296. * Audio streams are the core of the SDL3 audio interface. You create one or
  297. * more of them, bind them to an opened audio device, and feed data to them
  298. * (or for recording, consume data from them).
  299. *
  300. * \since This struct is available since SDL 3.0.0.
  301. *
  302. * \sa SDL_CreateAudioStream
  303. */
  304. typedef struct SDL_AudioStream SDL_AudioStream;
  305. /* Function prototypes */
  306. /**
  307. * \name Driver discovery functions
  308. *
  309. * These functions return the list of built in audio drivers, in the
  310. * order that they are normally initialized by default.
  311. */
  312. /* @{ */
  313. /**
  314. * Use this function to get the number of built-in audio drivers.
  315. *
  316. * This function returns a hardcoded number. This never returns a negative
  317. * value; if there are no drivers compiled into this build of SDL, this
  318. * function returns zero. The presence of a driver in this list does not mean
  319. * it will function, it just means SDL is capable of interacting with that
  320. * interface. For example, a build of SDL might have esound support, but if
  321. * there's no esound server available, SDL's esound driver would fail if used.
  322. *
  323. * By default, SDL tries all drivers, in its preferred order, until one is
  324. * found to be usable.
  325. *
  326. * \returns the number of built-in audio drivers.
  327. *
  328. * \threadsafety It is safe to call this function from any thread.
  329. *
  330. * \since This function is available since SDL 3.0.0.
  331. *
  332. * \sa SDL_GetAudioDriver
  333. */
  334. extern SDL_DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
  335. /**
  336. * Use this function to get the name of a built in audio driver.
  337. *
  338. * The list of audio drivers is given in the order that they are normally
  339. * initialized by default; the drivers that seem more reasonable to choose
  340. * first (as far as the SDL developers believe) are earlier in the list.
  341. *
  342. * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
  343. * "coreaudio" or "wasapi". These never have Unicode characters, and are not
  344. * meant to be proper names.
  345. *
  346. * The returned string follows the SDL_GetStringRule.
  347. *
  348. * \param index the index of the audio driver; the value ranges from 0 to
  349. * SDL_GetNumAudioDrivers() - 1
  350. * \returns the name of the audio driver at the requested index, or NULL if an
  351. * invalid index was specified.
  352. *
  353. * \threadsafety It is safe to call this function from any thread.
  354. *
  355. * \since This function is available since SDL 3.0.0.
  356. *
  357. * \sa SDL_GetNumAudioDrivers
  358. */
  359. extern SDL_DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
  360. /* @} */
  361. /**
  362. * Get the name of the current audio driver.
  363. *
  364. * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
  365. * "coreaudio" or "wasapi". These never have Unicode characters, and are not
  366. * meant to be proper names.
  367. *
  368. * The returned string follows the SDL_GetStringRule.
  369. *
  370. * \returns the name of the current audio driver or NULL if no driver has been
  371. * initialized.
  372. *
  373. * \threadsafety It is safe to call this function from any thread.
  374. *
  375. * \since This function is available since SDL 3.0.0.
  376. */
  377. extern SDL_DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
  378. /**
  379. * Get a list of currently-connected audio output devices.
  380. *
  381. * This returns of list of available devices that play sound, perhaps to
  382. * speakers or headphones ("output" devices). If you want devices that record
  383. * audio, like a microphone ("capture" devices), use
  384. * SDL_GetAudioCaptureDevices() instead.
  385. *
  386. * This only returns a list of physical devices; it will not have any device
  387. * IDs returned by SDL_OpenAudioDevice().
  388. *
  389. * If this function returns NULL, to signify an error, `*count` will be set to
  390. * zero.
  391. *
  392. * \param count a pointer filled in with the number of devices returned. NULL
  393. * is allowed.
  394. * \returns a 0 terminated array of device instance IDs which should be freed
  395. * with SDL_free(), or NULL on error; call SDL_GetError() for more
  396. * details.
  397. *
  398. * \threadsafety It is safe to call this function from any thread.
  399. *
  400. * \since This function is available since SDL 3.0.0.
  401. *
  402. * \sa SDL_OpenAudioDevice
  403. * \sa SDL_GetAudioCaptureDevices
  404. */
  405. extern SDL_DECLSPEC SDL_AudioDeviceID *SDLCALL SDL_GetAudioOutputDevices(int *count);
  406. /**
  407. * Get a list of currently-connected audio capture devices.
  408. *
  409. * This returns of list of available devices that record audio, like a
  410. * microphone ("capture" devices). If you want devices that play sound,
  411. * perhaps to speakers or headphones ("output" devices), use
  412. * SDL_GetAudioOutputDevices() instead.
  413. *
  414. * This only returns a list of physical devices; it will not have any device
  415. * IDs returned by SDL_OpenAudioDevice().
  416. *
  417. * If this function returns NULL, to signify an error, `*count` will be set to
  418. * zero.
  419. *
  420. * \param count a pointer filled in with the number of devices returned. NULL
  421. * is allowed.
  422. * \returns a 0 terminated array of device instance IDs which should be freed
  423. * with SDL_free(), or NULL on error; call SDL_GetError() for more
  424. * details.
  425. *
  426. * \threadsafety It is safe to call this function from any thread.
  427. *
  428. * \since This function is available since SDL 3.0.0.
  429. *
  430. * \sa SDL_OpenAudioDevice
  431. * \sa SDL_GetAudioOutputDevices
  432. */
  433. extern SDL_DECLSPEC SDL_AudioDeviceID *SDLCALL SDL_GetAudioCaptureDevices(int *count);
  434. /**
  435. * Get the human-readable name of a specific audio device.
  436. *
  437. * The returned string follows the SDL_GetStringRule.
  438. *
  439. * \param devid the instance ID of the device to query.
  440. * \returns the name of the audio device, or NULL on error.
  441. *
  442. * \threadsafety It is safe to call this function from any thread.
  443. *
  444. * \since This function is available since SDL 3.0.0.
  445. *
  446. * \sa SDL_GetAudioOutputDevices
  447. * \sa SDL_GetAudioCaptureDevices
  448. * \sa SDL_GetDefaultAudioInfo
  449. */
  450. extern SDL_DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(SDL_AudioDeviceID devid);
  451. /**
  452. * Get the current audio format of a specific audio device.
  453. *
  454. * For an opened device, this will report the format the device is currently
  455. * using. If the device isn't yet opened, this will report the device's
  456. * preferred format (or a reasonable default if this can't be determined).
  457. *
  458. * You may also specify SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
  459. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE here, which is useful for getting a
  460. * reasonable recommendation before opening the system-recommended default
  461. * device.
  462. *
  463. * You can also use this to request the current device buffer size. This is
  464. * specified in sample frames and represents the amount of data SDL will feed
  465. * to the physical hardware in each chunk. This can be converted to
  466. * milliseconds of audio with the following equation:
  467. *
  468. * `ms = (int) ((((Sint64) frames) * 1000) / spec.freq);`
  469. *
  470. * Buffer size is only important if you need low-level control over the audio
  471. * playback timing. Most apps do not need this.
  472. *
  473. * \param devid the instance ID of the device to query.
  474. * \param spec On return, will be filled with device details.
  475. * \param sample_frames Pointer to store device buffer size, in sample frames.
  476. * Can be NULL.
  477. * \returns 0 on success or a negative error code on failure; call
  478. * SDL_GetError() for more information.
  479. *
  480. * \threadsafety It is safe to call this function from any thread.
  481. *
  482. * \since This function is available since SDL 3.0.0.
  483. */
  484. extern SDL_DECLSPEC int SDLCALL SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec, int *sample_frames);
  485. /**
  486. * Open a specific audio device.
  487. *
  488. * You can open both output and capture devices through this function. Output
  489. * devices will take data from bound audio streams, mix it, and send it to the
  490. * hardware. Capture devices will feed any bound audio streams with a copy of
  491. * any incoming data.
  492. *
  493. * An opened audio device starts out with no audio streams bound. To start
  494. * audio playing, bind a stream and supply audio data to it. Unlike SDL2,
  495. * there is no audio callback; you only bind audio streams and make sure they
  496. * have data flowing into them (however, you can simulate SDL2's semantics
  497. * fairly closely by using SDL_OpenAudioDeviceStream instead of this
  498. * function).
  499. *
  500. * If you don't care about opening a specific device, pass a `devid` of either
  501. * `SDL_AUDIO_DEVICE_DEFAULT_OUTPUT` or `SDL_AUDIO_DEVICE_DEFAULT_CAPTURE`. In
  502. * this case, SDL will try to pick the most reasonable default, and may also
  503. * switch between physical devices seamlessly later, if the most reasonable
  504. * default changes during the lifetime of this opened device (user changed the
  505. * default in the OS's system preferences, the default got unplugged so the
  506. * system jumped to a new default, the user plugged in headphones on a mobile
  507. * device, etc). Unless you have a good reason to choose a specific device,
  508. * this is probably what you want.
  509. *
  510. * You may request a specific format for the audio device, but there is no
  511. * promise the device will honor that request for several reasons. As such,
  512. * it's only meant to be a hint as to what data your app will provide. Audio
  513. * streams will accept data in whatever format you specify and manage
  514. * conversion for you as appropriate. SDL_GetAudioDeviceFormat can tell you
  515. * the preferred format for the device before opening and the actual format
  516. * the device is using after opening.
  517. *
  518. * It's legal to open the same device ID more than once; each successful open
  519. * will generate a new logical SDL_AudioDeviceID that is managed separately
  520. * from others on the same physical device. This allows libraries to open a
  521. * device separately from the main app and bind its own streams without
  522. * conflicting.
  523. *
  524. * It is also legal to open a device ID returned by a previous call to this
  525. * function; doing so just creates another logical device on the same physical
  526. * device. This may be useful for making logical groupings of audio streams.
  527. *
  528. * This function returns the opened device ID on success. This is a new,
  529. * unique SDL_AudioDeviceID that represents a logical device.
  530. *
  531. * Some backends might offer arbitrary devices (for example, a networked audio
  532. * protocol that can connect to an arbitrary server). For these, as a change
  533. * from SDL2, you should open a default device ID and use an SDL hint to
  534. * specify the target if you care, or otherwise let the backend figure out a
  535. * reasonable default. Most backends don't offer anything like this, and often
  536. * this would be an end user setting an environment variable for their custom
  537. * need, and not something an application should specifically manage.
  538. *
  539. * When done with an audio device, possibly at the end of the app's life, one
  540. * should call SDL_CloseAudioDevice() on the returned device id.
  541. *
  542. * \param devid the device instance id to open, or
  543. * SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
  544. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE for the most reasonable
  545. * default device.
  546. * \param spec the requested device configuration. Can be NULL to use
  547. * reasonable defaults.
  548. * \returns The device ID on success, 0 on error; call SDL_GetError() for more
  549. * information.
  550. *
  551. * \threadsafety It is safe to call this function from any thread.
  552. *
  553. * \since This function is available since SDL 3.0.0.
  554. *
  555. * \sa SDL_CloseAudioDevice
  556. * \sa SDL_GetAudioDeviceFormat
  557. */
  558. extern SDL_DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec);
  559. /**
  560. * Use this function to pause audio playback on a specified device.
  561. *
  562. * This function pauses audio processing for a given device. Any bound audio
  563. * streams will not progress, and no audio will be generated. Pausing one
  564. * device does not prevent other unpaused devices from running.
  565. *
  566. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  567. * has to bind a stream before any audio will flow. Pausing a paused device is
  568. * a legal no-op.
  569. *
  570. * Pausing a device can be useful to halt all audio without unbinding all the
  571. * audio streams. This might be useful while a game is paused, or a level is
  572. * loading, etc.
  573. *
  574. * Physical devices can not be paused or unpaused, only logical devices
  575. * created through SDL_OpenAudioDevice() can be.
  576. *
  577. * \param dev a device opened by SDL_OpenAudioDevice()
  578. * \returns 0 on success or a negative error code on failure; call
  579. * SDL_GetError() for more information.
  580. *
  581. * \threadsafety It is safe to call this function from any thread.
  582. *
  583. * \since This function is available since SDL 3.0.0.
  584. *
  585. * \sa SDL_ResumeAudioDevice
  586. * \sa SDL_AudioDevicePaused
  587. */
  588. extern SDL_DECLSPEC int SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev);
  589. /**
  590. * Use this function to unpause audio playback on a specified device.
  591. *
  592. * This function unpauses audio processing for a given device that has
  593. * previously been paused with SDL_PauseAudioDevice(). Once unpaused, any
  594. * bound audio streams will begin to progress again, and audio can be
  595. * generated.
  596. *
  597. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  598. * has to bind a stream before any audio will flow. Unpausing an unpaused
  599. * device is a legal no-op.
  600. *
  601. * Physical devices can not be paused or unpaused, only logical devices
  602. * created through SDL_OpenAudioDevice() can be.
  603. *
  604. * \param dev a device opened by SDL_OpenAudioDevice()
  605. * \returns 0 on success or a negative error code on failure; call
  606. * SDL_GetError() for more information.
  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_AudioDevicePaused
  613. * \sa SDL_PauseAudioDevice
  614. */
  615. extern SDL_DECLSPEC int SDLCALL SDL_ResumeAudioDevice(SDL_AudioDeviceID dev);
  616. /**
  617. * Use this function to query if an audio device is paused.
  618. *
  619. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  620. * has to bind a stream before any audio will flow.
  621. *
  622. * Physical devices can not be paused or unpaused, only logical devices
  623. * created through SDL_OpenAudioDevice() can be. Physical and invalid device
  624. * IDs will report themselves as unpaused here.
  625. *
  626. * \param dev a device opened by SDL_OpenAudioDevice()
  627. * \returns SDL_TRUE if device is valid and paused, SDL_FALSE otherwise.
  628. *
  629. * \threadsafety It is safe to call this function from any thread.
  630. *
  631. * \since This function is available since SDL 3.0.0.
  632. *
  633. * \sa SDL_PauseAudioDevice
  634. * \sa SDL_ResumeAudioDevice
  635. */
  636. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_AudioDevicePaused(SDL_AudioDeviceID dev);
  637. /**
  638. * Close a previously-opened audio device.
  639. *
  640. * The application should close open audio devices once they are no longer
  641. * needed.
  642. *
  643. * This function may block briefly while pending audio data is played by the
  644. * hardware, so that applications don't drop the last buffer of data they
  645. * supplied if terminating immediately afterwards.
  646. *
  647. * \param devid an audio device id previously returned by
  648. * SDL_OpenAudioDevice()
  649. *
  650. * \threadsafety It is safe to call this function from any thread.
  651. *
  652. * \since This function is available since SDL 3.0.0.
  653. *
  654. * \sa SDL_OpenAudioDevice
  655. */
  656. extern SDL_DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID devid);
  657. /**
  658. * Bind a list of audio streams to an audio device.
  659. *
  660. * Audio data will flow through any bound streams. For an output device, data
  661. * for all bound streams will be mixed together and fed to the device. For a
  662. * capture device, a copy of recorded data will be provided to each bound
  663. * stream.
  664. *
  665. * Audio streams can only be bound to an open device. This operation is
  666. * atomic--all streams bound in the same call will start processing at the
  667. * same time, so they can stay in sync. Also: either all streams will be bound
  668. * or none of them will be.
  669. *
  670. * It is an error to bind an already-bound stream; it must be explicitly
  671. * unbound first.
  672. *
  673. * Binding a stream to a device will set its output format for output devices,
  674. * and its input format for capture devices, so they match the device's
  675. * settings. The caller is welcome to change the other end of the stream's
  676. * format at any time.
  677. *
  678. * \param devid an audio device to bind a stream to.
  679. * \param streams an array of audio streams to unbind.
  680. * \param num_streams Number streams listed in the `streams` array.
  681. * \returns 0 on success, -1 on error; call SDL_GetError() for more
  682. * information.
  683. *
  684. * \threadsafety It is safe to call this function from any thread.
  685. *
  686. * \since This function is available since SDL 3.0.0.
  687. *
  688. * \sa SDL_BindAudioStreams
  689. * \sa SDL_UnbindAudioStream
  690. * \sa SDL_GetAudioStreamDevice
  691. */
  692. extern SDL_DECLSPEC int SDLCALL SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream **streams, int num_streams);
  693. /**
  694. * Bind a single audio stream to an audio device.
  695. *
  696. * This is a convenience function, equivalent to calling
  697. * `SDL_BindAudioStreams(devid, &stream, 1)`.
  698. *
  699. * \param devid an audio device to bind a stream to.
  700. * \param stream an audio stream to bind to a device.
  701. * \returns 0 on success, -1 on error; call SDL_GetError() for more
  702. * information.
  703. *
  704. * \threadsafety It is safe to call this function from any thread.
  705. *
  706. * \since This function is available since SDL 3.0.0.
  707. *
  708. * \sa SDL_BindAudioStreams
  709. * \sa SDL_UnbindAudioStream
  710. * \sa SDL_GetAudioStreamDevice
  711. */
  712. extern SDL_DECLSPEC int SDLCALL SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream);
  713. /**
  714. * Unbind a list of audio streams from their audio devices.
  715. *
  716. * The streams being unbound do not all have to be on the same device. All
  717. * streams on the same device will be unbound atomically (data will stop
  718. * flowing through all unbound streams on the same device at the same time).
  719. *
  720. * Unbinding a stream that isn't bound to a device is a legal no-op.
  721. *
  722. * \param streams an array of audio streams to unbind.
  723. * \param num_streams Number streams listed in the `streams` array.
  724. *
  725. * \threadsafety It is safe to call this function from any thread.
  726. *
  727. * \since This function is available since SDL 3.0.0.
  728. *
  729. * \sa SDL_BindAudioStreams
  730. */
  731. extern SDL_DECLSPEC void SDLCALL SDL_UnbindAudioStreams(SDL_AudioStream **streams, int num_streams);
  732. /**
  733. * Unbind a single audio stream from its audio device.
  734. *
  735. * This is a convenience function, equivalent to calling
  736. * `SDL_UnbindAudioStreams(&stream, 1)`.
  737. *
  738. * \param stream an audio stream to unbind from a device.
  739. *
  740. * \threadsafety It is safe to call this function from any thread.
  741. *
  742. * \since This function is available since SDL 3.0.0.
  743. *
  744. * \sa SDL_BindAudioStream
  745. */
  746. extern SDL_DECLSPEC void SDLCALL SDL_UnbindAudioStream(SDL_AudioStream *stream);
  747. /**
  748. * Query an audio stream for its currently-bound device.
  749. *
  750. * This reports the audio device that an audio stream is currently bound to.
  751. *
  752. * If not bound, or invalid, this returns zero, which is not a valid device
  753. * ID.
  754. *
  755. * \param stream the audio stream to query.
  756. * \returns The bound audio device, or 0 if not bound or invalid.
  757. *
  758. * \threadsafety It is safe to call this function from any thread.
  759. *
  760. * \since This function is available since SDL 3.0.0.
  761. *
  762. * \sa SDL_BindAudioStream
  763. * \sa SDL_BindAudioStreams
  764. */
  765. extern SDL_DECLSPEC SDL_AudioDeviceID SDLCALL SDL_GetAudioStreamDevice(SDL_AudioStream *stream);
  766. /**
  767. * Create a new audio stream.
  768. *
  769. * \param src_spec The format details of the input audio
  770. * \param dst_spec The format details of the output audio
  771. * \returns a new audio stream on success, or NULL on failure.
  772. *
  773. * \threadsafety It is safe to call this function from any thread.
  774. *
  775. * \since This function is available since SDL 3.0.0.
  776. *
  777. * \sa SDL_PutAudioStreamData
  778. * \sa SDL_GetAudioStreamData
  779. * \sa SDL_GetAudioStreamAvailable
  780. * \sa SDL_FlushAudioStream
  781. * \sa SDL_ClearAudioStream
  782. * \sa SDL_ChangeAudioStreamOutput
  783. * \sa SDL_DestroyAudioStream
  784. */
  785. extern SDL_DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
  786. /**
  787. * Get the properties associated with an audio stream.
  788. *
  789. * \param stream the SDL_AudioStream to query
  790. * \returns a valid property ID on success or 0 on failure; call
  791. * SDL_GetError() for more information.
  792. *
  793. * \since This function is available since SDL 3.0.0.
  794. *
  795. * \sa SDL_GetProperty
  796. * \sa SDL_SetProperty
  797. */
  798. extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetAudioStreamProperties(SDL_AudioStream *stream);
  799. /**
  800. * Query the current format of an audio stream.
  801. *
  802. * \param stream the SDL_AudioStream to query.
  803. * \param src_spec Where to store the input audio format; ignored if NULL.
  804. * \param dst_spec Where to store the output audio format; ignored if NULL.
  805. * \returns 0 on success, or -1 on error.
  806. *
  807. * \threadsafety It is safe to call this function from any thread, as it holds
  808. * a stream-specific mutex while running.
  809. *
  810. * \since This function is available since SDL 3.0.0.
  811. */
  812. extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamFormat(SDL_AudioStream *stream,
  813. SDL_AudioSpec *src_spec,
  814. SDL_AudioSpec *dst_spec);
  815. /**
  816. * Change the input and output formats of an audio stream.
  817. *
  818. * Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData
  819. * will reflect the new format, and future calls to SDL_PutAudioStreamData
  820. * must provide data in the new input formats.
  821. *
  822. * Data that was previously queued in the stream will still be operated on in
  823. * the format that was current when it was added, which is to say you can put
  824. * the end of a sound file in one format to a stream, change formats for the
  825. * next sound file, and start putting that new data while the previous sound
  826. * file is still queued, and everything will still play back correctly.
  827. *
  828. * \param stream The stream the format is being changed
  829. * \param src_spec The new format of the audio input; if NULL, it is not
  830. * changed.
  831. * \param dst_spec The new format of the audio output; if NULL, it is not
  832. * changed.
  833. * \returns 0 on success, or -1 on error.
  834. *
  835. * \threadsafety It is safe to call this function from any thread, as it holds
  836. * a stream-specific mutex while running.
  837. *
  838. * \since This function is available since SDL 3.0.0.
  839. *
  840. * \sa SDL_GetAudioStreamFormat
  841. * \sa SDL_SetAudioStreamFrequencyRatio
  842. */
  843. extern SDL_DECLSPEC int SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream,
  844. const SDL_AudioSpec *src_spec,
  845. const SDL_AudioSpec *dst_spec);
  846. /**
  847. * Get the frequency ratio of an audio stream.
  848. *
  849. * \param stream the SDL_AudioStream to query.
  850. * \returns the frequency ratio of the stream, or 0.0 on error
  851. *
  852. * \threadsafety It is safe to call this function from any thread, as it holds
  853. * a stream-specific mutex while running.
  854. *
  855. * \since This function is available since SDL 3.0.0.
  856. *
  857. * \sa SDL_SetAudioStreamFrequencyRatio
  858. */
  859. extern SDL_DECLSPEC float SDLCALL SDL_GetAudioStreamFrequencyRatio(SDL_AudioStream *stream);
  860. /**
  861. * Change the frequency ratio of an audio stream.
  862. *
  863. * The frequency ratio is used to adjust the rate at which input data is
  864. * consumed. Changing this effectively modifies the speed and pitch of the
  865. * audio. A value greater than 1.0 will play the audio faster, and at a higher
  866. * pitch. A value less than 1.0 will play the audio slower, and at a lower
  867. * pitch.
  868. *
  869. * This is applied during SDL_GetAudioStreamData, and can be continuously
  870. * changed to create various effects.
  871. *
  872. * \param stream The stream the frequency ratio is being changed
  873. * \param ratio The frequency ratio. 1.0 is normal speed. Must be between 0.01
  874. * and 100.
  875. * \returns 0 on success, or -1 on error.
  876. *
  877. * \threadsafety It is safe to call this function from any thread, as it holds
  878. * a stream-specific mutex while running.
  879. *
  880. * \since This function is available since SDL 3.0.0.
  881. *
  882. * \sa SDL_GetAudioStreamFrequencyRatio
  883. * \sa SDL_SetAudioStreamFormat
  884. */
  885. extern SDL_DECLSPEC int SDLCALL SDL_SetAudioStreamFrequencyRatio(SDL_AudioStream *stream, float ratio);
  886. /**
  887. * Add data to the stream.
  888. *
  889. * This data must match the format/channels/samplerate specified in the latest
  890. * call to SDL_SetAudioStreamFormat, or the format specified when creating the
  891. * stream if it hasn't been changed.
  892. *
  893. * Note that this call simply copies the unconverted data for later. This is
  894. * different than SDL2, where data was converted during the Put call and the
  895. * Get call would just dequeue the previously-converted data.
  896. *
  897. * \param stream The stream the audio data is being added to
  898. * \param buf A pointer to the audio data to add
  899. * \param len The number of bytes to write to the stream
  900. * \returns 0 on success or a negative error code on failure; call
  901. * SDL_GetError() for more information.
  902. *
  903. * \threadsafety It is safe to call this function from any thread, but if the
  904. * stream has a callback set, the caller might need to manage
  905. * extra locking.
  906. *
  907. * \since This function is available since SDL 3.0.0.
  908. *
  909. * \sa SDL_ClearAudioStream
  910. * \sa SDL_FlushAudioStream
  911. * \sa SDL_GetAudioStreamData
  912. * \sa SDL_GetAudioStreamQueued
  913. */
  914. extern SDL_DECLSPEC int SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
  915. /**
  916. * Get converted/resampled data from the stream.
  917. *
  918. * The input/output data format/channels/samplerate is specified when creating
  919. * the stream, and can be changed after creation by calling
  920. * SDL_SetAudioStreamFormat.
  921. *
  922. * Note that any conversion and resampling necessary is done during this call,
  923. * and SDL_PutAudioStreamData simply queues unconverted data for later. This
  924. * is different than SDL2, where that work was done while inputting new data
  925. * to the stream and requesting the output just copied the converted data.
  926. *
  927. * \param stream The stream the audio is being requested from
  928. * \param buf A buffer to fill with audio data
  929. * \param len The maximum number of bytes to fill
  930. * \returns the number of bytes read from the stream, or -1 on error
  931. *
  932. * \threadsafety It is safe to call this function from any thread, but if the
  933. * stream has a callback set, the caller might need to manage
  934. * extra locking.
  935. *
  936. * \since This function is available since SDL 3.0.0.
  937. *
  938. * \sa SDL_ClearAudioStream
  939. * \sa SDL_GetAudioStreamAvailable
  940. * \sa SDL_PutAudioStreamData
  941. */
  942. extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
  943. /**
  944. * Get the number of converted/resampled bytes available.
  945. *
  946. * The stream may be buffering data behind the scenes until it has enough to
  947. * resample correctly, so this number might be lower than what you expect, or
  948. * even be zero. Add more data or flush the stream if you need the data now.
  949. *
  950. * If the stream has so much data that it would overflow an int, the return
  951. * value is clamped to a maximum value, but no queued data is lost; if there
  952. * are gigabytes of data queued, the app might need to read some of it with
  953. * SDL_GetAudioStreamData before this function's return value is no longer
  954. * clamped.
  955. *
  956. * \param stream The audio stream to query
  957. * \returns the number of converted/resampled bytes available.
  958. *
  959. * \threadsafety It is safe to call this function from any thread.
  960. *
  961. * \since This function is available since SDL 3.0.0.
  962. *
  963. * \sa SDL_GetAudioStreamData
  964. * \sa SDL_PutAudioStreamData
  965. */
  966. extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
  967. /**
  968. * Get the number of bytes currently queued.
  969. *
  970. * Note that audio streams can change their input format at any time, even if
  971. * there is still data queued in a different format, so the returned byte
  972. * count will not necessarily match the number of _sample frames_ available.
  973. * Users of this API should be aware of format changes they make when feeding
  974. * a stream and plan accordingly.
  975. *
  976. * Queued data is not converted until it is consumed by
  977. * SDL_GetAudioStreamData, so this value should be representative of the exact
  978. * data that was put into the stream.
  979. *
  980. * If the stream has so much data that it would overflow an int, the return
  981. * value is clamped to a maximum value, but no queued data is lost; if there
  982. * are gigabytes of data queued, the app might need to read some of it with
  983. * SDL_GetAudioStreamData before this function's return value is no longer
  984. * clamped.
  985. *
  986. * \param stream The audio stream to query
  987. * \returns the number of bytes queued.
  988. *
  989. * \threadsafety It is safe to call this function from any thread.
  990. *
  991. * \since This function is available since SDL 3.0.0.
  992. *
  993. * \sa SDL_PutAudioStreamData
  994. * \sa SDL_ClearAudioStream
  995. */
  996. extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamQueued(SDL_AudioStream *stream);
  997. /**
  998. * Tell the stream that you're done sending data, and anything being buffered
  999. * should be converted/resampled and made available immediately.
  1000. *
  1001. * It is legal to add more data to a stream after flushing, but there may be
  1002. * audio gaps in the output. Generally this is intended to signal the end of
  1003. * input, so the complete output becomes available.
  1004. *
  1005. * \param stream The audio stream to flush
  1006. * \returns 0 on success or a negative error code on failure; call
  1007. * SDL_GetError() for more information.
  1008. *
  1009. * \threadsafety It is safe to call this function from any thread.
  1010. *
  1011. * \since This function is available since SDL 3.0.0.
  1012. *
  1013. * \sa SDL_PutAudioStreamData
  1014. */
  1015. extern SDL_DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
  1016. /**
  1017. * Clear any pending data in the stream.
  1018. *
  1019. * This drops any queued data, so there will be nothing to read from the
  1020. * stream until more is added.
  1021. *
  1022. * \param stream The audio stream to clear
  1023. * \returns 0 on success or a negative error code on failure; call
  1024. * SDL_GetError() for more information.
  1025. *
  1026. * \threadsafety It is safe to call this function from any thread.
  1027. *
  1028. * \since This function is available since SDL 3.0.0.
  1029. *
  1030. * \sa SDL_GetAudioStreamAvailable
  1031. * \sa SDL_GetAudioStreamData
  1032. * \sa SDL_GetAudioStreamQueued
  1033. * \sa SDL_PutAudioStreamData
  1034. */
  1035. extern SDL_DECLSPEC int SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
  1036. /**
  1037. * Use this function to pause audio playback on the audio device associated
  1038. * with an audio stream.
  1039. *
  1040. * This function pauses audio processing for a given device. Any bound audio
  1041. * streams will not progress, and no audio will be generated. Pausing one
  1042. * device does not prevent other unpaused devices from running.
  1043. *
  1044. * Pausing a device can be useful to halt all audio without unbinding all the
  1045. * audio streams. This might be useful while a game is paused, or a level is
  1046. * loading, etc.
  1047. *
  1048. * \param stream The audio stream associated with the audio device to pause
  1049. * \returns 0 on success or a negative error code on failure; call
  1050. * SDL_GetError() for more information.
  1051. *
  1052. * \threadsafety It is safe to call this function from any thread.
  1053. *
  1054. * \since This function is available since SDL 3.0.0.
  1055. *
  1056. * \sa SDL_ResumeAudioStreamDevice
  1057. */
  1058. extern SDL_DECLSPEC int SDLCALL SDL_PauseAudioStreamDevice(SDL_AudioStream *stream);
  1059. /**
  1060. * Use this function to unpause audio playback on the audio device associated
  1061. * with an audio stream.
  1062. *
  1063. * This function unpauses audio processing for a given device that has
  1064. * previously been paused. Once unpaused, any bound audio streams will begin
  1065. * to progress again, and audio can be generated.
  1066. *
  1067. * \param stream The audio stream associated with the audio device to resume
  1068. * \returns 0 on success or a negative error code on failure; call
  1069. * SDL_GetError() for more information.
  1070. *
  1071. * \threadsafety It is safe to call this function from any thread.
  1072. *
  1073. * \since This function is available since SDL 3.0.0.
  1074. *
  1075. * \sa SDL_PauseAudioStreamDevice
  1076. */
  1077. extern SDL_DECLSPEC int SDLCALL SDL_ResumeAudioStreamDevice(SDL_AudioStream *stream);
  1078. /**
  1079. * Lock an audio stream for serialized access.
  1080. *
  1081. * Each SDL_AudioStream has an internal mutex it uses to protect its data
  1082. * structures from threading conflicts. This function allows an app to lock
  1083. * that mutex, which could be useful if registering callbacks on this stream.
  1084. *
  1085. * One does not need to lock a stream to use in it most cases, as the stream
  1086. * manages this lock internally. However, this lock is held during callbacks,
  1087. * which may run from arbitrary threads at any time, so if an app needs to
  1088. * protect shared data during those callbacks, locking the stream guarantees
  1089. * that the callback is not running while the lock is held.
  1090. *
  1091. * As this is just a wrapper over SDL_LockMutex for an internal lock; it has
  1092. * all the same attributes (recursive locks are allowed, etc).
  1093. *
  1094. * \param stream The audio stream to lock.
  1095. * \returns 0 on success or a negative error code on failure; call
  1096. * SDL_GetError() for more information.
  1097. *
  1098. * \threadsafety It is safe to call this function from any thread.
  1099. *
  1100. * \since This function is available since SDL 3.0.0.
  1101. *
  1102. * \sa SDL_UnlockAudioStream
  1103. */
  1104. extern SDL_DECLSPEC int SDLCALL SDL_LockAudioStream(SDL_AudioStream *stream);
  1105. /**
  1106. * Unlock an audio stream for serialized access.
  1107. *
  1108. * This unlocks an audio stream after a call to SDL_LockAudioStream.
  1109. *
  1110. * \param stream The audio stream to unlock.
  1111. * \returns 0 on success or a negative error code on failure; call
  1112. * SDL_GetError() for more information.
  1113. *
  1114. * \threadsafety You should only call this from the same thread that
  1115. * previously called SDL_LockAudioStream.
  1116. *
  1117. * \since This function is available since SDL 3.0.0.
  1118. *
  1119. * \sa SDL_LockAudioStream
  1120. */
  1121. extern SDL_DECLSPEC int SDLCALL SDL_UnlockAudioStream(SDL_AudioStream *stream);
  1122. /**
  1123. * A callback that fires when data passes through an SDL_AudioStream.
  1124. *
  1125. * Apps can (optionally) register a callback with an audio stream that is
  1126. * called when data is added with SDL_PutAudioStreamData, or requested with
  1127. * SDL_GetAudioStreamData.
  1128. *
  1129. * Two values are offered here: one is the amount of additional data needed to
  1130. * satisfy the immediate request (which might be zero if the stream already
  1131. * has enough data queued) and the other is the total amount being requested.
  1132. * In a Get call triggering a Put callback, these values can be different. In
  1133. * a Put call triggering a Get callback, these values are always the same.
  1134. *
  1135. * Byte counts might be slightly overestimated due to buffering or resampling,
  1136. * and may change from call to call.
  1137. *
  1138. * This callback is not required to do anything. Generally this is useful for
  1139. * adding/reading data on demand, and the app will often put/get data as
  1140. * appropriate, but the system goes on with the data currently available to it
  1141. * if this callback does nothing.
  1142. *
  1143. * \param stream The SDL audio stream associated with this callback.
  1144. * \param additional_amount The amount of data, in bytes, that is needed right
  1145. * now.
  1146. * \param total_amount The total amount of data requested, in bytes, that is
  1147. * requested or available.
  1148. * \param userdata An opaque pointer provided by the app for their personal
  1149. * use.
  1150. *
  1151. * \threadsafety This callbacks may run from any thread, so if you need to
  1152. * protect shared data, you should use SDL_LockAudioStream to
  1153. * serialize access; this lock will be held before your callback
  1154. * is called, so your callback does not need to manage the lock
  1155. * explicitly.
  1156. *
  1157. * \since This datatype is available since SDL 3.0.0.
  1158. *
  1159. * \sa SDL_SetAudioStreamGetCallback
  1160. * \sa SDL_SetAudioStreamPutCallback
  1161. */
  1162. typedef void (SDLCALL *SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount);
  1163. /**
  1164. * Set a callback that runs when data is requested from an audio stream.
  1165. *
  1166. * This callback is called _before_ data is obtained from the stream, giving
  1167. * the callback the chance to add more on-demand.
  1168. *
  1169. * The callback can (optionally) call SDL_PutAudioStreamData() to add more
  1170. * audio to the stream during this call; if needed, the request that triggered
  1171. * this callback will obtain the new data immediately.
  1172. *
  1173. * The callback's `approx_request` argument is roughly how many bytes of
  1174. * _unconverted_ data (in the stream's input format) is needed by the caller,
  1175. * although this may overestimate a little for safety. This takes into account
  1176. * how much is already in the stream and only asks for any extra necessary to
  1177. * resolve the request, which means the callback may be asked for zero bytes,
  1178. * and a different amount on each call.
  1179. *
  1180. * The callback is not required to supply exact amounts; it is allowed to
  1181. * supply too much or too little or none at all. The caller will get what's
  1182. * available, up to the amount they requested, regardless of this callback's
  1183. * outcome.
  1184. *
  1185. * Clearing or flushing an audio stream does not call this callback.
  1186. *
  1187. * This function obtains the stream's lock, which means any existing callback
  1188. * (get or put) in progress will finish running before setting the new
  1189. * callback.
  1190. *
  1191. * Setting a NULL function turns off the callback.
  1192. *
  1193. * \param stream the audio stream to set the new callback on.
  1194. * \param callback the new callback function to call when data is added to the
  1195. * stream.
  1196. * \param userdata an opaque pointer provided to the callback for its own
  1197. * personal use.
  1198. * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
  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. * \sa SDL_SetAudioStreamPutCallback
  1205. */
  1206. extern SDL_DECLSPEC int SDLCALL SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
  1207. /**
  1208. * Set a callback that runs when data is added to an audio stream.
  1209. *
  1210. * This callback is called _after_ the data is added to the stream, giving the
  1211. * callback the chance to obtain it immediately.
  1212. *
  1213. * The callback can (optionally) call SDL_GetAudioStreamData() to obtain audio
  1214. * from the stream during this call.
  1215. *
  1216. * The callback's `approx_request` argument is how many bytes of _converted_
  1217. * data (in the stream's output format) was provided by the caller, although
  1218. * this may underestimate a little for safety. This value might be less than
  1219. * what is currently available in the stream, if data was already there, and
  1220. * might be less than the caller provided if the stream needs to keep a buffer
  1221. * to aid in resampling. Which means the callback may be provided with zero
  1222. * bytes, and a different amount on each call.
  1223. *
  1224. * The callback may call SDL_GetAudioStreamAvailable to see the total amount
  1225. * currently available to read from the stream, instead of the total provided
  1226. * by the current call.
  1227. *
  1228. * The callback is not required to obtain all data. It is allowed to read less
  1229. * or none at all. Anything not read now simply remains in the stream for
  1230. * later access.
  1231. *
  1232. * Clearing or flushing an audio stream does not call this callback.
  1233. *
  1234. * This function obtains the stream's lock, which means any existing callback
  1235. * (get or put) in progress will finish running before setting the new
  1236. * callback.
  1237. *
  1238. * Setting a NULL function turns off the callback.
  1239. *
  1240. * \param stream the audio stream to set the new callback on.
  1241. * \param callback the new callback function to call when data is added to the
  1242. * stream.
  1243. * \param userdata an opaque pointer provided to the callback for its own
  1244. * personal use.
  1245. * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
  1246. *
  1247. * \threadsafety It is safe to call this function from any thread.
  1248. *
  1249. * \since This function is available since SDL 3.0.0.
  1250. *
  1251. * \sa SDL_SetAudioStreamGetCallback
  1252. */
  1253. extern SDL_DECLSPEC int SDLCALL SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
  1254. /**
  1255. * Free an audio stream.
  1256. *
  1257. * This will release all allocated data, including any audio that is still
  1258. * queued. You do not need to manually clear the stream first.
  1259. *
  1260. * If this stream was bound to an audio device, it is unbound during this
  1261. * call. If this stream was created with SDL_OpenAudioDeviceStream, the audio
  1262. * device that was opened alongside this stream's creation will be closed,
  1263. * too.
  1264. *
  1265. * \param stream The audio stream to destroy.
  1266. *
  1267. * \threadsafety It is safe to call this function from any thread.
  1268. *
  1269. * \since This function is available since SDL 3.0.0.
  1270. *
  1271. * \sa SDL_CreateAudioStream
  1272. */
  1273. extern SDL_DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
  1274. /**
  1275. * Convenience function for straightforward audio init for the common case.
  1276. *
  1277. * If all your app intends to do is provide a single source of PCM audio, this
  1278. * function allows you to do all your audio setup in a single call.
  1279. *
  1280. * This is also intended to be a clean means to migrate apps from SDL2.
  1281. *
  1282. * This function will open an audio device, create a stream and bind it.
  1283. * Unlike other methods of setup, the audio device will be closed when this
  1284. * stream is destroyed, so the app can treat the returned SDL_AudioStream as
  1285. * the only object needed to manage audio playback.
  1286. *
  1287. * Also unlike other functions, the audio device begins paused. This is to map
  1288. * more closely to SDL2-style behavior, since there is no extra step here to
  1289. * bind a stream to begin audio flowing. The audio device should be resumed
  1290. * with `SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream));`
  1291. *
  1292. * This function works with both playback and capture devices.
  1293. *
  1294. * The `spec` parameter represents the app's side of the audio stream. That
  1295. * is, for recording audio, this will be the output format, and for playing
  1296. * audio, this will be the input format. If spec is NULL, the system will
  1297. * choose the format, and the app can use SDL_GetAudioStreamFormat() to obtain
  1298. * this information later.
  1299. *
  1300. * If you don't care about opening a specific audio device, you can (and
  1301. * probably _should_), use SDL_AUDIO_DEVICE_DEFAULT_OUTPUT for playback and
  1302. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE for recording.
  1303. *
  1304. * One can optionally provide a callback function; if NULL, the app is
  1305. * expected to queue audio data for playback (or unqueue audio data if
  1306. * capturing). Otherwise, the callback will begin to fire once the device is
  1307. * unpaused.
  1308. *
  1309. * Destroying the returned stream with SDL_DestroyAudioStream will also close
  1310. * the audio device associated with this stream.
  1311. *
  1312. * \param devid an audio device to open, or SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
  1313. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE.
  1314. * \param spec the audio stream's data format. Can be NULL.
  1315. * \param callback A callback where the app will provide new data for
  1316. * playback, or receive new data for capture. Can be NULL, in
  1317. * which case the app will need to call SDL_PutAudioStreamData
  1318. * or SDL_GetAudioStreamData as necessary.
  1319. * \param userdata App-controlled pointer passed to callback. Can be NULL.
  1320. * Ignored if callback is NULL.
  1321. * \returns an audio stream on success, ready to use. NULL on error; call
  1322. * SDL_GetError() for more information. When done with this stream,
  1323. * call SDL_DestroyAudioStream to free resources and close the
  1324. * device.
  1325. *
  1326. * \threadsafety It is safe to call this function from any thread.
  1327. *
  1328. * \since This function is available since SDL 3.0.0.
  1329. *
  1330. * \sa SDL_GetAudioStreamDevice
  1331. * \sa SDL_ResumeAudioDevice
  1332. */
  1333. extern SDL_DECLSPEC SDL_AudioStream *SDLCALL SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata);
  1334. /**
  1335. * A callback that fires when data is about to be fed to an audio device.
  1336. *
  1337. * This is useful for accessing the final mix, perhaps for writing a
  1338. * visualizer or applying a final effect to the audio data before playback.
  1339. *
  1340. * This callback should run as quickly as possible and not block for any
  1341. * significant time, as this callback delays submission of data to the audio
  1342. * device, which can cause audio playback problems.
  1343. *
  1344. * The postmix callback _must_ be able to handle any audio data format
  1345. * specified in `spec`, which can change between callbacks if the audio device
  1346. * changed. However, this only covers frequency and channel count; data is
  1347. * always provided here in SDL_AUDIO_F32 format.
  1348. *
  1349. * \param userdata a pointer provided by the app through
  1350. * SDL_SetAudioPostmixCallback, for its own use.
  1351. * \param spec the current format of audio that is to be submitted to the
  1352. * audio device.
  1353. * \param buffer the buffer of audio samples to be submitted. The callback can
  1354. * inspect and/or modify this data.
  1355. * \param buflen the size of `buffer` in bytes.
  1356. *
  1357. * \threadsafety This will run from a background thread owned by SDL. The
  1358. * application is responsible for locking resources the callback
  1359. * touches that need to be protected.
  1360. *
  1361. * \since This datatype is available since SDL 3.0.0.
  1362. *
  1363. * \sa SDL_SetAudioPostmixCallback
  1364. */
  1365. typedef void (SDLCALL *SDL_AudioPostmixCallback)(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen);
  1366. /**
  1367. * Set a callback that fires when data is about to be fed to an audio device.
  1368. *
  1369. * This is useful for accessing the final mix, perhaps for writing a
  1370. * visualizer or applying a final effect to the audio data before playback.
  1371. *
  1372. * The buffer is the final mix of all bound audio streams on an opened device;
  1373. * this callback will fire regularly for any device that is both opened and
  1374. * unpaused. If there is no new data to mix, either because no streams are
  1375. * bound to the device or all the streams are empty, this callback will still
  1376. * fire with the entire buffer set to silence.
  1377. *
  1378. * This callback is allowed to make changes to the data; the contents of the
  1379. * buffer after this call is what is ultimately passed along to the hardware.
  1380. *
  1381. * The callback is always provided the data in float format (values from -1.0f
  1382. * to 1.0f), but the number of channels or sample rate may be different than
  1383. * the format the app requested when opening the device; SDL might have had to
  1384. * manage a conversion behind the scenes, or the playback might have jumped to
  1385. * new physical hardware when a system default changed, etc. These details may
  1386. * change between calls. Accordingly, the size of the buffer might change
  1387. * between calls as well.
  1388. *
  1389. * This callback can run at any time, and from any thread; if you need to
  1390. * serialize access to your app's data, you should provide and use a mutex or
  1391. * other synchronization device.
  1392. *
  1393. * All of this to say: there are specific needs this callback can fulfill, but
  1394. * it is not the simplest interface. Apps should generally provide audio in
  1395. * their preferred format through an SDL_AudioStream and let SDL handle the
  1396. * difference.
  1397. *
  1398. * This function is extremely time-sensitive; the callback should do the least
  1399. * amount of work possible and return as quickly as it can. The longer the
  1400. * callback runs, the higher the risk of audio dropouts or other problems.
  1401. *
  1402. * This function will block until the audio device is in between iterations,
  1403. * so any existing callback that might be running will finish before this
  1404. * function sets the new callback and returns.
  1405. *
  1406. * Setting a NULL callback function disables any previously-set callback.
  1407. *
  1408. * \param devid The ID of an opened audio device.
  1409. * \param callback A callback function to be called. Can be NULL.
  1410. * \param userdata App-controlled pointer passed to callback. Can be NULL.
  1411. * \returns zero on success, -1 on error; call SDL_GetError() for more
  1412. * information.
  1413. *
  1414. * \threadsafety It is safe to call this function from any thread.
  1415. *
  1416. * \since This function is available since SDL 3.0.0.
  1417. */
  1418. extern SDL_DECLSPEC int SDLCALL SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata);
  1419. /**
  1420. * Load the audio data of a WAVE file into memory.
  1421. *
  1422. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  1423. * be valid pointers. The entire data portion of the file is then loaded into
  1424. * memory and decoded if necessary.
  1425. *
  1426. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  1427. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  1428. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  1429. * cause an error.
  1430. *
  1431. * If this function succeeds, the return value is zero and the pointer to the
  1432. * audio data allocated by the function is written to `audio_buf` and its
  1433. * length in bytes to `audio_len`. The SDL_AudioSpec members `freq`,
  1434. * `channels`, and `format` are set to the values of the audio data in the
  1435. * buffer.
  1436. *
  1437. * It's necessary to use SDL_free() to free the audio data returned in
  1438. * `audio_buf` when it is no longer used.
  1439. *
  1440. * Because of the underspecification of the .WAV format, there are many
  1441. * problematic files in the wild that cause issues with strict decoders. To
  1442. * provide compatibility with these files, this decoder is lenient in regards
  1443. * to the truncation of the file, the fact chunk, and the size of the RIFF
  1444. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  1445. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  1446. * tune the behavior of the loading process.
  1447. *
  1448. * Any file that is invalid (due to truncation, corruption, or wrong values in
  1449. * the headers), too big, or unsupported causes an error. Additionally, any
  1450. * critical I/O error from the data source will terminate the loading process
  1451. * with an error. The function returns NULL on error and in all cases (with
  1452. * the exception of `src` being NULL), an appropriate error message will be
  1453. * set.
  1454. *
  1455. * It is required that the data source supports seeking.
  1456. *
  1457. * Example:
  1458. *
  1459. * ```c
  1460. * SDL_LoadWAV_IO(SDL_IOFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
  1461. * ```
  1462. *
  1463. * Note that the SDL_LoadWAV function does this same thing for you, but in a
  1464. * less messy way:
  1465. *
  1466. * ```c
  1467. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  1468. * ```
  1469. *
  1470. * \param src The data source for the WAVE data
  1471. * \param closeio If SDL_TRUE, calls SDL_CloseIO() on `src` before returning,
  1472. * even in the case of an error
  1473. * \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE
  1474. * data's format details on successful return
  1475. * \param audio_buf A pointer filled with the audio data, allocated by the
  1476. * function
  1477. * \param audio_len A pointer filled with the length of the audio data buffer
  1478. * in bytes
  1479. * \returns 0 on success. `audio_buf` will be filled with a pointer to an
  1480. * allocated buffer containing the audio data, and `audio_len` is
  1481. * filled with the length of that audio buffer in bytes.
  1482. *
  1483. * This function returns -1 if the .WAV file cannot be opened, uses
  1484. * an unknown data format, or is corrupt; call SDL_GetError() for
  1485. * more information.
  1486. *
  1487. * When the application is done with the data returned in
  1488. * `audio_buf`, it should call SDL_free() to dispose of it.
  1489. *
  1490. * \threadsafety It is safe to call this function from any thread.
  1491. *
  1492. * \since This function is available since SDL 3.0.0.
  1493. *
  1494. * \sa SDL_free
  1495. * \sa SDL_LoadWAV
  1496. */
  1497. extern SDL_DECLSPEC int SDLCALL SDL_LoadWAV_IO(SDL_IOStream * src, SDL_bool closeio,
  1498. SDL_AudioSpec * spec, Uint8 ** audio_buf,
  1499. Uint32 * audio_len);
  1500. /**
  1501. * Loads a WAV from a file path.
  1502. *
  1503. * This is a convenience function that is effectively the same as:
  1504. *
  1505. * ```c
  1506. * SDL_LoadWAV_IO(SDL_IOFromFile(path, "rb"), 1, spec, audio_buf, audio_len);
  1507. * ```
  1508. *
  1509. * \param path The file path of the WAV file to open.
  1510. * \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE
  1511. * data's format details on successful return.
  1512. * \param audio_buf A pointer filled with the audio data, allocated by the
  1513. * function.
  1514. * \param audio_len A pointer filled with the length of the audio data buffer
  1515. * in bytes
  1516. * \returns 0 on success. `audio_buf` will be filled with a pointer to an
  1517. * allocated buffer containing the audio data, and `audio_len` is
  1518. * filled with the length of that audio buffer in bytes.
  1519. *
  1520. * This function returns -1 if the .WAV file cannot be opened, uses
  1521. * an unknown data format, or is corrupt; call SDL_GetError() for
  1522. * more information.
  1523. *
  1524. * When the application is done with the data returned in
  1525. * `audio_buf`, it should call SDL_free() to dispose of it.
  1526. *
  1527. * \threadsafety It is safe to call this function from any thread.
  1528. *
  1529. * \since This function is available since SDL 3.0.0.
  1530. *
  1531. * \sa SDL_free
  1532. * \sa SDL_LoadWAV_IO
  1533. */
  1534. extern SDL_DECLSPEC int SDLCALL SDL_LoadWAV(const char *path, SDL_AudioSpec * spec,
  1535. Uint8 ** audio_buf, Uint32 * audio_len);
  1536. /**
  1537. * Mix audio data in a specified format.
  1538. *
  1539. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  1540. * it into `dst`, performing addition, volume adjustment, and overflow
  1541. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  1542. * `format` data.
  1543. *
  1544. * This is provided for convenience -- you can mix your own audio data.
  1545. *
  1546. * Do not use this function for mixing together more than two streams of
  1547. * sample data. The output from repeated application of this function may be
  1548. * distorted by clipping, because there is no accumulator with greater range
  1549. * than the input (not to mention this being an inefficient way of doing it).
  1550. *
  1551. * It is a common misconception that this function is required to write audio
  1552. * data to an output stream in an audio callback. While you can do that,
  1553. * SDL_MixAudio() is really only needed when you're mixing a single audio
  1554. * stream with a volume adjustment.
  1555. *
  1556. * \param dst the destination for the mixed audio
  1557. * \param src the source audio buffer to be mixed
  1558. * \param format the SDL_AudioFormat structure representing the desired audio
  1559. * format
  1560. * \param len the length of the audio buffer in bytes
  1561. * \param volume ranges from 0.0 - 1.0, and should be set to 1.0 for full
  1562. * audio volume
  1563. * \returns 0 on success or a negative error code on failure; call
  1564. * SDL_GetError() for more information.
  1565. *
  1566. * \threadsafety It is safe to call this function from any thread.
  1567. *
  1568. * \since This function is available since SDL 3.0.0.
  1569. */
  1570. extern SDL_DECLSPEC int SDLCALL SDL_MixAudio(Uint8 * dst,
  1571. const Uint8 * src,
  1572. SDL_AudioFormat format,
  1573. Uint32 len, float volume);
  1574. /**
  1575. * Convert some audio data of one format to another format.
  1576. *
  1577. * Please note that this function is for convenience, but should not be used
  1578. * to resample audio in blocks, as it will introduce audio artifacts on the
  1579. * boundaries. You should only use this function if you are converting audio
  1580. * data in its entirety in one call. If you want to convert audio in smaller
  1581. * chunks, use an SDL_AudioStream, which is designed for this situation.
  1582. *
  1583. * Internally, this function creates and destroys an SDL_AudioStream on each
  1584. * use, so it's also less efficient than using one directly, if you need to
  1585. * convert multiple times.
  1586. *
  1587. * \param src_spec The format details of the input audio
  1588. * \param src_data The audio data to be converted
  1589. * \param src_len The len of src_data
  1590. * \param dst_spec The format details of the output audio
  1591. * \param dst_data Will be filled with a pointer to converted audio data,
  1592. * which should be freed with SDL_free(). On error, it will be
  1593. * NULL.
  1594. * \param dst_len Will be filled with the len of dst_data
  1595. * \returns 0 on success or a negative error code on failure; call
  1596. * SDL_GetError() for more information.
  1597. *
  1598. * \threadsafety It is safe to call this function from any thread.
  1599. *
  1600. * \since This function is available since SDL 3.0.0.
  1601. */
  1602. extern SDL_DECLSPEC int SDLCALL SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec,
  1603. const Uint8 *src_data,
  1604. int src_len,
  1605. const SDL_AudioSpec *dst_spec,
  1606. Uint8 **dst_data,
  1607. int *dst_len);
  1608. /**
  1609. * Get the appropriate memset value for silencing an audio format.
  1610. *
  1611. * The value returned by this function can be used as the second argument to
  1612. * memset (or SDL_memset) to set an audio buffer in a specific format to
  1613. * silence.
  1614. *
  1615. * \param format the audio data format to query.
  1616. * \returns A byte value that can be passed to memset.
  1617. *
  1618. * \threadsafety It is safe to call this function from any thread.
  1619. *
  1620. * \since This function is available since SDL 3.0.0.
  1621. */
  1622. extern SDL_DECLSPEC int SDLCALL SDL_GetSilenceValueForFormat(SDL_AudioFormat format);
  1623. /* Ends C function definitions when using C++ */
  1624. #ifdef __cplusplus
  1625. }
  1626. #endif
  1627. #include <SDL3/SDL_close_code.h>
  1628. #endif /* SDL_audio_h_ */