SDL_audio.h 59 KB

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