SDL_audio.h 95 KB

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