SDL_audio.h 81 KB

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