🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1500 lines
58 KiB

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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 LFE BL BR (4.1 surround)
  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. * \sa SDL_GetDefaultAudioInfo
  455. */
  456. extern DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(int index,
  457. int iscapture);
  458. /**
  459. * Get the preferred audio format of a specific audio device.
  460. *
  461. * This function is only valid after a successfully initializing the audio
  462. * subsystem. The values returned by this function reflect the latest call to
  463. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  464. * hardware.
  465. *
  466. * `spec` will be filled with the sample rate, sample format, and channel
  467. * count.
  468. *
  469. * \param index the index of the audio device; valid values range from 0 to
  470. * SDL_GetNumAudioDevices() - 1
  471. * \param iscapture non-zero to query the list of recording devices, zero to
  472. * query the list of output devices.
  473. * \param spec The SDL_AudioSpec to be initialized by this function.
  474. * \returns 0 on success, nonzero on error
  475. *
  476. * \since This function is available since SDL 2.0.16.
  477. *
  478. * \sa SDL_GetNumAudioDevices
  479. * \sa SDL_GetDefaultAudioInfo
  480. */
  481. extern DECLSPEC int SDLCALL SDL_GetAudioDeviceSpec(int index,
  482. int iscapture,
  483. SDL_AudioSpec *spec);
  484. /**
  485. * Get the name and preferred format of the default audio device.
  486. *
  487. * Some (but not all!) platforms have an isolated mechanism to get information
  488. * about the "default" device. This can actually be a completely different
  489. * device that's not in the list you get from SDL_GetAudioDeviceSpec(). It can
  490. * even be a network address! (This is discussed in SDL_OpenAudioDevice().)
  491. *
  492. * As a result, this call is not guaranteed to be performant, as it can query
  493. * the sound server directly every time, unlike the other query functions. You
  494. * should call this function sparingly!
  495. *
  496. * `spec` will be filled with the sample rate, sample format, and channel
  497. * count, if a default device exists on the system. If `name` is provided,
  498. * will be filled with either a dynamically-allocated UTF-8 string or NULL.
  499. *
  500. * \param name A pointer to be filled with the name of the default device (can
  501. * be NULL). Please call SDL_free() when you are done with this
  502. * pointer!
  503. * \param spec The SDL_AudioSpec to be initialized by this function.
  504. * \param iscapture non-zero to query the default recording device, zero to
  505. * query the default output device.
  506. * \returns 0 on success, nonzero on error
  507. *
  508. * \since This function is available since SDL 2.24.0.
  509. *
  510. * \sa SDL_GetAudioDeviceName
  511. * \sa SDL_GetAudioDeviceSpec
  512. * \sa SDL_OpenAudioDevice
  513. */
  514. extern DECLSPEC int SDLCALL SDL_GetDefaultAudioInfo(char **name,
  515. SDL_AudioSpec *spec,
  516. int iscapture);
  517. /**
  518. * Open a specific audio device.
  519. *
  520. * SDL_OpenAudio(), unlike this function, always acts on device ID 1. As such,
  521. * this function will never return a 1 so as not to conflict with the legacy
  522. * function.
  523. *
  524. * Please note that SDL 2.0 before 2.0.5 did not support recording; as such,
  525. * this function would fail if `iscapture` was not zero. Starting with SDL
  526. * 2.0.5, recording is implemented and this value can be non-zero.
  527. *
  528. * Passing in a `device` name of NULL requests the most reasonable default
  529. * (and is equivalent to what SDL_OpenAudio() does to choose a device). The
  530. * `device` name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but
  531. * some drivers allow arbitrary and driver-specific strings, such as a
  532. * hostname/IP address for a remote audio server, or a filename in the
  533. * diskaudio driver.
  534. *
  535. * An opened audio device starts out paused, and should be enabled for playing
  536. * by calling SDL_PauseAudioDevice(devid, 0) when you are ready for your audio
  537. * callback function to be called. Since the audio driver may modify the
  538. * requested size of the audio buffer, you should allocate any local mixing
  539. * buffers after you open the audio device.
  540. *
  541. * The audio callback runs in a separate thread in most cases; you can prevent
  542. * race conditions between your callback and other threads without fully
  543. * pausing playback with SDL_LockAudioDevice(). For more information about the
  544. * callback, see SDL_AudioSpec.
  545. *
  546. * Managing the audio spec via 'desired' and 'obtained':
  547. *
  548. * When filling in the desired audio spec structure:
  549. *
  550. * - `desired->freq` should be the frequency in sample-frames-per-second (Hz).
  551. * - `desired->format` should be the audio format (`AUDIO_S16SYS`, etc).
  552. * - `desired->samples` is the desired size of the audio buffer, in _sample
  553. * frames_ (with stereo output, two samples--left and right--would make a
  554. * single sample frame). This number should be a power of two, and may be
  555. * adjusted by the audio driver to a value more suitable for the hardware.
  556. * Good values seem to range between 512 and 8096 inclusive, depending on
  557. * the application and CPU speed. Smaller values reduce latency, but can
  558. * lead to underflow if the application is doing heavy processing and cannot
  559. * fill the audio buffer in time. Note that the number of sample frames is
  560. * directly related to time by the following formula: `ms =
  561. * (sampleframes*1000)/freq`
  562. * - `desired->size` is the size in _bytes_ of the audio buffer, and is
  563. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  564. * - `desired->silence` is the value used to set the buffer to silence, and is
  565. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  566. * - `desired->callback` should be set to a function that will be called when
  567. * the audio device is ready for more data. It is passed a pointer to the
  568. * audio buffer, and the length in bytes of the audio buffer. This function
  569. * usually runs in a separate thread, and so you should protect data
  570. * structures that it accesses by calling SDL_LockAudioDevice() and
  571. * SDL_UnlockAudioDevice() in your code. Alternately, you may pass a NULL
  572. * pointer here, and call SDL_QueueAudio() with some frequency, to queue
  573. * more audio samples to be played (or for capture devices, call
  574. * SDL_DequeueAudio() with some frequency, to obtain audio samples).
  575. * - `desired->userdata` is passed as the first parameter to your callback
  576. * function. If you passed a NULL callback, this value is ignored.
  577. *
  578. * `allowed_changes` can have the following flags OR'd together:
  579. *
  580. * - `SDL_AUDIO_ALLOW_FREQUENCY_CHANGE`
  581. * - `SDL_AUDIO_ALLOW_FORMAT_CHANGE`
  582. * - `SDL_AUDIO_ALLOW_CHANNELS_CHANGE`
  583. * - `SDL_AUDIO_ALLOW_SAMPLES_CHANGE`
  584. * - `SDL_AUDIO_ALLOW_ANY_CHANGE`
  585. *
  586. * These flags specify how SDL should behave when a device cannot offer a
  587. * specific feature. If the application requests a feature that the hardware
  588. * doesn't offer, SDL will always try to get the closest equivalent.
  589. *
  590. * For example, if you ask for float32 audio format, but the sound card only
  591. * supports int16, SDL will set the hardware to int16. If you had set
  592. * SDL_AUDIO_ALLOW_FORMAT_CHANGE, SDL will change the format in the `obtained`
  593. * structure. If that flag was *not* set, SDL will prepare to convert your
  594. * callback's float32 audio to int16 before feeding it to the hardware and
  595. * will keep the originally requested format in the `obtained` structure.
  596. *
  597. * The resulting audio specs, varying depending on hardware and on what
  598. * changes were allowed, will then be written back to `obtained`.
  599. *
  600. * If your application can only handle one specific data format, pass a zero
  601. * for `allowed_changes` and let SDL transparently handle any differences.
  602. *
  603. * \param device a UTF-8 string reported by SDL_GetAudioDeviceName() or a
  604. * driver-specific name as appropriate. NULL requests the most
  605. * reasonable default device.
  606. * \param iscapture non-zero to specify a device should be opened for
  607. * recording, not playback
  608. * \param desired an SDL_AudioSpec structure representing the desired output
  609. * format; see SDL_OpenAudio() for more information
  610. * \param obtained an SDL_AudioSpec structure filled in with the actual output
  611. * format; see SDL_OpenAudio() for more information
  612. * \param allowed_changes 0, or one or more flags OR'd together
  613. * \returns a valid device ID that is > 0 on success or 0 on failure; call
  614. * SDL_GetError() for more information.
  615. *
  616. * For compatibility with SDL 1.2, this will never return 1, since
  617. * SDL reserves that ID for the legacy SDL_OpenAudio() function.
  618. *
  619. * \since This function is available since SDL 2.0.0.
  620. *
  621. * \sa SDL_CloseAudioDevice
  622. * \sa SDL_GetAudioDeviceName
  623. * \sa SDL_LockAudioDevice
  624. * \sa SDL_OpenAudio
  625. * \sa SDL_PauseAudioDevice
  626. * \sa SDL_UnlockAudioDevice
  627. */
  628. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(
  629. const char *device,
  630. int iscapture,
  631. const SDL_AudioSpec *desired,
  632. SDL_AudioSpec *obtained,
  633. int allowed_changes);
  634. /**
  635. * \name Audio state
  636. *
  637. * Get the current audio state.
  638. */
  639. /* @{ */
  640. typedef enum
  641. {
  642. SDL_AUDIO_STOPPED = 0,
  643. SDL_AUDIO_PLAYING,
  644. SDL_AUDIO_PAUSED
  645. } SDL_AudioStatus;
  646. /**
  647. * This function is a legacy means of querying the audio device.
  648. *
  649. * New programs might want to use SDL_GetAudioDeviceStatus() instead. This
  650. * function is equivalent to calling...
  651. *
  652. * ```c
  653. * SDL_GetAudioDeviceStatus(1);
  654. * ```
  655. *
  656. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  657. *
  658. * \returns the SDL_AudioStatus of the audio device opened by SDL_OpenAudio().
  659. *
  660. * \since This function is available since SDL 2.0.0.
  661. *
  662. * \sa SDL_GetAudioDeviceStatus
  663. */
  664. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioStatus(void);
  665. /**
  666. * Use this function to get the current audio state of an audio device.
  667. *
  668. * \param dev the ID of an audio device previously opened with
  669. * SDL_OpenAudioDevice()
  670. * \returns the SDL_AudioStatus of the specified audio device.
  671. *
  672. * \since This function is available since SDL 2.0.0.
  673. *
  674. * \sa SDL_PauseAudioDevice
  675. */
  676. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev);
  677. /* @} *//* Audio State */
  678. /**
  679. * \name Pause audio functions
  680. *
  681. * These functions pause and unpause the audio callback processing.
  682. * They should be called with a parameter of 0 after opening the audio
  683. * device to start playing sound. This is so you can safely initialize
  684. * data for your callback function after opening the audio device.
  685. * Silence will be written to the audio device during the pause.
  686. */
  687. /* @{ */
  688. /**
  689. * This function is a legacy means of pausing the audio device.
  690. *
  691. * New programs might want to use SDL_PauseAudioDevice() instead. This
  692. * function is equivalent to calling...
  693. *
  694. * ```c
  695. * SDL_PauseAudioDevice(1, pause_on);
  696. * ```
  697. *
  698. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  699. *
  700. * \param pause_on non-zero to pause, 0 to unpause
  701. *
  702. * \since This function is available since SDL 2.0.0.
  703. *
  704. * \sa SDL_GetAudioStatus
  705. * \sa SDL_PauseAudioDevice
  706. */
  707. extern DECLSPEC void SDLCALL SDL_PauseAudio(int pause_on);
  708. /**
  709. * Use this function to pause and unpause audio playback on a specified
  710. * device.
  711. *
  712. * This function pauses and unpauses the audio callback processing for a given
  713. * device. Newly-opened audio devices start in the paused state, so you must
  714. * call this function with **pause_on**=0 after opening the specified audio
  715. * device to start playing sound. This allows you to safely initialize data
  716. * for your callback function after opening the audio device. Silence will be
  717. * written to the audio device while paused, and the audio callback is
  718. * guaranteed to not be called. Pausing one device does not prevent other
  719. * unpaused devices from running their callbacks.
  720. *
  721. * Pausing state does not stack; even if you pause a device several times, a
  722. * single unpause will start the device playing again, and vice versa. This is
  723. * different from how SDL_LockAudioDevice() works.
  724. *
  725. * If you just need to protect a few variables from race conditions vs your
  726. * callback, you shouldn't pause the audio device, as it will lead to dropouts
  727. * in the audio playback. Instead, you should use SDL_LockAudioDevice().
  728. *
  729. * \param dev a device opened by SDL_OpenAudioDevice()
  730. * \param pause_on non-zero to pause, 0 to unpause
  731. *
  732. * \since This function is available since SDL 2.0.0.
  733. *
  734. * \sa SDL_LockAudioDevice
  735. */
  736. extern DECLSPEC void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev,
  737. int pause_on);
  738. /* @} *//* Pause audio functions */
  739. /**
  740. * Load the audio data of a WAVE file into memory.
  741. *
  742. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len` to
  743. * be valid pointers. The entire data portion of the file is then loaded into
  744. * memory and decoded if necessary.
  745. *
  746. * If `freesrc` is non-zero, the data source gets automatically closed and
  747. * freed before the function returns.
  748. *
  749. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  750. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits), and
  751. * A-law and mu-law (8 bits). Other formats are currently unsupported and
  752. * cause an error.
  753. *
  754. * If this function succeeds, the pointer returned by it is equal to `spec`
  755. * and the pointer to the audio data allocated by the function is written to
  756. * `audio_buf` and its length in bytes to `audio_len`. The SDL_AudioSpec
  757. * members `freq`, `channels`, and `format` are set to the values of the audio
  758. * data in the buffer. The `samples` member is set to a sane default and all
  759. * others are set to zero.
  760. *
  761. * It's necessary to use SDL_FreeWAV() to free the audio data returned in
  762. * `audio_buf` when it is no longer used.
  763. *
  764. * Because of the underspecification of the .WAV format, there are many
  765. * problematic files in the wild that cause issues with strict decoders. To
  766. * provide compatibility with these files, this decoder is lenient in regards
  767. * to the truncation of the file, the fact chunk, and the size of the RIFF
  768. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`,
  769. * `SDL_HINT_WAVE_TRUNCATION`, and `SDL_HINT_WAVE_FACT_CHUNK` can be used to
  770. * tune the behavior of the loading process.
  771. *
  772. * Any file that is invalid (due to truncation, corruption, or wrong values in
  773. * the headers), too big, or unsupported causes an error. Additionally, any
  774. * critical I/O error from the data source will terminate the loading process
  775. * with an error. The function returns NULL on error and in all cases (with
  776. * the exception of `src` being NULL), an appropriate error message will be
  777. * set.
  778. *
  779. * It is required that the data source supports seeking.
  780. *
  781. * Example:
  782. *
  783. * ```c
  784. * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
  785. * ```
  786. *
  787. * Note that the SDL_LoadWAV macro does this same thing for you, but in a less
  788. * messy way:
  789. *
  790. * ```c
  791. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  792. * ```
  793. *
  794. * \param src The data source for the WAVE data
  795. * \param freesrc If non-zero, SDL will _always_ free the data source
  796. * \param spec An SDL_AudioSpec that will be filled in with the wave file's
  797. * format details
  798. * \param audio_buf A pointer filled with the audio data, allocated by the
  799. * function.
  800. * \param audio_len A pointer filled with the length of the audio data buffer
  801. * in bytes
  802. * \returns This function, if successfully called, returns `spec`, which will
  803. * be filled with the audio data format of the wave source data.
  804. * `audio_buf` will be filled with a pointer to an allocated buffer
  805. * containing the audio data, and `audio_len` is filled with the
  806. * length of that audio buffer in bytes.
  807. *
  808. * This function returns NULL if the .WAV file cannot be opened, uses
  809. * an unknown data format, or is corrupt; call SDL_GetError() for
  810. * more information.
  811. *
  812. * When the application is done with the data returned in
  813. * `audio_buf`, it should call SDL_FreeWAV() to dispose of it.
  814. *
  815. * \since This function is available since SDL 2.0.0.
  816. *
  817. * \sa SDL_FreeWAV
  818. * \sa SDL_LoadWAV
  819. */
  820. extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
  821. int freesrc,
  822. SDL_AudioSpec * spec,
  823. Uint8 ** audio_buf,
  824. Uint32 * audio_len);
  825. /**
  826. * Loads a WAV from a file.
  827. * Compatibility convenience function.
  828. */
  829. #define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
  830. SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
  831. /**
  832. * Free data previously allocated with SDL_LoadWAV() or SDL_LoadWAV_RW().
  833. *
  834. * After a WAVE file has been opened with SDL_LoadWAV() or SDL_LoadWAV_RW()
  835. * its data can eventually be freed with SDL_FreeWAV(). It is safe to call
  836. * this function with a NULL pointer.
  837. *
  838. * \param audio_buf a pointer to the buffer created by SDL_LoadWAV() or
  839. * SDL_LoadWAV_RW()
  840. *
  841. * \since This function is available since SDL 2.0.0.
  842. *
  843. * \sa SDL_LoadWAV
  844. * \sa SDL_LoadWAV_RW
  845. */
  846. extern DECLSPEC void SDLCALL SDL_FreeWAV(Uint8 * audio_buf);
  847. /**
  848. * Initialize an SDL_AudioCVT structure for conversion.
  849. *
  850. * Before an SDL_AudioCVT structure can be used to convert audio data it must
  851. * be initialized with source and destination information.
  852. *
  853. * This function will zero out every field of the SDL_AudioCVT, so it must be
  854. * called before the application fills in the final buffer information.
  855. *
  856. * Once this function has returned successfully, and reported that a
  857. * conversion is necessary, the application fills in the rest of the fields in
  858. * SDL_AudioCVT, now that it knows how large a buffer it needs to allocate,
  859. * and then can call SDL_ConvertAudio() to complete the conversion.
  860. *
  861. * \param cvt an SDL_AudioCVT structure filled in with audio conversion
  862. * information
  863. * \param src_format the source format of the audio data; for more info see
  864. * SDL_AudioFormat
  865. * \param src_channels the number of channels in the source
  866. * \param src_rate the frequency (sample-frames-per-second) of the source
  867. * \param dst_format the destination format of the audio data; for more info
  868. * see SDL_AudioFormat
  869. * \param dst_channels the number of channels in the destination
  870. * \param dst_rate the frequency (sample-frames-per-second) of the destination
  871. * \returns 1 if the audio filter is prepared, 0 if no conversion is needed,
  872. * or a negative error code on failure; call SDL_GetError() for more
  873. * information.
  874. *
  875. * \since This function is available since SDL 2.0.0.
  876. *
  877. * \sa SDL_ConvertAudio
  878. */
  879. extern DECLSPEC int SDLCALL SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
  880. SDL_AudioFormat src_format,
  881. Uint8 src_channels,
  882. int src_rate,
  883. SDL_AudioFormat dst_format,
  884. Uint8 dst_channels,
  885. int dst_rate);
  886. /**
  887. * Convert audio data to a desired audio format.
  888. *
  889. * This function does the actual audio data conversion, after the application
  890. * has called SDL_BuildAudioCVT() to prepare the conversion information and
  891. * then filled in the buffer details.
  892. *
  893. * Once the application has initialized the `cvt` structure using
  894. * SDL_BuildAudioCVT(), allocated an audio buffer and filled it with audio
  895. * data in the source format, this function will convert the buffer, in-place,
  896. * to the desired format.
  897. *
  898. * The data conversion may go through several passes; any given pass may
  899. * possibly temporarily increase the size of the data. For example, SDL might
  900. * expand 16-bit data to 32 bits before resampling to a lower frequency,
  901. * shrinking the data size after having grown it briefly. Since the supplied
  902. * buffer will be both the source and destination, converting as necessary
  903. * in-place, the application must allocate a buffer that will fully contain
  904. * the data during its largest conversion pass. After SDL_BuildAudioCVT()
  905. * returns, the application should set the `cvt->len` field to the size, in
  906. * bytes, of the source data, and allocate a buffer that is `cvt->len *
  907. * cvt->len_mult` bytes long for the `buf` field.
  908. *
  909. * The source data should be copied into this buffer before the call to
  910. * SDL_ConvertAudio(). Upon successful return, this buffer will contain the
  911. * converted audio, and `cvt->len_cvt` will be the size of the converted data,
  912. * in bytes. Any bytes in the buffer past `cvt->len_cvt` are undefined once
  913. * this function returns.
  914. *
  915. * \param cvt an SDL_AudioCVT structure that was previously set up by
  916. * SDL_BuildAudioCVT().
  917. * \returns 0 if the conversion was completed successfully or a negative error
  918. * code on failure; call SDL_GetError() for more information.
  919. *
  920. * \since This function is available since SDL 2.0.0.
  921. *
  922. * \sa SDL_BuildAudioCVT
  923. */
  924. extern DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT * cvt);
  925. /* SDL_AudioStream is a new audio conversion interface.
  926. The benefits vs SDL_AudioCVT:
  927. - it can handle resampling data in chunks without generating
  928. artifacts, when it doesn't have the complete buffer available.
  929. - it can handle incoming data in any variable size.
  930. - You push data as you have it, and pull it when you need it
  931. */
  932. /* this is opaque to the outside world. */
  933. struct _SDL_AudioStream;
  934. typedef struct _SDL_AudioStream SDL_AudioStream;
  935. /**
  936. * Create a new audio stream.
  937. *
  938. * \param src_format The format of the source audio
  939. * \param src_channels The number of channels of the source audio
  940. * \param src_rate The sampling rate of the source audio
  941. * \param dst_format The format of the desired audio output
  942. * \param dst_channels The number of channels of the desired audio output
  943. * \param dst_rate The sampling rate of the desired audio output
  944. * \returns 0 on success, or -1 on error.
  945. *
  946. * \since This function is available since SDL 2.0.7.
  947. *
  948. * \sa SDL_AudioStreamPut
  949. * \sa SDL_AudioStreamGet
  950. * \sa SDL_AudioStreamAvailable
  951. * \sa SDL_AudioStreamFlush
  952. * \sa SDL_AudioStreamClear
  953. * \sa SDL_FreeAudioStream
  954. */
  955. extern DECLSPEC SDL_AudioStream * SDLCALL SDL_NewAudioStream(const SDL_AudioFormat src_format,
  956. const Uint8 src_channels,
  957. const int src_rate,
  958. const SDL_AudioFormat dst_format,
  959. const Uint8 dst_channels,
  960. const int dst_rate);
  961. /**
  962. * Add data to be converted/resampled to the stream.
  963. *
  964. * \param stream The stream the audio data is being added to
  965. * \param buf A pointer to the audio data to add
  966. * \param len The number of bytes to write to the stream
  967. * \returns 0 on success, or -1 on error.
  968. *
  969. * \since This function is available since SDL 2.0.7.
  970. *
  971. * \sa SDL_NewAudioStream
  972. * \sa SDL_AudioStreamGet
  973. * \sa SDL_AudioStreamAvailable
  974. * \sa SDL_AudioStreamFlush
  975. * \sa SDL_AudioStreamClear
  976. * \sa SDL_FreeAudioStream
  977. */
  978. extern DECLSPEC int SDLCALL SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len);
  979. /**
  980. * Get converted/resampled data from the stream
  981. *
  982. * \param stream The stream the audio is being requested from
  983. * \param buf A buffer to fill with audio data
  984. * \param len The maximum number of bytes to fill
  985. * \returns the number of bytes read from the stream, or -1 on error
  986. *
  987. * \since This function is available since SDL 2.0.7.
  988. *
  989. * \sa SDL_NewAudioStream
  990. * \sa SDL_AudioStreamPut
  991. * \sa SDL_AudioStreamAvailable
  992. * \sa SDL_AudioStreamFlush
  993. * \sa SDL_AudioStreamClear
  994. * \sa SDL_FreeAudioStream
  995. */
  996. extern DECLSPEC int SDLCALL SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len);
  997. /**
  998. * Get the number of converted/resampled bytes available.
  999. *
  1000. * The stream may be buffering data behind the scenes until it has enough to
  1001. * resample correctly, so this number might be lower than what you expect, or
  1002. * even be zero. Add more data or flush the stream if you need the data now.
  1003. *
  1004. * \since This function is available since SDL 2.0.7.
  1005. *
  1006. * \sa SDL_NewAudioStream
  1007. * \sa SDL_AudioStreamPut
  1008. * \sa SDL_AudioStreamGet
  1009. * \sa SDL_AudioStreamFlush
  1010. * \sa SDL_AudioStreamClear
  1011. * \sa SDL_FreeAudioStream
  1012. */
  1013. extern DECLSPEC int SDLCALL SDL_AudioStreamAvailable(SDL_AudioStream *stream);
  1014. /**
  1015. * Tell the stream that you're done sending data, and anything being buffered
  1016. * should be converted/resampled and made available immediately.
  1017. *
  1018. * It is legal to add more data to a stream after flushing, but there will be
  1019. * audio gaps in the output. Generally this is intended to signal the end of
  1020. * input, so the complete output becomes available.
  1021. *
  1022. * \since This function is available since SDL 2.0.7.
  1023. *
  1024. * \sa SDL_NewAudioStream
  1025. * \sa SDL_AudioStreamPut
  1026. * \sa SDL_AudioStreamGet
  1027. * \sa SDL_AudioStreamAvailable
  1028. * \sa SDL_AudioStreamClear
  1029. * \sa SDL_FreeAudioStream
  1030. */
  1031. extern DECLSPEC int SDLCALL SDL_AudioStreamFlush(SDL_AudioStream *stream);
  1032. /**
  1033. * Clear any pending data in the stream without converting it
  1034. *
  1035. * \since This function is available since SDL 2.0.7.
  1036. *
  1037. * \sa SDL_NewAudioStream
  1038. * \sa SDL_AudioStreamPut
  1039. * \sa SDL_AudioStreamGet
  1040. * \sa SDL_AudioStreamAvailable
  1041. * \sa SDL_AudioStreamFlush
  1042. * \sa SDL_FreeAudioStream
  1043. */
  1044. extern DECLSPEC void SDLCALL SDL_AudioStreamClear(SDL_AudioStream *stream);
  1045. /**
  1046. * Free an audio stream
  1047. *
  1048. * \since This function is available since SDL 2.0.7.
  1049. *
  1050. * \sa SDL_NewAudioStream
  1051. * \sa SDL_AudioStreamPut
  1052. * \sa SDL_AudioStreamGet
  1053. * \sa SDL_AudioStreamAvailable
  1054. * \sa SDL_AudioStreamFlush
  1055. * \sa SDL_AudioStreamClear
  1056. */
  1057. extern DECLSPEC void SDLCALL SDL_FreeAudioStream(SDL_AudioStream *stream);
  1058. #define SDL_MIX_MAXVOLUME 128
  1059. /**
  1060. * This function is a legacy means of mixing audio.
  1061. *
  1062. * This function is equivalent to calling...
  1063. *
  1064. * ```c
  1065. * SDL_MixAudioFormat(dst, src, format, len, volume);
  1066. * ```
  1067. *
  1068. * ...where `format` is the obtained format of the audio device from the
  1069. * legacy SDL_OpenAudio() function.
  1070. *
  1071. * \param dst the destination for the mixed audio
  1072. * \param src the source audio buffer to be mixed
  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. * \sa SDL_MixAudioFormat
  1080. */
  1081. extern DECLSPEC void SDLCALL SDL_MixAudio(Uint8 * dst, const Uint8 * src,
  1082. Uint32 len, int volume);
  1083. /**
  1084. * Mix audio data in a specified format.
  1085. *
  1086. * This takes an audio buffer `src` of `len` bytes of `format` data and mixes
  1087. * it into `dst`, performing addition, volume adjustment, and overflow
  1088. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  1089. * `format` data.
  1090. *
  1091. * This is provided for convenience -- you can mix your own audio data.
  1092. *
  1093. * Do not use this function for mixing together more than two streams of
  1094. * sample data. The output from repeated application of this function may be
  1095. * distorted by clipping, because there is no accumulator with greater range
  1096. * than the input (not to mention this being an inefficient way of doing it).
  1097. *
  1098. * It is a common misconception that this function is required to write audio
  1099. * data to an output stream in an audio callback. While you can do that,
  1100. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  1101. * audio stream with a volume adjustment.
  1102. *
  1103. * \param dst the destination for the mixed audio
  1104. * \param src the source audio buffer to be mixed
  1105. * \param format the SDL_AudioFormat structure representing the desired audio
  1106. * format
  1107. * \param len the length of the audio buffer in bytes
  1108. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  1109. * for full audio volume
  1110. *
  1111. * \since This function is available since SDL 2.0.0.
  1112. */
  1113. extern DECLSPEC void SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  1114. const Uint8 * src,
  1115. SDL_AudioFormat format,
  1116. Uint32 len, int volume);
  1117. /**
  1118. * Queue more audio on non-callback devices.
  1119. *
  1120. * If you are looking to retrieve queued audio from a non-callback capture
  1121. * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
  1122. * -1 to signify an error if you use it with capture devices.
  1123. *
  1124. * SDL offers two ways to feed audio to the device: you can either supply a
  1125. * callback that SDL triggers with some frequency to obtain more audio (pull
  1126. * method), or you can supply no callback, and then SDL will expect you to
  1127. * supply data at regular intervals (push method) with this function.
  1128. *
  1129. * There are no limits on the amount of data you can queue, short of
  1130. * exhaustion of address space. Queued data will drain to the device as
  1131. * necessary without further intervention from you. If the device needs audio
  1132. * but there is not enough queued, it will play silence to make up the
  1133. * difference. This means you will have skips in your audio playback if you
  1134. * aren't routinely queueing sufficient data.
  1135. *
  1136. * This function copies the supplied data, so you are safe to free it when the
  1137. * function returns. This function is thread-safe, but queueing to the same
  1138. * device from two threads at once does not promise which buffer will be
  1139. * queued first.
  1140. *
  1141. * You may not queue audio on a device that is using an application-supplied
  1142. * callback; doing so returns an error. You have to use the audio callback or
  1143. * queue audio with this function, but not both.
  1144. *
  1145. * You should not call SDL_LockAudio() on the device before queueing; SDL
  1146. * handles locking internally for this function.
  1147. *
  1148. * Note that SDL2 does not support planar audio. You will need to resample
  1149. * from planar audio formats into a non-planar one (see SDL_AudioFormat)
  1150. * before queuing audio.
  1151. *
  1152. * \param dev the device ID to which we will queue audio
  1153. * \param data the data to queue to the device for later playback
  1154. * \param len the number of bytes (not samples!) to which `data` points
  1155. * \returns 0 on success or a negative error code on failure; call
  1156. * SDL_GetError() for more information.
  1157. *
  1158. * \since This function is available since SDL 2.0.4.
  1159. *
  1160. * \sa SDL_ClearQueuedAudio
  1161. * \sa SDL_GetQueuedAudioSize
  1162. */
  1163. extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
  1164. /**
  1165. * Dequeue more audio on non-callback devices.
  1166. *
  1167. * If you are looking to queue audio for output on a non-callback playback
  1168. * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
  1169. * return 0 if you use it with playback devices.
  1170. *
  1171. * SDL offers two ways to retrieve audio from a capture device: you can either
  1172. * supply a callback that SDL triggers with some frequency as the device
  1173. * records more audio data, (push method), or you can supply no callback, and
  1174. * then SDL will expect you to retrieve data at regular intervals (pull
  1175. * method) with this function.
  1176. *
  1177. * There are no limits on the amount of data you can queue, short of
  1178. * exhaustion of address space. Data from the device will keep queuing as
  1179. * necessary without further intervention from you. This means you will
  1180. * eventually run out of memory if you aren't routinely dequeueing data.
  1181. *
  1182. * Capture devices will not queue data when paused; if you are expecting to
  1183. * not need captured audio for some length of time, use SDL_PauseAudioDevice()
  1184. * to stop the capture device from queueing more data. This can be useful
  1185. * during, say, level loading times. When unpaused, capture devices will start
  1186. * queueing data from that point, having flushed any capturable data available
  1187. * while paused.
  1188. *
  1189. * This function is thread-safe, but dequeueing from the same device from two
  1190. * threads at once does not promise which thread will dequeue data first.
  1191. *
  1192. * You may not dequeue audio from a device that is using an
  1193. * application-supplied callback; doing so returns an error. You have to use
  1194. * the audio callback, or dequeue audio with this function, but not both.
  1195. *
  1196. * You should not call SDL_LockAudio() on the device before dequeueing; SDL
  1197. * handles locking internally for this function.
  1198. *
  1199. * \param dev the device ID from which we will dequeue audio
  1200. * \param data a pointer into where audio data should be copied
  1201. * \param len the number of bytes (not samples!) to which (data) points
  1202. * \returns the number of bytes dequeued, which could be less than requested;
  1203. * call SDL_GetError() for more information.
  1204. *
  1205. * \since This function is available since SDL 2.0.5.
  1206. *
  1207. * \sa SDL_ClearQueuedAudio
  1208. * \sa SDL_GetQueuedAudioSize
  1209. */
  1210. extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
  1211. /**
  1212. * Get the number of bytes of still-queued audio.
  1213. *
  1214. * For playback devices: this is the number of bytes that have been queued for
  1215. * playback with SDL_QueueAudio(), but have not yet been sent to the hardware.
  1216. *
  1217. * Once we've sent it to the hardware, this function can not decide the exact
  1218. * byte boundary of what has been played. It's possible that we just gave the
  1219. * hardware several kilobytes right before you called this function, but it
  1220. * hasn't played any of it yet, or maybe half of it, etc.
  1221. *
  1222. * For capture devices, this is the number of bytes that have been captured by
  1223. * the device and are waiting for you to dequeue. This number may grow at any
  1224. * time, so this only informs of the lower-bound of available data.
  1225. *
  1226. * You may not queue or dequeue audio on a device that is using an
  1227. * application-supplied callback; calling this function on such a device
  1228. * always returns 0. You have to use the audio callback or queue audio, but
  1229. * not both.
  1230. *
  1231. * You should not call SDL_LockAudio() on the device before querying; SDL
  1232. * handles locking internally for this function.
  1233. *
  1234. * \param dev the device ID of which we will query queued audio size
  1235. * \returns the number of bytes (not samples!) of queued audio.
  1236. *
  1237. * \since This function is available since SDL 2.0.4.
  1238. *
  1239. * \sa SDL_ClearQueuedAudio
  1240. * \sa SDL_QueueAudio
  1241. * \sa SDL_DequeueAudio
  1242. */
  1243. extern DECLSPEC Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev);
  1244. /**
  1245. * Drop any queued audio data waiting to be sent to the hardware.
  1246. *
  1247. * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
  1248. * output devices, the hardware will start playing silence if more audio isn't
  1249. * queued. For capture devices, the hardware will start filling the empty
  1250. * queue with new data if the capture device isn't paused.
  1251. *
  1252. * This will not prevent playback of queued audio that's already been sent to
  1253. * the hardware, as we can not undo that, so expect there to be some fraction
  1254. * of a second of audio that might still be heard. This can be useful if you
  1255. * want to, say, drop any pending music or any unprocessed microphone input
  1256. * during a level change in your game.
  1257. *
  1258. * You may not queue or dequeue audio on a device that is using an
  1259. * application-supplied callback; calling this function on such a device
  1260. * always returns 0. You have to use the audio callback or queue audio, but
  1261. * not both.
  1262. *
  1263. * You should not call SDL_LockAudio() on the device before clearing the
  1264. * queue; SDL handles locking internally for this function.
  1265. *
  1266. * This function always succeeds and thus returns void.
  1267. *
  1268. * \param dev the device ID of which to clear the audio queue
  1269. *
  1270. * \since This function is available since SDL 2.0.4.
  1271. *
  1272. * \sa SDL_GetQueuedAudioSize
  1273. * \sa SDL_QueueAudio
  1274. * \sa SDL_DequeueAudio
  1275. */
  1276. extern DECLSPEC void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
  1277. /**
  1278. * \name Audio lock functions
  1279. *
  1280. * The lock manipulated by these functions protects the callback function.
  1281. * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
  1282. * the callback function is not running. Do not call these from the callback
  1283. * function or you will cause deadlock.
  1284. */
  1285. /* @{ */
  1286. /**
  1287. * This function is a legacy means of locking the audio device.
  1288. *
  1289. * New programs might want to use SDL_LockAudioDevice() instead. This function
  1290. * is equivalent to calling...
  1291. *
  1292. * ```c
  1293. * SDL_LockAudioDevice(1);
  1294. * ```
  1295. *
  1296. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  1297. *
  1298. * \since This function is available since SDL 2.0.0.
  1299. *
  1300. * \sa SDL_LockAudioDevice
  1301. * \sa SDL_UnlockAudio
  1302. * \sa SDL_UnlockAudioDevice
  1303. */
  1304. extern DECLSPEC void SDLCALL SDL_LockAudio(void);
  1305. /**
  1306. * Use this function to lock out the audio callback function for a specified
  1307. * device.
  1308. *
  1309. * The lock manipulated by these functions protects the audio callback
  1310. * function specified in SDL_OpenAudioDevice(). During a
  1311. * SDL_LockAudioDevice()/SDL_UnlockAudioDevice() pair, you can be guaranteed
  1312. * that the callback function for that device is not running, even if the
  1313. * device is not paused. While a device is locked, any other unpaused,
  1314. * unlocked devices may still run their callbacks.
  1315. *
  1316. * Calling this function from inside your audio callback is unnecessary. SDL
  1317. * obtains this lock before calling your function, and releases it when the
  1318. * function returns.
  1319. *
  1320. * You should not hold the lock longer than absolutely necessary. If you hold
  1321. * it too long, you'll experience dropouts in your audio playback. Ideally,
  1322. * your application locks the device, sets a few variables and unlocks again.
  1323. * Do not do heavy work while holding the lock for a device.
  1324. *
  1325. * It is safe to lock the audio device multiple times, as long as you unlock
  1326. * it an equivalent number of times. The callback will not run until the
  1327. * device has been unlocked completely in this way. If your application fails
  1328. * to unlock the device appropriately, your callback will never run, you might
  1329. * hear repeating bursts of audio, and SDL_CloseAudioDevice() will probably
  1330. * deadlock.
  1331. *
  1332. * Internally, the audio device lock is a mutex; if you lock from two threads
  1333. * at once, not only will you block the audio callback, you'll block the other
  1334. * thread.
  1335. *
  1336. * \param dev the ID of the device to be locked
  1337. *
  1338. * \since This function is available since SDL 2.0.0.
  1339. *
  1340. * \sa SDL_UnlockAudioDevice
  1341. */
  1342. extern DECLSPEC void SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
  1343. /**
  1344. * This function is a legacy means of unlocking the audio device.
  1345. *
  1346. * New programs might want to use SDL_UnlockAudioDevice() instead. This
  1347. * function is equivalent to calling...
  1348. *
  1349. * ```c
  1350. * SDL_UnlockAudioDevice(1);
  1351. * ```
  1352. *
  1353. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  1354. *
  1355. * \since This function is available since SDL 2.0.0.
  1356. *
  1357. * \sa SDL_LockAudio
  1358. * \sa SDL_UnlockAudioDevice
  1359. */
  1360. extern DECLSPEC void SDLCALL SDL_UnlockAudio(void);
  1361. /**
  1362. * Use this function to unlock the audio callback function for a specified
  1363. * device.
  1364. *
  1365. * This function should be paired with a previous SDL_LockAudioDevice() call.
  1366. *
  1367. * \param dev the ID of the device to be unlocked
  1368. *
  1369. * \since This function is available since SDL 2.0.0.
  1370. *
  1371. * \sa SDL_LockAudioDevice
  1372. */
  1373. extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
  1374. /* @} *//* Audio lock functions */
  1375. /**
  1376. * This function is a legacy means of closing the audio device.
  1377. *
  1378. * This function is equivalent to calling...
  1379. *
  1380. * ```c
  1381. * SDL_CloseAudioDevice(1);
  1382. * ```
  1383. *
  1384. * ...and is only useful if you used the legacy SDL_OpenAudio() function.
  1385. *
  1386. * \since This function is available since SDL 2.0.0.
  1387. *
  1388. * \sa SDL_OpenAudio
  1389. */
  1390. extern DECLSPEC void SDLCALL SDL_CloseAudio(void);
  1391. /**
  1392. * Use this function to shut down audio processing and close the audio device.
  1393. *
  1394. * The application should close open audio devices once they are no longer
  1395. * needed. Calling this function will wait until the device's audio callback
  1396. * is not running, release the audio hardware and then clean up internal
  1397. * state. No further audio will play from this device once this function
  1398. * returns.
  1399. *
  1400. * This function may block briefly while pending audio data is played by the
  1401. * hardware, so that applications don't drop the last buffer of data they
  1402. * supplied.
  1403. *
  1404. * The device ID is invalid as soon as the device is closed, and is eligible
  1405. * for reuse in a new SDL_OpenAudioDevice() call immediately.
  1406. *
  1407. * \param dev an audio device previously opened with SDL_OpenAudioDevice()
  1408. *
  1409. * \since This function is available since SDL 2.0.0.
  1410. *
  1411. * \sa SDL_OpenAudioDevice
  1412. */
  1413. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
  1414. /* Ends C function definitions when using C++ */
  1415. #ifdef __cplusplus
  1416. }
  1417. #endif
  1418. #include "close_code.h"
  1419. #endif /* SDL_audio_h_ */
  1420. /* vi: set ts=4 sw=4 expandtab: */