SDL_audio.h 80 KB

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