SDL_audio.h 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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. * # 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 the streams as available. When the device needs more
  33. * data, it will pull it from all bound streams and mix them together for
  34. * playback.
  35. *
  36. * Audio streams can also use an app-provided callback to supply data
  37. * on-demand, which maps pretty closely to the SDL2 audio model.
  38. *
  39. * SDL also provides a simple .WAV loader in SDL_LoadWAV (and SDL_LoadWAV_IO
  40. * if you aren't reading from a file) as a basic means to load sound data into
  41. * your program.
  42. *
  43. * ## Logical audio devices
  44. *
  45. * In SDL3, opening a physical device (like a SoundBlaster 16 Pro) gives you a
  46. * logical device ID that you can bind audio streams to. In almost all cases,
  47. * logical devices can be used anywhere in the API that a physical device is
  48. * normally used. However, since each device opening generates a new logical
  49. * device, different parts of the program (say, a VoIP library, or
  50. * text-to-speech framework, or maybe some other sort of mixer on top of SDL)
  51. * can have their own device opens that do not interfere with each other; each
  52. * logical device will mix its separate audio down to a single buffer, fed to
  53. * the physical device, behind the scenes. As many logical devices as you like
  54. * can come and go; SDL will only have to open the physical device at the OS
  55. * level once, and will manage all the logical devices on top of it
  56. * internally.
  57. *
  58. * One other benefit of logical devices: if you don't open a specific physical
  59. * device, instead opting for the default, SDL can automatically migrate those
  60. * logical devices to different hardware as circumstances change: a user
  61. * plugged in headphones? The system default changed? SDL can transparently
  62. * migrate the logical devices to the correct physical device seamlessly and
  63. * keep playing; the app doesn't even have to know it happened if it doesn't
  64. * want to.
  65. *
  66. * ## Simplified audio
  67. *
  68. * As a simplified model for when a single source of audio is all that's
  69. * needed, an app can use SDL_OpenAudioDeviceStream, which is a single
  70. * function to open an audio device, create an audio stream, bind that stream
  71. * to the newly-opened device, and (optionally) provide a callback for
  72. * obtaining audio data. When using this function, the primary interface is
  73. * the SDL_AudioStream and the device handle is mostly hidden away; destroying
  74. * a stream created through this function will also close the device, stream
  75. * bindings cannot be changed, etc. One other quirk of this is that the device
  76. * is started in a _paused_ state and must be explicitly resumed; this is
  77. * partially to offer a clean migration for SDL2 apps and partially because
  78. * the app might have to do more setup before playback begins; in the
  79. * non-simplified form, nothing will play until a stream is bound to a device,
  80. * so they start _unpaused_.
  81. *
  82. * ## Channel layouts
  83. *
  84. * Audio data passing through SDL is uncompressed PCM data, interleaved. One
  85. * can provide their own decompression through an MP3, etc, decoder, but SDL
  86. * does not provide this directly. Each interleaved channel of data is meant
  87. * to be in a specific order.
  88. *
  89. * Abbreviations:
  90. *
  91. * - FRONT = single mono speaker
  92. * - FL = front left speaker
  93. * - FR = front right speaker
  94. * - FC = front center speaker
  95. * - BL = back left speaker
  96. * - BR = back right speaker
  97. * - SR = surround right speaker
  98. * - SL = surround left speaker
  99. * - BC = back center speaker
  100. * - LFE = low-frequency speaker
  101. *
  102. * These are listed in the order they are laid out in memory, so "FL, FR"
  103. * means "the front left speaker is laid out in memory first, then the front
  104. * right, then it repeats for the next audio frame".
  105. *
  106. * - 1 channel (mono) layout: FRONT
  107. * - 2 channels (stereo) layout: FL, FR
  108. * - 3 channels (2.1) layout: FL, FR, LFE
  109. * - 4 channels (quad) layout: FL, FR, BL, BR
  110. * - 5 channels (4.1) layout: FL, FR, LFE, BL, BR
  111. * - 6 channels (5.1) layout: FL, FR, FC, LFE, BL, BR (last two can also be
  112. * SL, SR)
  113. * - 7 channels (6.1) layout: FL, FR, FC, LFE, BC, SL, SR
  114. * - 8 channels (7.1) layout: FL, FR, FC, LFE, BL, BR, SL, SR
  115. *
  116. * This is the same order as DirectSound expects, but applied to all
  117. * platforms; SDL will swizzle the channels as necessary if a platform expects
  118. * something different.
  119. *
  120. * SDL_AudioStream can also be provided channel maps to change this ordering
  121. * to whatever is necessary, in other audio processing scenarios.
  122. */
  123. #ifndef SDL_audio_h_
  124. #define SDL_audio_h_
  125. #include <SDL3/SDL_stdinc.h>
  126. #include <SDL3/SDL_endian.h>
  127. #include <SDL3/SDL_error.h>
  128. #include <SDL3/SDL_mutex.h>
  129. #include <SDL3/SDL_properties.h>
  130. #include <SDL3/SDL_iostream.h>
  131. #include <SDL3/SDL_begin_code.h>
  132. /* Set up for C function definitions, even when using C++ */
  133. #ifdef __cplusplus
  134. extern "C" {
  135. #endif
  136. /**
  137. * Mask of bits in an SDL_AudioFormat that contains the format bit size.
  138. *
  139. * Generally one should use SDL_AUDIO_BITSIZE instead of this macro directly.
  140. *
  141. * \since This macro is available since SDL 3.2.0.
  142. */
  143. #define SDL_AUDIO_MASK_BITSIZE (0xFFu)
  144. /**
  145. * Mask of bits in an SDL_AudioFormat that contain the floating point flag.
  146. *
  147. * Generally one should use SDL_AUDIO_ISFLOAT instead of this macro directly.
  148. *
  149. * \since This macro is available since SDL 3.2.0.
  150. */
  151. #define SDL_AUDIO_MASK_FLOAT (1u<<8)
  152. /**
  153. * Mask of bits in an SDL_AudioFormat that contain the bigendian flag.
  154. *
  155. * Generally one should use SDL_AUDIO_ISBIGENDIAN or SDL_AUDIO_ISLITTLEENDIAN
  156. * instead of this macro directly.
  157. *
  158. * \since This macro is available since SDL 3.2.0.
  159. */
  160. #define SDL_AUDIO_MASK_BIG_ENDIAN (1u<<12)
  161. /**
  162. * Mask of bits in an SDL_AudioFormat that contain the signed data flag.
  163. *
  164. * Generally one should use SDL_AUDIO_ISSIGNED instead of this macro directly.
  165. *
  166. * \since This macro is available since SDL 3.2.0.
  167. */
  168. #define SDL_AUDIO_MASK_SIGNED (1u<<15)
  169. /**
  170. * Define an SDL_AudioFormat value.
  171. *
  172. * SDL does not support custom audio formats, so this macro is not of much use
  173. * externally, but it can be illustrative as to what the various bits of an
  174. * SDL_AudioFormat mean.
  175. *
  176. * For example, SDL_AUDIO_S32LE looks like this:
  177. *
  178. * ```c
  179. * SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 32)
  180. * ```
  181. *
  182. * \param signed 1 for signed data, 0 for unsigned data.
  183. * \param bigendian 1 for bigendian data, 0 for littleendian data.
  184. * \param flt 1 for floating point data, 0 for integer data.
  185. * \param size number of bits per sample.
  186. * \returns a format value in the style of SDL_AudioFormat.
  187. *
  188. * \threadsafety It is safe to call this macro from any thread.
  189. *
  190. * \since This macro is available since SDL 3.2.0.
  191. */
  192. #define SDL_DEFINE_AUDIO_FORMAT(signed, bigendian, flt, size) \
  193. (((Uint16)(signed) << 15) | ((Uint16)(bigendian) << 12) | ((Uint16)(flt) << 8) | ((size) & SDL_AUDIO_MASK_BITSIZE))
  194. /**
  195. * Audio format.
  196. *
  197. * \since This enum is available since SDL 3.2.0.
  198. *
  199. * \sa SDL_AUDIO_BITSIZE
  200. * \sa SDL_AUDIO_BYTESIZE
  201. * \sa SDL_AUDIO_ISINT
  202. * \sa SDL_AUDIO_ISFLOAT
  203. * \sa SDL_AUDIO_ISBIGENDIAN
  204. * \sa SDL_AUDIO_ISLITTLEENDIAN
  205. * \sa SDL_AUDIO_ISSIGNED
  206. * \sa SDL_AUDIO_ISUNSIGNED
  207. */
  208. typedef enum SDL_AudioFormat
  209. {
  210. SDL_AUDIO_UNKNOWN = 0x0000u, /**< Unspecified audio format */
  211. SDL_AUDIO_U8 = 0x0008u, /**< Unsigned 8-bit samples */
  212. /* SDL_DEFINE_AUDIO_FORMAT(0, 0, 0, 8), */
  213. SDL_AUDIO_S8 = 0x8008u, /**< Signed 8-bit samples */
  214. /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 8), */
  215. SDL_AUDIO_S16LE = 0x8010u, /**< Signed 16-bit samples */
  216. /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 16), */
  217. SDL_AUDIO_S16BE = 0x9010u, /**< As above, but big-endian byte order */
  218. /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 16), */
  219. SDL_AUDIO_S32LE = 0x8020u, /**< 32-bit integer samples */
  220. /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 0, 32), */
  221. SDL_AUDIO_S32BE = 0x9020u, /**< As above, but big-endian byte order */
  222. /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 0, 32), */
  223. SDL_AUDIO_F32LE = 0x8120u, /**< 32-bit floating point samples */
  224. /* SDL_DEFINE_AUDIO_FORMAT(1, 0, 1, 32), */
  225. SDL_AUDIO_F32BE = 0x9120u, /**< As above, but big-endian byte order */
  226. /* SDL_DEFINE_AUDIO_FORMAT(1, 1, 1, 32), */
  227. /* These represent the current system's byteorder. */
  228. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  229. SDL_AUDIO_S16 = SDL_AUDIO_S16LE,
  230. SDL_AUDIO_S32 = SDL_AUDIO_S32LE,
  231. SDL_AUDIO_F32 = SDL_AUDIO_F32LE
  232. #else
  233. SDL_AUDIO_S16 = SDL_AUDIO_S16BE,
  234. SDL_AUDIO_S32 = SDL_AUDIO_S32BE,
  235. SDL_AUDIO_F32 = SDL_AUDIO_F32BE
  236. #endif
  237. } SDL_AudioFormat;
  238. /**
  239. * Retrieve the size, in bits, from an SDL_AudioFormat.
  240. *
  241. * For example, `SDL_AUDIO_BITSIZE(SDL_AUDIO_S16)` returns 16.
  242. *
  243. * \param x an SDL_AudioFormat value.
  244. * \returns data size in bits.
  245. *
  246. * \threadsafety It is safe to call this macro from any thread.
  247. *
  248. * \since This macro is available since SDL 3.2.0.
  249. */
  250. #define SDL_AUDIO_BITSIZE(x) ((x) & SDL_AUDIO_MASK_BITSIZE)
  251. /**
  252. * Retrieve the size, in bytes, from an SDL_AudioFormat.
  253. *
  254. * For example, `SDL_AUDIO_BYTESIZE(SDL_AUDIO_S16)` returns 2.
  255. *
  256. * \param x an SDL_AudioFormat value.
  257. * \returns data size in bytes.
  258. *
  259. * \threadsafety It is safe to call this macro from any thread.
  260. *
  261. * \since This macro is available since SDL 3.2.0.
  262. */
  263. #define SDL_AUDIO_BYTESIZE(x) (SDL_AUDIO_BITSIZE(x) / 8)
  264. /**
  265. * Determine if an SDL_AudioFormat represents floating point data.
  266. *
  267. * For example, `SDL_AUDIO_ISFLOAT(SDL_AUDIO_S16)` returns 0.
  268. *
  269. * \param x an SDL_AudioFormat value.
  270. * \returns non-zero if format is floating point, zero otherwise.
  271. *
  272. * \threadsafety It is safe to call this macro from any thread.
  273. *
  274. * \since This macro is available since SDL 3.2.0.
  275. */
  276. #define SDL_AUDIO_ISFLOAT(x) ((x) & SDL_AUDIO_MASK_FLOAT)
  277. /**
  278. * Determine if an SDL_AudioFormat represents bigendian data.
  279. *
  280. * For example, `SDL_AUDIO_ISBIGENDIAN(SDL_AUDIO_S16LE)` returns 0.
  281. *
  282. * \param x an SDL_AudioFormat value.
  283. * \returns non-zero if format is bigendian, zero otherwise.
  284. *
  285. * \threadsafety It is safe to call this macro from any thread.
  286. *
  287. * \since This macro is available since SDL 3.2.0.
  288. */
  289. #define SDL_AUDIO_ISBIGENDIAN(x) ((x) & SDL_AUDIO_MASK_BIG_ENDIAN)
  290. /**
  291. * Determine if an SDL_AudioFormat represents littleendian data.
  292. *
  293. * For example, `SDL_AUDIO_ISLITTLEENDIAN(SDL_AUDIO_S16BE)` returns 0.
  294. *
  295. * \param x an SDL_AudioFormat value.
  296. * \returns non-zero if format is littleendian, zero otherwise.
  297. *
  298. * \threadsafety It is safe to call this macro from any thread.
  299. *
  300. * \since This macro is available since SDL 3.2.0.
  301. */
  302. #define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))
  303. /**
  304. * Determine if an SDL_AudioFormat represents signed data.
  305. *
  306. * For example, `SDL_AUDIO_ISSIGNED(SDL_AUDIO_U8)` returns 0.
  307. *
  308. * \param x an SDL_AudioFormat value.
  309. * \returns non-zero if format is signed, zero otherwise.
  310. *
  311. * \threadsafety It is safe to call this macro from any thread.
  312. *
  313. * \since This macro is available since SDL 3.2.0.
  314. */
  315. #define SDL_AUDIO_ISSIGNED(x) ((x) & SDL_AUDIO_MASK_SIGNED)
  316. /**
  317. * Determine if an SDL_AudioFormat represents integer data.
  318. *
  319. * For example, `SDL_AUDIO_ISINT(SDL_AUDIO_F32)` returns 0.
  320. *
  321. * \param x an SDL_AudioFormat value.
  322. * \returns non-zero if format is integer, zero otherwise.
  323. *
  324. * \threadsafety It is safe to call this macro from any thread.
  325. *
  326. * \since This macro is available since SDL 3.2.0.
  327. */
  328. #define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))
  329. /**
  330. * Determine if an SDL_AudioFormat represents unsigned data.
  331. *
  332. * For example, `SDL_AUDIO_ISUNSIGNED(SDL_AUDIO_S16)` returns 0.
  333. *
  334. * \param x an SDL_AudioFormat value.
  335. * \returns non-zero if format is unsigned, zero otherwise.
  336. *
  337. * \threadsafety It is safe to call this macro from any thread.
  338. *
  339. * \since This macro is available since SDL 3.2.0.
  340. */
  341. #define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))
  342. /**
  343. * SDL Audio Device instance IDs.
  344. *
  345. * Zero is used to signify an invalid/null device.
  346. *
  347. * \since This datatype is available since SDL 3.2.0.
  348. */
  349. typedef Uint32 SDL_AudioDeviceID;
  350. /**
  351. * A value used to request a default playback audio device.
  352. *
  353. * Several functions that require an SDL_AudioDeviceID will accept this value
  354. * to signify the app just wants the system to choose a default device instead
  355. * of the app providing a specific one.
  356. *
  357. * \since This macro is available since SDL 3.2.0.
  358. */
  359. #define SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK ((SDL_AudioDeviceID) 0xFFFFFFFFu)
  360. /**
  361. * A value used to request a default recording audio device.
  362. *
  363. * Several functions that require an SDL_AudioDeviceID will accept this value
  364. * to signify the app just wants the system to choose a default device instead
  365. * of the app providing a specific one.
  366. *
  367. * \since This macro is available since SDL 3.2.0.
  368. */
  369. #define SDL_AUDIO_DEVICE_DEFAULT_RECORDING ((SDL_AudioDeviceID) 0xFFFFFFFEu)
  370. /**
  371. * Format specifier for audio data.
  372. *
  373. * \since This struct is available since SDL 3.2.0.
  374. *
  375. * \sa SDL_AudioFormat
  376. */
  377. typedef struct SDL_AudioSpec
  378. {
  379. SDL_AudioFormat format; /**< Audio data format */
  380. int channels; /**< Number of channels: 1 mono, 2 stereo, etc */
  381. int freq; /**< sample rate: sample frames per second */
  382. } SDL_AudioSpec;
  383. /**
  384. * Calculate the size of each audio frame (in bytes) from an SDL_AudioSpec.
  385. *
  386. * This reports on the size of an audio sample frame: stereo Sint16 data (2
  387. * channels of 2 bytes each) would be 4 bytes per frame, for example.
  388. *
  389. * \param x an SDL_AudioSpec to query.
  390. * \returns the number of bytes used per sample frame.
  391. *
  392. * \threadsafety It is safe to call this macro from any thread.
  393. *
  394. * \since This macro is available since SDL 3.2.0.
  395. */
  396. #define SDL_AUDIO_FRAMESIZE(x) (SDL_AUDIO_BYTESIZE((x).format) * (x).channels)
  397. /**
  398. * The opaque handle that represents an audio stream.
  399. *
  400. * SDL_AudioStream is an audio conversion interface.
  401. *
  402. * - It can handle resampling data in chunks without generating artifacts,
  403. * when it doesn't have the complete buffer available.
  404. * - It can handle incoming data in any variable size.
  405. * - It can handle input/output format changes on the fly.
  406. * - It can remap audio channels between inputs and outputs.
  407. * - You push data as you have it, and pull it when you need it; the
  408. * stream will buffer data as needed.
  409. * - It can also function as a basic audio data queue even if you just have
  410. * sound that needs to pass from one place to another.
  411. * - You can hook callbacks up to them when more data is added or requested,
  412. * to manage data on-the-fly.
  413. *
  414. * Audio streams are the core of the SDL3 audio interface. You create one or
  415. * more of them, bind them to an opened audio device, and feed data to them
  416. * (or for recording, consume data from them).
  417. *
  418. * \since This struct is available since SDL 3.2.0.
  419. *
  420. * \sa SDL_CreateAudioStream
  421. */
  422. typedef struct SDL_AudioStream SDL_AudioStream;
  423. /* Function prototypes */
  424. /**
  425. * Use this function to get the number of built-in audio drivers.
  426. *
  427. * This function returns a hardcoded number. This never returns a negative
  428. * value; if there are no drivers compiled into this build of SDL, this
  429. * function returns zero. The presence of a driver in this list does not mean
  430. * it will function, it just means SDL is capable of interacting with that
  431. * interface. For example, a build of SDL might have esound support, but if
  432. * there's no esound server available, SDL's esound driver would fail if used.
  433. *
  434. * By default, SDL tries all drivers, in its preferred order, until one is
  435. * found to be usable.
  436. *
  437. * \returns the number of built-in audio drivers.
  438. *
  439. * \threadsafety It is safe to call this function from any thread.
  440. *
  441. * \since This function is available since SDL 3.2.0.
  442. *
  443. * \sa SDL_GetAudioDriver
  444. */
  445. extern SDL_DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
  446. /**
  447. * Use this function to get the name of a built in audio driver.
  448. *
  449. * The list of audio drivers is given in the order that they are normally
  450. * initialized by default; the drivers that seem more reasonable to choose
  451. * first (as far as the SDL developers believe) are earlier in the list.
  452. *
  453. * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
  454. * "coreaudio" or "wasapi". These never have Unicode characters, and are not
  455. * meant to be proper names.
  456. *
  457. * \param index the index of the audio driver; the value ranges from 0 to
  458. * SDL_GetNumAudioDrivers() - 1.
  459. * \returns the name of the audio driver at the requested index, or NULL if an
  460. * invalid index was specified.
  461. *
  462. * \threadsafety It is safe to call this function from any thread.
  463. *
  464. * \since This function is available since SDL 3.2.0.
  465. *
  466. * \sa SDL_GetNumAudioDrivers
  467. */
  468. extern SDL_DECLSPEC const char * SDLCALL SDL_GetAudioDriver(int index);
  469. /**
  470. * Get the name of the current audio driver.
  471. *
  472. * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
  473. * "coreaudio" or "wasapi". These never have Unicode characters, and are not
  474. * meant to be proper names.
  475. *
  476. * \returns the name of the current audio driver or NULL if no driver has been
  477. * initialized.
  478. *
  479. * \threadsafety It is safe to call this function from any thread.
  480. *
  481. * \since This function is available since SDL 3.2.0.
  482. */
  483. extern SDL_DECLSPEC const char * SDLCALL SDL_GetCurrentAudioDriver(void);
  484. /**
  485. * Get a list of currently-connected audio playback devices.
  486. *
  487. * This returns of list of available devices that play sound, perhaps to
  488. * speakers or headphones ("playback" devices). If you want devices that
  489. * record audio, like a microphone ("recording" devices), use
  490. * SDL_GetAudioRecordingDevices() instead.
  491. *
  492. * This only returns a list of physical devices; it will not have any device
  493. * IDs returned by SDL_OpenAudioDevice().
  494. *
  495. * If this function returns NULL, to signify an error, `*count` will be set to
  496. * zero.
  497. *
  498. * \param count a pointer filled in with the number of devices returned, may
  499. * be NULL.
  500. * \returns a 0 terminated array of device instance IDs or NULL on error; call
  501. * SDL_GetError() for more information. This should be freed with
  502. * SDL_free() when it is no longer needed.
  503. *
  504. * \threadsafety It is safe to call this function from any thread.
  505. *
  506. * \since This function is available since SDL 3.2.0.
  507. *
  508. * \sa SDL_OpenAudioDevice
  509. * \sa SDL_GetAudioRecordingDevices
  510. */
  511. extern SDL_DECLSPEC SDL_AudioDeviceID * SDLCALL SDL_GetAudioPlaybackDevices(int *count);
  512. /**
  513. * Get a list of currently-connected audio recording devices.
  514. *
  515. * This returns of list of available devices that record audio, like a
  516. * microphone ("recording" devices). If you want devices that play sound,
  517. * perhaps to speakers or headphones ("playback" devices), use
  518. * SDL_GetAudioPlaybackDevices() instead.
  519. *
  520. * This only returns a list of physical devices; it will not have any device
  521. * IDs returned by SDL_OpenAudioDevice().
  522. *
  523. * If this function returns NULL, to signify an error, `*count` will be set to
  524. * zero.
  525. *
  526. * \param count a pointer filled in with the number of devices returned, may
  527. * be NULL.
  528. * \returns a 0 terminated array of device instance IDs, or NULL on failure;
  529. * call SDL_GetError() for more information. This should be freed
  530. * with SDL_free() when it is no longer needed.
  531. *
  532. * \threadsafety It is safe to call this function from any thread.
  533. *
  534. * \since This function is available since SDL 3.2.0.
  535. *
  536. * \sa SDL_OpenAudioDevice
  537. * \sa SDL_GetAudioPlaybackDevices
  538. */
  539. extern SDL_DECLSPEC SDL_AudioDeviceID * SDLCALL SDL_GetAudioRecordingDevices(int *count);
  540. /**
  541. * Get the human-readable name of a specific audio device.
  542. *
  543. * **WARNING**: this function will work with SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK
  544. * and SDL_AUDIO_DEVICE_DEFAULT_RECORDING, returning the current default
  545. * physical devices' names. However, as the default device may change at any
  546. * time, it is likely better to show a generic name to the user, like "System
  547. * default audio device" or perhaps "default [currently %s]". Do not store
  548. * this name to disk to reidentify the device in a later run of the program,
  549. * as the default might change in general, and the string will be the name of
  550. * a specific device and not the abstract system default.
  551. *
  552. * \param devid the instance ID of the device to query.
  553. * \returns the name of the audio device, or NULL on failure; call
  554. * SDL_GetError() for more information.
  555. *
  556. * \threadsafety It is safe to call this function from any thread.
  557. *
  558. * \since This function is available since SDL 3.2.0.
  559. *
  560. * \sa SDL_GetAudioPlaybackDevices
  561. * \sa SDL_GetAudioRecordingDevices
  562. */
  563. extern SDL_DECLSPEC const char * SDLCALL SDL_GetAudioDeviceName(SDL_AudioDeviceID devid);
  564. /**
  565. * Get the current audio format of a specific audio device.
  566. *
  567. * For an opened device, this will report the format the device is currently
  568. * using. If the device isn't yet opened, this will report the device's
  569. * preferred format (or a reasonable default if this can't be determined).
  570. *
  571. * You may also specify SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or
  572. * SDL_AUDIO_DEVICE_DEFAULT_RECORDING here, which is useful for getting a
  573. * reasonable recommendation before opening the system-recommended default
  574. * device.
  575. *
  576. * You can also use this to request the current device buffer size. This is
  577. * specified in sample frames and represents the amount of data SDL will feed
  578. * to the physical hardware in each chunk. This can be converted to
  579. * milliseconds of audio with the following equation:
  580. *
  581. * `ms = (int) ((((Sint64) frames) * 1000) / spec.freq);`
  582. *
  583. * Buffer size is only important if you need low-level control over the audio
  584. * playback timing. Most apps do not need this.
  585. *
  586. * \param devid the instance ID of the device to query.
  587. * \param spec on return, will be filled with device details.
  588. * \param sample_frames pointer to store device buffer size, in sample frames.
  589. * Can be NULL.
  590. * \returns true on success or false on failure; call SDL_GetError() for more
  591. * information.
  592. *
  593. * \threadsafety It is safe to call this function from any thread.
  594. *
  595. * \since This function is available since SDL 3.2.0.
  596. */
  597. extern SDL_DECLSPEC bool SDLCALL SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec, int *sample_frames);
  598. /**
  599. * Get the current channel map of an audio device.
  600. *
  601. * Channel maps are optional; most things do not need them, instead passing
  602. * data in the [order that SDL expects](CategoryAudio#channel-layouts).
  603. *
  604. * Audio devices usually have no remapping applied. This is represented by
  605. * returning NULL, and does not signify an error.
  606. *
  607. * \param devid the instance ID of the device to query.
  608. * \param count On output, set to number of channels in the map. Can be NULL.
  609. * \returns an array of the current channel mapping, with as many elements as
  610. * the current output spec's channels, or NULL if default. This
  611. * should be freed with SDL_free() when it is no longer needed.
  612. *
  613. * \threadsafety It is safe to call this function from any thread.
  614. *
  615. * \since This function is available since SDL 3.2.0.
  616. *
  617. * \sa SDL_SetAudioStreamInputChannelMap
  618. */
  619. extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioDeviceChannelMap(SDL_AudioDeviceID devid, int *count);
  620. /**
  621. * Open a specific audio device.
  622. *
  623. * You can open both playback and recording devices through this function.
  624. * Playback devices will take data from bound audio streams, mix it, and send
  625. * it to the hardware. Recording devices will feed any bound audio streams
  626. * with a copy of any incoming data.
  627. *
  628. * An opened audio device starts out with no audio streams bound. To start
  629. * audio playing, bind a stream and supply audio data to it. Unlike SDL2,
  630. * there is no audio callback; you only bind audio streams and make sure they
  631. * have data flowing into them (however, you can simulate SDL2's semantics
  632. * fairly closely by using SDL_OpenAudioDeviceStream instead of this
  633. * function).
  634. *
  635. * If you don't care about opening a specific device, pass a `devid` of either
  636. * `SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK` or
  637. * `SDL_AUDIO_DEVICE_DEFAULT_RECORDING`. In this case, SDL will try to pick
  638. * the most reasonable default, and may also switch between physical devices
  639. * seamlessly later, if the most reasonable default changes during the
  640. * lifetime of this opened device (user changed the default in the OS's system
  641. * preferences, the default got unplugged so the system jumped to a new
  642. * default, the user plugged in headphones on a mobile device, etc). Unless
  643. * you have a good reason to choose a specific device, this is probably what
  644. * you want.
  645. *
  646. * You may request a specific format for the audio device, but there is no
  647. * promise the device will honor that request for several reasons. As such,
  648. * it's only meant to be a hint as to what data your app will provide. Audio
  649. * streams will accept data in whatever format you specify and manage
  650. * conversion for you as appropriate. SDL_GetAudioDeviceFormat can tell you
  651. * the preferred format for the device before opening and the actual format
  652. * the device is using after opening.
  653. *
  654. * It's legal to open the same device ID more than once; each successful open
  655. * will generate a new logical SDL_AudioDeviceID that is managed separately
  656. * from others on the same physical device. This allows libraries to open a
  657. * device separately from the main app and bind its own streams without
  658. * conflicting.
  659. *
  660. * It is also legal to open a device ID returned by a previous call to this
  661. * function; doing so just creates another logical device on the same physical
  662. * device. This may be useful for making logical groupings of audio streams.
  663. *
  664. * This function returns the opened device ID on success. This is a new,
  665. * unique SDL_AudioDeviceID that represents a logical device.
  666. *
  667. * Some backends might offer arbitrary devices (for example, a networked audio
  668. * protocol that can connect to an arbitrary server). For these, as a change
  669. * from SDL2, you should open a default device ID and use an SDL hint to
  670. * specify the target if you care, or otherwise let the backend figure out a
  671. * reasonable default. Most backends don't offer anything like this, and often
  672. * this would be an end user setting an environment variable for their custom
  673. * need, and not something an application should specifically manage.
  674. *
  675. * When done with an audio device, possibly at the end of the app's life, one
  676. * should call SDL_CloseAudioDevice() on the returned device id.
  677. *
  678. * \param devid the device instance id to open, or
  679. * SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or
  680. * SDL_AUDIO_DEVICE_DEFAULT_RECORDING for the most reasonable
  681. * default device.
  682. * \param spec the requested device configuration. Can be NULL to use
  683. * reasonable defaults.
  684. * \returns the device ID on success or 0 on failure; call SDL_GetError() for
  685. * more information.
  686. *
  687. * \threadsafety It is safe to call this function from any thread.
  688. *
  689. * \since This function is available since SDL 3.2.0.
  690. *
  691. * \sa SDL_CloseAudioDevice
  692. * \sa SDL_GetAudioDeviceFormat
  693. */
  694. extern SDL_DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec);
  695. /**
  696. * Determine if an audio device is physical (instead of logical).
  697. *
  698. * An SDL_AudioDeviceID that represents physical hardware is a physical
  699. * device; there is one for each piece of hardware that SDL can see. Logical
  700. * devices are created by calling SDL_OpenAudioDevice or
  701. * SDL_OpenAudioDeviceStream, and while each is associated with a physical
  702. * device, there can be any number of logical devices on one physical device.
  703. *
  704. * For the most part, logical and physical IDs are interchangeable--if you try
  705. * to open a logical device, SDL understands to assign that effort to the
  706. * underlying physical device, etc. However, it might be useful to know if an
  707. * arbitrary device ID is physical or logical. This function reports which.
  708. *
  709. * This function may return either true or false for invalid device IDs.
  710. *
  711. * \param devid the device ID to query.
  712. * \returns true if devid is a physical device, false if it is logical.
  713. *
  714. * \threadsafety It is safe to call this function from any thread.
  715. *
  716. * \since This function is available since SDL 3.2.0.
  717. */
  718. extern SDL_DECLSPEC bool SDLCALL SDL_IsAudioDevicePhysical(SDL_AudioDeviceID devid);
  719. /**
  720. * Determine if an audio device is a playback device (instead of recording).
  721. *
  722. * This function may return either true or false for invalid device IDs.
  723. *
  724. * \param devid the device ID to query.
  725. * \returns true if devid is a playback device, false if it is recording.
  726. *
  727. * \threadsafety It is safe to call this function from any thread.
  728. *
  729. * \since This function is available since SDL 3.2.0.
  730. */
  731. extern SDL_DECLSPEC bool SDLCALL SDL_IsAudioDevicePlayback(SDL_AudioDeviceID devid);
  732. /**
  733. * Use this function to pause audio playback on a specified device.
  734. *
  735. * This function pauses audio processing for a given device. Any bound audio
  736. * streams will not progress, and no audio will be generated. Pausing one
  737. * device does not prevent other unpaused devices from running.
  738. *
  739. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  740. * has to bind a stream before any audio will flow. Pausing a paused device is
  741. * a legal no-op.
  742. *
  743. * Pausing a device can be useful to halt all audio without unbinding all the
  744. * audio streams. This might be useful while a game is paused, or a level is
  745. * loading, etc.
  746. *
  747. * Physical devices can not be paused or unpaused, only logical devices
  748. * created through SDL_OpenAudioDevice() can be.
  749. *
  750. * \param devid a device opened by SDL_OpenAudioDevice().
  751. * \returns true on success or false on failure; call SDL_GetError() for more
  752. * information.
  753. *
  754. * \threadsafety It is safe to call this function from any thread.
  755. *
  756. * \since This function is available since SDL 3.2.0.
  757. *
  758. * \sa SDL_ResumeAudioDevice
  759. * \sa SDL_AudioDevicePaused
  760. */
  761. extern SDL_DECLSPEC bool SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID devid);
  762. /**
  763. * Use this function to unpause audio playback on a specified device.
  764. *
  765. * This function unpauses audio processing for a given device that has
  766. * previously been paused with SDL_PauseAudioDevice(). Once unpaused, any
  767. * bound audio streams will begin to progress again, and audio can be
  768. * generated.
  769. *
  770. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  771. * has to bind a stream before any audio will flow. Unpausing an unpaused
  772. * device is a legal no-op.
  773. *
  774. * Physical devices can not be paused or unpaused, only logical devices
  775. * created through SDL_OpenAudioDevice() can be.
  776. *
  777. * \param devid a device opened by SDL_OpenAudioDevice().
  778. * \returns true on success or false on failure; call SDL_GetError() for more
  779. * information.
  780. *
  781. * \threadsafety It is safe to call this function from any thread.
  782. *
  783. * \since This function is available since SDL 3.2.0.
  784. *
  785. * \sa SDL_AudioDevicePaused
  786. * \sa SDL_PauseAudioDevice
  787. */
  788. extern SDL_DECLSPEC bool SDLCALL SDL_ResumeAudioDevice(SDL_AudioDeviceID devid);
  789. /**
  790. * Use this function to query if an audio device is paused.
  791. *
  792. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  793. * has to bind a stream before any audio will flow.
  794. *
  795. * Physical devices can not be paused or unpaused, only logical devices
  796. * created through SDL_OpenAudioDevice() can be. Physical and invalid device
  797. * IDs will report themselves as unpaused here.
  798. *
  799. * \param devid a device opened by SDL_OpenAudioDevice().
  800. * \returns true if device is valid and paused, false otherwise.
  801. *
  802. * \threadsafety It is safe to call this function from any thread.
  803. *
  804. * \since This function is available since SDL 3.2.0.
  805. *
  806. * \sa SDL_PauseAudioDevice
  807. * \sa SDL_ResumeAudioDevice
  808. */
  809. extern SDL_DECLSPEC bool SDLCALL SDL_AudioDevicePaused(SDL_AudioDeviceID devid);
  810. /**
  811. * Get the gain of an audio device.
  812. *
  813. * The gain of a device is its volume; a larger gain means a louder output,
  814. * with a gain of zero being silence.
  815. *
  816. * Audio devices default to a gain of 1.0f (no change in output).
  817. *
  818. * Physical devices may not have their gain changed, only logical devices, and
  819. * this function will always return -1.0f when used on physical devices.
  820. *
  821. * \param devid the audio device to query.
  822. * \returns the gain of the device or -1.0f on failure; call SDL_GetError()
  823. * for more information.
  824. *
  825. * \threadsafety It is safe to call this function from any thread.
  826. *
  827. * \since This function is available since SDL 3.2.0.
  828. *
  829. * \sa SDL_SetAudioDeviceGain
  830. */
  831. extern SDL_DECLSPEC float SDLCALL SDL_GetAudioDeviceGain(SDL_AudioDeviceID devid);
  832. /**
  833. * Change the gain of an audio device.
  834. *
  835. * The gain of a device is its volume; a larger gain means a louder output,
  836. * with a gain of zero being silence.
  837. *
  838. * Audio devices default to a gain of 1.0f (no change in output).
  839. *
  840. * Physical devices may not have their gain changed, only logical devices, and
  841. * this function will always return false when used on physical devices. While
  842. * it might seem attractive to adjust several logical devices at once in this
  843. * way, it would allow an app or library to interfere with another portion of
  844. * the program's otherwise-isolated devices.
  845. *
  846. * This is applied, along with any per-audiostream gain, during playback to
  847. * the hardware, and can be continuously changed to create various effects. On
  848. * recording devices, this will adjust the gain before passing the data into
  849. * an audiostream; that recording audiostream can then adjust its gain further
  850. * when outputting the data elsewhere, if it likes, but that second gain is
  851. * not applied until the data leaves the audiostream again.
  852. *
  853. * \param devid the audio device on which to change gain.
  854. * \param gain the gain. 1.0f is no change, 0.0f is silence.
  855. * \returns true on success or false on failure; call SDL_GetError() for more
  856. * information.
  857. *
  858. * \threadsafety It is safe to call this function from any thread, as it holds
  859. * a stream-specific mutex while running.
  860. *
  861. * \since This function is available since SDL 3.2.0.
  862. *
  863. * \sa SDL_GetAudioDeviceGain
  864. */
  865. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioDeviceGain(SDL_AudioDeviceID devid, float gain);
  866. /**
  867. * Close a previously-opened audio device.
  868. *
  869. * The application should close open audio devices once they are no longer
  870. * needed.
  871. *
  872. * This function may block briefly while pending audio data is played by the
  873. * hardware, so that applications don't drop the last buffer of data they
  874. * supplied if terminating immediately afterwards.
  875. *
  876. * \param devid an audio device id previously returned by
  877. * SDL_OpenAudioDevice().
  878. *
  879. * \threadsafety It is safe to call this function from any thread.
  880. *
  881. * \since This function is available since SDL 3.2.0.
  882. *
  883. * \sa SDL_OpenAudioDevice
  884. */
  885. extern SDL_DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID devid);
  886. /**
  887. * Bind a list of audio streams to an audio device.
  888. *
  889. * Audio data will flow through any bound streams. For a playback device, data
  890. * for all bound streams will be mixed together and fed to the device. For a
  891. * recording device, a copy of recorded data will be provided to each bound
  892. * stream.
  893. *
  894. * Audio streams can only be bound to an open device. This operation is
  895. * atomic--all streams bound in the same call will start processing at the
  896. * same time, so they can stay in sync. Also: either all streams will be bound
  897. * or none of them will be.
  898. *
  899. * It is an error to bind an already-bound stream; it must be explicitly
  900. * unbound first.
  901. *
  902. * Binding a stream to a device will set its output format for playback
  903. * devices, and its input format for recording devices, so they match the
  904. * device's settings. The caller is welcome to change the other end of the
  905. * stream's format at any time with SDL_SetAudioStreamFormat(). If the other
  906. * end of the stream's format has never been set (the audio stream was created
  907. * with a NULL audio spec), this function will set it to match the device
  908. * end's format.
  909. *
  910. * \param devid an audio device to bind a stream to.
  911. * \param streams an array of audio streams to bind.
  912. * \param num_streams number streams listed in the `streams` array.
  913. * \returns true on success or false on failure; call SDL_GetError() for more
  914. * information.
  915. *
  916. * \threadsafety It is safe to call this function from any thread.
  917. *
  918. * \since This function is available since SDL 3.2.0.
  919. *
  920. * \sa SDL_BindAudioStreams
  921. * \sa SDL_UnbindAudioStream
  922. * \sa SDL_GetAudioStreamDevice
  923. */
  924. extern SDL_DECLSPEC bool SDLCALL SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream * const *streams, int num_streams);
  925. /**
  926. * Bind a single audio stream to an audio device.
  927. *
  928. * This is a convenience function, equivalent to calling
  929. * `SDL_BindAudioStreams(devid, &stream, 1)`.
  930. *
  931. * \param devid an audio device to bind a stream to.
  932. * \param stream an audio stream to bind to a device.
  933. * \returns true on success or false on failure; call SDL_GetError() for more
  934. * information.
  935. *
  936. * \threadsafety It is safe to call this function from any thread.
  937. *
  938. * \since This function is available since SDL 3.2.0.
  939. *
  940. * \sa SDL_BindAudioStreams
  941. * \sa SDL_UnbindAudioStream
  942. * \sa SDL_GetAudioStreamDevice
  943. */
  944. extern SDL_DECLSPEC bool SDLCALL SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream);
  945. /**
  946. * Unbind a list of audio streams from their audio devices.
  947. *
  948. * The streams being unbound do not all have to be on the same device. All
  949. * streams on the same device will be unbound atomically (data will stop
  950. * flowing through all unbound streams on the same device at the same time).
  951. *
  952. * Unbinding a stream that isn't bound to a device is a legal no-op.
  953. *
  954. * \param streams an array of audio streams to unbind. Can be NULL or contain
  955. * NULL.
  956. * \param num_streams number streams listed in the `streams` array.
  957. *
  958. * \threadsafety It is safe to call this function from any thread.
  959. *
  960. * \since This function is available since SDL 3.2.0.
  961. *
  962. * \sa SDL_BindAudioStreams
  963. */
  964. extern SDL_DECLSPEC void SDLCALL SDL_UnbindAudioStreams(SDL_AudioStream * const *streams, int num_streams);
  965. /**
  966. * Unbind a single audio stream from its audio device.
  967. *
  968. * This is a convenience function, equivalent to calling
  969. * `SDL_UnbindAudioStreams(&stream, 1)`.
  970. *
  971. * \param stream an audio stream to unbind from a device. Can be NULL.
  972. *
  973. * \threadsafety It is safe to call this function from any thread.
  974. *
  975. * \since This function is available since SDL 3.2.0.
  976. *
  977. * \sa SDL_BindAudioStream
  978. */
  979. extern SDL_DECLSPEC void SDLCALL SDL_UnbindAudioStream(SDL_AudioStream *stream);
  980. /**
  981. * Query an audio stream for its currently-bound device.
  982. *
  983. * This reports the logical audio device that an audio stream is currently
  984. * bound to.
  985. *
  986. * If not bound, or invalid, this returns zero, which is not a valid device
  987. * ID.
  988. *
  989. * \param stream the audio stream to query.
  990. * \returns the bound audio device, or 0 if not bound or invalid.
  991. *
  992. * \threadsafety It is safe to call this function from any thread.
  993. *
  994. * \since This function is available since SDL 3.2.0.
  995. *
  996. * \sa SDL_BindAudioStream
  997. * \sa SDL_BindAudioStreams
  998. */
  999. extern SDL_DECLSPEC SDL_AudioDeviceID SDLCALL SDL_GetAudioStreamDevice(SDL_AudioStream *stream);
  1000. /**
  1001. * Create a new audio stream.
  1002. *
  1003. * Note that `src_spec` or `dst_spec` may be NULL, but any attempts to
  1004. * put or get data from an audio stream will fail until it has valid
  1005. * specs assigned to both ends of the stream. Specs can be assigned later
  1006. * through SDL_SetAudioStreamFormat(), or binding the stream to an audio
  1007. * device (which will set the format of only the input or output,
  1008. * depending on what kind of device the stream was bound to).
  1009. *
  1010. * \param src_spec the format details of the input audio. May be NULL.
  1011. * \param dst_spec the format details of the output audio. May be NULL.
  1012. * \returns a new audio stream on success or NULL on failure; call
  1013. * SDL_GetError() for more information.
  1014. *
  1015. * \threadsafety It is safe to call this function from any thread.
  1016. *
  1017. * \since This function is available since SDL 3.2.0.
  1018. *
  1019. * \sa SDL_PutAudioStreamData
  1020. * \sa SDL_GetAudioStreamData
  1021. * \sa SDL_GetAudioStreamAvailable
  1022. * \sa SDL_FlushAudioStream
  1023. * \sa SDL_ClearAudioStream
  1024. * \sa SDL_SetAudioStreamFormat
  1025. * \sa SDL_DestroyAudioStream
  1026. */
  1027. extern SDL_DECLSPEC SDL_AudioStream * SDLCALL SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
  1028. /**
  1029. * Get the properties associated with an audio stream.
  1030. *
  1031. * The application can hang any data it wants here, but the following
  1032. * properties are understood by SDL:
  1033. *
  1034. * - `SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN`: if true (the default), the
  1035. * stream be automatically cleaned up when the audio subsystem quits. If set
  1036. * to false, the streams will persist beyond that. This property is ignored
  1037. * for streams created through SDL_OpenAudioDeviceStream(), and will always
  1038. * be cleaned up. Streams that are not cleaned up will still be unbound from
  1039. * devices when the audio subsystem quits. This property was added in SDL
  1040. * 3.4.0.
  1041. *
  1042. * \param stream the SDL_AudioStream to query.
  1043. * \returns a valid property ID on success or 0 on failure; call
  1044. * SDL_GetError() for more information.
  1045. *
  1046. * \threadsafety It is safe to call this function from any thread.
  1047. *
  1048. * \since This function is available since SDL 3.2.0.
  1049. */
  1050. extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetAudioStreamProperties(SDL_AudioStream *stream);
  1051. #define SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN "SDL.audiostream.auto_cleanup"
  1052. /**
  1053. * Query the current format of an audio stream.
  1054. *
  1055. * \param stream the SDL_AudioStream to query.
  1056. * \param src_spec where to store the input audio format; ignored if NULL.
  1057. * \param dst_spec where to store the output audio format; ignored if NULL.
  1058. * \returns true on success or false on failure; call SDL_GetError() for more
  1059. * information.
  1060. *
  1061. * \threadsafety It is safe to call this function from any thread, as it holds
  1062. * a stream-specific mutex while running.
  1063. *
  1064. * \since This function is available since SDL 3.2.0.
  1065. *
  1066. * \sa SDL_SetAudioStreamFormat
  1067. */
  1068. extern SDL_DECLSPEC bool SDLCALL SDL_GetAudioStreamFormat(SDL_AudioStream *stream, SDL_AudioSpec *src_spec, SDL_AudioSpec *dst_spec);
  1069. /**
  1070. * Change the input and output formats of an audio stream.
  1071. *
  1072. * Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData
  1073. * will reflect the new format, and future calls to SDL_PutAudioStreamData
  1074. * must provide data in the new input formats.
  1075. *
  1076. * Data that was previously queued in the stream will still be operated on in
  1077. * the format that was current when it was added, which is to say you can put
  1078. * the end of a sound file in one format to a stream, change formats for the
  1079. * next sound file, and start putting that new data while the previous sound
  1080. * file is still queued, and everything will still play back correctly.
  1081. *
  1082. * If a stream is bound to a device, then the format of the side of the stream
  1083. * bound to a device cannot be changed (src_spec for recording devices,
  1084. * dst_spec for playback devices). Attempts to make a change to this side will
  1085. * be ignored, but this will not report an error. The other side's format can
  1086. * be changed.
  1087. *
  1088. * `src_spec` and `dst_spec` may each be NULL; a NULL spec signals not to
  1089. * change the current format for that side of the stream.
  1090. *
  1091. * \param stream the stream the format is being changed.
  1092. * \param src_spec the new format of the audio input; if NULL, it is not
  1093. * changed.
  1094. * \param dst_spec the new format of the audio output; if NULL, it is not
  1095. * changed.
  1096. * \returns true on success or false on failure; call SDL_GetError() for more
  1097. * information.
  1098. *
  1099. * \threadsafety It is safe to call this function from any thread, as it holds
  1100. * a stream-specific mutex while running.
  1101. *
  1102. * \since This function is available since SDL 3.2.0.
  1103. *
  1104. * \sa SDL_GetAudioStreamFormat
  1105. * \sa SDL_SetAudioStreamFrequencyRatio
  1106. */
  1107. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream, const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
  1108. /**
  1109. * Get the frequency ratio of an audio stream.
  1110. *
  1111. * \param stream the SDL_AudioStream to query.
  1112. * \returns the frequency ratio of the stream or 0.0 on failure; call
  1113. * SDL_GetError() for more information.
  1114. *
  1115. * \threadsafety It is safe to call this function from any thread, as it holds
  1116. * a stream-specific mutex while running.
  1117. *
  1118. * \since This function is available since SDL 3.2.0.
  1119. *
  1120. * \sa SDL_SetAudioStreamFrequencyRatio
  1121. */
  1122. extern SDL_DECLSPEC float SDLCALL SDL_GetAudioStreamFrequencyRatio(SDL_AudioStream *stream);
  1123. /**
  1124. * Change the frequency ratio of an audio stream.
  1125. *
  1126. * The frequency ratio is used to adjust the rate at which input data is
  1127. * consumed. Changing this effectively modifies the speed and pitch of the
  1128. * audio. A value greater than 1.0f will play the audio faster, and at a
  1129. * higher pitch. A value less than 1.0f will play the audio slower, and at a
  1130. * lower pitch. 1.0f means play at normal speed.
  1131. *
  1132. * This is applied during SDL_GetAudioStreamData, and can be continuously
  1133. * changed to create various effects.
  1134. *
  1135. * \param stream the stream on which the frequency ratio is being changed.
  1136. * \param ratio the frequency ratio. 1.0 is normal speed. Must be between 0.01
  1137. * and 100.
  1138. * \returns true on success or false on failure; call SDL_GetError() for more
  1139. * information.
  1140. *
  1141. * \threadsafety It is safe to call this function from any thread, as it holds
  1142. * a stream-specific mutex while running.
  1143. *
  1144. * \since This function is available since SDL 3.2.0.
  1145. *
  1146. * \sa SDL_GetAudioStreamFrequencyRatio
  1147. * \sa SDL_SetAudioStreamFormat
  1148. */
  1149. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamFrequencyRatio(SDL_AudioStream *stream, float ratio);
  1150. /**
  1151. * Get the gain of an audio stream.
  1152. *
  1153. * The gain of a stream is its volume; a larger gain means a louder output,
  1154. * with a gain of zero being silence.
  1155. *
  1156. * Audio streams default to a gain of 1.0f (no change in output).
  1157. *
  1158. * \param stream the SDL_AudioStream to query.
  1159. * \returns the gain of the stream or -1.0f on failure; call SDL_GetError()
  1160. * for more information.
  1161. *
  1162. * \threadsafety It is safe to call this function from any thread, as it holds
  1163. * a stream-specific mutex while running.
  1164. *
  1165. * \since This function is available since SDL 3.2.0.
  1166. *
  1167. * \sa SDL_SetAudioStreamGain
  1168. */
  1169. extern SDL_DECLSPEC float SDLCALL SDL_GetAudioStreamGain(SDL_AudioStream *stream);
  1170. /**
  1171. * Change the gain of an audio stream.
  1172. *
  1173. * The gain of a stream is its volume; a larger gain means a louder output,
  1174. * with a gain of zero being silence.
  1175. *
  1176. * Audio streams default to a gain of 1.0f (no change in output).
  1177. *
  1178. * This is applied during SDL_GetAudioStreamData, and can be continuously
  1179. * changed to create various effects.
  1180. *
  1181. * \param stream the stream on which the gain is being changed.
  1182. * \param gain the gain. 1.0f is no change, 0.0f is silence.
  1183. * \returns true on success or false on failure; call SDL_GetError() for more
  1184. * information.
  1185. *
  1186. * \threadsafety It is safe to call this function from any thread, as it holds
  1187. * a stream-specific mutex while running.
  1188. *
  1189. * \since This function is available since SDL 3.2.0.
  1190. *
  1191. * \sa SDL_GetAudioStreamGain
  1192. */
  1193. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamGain(SDL_AudioStream *stream, float gain);
  1194. /**
  1195. * Get the current input channel map of an audio stream.
  1196. *
  1197. * Channel maps are optional; most things do not need them, instead passing
  1198. * data in the [order that SDL expects](CategoryAudio#channel-layouts).
  1199. *
  1200. * Audio streams default to no remapping applied. This is represented by
  1201. * returning NULL, and does not signify an error.
  1202. *
  1203. * \param stream the SDL_AudioStream to query.
  1204. * \param count On output, set to number of channels in the map. Can be NULL.
  1205. * \returns an array of the current channel mapping, with as many elements as
  1206. * the current output spec's channels, or NULL if default. This
  1207. * should be freed with SDL_free() when it is no longer needed.
  1208. *
  1209. * \threadsafety It is safe to call this function from any thread, as it holds
  1210. * a stream-specific mutex while running.
  1211. *
  1212. * \since This function is available since SDL 3.2.0.
  1213. *
  1214. * \sa SDL_SetAudioStreamInputChannelMap
  1215. */
  1216. extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioStreamInputChannelMap(SDL_AudioStream *stream, int *count);
  1217. /**
  1218. * Get the current output channel map of an audio stream.
  1219. *
  1220. * Channel maps are optional; most things do not need them, instead passing
  1221. * data in the [order that SDL expects](CategoryAudio#channel-layouts).
  1222. *
  1223. * Audio streams default to no remapping applied. This is represented by
  1224. * returning NULL, and does not signify an error.
  1225. *
  1226. * \param stream the SDL_AudioStream to query.
  1227. * \param count On output, set to number of channels in the map. Can be NULL.
  1228. * \returns an array of the current channel mapping, with as many elements as
  1229. * the current output spec's channels, or NULL if default. This
  1230. * should be freed with SDL_free() when it is no longer needed.
  1231. *
  1232. * \threadsafety It is safe to call this function from any thread, as it holds
  1233. * a stream-specific mutex while running.
  1234. *
  1235. * \since This function is available since SDL 3.2.0.
  1236. *
  1237. * \sa SDL_SetAudioStreamInputChannelMap
  1238. */
  1239. extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioStreamOutputChannelMap(SDL_AudioStream *stream, int *count);
  1240. /**
  1241. * Set the current input channel map of an audio stream.
  1242. *
  1243. * Channel maps are optional; most things do not need them, instead passing
  1244. * data in the [order that SDL expects](CategoryAudio#channel-layouts).
  1245. *
  1246. * The input channel map reorders data that is added to a stream via
  1247. * SDL_PutAudioStreamData. Future calls to SDL_PutAudioStreamData must provide
  1248. * data in the new channel order.
  1249. *
  1250. * Each item in the array represents an input channel, and its value is the
  1251. * channel that it should be remapped to. To reverse a stereo signal's left
  1252. * and right values, you'd have an array of `{ 1, 0 }`. It is legal to remap
  1253. * multiple channels to the same thing, so `{ 1, 1 }` would duplicate the
  1254. * right channel to both channels of a stereo signal. An element in the
  1255. * channel map set to -1 instead of a valid channel will mute that channel,
  1256. * setting it to a silence value.
  1257. *
  1258. * You cannot change the number of channels through a channel map, just
  1259. * reorder/mute them.
  1260. *
  1261. * Data that was previously queued in the stream will still be operated on in
  1262. * the order that was current when it was added, which is to say you can put
  1263. * the end of a sound file in one order to a stream, change orders for the
  1264. * next sound file, and start putting that new data while the previous sound
  1265. * file is still queued, and everything will still play back correctly.
  1266. *
  1267. * Audio streams default to no remapping applied. Passing a NULL channel map
  1268. * is legal, and turns off remapping.
  1269. *
  1270. * SDL will copy the channel map; the caller does not have to save this array
  1271. * after this call.
  1272. *
  1273. * If `count` is not equal to the current number of channels in the audio
  1274. * stream's format, this will fail. This is a safety measure to make sure a
  1275. * race condition hasn't changed the format while this call is setting the
  1276. * channel map.
  1277. *
  1278. * Unlike attempting to change the stream's format, the input channel map on a
  1279. * stream bound to a recording device is permitted to change at any time; any
  1280. * data added to the stream from the device after this call will have the new
  1281. * mapping, but previously-added data will still have the prior mapping.
  1282. *
  1283. * \param stream the SDL_AudioStream to change.
  1284. * \param chmap the new channel map, NULL to reset to default.
  1285. * \param count The number of channels in the map.
  1286. * \returns true on success or false on failure; call SDL_GetError() for more
  1287. * information.
  1288. *
  1289. * \threadsafety It is safe to call this function from any thread, as it holds
  1290. * a stream-specific mutex while running. Don't change the
  1291. * stream's format to have a different number of channels from a
  1292. * different thread at the same time, though!
  1293. *
  1294. * \since This function is available since SDL 3.2.0.
  1295. *
  1296. * \sa SDL_SetAudioStreamOutputChannelMap
  1297. */
  1298. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamInputChannelMap(SDL_AudioStream *stream, const int *chmap, int count);
  1299. /**
  1300. * Set the current output channel map of an audio stream.
  1301. *
  1302. * Channel maps are optional; most things do not need them, instead passing
  1303. * data in the [order that SDL expects](CategoryAudio#channel-layouts).
  1304. *
  1305. * The output channel map reorders data that is leaving a stream via
  1306. * SDL_GetAudioStreamData.
  1307. *
  1308. * Each item in the array represents an input channel, and its value is the
  1309. * channel that it should be remapped to. To reverse a stereo signal's left
  1310. * and right values, you'd have an array of `{ 1, 0 }`. It is legal to remap
  1311. * multiple channels to the same thing, so `{ 1, 1 }` would duplicate the
  1312. * right channel to both channels of a stereo signal. An element in the
  1313. * channel map set to -1 instead of a valid channel will mute that channel,
  1314. * setting it to a silence value.
  1315. *
  1316. * You cannot change the number of channels through a channel map, just
  1317. * reorder/mute them.
  1318. *
  1319. * The output channel map can be changed at any time, as output remapping is
  1320. * applied during SDL_GetAudioStreamData.
  1321. *
  1322. * Audio streams default to no remapping applied. Passing a NULL channel map
  1323. * is legal, and turns off remapping.
  1324. *
  1325. * SDL will copy the channel map; the caller does not have to save this array
  1326. * after this call.
  1327. *
  1328. * If `count` is not equal to the current number of channels in the audio
  1329. * stream's format, this will fail. This is a safety measure to make sure a
  1330. * race condition hasn't changed the format while this call is setting the
  1331. * channel map.
  1332. *
  1333. * Unlike attempting to change the stream's format, the output channel map on
  1334. * a stream bound to a recording device is permitted to change at any time;
  1335. * any data added to the stream after this call will have the new mapping, but
  1336. * previously-added data will still have the prior mapping. When the channel
  1337. * map doesn't match the hardware's channel layout, SDL will convert the data
  1338. * before feeding it to the device for playback.
  1339. *
  1340. * \param stream the SDL_AudioStream to change.
  1341. * \param chmap the new channel map, NULL to reset to default.
  1342. * \param count The number of channels in the map.
  1343. * \returns true on success or false on failure; call SDL_GetError() for more
  1344. * information.
  1345. *
  1346. * \threadsafety It is safe to call this function from any thread, as it holds
  1347. * a stream-specific mutex while running. Don't change the
  1348. * stream's format to have a different number of channels from a
  1349. * a different thread at the same time, though!
  1350. *
  1351. * \since This function is available since SDL 3.2.0.
  1352. *
  1353. * \sa SDL_SetAudioStreamInputChannelMap
  1354. */
  1355. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamOutputChannelMap(SDL_AudioStream *stream, const int *chmap, int count);
  1356. /**
  1357. * Add data to the stream.
  1358. *
  1359. * This data must match the format/channels/samplerate specified in the latest
  1360. * call to SDL_SetAudioStreamFormat, or the format specified when creating the
  1361. * stream if it hasn't been changed.
  1362. *
  1363. * Note that this call simply copies the unconverted data for later. This is
  1364. * different than SDL2, where data was converted during the Put call and the
  1365. * Get call would just dequeue the previously-converted data.
  1366. *
  1367. * \param stream the stream the audio data is being added to.
  1368. * \param buf a pointer to the audio data to add.
  1369. * \param len the number of bytes to write to the stream.
  1370. * \returns true on success or false on failure; call SDL_GetError() for more
  1371. * information.
  1372. *
  1373. * \threadsafety It is safe to call this function from any thread, but if the
  1374. * stream has a callback set, the caller might need to manage
  1375. * extra locking.
  1376. *
  1377. * \since This function is available since SDL 3.2.0.
  1378. *
  1379. * \sa SDL_ClearAudioStream
  1380. * \sa SDL_FlushAudioStream
  1381. * \sa SDL_GetAudioStreamData
  1382. * \sa SDL_GetAudioStreamQueued
  1383. */
  1384. extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
  1385. /**
  1386. * A callback that fires for completed SDL_PutAudioStreamDataNoCopy() data.
  1387. *
  1388. * When using SDL_PutAudioStreamDataNoCopy() to provide data to an
  1389. * SDL_AudioStream, it's not safe to dispose of the data until the stream has
  1390. * completely consumed it. Often times it's difficult to know exactly when
  1391. * this has happened.
  1392. *
  1393. * This callback fires once when the stream no longer needs the buffer,
  1394. * allowing the app to easily free or reuse it.
  1395. *
  1396. * \param userdata an opaque pointer provided by the app for their personal
  1397. * use.
  1398. * \param buf the pointer provided to SDL_PutAudioStreamDataNoCopy().
  1399. * \param buflen the size of buffer, in bytes, provided to
  1400. * SDL_PutAudioStreamDataNoCopy().
  1401. *
  1402. * \threadsafety This callbacks may run from any thread, so if you need to
  1403. * protect shared data, you should use SDL_LockAudioStream to
  1404. * serialize access; this lock will be held before your callback
  1405. * is called, so your callback does not need to manage the lock
  1406. * explicitly.
  1407. *
  1408. * \since This datatype is available since SDL 3.4.0.
  1409. *
  1410. * \sa SDL_SetAudioStreamGetCallback
  1411. * \sa SDL_SetAudioStreamPutCallback
  1412. */
  1413. typedef void (SDLCALL *SDL_AudioStreamDataCompleteCallback)(void *userdata, const void *buf, int buflen);
  1414. /**
  1415. * Add external data to an audio stream without copying it.
  1416. *
  1417. * Unlike SDL_PutAudioStreamData(), this function does not make a copy of the
  1418. * provided data, instead storing the provided pointer. This means that the
  1419. * put operation does not need to allocate and copy the data, but the original
  1420. * data must remain available until the stream is done with it, either by
  1421. * being read from the stream in its entirety, or a call to
  1422. * SDL_ClearAudioStream() or SDL_DestroyAudioStream().
  1423. *
  1424. * The data must match the format/channels/samplerate specified in the latest
  1425. * call to SDL_SetAudioStreamFormat, or the format specified when creating the
  1426. * stream if it hasn't been changed.
  1427. *
  1428. * An optional callback may be provided, which is called when the stream no
  1429. * longer needs the data. Once this callback fires, the stream will not access
  1430. * the data again. This callback will fire for any reason the data is no
  1431. * longer needed, including clearing or destroying the stream.
  1432. *
  1433. * Note that there is still an allocation to store tracking information, so
  1434. * this function is more efficient for larger blocks of data. If you're
  1435. * planning to put a few samples at a time, it will be more efficient to use
  1436. * SDL_PutAudioStreamData(), which allocates and buffers in blocks.
  1437. *
  1438. * \param stream the stream the audio data is being added to.
  1439. * \param buf a pointer to the audio data to add.
  1440. * \param len the number of bytes to add to the stream.
  1441. * \param callback the callback function to call when the data is no longer
  1442. * needed by the stream. May be NULL.
  1443. * \param userdata an opaque pointer provided to the callback for its own
  1444. * personal use.
  1445. * \returns true on success or false on failure; call SDL_GetError() for more
  1446. * information.
  1447. *
  1448. * \threadsafety It is safe to call this function from any thread, but if the
  1449. * stream has a callback set, the caller might need to manage
  1450. * extra locking.
  1451. *
  1452. * \since This function is available since SDL 3.4.0.
  1453. *
  1454. * \sa SDL_ClearAudioStream
  1455. * \sa SDL_FlushAudioStream
  1456. * \sa SDL_GetAudioStreamData
  1457. * \sa SDL_GetAudioStreamQueued
  1458. */
  1459. extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamDataNoCopy(SDL_AudioStream *stream, const void *buf, int len, SDL_AudioStreamDataCompleteCallback callback, void *userdata);
  1460. /**
  1461. * Add data to the stream with each channel in a separate array.
  1462. *
  1463. * This data must match the format/channels/samplerate specified in the latest
  1464. * call to SDL_SetAudioStreamFormat, or the format specified when creating the
  1465. * stream if it hasn't been changed.
  1466. *
  1467. * The data will be interleaved and queued. Note that SDL_AudioStream only
  1468. * operates on interleaved data, so this is simply a convenience function for
  1469. * easily queueing data from sources that provide separate arrays. There is no
  1470. * equivalent function to retrieve planar data.
  1471. *
  1472. * The arrays in `channel_buffers` are ordered as they are to be interleaved;
  1473. * the first array will be the first sample in the interleaved data. Any
  1474. * individual array may be NULL; in this case, silence will be interleaved for
  1475. * that channel.
  1476. *
  1477. * `num_channels` specifies how many arrays are in `channel_buffers`. This can
  1478. * be used as a safety to prevent overflow, in case the stream format has
  1479. * changed elsewhere. If more channels are specified than the current input
  1480. * spec, they are ignored. If less channels are specified, the missing arrays
  1481. * are treated as if they are NULL (silence is written to those channels). If
  1482. * the count is -1, SDL will assume the array count matches the current input
  1483. * spec.
  1484. *
  1485. * Note that `num_samples` is the number of _samples per array_. This can also
  1486. * be thought of as the number of _sample frames_ to be queued. A value of 1
  1487. * with stereo arrays will queue two samples to the stream. This is different
  1488. * than SDL_PutAudioStreamData, which wants the size of a single array in
  1489. * bytes.
  1490. *
  1491. * \param stream the stream the audio data is being added to.
  1492. * \param channel_buffers a pointer to an array of arrays, one array per
  1493. * channel.
  1494. * \param num_channels the number of arrays in `channel_buffers` or -1.
  1495. * \param num_samples the number of _samples_ per array to write to the
  1496. * stream.
  1497. * \returns true on success or false on failure; call SDL_GetError() for more
  1498. * information.
  1499. *
  1500. * \threadsafety It is safe to call this function from any thread, but if the
  1501. * stream has a callback set, the caller might need to manage
  1502. * extra locking.
  1503. *
  1504. * \since This function is available since SDL 3.4.0.
  1505. *
  1506. * \sa SDL_ClearAudioStream
  1507. * \sa SDL_FlushAudioStream
  1508. * \sa SDL_GetAudioStreamData
  1509. * \sa SDL_GetAudioStreamQueued
  1510. */
  1511. extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void * const *channel_buffers, int num_channels, int num_samples);
  1512. /**
  1513. * Get converted/resampled data from the stream.
  1514. *
  1515. * The input/output data format/channels/samplerate is specified when creating
  1516. * the stream, and can be changed after creation by calling
  1517. * SDL_SetAudioStreamFormat.
  1518. *
  1519. * Note that any conversion and resampling necessary is done during this call,
  1520. * and SDL_PutAudioStreamData simply queues unconverted data for later. This
  1521. * is different than SDL2, where that work was done while inputting new data
  1522. * to the stream and requesting the output just copied the converted data.
  1523. *
  1524. * \param stream the stream the audio is being requested from.
  1525. * \param buf a buffer to fill with audio data.
  1526. * \param len the maximum number of bytes to fill.
  1527. * \returns the number of bytes read from the stream or -1 on failure; call
  1528. * SDL_GetError() for more information.
  1529. *
  1530. * \threadsafety It is safe to call this function from any thread, but if the
  1531. * stream has a callback set, the caller might need to manage
  1532. * extra locking.
  1533. *
  1534. * \since This function is available since SDL 3.2.0.
  1535. *
  1536. * \sa SDL_ClearAudioStream
  1537. * \sa SDL_GetAudioStreamAvailable
  1538. * \sa SDL_PutAudioStreamData
  1539. */
  1540. extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
  1541. /**
  1542. * Get the number of converted/resampled bytes available.
  1543. *
  1544. * The stream may be buffering data behind the scenes until it has enough to
  1545. * resample correctly, so this number might be lower than what you expect, or
  1546. * even be zero. Add more data or flush the stream if you need the data now.
  1547. *
  1548. * If the stream has so much data that it would overflow an int, the return
  1549. * value is clamped to a maximum value, but no queued data is lost; if there
  1550. * are gigabytes of data queued, the app might need to read some of it with
  1551. * SDL_GetAudioStreamData before this function's return value is no longer
  1552. * clamped.
  1553. *
  1554. * \param stream the audio stream to query.
  1555. * \returns the number of converted/resampled bytes available or -1 on
  1556. * failure; call SDL_GetError() for more information.
  1557. *
  1558. * \threadsafety It is safe to call this function from any thread.
  1559. *
  1560. * \since This function is available since SDL 3.2.0.
  1561. *
  1562. * \sa SDL_GetAudioStreamData
  1563. * \sa SDL_PutAudioStreamData
  1564. */
  1565. extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
  1566. /**
  1567. * Get the number of bytes currently queued.
  1568. *
  1569. * This is the number of bytes put into a stream as input, not the number that
  1570. * can be retrieved as output. Because of several details, it's not possible
  1571. * to calculate one number directly from the other. If you need to know how
  1572. * much usable data can be retrieved right now, you should use
  1573. * SDL_GetAudioStreamAvailable() and not this function.
  1574. *
  1575. * Note that audio streams can change their input format at any time, even if
  1576. * there is still data queued in a different format, so the returned byte
  1577. * count will not necessarily match the number of _sample frames_ available.
  1578. * Users of this API should be aware of format changes they make when feeding
  1579. * a stream and plan accordingly.
  1580. *
  1581. * Queued data is not converted until it is consumed by
  1582. * SDL_GetAudioStreamData, so this value should be representative of the exact
  1583. * data that was put into the stream.
  1584. *
  1585. * If the stream has so much data that it would overflow an int, the return
  1586. * value is clamped to a maximum value, but no queued data is lost; if there
  1587. * are gigabytes of data queued, the app might need to read some of it with
  1588. * SDL_GetAudioStreamData before this function's return value is no longer
  1589. * clamped.
  1590. *
  1591. * \param stream the audio stream to query.
  1592. * \returns the number of bytes queued or -1 on failure; call SDL_GetError()
  1593. * for more information.
  1594. *
  1595. * \threadsafety It is safe to call this function from any thread.
  1596. *
  1597. * \since This function is available since SDL 3.2.0.
  1598. *
  1599. * \sa SDL_PutAudioStreamData
  1600. * \sa SDL_ClearAudioStream
  1601. */
  1602. extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamQueued(SDL_AudioStream *stream);
  1603. /**
  1604. * Tell the stream that you're done sending data, and anything being buffered
  1605. * should be converted/resampled and made available immediately.
  1606. *
  1607. * It is legal to add more data to a stream after flushing, but there may be
  1608. * audio gaps in the output. Generally this is intended to signal the end of
  1609. * input, so the complete output becomes available.
  1610. *
  1611. * \param stream the audio stream to flush.
  1612. * \returns true on success or false on failure; call SDL_GetError() for more
  1613. * information.
  1614. *
  1615. * \threadsafety It is safe to call this function from any thread.
  1616. *
  1617. * \since This function is available since SDL 3.2.0.
  1618. *
  1619. * \sa SDL_PutAudioStreamData
  1620. */
  1621. extern SDL_DECLSPEC bool SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
  1622. /**
  1623. * Clear any pending data in the stream.
  1624. *
  1625. * This drops any queued data, so there will be nothing to read from the
  1626. * stream until more is added.
  1627. *
  1628. * \param stream the audio stream to clear.
  1629. * \returns true on success or false on failure; call SDL_GetError() for more
  1630. * information.
  1631. *
  1632. * \threadsafety It is safe to call this function from any thread.
  1633. *
  1634. * \since This function is available since SDL 3.2.0.
  1635. *
  1636. * \sa SDL_GetAudioStreamAvailable
  1637. * \sa SDL_GetAudioStreamData
  1638. * \sa SDL_GetAudioStreamQueued
  1639. * \sa SDL_PutAudioStreamData
  1640. */
  1641. extern SDL_DECLSPEC bool SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
  1642. /**
  1643. * Use this function to pause audio playback on the audio device associated
  1644. * with an audio stream.
  1645. *
  1646. * This function pauses audio processing for a given device. Any bound audio
  1647. * streams will not progress, and no audio will be generated. Pausing one
  1648. * device does not prevent other unpaused devices from running.
  1649. *
  1650. * Pausing a device can be useful to halt all audio without unbinding all the
  1651. * audio streams. This might be useful while a game is paused, or a level is
  1652. * loading, etc.
  1653. *
  1654. * \param stream the audio stream associated with the audio device to pause.
  1655. * \returns true on success or false on failure; call SDL_GetError() for more
  1656. * information.
  1657. *
  1658. * \threadsafety It is safe to call this function from any thread.
  1659. *
  1660. * \since This function is available since SDL 3.2.0.
  1661. *
  1662. * \sa SDL_ResumeAudioStreamDevice
  1663. */
  1664. extern SDL_DECLSPEC bool SDLCALL SDL_PauseAudioStreamDevice(SDL_AudioStream *stream);
  1665. /**
  1666. * Use this function to unpause audio playback on the audio device associated
  1667. * with an audio stream.
  1668. *
  1669. * This function unpauses audio processing for a given device that has
  1670. * previously been paused. Once unpaused, any bound audio streams will begin
  1671. * to progress again, and audio can be generated.
  1672. *
  1673. * SDL_OpenAudioDeviceStream opens audio devices in a paused state, so this
  1674. * function call is required for audio playback to begin on such devices.
  1675. *
  1676. * \param stream the audio stream associated with the audio device to resume.
  1677. * \returns true on success or false on failure; call SDL_GetError() for more
  1678. * information.
  1679. *
  1680. * \threadsafety It is safe to call this function from any thread.
  1681. *
  1682. * \since This function is available since SDL 3.2.0.
  1683. *
  1684. * \sa SDL_PauseAudioStreamDevice
  1685. */
  1686. extern SDL_DECLSPEC bool SDLCALL SDL_ResumeAudioStreamDevice(SDL_AudioStream *stream);
  1687. /**
  1688. * Use this function to query if an audio device associated with a stream is
  1689. * paused.
  1690. *
  1691. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  1692. * has to bind a stream before any audio will flow.
  1693. *
  1694. * \param stream the audio stream associated with the audio device to query.
  1695. * \returns true if device is valid and paused, false otherwise.
  1696. *
  1697. * \threadsafety It is safe to call this function from any thread.
  1698. *
  1699. * \since This function is available since SDL 3.2.0.
  1700. *
  1701. * \sa SDL_PauseAudioStreamDevice
  1702. * \sa SDL_ResumeAudioStreamDevice
  1703. */
  1704. extern SDL_DECLSPEC bool SDLCALL SDL_AudioStreamDevicePaused(SDL_AudioStream *stream);
  1705. /**
  1706. * Lock an audio stream for serialized access.
  1707. *
  1708. * Each SDL_AudioStream has an internal mutex it uses to protect its data
  1709. * structures from threading conflicts. This function allows an app to lock
  1710. * that mutex, which could be useful if registering callbacks on this stream.
  1711. *
  1712. * One does not need to lock a stream to use in it most cases, as the stream
  1713. * manages this lock internally. However, this lock is held during callbacks,
  1714. * which may run from arbitrary threads at any time, so if an app needs to
  1715. * protect shared data during those callbacks, locking the stream guarantees
  1716. * that the callback is not running while the lock is held.
  1717. *
  1718. * As this is just a wrapper over SDL_LockMutex for an internal lock; it has
  1719. * all the same attributes (recursive locks are allowed, etc).
  1720. *
  1721. * \param stream the audio stream to lock.
  1722. * \returns true on success or false on failure; call SDL_GetError() for more
  1723. * information.
  1724. *
  1725. * \threadsafety It is safe to call this function from any thread.
  1726. *
  1727. * \since This function is available since SDL 3.2.0.
  1728. *
  1729. * \sa SDL_UnlockAudioStream
  1730. */
  1731. extern SDL_DECLSPEC bool SDLCALL SDL_LockAudioStream(SDL_AudioStream *stream);
  1732. /**
  1733. * Unlock an audio stream for serialized access.
  1734. *
  1735. * This unlocks an audio stream after a call to SDL_LockAudioStream.
  1736. *
  1737. * \param stream the audio stream to unlock.
  1738. * \returns true on success or false on failure; call SDL_GetError() for more
  1739. * information.
  1740. *
  1741. * \threadsafety You should only call this from the same thread that
  1742. * previously called SDL_LockAudioStream.
  1743. *
  1744. * \since This function is available since SDL 3.2.0.
  1745. *
  1746. * \sa SDL_LockAudioStream
  1747. */
  1748. extern SDL_DECLSPEC bool SDLCALL SDL_UnlockAudioStream(SDL_AudioStream *stream);
  1749. /**
  1750. * A callback that fires when data passes through an SDL_AudioStream.
  1751. *
  1752. * Apps can (optionally) register a callback with an audio stream that is
  1753. * called when data is added with SDL_PutAudioStreamData, or requested with
  1754. * SDL_GetAudioStreamData.
  1755. *
  1756. * Two values are offered here: one is the amount of additional data needed to
  1757. * satisfy the immediate request (which might be zero if the stream already
  1758. * has enough data queued) and the other is the total amount being requested.
  1759. * In a Get call triggering a Put callback, these values can be different. In
  1760. * a Put call triggering a Get callback, these values are always the same.
  1761. *
  1762. * Byte counts might be slightly overestimated due to buffering or resampling,
  1763. * and may change from call to call.
  1764. *
  1765. * This callback is not required to do anything. Generally this is useful for
  1766. * adding/reading data on demand, and the app will often put/get data as
  1767. * appropriate, but the system goes on with the data currently available to it
  1768. * if this callback does nothing.
  1769. *
  1770. * \param stream the SDL audio stream associated with this callback.
  1771. * \param additional_amount the amount of data, in bytes, that is needed right
  1772. * now.
  1773. * \param total_amount the total amount of data requested, in bytes, that is
  1774. * requested or available.
  1775. * \param userdata an opaque pointer provided by the app for their personal
  1776. * use.
  1777. *
  1778. * \threadsafety This callbacks may run from any thread, so if you need to
  1779. * protect shared data, you should use SDL_LockAudioStream to
  1780. * serialize access; this lock will be held before your callback
  1781. * is called, so your callback does not need to manage the lock
  1782. * explicitly.
  1783. *
  1784. * \since This datatype is available since SDL 3.2.0.
  1785. *
  1786. * \sa SDL_SetAudioStreamGetCallback
  1787. * \sa SDL_SetAudioStreamPutCallback
  1788. */
  1789. typedef void (SDLCALL *SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount);
  1790. /**
  1791. * Set a callback that runs when data is requested from an audio stream.
  1792. *
  1793. * This callback is called _before_ data is obtained from the stream, giving
  1794. * the callback the chance to add more on-demand.
  1795. *
  1796. * The callback can (optionally) call SDL_PutAudioStreamData() to add more
  1797. * audio to the stream during this call; if needed, the request that triggered
  1798. * this callback will obtain the new data immediately.
  1799. *
  1800. * The callback's `additional_amount` argument is roughly how many bytes of
  1801. * _unconverted_ data (in the stream's input format) is needed by the caller,
  1802. * although this may overestimate a little for safety. This takes into account
  1803. * how much is already in the stream and only asks for any extra necessary to
  1804. * resolve the request, which means the callback may be asked for zero bytes,
  1805. * and a different amount on each call.
  1806. *
  1807. * The callback is not required to supply exact amounts; it is allowed to
  1808. * supply too much or too little or none at all. The caller will get what's
  1809. * available, up to the amount they requested, regardless of this callback's
  1810. * outcome.
  1811. *
  1812. * Clearing or flushing an audio stream does not call this callback.
  1813. *
  1814. * This function obtains the stream's lock, which means any existing callback
  1815. * (get or put) in progress will finish running before setting the new
  1816. * callback.
  1817. *
  1818. * Setting a NULL function turns off the callback.
  1819. *
  1820. * \param stream the audio stream to set the new callback on.
  1821. * \param callback the new callback function to call when data is requested
  1822. * from the stream.
  1823. * \param userdata an opaque pointer provided to the callback for its own
  1824. * personal use.
  1825. * \returns true on success or false on failure; call SDL_GetError() for more
  1826. * information. This only fails if `stream` is NULL.
  1827. *
  1828. * \threadsafety It is safe to call this function from any thread.
  1829. *
  1830. * \since This function is available since SDL 3.2.0.
  1831. *
  1832. * \sa SDL_SetAudioStreamPutCallback
  1833. */
  1834. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
  1835. /**
  1836. * Set a callback that runs when data is added to an audio stream.
  1837. *
  1838. * This callback is called _after_ the data is added to the stream, giving the
  1839. * callback the chance to obtain it immediately.
  1840. *
  1841. * The callback can (optionally) call SDL_GetAudioStreamData() to obtain audio
  1842. * from the stream during this call.
  1843. *
  1844. * The callback's `additional_amount` argument is how many bytes of
  1845. * _converted_ data (in the stream's output format) was provided by the
  1846. * caller, although this may underestimate a little for safety. This value
  1847. * might be less than what is currently available in the stream, if data was
  1848. * already there, and might be less than the caller provided if the stream
  1849. * needs to keep a buffer to aid in resampling. Which means the callback may
  1850. * be provided with zero bytes, and a different amount on each call.
  1851. *
  1852. * The callback may call SDL_GetAudioStreamAvailable to see the total amount
  1853. * currently available to read from the stream, instead of the total provided
  1854. * by the current call.
  1855. *
  1856. * The callback is not required to obtain all data. It is allowed to read less
  1857. * or none at all. Anything not read now simply remains in the stream for
  1858. * later access.
  1859. *
  1860. * Clearing or flushing an audio stream does not call this callback.
  1861. *
  1862. * This function obtains the stream's lock, which means any existing callback
  1863. * (get or put) in progress will finish running before setting the new
  1864. * callback.
  1865. *
  1866. * Setting a NULL function turns off the callback.
  1867. *
  1868. * \param stream the audio stream to set the new callback on.
  1869. * \param callback the new callback function to call when data is added to the
  1870. * stream.
  1871. * \param userdata an opaque pointer provided to the callback for its own
  1872. * personal use.
  1873. * \returns true on success or false on failure; call SDL_GetError() for more
  1874. * information. This only fails if `stream` is NULL.
  1875. *
  1876. * \threadsafety It is safe to call this function from any thread.
  1877. *
  1878. * \since This function is available since SDL 3.2.0.
  1879. *
  1880. * \sa SDL_SetAudioStreamGetCallback
  1881. */
  1882. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
  1883. /**
  1884. * Free an audio stream.
  1885. *
  1886. * This will release all allocated data, including any audio that is still
  1887. * queued. You do not need to manually clear the stream first.
  1888. *
  1889. * If this stream was bound to an audio device, it is unbound during this
  1890. * call. If this stream was created with SDL_OpenAudioDeviceStream, the audio
  1891. * device that was opened alongside this stream's creation will be closed,
  1892. * too.
  1893. *
  1894. * \param stream the audio stream to destroy.
  1895. *
  1896. * \threadsafety It is safe to call this function from any thread.
  1897. *
  1898. * \since This function is available since SDL 3.2.0.
  1899. *
  1900. * \sa SDL_CreateAudioStream
  1901. */
  1902. extern SDL_DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
  1903. /**
  1904. * Convenience function for straightforward audio init for the common case.
  1905. *
  1906. * If all your app intends to do is provide a single source of PCM audio, this
  1907. * function allows you to do all your audio setup in a single call.
  1908. *
  1909. * This is also intended to be a clean means to migrate apps from SDL2.
  1910. *
  1911. * This function will open an audio device, create a stream and bind it.
  1912. * Unlike other methods of setup, the audio device will be closed when this
  1913. * stream is destroyed, so the app can treat the returned SDL_AudioStream as
  1914. * the only object needed to manage audio playback.
  1915. *
  1916. * Also unlike other functions, the audio device begins paused. This is to map
  1917. * more closely to SDL2-style behavior, since there is no extra step here to
  1918. * bind a stream to begin audio flowing. The audio device should be resumed
  1919. * with SDL_ResumeAudioStreamDevice().
  1920. *
  1921. * This function works with both playback and recording devices.
  1922. *
  1923. * The `spec` parameter represents the app's side of the audio stream. That
  1924. * is, for recording audio, this will be the output format, and for playing
  1925. * audio, this will be the input format. If spec is NULL, the system will
  1926. * choose the format, and the app can use SDL_GetAudioStreamFormat() to obtain
  1927. * this information later.
  1928. *
  1929. * If you don't care about opening a specific audio device, you can (and
  1930. * probably _should_), use SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK for playback and
  1931. * SDL_AUDIO_DEVICE_DEFAULT_RECORDING for recording.
  1932. *
  1933. * One can optionally provide a callback function; if NULL, the app is
  1934. * expected to queue audio data for playback (or unqueue audio data if
  1935. * capturing). Otherwise, the callback will begin to fire once the device is
  1936. * unpaused.
  1937. *
  1938. * Destroying the returned stream with SDL_DestroyAudioStream will also close
  1939. * the audio device associated with this stream.
  1940. *
  1941. * \param devid an audio device to open, or SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK
  1942. * or SDL_AUDIO_DEVICE_DEFAULT_RECORDING.
  1943. * \param spec the audio stream's data format. Can be NULL.
  1944. * \param callback a callback where the app will provide new data for
  1945. * playback, or receive new data for recording. Can be NULL,
  1946. * in which case the app will need to call
  1947. * SDL_PutAudioStreamData or SDL_GetAudioStreamData as
  1948. * necessary.
  1949. * \param userdata app-controlled pointer passed to callback. Can be NULL.
  1950. * Ignored if callback is NULL.
  1951. * \returns an audio stream on success, ready to use, or NULL on failure; call
  1952. * SDL_GetError() for more information. When done with this stream,
  1953. * call SDL_DestroyAudioStream to free resources and close the
  1954. * device.
  1955. *
  1956. * \threadsafety It is safe to call this function from any thread.
  1957. *
  1958. * \since This function is available since SDL 3.2.0.
  1959. *
  1960. * \sa SDL_GetAudioStreamDevice
  1961. * \sa SDL_ResumeAudioStreamDevice
  1962. */
  1963. extern SDL_DECLSPEC SDL_AudioStream * SDLCALL SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata);
  1964. /**
  1965. * A callback that fires when data is about to be fed to an audio device.
  1966. *
  1967. * This is useful for accessing the final mix, perhaps for writing a
  1968. * visualizer or applying a final effect to the audio data before playback.
  1969. *
  1970. * This callback should run as quickly as possible and not block for any
  1971. * significant time, as this callback delays submission of data to the audio
  1972. * device, which can cause audio playback problems.
  1973. *
  1974. * The postmix callback _must_ be able to handle any audio data format
  1975. * specified in `spec`, which can change between callbacks if the audio device
  1976. * changed. However, this only covers frequency and channel count; data is
  1977. * always provided here in SDL_AUDIO_F32 format.
  1978. *
  1979. * The postmix callback runs _after_ logical device gain and audiostream gain
  1980. * have been applied, which is to say you can make the output data louder at
  1981. * this point than the gain settings would suggest.
  1982. *
  1983. * \param userdata a pointer provided by the app through
  1984. * SDL_SetAudioPostmixCallback, for its own use.
  1985. * \param spec the current format of audio that is to be submitted to the
  1986. * audio device.
  1987. * \param buffer the buffer of audio samples to be submitted. The callback can
  1988. * inspect and/or modify this data.
  1989. * \param buflen the size of `buffer` in bytes.
  1990. *
  1991. * \threadsafety This will run from a background thread owned by SDL. The
  1992. * application is responsible for locking resources the callback
  1993. * touches that need to be protected.
  1994. *
  1995. * \since This datatype is available since SDL 3.2.0.
  1996. *
  1997. * \sa SDL_SetAudioPostmixCallback
  1998. */
  1999. typedef void (SDLCALL *SDL_AudioPostmixCallback)(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen);
  2000. /**
  2001. * Set a callback that fires when data is about to be fed to an audio device.
  2002. *
  2003. * This is useful for accessing the final mix, perhaps for writing a
  2004. * visualizer or applying a final effect to the audio data before playback.
  2005. *
  2006. * The buffer is the final mix of all bound audio streams on an opened device;
  2007. * this callback will fire regularly for any device that is both opened and
  2008. * unpaused. If there is no new data to mix, either because no streams are
  2009. * bound to the device or all the streams are empty, this callback will still
  2010. * fire with the entire buffer set to silence.
  2011. *
  2012. * This callback is allowed to make changes to the data; the contents of the
  2013. * buffer after this call is what is ultimately passed along to the hardware.
  2014. *
  2015. * The callback is always provided the data in float format (values from -1.0f
  2016. * to 1.0f), but the number of channels or sample rate may be different than
  2017. * the format the app requested when opening the device; SDL might have had to
  2018. * manage a conversion behind the scenes, or the playback might have jumped to
  2019. * new physical hardware when a system default changed, etc. These details may
  2020. * change between calls. Accordingly, the size of the buffer might change
  2021. * between calls as well.
  2022. *
  2023. * This callback can run at any time, and from any thread; if you need to
  2024. * serialize access to your app's data, you should provide and use a mutex or
  2025. * other synchronization device.
  2026. *
  2027. * All of this to say: there are specific needs this callback can fulfill, but
  2028. * it is not the simplest interface. Apps should generally provide audio in
  2029. * their preferred format through an SDL_AudioStream and let SDL handle the
  2030. * difference.
  2031. *
  2032. * This function is extremely time-sensitive; the callback should do the least
  2033. * amount of work possible and return as quickly as it can. The longer the
  2034. * callback runs, the higher the risk of audio dropouts or other problems.
  2035. *
  2036. * This function will block until the audio device is in between iterations,
  2037. * so any existing callback that might be running will finish before this
  2038. * function sets the new callback and returns.
  2039. *
  2040. * Setting a NULL callback function disables any previously-set callback.
  2041. *
  2042. * \param devid the ID of an opened audio device.
  2043. * \param callback a callback function to be called. Can be NULL.
  2044. * \param userdata app-controlled pointer passed to callback. Can be NULL.
  2045. * \returns true on success or false on failure; call SDL_GetError() for more
  2046. * information.
  2047. *
  2048. * \threadsafety It is safe to call this function from any thread.
  2049. *
  2050. * \since This function is available since SDL 3.2.0.
  2051. */
  2052. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata);
  2053. /**
  2054. * Load the audio data of a WAVE file into memory.
  2055. *
  2056. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  2057. * be valid pointers. The entire data portion of the file is then loaded into
  2058. * memory and decoded if necessary.
  2059. *
  2060. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  2061. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  2062. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  2063. * cause an error.
  2064. *
  2065. * If this function succeeds, the return value is zero and the pointer to the
  2066. * audio data allocated by the function is written to `audio_buf` and its
  2067. * length in bytes to `audio_len`. The SDL_AudioSpec members `freq`,
  2068. * `channels`, and `format` are set to the values of the audio data in the
  2069. * buffer.
  2070. *
  2071. * It's necessary to use SDL_free() to free the audio data returned in
  2072. * `audio_buf` when it is no longer used.
  2073. *
  2074. * Because of the underspecification of the .WAV format, there are many
  2075. * problematic files in the wild that cause issues with strict decoders. To
  2076. * provide compatibility with these files, this decoder is lenient in regards
  2077. * to the truncation of the file, the fact chunk, and the size of the RIFF
  2078. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  2079. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  2080. * tune the behavior of the loading process.
  2081. *
  2082. * Any file that is invalid (due to truncation, corruption, or wrong values in
  2083. * the headers), too big, or unsupported causes an error. Additionally, any
  2084. * critical I/O error from the data source will terminate the loading process
  2085. * with an error. The function returns NULL on error and in all cases (with
  2086. * the exception of `src` being NULL), an appropriate error message will be
  2087. * set.
  2088. *
  2089. * It is required that the data source supports seeking.
  2090. *
  2091. * Example:
  2092. *
  2093. * ```c
  2094. * SDL_LoadWAV_IO(SDL_IOFromFile("sample.wav", "rb"), true, &spec, &buf, &len);
  2095. * ```
  2096. *
  2097. * Note that the SDL_LoadWAV function does this same thing for you, but in a
  2098. * less messy way:
  2099. *
  2100. * ```c
  2101. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  2102. * ```
  2103. *
  2104. * \param src the data source for the WAVE data.
  2105. * \param closeio if true, calls SDL_CloseIO() on `src` before returning, even
  2106. * in the case of an error.
  2107. * \param spec a pointer to an SDL_AudioSpec that will be set to the WAVE
  2108. * data's format details on successful return.
  2109. * \param audio_buf a pointer filled with the audio data, allocated by the
  2110. * function.
  2111. * \param audio_len a pointer filled with the length of the audio data buffer
  2112. * in bytes.
  2113. * \returns true on success. `audio_buf` will be filled with a pointer to an
  2114. * allocated buffer containing the audio data, and `audio_len` is
  2115. * filled with the length of that audio buffer in bytes.
  2116. *
  2117. * This function returns false if the .WAV file cannot be opened,
  2118. * uses an unknown data format, or is corrupt; call SDL_GetError()
  2119. * for more information.
  2120. *
  2121. * When the application is done with the data returned in
  2122. * `audio_buf`, it should call SDL_free() to dispose of it.
  2123. *
  2124. * \threadsafety It is safe to call this function from any thread.
  2125. *
  2126. * \since This function is available since SDL 3.2.0.
  2127. *
  2128. * \sa SDL_free
  2129. * \sa SDL_LoadWAV
  2130. */
  2131. extern SDL_DECLSPEC bool SDLCALL SDL_LoadWAV_IO(SDL_IOStream *src, bool closeio, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);
  2132. /**
  2133. * Loads a WAV from a file path.
  2134. *
  2135. * This is a convenience function that is effectively the same as:
  2136. *
  2137. * ```c
  2138. * SDL_LoadWAV_IO(SDL_IOFromFile(path, "rb"), true, spec, audio_buf, audio_len);
  2139. * ```
  2140. *
  2141. * \param path the file path of the WAV file to open.
  2142. * \param spec a pointer to an SDL_AudioSpec that will be set to the WAVE
  2143. * data's format details on successful return.
  2144. * \param audio_buf a pointer filled with the audio data, allocated by the
  2145. * function.
  2146. * \param audio_len a pointer filled with the length of the audio data buffer
  2147. * in bytes.
  2148. * \returns true on success. `audio_buf` will be filled with a pointer to an
  2149. * allocated buffer containing the audio data, and `audio_len` is
  2150. * filled with the length of that audio buffer in bytes.
  2151. *
  2152. * This function returns false if the .WAV file cannot be opened,
  2153. * uses an unknown data format, or is corrupt; call SDL_GetError()
  2154. * for more information.
  2155. *
  2156. * When the application is done with the data returned in
  2157. * `audio_buf`, it should call SDL_free() to dispose of it.
  2158. *
  2159. * \threadsafety It is safe to call this function from any thread.
  2160. *
  2161. * \since This function is available since SDL 3.2.0.
  2162. *
  2163. * \sa SDL_free
  2164. * \sa SDL_LoadWAV_IO
  2165. */
  2166. extern SDL_DECLSPEC bool SDLCALL SDL_LoadWAV(const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);
  2167. /**
  2168. * Mix audio data in a specified format.
  2169. *
  2170. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  2171. * it into `dst`, performing addition, volume adjustment, and overflow
  2172. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  2173. * `format` data.
  2174. *
  2175. * This is provided for convenience -- you can mix your own audio data.
  2176. *
  2177. * Do not use this function for mixing together more than two streams of
  2178. * sample data. The output from repeated application of this function may be
  2179. * distorted by clipping, because there is no accumulator with greater range
  2180. * than the input (not to mention this being an inefficient way of doing it).
  2181. *
  2182. * It is a common misconception that this function is required to write audio
  2183. * data to an output stream in an audio callback. While you can do that,
  2184. * SDL_MixAudio() is really only needed when you're mixing a single audio
  2185. * stream with a volume adjustment.
  2186. *
  2187. * \param dst the destination for the mixed audio.
  2188. * \param src the source audio buffer to be mixed.
  2189. * \param format the SDL_AudioFormat structure representing the desired audio
  2190. * format.
  2191. * \param len the length of the audio buffer in bytes.
  2192. * \param volume ranges from 0.0 - 1.0, and should be set to 1.0 for full
  2193. * audio volume.
  2194. * \returns true on success or false on failure; call SDL_GetError() for more
  2195. * information.
  2196. *
  2197. * \threadsafety It is safe to call this function from any thread.
  2198. *
  2199. * \since This function is available since SDL 3.2.0.
  2200. */
  2201. extern SDL_DECLSPEC bool SDLCALL SDL_MixAudio(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, float volume);
  2202. /**
  2203. * Convert some audio data of one format to another format.
  2204. *
  2205. * Please note that this function is for convenience, but should not be used
  2206. * to resample audio in blocks, as it will introduce audio artifacts on the
  2207. * boundaries. You should only use this function if you are converting audio
  2208. * data in its entirety in one call. If you want to convert audio in smaller
  2209. * chunks, use an SDL_AudioStream, which is designed for this situation.
  2210. *
  2211. * Internally, this function creates and destroys an SDL_AudioStream on each
  2212. * use, so it's also less efficient than using one directly, if you need to
  2213. * convert multiple times.
  2214. *
  2215. * \param src_spec the format details of the input audio.
  2216. * \param src_data the audio data to be converted.
  2217. * \param src_len the len of src_data.
  2218. * \param dst_spec the format details of the output audio.
  2219. * \param dst_data will be filled with a pointer to converted audio data,
  2220. * which should be freed with SDL_free(). On error, it will be
  2221. * NULL.
  2222. * \param dst_len will be filled with the len of dst_data.
  2223. * \returns true on success or false on failure; call SDL_GetError() for more
  2224. * information.
  2225. *
  2226. * \threadsafety It is safe to call this function from any thread.
  2227. *
  2228. * \since This function is available since SDL 3.2.0.
  2229. */
  2230. extern SDL_DECLSPEC bool SDLCALL SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec, const Uint8 *src_data, int src_len, const SDL_AudioSpec *dst_spec, Uint8 **dst_data, int *dst_len);
  2231. /**
  2232. * Get the human readable name of an audio format.
  2233. *
  2234. * \param format the audio format to query.
  2235. * \returns the human readable name of the specified audio format or
  2236. * "SDL_AUDIO_UNKNOWN" if the format isn't recognized.
  2237. *
  2238. * \threadsafety It is safe to call this function from any thread.
  2239. *
  2240. * \since This function is available since SDL 3.2.0.
  2241. */
  2242. extern SDL_DECLSPEC const char * SDLCALL SDL_GetAudioFormatName(SDL_AudioFormat format);
  2243. /**
  2244. * Get the appropriate memset value for silencing an audio format.
  2245. *
  2246. * The value returned by this function can be used as the second argument to
  2247. * memset (or SDL_memset) to set an audio buffer in a specific format to
  2248. * silence.
  2249. *
  2250. * \param format the audio data format to query.
  2251. * \returns a byte value that can be passed to memset.
  2252. *
  2253. * \threadsafety It is safe to call this function from any thread.
  2254. *
  2255. * \since This function is available since SDL 3.2.0.
  2256. */
  2257. extern SDL_DECLSPEC int SDLCALL SDL_GetSilenceValueForFormat(SDL_AudioFormat format);
  2258. /* Ends C function definitions when using C++ */
  2259. #ifdef __cplusplus
  2260. }
  2261. #endif
  2262. #include <SDL3/SDL_close_code.h>
  2263. #endif /* SDL_audio_h_ */