SDL_audio.h 58 KB

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