SDL_audio.h 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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. /* !!! FIXME: several functions in here need Doxygen comments. */
  19. /**
  20. * \file SDL_audio.h
  21. *
  22. * Access to the raw audio mixing buffer for the SDL library.
  23. */
  24. #ifndef SDL_audio_h_
  25. #define SDL_audio_h_
  26. #include "SDL_stdinc.h"
  27. #include "SDL_error.h"
  28. #include "SDL_endian.h"
  29. #include "SDL_mutex.h"
  30. #include "SDL_thread.h"
  31. #include "SDL_rwops.h"
  32. #include "begin_code.h"
  33. /* Set up for C function definitions, even when using C++ */
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * \brief Audio format flags.
  39. *
  40. * These are what the 16 bits in SDL_AudioFormat currently mean...
  41. * (Unspecified bits are always zero).
  42. *
  43. * \verbatim
  44. ++-----------------------sample is signed if set
  45. ||
  46. || ++-----------sample is bigendian if set
  47. || ||
  48. || || ++---sample is float if set
  49. || || ||
  50. || || || +---sample bit size---+
  51. || || || | |
  52. 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
  53. \endverbatim
  54. *
  55. * There are macros in SDL 2.0 and later to query these bits.
  56. */
  57. typedef Uint16 SDL_AudioFormat;
  58. /**
  59. * \name Audio flags
  60. */
  61. /* @{ */
  62. #define SDL_AUDIO_MASK_BITSIZE (0xFF)
  63. #define SDL_AUDIO_MASK_DATATYPE (1<<8)
  64. #define SDL_AUDIO_MASK_ENDIAN (1<<12)
  65. #define SDL_AUDIO_MASK_SIGNED (1<<15)
  66. #define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
  67. #define SDL_AUDIO_ISFLOAT(x) (x & SDL_AUDIO_MASK_DATATYPE)
  68. #define SDL_AUDIO_ISBIGENDIAN(x) (x & SDL_AUDIO_MASK_ENDIAN)
  69. #define SDL_AUDIO_ISSIGNED(x) (x & SDL_AUDIO_MASK_SIGNED)
  70. #define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))
  71. #define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))
  72. #define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))
  73. /**
  74. * \name Audio format flags
  75. *
  76. * Defaults to LSB byte order.
  77. */
  78. /* @{ */
  79. #define AUDIO_U8 0x0008 /**< Unsigned 8-bit samples */
  80. #define AUDIO_S8 0x8008 /**< Signed 8-bit samples */
  81. #define AUDIO_U16LSB 0x0010 /**< Unsigned 16-bit samples */
  82. #define AUDIO_S16LSB 0x8010 /**< Signed 16-bit samples */
  83. #define AUDIO_U16MSB 0x1010 /**< As above, but big-endian byte order */
  84. #define AUDIO_S16MSB 0x9010 /**< As above, but big-endian byte order */
  85. #define AUDIO_U16 AUDIO_U16LSB
  86. #define AUDIO_S16 AUDIO_S16LSB
  87. /* @} */
  88. /**
  89. * \name int32 support
  90. */
  91. /* @{ */
  92. #define AUDIO_S32LSB 0x8020 /**< 32-bit integer samples */
  93. #define AUDIO_S32MSB 0x9020 /**< As above, but big-endian byte order */
  94. #define AUDIO_S32 AUDIO_S32LSB
  95. /* @} */
  96. /**
  97. * \name float32 support
  98. */
  99. /* @{ */
  100. #define AUDIO_F32LSB 0x8120 /**< 32-bit floating point samples */
  101. #define AUDIO_F32MSB 0x9120 /**< As above, but big-endian byte order */
  102. #define AUDIO_F32 AUDIO_F32LSB
  103. /* @} */
  104. /**
  105. * \name Native audio byte ordering
  106. */
  107. /* @{ */
  108. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  109. #define AUDIO_U16SYS AUDIO_U16LSB
  110. #define AUDIO_S16SYS AUDIO_S16LSB
  111. #define AUDIO_S32SYS AUDIO_S32LSB
  112. #define AUDIO_F32SYS AUDIO_F32LSB
  113. #else
  114. #define AUDIO_U16SYS AUDIO_U16MSB
  115. #define AUDIO_S16SYS AUDIO_S16MSB
  116. #define AUDIO_S32SYS AUDIO_S32MSB
  117. #define AUDIO_F32SYS AUDIO_F32MSB
  118. #endif
  119. /* @} */
  120. /**
  121. * \name Allow change flags
  122. *
  123. * Which audio format changes are allowed when opening a device.
  124. */
  125. /* @{ */
  126. #define SDL_AUDIO_ALLOW_FREQUENCY_CHANGE 0x00000001
  127. #define SDL_AUDIO_ALLOW_FORMAT_CHANGE 0x00000002
  128. #define SDL_AUDIO_ALLOW_CHANNELS_CHANGE 0x00000004
  129. #define SDL_AUDIO_ALLOW_SAMPLES_CHANGE 0x00000008
  130. #define SDL_AUDIO_ALLOW_ANY_CHANGE (SDL_AUDIO_ALLOW_FREQUENCY_CHANGE|SDL_AUDIO_ALLOW_FORMAT_CHANGE|SDL_AUDIO_ALLOW_CHANNELS_CHANGE|SDL_AUDIO_ALLOW_SAMPLES_CHANGE)
  131. /* @} */
  132. /* @} *//* Audio flags */
  133. /**
  134. * This function is called when the audio device needs more data.
  135. *
  136. * \param userdata An application-specific parameter saved in
  137. * the SDL_AudioSpec structure
  138. * \param stream A pointer to the audio data buffer.
  139. * \param len The length of that buffer in bytes.
  140. *
  141. * Once the callback returns, the buffer will no longer be valid.
  142. * Stereo samples are stored in a LRLRLR ordering.
  143. *
  144. * You can choose to avoid callbacks and use SDL_QueueAudio() instead, if
  145. * you like. Just open your audio device with a NULL callback.
  146. */
  147. typedef void (SDLCALL * SDL_AudioCallback) (void *userdata, Uint8 * stream,
  148. int len);
  149. /**
  150. * The calculated values in this structure are calculated by SDL_OpenAudio().
  151. *
  152. * For multi-channel audio, the default SDL channel mapping is:
  153. * 2: FL FR (stereo)
  154. * 3: FL FR LFE (2.1 surround)
  155. * 4: FL FR BL BR (quad)
  156. * 5: FL FR FC BL BR (quad + center)
  157. * 6: FL FR FC LFE SL SR (5.1 surround - last two can also be BL BR)
  158. * 7: FL FR FC LFE BC SL SR (6.1 surround)
  159. * 8: FL FR FC LFE BL BR SL SR (7.1 surround)
  160. */
  161. typedef struct SDL_AudioSpec
  162. {
  163. int freq; /**< DSP frequency -- samples per second */
  164. SDL_AudioFormat format; /**< Audio data format */
  165. Uint8 channels; /**< Number of channels: 1 mono, 2 stereo */
  166. Uint8 silence; /**< Audio buffer silence value (calculated) */
  167. Uint16 samples; /**< Audio buffer size in sample FRAMES (total samples divided by channel count) */
  168. Uint16 padding; /**< Necessary for some compile environments */
  169. Uint32 size; /**< Audio buffer size in bytes (calculated) */
  170. SDL_AudioCallback callback; /**< Callback that feeds the audio device (NULL to use SDL_QueueAudio()). */
  171. void *userdata; /**< Userdata passed to callback (ignored for NULL callbacks). */
  172. } SDL_AudioSpec;
  173. struct SDL_AudioCVT;
  174. typedef void (SDLCALL * SDL_AudioFilter) (struct SDL_AudioCVT * cvt,
  175. SDL_AudioFormat format);
  176. /**
  177. * \brief Upper limit of filters in SDL_AudioCVT
  178. *
  179. * The maximum number of SDL_AudioFilter functions in SDL_AudioCVT is
  180. * currently limited to 9. The SDL_AudioCVT.filters array has 10 pointers,
  181. * one of which is the terminating NULL pointer.
  182. */
  183. #define SDL_AUDIOCVT_MAX_FILTERS 9
  184. /**
  185. * \struct SDL_AudioCVT
  186. * \brief A structure to hold a set of audio conversion filters and buffers.
  187. *
  188. * Note that various parts of the conversion pipeline can take advantage
  189. * of SIMD operations (like SSE2, for example). SDL_AudioCVT doesn't require
  190. * you to pass it aligned data, but can possibly run much faster if you
  191. * set both its (buf) field to a pointer that is aligned to 16 bytes, and its
  192. * (len) field to something that's a multiple of 16, if possible.
  193. */
  194. #if defined(__GNUC__) && !defined(__CHERI_PURE_CAPABILITY__)
  195. /* This structure is 84 bytes on 32-bit architectures, make sure GCC doesn't
  196. pad it out to 88 bytes to guarantee ABI compatibility between compilers.
  197. This is not a concern on CHERI architectures, where pointers must be stored
  198. at aligned locations otherwise they will become invalid, and thus structs
  199. containing pointers cannot be packed without giving a warning or error.
  200. vvv
  201. The next time we rev the ABI, make sure to size the ints and add padding.
  202. */
  203. #define SDL_AUDIOCVT_PACKED __attribute__((packed))
  204. #else
  205. #define SDL_AUDIOCVT_PACKED
  206. #endif
  207. /* */
  208. typedef struct SDL_AudioCVT
  209. {
  210. int needed; /**< Set to 1 if conversion possible */
  211. SDL_AudioFormat src_format; /**< Source audio format */
  212. SDL_AudioFormat dst_format; /**< Target audio format */
  213. double rate_incr; /**< Rate conversion increment */
  214. Uint8 *buf; /**< Buffer to hold entire audio data */
  215. int len; /**< Length of original audio buffer */
  216. int len_cvt; /**< Length of converted audio buffer */
  217. int len_mult; /**< buffer must be len*len_mult big */
  218. double len_ratio; /**< Given len, final size is len*len_ratio */
  219. SDL_AudioFilter filters[SDL_AUDIOCVT_MAX_FILTERS + 1]; /**< NULL-terminated list of filter functions */
  220. int filter_index; /**< Current audio conversion function */
  221. } SDL_AUDIOCVT_PACKED SDL_AudioCVT;
  222. /* Function prototypes */
  223. /**
  224. * \name Driver discovery functions
  225. *
  226. * These functions return the list of built in audio drivers, in the
  227. * order that they are normally initialized by default.
  228. */
  229. /* @{ */
  230. /**
  231. * Use this function to get the number of built-in audio drivers.
  232. *
  233. * This function returns a hardcoded number. This never returns a negative
  234. * value; if there are no drivers compiled into this build of SDL, this
  235. * function returns zero. The presence of a driver in this list does not mean
  236. * it will function, it just means SDL is capable of interacting with that
  237. * interface. For example, a build of SDL might have esound support, but if
  238. * there's no esound server available, SDL's esound driver would fail if used.
  239. *
  240. * By default, SDL tries all drivers, in its preferred order, until one is
  241. * found to be usable.
  242. *
  243. * \returns the number of built-in audio drivers.
  244. *
  245. * \since This function is available since SDL 2.0.0.
  246. *
  247. * \sa SDL_GetAudioDriver
  248. */
  249. extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
  250. /**
  251. * Use this function to get the name of a built in audio driver.
  252. *
  253. * The list of audio drivers is given in the order that they are normally
  254. * initialized by default; the drivers that seem more reasonable to choose
  255. * first (as far as the SDL developers believe) are earlier in the list.
  256. *
  257. * The names of drivers are all simple, low-ASCII identifiers, like "alsa",
  258. * "coreaudio" or "xaudio2". These never have Unicode characters, and are not
  259. * meant to be proper names.
  260. *
  261. * \param index the index of the audio driver; the value ranges from 0 to
  262. * SDL_GetNumAudioDrivers() - 1
  263. * \returns the name of the audio driver at the requested index, or NULL if an
  264. * invalid index was specified.
  265. *
  266. * \since This function is available since SDL 2.0.0.
  267. *
  268. * \sa SDL_GetNumAudioDrivers
  269. */
  270. extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
  271. /* @} */
  272. /**
  273. * \name Initialization and cleanup
  274. *
  275. * \internal These functions are used internally, and should not be used unless
  276. * you have a specific need to specify the audio driver you want to
  277. * use. You should normally use SDL_Init() or SDL_InitSubSystem().
  278. */
  279. /* @{ */
  280. /**
  281. * Use this function to initialize a particular audio driver.
  282. *
  283. * This function is used internally, and should not be used unless you have a
  284. * specific need to designate the audio driver you want to use. You should
  285. * normally use SDL_Init() or SDL_InitSubSystem().
  286. *
  287. * \param driver_name the name of the desired audio driver
  288. * \returns 0 on success or a negative error code on failure; call
  289. * SDL_GetError() for more information.
  290. *
  291. * \since This function is available since SDL 2.0.0.
  292. *
  293. * \sa SDL_AudioQuit
  294. */
  295. extern DECLSPEC int SDLCALL SDL_AudioInit(const char *driver_name);
  296. /**
  297. * Use this function to shut down audio if you initialized it with
  298. * SDL_AudioInit().
  299. *
  300. * This function is used internally, and should not be used unless you have a
  301. * specific need to specify the audio driver you want to use. You should
  302. * normally use SDL_Quit() or SDL_QuitSubSystem().
  303. *
  304. * \since This function is available since SDL 2.0.0.
  305. *
  306. * \sa SDL_AudioInit
  307. */
  308. extern DECLSPEC void SDLCALL SDL_AudioQuit(void);
  309. /* @} */
  310. /**
  311. * Get the name of the current audio driver.
  312. *
  313. * The returned string points to internal static memory and thus never becomes
  314. * invalid, even if you quit the audio subsystem and initialize a new driver
  315. * (although such a case would return a different static string from another
  316. * call to this function, of course). As such, you should not modify or free
  317. * the returned string.
  318. *
  319. * \returns the name of the current audio driver or NULL if no driver has been
  320. * initialized.
  321. *
  322. * \since This function is available since SDL 2.0.0.
  323. *
  324. * \sa SDL_AudioInit
  325. */
  326. extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
  327. /**
  328. * This function is a legacy means of opening the audio device.
  329. *
  330. * This function remains for compatibility with SDL 1.2, but also because it's
  331. * slightly easier to use than the new functions in SDL 2.0. The new, more
  332. * powerful, and preferred way to do this is SDL_OpenAudioDevice().
  333. *
  334. * This function is roughly equivalent to:
  335. *
  336. * ```c
  337. * SDL_OpenAudioDevice(NULL, 0, desired, obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
  338. * ```
  339. *
  340. * With two notable exceptions:
  341. *
  342. * - If `obtained` is NULL, we use `desired` (and allow no changes), which
  343. * means desired will be modified to have the correct values for silence,
  344. * etc, and SDL will convert any differences between your app's specific
  345. * request and the hardware behind the scenes.
  346. * - The return value is always success or failure, and not a device ID, which
  347. * means you can only have one device open at a time with this function.
  348. *
  349. * \param desired an SDL_AudioSpec structure representing the desired output
  350. * format. Please refer to the SDL_OpenAudioDevice
  351. * documentation for details on how to prepare this structure.
  352. * \param obtained an SDL_AudioSpec structure filled in with the actual
  353. * parameters, or NULL.
  354. * \returns 0 if successful, placing the actual hardware parameters in the
  355. * structure pointed to by `obtained`.
  356. *
  357. * If `obtained` is NULL, the audio data passed to the callback
  358. * function will be guaranteed to be in the requested format, and
  359. * will be automatically converted to the actual hardware audio
  360. * format if necessary. If `obtained` is NULL, `desired` will have
  361. * fields modified.
  362. *
  363. * This function returns a negative error code on failure to open the
  364. * audio device or failure to set up the audio thread; call
  365. * SDL_GetError() for more information.
  366. *
  367. * \since This function is available since SDL 2.0.0.
  368. *
  369. * \sa SDL_CloseAudio
  370. * \sa SDL_LockAudio
  371. * \sa SDL_PauseAudio
  372. * \sa SDL_UnlockAudio
  373. */
  374. extern DECLSPEC int SDLCALL SDL_OpenAudio(SDL_AudioSpec * desired,
  375. SDL_AudioSpec * obtained);
  376. /**
  377. * SDL Audio Device IDs.
  378. *
  379. * A successful call to SDL_OpenAudio() is always device id 1, and legacy
  380. * SDL audio APIs assume you want this device ID. SDL_OpenAudioDevice() calls
  381. * always returns devices >= 2 on success. The legacy calls are good both
  382. * for backwards compatibility and when you don't care about multiple,
  383. * specific, or capture devices.
  384. */
  385. typedef Uint32 SDL_AudioDeviceID;
  386. /**
  387. * Get the number of built-in audio devices.
  388. *
  389. * This function is only valid after successfully initializing the audio
  390. * subsystem.
  391. *
  392. * Note that audio capture support is not implemented as of SDL 2.0.4, so the
  393. * `iscapture` parameter is for future expansion and should always be zero for
  394. * now.
  395. *
  396. * This function will return -1 if an explicit list of devices can't be
  397. * determined. Returning -1 is not an error. For example, if SDL is set up to
  398. * talk to a remote audio server, it can't list every one available on the
  399. * Internet, but it will still allow a specific host to be specified in
  400. * SDL_OpenAudioDevice().
  401. *
  402. * In many common cases, when this function returns a value <= 0, it can still
  403. * successfully open the default device (NULL for first argument of
  404. * SDL_OpenAudioDevice()).
  405. *
  406. * This function may trigger a complete redetect of available hardware. It
  407. * should not be called for each iteration of a loop, but rather once at the
  408. * start of a loop:
  409. *
  410. * ```c
  411. * // Don't do this:
  412. * for (int i = 0; i < SDL_GetNumAudioDevices(0); i++)
  413. *
  414. * // do this instead:
  415. * const int count = SDL_GetNumAudioDevices(0);
  416. * for (int i = 0; i < count; ++i) { do_something_here(); }
  417. * ```
  418. *
  419. * \param iscapture zero to request playback devices, non-zero to request
  420. * recording devices
  421. * \returns the number of available devices exposed by the current driver or
  422. * -1 if an explicit list of devices can't be determined. A return
  423. * value of -1 does not necessarily mean an error condition.
  424. *
  425. * \since This function is available since SDL 2.0.0.
  426. *
  427. * \sa SDL_GetAudioDeviceName
  428. * \sa SDL_OpenAudioDevice
  429. */
  430. extern DECLSPEC int SDLCALL SDL_GetNumAudioDevices(int iscapture);
  431. /**
  432. * Get the human-readable name of a specific audio device.
  433. *
  434. * This function is only valid after successfully initializing the audio
  435. * subsystem. The values returned by this function reflect the latest call to
  436. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  437. * hardware.
  438. *
  439. * The string returned by this function is UTF-8 encoded, read-only, and
  440. * managed internally. You are not to free it. If you need to keep the string
  441. * for any length of time, you should make your own copy of it, as it will be
  442. * invalid next time any of several other SDL functions are called.
  443. *
  444. * \param index the index of the audio device; valid values range from 0 to
  445. * SDL_GetNumAudioDevices() - 1
  446. * \param iscapture non-zero to query the list of recording devices, zero to
  447. * query the list of output devices.
  448. * \returns the name of the audio device at the requested index, or NULL on
  449. * error.
  450. *
  451. * \since This function is available since SDL 2.0.0.
  452. *
  453. * \sa SDL_GetNumAudioDevices
  454. */
  455. extern DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(int index,
  456. int iscapture);
  457. /**
  458. * Get the preferred audio format of a specific audio device.
  459. *
  460. * This function is only valid after a successfully initializing the audio
  461. * subsystem. The values returned by this function reflect the latest call to
  462. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  463. * hardware.
  464. *
  465. * `spec` will be filled with the sample rate, sample format, and channel
  466. * count. All other values in the structure are filled with 0. When the
  467. * supported struct members are 0, SDL was unable to get the property from the
  468. * backend.
  469. *
  470. * \param index the index of the audio device; valid values range from 0 to
  471. * SDL_GetNumAudioDevices() - 1
  472. * \param iscapture non-zero to query the list of recording devices, zero to
  473. * query the list of output devices.
  474. * \param spec The SDL_AudioSpec to be initialized by this function.
  475. * \returns 0 on success, nonzero on error
  476. *
  477. * \since This function is available since SDL 2.0.16.
  478. *
  479. * \sa SDL_GetNumAudioDevices
  480. */
  481. extern DECLSPEC int SDLCALL SDL_GetAudioDeviceSpec(int index,
  482. int iscapture,
  483. SDL_AudioSpec *spec);
  484. /**
  485. * Open a specific audio device.
  486. *
  487. * SDL_OpenAudio(), unlike this function, always acts on device ID 1. As such,
  488. * this function will never return a 1 so as not to conflict with the legacy
  489. * function.
  490. *
  491. * Please note that SDL 2.0 before 2.0.5 did not support recording; as such,
  492. * this function would fail if `iscapture` was not zero. Starting with SDL
  493. * 2.0.5, recording is implemented and this value can be non-zero.
  494. *
  495. * Passing in a `device` name of NULL requests the most reasonable default
  496. * (and is equivalent to what SDL_OpenAudio() does to choose a device). The
  497. * `device` name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but
  498. * some drivers allow arbitrary and driver-specific strings, such as a
  499. * hostname/IP address for a remote audio server, or a filename in the
  500. * diskaudio driver.
  501. *
  502. * An opened audio device starts out paused, and should be enabled for playing
  503. * by calling SDL_PauseAudioDevice(devid, 0) when you are ready for your audio
  504. * callback function to be called. Since the audio driver may modify the
  505. * requested size of the audio buffer, you should allocate any local mixing
  506. * buffers after you open the audio device.
  507. *
  508. * The audio callback runs in a separate thread in most cases; you can prevent
  509. * race conditions between your callback and other threads without fully
  510. * pausing playback with SDL_LockAudioDevice(). For more information about the
  511. * callback, see SDL_AudioSpec.
  512. *
  513. * Managing the audio spec via 'desired' and 'obtained':
  514. *
  515. * When filling in the desired audio spec structure:
  516. *
  517. * - `desired->freq` should be the frequency in sample-frames-per-second (Hz).
  518. * - `desired->format` should be the audio format (`AUDIO_S16SYS`, etc).
  519. * - `desired->samples` is the desired size of the audio buffer, in _sample
  520. * frames_ (with stereo output, two samples--left and right--would make a
  521. * single sample frame). This number should be a power of two, and may be
  522. * adjusted by the audio driver to a value more suitable for the hardware.
  523. * Good values seem to range between 512 and 8096 inclusive, depending on
  524. * the application and CPU speed. Smaller values reduce latency, but can
  525. * lead to underflow if the application is doing heavy processing and cannot
  526. * fill the audio buffer in time. Note that the number of sample frames is
  527. * directly related to time by the following formula: `ms =
  528. * (sampleframes*1000)/freq`
  529. * - `desired->size` is the size in _bytes_ of the audio buffer, and is
  530. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  531. * - `desired->silence` is the value used to set the buffer to silence, and is
  532. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  533. * - `desired->callback` should be set to a function that will be called when
  534. * the audio device is ready for more data. It is passed a pointer to the
  535. * audio buffer, and the length in bytes of the audio buffer. This function
  536. * usually runs in a separate thread, and so you should protect data
  537. * structures that it accesses by calling SDL_LockAudioDevice() and
  538. * SDL_UnlockAudioDevice() in your code. Alternately, you may pass a NULL
  539. * pointer here, and call SDL_QueueAudio() with some frequency, to queue
  540. * more audio samples to be played (or for capture devices, call
  541. * SDL_DequeueAudio() with some frequency, to obtain audio samples).
  542. * - `desired->userdata` is passed as the first parameter to your callback
  543. * function. If you passed a NULL callback, this value is ignored.
  544. *
  545. * `allowed_changes` can have the following flags OR'd together:
  546. *
  547. * - `SDL_AUDIO_ALLOW_FREQUENCY_CHANGE`
  548. * - `SDL_AUDIO_ALLOW_FORMAT_CHANGE`
  549. * - `SDL_AUDIO_ALLOW_CHANNELS_CHANGE`
  550. * - `SDL_AUDIO_ALLOW_ANY_CHANGE`
  551. *
  552. * These flags specify how SDL should behave when a device cannot offer a
  553. * specific feature. If the application requests a feature that the hardware
  554. * doesn't offer, SDL will always try to get the closest equivalent.
  555. *
  556. * For example, if you ask for float32 audio format, but the sound card only
  557. * supports int16, SDL will set the hardware to int16. If you had set
  558. * SDL_AUDIO_ALLOW_FORMAT_CHANGE, SDL will change the format in the `obtained`
  559. * structure. If that flag was *not* set, SDL will prepare to convert your
  560. * callback's float32 audio to int16 before feeding it to the hardware and
  561. * will keep the originally requested format in the `obtained` structure.
  562. *
  563. * The resulting audio specs, varying depending on hardware and on what
  564. * changes were allowed, will then be written back to `obtained`.
  565. *
  566. * If your application can only handle one specific data format, pass a zero
  567. * for `allowed_changes` and let SDL transparently handle any differences.
  568. *
  569. * \param device a UTF-8 string reported by SDL_GetAudioDeviceName() or a
  570. * driver-specific name as appropriate. NULL requests the most
  571. * reasonable default device.
  572. * \param iscapture non-zero to specify a device should be opened for
  573. * recording, not playback
  574. * \param desired an SDL_AudioSpec structure representing the desired output
  575. * format; see SDL_OpenAudio() for more information
  576. * \param obtained an SDL_AudioSpec structure filled in with the actual output
  577. * format; see SDL_OpenAudio() for more information
  578. * \param allowed_changes 0, or one or more flags OR'd together
  579. * \returns a valid device ID that is > 0 on success or 0 on failure; call
  580. * SDL_GetError() for more information.
  581. *
  582. * For compatibility with SDL 1.2, this will never return 1, since
  583. * SDL reserves that ID for the legacy SDL_OpenAudio() function.
  584. *
  585. * \since This function is available since SDL 2.0.0.
  586. *
  587. * \sa SDL_CloseAudioDevice
  588. * \sa SDL_GetAudioDeviceName
  589. * \sa SDL_LockAudioDevice
  590. * \sa SDL_OpenAudio
  591. * \sa SDL_PauseAudioDevice
  592. * \sa SDL_UnlockAudioDevice
  593. */
  594. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(
  595. const char *device,
  596. int iscapture,
  597. const SDL_AudioSpec *desired,
  598. SDL_AudioSpec *obtained,
  599. int allowed_changes);
  600. /**
  601. * \name Audio state
  602. *
  603. * Get the current audio state.
  604. */
  605. /* @{ */
  606. typedef enum
  607. {
  608. SDL_AUDIO_STOPPED = 0,
  609. SDL_AUDIO_PLAYING,
  610. SDL_AUDIO_PAUSED
  611. } SDL_AudioStatus;
  612. /**
  613. * This function is a legacy means of querying the audio device.
  614. *
  615. * New programs might want to use SDL_GetAudioDeviceStatus() instead. This
  616. * function is equivalent to calling...
  617. *
  618. * ```c
  619. * SDL_GetAudioDeviceStatus(1);
  620. * ```
  621. *
  622. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  623. *
  624. * \returns the SDL_AudioStatus of the audio device opened by SDL_OpenAudio().
  625. *
  626. * \since This function is available since SDL 2.0.0.
  627. *
  628. * \sa SDL_GetAudioDeviceStatus
  629. */
  630. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioStatus(void);
  631. /**
  632. * Use this function to get the current audio state of an audio device.
  633. *
  634. * \param dev the ID of an audio device previously opened with
  635. * SDL_OpenAudioDevice()
  636. * \returns the SDL_AudioStatus of the specified audio device.
  637. *
  638. * \since This function is available since SDL 2.0.0.
  639. *
  640. * \sa SDL_PauseAudioDevice
  641. */
  642. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev);
  643. /* @} *//* Audio State */
  644. /**
  645. * \name Pause audio functions
  646. *
  647. * These functions pause and unpause the audio callback processing.
  648. * They should be called with a parameter of 0 after opening the audio
  649. * device to start playing sound. This is so you can safely initialize
  650. * data for your callback function after opening the audio device.
  651. * Silence will be written to the audio device during the pause.
  652. */
  653. /* @{ */
  654. /**
  655. * This function is a legacy means of pausing the audio device.
  656. *
  657. * New programs might want to use SDL_PauseAudioDevice() instead. This
  658. * function is equivalent to calling...
  659. *
  660. * ```c
  661. * SDL_PauseAudioDevice(1, pause_on);
  662. * ```
  663. *
  664. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  665. *
  666. * \param pause_on non-zero to pause, 0 to unpause
  667. *
  668. * \since This function is available since SDL 2.0.0.
  669. *
  670. * \sa SDL_GetAudioStatus
  671. * \sa SDL_PauseAudioDevice
  672. */
  673. extern DECLSPEC void SDLCALL SDL_PauseAudio(int pause_on);
  674. /**
  675. * Use this function to pause and unpause audio playback on a specified
  676. * device.
  677. *
  678. * This function pauses and unpauses the audio callback processing for a given
  679. * device. Newly-opened audio devices start in the paused state, so you must
  680. * call this function with **pause_on**=0 after opening the specified audio
  681. * device to start playing sound. This allows you to safely initialize data
  682. * for your callback function after opening the audio device. Silence will be
  683. * written to the audio device while paused, and the audio callback is
  684. * guaranteed to not be called. Pausing one device does not prevent other
  685. * unpaused devices from running their callbacks.
  686. *
  687. * Pausing state does not stack; even if you pause a device several times, a
  688. * single unpause will start the device playing again, and vice versa. This is
  689. * different from how SDL_LockAudioDevice() works.
  690. *
  691. * If you just need to protect a few variables from race conditions vs your
  692. * callback, you shouldn't pause the audio device, as it will lead to dropouts
  693. * in the audio playback. Instead, you should use SDL_LockAudioDevice().
  694. *
  695. * \param dev a device opened by SDL_OpenAudioDevice()
  696. * \param pause_on non-zero to pause, 0 to unpause
  697. *
  698. * \since This function is available since SDL 2.0.0.
  699. *
  700. * \sa SDL_LockAudioDevice
  701. */
  702. extern DECLSPEC void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev,
  703. int pause_on);
  704. /* @} *//* Pause audio functions */
  705. /**
  706. * Load the audio data of a WAVE file into memory.
  707. *
  708. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  709. * be valid pointers. The entire data portion of the file is then loaded into
  710. * memory and decoded if necessary.
  711. *
  712. * If `freesrc` is non-zero, the data source gets automatically closed and
  713. * freed before the function returns.
  714. *
  715. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  716. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  717. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  718. * cause an error.
  719. *
  720. * If this function succeeds, the pointer returned by it is equal to `spec`
  721. * and the pointer to the audio data allocated by the function is written to
  722. * `audio_buf` and its length in bytes to `audio_len`. The SDL_AudioSpec
  723. * members `freq`, `channels`, and `format` are set to the values of the audio
  724. * data in the buffer. The `samples` member is set to a sane default and all
  725. * others are set to zero.
  726. *
  727. * It's necessary to use SDL_FreeWAV() to free the audio data returned in
  728. * `audio_buf` when it is no longer used.
  729. *
  730. * Because of the underspecification of the .WAV format, there are many
  731. * problematic files in the wild that cause issues with strict decoders. To
  732. * provide compatibility with these files, this decoder is lenient in regards
  733. * to the truncation of the file, the fact chunk, and the size of the RIFF
  734. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  735. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  736. * tune the behavior of the loading process.
  737. *
  738. * Any file that is invalid (due to truncation, corruption, or wrong values in
  739. * the headers), too big, or unsupported causes an error. Additionally, any
  740. * critical I/O error from the data source will terminate the loading process
  741. * with an error. The function returns NULL on error and in all cases (with
  742. * the exception of `src` being NULL), an appropriate error message will be
  743. * set.
  744. *
  745. * It is required that the data source supports seeking.
  746. *
  747. * Example:
  748. *
  749. * ```c
  750. * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
  751. * ```
  752. *
  753. * Note that the SDL_LoadWAV macro does this same thing for you, but in a less
  754. * messy way:
  755. *
  756. * ```c
  757. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  758. * ```
  759. *
  760. * \param src The data source for the WAVE data
  761. * \param freesrc If non-zero, SDL will _always_ free the data source
  762. * \param spec An SDL_AudioSpec that will be filled in with the wave file's
  763. * format details
  764. * \param audio_buf A pointer filled with the audio data, allocated by the
  765. * function.
  766. * \param audio_len A pointer filled with the length of the audio data buffer
  767. * in bytes
  768. * \returns This function, if successfully called, returns `spec`, which will
  769. * be filled with the audio data format of the wave source data.
  770. * `audio_buf` will be filled with a pointer to an allocated buffer
  771. * containing the audio data, and `audio_len` is filled with the
  772. * length of that audio buffer in bytes.
  773. *
  774. * This function returns NULL if the .WAV file cannot be opened, uses
  775. * an unknown data format, or is corrupt; call SDL_GetError() for
  776. * more information.
  777. *
  778. * When the application is done with the data returned in
  779. * `audio_buf`, it should call SDL_FreeWAV() to dispose of it.
  780. *
  781. * \since This function is available since SDL 2.0.0.
  782. *
  783. * \sa SDL_FreeWAV
  784. * \sa SDL_LoadWAV
  785. */
  786. extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
  787. int freesrc,
  788. SDL_AudioSpec * spec,
  789. Uint8 ** audio_buf,
  790. Uint32 * audio_len);
  791. /**
  792. * Loads a WAV from a file.
  793. * Compatibility convenience function.
  794. */
  795. #define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
  796. SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
  797. /**
  798. * Free data previously allocated with SDL_LoadWAV() or SDL_LoadWAV_RW().
  799. *
  800. * After a WAVE file has been opened with SDL_LoadWAV() or SDL_LoadWAV_RW()
  801. * its data can eventually be freed with SDL_FreeWAV(). It is safe to call
  802. * this function with a NULL pointer.
  803. *
  804. * \param audio_buf a pointer to the buffer created by SDL_LoadWAV() or
  805. * SDL_LoadWAV_RW()
  806. *
  807. * \since This function is available since SDL 2.0.0.
  808. *
  809. * \sa SDL_LoadWAV
  810. * \sa SDL_LoadWAV_RW
  811. */
  812. extern DECLSPEC void SDLCALL SDL_FreeWAV(Uint8 * audio_buf);
  813. /**
  814. * Initialize an SDL_AudioCVT structure for conversion.
  815. *
  816. * Before an SDL_AudioCVT structure can be used to convert audio data it must
  817. * be initialized with source and destination information.
  818. *
  819. * This function will zero out every field of the SDL_AudioCVT, so it must be
  820. * called before the application fills in the final buffer information.
  821. *
  822. * Once this function has returned successfully, and reported that a
  823. * conversion is necessary, the application fills in the rest of the fields in
  824. * SDL_AudioCVT, now that it knows how large a buffer it needs to allocate,
  825. * and then can call SDL_ConvertAudio() to complete the conversion.
  826. *
  827. * \param cvt an SDL_AudioCVT structure filled in with audio conversion
  828. * information
  829. * \param src_format the source format of the audio data; for more info see
  830. * SDL_AudioFormat
  831. * \param src_channels the number of channels in the source
  832. * \param src_rate the frequency (sample-frames-per-second) of the source
  833. * \param dst_format the destination format of the audio data; for more info
  834. * see SDL_AudioFormat
  835. * \param dst_channels the number of channels in the destination
  836. * \param dst_rate the frequency (sample-frames-per-second) of the destination
  837. * \returns 1 if the audio filter is prepared, 0 if no conversion is needed,
  838. * or a negative error code on failure; call SDL_GetError() for more
  839. * information.
  840. *
  841. * \since This function is available since SDL 2.0.0.
  842. *
  843. * \sa SDL_ConvertAudio
  844. */
  845. extern DECLSPEC int SDLCALL SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
  846. SDL_AudioFormat src_format,
  847. Uint8 src_channels,
  848. int src_rate,
  849. SDL_AudioFormat dst_format,
  850. Uint8 dst_channels,
  851. int dst_rate);
  852. /**
  853. * Convert audio data to a desired audio format.
  854. *
  855. * This function does the actual audio data conversion, after the application
  856. * has called SDL_BuildAudioCVT() to prepare the conversion information and
  857. * then filled in the buffer details.
  858. *
  859. * Once the application has initialized the `cvt` structure using
  860. * SDL_BuildAudioCVT(), allocated an audio buffer and filled it with audio
  861. * data in the source format, this function will convert the buffer, in-place,
  862. * to the desired format.
  863. *
  864. * The data conversion may go through several passes; any given pass may
  865. * possibly temporarily increase the size of the data. For example, SDL might
  866. * expand 16-bit data to 32 bits before resampling to a lower frequency,
  867. * shrinking the data size after having grown it briefly. Since the supplied
  868. * buffer will be both the source and destination, converting as necessary
  869. * in-place, the application must allocate a buffer that will fully contain
  870. * the data during its largest conversion pass. After SDL_BuildAudioCVT()
  871. * returns, the application should set the `cvt->len` field to the size, in
  872. * bytes, of the source data, and allocate a buffer that is `cvt->len *
  873. * cvt->len_mult` bytes long for the `buf` field.
  874. *
  875. * The source data should be copied into this buffer before the call to
  876. * SDL_ConvertAudio(). Upon successful return, this buffer will contain the
  877. * converted audio, and `cvt->len_cvt` will be the size of the converted data,
  878. * in bytes. Any bytes in the buffer past `cvt->len_cvt` are undefined once
  879. * this function returns.
  880. *
  881. * \param cvt an SDL_AudioCVT structure that was previously set up by
  882. * SDL_BuildAudioCVT().
  883. * \returns 0 if the conversion was completed successfully or a negative error
  884. * code on failure; call SDL_GetError() for more information.
  885. *
  886. * \since This function is available since SDL 2.0.0.
  887. *
  888. * \sa SDL_BuildAudioCVT
  889. */
  890. extern DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT * cvt);
  891. /* SDL_AudioStream is a new audio conversion interface.
  892. The benefits vs SDL_AudioCVT:
  893. - it can handle resampling data in chunks without generating
  894. artifacts, when it doesn't have the complete buffer available.
  895. - it can handle incoming data in any variable size.
  896. - You push data as you have it, and pull it when you need it
  897. */
  898. /* this is opaque to the outside world. */
  899. struct _SDL_AudioStream;
  900. typedef struct _SDL_AudioStream SDL_AudioStream;
  901. /**
  902. * Create a new audio stream.
  903. *
  904. * \param src_format The format of the source audio
  905. * \param src_channels The number of channels of the source audio
  906. * \param src_rate The sampling rate of the source audio
  907. * \param dst_format The format of the desired audio output
  908. * \param dst_channels The number of channels of the desired audio output
  909. * \param dst_rate The sampling rate of the desired audio output
  910. * \returns 0 on success, or -1 on error.
  911. *
  912. * \since This function is available since SDL 2.0.7.
  913. *
  914. * \sa SDL_AudioStreamPut
  915. * \sa SDL_AudioStreamGet
  916. * \sa SDL_AudioStreamAvailable
  917. * \sa SDL_AudioStreamFlush
  918. * \sa SDL_AudioStreamClear
  919. * \sa SDL_FreeAudioStream
  920. */
  921. extern DECLSPEC SDL_AudioStream * SDLCALL SDL_NewAudioStream(const SDL_AudioFormat src_format,
  922. const Uint8 src_channels,
  923. const int src_rate,
  924. const SDL_AudioFormat dst_format,
  925. const Uint8 dst_channels,
  926. const int dst_rate);
  927. /**
  928. * Add data to be converted/resampled to the stream.
  929. *
  930. * \param stream The stream the audio data is being added to
  931. * \param buf A pointer to the audio data to add
  932. * \param len The number of bytes to write to the stream
  933. * \returns 0 on success, or -1 on error.
  934. *
  935. * \since This function is available since SDL 2.0.7.
  936. *
  937. * \sa SDL_NewAudioStream
  938. * \sa SDL_AudioStreamGet
  939. * \sa SDL_AudioStreamAvailable
  940. * \sa SDL_AudioStreamFlush
  941. * \sa SDL_AudioStreamClear
  942. * \sa SDL_FreeAudioStream
  943. */
  944. extern DECLSPEC int SDLCALL SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len);
  945. /**
  946. * Get converted/resampled data from the stream
  947. *
  948. * \param stream The stream the audio is being requested from
  949. * \param buf A buffer to fill with audio data
  950. * \param len The maximum number of bytes to fill
  951. * \returns the number of bytes read from the stream, or -1 on error
  952. *
  953. * \since This function is available since SDL 2.0.7.
  954. *
  955. * \sa SDL_NewAudioStream
  956. * \sa SDL_AudioStreamPut
  957. * \sa SDL_AudioStreamAvailable
  958. * \sa SDL_AudioStreamFlush
  959. * \sa SDL_AudioStreamClear
  960. * \sa SDL_FreeAudioStream
  961. */
  962. extern DECLSPEC int SDLCALL SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len);
  963. /**
  964. * Get the number of converted/resampled bytes available.
  965. *
  966. * The stream may be buffering data behind the scenes until it has enough to
  967. * resample correctly, so this number might be lower than what you expect, or
  968. * even be zero. Add more data or flush the stream if you need the data now.
  969. *
  970. * \since This function is available since SDL 2.0.7.
  971. *
  972. * \sa SDL_NewAudioStream
  973. * \sa SDL_AudioStreamPut
  974. * \sa SDL_AudioStreamGet
  975. * \sa SDL_AudioStreamFlush
  976. * \sa SDL_AudioStreamClear
  977. * \sa SDL_FreeAudioStream
  978. */
  979. extern DECLSPEC int SDLCALL SDL_AudioStreamAvailable(SDL_AudioStream *stream);
  980. /**
  981. * Tell the stream that you're done sending data, and anything being buffered
  982. * should be converted/resampled and made available immediately.
  983. *
  984. * It is legal to add more data to a stream after flushing, but there will be
  985. * audio gaps in the output. Generally this is intended to signal the end of
  986. * input, so the complete output becomes available.
  987. *
  988. * \since This function is available since SDL 2.0.7.
  989. *
  990. * \sa SDL_NewAudioStream
  991. * \sa SDL_AudioStreamPut
  992. * \sa SDL_AudioStreamGet
  993. * \sa SDL_AudioStreamAvailable
  994. * \sa SDL_AudioStreamClear
  995. * \sa SDL_FreeAudioStream
  996. */
  997. extern DECLSPEC int SDLCALL SDL_AudioStreamFlush(SDL_AudioStream *stream);
  998. /**
  999. * Clear any pending data in the stream without converting it
  1000. *
  1001. * \since This function is available since SDL 2.0.7.
  1002. *
  1003. * \sa SDL_NewAudioStream
  1004. * \sa SDL_AudioStreamPut
  1005. * \sa SDL_AudioStreamGet
  1006. * \sa SDL_AudioStreamAvailable
  1007. * \sa SDL_AudioStreamFlush
  1008. * \sa SDL_FreeAudioStream
  1009. */
  1010. extern DECLSPEC void SDLCALL SDL_AudioStreamClear(SDL_AudioStream *stream);
  1011. /**
  1012. * Free an audio stream
  1013. *
  1014. * \since This function is available since SDL 2.0.7.
  1015. *
  1016. * \sa SDL_NewAudioStream
  1017. * \sa SDL_AudioStreamPut
  1018. * \sa SDL_AudioStreamGet
  1019. * \sa SDL_AudioStreamAvailable
  1020. * \sa SDL_AudioStreamFlush
  1021. * \sa SDL_AudioStreamClear
  1022. */
  1023. extern DECLSPEC void SDLCALL SDL_FreeAudioStream(SDL_AudioStream *stream);
  1024. #define SDL_MIX_MAXVOLUME 128
  1025. /**
  1026. * This function is a legacy means of mixing audio.
  1027. *
  1028. * This function is equivalent to calling...
  1029. *
  1030. * ```c
  1031. * SDL_MixAudioFormat(dst, src, format, len, volume);
  1032. * ```
  1033. *
  1034. * ...where `format` is the obtained format of the audio device from the
  1035. * legacy SDL_OpenAudio() function.
  1036. *
  1037. * \param dst the destination for the mixed audio
  1038. * \param src the source audio buffer to be mixed
  1039. * \param len the length of the audio buffer in bytes
  1040. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  1041. * for full audio volume
  1042. *
  1043. * \since This function is available since SDL 2.0.0.
  1044. *
  1045. * \sa SDL_MixAudioFormat
  1046. */
  1047. extern DECLSPEC void SDLCALL SDL_MixAudio(Uint8 * dst, const Uint8 * src,
  1048. Uint32 len, int volume);
  1049. /**
  1050. * Mix audio data in a specified format.
  1051. *
  1052. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  1053. * it into `dst`, performing addition, volume adjustment, and overflow
  1054. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  1055. * `format` data.
  1056. *
  1057. * This is provided for convenience -- you can mix your own audio data.
  1058. *
  1059. * Do not use this function for mixing together more than two streams of
  1060. * sample data. The output from repeated application of this function may be
  1061. * distorted by clipping, because there is no accumulator with greater range
  1062. * than the input (not to mention this being an inefficient way of doing it).
  1063. *
  1064. * It is a common misconception that this function is required to write audio
  1065. * data to an output stream in an audio callback. While you can do that,
  1066. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  1067. * audio stream with a volume adjustment.
  1068. *
  1069. * \param dst the destination for the mixed audio
  1070. * \param src the source audio buffer to be mixed
  1071. * \param format the SDL_AudioFormat structure representing the desired audio
  1072. * format
  1073. * \param len the length of the audio buffer in bytes
  1074. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  1075. * for full audio volume
  1076. *
  1077. * \since This function is available since SDL 2.0.0.
  1078. */
  1079. extern DECLSPEC void SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  1080. const Uint8 * src,
  1081. SDL_AudioFormat format,
  1082. Uint32 len, int volume);
  1083. /**
  1084. * Queue more audio on non-callback devices.
  1085. *
  1086. * If you are looking to retrieve queued audio from a non-callback capture
  1087. * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
  1088. * -1 to signify an error if you use it with capture devices.
  1089. *
  1090. * SDL offers two ways to feed audio to the device: you can either supply a
  1091. * callback that SDL triggers with some frequency to obtain more audio (pull
  1092. * method), or you can supply no callback, and then SDL will expect you to
  1093. * supply data at regular intervals (push method) with this function.
  1094. *
  1095. * There are no limits on the amount of data you can queue, short of
  1096. * exhaustion of address space. Queued data will drain to the device as
  1097. * necessary without further intervention from you. If the device needs audio
  1098. * but there is not enough queued, it will play silence to make up the
  1099. * difference. This means you will have skips in your audio playback if you
  1100. * aren't routinely queueing sufficient data.
  1101. *
  1102. * This function copies the supplied data, so you are safe to free it when the
  1103. * function returns. This function is thread-safe, but queueing to the same
  1104. * device from two threads at once does not promise which buffer will be
  1105. * queued first.
  1106. *
  1107. * You may not queue audio on a device that is using an application-supplied
  1108. * callback; doing so returns an error. You have to use the audio callback or
  1109. * queue audio with this function, but not both.
  1110. *
  1111. * You should not call SDL_LockAudio() on the device before queueing; SDL
  1112. * handles locking internally for this function.
  1113. *
  1114. * Note that SDL2 does not support planar audio. You will need to resample
  1115. * from planar audio formats into a non-planar one (see SDL_AudioFormat)
  1116. * before queuing audio.
  1117. *
  1118. * \param dev the device ID to which we will queue audio
  1119. * \param data the data to queue to the device for later playback
  1120. * \param len the number of bytes (not samples!) to which `data` points
  1121. * \returns 0 on success or a negative error code on failure; call
  1122. * SDL_GetError() for more information.
  1123. *
  1124. * \since This function is available since SDL 2.0.4.
  1125. *
  1126. * \sa SDL_ClearQueuedAudio
  1127. * \sa SDL_GetQueuedAudioSize
  1128. */
  1129. extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
  1130. /**
  1131. * Dequeue more audio on non-callback devices.
  1132. *
  1133. * If you are looking to queue audio for output on a non-callback playback
  1134. * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
  1135. * return 0 if you use it with playback devices.
  1136. *
  1137. * SDL offers two ways to retrieve audio from a capture device: you can either
  1138. * supply a callback that SDL triggers with some frequency as the device
  1139. * records more audio data, (push method), or you can supply no callback, and
  1140. * then SDL will expect you to retrieve data at regular intervals (pull
  1141. * method) with this function.
  1142. *
  1143. * There are no limits on the amount of data you can queue, short of
  1144. * exhaustion of address space. Data from the device will keep queuing as
  1145. * necessary without further intervention from you. This means you will
  1146. * eventually run out of memory if you aren't routinely dequeueing data.
  1147. *
  1148. * Capture devices will not queue data when paused; if you are expecting to
  1149. * not need captured audio for some length of time, use SDL_PauseAudioDevice()
  1150. * to stop the capture device from queueing more data. This can be useful
  1151. * during, say, level loading times. When unpaused, capture devices will start
  1152. * queueing data from that point, having flushed any capturable data available
  1153. * while paused.
  1154. *
  1155. * This function is thread-safe, but dequeueing from the same device from two
  1156. * threads at once does not promise which thread will dequeue data first.
  1157. *
  1158. * You may not dequeue audio from a device that is using an
  1159. * application-supplied callback; doing so returns an error. You have to use
  1160. * the audio callback, or dequeue audio with this function, but not both.
  1161. *
  1162. * You should not call SDL_LockAudio() on the device before dequeueing; SDL
  1163. * handles locking internally for this function.
  1164. *
  1165. * \param dev the device ID from which we will dequeue audio
  1166. * \param data a pointer into where audio data should be copied
  1167. * \param len the number of bytes (not samples!) to which (data) points
  1168. * \returns the number of bytes dequeued, which could be less than requested;
  1169. * call SDL_GetError() for more information.
  1170. *
  1171. * \since This function is available since SDL 2.0.5.
  1172. *
  1173. * \sa SDL_ClearQueuedAudio
  1174. * \sa SDL_GetQueuedAudioSize
  1175. */
  1176. extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
  1177. /**
  1178. * Get the number of bytes of still-queued audio.
  1179. *
  1180. * For playback devices: this is the number of bytes that have been queued for
  1181. * playback with SDL_QueueAudio(), but have not yet been sent to the hardware.
  1182. *
  1183. * Once we've sent it to the hardware, this function can not decide the exact
  1184. * byte boundary of what has been played. It's possible that we just gave the
  1185. * hardware several kilobytes right before you called this function, but it
  1186. * hasn't played any of it yet, or maybe half of it, etc.
  1187. *
  1188. * For capture devices, this is the number of bytes that have been captured by
  1189. * the device and are waiting for you to dequeue. This number may grow at any
  1190. * time, so this only informs of the lower-bound of available data.
  1191. *
  1192. * You may not queue or dequeue audio on a device that is using an
  1193. * application-supplied callback; calling this function on such a device
  1194. * always returns 0. You have to use the audio callback or queue audio, but
  1195. * not both.
  1196. *
  1197. * You should not call SDL_LockAudio() on the device before querying; SDL
  1198. * handles locking internally for this function.
  1199. *
  1200. * \param dev the device ID of which we will query queued audio size
  1201. * \returns the number of bytes (not samples!) of queued audio.
  1202. *
  1203. * \since This function is available since SDL 2.0.4.
  1204. *
  1205. * \sa SDL_ClearQueuedAudio
  1206. * \sa SDL_QueueAudio
  1207. * \sa SDL_DequeueAudio
  1208. */
  1209. extern DECLSPEC Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev);
  1210. /**
  1211. * Drop any queued audio data waiting to be sent to the hardware.
  1212. *
  1213. * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
  1214. * output devices, the hardware will start playing silence if more audio isn't
  1215. * queued. For capture devices, the hardware will start filling the empty
  1216. * queue with new data if the capture device isn't paused.
  1217. *
  1218. * This will not prevent playback of queued audio that's already been sent to
  1219. * the hardware, as we can not undo that, so expect there to be some fraction
  1220. * of a second of audio that might still be heard. This can be useful if you
  1221. * want to, say, drop any pending music or any unprocessed microphone input
  1222. * during a level change in your game.
  1223. *
  1224. * You may not queue or dequeue audio on a device that is using an
  1225. * application-supplied callback; calling this function on such a device
  1226. * always returns 0. You have to use the audio callback or queue audio, but
  1227. * not both.
  1228. *
  1229. * You should not call SDL_LockAudio() on the device before clearing the
  1230. * queue; SDL handles locking internally for this function.
  1231. *
  1232. * This function always succeeds and thus returns void.
  1233. *
  1234. * \param dev the device ID of which to clear the audio queue
  1235. *
  1236. * \since This function is available since SDL 2.0.4.
  1237. *
  1238. * \sa SDL_GetQueuedAudioSize
  1239. * \sa SDL_QueueAudio
  1240. * \sa SDL_DequeueAudio
  1241. */
  1242. extern DECLSPEC void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
  1243. /**
  1244. * \name Audio lock functions
  1245. *
  1246. * The lock manipulated by these functions protects the callback function.
  1247. * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
  1248. * the callback function is not running. Do not call these from the callback
  1249. * function or you will cause deadlock.
  1250. */
  1251. /* @{ */
  1252. /**
  1253. * This function is a legacy means of locking the audio device.
  1254. *
  1255. * New programs might want to use SDL_LockAudioDevice() instead. This function
  1256. * is equivalent to calling...
  1257. *
  1258. * ```c
  1259. * SDL_LockAudioDevice(1);
  1260. * ```
  1261. *
  1262. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  1263. *
  1264. * \since This function is available since SDL 2.0.0.
  1265. *
  1266. * \sa SDL_LockAudioDevice
  1267. * \sa SDL_UnlockAudio
  1268. * \sa SDL_UnlockAudioDevice
  1269. */
  1270. extern DECLSPEC void SDLCALL SDL_LockAudio(void);
  1271. /**
  1272. * Use this function to lock out the audio callback function for a specified
  1273. * device.
  1274. *
  1275. * The lock manipulated by these functions protects the audio callback
  1276. * function specified in SDL_OpenAudioDevice(). During a
  1277. * SDL_LockAudioDevice()/SDL_UnlockAudioDevice() pair, you can be guaranteed
  1278. * that the callback function for that device is not running, even if the
  1279. * device is not paused. While a device is locked, any other unpaused,
  1280. * unlocked devices may still run their callbacks.
  1281. *
  1282. * Calling this function from inside your audio callback is unnecessary. SDL
  1283. * obtains this lock before calling your function, and releases it when the
  1284. * function returns.
  1285. *
  1286. * You should not hold the lock longer than absolutely necessary. If you hold
  1287. * it too long, you'll experience dropouts in your audio playback. Ideally,
  1288. * your application locks the device, sets a few variables and unlocks again.
  1289. * Do not do heavy work while holding the lock for a device.
  1290. *
  1291. * It is safe to lock the audio device multiple times, as long as you unlock
  1292. * it an equivalent number of times. The callback will not run until the
  1293. * device has been unlocked completely in this way. If your application fails
  1294. * to unlock the device appropriately, your callback will never run, you might
  1295. * hear repeating bursts of audio, and SDL_CloseAudioDevice() will probably
  1296. * deadlock.
  1297. *
  1298. * Internally, the audio device lock is a mutex; if you lock from two threads
  1299. * at once, not only will you block the audio callback, you'll block the other
  1300. * thread.
  1301. *
  1302. * \param dev the ID of the device to be locked
  1303. *
  1304. * \since This function is available since SDL 2.0.0.
  1305. *
  1306. * \sa SDL_UnlockAudioDevice
  1307. */
  1308. extern DECLSPEC void SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
  1309. /**
  1310. * This function is a legacy means of unlocking the audio device.
  1311. *
  1312. * New programs might want to use SDL_UnlockAudioDevice() instead. This
  1313. * function is equivalent to calling...
  1314. *
  1315. * ```c
  1316. * SDL_UnlockAudioDevice(1);
  1317. * ```
  1318. *
  1319. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  1320. *
  1321. * \since This function is available since SDL 2.0.0.
  1322. *
  1323. * \sa SDL_LockAudio
  1324. * \sa SDL_UnlockAudioDevice
  1325. */
  1326. extern DECLSPEC void SDLCALL SDL_UnlockAudio(void);
  1327. /**
  1328. * Use this function to unlock the audio callback function for a specified
  1329. * device.
  1330. *
  1331. * This function should be paired with a previous SDL_LockAudioDevice() call.
  1332. *
  1333. * \param dev the ID of the device to be unlocked
  1334. *
  1335. * \since This function is available since SDL 2.0.0.
  1336. *
  1337. * \sa SDL_LockAudioDevice
  1338. */
  1339. extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
  1340. /* @} *//* Audio lock functions */
  1341. /**
  1342. * This function is a legacy means of closing the audio device.
  1343. *
  1344. * This function is equivalent to calling...
  1345. *
  1346. * ```c
  1347. * SDL_CloseAudioDevice(1);
  1348. * ```
  1349. *
  1350. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  1351. *
  1352. * \since This function is available since SDL 2.0.0.
  1353. *
  1354. * \sa SDL_OpenAudio
  1355. */
  1356. extern DECLSPEC void SDLCALL SDL_CloseAudio(void);
  1357. /**
  1358. * Use this function to shut down audio processing and close the audio device.
  1359. *
  1360. * The application should close open audio devices once they are no longer
  1361. * needed. Calling this function will wait until the device's audio callback
  1362. * is not running, release the audio hardware and then clean up internal
  1363. * state. No further audio will play from this device once this function
  1364. * returns.
  1365. *
  1366. * This function may block briefly while pending audio data is played by the
  1367. * hardware, so that applications don't drop the last buffer of data they
  1368. * supplied.
  1369. *
  1370. * The device ID is invalid as soon as the device is closed, and is eligible
  1371. * for reuse in a new SDL_OpenAudioDevice() call immediately.
  1372. *
  1373. * \param dev an audio device previously opened with SDL_OpenAudioDevice()
  1374. *
  1375. * \since This function is available since SDL 2.0.0.
  1376. *
  1377. * \sa SDL_OpenAudioDevice
  1378. */
  1379. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
  1380. /* Ends C function definitions when using C++ */
  1381. #ifdef __cplusplus
  1382. }
  1383. #endif
  1384. #include "close_code.h"
  1385. #endif /* SDL_audio_h_ */
  1386. /* vi: set ts=4 sw=4 expandtab: */