SDL_audio.h 67 KB

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