SDL_audio.h 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. * \param devid the instance ID of the device to query.
  543. * \returns the name of the audio device, or NULL on failure; call
  544. * SDL_GetError() for more information.
  545. *
  546. * \threadsafety It is safe to call this function from any thread.
  547. *
  548. * \since This function is available since SDL 3.2.0.
  549. *
  550. * \sa SDL_GetAudioPlaybackDevices
  551. * \sa SDL_GetAudioRecordingDevices
  552. */
  553. extern SDL_DECLSPEC const char * SDLCALL SDL_GetAudioDeviceName(SDL_AudioDeviceID devid);
  554. /**
  555. * Get the current audio format of a specific audio device.
  556. *
  557. * For an opened device, this will report the format the device is currently
  558. * using. If the device isn't yet opened, this will report the device's
  559. * preferred format (or a reasonable default if this can't be determined).
  560. *
  561. * You may also specify SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or
  562. * SDL_AUDIO_DEVICE_DEFAULT_RECORDING here, which is useful for getting a
  563. * reasonable recommendation before opening the system-recommended default
  564. * device.
  565. *
  566. * You can also use this to request the current device buffer size. This is
  567. * specified in sample frames and represents the amount of data SDL will feed
  568. * to the physical hardware in each chunk. This can be converted to
  569. * milliseconds of audio with the following equation:
  570. *
  571. * `ms = (int) ((((Sint64) frames) * 1000) / spec.freq);`
  572. *
  573. * Buffer size is only important if you need low-level control over the audio
  574. * playback timing. Most apps do not need this.
  575. *
  576. * \param devid the instance ID of the device to query.
  577. * \param spec on return, will be filled with device details.
  578. * \param sample_frames pointer to store device buffer size, in sample frames.
  579. * Can be NULL.
  580. * \returns true on success or false on failure; call SDL_GetError() for more
  581. * information.
  582. *
  583. * \threadsafety It is safe to call this function from any thread.
  584. *
  585. * \since This function is available since SDL 3.2.0.
  586. */
  587. extern SDL_DECLSPEC bool SDLCALL SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec, int *sample_frames);
  588. /**
  589. * Get the current channel map of an audio device.
  590. *
  591. * Channel maps are optional; most things do not need them, instead passing
  592. * data in the [order that SDL expects](CategoryAudio#channel-layouts).
  593. *
  594. * Audio devices usually have no remapping applied. This is represented by
  595. * returning NULL, and does not signify an error.
  596. *
  597. * \param devid the instance ID of the device to query.
  598. * \param count On output, set to number of channels in the map. Can be NULL.
  599. * \returns an array of the current channel mapping, with as many elements as
  600. * the current output spec's channels, or NULL if default. This
  601. * should be freed with SDL_free() when it is no longer needed.
  602. *
  603. * \threadsafety It is safe to call this function from any thread.
  604. *
  605. * \since This function is available since SDL 3.2.0.
  606. *
  607. * \sa SDL_SetAudioStreamInputChannelMap
  608. */
  609. extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioDeviceChannelMap(SDL_AudioDeviceID devid, int *count);
  610. /**
  611. * Open a specific audio device.
  612. *
  613. * You can open both playback and recording devices through this function.
  614. * Playback devices will take data from bound audio streams, mix it, and send
  615. * it to the hardware. Recording devices will feed any bound audio streams
  616. * with a copy of any incoming data.
  617. *
  618. * An opened audio device starts out with no audio streams bound. To start
  619. * audio playing, bind a stream and supply audio data to it. Unlike SDL2,
  620. * there is no audio callback; you only bind audio streams and make sure they
  621. * have data flowing into them (however, you can simulate SDL2's semantics
  622. * fairly closely by using SDL_OpenAudioDeviceStream instead of this
  623. * function).
  624. *
  625. * If you don't care about opening a specific device, pass a `devid` of either
  626. * `SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK` or
  627. * `SDL_AUDIO_DEVICE_DEFAULT_RECORDING`. In this case, SDL will try to pick
  628. * the most reasonable default, and may also switch between physical devices
  629. * seamlessly later, if the most reasonable default changes during the
  630. * lifetime of this opened device (user changed the default in the OS's system
  631. * preferences, the default got unplugged so the system jumped to a new
  632. * default, the user plugged in headphones on a mobile device, etc). Unless
  633. * you have a good reason to choose a specific device, this is probably what
  634. * you want.
  635. *
  636. * You may request a specific format for the audio device, but there is no
  637. * promise the device will honor that request for several reasons. As such,
  638. * it's only meant to be a hint as to what data your app will provide. Audio
  639. * streams will accept data in whatever format you specify and manage
  640. * conversion for you as appropriate. SDL_GetAudioDeviceFormat can tell you
  641. * the preferred format for the device before opening and the actual format
  642. * the device is using after opening.
  643. *
  644. * It's legal to open the same device ID more than once; each successful open
  645. * will generate a new logical SDL_AudioDeviceID that is managed separately
  646. * from others on the same physical device. This allows libraries to open a
  647. * device separately from the main app and bind its own streams without
  648. * conflicting.
  649. *
  650. * It is also legal to open a device ID returned by a previous call to this
  651. * function; doing so just creates another logical device on the same physical
  652. * device. This may be useful for making logical groupings of audio streams.
  653. *
  654. * This function returns the opened device ID on success. This is a new,
  655. * unique SDL_AudioDeviceID that represents a logical device.
  656. *
  657. * Some backends might offer arbitrary devices (for example, a networked audio
  658. * protocol that can connect to an arbitrary server). For these, as a change
  659. * from SDL2, you should open a default device ID and use an SDL hint to
  660. * specify the target if you care, or otherwise let the backend figure out a
  661. * reasonable default. Most backends don't offer anything like this, and often
  662. * this would be an end user setting an environment variable for their custom
  663. * need, and not something an application should specifically manage.
  664. *
  665. * When done with an audio device, possibly at the end of the app's life, one
  666. * should call SDL_CloseAudioDevice() on the returned device id.
  667. *
  668. * \param devid the device instance id to open, or
  669. * SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK or
  670. * SDL_AUDIO_DEVICE_DEFAULT_RECORDING for the most reasonable
  671. * default device.
  672. * \param spec the requested device configuration. Can be NULL to use
  673. * reasonable defaults.
  674. * \returns the device ID on success or 0 on failure; call SDL_GetError() for
  675. * more information.
  676. *
  677. * \threadsafety It is safe to call this function from any thread.
  678. *
  679. * \since This function is available since SDL 3.2.0.
  680. *
  681. * \sa SDL_CloseAudioDevice
  682. * \sa SDL_GetAudioDeviceFormat
  683. */
  684. extern SDL_DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec);
  685. /**
  686. * Determine if an audio device is physical (instead of logical).
  687. *
  688. * An SDL_AudioDeviceID that represents physical hardware is a physical
  689. * device; there is one for each piece of hardware that SDL can see. Logical
  690. * devices are created by calling SDL_OpenAudioDevice or
  691. * SDL_OpenAudioDeviceStream, and while each is associated with a physical
  692. * device, there can be any number of logical devices on one physical device.
  693. *
  694. * For the most part, logical and physical IDs are interchangeable--if you try
  695. * to open a logical device, SDL understands to assign that effort to the
  696. * underlying physical device, etc. However, it might be useful to know if an
  697. * arbitrary device ID is physical or logical. This function reports which.
  698. *
  699. * This function may return either true or false for invalid device IDs.
  700. *
  701. * \param devid the device ID to query.
  702. * \returns true if devid is a physical device, false if it is logical.
  703. *
  704. * \threadsafety It is safe to call this function from any thread.
  705. *
  706. * \since This function is available since SDL 3.2.0.
  707. */
  708. extern SDL_DECLSPEC bool SDLCALL SDL_IsAudioDevicePhysical(SDL_AudioDeviceID devid);
  709. /**
  710. * Determine if an audio device is a playback device (instead of recording).
  711. *
  712. * This function may return either true or false for invalid device IDs.
  713. *
  714. * \param devid the device ID to query.
  715. * \returns true if devid is a playback device, false if it is recording.
  716. *
  717. * \threadsafety It is safe to call this function from any thread.
  718. *
  719. * \since This function is available since SDL 3.2.0.
  720. */
  721. extern SDL_DECLSPEC bool SDLCALL SDL_IsAudioDevicePlayback(SDL_AudioDeviceID devid);
  722. /**
  723. * Use this function to pause audio playback on a specified device.
  724. *
  725. * This function pauses audio processing for a given device. Any bound audio
  726. * streams will not progress, and no audio will be generated. Pausing one
  727. * device does not prevent other unpaused devices from running.
  728. *
  729. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  730. * has to bind a stream before any audio will flow. Pausing a paused device is
  731. * a legal no-op.
  732. *
  733. * Pausing a device can be useful to halt all audio without unbinding all the
  734. * audio streams. This might be useful while a game is paused, or a level is
  735. * loading, etc.
  736. *
  737. * Physical devices can not be paused or unpaused, only logical devices
  738. * created through SDL_OpenAudioDevice() can be.
  739. *
  740. * \param devid a device opened by SDL_OpenAudioDevice().
  741. * \returns true on success or false on failure; call SDL_GetError() for more
  742. * information.
  743. *
  744. * \threadsafety It is safe to call this function from any thread.
  745. *
  746. * \since This function is available since SDL 3.2.0.
  747. *
  748. * \sa SDL_ResumeAudioDevice
  749. * \sa SDL_AudioDevicePaused
  750. */
  751. extern SDL_DECLSPEC bool SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID devid);
  752. /**
  753. * Use this function to unpause audio playback on a specified device.
  754. *
  755. * This function unpauses audio processing for a given device that has
  756. * previously been paused with SDL_PauseAudioDevice(). Once unpaused, any
  757. * bound audio streams will begin to progress again, and audio can be
  758. * generated.
  759. *
  760. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  761. * has to bind a stream before any audio will flow. Unpausing an unpaused
  762. * device is a legal no-op.
  763. *
  764. * Physical devices can not be paused or unpaused, only logical devices
  765. * created through SDL_OpenAudioDevice() can be.
  766. *
  767. * \param devid a device opened by SDL_OpenAudioDevice().
  768. * \returns true on success or false on failure; call SDL_GetError() for more
  769. * information.
  770. *
  771. * \threadsafety It is safe to call this function from any thread.
  772. *
  773. * \since This function is available since SDL 3.2.0.
  774. *
  775. * \sa SDL_AudioDevicePaused
  776. * \sa SDL_PauseAudioDevice
  777. */
  778. extern SDL_DECLSPEC bool SDLCALL SDL_ResumeAudioDevice(SDL_AudioDeviceID devid);
  779. /**
  780. * Use this function to query if an audio device is paused.
  781. *
  782. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  783. * has to bind a stream before any audio will flow.
  784. *
  785. * Physical devices can not be paused or unpaused, only logical devices
  786. * created through SDL_OpenAudioDevice() can be. Physical and invalid device
  787. * IDs will report themselves as unpaused here.
  788. *
  789. * \param devid a device opened by SDL_OpenAudioDevice().
  790. * \returns true if device is valid and paused, false otherwise.
  791. *
  792. * \threadsafety It is safe to call this function from any thread.
  793. *
  794. * \since This function is available since SDL 3.2.0.
  795. *
  796. * \sa SDL_PauseAudioDevice
  797. * \sa SDL_ResumeAudioDevice
  798. */
  799. extern SDL_DECLSPEC bool SDLCALL SDL_AudioDevicePaused(SDL_AudioDeviceID devid);
  800. /**
  801. * Get the gain of an audio device.
  802. *
  803. * The gain of a device is its volume; a larger gain means a louder output,
  804. * with a gain of zero being silence.
  805. *
  806. * Audio devices default to a gain of 1.0f (no change in output).
  807. *
  808. * Physical devices may not have their gain changed, only logical devices, and
  809. * this function will always return -1.0f when used on physical devices.
  810. *
  811. * \param devid the audio device to query.
  812. * \returns the gain of the device or -1.0f on failure; call SDL_GetError()
  813. * for more information.
  814. *
  815. * \threadsafety It is safe to call this function from any thread.
  816. *
  817. * \since This function is available since SDL 3.2.0.
  818. *
  819. * \sa SDL_SetAudioDeviceGain
  820. */
  821. extern SDL_DECLSPEC float SDLCALL SDL_GetAudioDeviceGain(SDL_AudioDeviceID devid);
  822. /**
  823. * Change the gain of an audio device.
  824. *
  825. * The gain of a device is its volume; a larger gain means a louder output,
  826. * with a gain of zero being silence.
  827. *
  828. * Audio devices default to a gain of 1.0f (no change in output).
  829. *
  830. * Physical devices may not have their gain changed, only logical devices, and
  831. * this function will always return false when used on physical devices. While
  832. * it might seem attractive to adjust several logical devices at once in this
  833. * way, it would allow an app or library to interfere with another portion of
  834. * the program's otherwise-isolated devices.
  835. *
  836. * This is applied, along with any per-audiostream gain, during playback to
  837. * the hardware, and can be continuously changed to create various effects. On
  838. * recording devices, this will adjust the gain before passing the data into
  839. * an audiostream; that recording audiostream can then adjust its gain further
  840. * when outputting the data elsewhere, if it likes, but that second gain is
  841. * not applied until the data leaves the audiostream again.
  842. *
  843. * \param devid the audio device on which to change gain.
  844. * \param gain the gain. 1.0f is no change, 0.0f is silence.
  845. * \returns true on success or false on failure; call SDL_GetError() for more
  846. * information.
  847. *
  848. * \threadsafety It is safe to call this function from any thread, as it holds
  849. * a stream-specific mutex while running.
  850. *
  851. * \since This function is available since SDL 3.2.0.
  852. *
  853. * \sa SDL_GetAudioDeviceGain
  854. */
  855. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioDeviceGain(SDL_AudioDeviceID devid, float gain);
  856. /**
  857. * Close a previously-opened audio device.
  858. *
  859. * The application should close open audio devices once they are no longer
  860. * needed.
  861. *
  862. * This function may block briefly while pending audio data is played by the
  863. * hardware, so that applications don't drop the last buffer of data they
  864. * supplied if terminating immediately afterwards.
  865. *
  866. * \param devid an audio device id previously returned by
  867. * SDL_OpenAudioDevice().
  868. *
  869. * \threadsafety It is safe to call this function from any thread.
  870. *
  871. * \since This function is available since SDL 3.2.0.
  872. *
  873. * \sa SDL_OpenAudioDevice
  874. */
  875. extern SDL_DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID devid);
  876. /**
  877. * Bind a list of audio streams to an audio device.
  878. *
  879. * Audio data will flow through any bound streams. For a playback device, data
  880. * for all bound streams will be mixed together and fed to the device. For a
  881. * recording device, a copy of recorded data will be provided to each bound
  882. * stream.
  883. *
  884. * Audio streams can only be bound to an open device. This operation is
  885. * atomic--all streams bound in the same call will start processing at the
  886. * same time, so they can stay in sync. Also: either all streams will be bound
  887. * or none of them will be.
  888. *
  889. * It is an error to bind an already-bound stream; it must be explicitly
  890. * unbound first.
  891. *
  892. * Binding a stream to a device will set its output format for playback
  893. * devices, and its input format for recording devices, so they match the
  894. * device's settings. The caller is welcome to change the other end of the
  895. * stream's format at any time with SDL_SetAudioStreamFormat().
  896. *
  897. * \param devid an audio device to bind a stream to.
  898. * \param streams an array of audio streams to bind.
  899. * \param num_streams number streams listed in the `streams` array.
  900. * \returns true on success or false on failure; call SDL_GetError() for more
  901. * information.
  902. *
  903. * \threadsafety It is safe to call this function from any thread.
  904. *
  905. * \since This function is available since SDL 3.2.0.
  906. *
  907. * \sa SDL_BindAudioStreams
  908. * \sa SDL_UnbindAudioStream
  909. * \sa SDL_GetAudioStreamDevice
  910. */
  911. extern SDL_DECLSPEC bool SDLCALL SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream * const *streams, int num_streams);
  912. /**
  913. * Bind a single audio stream to an audio device.
  914. *
  915. * This is a convenience function, equivalent to calling
  916. * `SDL_BindAudioStreams(devid, &stream, 1)`.
  917. *
  918. * \param devid an audio device to bind a stream to.
  919. * \param stream an audio stream to bind to a device.
  920. * \returns true on success or false on failure; call SDL_GetError() for more
  921. * information.
  922. *
  923. * \threadsafety It is safe to call this function from any thread.
  924. *
  925. * \since This function is available since SDL 3.2.0.
  926. *
  927. * \sa SDL_BindAudioStreams
  928. * \sa SDL_UnbindAudioStream
  929. * \sa SDL_GetAudioStreamDevice
  930. */
  931. extern SDL_DECLSPEC bool SDLCALL SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream);
  932. /**
  933. * Unbind a list of audio streams from their audio devices.
  934. *
  935. * The streams being unbound do not all have to be on the same device. All
  936. * streams on the same device will be unbound atomically (data will stop
  937. * flowing through all unbound streams on the same device at the same time).
  938. *
  939. * Unbinding a stream that isn't bound to a device is a legal no-op.
  940. *
  941. * \param streams an array of audio streams to unbind. Can be NULL or contain
  942. * NULL.
  943. * \param num_streams number streams listed in the `streams` array.
  944. *
  945. * \threadsafety It is safe to call this function from any thread.
  946. *
  947. * \since This function is available since SDL 3.2.0.
  948. *
  949. * \sa SDL_BindAudioStreams
  950. */
  951. extern SDL_DECLSPEC void SDLCALL SDL_UnbindAudioStreams(SDL_AudioStream * const *streams, int num_streams);
  952. /**
  953. * Unbind a single audio stream from its audio device.
  954. *
  955. * This is a convenience function, equivalent to calling
  956. * `SDL_UnbindAudioStreams(&stream, 1)`.
  957. *
  958. * \param stream an audio stream to unbind from a device. Can be NULL.
  959. *
  960. * \threadsafety It is safe to call this function from any thread.
  961. *
  962. * \since This function is available since SDL 3.2.0.
  963. *
  964. * \sa SDL_BindAudioStream
  965. */
  966. extern SDL_DECLSPEC void SDLCALL SDL_UnbindAudioStream(SDL_AudioStream *stream);
  967. /**
  968. * Query an audio stream for its currently-bound device.
  969. *
  970. * This reports the logical audio device that an audio stream is currently
  971. * bound to.
  972. *
  973. * If not bound, or invalid, this returns zero, which is not a valid device
  974. * ID.
  975. *
  976. * \param stream the audio stream to query.
  977. * \returns the bound audio device, or 0 if not bound or invalid.
  978. *
  979. * \threadsafety It is safe to call this function from any thread.
  980. *
  981. * \since This function is available since SDL 3.2.0.
  982. *
  983. * \sa SDL_BindAudioStream
  984. * \sa SDL_BindAudioStreams
  985. */
  986. extern SDL_DECLSPEC SDL_AudioDeviceID SDLCALL SDL_GetAudioStreamDevice(SDL_AudioStream *stream);
  987. /**
  988. * Create a new audio stream.
  989. *
  990. * \param src_spec the format details of the input audio.
  991. * \param dst_spec the format details of the output audio.
  992. * \returns a new audio stream on success or NULL on failure; call
  993. * SDL_GetError() for more information.
  994. *
  995. * \threadsafety It is safe to call this function from any thread.
  996. *
  997. * \since This function is available since SDL 3.2.0.
  998. *
  999. * \sa SDL_PutAudioStreamData
  1000. * \sa SDL_GetAudioStreamData
  1001. * \sa SDL_GetAudioStreamAvailable
  1002. * \sa SDL_FlushAudioStream
  1003. * \sa SDL_ClearAudioStream
  1004. * \sa SDL_SetAudioStreamFormat
  1005. * \sa SDL_DestroyAudioStream
  1006. */
  1007. extern SDL_DECLSPEC SDL_AudioStream * SDLCALL SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
  1008. /**
  1009. * Get the properties associated with an audio stream.
  1010. *
  1011. * The application can hang any data it wants here, but the following
  1012. * properties are understood by SDL:
  1013. *
  1014. * - `SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN`: if true (the default), the
  1015. * stream be automatically cleaned up when the audio subsystem quits. If set
  1016. * to false, the streams will persist beyond that. This property is ignored
  1017. * for streams created through SDL_OpenAudioDeviceStream(), and will always
  1018. * be cleaned up. Streams that are not cleaned up will still be unbound from
  1019. * devices when the audio subsystem quits. This property was added in SDL
  1020. * 3.4.0.
  1021. *
  1022. * \param stream the SDL_AudioStream to query.
  1023. * \returns a valid property ID on success or 0 on failure; call
  1024. * SDL_GetError() for more information.
  1025. *
  1026. * \threadsafety It is safe to call this function from any thread.
  1027. *
  1028. * \since This function is available since SDL 3.2.0.
  1029. */
  1030. extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetAudioStreamProperties(SDL_AudioStream *stream);
  1031. #define SDL_PROP_AUDIOSTREAM_AUTO_CLEANUP_BOOLEAN "SDL.audiostream.auto_cleanup"
  1032. /**
  1033. * Query the current format of an audio stream.
  1034. *
  1035. * \param stream the SDL_AudioStream to query.
  1036. * \param src_spec where to store the input audio format; ignored if NULL.
  1037. * \param dst_spec where to store the output audio format; ignored if NULL.
  1038. * \returns true on success or false on failure; call SDL_GetError() for more
  1039. * information.
  1040. *
  1041. * \threadsafety It is safe to call this function from any thread, as it holds
  1042. * a stream-specific mutex while running.
  1043. *
  1044. * \since This function is available since SDL 3.2.0.
  1045. *
  1046. * \sa SDL_SetAudioStreamFormat
  1047. */
  1048. extern SDL_DECLSPEC bool SDLCALL SDL_GetAudioStreamFormat(SDL_AudioStream *stream, SDL_AudioSpec *src_spec, SDL_AudioSpec *dst_spec);
  1049. /**
  1050. * Change the input and output formats of an audio stream.
  1051. *
  1052. * Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData
  1053. * will reflect the new format, and future calls to SDL_PutAudioStreamData
  1054. * must provide data in the new input formats.
  1055. *
  1056. * Data that was previously queued in the stream will still be operated on in
  1057. * the format that was current when it was added, which is to say you can put
  1058. * the end of a sound file in one format to a stream, change formats for the
  1059. * next sound file, and start putting that new data while the previous sound
  1060. * file is still queued, and everything will still play back correctly.
  1061. *
  1062. * If a stream is bound to a device, then the format of the side of the stream
  1063. * bound to a device cannot be changed (src_spec for recording devices,
  1064. * dst_spec for playback devices). Attempts to make a change to this side will
  1065. * be ignored, but this will not report an error. The other side's format can
  1066. * be changed.
  1067. *
  1068. * \param stream the stream the format is being changed.
  1069. * \param src_spec the new format of the audio input; if NULL, it is not
  1070. * changed.
  1071. * \param dst_spec the new format of the audio output; if NULL, it is not
  1072. * changed.
  1073. * \returns true on success or false on failure; call SDL_GetError() for more
  1074. * information.
  1075. *
  1076. * \threadsafety It is safe to call this function from any thread, as it holds
  1077. * a stream-specific mutex while running.
  1078. *
  1079. * \since This function is available since SDL 3.2.0.
  1080. *
  1081. * \sa SDL_GetAudioStreamFormat
  1082. * \sa SDL_SetAudioStreamFrequencyRatio
  1083. */
  1084. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream, const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
  1085. /**
  1086. * Get the frequency ratio of an audio stream.
  1087. *
  1088. * \param stream the SDL_AudioStream to query.
  1089. * \returns the frequency ratio of the stream or 0.0 on failure; call
  1090. * SDL_GetError() for more information.
  1091. *
  1092. * \threadsafety It is safe to call this function from any thread, as it holds
  1093. * a stream-specific mutex while running.
  1094. *
  1095. * \since This function is available since SDL 3.2.0.
  1096. *
  1097. * \sa SDL_SetAudioStreamFrequencyRatio
  1098. */
  1099. extern SDL_DECLSPEC float SDLCALL SDL_GetAudioStreamFrequencyRatio(SDL_AudioStream *stream);
  1100. /**
  1101. * Change the frequency ratio of an audio stream.
  1102. *
  1103. * The frequency ratio is used to adjust the rate at which input data is
  1104. * consumed. Changing this effectively modifies the speed and pitch of the
  1105. * audio. A value greater than 1.0f will play the audio faster, and at a
  1106. * higher pitch. A value less than 1.0f will play the audio slower, and at a
  1107. * lower pitch. 1.0f means play at normal speed.
  1108. *
  1109. * This is applied during SDL_GetAudioStreamData, and can be continuously
  1110. * changed to create various effects.
  1111. *
  1112. * \param stream the stream on which the frequency ratio is being changed.
  1113. * \param ratio the frequency ratio. 1.0 is normal speed. Must be between 0.01
  1114. * and 100.
  1115. * \returns true on success or false on failure; call SDL_GetError() for more
  1116. * information.
  1117. *
  1118. * \threadsafety It is safe to call this function from any thread, as it holds
  1119. * a stream-specific mutex while running.
  1120. *
  1121. * \since This function is available since SDL 3.2.0.
  1122. *
  1123. * \sa SDL_GetAudioStreamFrequencyRatio
  1124. * \sa SDL_SetAudioStreamFormat
  1125. */
  1126. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamFrequencyRatio(SDL_AudioStream *stream, float ratio);
  1127. /**
  1128. * Get the gain of an audio stream.
  1129. *
  1130. * The gain of a stream is its volume; a larger gain means a louder output,
  1131. * with a gain of zero being silence.
  1132. *
  1133. * Audio streams default to a gain of 1.0f (no change in output).
  1134. *
  1135. * \param stream the SDL_AudioStream to query.
  1136. * \returns the gain of the stream or -1.0f on failure; call SDL_GetError()
  1137. * for more information.
  1138. *
  1139. * \threadsafety It is safe to call this function from any thread, as it holds
  1140. * a stream-specific mutex while running.
  1141. *
  1142. * \since This function is available since SDL 3.2.0.
  1143. *
  1144. * \sa SDL_SetAudioStreamGain
  1145. */
  1146. extern SDL_DECLSPEC float SDLCALL SDL_GetAudioStreamGain(SDL_AudioStream *stream);
  1147. /**
  1148. * Change the gain of an audio stream.
  1149. *
  1150. * The gain of a stream is its volume; a larger gain means a louder output,
  1151. * with a gain of zero being silence.
  1152. *
  1153. * Audio streams default to a gain of 1.0f (no change in output).
  1154. *
  1155. * This is applied during SDL_GetAudioStreamData, and can be continuously
  1156. * changed to create various effects.
  1157. *
  1158. * \param stream the stream on which the gain is being changed.
  1159. * \param gain the gain. 1.0f is no change, 0.0f is silence.
  1160. * \returns true on success or false on failure; call SDL_GetError() for more
  1161. * information.
  1162. *
  1163. * \threadsafety It is safe to call this function from any thread, as it holds
  1164. * a stream-specific mutex while running.
  1165. *
  1166. * \since This function is available since SDL 3.2.0.
  1167. *
  1168. * \sa SDL_GetAudioStreamGain
  1169. */
  1170. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamGain(SDL_AudioStream *stream, float gain);
  1171. /**
  1172. * Get the current input channel map of an audio stream.
  1173. *
  1174. * Channel maps are optional; most things do not need them, instead passing
  1175. * data in the [order that SDL expects](CategoryAudio#channel-layouts).
  1176. *
  1177. * Audio streams default to no remapping applied. This is represented by
  1178. * returning NULL, and does not signify an error.
  1179. *
  1180. * \param stream the SDL_AudioStream to query.
  1181. * \param count On output, set to number of channels in the map. Can be NULL.
  1182. * \returns an array of the current channel mapping, with as many elements as
  1183. * the current output spec's channels, or NULL if default. This
  1184. * should be freed with SDL_free() when it is no longer needed.
  1185. *
  1186. * \threadsafety It is safe to call this function from any thread, as it holds
  1187. * a stream-specific mutex while running.
  1188. *
  1189. * \since This function is available since SDL 3.2.0.
  1190. *
  1191. * \sa SDL_SetAudioStreamInputChannelMap
  1192. */
  1193. extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioStreamInputChannelMap(SDL_AudioStream *stream, int *count);
  1194. /**
  1195. * Get the current output channel map of an audio stream.
  1196. *
  1197. * Channel maps are optional; most things do not need them, instead passing
  1198. * data in the [order that SDL expects](CategoryAudio#channel-layouts).
  1199. *
  1200. * Audio streams default to no remapping applied. This is represented by
  1201. * returning NULL, and does not signify an error.
  1202. *
  1203. * \param stream the SDL_AudioStream to query.
  1204. * \param count On output, set to number of channels in the map. Can be NULL.
  1205. * \returns an array of the current channel mapping, with as many elements as
  1206. * the current output spec's channels, or NULL if default. This
  1207. * should be freed with SDL_free() when it is no longer needed.
  1208. *
  1209. * \threadsafety It is safe to call this function from any thread, as it holds
  1210. * a stream-specific mutex while running.
  1211. *
  1212. * \since This function is available since SDL 3.2.0.
  1213. *
  1214. * \sa SDL_SetAudioStreamInputChannelMap
  1215. */
  1216. extern SDL_DECLSPEC int * SDLCALL SDL_GetAudioStreamOutputChannelMap(SDL_AudioStream *stream, int *count);
  1217. /**
  1218. * Set the current input channel map of an audio stream.
  1219. *
  1220. * Channel maps are optional; most things do not need them, instead passing
  1221. * data in the [order that SDL expects](CategoryAudio#channel-layouts).
  1222. *
  1223. * The input channel map reorders data that is added to a stream via
  1224. * SDL_PutAudioStreamData. Future calls to SDL_PutAudioStreamData must provide
  1225. * data in the new channel order.
  1226. *
  1227. * Each item in the array represents an input channel, and its value is the
  1228. * channel that it should be remapped to. To reverse a stereo signal's left
  1229. * and right values, you'd have an array of `{ 1, 0 }`. It is legal to remap
  1230. * multiple channels to the same thing, so `{ 1, 1 }` would duplicate the
  1231. * right channel to both channels of a stereo signal. An element in the
  1232. * channel map set to -1 instead of a valid channel will mute that channel,
  1233. * setting it to a silence value.
  1234. *
  1235. * You cannot change the number of channels through a channel map, just
  1236. * reorder/mute them.
  1237. *
  1238. * Data that was previously queued in the stream will still be operated on in
  1239. * the order that was current when it was added, which is to say you can put
  1240. * the end of a sound file in one order to a stream, change orders for the
  1241. * next sound file, and start putting that new data while the previous sound
  1242. * file is still queued, and everything will still play back correctly.
  1243. *
  1244. * Audio streams default to no remapping applied. Passing a NULL channel map
  1245. * is legal, and turns off remapping.
  1246. *
  1247. * SDL will copy the channel map; the caller does not have to save this array
  1248. * after this call.
  1249. *
  1250. * If `count` is not equal to the current number of channels in the audio
  1251. * stream's format, this will fail. This is a safety measure to make sure a
  1252. * race condition hasn't changed the format while this call is setting the
  1253. * channel map.
  1254. *
  1255. * Unlike attempting to change the stream's format, the input channel map on a
  1256. * stream bound to a recording device is permitted to change at any time; any
  1257. * data added to the stream from the device after this call will have the new
  1258. * mapping, but previously-added data will still have the prior mapping.
  1259. *
  1260. * \param stream the SDL_AudioStream to change.
  1261. * \param chmap the new channel map, NULL to reset to default.
  1262. * \param count The number of channels in the map.
  1263. * \returns true on success or false on failure; call SDL_GetError() for more
  1264. * information.
  1265. *
  1266. * \threadsafety It is safe to call this function from any thread, as it holds
  1267. * a stream-specific mutex while running. Don't change the
  1268. * stream's format to have a different number of channels from a
  1269. * a different thread at the same time, though!
  1270. *
  1271. * \since This function is available since SDL 3.2.0.
  1272. *
  1273. * \sa SDL_SetAudioStreamInputChannelMap
  1274. */
  1275. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamInputChannelMap(SDL_AudioStream *stream, const int *chmap, int count);
  1276. /**
  1277. * Set the current output channel map of an audio stream.
  1278. *
  1279. * Channel maps are optional; most things do not need them, instead passing
  1280. * data in the [order that SDL expects](CategoryAudio#channel-layouts).
  1281. *
  1282. * The output channel map reorders data that is leaving a stream via
  1283. * SDL_GetAudioStreamData.
  1284. *
  1285. * Each item in the array represents an input channel, and its value is the
  1286. * channel that it should be remapped to. To reverse a stereo signal's left
  1287. * and right values, you'd have an array of `{ 1, 0 }`. It is legal to remap
  1288. * multiple channels to the same thing, so `{ 1, 1 }` would duplicate the
  1289. * right channel to both channels of a stereo signal. An element in the
  1290. * channel map set to -1 instead of a valid channel will mute that channel,
  1291. * setting it to a silence value.
  1292. *
  1293. * You cannot change the number of channels through a channel map, just
  1294. * reorder/mute them.
  1295. *
  1296. * The output channel map can be changed at any time, as output remapping is
  1297. * applied during SDL_GetAudioStreamData.
  1298. *
  1299. * Audio streams default to no remapping applied. Passing a NULL channel map
  1300. * is legal, and turns off remapping.
  1301. *
  1302. * SDL will copy the channel map; the caller does not have to save this array
  1303. * after this call.
  1304. *
  1305. * If `count` is not equal to the current number of channels in the audio
  1306. * stream's format, this will fail. This is a safety measure to make sure a
  1307. * race condition hasn't changed the format while this call is setting the
  1308. * channel map.
  1309. *
  1310. * Unlike attempting to change the stream's format, the output channel map on
  1311. * a stream bound to a recording device is permitted to change at any time;
  1312. * any data added to the stream after this call will have the new mapping, but
  1313. * previously-added data will still have the prior mapping. When the channel
  1314. * map doesn't match the hardware's channel layout, SDL will convert the data
  1315. * before feeding it to the device for playback.
  1316. *
  1317. * \param stream the SDL_AudioStream to change.
  1318. * \param chmap the new channel map, NULL to reset to default.
  1319. * \param count The number of channels in the map.
  1320. * \returns true on success or false on failure; call SDL_GetError() for more
  1321. * information.
  1322. *
  1323. * \threadsafety It is safe to call this function from any thread, as it holds
  1324. * a stream-specific mutex while running. Don't change the
  1325. * stream's format to have a different number of channels from a
  1326. * a different thread at the same time, though!
  1327. *
  1328. * \since This function is available since SDL 3.2.0.
  1329. *
  1330. * \sa SDL_SetAudioStreamInputChannelMap
  1331. */
  1332. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamOutputChannelMap(SDL_AudioStream *stream, const int *chmap, int count);
  1333. /**
  1334. * Add data to the stream.
  1335. *
  1336. * This data must match the format/channels/samplerate specified in the latest
  1337. * call to SDL_SetAudioStreamFormat, or the format specified when creating the
  1338. * stream if it hasn't been changed.
  1339. *
  1340. * Note that this call simply copies the unconverted data for later. This is
  1341. * different than SDL2, where data was converted during the Put call and the
  1342. * Get call would just dequeue the previously-converted data.
  1343. *
  1344. * \param stream the stream the audio data is being added to.
  1345. * \param buf a pointer to the audio data to add.
  1346. * \param len the number of bytes to write to the stream.
  1347. * \returns true on success or false on failure; call SDL_GetError() for more
  1348. * information.
  1349. *
  1350. * \threadsafety It is safe to call this function from any thread, but if the
  1351. * stream has a callback set, the caller might need to manage
  1352. * extra locking.
  1353. *
  1354. * \since This function is available since SDL 3.2.0.
  1355. *
  1356. * \sa SDL_ClearAudioStream
  1357. * \sa SDL_FlushAudioStream
  1358. * \sa SDL_GetAudioStreamData
  1359. * \sa SDL_GetAudioStreamQueued
  1360. */
  1361. extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
  1362. /**
  1363. * A callback that fires for completed SDL_PutAudioStreamDataNoCopy() data.
  1364. *
  1365. * When using SDL_PutAudioStreamDataNoCopy() to provide data to an
  1366. * SDL_AudioStream, it's not safe to dispose of the data until the stream has
  1367. * completely consumed it. Often times it's difficult to know exactly when
  1368. * this has happened.
  1369. *
  1370. * This callback fires once when the stream no longer needs the buffer,
  1371. * allowing the app to easily free or reuse it.
  1372. *
  1373. * \param userdata an opaque pointer provided by the app for their personal
  1374. * use.
  1375. * \param buf the pointer provided to SDL_PutAudioStreamDataNoCopy().
  1376. * \param buflen the size of buffer, in bytes, provided to
  1377. * SDL_PutAudioStreamDataNoCopy().
  1378. *
  1379. * \threadsafety This callbacks may run from any thread, so if you need to
  1380. * protect shared data, you should use SDL_LockAudioStream to
  1381. * serialize access; this lock will be held before your callback
  1382. * is called, so your callback does not need to manage the lock
  1383. * explicitly.
  1384. *
  1385. * \since This datatype is available since SDL 3.4.0.
  1386. *
  1387. * \sa SDL_SetAudioStreamGetCallback
  1388. * \sa SDL_SetAudioStreamPutCallback
  1389. */
  1390. typedef void (SDLCALL *SDL_AudioStreamDataCompleteCallback)(void *userdata, const void *buf, int buflen);
  1391. /**
  1392. * Add external data to an audio stream without copying it.
  1393. *
  1394. * Unlike SDL_PutAudioStreamData(), this function does not make a copy of the
  1395. * provided data, instead storing the provided pointer. This means that the
  1396. * put operation does not need to allocate and copy the data, but the original
  1397. * data must remain available until the stream is done with it, either by
  1398. * being read from the stream in its entirety, or a call to
  1399. * SDL_ClearAudioStream() or SDL_DestroyAudioStream().
  1400. *
  1401. * The data must match the format/channels/samplerate specified in the latest
  1402. * call to SDL_SetAudioStreamFormat, or the format specified when creating the
  1403. * stream if it hasn't been changed.
  1404. *
  1405. * An optional callback may be provided, which is called when the stream no
  1406. * longer needs the data. Once this callback fires, the stream will not access
  1407. * the data again. This callback will fire for any reason the data is no
  1408. * longer needed, including clearing or destroying the stream.
  1409. *
  1410. * Note that there is still an allocation to store tracking information, so
  1411. * this function is more efficient for larger blocks of data. If you're
  1412. * planning to put a few samples at a time, it will be more efficient to use
  1413. * SDL_PutAudioStreamData(), which allocates and buffers in blocks.
  1414. *
  1415. * \param stream the stream the audio data is being added to.
  1416. * \param buf a pointer to the audio data to add.
  1417. * \param len the number of bytes to add to the stream.
  1418. * \param callback the callback function to call when the data is no longer
  1419. * needed by the stream. May be NULL.
  1420. * \param userdata an opaque pointer provided to the callback for its own
  1421. * personal use.
  1422. * \returns true on success or false on failure; call SDL_GetError() for more
  1423. * information.
  1424. *
  1425. * \threadsafety It is safe to call this function from any thread, but if the
  1426. * stream has a callback set, the caller might need to manage
  1427. * extra locking.
  1428. *
  1429. * \since This function is available since SDL 3.4.0.
  1430. *
  1431. * \sa SDL_ClearAudioStream
  1432. * \sa SDL_FlushAudioStream
  1433. * \sa SDL_GetAudioStreamData
  1434. * \sa SDL_GetAudioStreamQueued
  1435. */
  1436. extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamDataNoCopy(SDL_AudioStream *stream, const void *buf, int len, SDL_AudioStreamDataCompleteCallback callback, void *userdata);
  1437. /**
  1438. * Add data to the stream with each channel in a separate array.
  1439. *
  1440. * This data must match the format/channels/samplerate specified in the latest
  1441. * call to SDL_SetAudioStreamFormat, or the format specified when creating the
  1442. * stream if it hasn't been changed.
  1443. *
  1444. * The data will be interleaved and queued. Note that SDL_AudioStream only
  1445. * operates on interleaved data, so this is simply a convenience function for
  1446. * easily queueing data from sources that provide separate arrays. There is no
  1447. * equivalent function to retrieve planar data.
  1448. *
  1449. * The arrays in `channel_buffers` are ordered as they are to be interleaved;
  1450. * the first array will be the first sample in the interleaved data. Any
  1451. * individual array may be NULL; in this case, silence will be interleaved for
  1452. * that channel.
  1453. *
  1454. * `num_channels` specifies how many arrays are in `channel_buffers`. This can
  1455. * be used as a safety to prevent overflow, in case the stream format has
  1456. * changed elsewhere. If more channels are specified than the current input
  1457. * spec, they are ignored. If less channels are specified, the missing arrays
  1458. * are treated as if they are NULL (silence is written to those channels). If
  1459. * the count is -1, SDL will assume the array count matches the current input
  1460. * spec.
  1461. *
  1462. * Note that `num_samples` is the number of _samples per array_. This can also
  1463. * be thought of as the number of _sample frames_ to be queued. A value of 1
  1464. * with stereo arrays will queue two samples to the stream. This is different
  1465. * than SDL_PutAudioStreamData, which wants the size of a single array in
  1466. * bytes.
  1467. *
  1468. * \param stream the stream the audio data is being added to.
  1469. * \param channel_buffers a pointer to an array of arrays, one array per
  1470. * channel.
  1471. * \param num_channels the number of arrays in `channel_buffers` or -1.
  1472. * \param num_samples the number of _samples_ per array to write to the
  1473. * stream.
  1474. * \returns true on success or false on failure; call SDL_GetError() for more
  1475. * information.
  1476. *
  1477. * \threadsafety It is safe to call this function from any thread, but if the
  1478. * stream has a callback set, the caller might need to manage
  1479. * extra locking.
  1480. *
  1481. * \since This function is available since SDL 3.4.0.
  1482. *
  1483. * \sa SDL_ClearAudioStream
  1484. * \sa SDL_FlushAudioStream
  1485. * \sa SDL_GetAudioStreamData
  1486. * \sa SDL_GetAudioStreamQueued
  1487. */
  1488. extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void * const *channel_buffers, int num_channels, int num_samples);
  1489. /**
  1490. * Get converted/resampled data from the stream.
  1491. *
  1492. * The input/output data format/channels/samplerate is specified when creating
  1493. * the stream, and can be changed after creation by calling
  1494. * SDL_SetAudioStreamFormat.
  1495. *
  1496. * Note that any conversion and resampling necessary is done during this call,
  1497. * and SDL_PutAudioStreamData simply queues unconverted data for later. This
  1498. * is different than SDL2, where that work was done while inputting new data
  1499. * to the stream and requesting the output just copied the converted data.
  1500. *
  1501. * \param stream the stream the audio is being requested from.
  1502. * \param buf a buffer to fill with audio data.
  1503. * \param len the maximum number of bytes to fill.
  1504. * \returns the number of bytes read from the stream or -1 on failure; call
  1505. * SDL_GetError() for more information.
  1506. *
  1507. * \threadsafety It is safe to call this function from any thread, but if the
  1508. * stream has a callback set, the caller might need to manage
  1509. * extra locking.
  1510. *
  1511. * \since This function is available since SDL 3.2.0.
  1512. *
  1513. * \sa SDL_ClearAudioStream
  1514. * \sa SDL_GetAudioStreamAvailable
  1515. * \sa SDL_PutAudioStreamData
  1516. */
  1517. extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
  1518. /**
  1519. * Get the number of converted/resampled bytes available.
  1520. *
  1521. * The stream may be buffering data behind the scenes until it has enough to
  1522. * resample correctly, so this number might be lower than what you expect, or
  1523. * even be zero. Add more data or flush the stream if you need the data now.
  1524. *
  1525. * If the stream has so much data that it would overflow an int, the return
  1526. * value is clamped to a maximum value, but no queued data is lost; if there
  1527. * are gigabytes of data queued, the app might need to read some of it with
  1528. * SDL_GetAudioStreamData before this function's return value is no longer
  1529. * clamped.
  1530. *
  1531. * \param stream the audio stream to query.
  1532. * \returns the number of converted/resampled bytes available or -1 on
  1533. * failure; call SDL_GetError() for more information.
  1534. *
  1535. * \threadsafety It is safe to call this function from any thread.
  1536. *
  1537. * \since This function is available since SDL 3.2.0.
  1538. *
  1539. * \sa SDL_GetAudioStreamData
  1540. * \sa SDL_PutAudioStreamData
  1541. */
  1542. extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
  1543. /**
  1544. * Get the number of bytes currently queued.
  1545. *
  1546. * This is the number of bytes put into a stream as input, not the number that
  1547. * can be retrieved as output. Because of several details, it's not possible
  1548. * to calculate one number directly from the other. If you need to know how
  1549. * much usable data can be retrieved right now, you should use
  1550. * SDL_GetAudioStreamAvailable() and not this function.
  1551. *
  1552. * Note that audio streams can change their input format at any time, even if
  1553. * there is still data queued in a different format, so the returned byte
  1554. * count will not necessarily match the number of _sample frames_ available.
  1555. * Users of this API should be aware of format changes they make when feeding
  1556. * a stream and plan accordingly.
  1557. *
  1558. * Queued data is not converted until it is consumed by
  1559. * SDL_GetAudioStreamData, so this value should be representative of the exact
  1560. * data that was put into the stream.
  1561. *
  1562. * If the stream has so much data that it would overflow an int, the return
  1563. * value is clamped to a maximum value, but no queued data is lost; if there
  1564. * are gigabytes of data queued, the app might need to read some of it with
  1565. * SDL_GetAudioStreamData before this function's return value is no longer
  1566. * clamped.
  1567. *
  1568. * \param stream the audio stream to query.
  1569. * \returns the number of bytes queued or -1 on failure; call SDL_GetError()
  1570. * for more information.
  1571. *
  1572. * \threadsafety It is safe to call this function from any thread.
  1573. *
  1574. * \since This function is available since SDL 3.2.0.
  1575. *
  1576. * \sa SDL_PutAudioStreamData
  1577. * \sa SDL_ClearAudioStream
  1578. */
  1579. extern SDL_DECLSPEC int SDLCALL SDL_GetAudioStreamQueued(SDL_AudioStream *stream);
  1580. /**
  1581. * Tell the stream that you're done sending data, and anything being buffered
  1582. * should be converted/resampled and made available immediately.
  1583. *
  1584. * It is legal to add more data to a stream after flushing, but there may be
  1585. * audio gaps in the output. Generally this is intended to signal the end of
  1586. * input, so the complete output becomes available.
  1587. *
  1588. * \param stream the audio stream to flush.
  1589. * \returns true on success or false on failure; call SDL_GetError() for more
  1590. * information.
  1591. *
  1592. * \threadsafety It is safe to call this function from any thread.
  1593. *
  1594. * \since This function is available since SDL 3.2.0.
  1595. *
  1596. * \sa SDL_PutAudioStreamData
  1597. */
  1598. extern SDL_DECLSPEC bool SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
  1599. /**
  1600. * Clear any pending data in the stream.
  1601. *
  1602. * This drops any queued data, so there will be nothing to read from the
  1603. * stream until more is added.
  1604. *
  1605. * \param stream the audio stream to clear.
  1606. * \returns true on success or false on failure; call SDL_GetError() for more
  1607. * information.
  1608. *
  1609. * \threadsafety It is safe to call this function from any thread.
  1610. *
  1611. * \since This function is available since SDL 3.2.0.
  1612. *
  1613. * \sa SDL_GetAudioStreamAvailable
  1614. * \sa SDL_GetAudioStreamData
  1615. * \sa SDL_GetAudioStreamQueued
  1616. * \sa SDL_PutAudioStreamData
  1617. */
  1618. extern SDL_DECLSPEC bool SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
  1619. /**
  1620. * Use this function to pause audio playback on the audio device associated
  1621. * with an audio stream.
  1622. *
  1623. * This function pauses audio processing for a given device. Any bound audio
  1624. * streams will not progress, and no audio will be generated. Pausing one
  1625. * device does not prevent other unpaused devices from running.
  1626. *
  1627. * Pausing a device can be useful to halt all audio without unbinding all the
  1628. * audio streams. This might be useful while a game is paused, or a level is
  1629. * loading, etc.
  1630. *
  1631. * \param stream the audio stream associated with the audio device to pause.
  1632. * \returns true on success or false on failure; call SDL_GetError() for more
  1633. * information.
  1634. *
  1635. * \threadsafety It is safe to call this function from any thread.
  1636. *
  1637. * \since This function is available since SDL 3.2.0.
  1638. *
  1639. * \sa SDL_ResumeAudioStreamDevice
  1640. */
  1641. extern SDL_DECLSPEC bool SDLCALL SDL_PauseAudioStreamDevice(SDL_AudioStream *stream);
  1642. /**
  1643. * Use this function to unpause audio playback on the audio device associated
  1644. * with an audio stream.
  1645. *
  1646. * This function unpauses audio processing for a given device that has
  1647. * previously been paused. Once unpaused, any bound audio streams will begin
  1648. * to progress again, and audio can be generated.
  1649. *
  1650. * SDL_OpenAudioDeviceStream opens audio devices in a paused state, so this
  1651. * function call is required for audio playback to begin on such devices.
  1652. *
  1653. * \param stream the audio stream associated with the audio device to resume.
  1654. * \returns true on success or false on failure; call SDL_GetError() for more
  1655. * information.
  1656. *
  1657. * \threadsafety It is safe to call this function from any thread.
  1658. *
  1659. * \since This function is available since SDL 3.2.0.
  1660. *
  1661. * \sa SDL_PauseAudioStreamDevice
  1662. */
  1663. extern SDL_DECLSPEC bool SDLCALL SDL_ResumeAudioStreamDevice(SDL_AudioStream *stream);
  1664. /**
  1665. * Use this function to query if an audio device associated with a stream is
  1666. * paused.
  1667. *
  1668. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  1669. * has to bind a stream before any audio will flow.
  1670. *
  1671. * \param stream the audio stream associated with the audio device to query.
  1672. * \returns true if device is valid and paused, false otherwise.
  1673. *
  1674. * \threadsafety It is safe to call this function from any thread.
  1675. *
  1676. * \since This function is available since SDL 3.2.0.
  1677. *
  1678. * \sa SDL_PauseAudioStreamDevice
  1679. * \sa SDL_ResumeAudioStreamDevice
  1680. */
  1681. extern SDL_DECLSPEC bool SDLCALL SDL_AudioStreamDevicePaused(SDL_AudioStream *stream);
  1682. /**
  1683. * Lock an audio stream for serialized access.
  1684. *
  1685. * Each SDL_AudioStream has an internal mutex it uses to protect its data
  1686. * structures from threading conflicts. This function allows an app to lock
  1687. * that mutex, which could be useful if registering callbacks on this stream.
  1688. *
  1689. * One does not need to lock a stream to use in it most cases, as the stream
  1690. * manages this lock internally. However, this lock is held during callbacks,
  1691. * which may run from arbitrary threads at any time, so if an app needs to
  1692. * protect shared data during those callbacks, locking the stream guarantees
  1693. * that the callback is not running while the lock is held.
  1694. *
  1695. * As this is just a wrapper over SDL_LockMutex for an internal lock; it has
  1696. * all the same attributes (recursive locks are allowed, etc).
  1697. *
  1698. * \param stream the audio stream to lock.
  1699. * \returns true on success or false on failure; call SDL_GetError() for more
  1700. * information.
  1701. *
  1702. * \threadsafety It is safe to call this function from any thread.
  1703. *
  1704. * \since This function is available since SDL 3.2.0.
  1705. *
  1706. * \sa SDL_UnlockAudioStream
  1707. */
  1708. extern SDL_DECLSPEC bool SDLCALL SDL_LockAudioStream(SDL_AudioStream *stream);
  1709. /**
  1710. * Unlock an audio stream for serialized access.
  1711. *
  1712. * This unlocks an audio stream after a call to SDL_LockAudioStream.
  1713. *
  1714. * \param stream the audio stream to unlock.
  1715. * \returns true on success or false on failure; call SDL_GetError() for more
  1716. * information.
  1717. *
  1718. * \threadsafety You should only call this from the same thread that
  1719. * previously called SDL_LockAudioStream.
  1720. *
  1721. * \since This function is available since SDL 3.2.0.
  1722. *
  1723. * \sa SDL_LockAudioStream
  1724. */
  1725. extern SDL_DECLSPEC bool SDLCALL SDL_UnlockAudioStream(SDL_AudioStream *stream);
  1726. /**
  1727. * A callback that fires when data passes through an SDL_AudioStream.
  1728. *
  1729. * Apps can (optionally) register a callback with an audio stream that is
  1730. * called when data is added with SDL_PutAudioStreamData, or requested with
  1731. * SDL_GetAudioStreamData.
  1732. *
  1733. * Two values are offered here: one is the amount of additional data needed to
  1734. * satisfy the immediate request (which might be zero if the stream already
  1735. * has enough data queued) and the other is the total amount being requested.
  1736. * In a Get call triggering a Put callback, these values can be different. In
  1737. * a Put call triggering a Get callback, these values are always the same.
  1738. *
  1739. * Byte counts might be slightly overestimated due to buffering or resampling,
  1740. * and may change from call to call.
  1741. *
  1742. * This callback is not required to do anything. Generally this is useful for
  1743. * adding/reading data on demand, and the app will often put/get data as
  1744. * appropriate, but the system goes on with the data currently available to it
  1745. * if this callback does nothing.
  1746. *
  1747. * \param stream the SDL audio stream associated with this callback.
  1748. * \param additional_amount the amount of data, in bytes, that is needed right
  1749. * now.
  1750. * \param total_amount the total amount of data requested, in bytes, that is
  1751. * requested or available.
  1752. * \param userdata an opaque pointer provided by the app for their personal
  1753. * use.
  1754. *
  1755. * \threadsafety This callbacks may run from any thread, so if you need to
  1756. * protect shared data, you should use SDL_LockAudioStream to
  1757. * serialize access; this lock will be held before your callback
  1758. * is called, so your callback does not need to manage the lock
  1759. * explicitly.
  1760. *
  1761. * \since This datatype is available since SDL 3.2.0.
  1762. *
  1763. * \sa SDL_SetAudioStreamGetCallback
  1764. * \sa SDL_SetAudioStreamPutCallback
  1765. */
  1766. typedef void (SDLCALL *SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount);
  1767. /**
  1768. * Set a callback that runs when data is requested from an audio stream.
  1769. *
  1770. * This callback is called _before_ data is obtained from the stream, giving
  1771. * the callback the chance to add more on-demand.
  1772. *
  1773. * The callback can (optionally) call SDL_PutAudioStreamData() to add more
  1774. * audio to the stream during this call; if needed, the request that triggered
  1775. * this callback will obtain the new data immediately.
  1776. *
  1777. * The callback's `additional_amount` argument is roughly how many bytes of
  1778. * _unconverted_ data (in the stream's input format) is needed by the caller,
  1779. * although this may overestimate a little for safety. This takes into account
  1780. * how much is already in the stream and only asks for any extra necessary to
  1781. * resolve the request, which means the callback may be asked for zero bytes,
  1782. * and a different amount on each call.
  1783. *
  1784. * The callback is not required to supply exact amounts; it is allowed to
  1785. * supply too much or too little or none at all. The caller will get what's
  1786. * available, up to the amount they requested, regardless of this callback's
  1787. * outcome.
  1788. *
  1789. * Clearing or flushing an audio stream does not call this callback.
  1790. *
  1791. * This function obtains the stream's lock, which means any existing callback
  1792. * (get or put) in progress will finish running before setting the new
  1793. * callback.
  1794. *
  1795. * Setting a NULL function turns off the callback.
  1796. *
  1797. * \param stream the audio stream to set the new callback on.
  1798. * \param callback the new callback function to call when data is requested
  1799. * from the stream.
  1800. * \param userdata an opaque pointer provided to the callback for its own
  1801. * personal use.
  1802. * \returns true on success or false on failure; call SDL_GetError() for more
  1803. * information. This only fails if `stream` is NULL.
  1804. *
  1805. * \threadsafety It is safe to call this function from any thread.
  1806. *
  1807. * \since This function is available since SDL 3.2.0.
  1808. *
  1809. * \sa SDL_SetAudioStreamPutCallback
  1810. */
  1811. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
  1812. /**
  1813. * Set a callback that runs when data is added to an audio stream.
  1814. *
  1815. * This callback is called _after_ the data is added to the stream, giving the
  1816. * callback the chance to obtain it immediately.
  1817. *
  1818. * The callback can (optionally) call SDL_GetAudioStreamData() to obtain audio
  1819. * from the stream during this call.
  1820. *
  1821. * The callback's `additional_amount` argument is how many bytes of
  1822. * _converted_ data (in the stream's output format) was provided by the
  1823. * caller, although this may underestimate a little for safety. This value
  1824. * might be less than what is currently available in the stream, if data was
  1825. * already there, and might be less than the caller provided if the stream
  1826. * needs to keep a buffer to aid in resampling. Which means the callback may
  1827. * be provided with zero bytes, and a different amount on each call.
  1828. *
  1829. * The callback may call SDL_GetAudioStreamAvailable to see the total amount
  1830. * currently available to read from the stream, instead of the total provided
  1831. * by the current call.
  1832. *
  1833. * The callback is not required to obtain all data. It is allowed to read less
  1834. * or none at all. Anything not read now simply remains in the stream for
  1835. * later access.
  1836. *
  1837. * Clearing or flushing an audio stream does not call this callback.
  1838. *
  1839. * This function obtains the stream's lock, which means any existing callback
  1840. * (get or put) in progress will finish running before setting the new
  1841. * callback.
  1842. *
  1843. * Setting a NULL function turns off the callback.
  1844. *
  1845. * \param stream the audio stream to set the new callback on.
  1846. * \param callback the new callback function to call when data is added to the
  1847. * stream.
  1848. * \param userdata an opaque pointer provided to the callback for its own
  1849. * personal use.
  1850. * \returns true on success or false on failure; call SDL_GetError() for more
  1851. * information. This only fails if `stream` is NULL.
  1852. *
  1853. * \threadsafety It is safe to call this function from any thread.
  1854. *
  1855. * \since This function is available since SDL 3.2.0.
  1856. *
  1857. * \sa SDL_SetAudioStreamGetCallback
  1858. */
  1859. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
  1860. /**
  1861. * Free an audio stream.
  1862. *
  1863. * This will release all allocated data, including any audio that is still
  1864. * queued. You do not need to manually clear the stream first.
  1865. *
  1866. * If this stream was bound to an audio device, it is unbound during this
  1867. * call. If this stream was created with SDL_OpenAudioDeviceStream, the audio
  1868. * device that was opened alongside this stream's creation will be closed,
  1869. * too.
  1870. *
  1871. * \param stream the audio stream to destroy.
  1872. *
  1873. * \threadsafety It is safe to call this function from any thread.
  1874. *
  1875. * \since This function is available since SDL 3.2.0.
  1876. *
  1877. * \sa SDL_CreateAudioStream
  1878. */
  1879. extern SDL_DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
  1880. /**
  1881. * Convenience function for straightforward audio init for the common case.
  1882. *
  1883. * If all your app intends to do is provide a single source of PCM audio, this
  1884. * function allows you to do all your audio setup in a single call.
  1885. *
  1886. * This is also intended to be a clean means to migrate apps from SDL2.
  1887. *
  1888. * This function will open an audio device, create a stream and bind it.
  1889. * Unlike other methods of setup, the audio device will be closed when this
  1890. * stream is destroyed, so the app can treat the returned SDL_AudioStream as
  1891. * the only object needed to manage audio playback.
  1892. *
  1893. * Also unlike other functions, the audio device begins paused. This is to map
  1894. * more closely to SDL2-style behavior, since there is no extra step here to
  1895. * bind a stream to begin audio flowing. The audio device should be resumed
  1896. * with SDL_ResumeAudioStreamDevice().
  1897. *
  1898. * This function works with both playback and recording devices.
  1899. *
  1900. * The `spec` parameter represents the app's side of the audio stream. That
  1901. * is, for recording audio, this will be the output format, and for playing
  1902. * audio, this will be the input format. If spec is NULL, the system will
  1903. * choose the format, and the app can use SDL_GetAudioStreamFormat() to obtain
  1904. * this information later.
  1905. *
  1906. * If you don't care about opening a specific audio device, you can (and
  1907. * probably _should_), use SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK for playback and
  1908. * SDL_AUDIO_DEVICE_DEFAULT_RECORDING for recording.
  1909. *
  1910. * One can optionally provide a callback function; if NULL, the app is
  1911. * expected to queue audio data for playback (or unqueue audio data if
  1912. * capturing). Otherwise, the callback will begin to fire once the device is
  1913. * unpaused.
  1914. *
  1915. * Destroying the returned stream with SDL_DestroyAudioStream will also close
  1916. * the audio device associated with this stream.
  1917. *
  1918. * \param devid an audio device to open, or SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK
  1919. * or SDL_AUDIO_DEVICE_DEFAULT_RECORDING.
  1920. * \param spec the audio stream's data format. Can be NULL.
  1921. * \param callback a callback where the app will provide new data for
  1922. * playback, or receive new data for recording. Can be NULL,
  1923. * in which case the app will need to call
  1924. * SDL_PutAudioStreamData or SDL_GetAudioStreamData as
  1925. * necessary.
  1926. * \param userdata app-controlled pointer passed to callback. Can be NULL.
  1927. * Ignored if callback is NULL.
  1928. * \returns an audio stream on success, ready to use, or NULL on failure; call
  1929. * SDL_GetError() for more information. When done with this stream,
  1930. * call SDL_DestroyAudioStream to free resources and close the
  1931. * device.
  1932. *
  1933. * \threadsafety It is safe to call this function from any thread.
  1934. *
  1935. * \since This function is available since SDL 3.2.0.
  1936. *
  1937. * \sa SDL_GetAudioStreamDevice
  1938. * \sa SDL_ResumeAudioStreamDevice
  1939. */
  1940. extern SDL_DECLSPEC SDL_AudioStream * SDLCALL SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata);
  1941. /**
  1942. * A callback that fires around an audio device's processing work.
  1943. *
  1944. * This callback fires when a logical audio device is about to start accessing
  1945. * its bound audio streams, and fires again when it has finished accessing
  1946. * them. It covers the range of one "iteration" of the audio device.
  1947. *
  1948. * It can be useful to use this callback to update state that must apply to
  1949. * all bound audio streams atomically: to make sure state changes don't happen
  1950. * while half of the streams are already processed for the latest audio
  1951. * buffer.
  1952. *
  1953. * This callback should run as quickly as possible and not block for any
  1954. * significant time, as this callback delays submission of data to the audio
  1955. * device, which can cause audio playback problems. This callback delays all
  1956. * audio processing across a single physical audio device: all its logical
  1957. * devices and all bound audio streams. Use it carefully.
  1958. *
  1959. * \param userdata a pointer provided by the app through
  1960. * SDL_SetAudioPostmixCallback, for its own use.
  1961. * \param devid the audio device this callback is running for.
  1962. * \param start true if this is the start of the iteration, false if the end.
  1963. *
  1964. * \threadsafety This will run from a background thread owned by SDL. The
  1965. * application is responsible for locking resources the callback
  1966. * touches that need to be protected.
  1967. *
  1968. * \since This datatype is available since SDL 3.4.0.
  1969. *
  1970. * \sa SDL_SetAudioIterationCallbacks
  1971. */
  1972. typedef void (SDLCALL *SDL_AudioIterationCallback)(void *userdata, SDL_AudioDeviceID devid, bool start);
  1973. /**
  1974. * Set callbacks that fire around a new iteration of audio device processing.
  1975. *
  1976. * Two callbacks are provided here: one that runs when a device is about to
  1977. * process its bound audio streams, and another that runs when the device has
  1978. * finished processing them.
  1979. *
  1980. * These callbacks can run at any time, and from any thread; if you need to
  1981. * serialize access to your app's data, you should provide and use a mutex or
  1982. * other synchronization device.
  1983. *
  1984. * Generally these callbacks are used to apply state that applies to multiple
  1985. * bound audio streams, with a guarantee that the audio device's thread isn't
  1986. * halfway through processing them. Generally a finer-grained lock through
  1987. * SDL_LockAudioStream() is more appropriate.
  1988. *
  1989. * The callbacks are extremely time-sensitive; the callback should do the
  1990. * least amount of work possible and return as quickly as it can. The longer
  1991. * the callback runs, the higher the risk of audio dropouts or other problems.
  1992. *
  1993. * This function will block until the audio device is in between iterations,
  1994. * so any existing callback that might be running will finish before this
  1995. * function sets the new callback and returns.
  1996. *
  1997. * Physical devices do not accept these callbacks, only logical devices
  1998. * created through SDL_OpenAudioDevice() can be.
  1999. *
  2000. * Setting a NULL callback function disables any previously-set callback.
  2001. * Either callback may be NULL, and the same callback is permitted to be used
  2002. * for both.
  2003. *
  2004. * \param devid the ID of an opened audio device.
  2005. * \param start a callback function to be called at the start of an iteration.
  2006. * Can be NULL.
  2007. * \param end a callback function to be called at the end of an iteration. Can
  2008. * be NULL.
  2009. * \param userdata app-controlled pointer passed to callback. Can be NULL.
  2010. * \returns true on success or false on failure; call SDL_GetError() for more
  2011. * information.
  2012. *
  2013. * \threadsafety It is safe to call this function from any thread.
  2014. *
  2015. * \since This function is available since SDL 3.4.0.
  2016. */
  2017. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioIterationCallbacks(SDL_AudioDeviceID devid, SDL_AudioIterationCallback start, SDL_AudioIterationCallback end, void *userdata);
  2018. /**
  2019. * A callback that fires when data is about to be fed to an audio device.
  2020. *
  2021. * This is useful for accessing the final mix, perhaps for writing a
  2022. * visualizer or applying a final effect to the audio data before playback.
  2023. *
  2024. * This callback should run as quickly as possible and not block for any
  2025. * significant time, as this callback delays submission of data to the audio
  2026. * device, which can cause audio playback problems.
  2027. *
  2028. * The postmix callback _must_ be able to handle any audio data format
  2029. * specified in `spec`, which can change between callbacks if the audio device
  2030. * changed. However, this only covers frequency and channel count; data is
  2031. * always provided here in SDL_AUDIO_F32 format.
  2032. *
  2033. * The postmix callback runs _after_ logical device gain and audiostream gain
  2034. * have been applied, which is to say you can make the output data louder at
  2035. * this point than the gain settings would suggest.
  2036. *
  2037. * \param userdata a pointer provided by the app through
  2038. * SDL_SetAudioPostmixCallback, for its own use.
  2039. * \param spec the current format of audio that is to be submitted to the
  2040. * audio device.
  2041. * \param buffer the buffer of audio samples to be submitted. The callback can
  2042. * inspect and/or modify this data.
  2043. * \param buflen the size of `buffer` in bytes.
  2044. *
  2045. * \threadsafety This will run from a background thread owned by SDL. The
  2046. * application is responsible for locking resources the callback
  2047. * touches that need to be protected.
  2048. *
  2049. * \since This datatype is available since SDL 3.2.0.
  2050. *
  2051. * \sa SDL_SetAudioPostmixCallback
  2052. */
  2053. typedef void (SDLCALL *SDL_AudioPostmixCallback)(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen);
  2054. /**
  2055. * Set a callback that fires when data is about to be fed to an audio device.
  2056. *
  2057. * This is useful for accessing the final mix, perhaps for writing a
  2058. * visualizer or applying a final effect to the audio data before playback.
  2059. *
  2060. * The buffer is the final mix of all bound audio streams on an opened device;
  2061. * this callback will fire regularly for any device that is both opened and
  2062. * unpaused. If there is no new data to mix, either because no streams are
  2063. * bound to the device or all the streams are empty, this callback will still
  2064. * fire with the entire buffer set to silence.
  2065. *
  2066. * This callback is allowed to make changes to the data; the contents of the
  2067. * buffer after this call is what is ultimately passed along to the hardware.
  2068. *
  2069. * The callback is always provided the data in float format (values from -1.0f
  2070. * to 1.0f), but the number of channels or sample rate may be different than
  2071. * the format the app requested when opening the device; SDL might have had to
  2072. * manage a conversion behind the scenes, or the playback might have jumped to
  2073. * new physical hardware when a system default changed, etc. These details may
  2074. * change between calls. Accordingly, the size of the buffer might change
  2075. * between calls as well.
  2076. *
  2077. * This callback can run at any time, and from any thread; if you need to
  2078. * serialize access to your app's data, you should provide and use a mutex or
  2079. * other synchronization device.
  2080. *
  2081. * All of this to say: there are specific needs this callback can fulfill, but
  2082. * it is not the simplest interface. Apps should generally provide audio in
  2083. * their preferred format through an SDL_AudioStream and let SDL handle the
  2084. * difference.
  2085. *
  2086. * This function is extremely time-sensitive; the callback should do the least
  2087. * amount of work possible and return as quickly as it can. The longer the
  2088. * callback runs, the higher the risk of audio dropouts or other problems.
  2089. *
  2090. * This function will block until the audio device is in between iterations,
  2091. * so any existing callback that might be running will finish before this
  2092. * function sets the new callback and returns.
  2093. *
  2094. * Setting a NULL callback function disables any previously-set callback.
  2095. *
  2096. * \param devid the ID of an opened audio device.
  2097. * \param callback a callback function to be called. Can be NULL.
  2098. * \param userdata app-controlled pointer passed to callback. Can be NULL.
  2099. * \returns true on success or false on failure; call SDL_GetError() for more
  2100. * information.
  2101. *
  2102. * \threadsafety It is safe to call this function from any thread.
  2103. *
  2104. * \since This function is available since SDL 3.2.0.
  2105. */
  2106. extern SDL_DECLSPEC bool SDLCALL SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata);
  2107. /**
  2108. * Load the audio data of a WAVE file into memory.
  2109. *
  2110. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  2111. * be valid pointers. The entire data portion of the file is then loaded into
  2112. * memory and decoded if necessary.
  2113. *
  2114. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  2115. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  2116. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  2117. * cause an error.
  2118. *
  2119. * If this function succeeds, the return value is zero and the pointer to the
  2120. * audio data allocated by the function is written to `audio_buf` and its
  2121. * length in bytes to `audio_len`. The SDL_AudioSpec members `freq`,
  2122. * `channels`, and `format` are set to the values of the audio data in the
  2123. * buffer.
  2124. *
  2125. * It's necessary to use SDL_free() to free the audio data returned in
  2126. * `audio_buf` when it is no longer used.
  2127. *
  2128. * Because of the underspecification of the .WAV format, there are many
  2129. * problematic files in the wild that cause issues with strict decoders. To
  2130. * provide compatibility with these files, this decoder is lenient in regards
  2131. * to the truncation of the file, the fact chunk, and the size of the RIFF
  2132. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  2133. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  2134. * tune the behavior of the loading process.
  2135. *
  2136. * Any file that is invalid (due to truncation, corruption, or wrong values in
  2137. * the headers), too big, or unsupported causes an error. Additionally, any
  2138. * critical I/O error from the data source will terminate the loading process
  2139. * with an error. The function returns NULL on error and in all cases (with
  2140. * the exception of `src` being NULL), an appropriate error message will be
  2141. * set.
  2142. *
  2143. * It is required that the data source supports seeking.
  2144. *
  2145. * Example:
  2146. *
  2147. * ```c
  2148. * SDL_LoadWAV_IO(SDL_IOFromFile("sample.wav", "rb"), true, &spec, &buf, &len);
  2149. * ```
  2150. *
  2151. * Note that the SDL_LoadWAV function does this same thing for you, but in a
  2152. * less messy way:
  2153. *
  2154. * ```c
  2155. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  2156. * ```
  2157. *
  2158. * \param src the data source for the WAVE data.
  2159. * \param closeio if true, calls SDL_CloseIO() on `src` before returning, even
  2160. * in the case of an error.
  2161. * \param spec a pointer to an SDL_AudioSpec that will be set to the WAVE
  2162. * data's format details on successful return.
  2163. * \param audio_buf a pointer filled with the audio data, allocated by the
  2164. * function.
  2165. * \param audio_len a pointer filled with the length of the audio data buffer
  2166. * in bytes.
  2167. * \returns true on success. `audio_buf` will be filled with a pointer to an
  2168. * allocated buffer containing the audio data, and `audio_len` is
  2169. * filled with the length of that audio buffer in bytes.
  2170. *
  2171. * This function returns false if the .WAV file cannot be opened,
  2172. * uses an unknown data format, or is corrupt; call SDL_GetError()
  2173. * for more information.
  2174. *
  2175. * When the application is done with the data returned in
  2176. * `audio_buf`, it should call SDL_free() to dispose of it.
  2177. *
  2178. * \threadsafety It is safe to call this function from any thread.
  2179. *
  2180. * \since This function is available since SDL 3.2.0.
  2181. *
  2182. * \sa SDL_free
  2183. * \sa SDL_LoadWAV
  2184. */
  2185. extern SDL_DECLSPEC bool SDLCALL SDL_LoadWAV_IO(SDL_IOStream *src, bool closeio, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);
  2186. /**
  2187. * Loads a WAV from a file path.
  2188. *
  2189. * This is a convenience function that is effectively the same as:
  2190. *
  2191. * ```c
  2192. * SDL_LoadWAV_IO(SDL_IOFromFile(path, "rb"), true, spec, audio_buf, audio_len);
  2193. * ```
  2194. *
  2195. * \param path the file path of the WAV file to open.
  2196. * \param spec a pointer to an SDL_AudioSpec that will be set to the WAVE
  2197. * data's format details on successful return.
  2198. * \param audio_buf a pointer filled with the audio data, allocated by the
  2199. * function.
  2200. * \param audio_len a pointer filled with the length of the audio data buffer
  2201. * in bytes.
  2202. * \returns true on success. `audio_buf` will be filled with a pointer to an
  2203. * allocated buffer containing the audio data, and `audio_len` is
  2204. * filled with the length of that audio buffer in bytes.
  2205. *
  2206. * This function returns false if the .WAV file cannot be opened,
  2207. * uses an unknown data format, or is corrupt; call SDL_GetError()
  2208. * for more information.
  2209. *
  2210. * When the application is done with the data returned in
  2211. * `audio_buf`, it should call SDL_free() to dispose of it.
  2212. *
  2213. * \threadsafety It is safe to call this function from any thread.
  2214. *
  2215. * \since This function is available since SDL 3.2.0.
  2216. *
  2217. * \sa SDL_free
  2218. * \sa SDL_LoadWAV_IO
  2219. */
  2220. extern SDL_DECLSPEC bool SDLCALL SDL_LoadWAV(const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);
  2221. /**
  2222. * Mix audio data in a specified format.
  2223. *
  2224. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  2225. * it into `dst`, performing addition, volume adjustment, and overflow
  2226. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  2227. * `format` data.
  2228. *
  2229. * This is provided for convenience -- you can mix your own audio data.
  2230. *
  2231. * Do not use this function for mixing together more than two streams of
  2232. * sample data. The output from repeated application of this function may be
  2233. * distorted by clipping, because there is no accumulator with greater range
  2234. * than the input (not to mention this being an inefficient way of doing it).
  2235. *
  2236. * It is a common misconception that this function is required to write audio
  2237. * data to an output stream in an audio callback. While you can do that,
  2238. * SDL_MixAudio() is really only needed when you're mixing a single audio
  2239. * stream with a volume adjustment.
  2240. *
  2241. * \param dst the destination for the mixed audio.
  2242. * \param src the source audio buffer to be mixed.
  2243. * \param format the SDL_AudioFormat structure representing the desired audio
  2244. * format.
  2245. * \param len the length of the audio buffer in bytes.
  2246. * \param volume ranges from 0.0 - 1.0, and should be set to 1.0 for full
  2247. * audio volume.
  2248. * \returns true on success or false on failure; call SDL_GetError() for more
  2249. * information.
  2250. *
  2251. * \threadsafety It is safe to call this function from any thread.
  2252. *
  2253. * \since This function is available since SDL 3.2.0.
  2254. */
  2255. extern SDL_DECLSPEC bool SDLCALL SDL_MixAudio(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, float volume);
  2256. /**
  2257. * Convert some audio data of one format to another format.
  2258. *
  2259. * Please note that this function is for convenience, but should not be used
  2260. * to resample audio in blocks, as it will introduce audio artifacts on the
  2261. * boundaries. You should only use this function if you are converting audio
  2262. * data in its entirety in one call. If you want to convert audio in smaller
  2263. * chunks, use an SDL_AudioStream, which is designed for this situation.
  2264. *
  2265. * Internally, this function creates and destroys an SDL_AudioStream on each
  2266. * use, so it's also less efficient than using one directly, if you need to
  2267. * convert multiple times.
  2268. *
  2269. * \param src_spec the format details of the input audio.
  2270. * \param src_data the audio data to be converted.
  2271. * \param src_len the len of src_data.
  2272. * \param dst_spec the format details of the output audio.
  2273. * \param dst_data will be filled with a pointer to converted audio data,
  2274. * which should be freed with SDL_free(). On error, it will be
  2275. * NULL.
  2276. * \param dst_len will be filled with the len of dst_data.
  2277. * \returns true on success or false on failure; call SDL_GetError() for more
  2278. * information.
  2279. *
  2280. * \threadsafety It is safe to call this function from any thread.
  2281. *
  2282. * \since This function is available since SDL 3.2.0.
  2283. */
  2284. 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);
  2285. /**
  2286. * Get the human readable name of an audio format.
  2287. *
  2288. * \param format the audio format to query.
  2289. * \returns the human readable name of the specified audio format or
  2290. * "SDL_AUDIO_UNKNOWN" if the format isn't recognized.
  2291. *
  2292. * \threadsafety It is safe to call this function from any thread.
  2293. *
  2294. * \since This function is available since SDL 3.2.0.
  2295. */
  2296. extern SDL_DECLSPEC const char * SDLCALL SDL_GetAudioFormatName(SDL_AudioFormat format);
  2297. /**
  2298. * Get the appropriate memset value for silencing an audio format.
  2299. *
  2300. * The value returned by this function can be used as the second argument to
  2301. * memset (or SDL_memset) to set an audio buffer in a specific format to
  2302. * silence.
  2303. *
  2304. * \param format the audio data format to query.
  2305. * \returns a byte value that can be passed to memset.
  2306. *
  2307. * \threadsafety It is safe to call this function from any thread.
  2308. *
  2309. * \since This function is available since SDL 3.2.0.
  2310. */
  2311. extern SDL_DECLSPEC int SDLCALL SDL_GetSilenceValueForFormat(SDL_AudioFormat format);
  2312. /* Ends C function definitions when using C++ */
  2313. #ifdef __cplusplus
  2314. }
  2315. #endif
  2316. #include <SDL3/SDL_close_code.h>
  2317. #endif /* SDL_audio_h_ */