SDL_audio.h 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. * \file SDL_audio.h
  20. *
  21. * \brief Audio functionality for the SDL library.
  22. */
  23. #ifndef SDL_audio_h_
  24. #define SDL_audio_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. #include <SDL3/SDL_endian.h>
  28. #include <SDL3/SDL_mutex.h>
  29. #include <SDL3/SDL_thread.h>
  30. #include <SDL3/SDL_rwops.h>
  31. #include <SDL3/SDL_begin_code.h>
  32. /* Set up for C function definitions, even when using C++ */
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /*
  37. * For multi-channel audio, the default SDL channel mapping is:
  38. * 2: FL FR (stereo)
  39. * 3: FL FR LFE (2.1 surround)
  40. * 4: FL FR BL BR (quad)
  41. * 5: FL FR LFE BL BR (4.1 surround)
  42. * 6: FL FR FC LFE SL SR (5.1 surround - last two can also be BL BR)
  43. * 7: FL FR FC LFE BC SL SR (6.1 surround)
  44. * 8: FL FR FC LFE BL BR SL SR (7.1 surround)
  45. */
  46. /**
  47. * \brief Audio format flags.
  48. *
  49. * These are what the 16 bits in SDL_AudioFormat currently mean...
  50. * (Unspecified bits are always zero).
  51. *
  52. * \verbatim
  53. ++-----------------------sample is signed if set
  54. ||
  55. || ++-----------sample is bigendian if set
  56. || ||
  57. || || ++---sample is float if set
  58. || || ||
  59. || || || +---sample bit size---+
  60. || || || | |
  61. 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
  62. \endverbatim
  63. *
  64. * There are macros in SDL 2.0 and later to query these bits.
  65. */
  66. typedef Uint16 SDL_AudioFormat;
  67. /**
  68. * \name Audio flags
  69. */
  70. /* @{ */
  71. #define SDL_AUDIO_MASK_BITSIZE (0xFF)
  72. #define SDL_AUDIO_MASK_FLOAT (1<<8)
  73. #define SDL_AUDIO_MASK_BIG_ENDIAN (1<<12)
  74. #define SDL_AUDIO_MASK_SIGNED (1<<15)
  75. #define SDL_AUDIO_BITSIZE(x) ((x) & SDL_AUDIO_MASK_BITSIZE)
  76. #define SDL_AUDIO_BYTESIZE(x) (SDL_AUDIO_BITSIZE(x) / 8)
  77. #define SDL_AUDIO_ISFLOAT(x) ((x) & SDL_AUDIO_MASK_FLOAT)
  78. #define SDL_AUDIO_ISBIGENDIAN(x) ((x) & SDL_AUDIO_MASK_BIG_ENDIAN)
  79. #define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))
  80. #define SDL_AUDIO_ISSIGNED(x) ((x) & SDL_AUDIO_MASK_SIGNED)
  81. #define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))
  82. #define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))
  83. /**
  84. * \name Audio format flags
  85. *
  86. * Defaults to LSB byte order.
  87. */
  88. /* @{ */
  89. #define SDL_AUDIO_U8 0x0008 /**< Unsigned 8-bit samples */
  90. #define SDL_AUDIO_S8 0x8008 /**< Signed 8-bit samples */
  91. #define SDL_AUDIO_S16LE 0x8010 /**< Signed 16-bit samples */
  92. #define SDL_AUDIO_S16BE 0x9010 /**< As above, but big-endian byte order */
  93. /* @} */
  94. /**
  95. * \name int32 support
  96. */
  97. /* @{ */
  98. #define SDL_AUDIO_S32LE 0x8020 /**< 32-bit integer samples */
  99. #define SDL_AUDIO_S32BE 0x9020 /**< As above, but big-endian byte order */
  100. /* @} */
  101. /**
  102. * \name float32 support
  103. */
  104. /* @{ */
  105. #define SDL_AUDIO_F32LE 0x8120 /**< 32-bit floating point samples */
  106. #define SDL_AUDIO_F32BE 0x9120 /**< As above, but big-endian byte order */
  107. /* @} */
  108. /**
  109. * \name Native audio byte ordering
  110. */
  111. /* @{ */
  112. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  113. #define SDL_AUDIO_S16 SDL_AUDIO_S16LE
  114. #define SDL_AUDIO_S32 SDL_AUDIO_S32LE
  115. #define SDL_AUDIO_F32 SDL_AUDIO_F32LE
  116. #else
  117. #define SDL_AUDIO_S16 SDL_AUDIO_S16BE
  118. #define SDL_AUDIO_S32 SDL_AUDIO_S32BE
  119. #define SDL_AUDIO_F32 SDL_AUDIO_F32BE
  120. #endif
  121. /* @} */
  122. /* @} *//* Audio flags */
  123. /**
  124. * SDL Audio Device instance IDs.
  125. */
  126. typedef Uint32 SDL_AudioDeviceID;
  127. #define SDL_AUDIO_DEVICE_DEFAULT_OUTPUT ((SDL_AudioDeviceID) 0xFFFFFFFF)
  128. #define SDL_AUDIO_DEVICE_DEFAULT_CAPTURE ((SDL_AudioDeviceID) 0xFFFFFFFE)
  129. typedef struct SDL_AudioSpec
  130. {
  131. SDL_AudioFormat format; /**< Audio data format */
  132. int channels; /**< Number of channels: 1 mono, 2 stereo, etc */
  133. int freq; /**< sample rate: sample frames per second */
  134. } SDL_AudioSpec;
  135. /* Calculate the size of each audio frame (in bytes) */
  136. #define SDL_AUDIO_FRAMESIZE(x) (SDL_AUDIO_BYTESIZE((x).format) * (x).channels)
  137. /* SDL_AudioStream is an audio conversion interface.
  138. - It can handle resampling data in chunks without generating
  139. artifacts, when it doesn't have the complete buffer available.
  140. - It can handle incoming data in any variable size.
  141. - It can handle input/output format changes on the fly.
  142. - You push data as you have it, and pull it when you need it
  143. - It can also function as a basic audio data queue even if you
  144. just have sound that needs to pass from one place to another.
  145. - You can hook callbacks up to them when more data is added or
  146. requested, to manage data on-the-fly.
  147. */
  148. struct SDL_AudioStream; /* this is opaque to the outside world. */
  149. typedef struct SDL_AudioStream SDL_AudioStream;
  150. /* Function prototypes */
  151. /**
  152. * \name Driver discovery functions
  153. *
  154. * These functions return the list of built in audio drivers, in the
  155. * order that they are normally initialized by default.
  156. */
  157. /* @{ */
  158. /**
  159. * Use this function to get the number of built-in audio drivers.
  160. *
  161. * This function returns a hardcoded number. This never returns a negative
  162. * value; if there are no drivers compiled into this build of SDL, this
  163. * function returns zero. The presence of a driver in this list does not mean
  164. * it will function, it just means SDL is capable of interacting with that
  165. * interface. For example, a build of SDL might have esound support, but if
  166. * there's no esound server available, SDL's esound driver would fail if used.
  167. *
  168. * By default, SDL tries all drivers, in its preferred order, until one is
  169. * found to be usable.
  170. *
  171. * \returns the number of built-in audio drivers.
  172. *
  173. * \threadsafety It is safe to call this function from any thread.
  174. *
  175. * \since This function is available since SDL 3.0.0.
  176. *
  177. * \sa SDL_GetAudioDriver
  178. */
  179. extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
  180. /**
  181. * Use this function to get the name of a built in audio driver.
  182. *
  183. * The list of audio drivers is given in the order that they are normally
  184. * initialized by default; the drivers that seem more reasonable to choose
  185. * first (as far as the SDL developers believe) are earlier in the list.
  186. *
  187. * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
  188. * "coreaudio" or "xaudio2". These never have Unicode characters, and are not
  189. * meant to be proper names.
  190. *
  191. * \param index the index of the audio driver; the value ranges from 0 to
  192. * SDL_GetNumAudioDrivers() - 1
  193. * \returns the name of the audio driver at the requested index, or NULL if an
  194. * invalid index was specified.
  195. *
  196. * \threadsafety It is safe to call this function from any thread.
  197. *
  198. * \since This function is available since SDL 3.0.0.
  199. *
  200. * \sa SDL_GetNumAudioDrivers
  201. */
  202. extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
  203. /* @} */
  204. /**
  205. * Get the name of the current audio driver.
  206. *
  207. * The returned string points to internal static memory and thus never becomes
  208. * invalid, even if you quit the audio subsystem and initialize a new driver
  209. * (although such a case would return a different static string from another
  210. * call to this function, of course). As such, you should not modify or free
  211. * the returned string.
  212. *
  213. * \returns the name of the current audio driver or NULL if no driver has been
  214. * initialized.
  215. *
  216. * \threadsafety It is safe to call this function from any thread.
  217. *
  218. * \since This function is available since SDL 3.0.0.
  219. */
  220. extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
  221. /**
  222. * Get a list of currently-connected audio output devices.
  223. *
  224. * This returns of list of available devices that play sound, perhaps to
  225. * speakers or headphones ("output" devices). If you want devices that record
  226. * audio, like a microphone ("capture" devices), use
  227. * SDL_GetAudioCaptureDevices() instead.
  228. *
  229. * This only returns a list of physical devices; it will not have any device
  230. * IDs returned by SDL_OpenAudioDevice().
  231. *
  232. * \param count a pointer filled in with the number of devices returned
  233. * \returns a 0 terminated array of device instance IDs which should be freed
  234. * with SDL_free(), or NULL on error; call SDL_GetError() for more
  235. * details.
  236. *
  237. * \threadsafety It is safe to call this function from any thread.
  238. *
  239. * \since This function is available since SDL 3.0.0.
  240. *
  241. * \sa SDL_OpenAudioDevice
  242. * \sa SDL_GetAudioCaptureDevices
  243. */
  244. extern DECLSPEC SDL_AudioDeviceID *SDLCALL SDL_GetAudioOutputDevices(int *count);
  245. /**
  246. * Get a list of currently-connected audio capture devices.
  247. *
  248. * This returns of list of available devices that record audio, like a
  249. * microphone ("capture" devices). If you want devices that play sound,
  250. * perhaps to speakers or headphones ("output" devices), use
  251. * SDL_GetAudioOutputDevices() instead.
  252. *
  253. * This only returns a list of physical devices; it will not have any device
  254. * IDs returned by SDL_OpenAudioDevice().
  255. *
  256. * \param count a pointer filled in with the number of devices returned
  257. * \returns a 0 terminated array of device instance IDs which should be freed
  258. * with SDL_free(), or NULL on error; call SDL_GetError() for more
  259. * details.
  260. *
  261. * \threadsafety It is safe to call this function from any thread.
  262. *
  263. * \since This function is available since SDL 3.0.0.
  264. *
  265. * \sa SDL_OpenAudioDevice
  266. * \sa SDL_GetAudioOutputDevices
  267. */
  268. extern DECLSPEC SDL_AudioDeviceID *SDLCALL SDL_GetAudioCaptureDevices(int *count);
  269. /**
  270. * Get the human-readable name of a specific audio device.
  271. *
  272. * The string returned by this function is UTF-8 encoded. The caller should
  273. * call SDL_free on the return value when done with it.
  274. *
  275. * \param devid the instance ID of the device to query.
  276. * \returns the name of the audio device, or NULL on error.
  277. *
  278. * \threadsafety It is safe to call this function from any thread.
  279. *
  280. * \since This function is available since SDL 3.0.0.
  281. *
  282. * \sa SDL_GetAudioOutputDevices
  283. * \sa SDL_GetAudioCaptureDevices
  284. * \sa SDL_GetDefaultAudioInfo
  285. */
  286. extern DECLSPEC char *SDLCALL SDL_GetAudioDeviceName(SDL_AudioDeviceID devid);
  287. /**
  288. * Get the current audio format of a specific audio device.
  289. *
  290. * For an opened device, this will report the format the device is currently
  291. * using. If the device isn't yet opened, this will report the device's
  292. * preferred format (or a reasonable default if this can't be determined).
  293. *
  294. * You may also specify SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
  295. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE here, which is useful for getting a
  296. * reasonable recommendation before opening the system-recommended default
  297. * device.
  298. *
  299. * \param devid the instance ID of the device to query.
  300. * \param spec On return, will be filled with device details.
  301. * \returns 0 on success or a negative error code on failure; call
  302. * SDL_GetError() for more information.
  303. *
  304. * \threadsafety It is safe to call this function from any thread.
  305. *
  306. * \since This function is available since SDL 3.0.0.
  307. */
  308. extern DECLSPEC int SDLCALL SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec);
  309. /**
  310. * Open a specific audio device.
  311. *
  312. * You can open both output and capture devices through this function. Output
  313. * devices will take data from bound audio streams, mix it, and send it to the
  314. * hardware. Capture devices will feed any bound audio streams with a copy of
  315. * any incoming data.
  316. *
  317. * An opened audio device starts out with no audio streams bound. To start
  318. * audio playing, bind a stream and supply audio data to it. Unlike SDL2,
  319. * there is no audio callback; you only bind audio streams and make sure they
  320. * have data flowing into them (however, you can simulate SDL2's semantics
  321. * fairly closely by using SDL_OpenAudioDeviceStream instead of this
  322. * function).
  323. *
  324. * If you don't care about opening a specific device, pass a `devid` of either
  325. * `SDL_AUDIO_DEVICE_DEFAULT_OUTPUT` or `SDL_AUDIO_DEVICE_DEFAULT_CAPTURE`. In
  326. * this case, SDL will try to pick the most reasonable default, and may also
  327. * switch between physical devices seamlessly later, if the most reasonable
  328. * default changes during the lifetime of this opened device (user changed the
  329. * default in the OS's system preferences, the default got unplugged so the
  330. * system jumped to a new default, the user plugged in headphones on a mobile
  331. * device, etc). Unless you have a good reason to choose a specific device,
  332. * this is probably what you want.
  333. *
  334. * You may request a specific format for the audio device, but there is no
  335. * promise the device will honor that request for several reasons. As such,
  336. * it's only meant to be a hint as to what data your app will provide. Audio
  337. * streams will accept data in whatever format you specify and manage
  338. * conversion for you as appropriate. SDL_GetAudioDeviceFormat can tell you
  339. * the preferred format for the device before opening and the actual format
  340. * the device is using after opening.
  341. *
  342. * It's legal to open the same device ID more than once; each successful open
  343. * will generate a new logical SDL_AudioDeviceID that is managed separately
  344. * from others on the same physical device. This allows libraries to open a
  345. * device separately from the main app and bind its own streams without
  346. * conflicting.
  347. *
  348. * It is also legal to open a device ID returned by a previous call to this
  349. * function; doing so just creates another logical device on the same physical
  350. * device. This may be useful for making logical groupings of audio streams.
  351. *
  352. * This function returns the opened device ID on success. This is a new,
  353. * unique SDL_AudioDeviceID that represents a logical device.
  354. *
  355. * Some backends might offer arbitrary devices (for example, a networked audio
  356. * protocol that can connect to an arbitrary server). For these, as a change
  357. * from SDL2, you should open a default device ID and use an SDL hint to
  358. * specify the target if you care, or otherwise let the backend figure out a
  359. * reasonable default. Most backends don't offer anything like this, and often
  360. * this would be an end user setting an environment variable for their custom
  361. * need, and not something an application should specifically manage.
  362. *
  363. * When done with an audio device, possibly at the end of the app's life, one
  364. * should call SDL_CloseAudioDevice() on the returned device id.
  365. *
  366. * \param devid the device instance id to open, or
  367. * SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
  368. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE for the most reasonable
  369. * default device.
  370. * \param spec the requested device configuration. Can be NULL to use
  371. * reasonable defaults.
  372. * \returns The device ID on success, 0 on error; call SDL_GetError() for more
  373. * information.
  374. *
  375. * \threadsafety It is safe to call this function from any thread.
  376. *
  377. * \since This function is available since SDL 3.0.0.
  378. *
  379. * \sa SDL_CloseAudioDevice
  380. * \sa SDL_GetAudioDeviceFormat
  381. */
  382. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec);
  383. /**
  384. * Use this function to pause audio playback on a specified device.
  385. *
  386. * This function pauses audio processing for a given device. Any bound audio
  387. * streams will not progress, and no audio will be generated. Pausing one
  388. * device does not prevent other unpaused devices from running.
  389. *
  390. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  391. * has to bind a stream before any audio will flow. Pausing a paused device is
  392. * a legal no-op.
  393. *
  394. * Pausing a device can be useful to halt all audio without unbinding all the
  395. * audio streams. This might be useful while a game is paused, or a level is
  396. * loading, etc.
  397. *
  398. * Physical devices can not be paused or unpaused, only logical devices
  399. * created through SDL_OpenAudioDevice() can be.
  400. *
  401. * \param dev a device opened by SDL_OpenAudioDevice()
  402. * \returns 0 on success or a negative error code on failure; call
  403. * SDL_GetError() for more information.
  404. *
  405. * \threadsafety It is safe to call this function from any thread.
  406. *
  407. * \since This function is available since SDL 3.0.0.
  408. *
  409. * \sa SDL_ResumeAudioDevice
  410. * \sa SDL_AudioDevicePaused
  411. */
  412. extern DECLSPEC int SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev);
  413. /**
  414. * Use this function to unpause audio playback on a specified device.
  415. *
  416. * This function unpauses audio processing for a given device that has
  417. * previously been paused with SDL_PauseAudioDevice(). Once unpaused, any
  418. * bound audio streams will begin to progress again, and audio can be
  419. * generated.
  420. *
  421. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  422. * has to bind a stream before any audio will flow. Unpausing an unpaused
  423. * device is a legal no-op.
  424. *
  425. * Physical devices can not be paused or unpaused, only logical devices
  426. * created through SDL_OpenAudioDevice() can be.
  427. *
  428. * \param dev a device opened by SDL_OpenAudioDevice()
  429. * \returns 0 on success or a negative error code on failure; call
  430. * SDL_GetError() for more information.
  431. *
  432. * \threadsafety It is safe to call this function from any thread.
  433. *
  434. * \since This function is available since SDL 3.0.0.
  435. *
  436. * \sa SDL_ResumeAudioDevice
  437. * \sa SDL_AudioDevicePaused
  438. */
  439. extern DECLSPEC int SDLCALL SDL_ResumeAudioDevice(SDL_AudioDeviceID dev);
  440. /**
  441. * Use this function to query if an audio device is paused.
  442. *
  443. * Unlike in SDL2, audio devices start in an _unpaused_ state, since an app
  444. * has to bind a stream before any audio will flow.
  445. *
  446. * Physical devices can not be paused or unpaused, only logical devices
  447. * created through SDL_OpenAudioDevice() can be. Physical and invalid device
  448. * IDs will report themselves as unpaused here.
  449. *
  450. * \param dev a device opened by SDL_OpenAudioDevice()
  451. * \returns SDL_TRUE if device is valid and paused, SDL_FALSE otherwise.
  452. *
  453. * \threadsafety It is safe to call this function from any thread.
  454. *
  455. * \since This function is available since SDL 3.0.0.
  456. *
  457. * \sa SDL_PauseAudioDevice
  458. * \sa SDL_ResumeAudioDevice
  459. * \sa SDL_AudioDevicePaused
  460. */
  461. extern DECLSPEC SDL_bool SDLCALL SDL_AudioDevicePaused(SDL_AudioDeviceID dev);
  462. /**
  463. * Close a previously-opened audio device.
  464. *
  465. * The application should close open audio devices once they are no longer
  466. * needed.
  467. *
  468. * This function may block briefly while pending audio data is played by the
  469. * hardware, so that applications don't drop the last buffer of data they
  470. * supplied if terminating immediately afterwards.
  471. *
  472. * \param devid an audio device id previously returned by
  473. * SDL_OpenAudioDevice()
  474. *
  475. * \threadsafety It is safe to call this function from any thread.
  476. *
  477. * \since This function is available since SDL 3.0.0.
  478. *
  479. * \sa SDL_OpenAudioDevice
  480. */
  481. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID devid);
  482. /**
  483. * Bind a list of audio streams to an audio device.
  484. *
  485. * Audio data will flow through any bound streams. For an output device, data
  486. * for all bound streams will be mixed together and fed to the device. For a
  487. * capture device, a copy of recorded data will be provided to each bound
  488. * stream.
  489. *
  490. * Audio streams can only be bound to an open device. This operation is
  491. * atomic--all streams bound in the same call will start processing at the
  492. * same time, so they can stay in sync. Also: either all streams will be bound
  493. * or none of them will be.
  494. *
  495. * It is an error to bind an already-bound stream; it must be explicitly
  496. * unbound first.
  497. *
  498. * Binding a stream to a device will set its output format for output devices,
  499. * and its input format for capture devices, so they match the device's
  500. * settings. The caller is welcome to change the other end of the stream's
  501. * format at any time.
  502. *
  503. * \param devid an audio device to bind a stream to.
  504. * \param streams an array of audio streams to unbind.
  505. * \param num_streams Number streams listed in the `streams` array.
  506. * \returns 0 on success, -1 on error; call SDL_GetError() for more
  507. * information.
  508. *
  509. * \threadsafety It is safe to call this function from any thread.
  510. *
  511. * \since This function is available since SDL 3.0.0.
  512. *
  513. * \sa SDL_BindAudioStreams
  514. * \sa SDL_UnbindAudioStreams
  515. * \sa SDL_UnbindAudioStream
  516. * \sa SDL_GetAudioStreamDevice
  517. */
  518. extern DECLSPEC int SDLCALL SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream **streams, int num_streams);
  519. /**
  520. * Bind a single audio stream to an audio device.
  521. *
  522. * This is a convenience function, equivalent to calling
  523. * `SDL_BindAudioStreams(devid, &stream, 1)`.
  524. *
  525. * \param devid an audio device to bind a stream to.
  526. * \param stream an audio stream to bind to a device.
  527. * \returns 0 on success, -1 on error; call SDL_GetError() for more
  528. * information.
  529. *
  530. * \threadsafety It is safe to call this function from any thread.
  531. *
  532. * \since This function is available since SDL 3.0.0.
  533. *
  534. * \sa SDL_BindAudioStreams
  535. * \sa SDL_UnbindAudioStreams
  536. * \sa SDL_UnbindAudioStream
  537. * \sa SDL_GetAudioStreamDevice
  538. */
  539. extern DECLSPEC int SDLCALL SDL_BindAudioStream(SDL_AudioDeviceID devid, SDL_AudioStream *stream);
  540. /**
  541. * Unbind a list of audio streams from their audio devices.
  542. *
  543. * The streams being unbound do not all have to be on the same device. All
  544. * streams on the same device will be unbound atomically (data will stop
  545. * flowing through them all unbound streams on the same device at the same
  546. * time).
  547. *
  548. * Unbinding a stream that isn't bound to a device is a legal no-op.
  549. *
  550. * \param streams an array of audio streams to unbind.
  551. * \param num_streams Number streams listed in the `streams` array.
  552. *
  553. * \threadsafety It is safe to call this function from any thread.
  554. *
  555. * \since This function is available since SDL 3.0.0.
  556. *
  557. * \sa SDL_BindAudioStreams
  558. * \sa SDL_BindAudioStream
  559. * \sa SDL_UnbindAudioStream
  560. * \sa SDL_GetAudioStreamDevice
  561. */
  562. extern DECLSPEC void SDLCALL SDL_UnbindAudioStreams(SDL_AudioStream **streams, int num_streams);
  563. /**
  564. * Unbind a single audio stream from its audio device.
  565. *
  566. * This is a convenience function, equivalent to calling
  567. * `SDL_UnbindAudioStreams(&stream, 1)`.
  568. *
  569. * \param stream an audio stream to unbind from a device.
  570. *
  571. * \threadsafety It is safe to call this function from any thread.
  572. *
  573. * \since This function is available since SDL 3.0.0.
  574. *
  575. * \sa SDL_BindAudioStream
  576. * \sa SDL_BindAudioStreams
  577. * \sa SDL_UnbindAudioStreams
  578. * \sa SDL_GetAudioStreamDevice
  579. */
  580. extern DECLSPEC void SDLCALL SDL_UnbindAudioStream(SDL_AudioStream *stream);
  581. /**
  582. * Query an audio stream for its currently-bound device.
  583. *
  584. * This reports the audio device that an audio stream is currently bound to.
  585. *
  586. * If not bound, or invalid, this returns zero, which is not a valid device
  587. * ID.
  588. *
  589. * \param stream the audio stream to query.
  590. * \returns The bound audio device, or 0 if not bound or invalid.
  591. *
  592. * \threadsafety It is safe to call this function from any thread.
  593. *
  594. * \since This function is available since SDL 3.0.0.
  595. *
  596. * \sa SDL_BindAudioStream
  597. * \sa SDL_BindAudioStreams
  598. * \sa SDL_UnbindAudioStream
  599. * \sa SDL_UnbindAudioStreams
  600. */
  601. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_GetAudioStreamDevice(SDL_AudioStream *stream);
  602. /**
  603. * Create a new audio stream.
  604. *
  605. * \param src_spec The format details of the input audio
  606. * \param dst_spec The format details of the output audio
  607. * \returns 0 on success, or -1 on error.
  608. *
  609. * \threadsafety It is safe to call this function from any thread.
  610. *
  611. * \since This function is available since SDL 3.0.0.
  612. *
  613. * \sa SDL_PutAudioStreamData
  614. * \sa SDL_GetAudioStreamData
  615. * \sa SDL_GetAudioStreamAvailable
  616. * \sa SDL_FlushAudioStream
  617. * \sa SDL_ClearAudioStream
  618. * \sa SDL_ChangeAudioStreamOutput
  619. * \sa SDL_DestroyAudioStream
  620. */
  621. extern DECLSPEC SDL_AudioStream *SDLCALL SDL_CreateAudioStream(const SDL_AudioSpec *src_spec, const SDL_AudioSpec *dst_spec);
  622. /**
  623. * Query the current format of an audio stream.
  624. *
  625. * \param stream the SDL_AudioStream to query.
  626. * \param src_spec Where to store the input audio format; ignored if NULL.
  627. * \param dst_spec Where to store the output audio format; ignored if NULL.
  628. * \returns 0 on success, or -1 on error.
  629. *
  630. * \threadsafety It is safe to call this function from any thread, as it holds
  631. * a stream-specific mutex while running.
  632. *
  633. * \since This function is available since SDL 3.0.0.
  634. */
  635. extern DECLSPEC int SDLCALL SDL_GetAudioStreamFormat(SDL_AudioStream *stream,
  636. SDL_AudioSpec *src_spec,
  637. SDL_AudioSpec *dst_spec);
  638. /**
  639. * Change the input and output formats of an audio stream.
  640. *
  641. * Future calls to and SDL_GetAudioStreamAvailable and SDL_GetAudioStreamData
  642. * will reflect the new format, and future calls to SDL_PutAudioStreamData
  643. * must provide data in the new input formats.
  644. *
  645. * \param stream The stream the format is being changed
  646. * \param src_spec The new format of the audio input; if NULL, it is not
  647. * changed.
  648. * \param dst_spec The new format of the audio output; if NULL, it is not
  649. * changed.
  650. * \returns 0 on success, or -1 on error.
  651. *
  652. * \threadsafety It is safe to call this function from any thread, as it holds
  653. * a stream-specific mutex while running.
  654. *
  655. * \since This function is available since SDL 3.0.0.
  656. *
  657. * \sa SDL_GetAudioStreamFormat
  658. * \sa SDL_PutAudioStreamData
  659. * \sa SDL_GetAudioStreamData
  660. * \sa SDL_GetAudioStreamAvailable
  661. * \sa SDL_SetAudioStreamFrequencyRatio
  662. */
  663. extern DECLSPEC int SDLCALL SDL_SetAudioStreamFormat(SDL_AudioStream *stream,
  664. const SDL_AudioSpec *src_spec,
  665. const SDL_AudioSpec *dst_spec);
  666. /**
  667. * Get the frequency ratio of an audio stream.
  668. *
  669. * \param stream the SDL_AudioStream to query.
  670. * \returns the frequency ratio of the stream, or 0.0 on error
  671. *
  672. * \threadsafety It is safe to call this function from any thread, as it holds
  673. * a stream-specific mutex while running.
  674. *
  675. * \since This function is available since SDL 3.0.0.
  676. *
  677. * \sa SDL_SetAudioStreamFrequencyRatio
  678. */
  679. extern DECLSPEC float SDLCALL SDL_GetAudioStreamFrequencyRatio(SDL_AudioStream *stream);
  680. /**
  681. * Change the frequency ratio of an audio stream.
  682. *
  683. * The frequency ratio is used to adjust the rate at which input data is
  684. * consumed. Changing this effectively modifies the speed and pitch of the
  685. * audio. A value greater than 1.0 will play the audio faster, and at a higher
  686. * pitch. A value less than 1.0 will play the audio slower, and at a lower
  687. * pitch.
  688. *
  689. * This is applied during SDL_GetAudioStreamData, and can be continuously
  690. * changed to create various effects.
  691. *
  692. * \param stream The stream the frequency ratio is being changed
  693. * \param ratio The frequency ratio. 1.0 is normal speed. Must be between 0.01
  694. * and 100.
  695. * \returns 0 on success, or -1 on error.
  696. *
  697. * \threadsafety It is safe to call this function from any thread, as it holds
  698. * a stream-specific mutex while running.
  699. *
  700. * \since This function is available since SDL 3.0.0.
  701. *
  702. * \sa SDL_GetAudioStreamFrequencyRatio
  703. * \sa SDL_SetAudioStreamFormat
  704. */
  705. extern DECLSPEC int SDLCALL SDL_SetAudioStreamFrequencyRatio(SDL_AudioStream *stream, float ratio);
  706. /**
  707. * Add data to be converted/resampled to the stream.
  708. *
  709. * This data must match the format/channels/samplerate specified in the latest
  710. * call to SDL_SetAudioStreamFormat, or the format specified when creating the
  711. * stream if it hasn't been changed.
  712. *
  713. * Note that this call simply queues unconverted data for later. This is
  714. * different than SDL2, where data was converted during the Put call and the
  715. * Get call would just dequeue the previously-converted data.
  716. *
  717. * \param stream The stream the audio data is being added to
  718. * \param buf A pointer to the audio data to add
  719. * \param len The number of bytes to write to the stream
  720. * \returns 0 on success or a negative error code on failure; call
  721. * SDL_GetError() for more information.
  722. *
  723. * \threadsafety It is safe to call this function from any thread, but if the
  724. * stream has a callback set, the caller might need to manage
  725. * extra locking.
  726. *
  727. * \since This function is available since SDL 3.0.0.
  728. *
  729. * \sa SDL_CreateAudioStream
  730. * \sa SDL_GetAudioStreamData
  731. * \sa SDL_GetAudioStreamAvailable
  732. * \sa SDL_FlushAudioStream
  733. * \sa SDL_ClearAudioStream
  734. * \sa SDL_DestroyAudioStream
  735. */
  736. extern DECLSPEC int SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream, const void *buf, int len);
  737. /**
  738. * Get converted/resampled data from the stream.
  739. *
  740. * The input/output data format/channels/samplerate is specified when creating
  741. * the stream, and can be changed after creation by calling
  742. * SDL_SetAudioStreamFormat.
  743. *
  744. * Note that any conversion and resampling necessary is done during this call,
  745. * and SDL_PutAudioStreamData simply queues unconverted data for later. This
  746. * is different than SDL2, where that work was done while inputting new data
  747. * to the stream and requesting the output just copied the converted data.
  748. *
  749. * \param stream The stream the audio is being requested from
  750. * \param buf A buffer to fill with audio data
  751. * \param len The maximum number of bytes to fill
  752. * \returns the number of bytes read from the stream, or -1 on error
  753. *
  754. * \threadsafety It is safe to call this function from any thread, but if the
  755. * stream has a callback set, the caller might need to manage
  756. * extra locking.
  757. *
  758. * \since This function is available since SDL 3.0.0.
  759. *
  760. * \sa SDL_CreateAudioStream
  761. * \sa SDL_PutAudioStreamData
  762. * \sa SDL_GetAudioStreamAvailable
  763. * \sa SDL_SetAudioStreamFormat
  764. * \sa SDL_FlushAudioStream
  765. * \sa SDL_ClearAudioStream
  766. * \sa SDL_DestroyAudioStream
  767. */
  768. extern DECLSPEC int SDLCALL SDL_GetAudioStreamData(SDL_AudioStream *stream, void *buf, int len);
  769. /**
  770. * Get the number of converted/resampled bytes available.
  771. *
  772. * The stream may be buffering data behind the scenes until it has enough to
  773. * resample correctly, so this number might be lower than what you expect, or
  774. * even be zero. Add more data or flush the stream if you need the data now.
  775. *
  776. * If the stream has so much data that it would overflow an int, the return
  777. * value is clamped to a maximum value, but no queued data is lost; if there
  778. * are gigabytes of data queued, the app might need to read some of it with
  779. * SDL_GetAudioStreamData before this function's return value is no longer
  780. * clamped.
  781. *
  782. * \param stream The audio stream to query
  783. * \returns the number of converted/resampled bytes available.
  784. *
  785. * \threadsafety It is safe to call this function from any thread.
  786. *
  787. * \since This function is available since SDL 3.0.0.
  788. *
  789. * \sa SDL_CreateAudioStream
  790. * \sa SDL_PutAudioStreamData
  791. * \sa SDL_GetAudioStreamData
  792. * \sa SDL_FlushAudioStream
  793. * \sa SDL_ClearAudioStream
  794. * \sa SDL_DestroyAudioStream
  795. */
  796. extern DECLSPEC int SDLCALL SDL_GetAudioStreamAvailable(SDL_AudioStream *stream);
  797. /**
  798. * Tell the stream that you're done sending data, and anything being buffered
  799. * should be converted/resampled and made available immediately.
  800. *
  801. * It is legal to add more data to a stream after flushing, but there will be
  802. * audio gaps in the output. Generally this is intended to signal the end of
  803. * input, so the complete output becomes available.
  804. *
  805. * \param stream The audio stream to flush
  806. * \returns 0 on success or a negative error code on failure; call
  807. * SDL_GetError() for more information.
  808. *
  809. * \threadsafety It is safe to call this function from any thread.
  810. *
  811. * \since This function is available since SDL 3.0.0.
  812. *
  813. * \sa SDL_CreateAudioStream
  814. * \sa SDL_PutAudioStreamData
  815. * \sa SDL_GetAudioStreamData
  816. * \sa SDL_GetAudioStreamAvailable
  817. * \sa SDL_ClearAudioStream
  818. * \sa SDL_DestroyAudioStream
  819. */
  820. extern DECLSPEC int SDLCALL SDL_FlushAudioStream(SDL_AudioStream *stream);
  821. /**
  822. * Clear any pending data in the stream without converting it
  823. *
  824. * \param stream The audio stream to clear
  825. * \returns 0 on success or a negative error code on failure; call
  826. * SDL_GetError() for more information.
  827. *
  828. * \threadsafety It is safe to call this function from any thread.
  829. *
  830. * \since This function is available since SDL 3.0.0.
  831. *
  832. * \sa SDL_CreateAudioStream
  833. * \sa SDL_PutAudioStreamData
  834. * \sa SDL_GetAudioStreamData
  835. * \sa SDL_GetAudioStreamAvailable
  836. * \sa SDL_FlushAudioStream
  837. * \sa SDL_DestroyAudioStream
  838. */
  839. extern DECLSPEC int SDLCALL SDL_ClearAudioStream(SDL_AudioStream *stream);
  840. /**
  841. * Lock an audio stream for serialized access.
  842. *
  843. * Each SDL_AudioStream has an internal mutex it uses to protect its data
  844. * structures from threading conflicts. This function allows an app to lock
  845. * that mutex, which could be useful if registering callbacks on this stream.
  846. *
  847. * One does not need to lock a stream to use in it most cases, as the stream
  848. * manages this lock internally. However, this lock is held during callbacks,
  849. * which may run from arbitrary threads at any time, so if an app needs to
  850. * protect shared data during those callbacks, locking the stream guarantees
  851. * that the callback is not running while the lock is held.
  852. *
  853. * As this is just a wrapper over SDL_LockMutex for an internal lock, it has
  854. * all the same attributes (recursive locks are allowed, etc).
  855. *
  856. * \param stream The audio stream to lock.
  857. * \returns 0 on success or a negative error code on failure; call
  858. * SDL_GetError() for more information.
  859. *
  860. * \threadsafety It is safe to call this function from any thread.
  861. *
  862. * \since This function is available since SDL 3.0.0.
  863. *
  864. * \sa SDL_UnlockAudioStream
  865. * \sa SDL_SetAudioStreamPutCallback
  866. * \sa SDL_SetAudioStreamGetCallback
  867. */
  868. extern DECLSPEC int SDLCALL SDL_LockAudioStream(SDL_AudioStream *stream);
  869. /**
  870. * Unlock an audio stream for serialized access.
  871. *
  872. * This unlocks an audio stream after a call to SDL_LockAudioStream.
  873. *
  874. * \param stream The audio stream to unlock.
  875. * \returns 0 on success or a negative error code on failure; call
  876. * SDL_GetError() for more information.
  877. *
  878. * \threadsafety You should only call this from the same thread that
  879. * previously called SDL_LockAudioStream.
  880. *
  881. * \since This function is available since SDL 3.0.0.
  882. *
  883. * \sa SDL_LockAudioStream
  884. * \sa SDL_SetAudioStreamPutCallback
  885. * \sa SDL_SetAudioStreamGetCallback
  886. */
  887. extern DECLSPEC int SDLCALL SDL_UnlockAudioStream(SDL_AudioStream *stream);
  888. /**
  889. * A callback that fires when data passes through an SDL_AudioStream.
  890. *
  891. * Apps can (optionally) register a callback with an audio stream that
  892. * is called when data is added with SDL_PutAudioStreamData, or requested
  893. * with SDL_GetAudioStreamData. These callbacks may run from any
  894. * thread, so if you need to protect shared data, you should use
  895. * SDL_LockAudioStream to serialize access; this lock will be held by
  896. * before your callback is called, so your callback does not need to
  897. * manage the lock explicitly.
  898. *
  899. * Two values are offered here: one is the amount of additional data needed
  900. * to satisfy the immediate request (which might be zero if the stream
  901. * already has enough data queued) and the other is the total amount
  902. * being requested. In a Get call triggering a Put callback, these
  903. * values can be different. In a Put call triggering a Get callback,
  904. * these values are always the same.
  905. *
  906. * Byte counts might be slightly overestimated due to buffering or
  907. * resampling, and may change from call to call.
  908. *
  909. * \param stream The SDL audio stream associated with this callback.
  910. * \param additional The amount of data, in bytes, that is needed right now
  911. * \param total_amount The total amount of data requested, in bytes, that is requested or available.
  912. * \param userdata An opaque pointer provided by the app for their personal use.
  913. */
  914. typedef void (SDLCALL *SDL_AudioStreamCallback)(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount);
  915. /**
  916. * Set a callback that runs when data is requested from an audio stream.
  917. *
  918. * This callback is called _before_ data is obtained from the stream, giving
  919. * the callback the chance to add more on-demand.
  920. *
  921. * The callback can (optionally) call SDL_PutAudioStreamData() to add more
  922. * audio to the stream during this call; if needed, the request that triggered
  923. * this callback will obtain the new data immediately.
  924. *
  925. * The callback's `approx_request` argument is roughly how many bytes of
  926. * _unconverted_ data (in the stream's input format) is needed by the caller,
  927. * although this may overestimate a little for safety. This takes into account
  928. * how much is already in the stream and only asks for any extra necessary to
  929. * resolve the request, which means the callback may be asked for zero bytes,
  930. * and a different amount on each call.
  931. *
  932. * The callback is not required to supply exact amounts; it is allowed to
  933. * supply too much or too little or none at all. The caller will get what's
  934. * available, up to the amount they requested, regardless of this callback's
  935. * outcome.
  936. *
  937. * Clearing or flushing an audio stream does not call this callback.
  938. *
  939. * This function obtains the stream's lock, which means any existing callback
  940. * (get or put) in progress will finish running before setting the new
  941. * callback.
  942. *
  943. * Setting a NULL function turns off the callback.
  944. *
  945. * \param stream the audio stream to set the new callback on.
  946. * \param callback the new callback function to call when data is added to the
  947. * stream.
  948. * \param userdata an opaque pointer provided to the callback for its own
  949. * personal use.
  950. * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
  951. *
  952. * \threadsafety It is safe to call this function from any thread.
  953. *
  954. * \since This function is available since SDL 3.0.0.
  955. *
  956. * \sa SDL_SetAudioStreamPutCallback
  957. */
  958. extern DECLSPEC int SDLCALL SDL_SetAudioStreamGetCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
  959. /**
  960. * Set a callback that runs when data is added to an audio stream.
  961. *
  962. * This callback is called _after_ the data is added to the stream, giving the
  963. * callback the chance to obtain it immediately.
  964. *
  965. * The callback can (optionally) call SDL_GetAudioStreamData() to obtain audio
  966. * from the stream during this call.
  967. *
  968. * The callback's `approx_request` argument is how many bytes of _converted_
  969. * data (in the stream's output format) was provided by the caller, although
  970. * this may underestimate a little for safety. This value might be less than
  971. * what is currently available in the stream, if data was already there, and
  972. * might be less than the caller provided if the stream needs to keep a buffer
  973. * to aid in resampling. Which means the callback may be provided with zero
  974. * bytes, and a different amount on each call.
  975. *
  976. * The callback may call SDL_GetAudioStreamAvailable to see the total amount
  977. * currently available to read from the stream, instead of the total provided
  978. * by the current call.
  979. *
  980. * The callback is not required to obtain all data. It is allowed to read less
  981. * or none at all. Anything not read now simply remains in the stream for
  982. * later access.
  983. *
  984. * Clearing or flushing an audio stream does not call this callback.
  985. *
  986. * This function obtains the stream's lock, which means any existing callback
  987. * (get or put) in progress will finish running before setting the new
  988. * callback.
  989. *
  990. * Setting a NULL function turns off the callback.
  991. *
  992. * \param stream the audio stream to set the new callback on.
  993. * \param callback the new callback function to call when data is added to the
  994. * stream.
  995. * \param userdata an opaque pointer provided to the callback for its own
  996. * personal use.
  997. * \returns 0 on success, -1 on error. This only fails if `stream` is NULL.
  998. *
  999. * \threadsafety It is safe to call this function from any thread.
  1000. *
  1001. * \since This function is available since SDL 3.0.0.
  1002. *
  1003. * \sa SDL_SetAudioStreamGetCallback
  1004. */
  1005. extern DECLSPEC int SDLCALL SDL_SetAudioStreamPutCallback(SDL_AudioStream *stream, SDL_AudioStreamCallback callback, void *userdata);
  1006. /**
  1007. * Free an audio stream
  1008. *
  1009. * \param stream The audio stream to free
  1010. *
  1011. * \threadsafety It is safe to call this function from any thread.
  1012. *
  1013. * \since This function is available since SDL 3.0.0.
  1014. *
  1015. * \sa SDL_CreateAudioStream
  1016. * \sa SDL_PutAudioStreamData
  1017. * \sa SDL_GetAudioStreamData
  1018. * \sa SDL_GetAudioStreamAvailable
  1019. * \sa SDL_FlushAudioStream
  1020. * \sa SDL_ClearAudioStream
  1021. */
  1022. extern DECLSPEC void SDLCALL SDL_DestroyAudioStream(SDL_AudioStream *stream);
  1023. /**
  1024. * Convenience function for straightforward audio init for the common case.
  1025. *
  1026. * If all your app intends to do is provide a single source of PCM audio, this
  1027. * function allows you to do all your audio setup in a single call.
  1028. *
  1029. * This is intended to be a clean means to migrate apps from SDL2.
  1030. *
  1031. * This function will open an audio device, create a stream and bind it.
  1032. * Unlike other methods of setup, the audio device will be closed when this
  1033. * stream is destroyed, so the app can treat the returned SDL_AudioStream as
  1034. * the only object needed to manage audio playback.
  1035. *
  1036. * Also unlike other functions, the audio device begins paused. This is to map
  1037. * more closely to SDL2-style behavior, and since there is no extra step here
  1038. * to bind a stream to begin audio flowing. The audio device should be resumed
  1039. * with SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream));
  1040. *
  1041. * This function works with both playback and capture devices.
  1042. *
  1043. * The `spec` parameter represents the app's side of the audio stream. That
  1044. * is, for recording audio, this will be the output format, and for playing
  1045. * audio, this will be the input format.
  1046. *
  1047. * If you don't care about opening a specific audio device, you can (and
  1048. * probably _should_), use SDL_AUDIO_DEVICE_DEFAULT_OUTPUT for playback and
  1049. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE for recording.
  1050. *
  1051. * One can optionally provide a callback function; if NULL, the app is
  1052. * expected to queue audio data for playback (or unqueue audio data if
  1053. * capturing). Otherwise, the callback will begin to fire once the device is
  1054. * unpaused.
  1055. *
  1056. * \param devid an audio device to open, or SDL_AUDIO_DEVICE_DEFAULT_OUTPUT or
  1057. * SDL_AUDIO_DEVICE_DEFAULT_CAPTURE.
  1058. * \param spec the audio stream's data format. Required.
  1059. * \param callback A callback where the app will provide new data for
  1060. * playback, or receive new data for capture. Can be NULL, in
  1061. * which case the app will need to call SDL_PutAudioStreamData
  1062. * or SDL_GetAudioStreamData as necessary.
  1063. * \param userdata App-controlled pointer passed to callback. Can be NULL.
  1064. * Ignored if callback is NULL.
  1065. * \returns an audio stream on success, ready to use. NULL on error; call
  1066. * SDL_GetError() for more information. When done with this stream,
  1067. * call SDL_DestroyAudioStream to free resources and close the
  1068. * device.
  1069. *
  1070. * \threadsafety It is safe to call this function from any thread.
  1071. *
  1072. * \since This function is available since SDL 3.0.0.
  1073. *
  1074. * \sa SDL_GetAudioStreamDevice
  1075. * \sa SDL_ResumeAudioDevice
  1076. */
  1077. extern DECLSPEC SDL_AudioStream *SDLCALL SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_AudioSpec *spec, SDL_AudioStreamCallback callback, void *userdata);
  1078. /**
  1079. * A callback that fires when data is about to be fed to an audio device.
  1080. *
  1081. * This is useful for accessing the final mix, perhaps for writing a
  1082. * visualizer or applying a final effect to the audio data before playback.
  1083. *
  1084. * \sa SDL_SetAudioDevicePostmixCallback
  1085. */
  1086. typedef void (SDLCALL *SDL_AudioPostmixCallback)(void *userdata, const SDL_AudioSpec *spec, float *buffer, int buflen);
  1087. /**
  1088. * Set a callback that fires when data is about to be fed to an audio device.
  1089. *
  1090. * This is useful for accessing the final mix, perhaps for writing a
  1091. * visualizer or applying a final effect to the audio data before playback.
  1092. *
  1093. * The buffer is the final mix of all bound audio streams on an opened device;
  1094. * this callback will fire regularly for any device that is both opened and
  1095. * unpaused. If there is no new data to mix, either because no streams are
  1096. * bound to the device or all the streams are empty, this callback will still
  1097. * fire with the entire buffer set to silence.
  1098. *
  1099. * This callback is allowed to make changes to the data; the contents of the
  1100. * buffer after this call is what is ultimately passed along to the hardware.
  1101. *
  1102. * The callback is always provided the data in float format (values from -1.0f
  1103. * to 1.0f), but the number of channels or sample rate may be different than
  1104. * the format the app requested when opening the device; SDL might have had to
  1105. * manage a conversion behind the scenes, or the playback might have jumped to
  1106. * new physical hardware when a system default changed, etc. These details may
  1107. * change between calls. Accordingly, the size of the buffer might change
  1108. * between calls as well.
  1109. *
  1110. * This callback can run at any time, and from any thread; if you need to
  1111. * serialize access to your app's data, you should provide and use a mutex or
  1112. * other synchronization device.
  1113. *
  1114. * All of this to say: there are specific needs this callback can fulfill, but
  1115. * it is not the simplest interface. Apps should generally provide audio in
  1116. * their preferred format through an SDL_AudioStream and let SDL handle the
  1117. * difference.
  1118. *
  1119. * This function is extremely time-sensitive; the callback should do the least
  1120. * amount of work possible and return as quickly as it can. The longer the
  1121. * callback runs, the higher the risk of audio dropouts or other problems.
  1122. *
  1123. * This function will block until the audio device is in between iterations,
  1124. * so any existing callback that might be running will finish before this
  1125. * function sets the new callback and returns.
  1126. *
  1127. * Setting a NULL callback function disables any previously-set callback.
  1128. *
  1129. * \param devid The ID of an opened audio device.
  1130. * \param callback A callback function to be called. Can be NULL.
  1131. * \param userdata App-controlled pointer passed to callback. Can be NULL.
  1132. * \returns zero on success, -1 on error; call SDL_GetError() for more
  1133. * information.
  1134. *
  1135. * \threadsafety It is safe to call this function from any thread.
  1136. *
  1137. * \since This function is available since SDL 3.0.0.
  1138. */
  1139. extern DECLSPEC int SDLCALL SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallback callback, void *userdata);
  1140. /**
  1141. * Load the audio data of a WAVE file into memory.
  1142. *
  1143. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  1144. * be valid pointers. The entire data portion of the file is then loaded into
  1145. * memory and decoded if necessary.
  1146. *
  1147. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  1148. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  1149. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  1150. * cause an error.
  1151. *
  1152. * If this function succeeds, the return value is zero and the pointer to the
  1153. * audio data allocated by the function is written to `audio_buf` and its
  1154. * length in bytes to `audio_len`. The SDL_AudioSpec members `freq`,
  1155. * `channels`, and `format` are set to the values of the audio data in the
  1156. * buffer. The `samples` member is set to a sane default and all others are
  1157. * set to zero.
  1158. *
  1159. * It's necessary to use SDL_free() to free the audio data returned in
  1160. * `audio_buf` when it is no longer used.
  1161. *
  1162. * Because of the underspecification of the .WAV format, there are many
  1163. * problematic files in the wild that cause issues with strict decoders. To
  1164. * provide compatibility with these files, this decoder is lenient in regards
  1165. * to the truncation of the file, the fact chunk, and the size of the RIFF
  1166. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  1167. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  1168. * tune the behavior of the loading process.
  1169. *
  1170. * Any file that is invalid (due to truncation, corruption, or wrong values in
  1171. * the headers), too big, or unsupported causes an error. Additionally, any
  1172. * critical I/O error from the data source will terminate the loading process
  1173. * with an error. The function returns NULL on error and in all cases (with
  1174. * the exception of `src` being NULL), an appropriate error message will be
  1175. * set.
  1176. *
  1177. * It is required that the data source supports seeking.
  1178. *
  1179. * Example:
  1180. *
  1181. * ```c
  1182. * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
  1183. * ```
  1184. *
  1185. * Note that the SDL_LoadWAV function does this same thing for you, but in a
  1186. * less messy way:
  1187. *
  1188. * ```c
  1189. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  1190. * ```
  1191. *
  1192. * \param src The data source for the WAVE data
  1193. * \param freesrc If SDL_TRUE, calls SDL_RWclose() on `src` before returning,
  1194. * even in the case of an error
  1195. * \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE
  1196. * data's format details on successful return
  1197. * \param audio_buf A pointer filled with the audio data, allocated by the
  1198. * function
  1199. * \param audio_len A pointer filled with the length of the audio data buffer
  1200. * in bytes
  1201. * \returns This function, if successfully called, returns 0. `audio_buf` will
  1202. * be filled with a pointer to an allocated buffer containing the
  1203. * audio data, and `audio_len` is filled with the length of that
  1204. * audio buffer in bytes.
  1205. *
  1206. * This function returns -1 if the .WAV file cannot be opened, uses
  1207. * an unknown data format, or is corrupt; call SDL_GetError() for
  1208. * more information.
  1209. *
  1210. * When the application is done with the data returned in
  1211. * `audio_buf`, it should call SDL_free() to dispose of it.
  1212. *
  1213. * \threadsafety It is safe to call this function from any thread.
  1214. *
  1215. * \since This function is available since SDL 3.0.0.
  1216. *
  1217. * \sa SDL_free
  1218. * \sa SDL_LoadWAV
  1219. */
  1220. extern DECLSPEC int SDLCALL SDL_LoadWAV_RW(SDL_RWops * src, SDL_bool freesrc,
  1221. SDL_AudioSpec * spec, Uint8 ** audio_buf,
  1222. Uint32 * audio_len);
  1223. /**
  1224. * Loads a WAV from a file path.
  1225. *
  1226. * This is a convenience function that is effectively the same as:
  1227. *
  1228. * ```c
  1229. * SDL_LoadWAV_RW(SDL_RWFromFile(path, "rb"), 1, spec, audio_buf, audio_len);
  1230. * ```
  1231. *
  1232. * Note that in SDL2, this was a preprocessor macro and not a real function.
  1233. *
  1234. * \param path The file path of the WAV file to open.
  1235. * \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE
  1236. * data's format details on successful return.
  1237. * \param audio_buf A pointer filled with the audio data, allocated by the
  1238. * function.
  1239. * \param audio_len A pointer filled with the length of the audio data buffer
  1240. * in bytes
  1241. * \returns This function, if successfully called, returns 0. `audio_buf` will
  1242. * be filled with a pointer to an allocated buffer containing the
  1243. * audio data, and `audio_len` is filled with the length of that
  1244. * audio buffer in bytes.
  1245. *
  1246. * This function returns -1 if the .WAV file cannot be opened, uses
  1247. * an unknown data format, or is corrupt; call SDL_GetError() for
  1248. * more information.
  1249. *
  1250. * When the application is done with the data returned in
  1251. * `audio_buf`, it should call SDL_free() to dispose of it.
  1252. *
  1253. * \threadsafety It is safe to call this function from any thread.
  1254. *
  1255. * \since This function is available since SDL 3.0.0.
  1256. *
  1257. * \sa SDL_free
  1258. * \sa SDL_LoadWAV_RW
  1259. */
  1260. extern DECLSPEC int SDLCALL SDL_LoadWAV(const char *path, SDL_AudioSpec * spec,
  1261. Uint8 ** audio_buf, Uint32 * audio_len);
  1262. #define SDL_MIX_MAXVOLUME 128
  1263. /**
  1264. * Mix audio data in a specified format.
  1265. *
  1266. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  1267. * it into `dst`, performing addition, volume adjustment, and overflow
  1268. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  1269. * `format` data.
  1270. *
  1271. * This is provided for convenience -- you can mix your own audio data.
  1272. *
  1273. * Do not use this function for mixing together more than two streams of
  1274. * sample data. The output from repeated application of this function may be
  1275. * distorted by clipping, because there is no accumulator with greater range
  1276. * than the input (not to mention this being an inefficient way of doing it).
  1277. *
  1278. * It is a common misconception that this function is required to write audio
  1279. * data to an output stream in an audio callback. While you can do that,
  1280. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  1281. * audio stream with a volume adjustment.
  1282. *
  1283. * \param dst the destination for the mixed audio
  1284. * \param src the source audio buffer to be mixed
  1285. * \param format the SDL_AudioFormat structure representing the desired audio
  1286. * format
  1287. * \param len the length of the audio buffer in bytes
  1288. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  1289. * for full audio volume
  1290. * \returns 0 on success or a negative error code on failure; call
  1291. * SDL_GetError() for more information.
  1292. *
  1293. * \threadsafety It is safe to call this function from any thread.
  1294. *
  1295. * \since This function is available since SDL 3.0.0.
  1296. */
  1297. extern DECLSPEC int SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  1298. const Uint8 * src,
  1299. SDL_AudioFormat format,
  1300. Uint32 len, int volume);
  1301. /**
  1302. * Convert some audio data of one format to another format.
  1303. *
  1304. * Please note that this function is for convenience, but should not be used
  1305. * to resample audio in blocks, as it will introduce audio artifacts on the
  1306. * boundaries. You should only use this function if you are converting audio
  1307. * data in its entirety in one call. If you want to convert audio in smaller
  1308. * chunks, use an SDL_AudioStream, which is designed for this situation.
  1309. *
  1310. * Internally, this function creates and destroys an SDL_AudioStream on each
  1311. * use, so it's also less efficient than using one directly, if you need to
  1312. * convert multiple times.
  1313. *
  1314. * \param src_spec The format details of the input audio
  1315. * \param src_data The audio data to be converted
  1316. * \param src_len The len of src_data
  1317. * \param dst_spec The format details of the output audio
  1318. * \param dst_data Will be filled with a pointer to converted audio data,
  1319. * which should be freed with SDL_free(). On error, it will be
  1320. * NULL.
  1321. * \param dst_len Will be filled with the len of dst_data
  1322. * \returns 0 on success or a negative error code on failure; call
  1323. * SDL_GetError() for more information.
  1324. *
  1325. * \threadsafety It is safe to call this function from any thread.
  1326. *
  1327. * \since This function is available since SDL 3.0.0.
  1328. *
  1329. * \sa SDL_CreateAudioStream
  1330. */
  1331. extern DECLSPEC int SDLCALL SDL_ConvertAudioSamples(const SDL_AudioSpec *src_spec,
  1332. const Uint8 *src_data,
  1333. int src_len,
  1334. const SDL_AudioSpec *dst_spec,
  1335. Uint8 **dst_data,
  1336. int *dst_len);
  1337. /**
  1338. * Get the appropriate memset value for silencing an audio format.
  1339. *
  1340. * The value returned by this function can be used as the second argument to
  1341. * memset (or SDL_memset) to set an audio buffer in a specific format to
  1342. * silence.
  1343. *
  1344. * \param format the audio data format to query.
  1345. * \returns A byte value that can be passed to memset.
  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. extern DECLSPEC int SDLCALL SDL_GetSilenceValueForFormat(SDL_AudioFormat format);
  1352. /* Ends C function definitions when using C++ */
  1353. #ifdef __cplusplus
  1354. }
  1355. #endif
  1356. #include <SDL3/SDL_close_code.h>
  1357. #endif /* SDL_audio_h_ */