SDL_audio.h 58 KB

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