SDL_audio.h 67 KB

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