SDL_audio.h 59 KB

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