SDL_audio.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. #ifdef __GNUC__
  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. vvv
  198. The next time we rev the ABI, make sure to size the ints and add padding.
  199. */
  200. #define SDL_AUDIOCVT_PACKED __attribute__((packed))
  201. #else
  202. #define SDL_AUDIOCVT_PACKED
  203. #endif
  204. /* */
  205. typedef struct SDL_AudioCVT
  206. {
  207. int needed; /**< Set to 1 if conversion possible */
  208. SDL_AudioFormat src_format; /**< Source audio format */
  209. SDL_AudioFormat dst_format; /**< Target audio format */
  210. double rate_incr; /**< Rate conversion increment */
  211. Uint8 *buf; /**< Buffer to hold entire audio data */
  212. int len; /**< Length of original audio buffer */
  213. int len_cvt; /**< Length of converted audio buffer */
  214. int len_mult; /**< buffer must be len*len_mult big */
  215. double len_ratio; /**< Given len, final size is len*len_ratio */
  216. SDL_AudioFilter filters[SDL_AUDIOCVT_MAX_FILTERS + 1]; /**< NULL-terminated list of filter functions */
  217. int filter_index; /**< Current audio conversion function */
  218. } SDL_AUDIOCVT_PACKED SDL_AudioCVT;
  219. /* Function prototypes */
  220. /**
  221. * \name Driver discovery functions
  222. *
  223. * These functions return the list of built in audio drivers, in the
  224. * order that they are normally initialized by default.
  225. */
  226. /* @{ */
  227. extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);
  228. extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);
  229. /* @} */
  230. /**
  231. * \name Initialization and cleanup
  232. *
  233. * \internal These functions are used internally, and should not be used unless
  234. * you have a specific need to specify the audio driver you want to
  235. * use. You should normally use SDL_Init() or SDL_InitSubSystem().
  236. */
  237. /* @{ */
  238. extern DECLSPEC int SDLCALL SDL_AudioInit(const char *driver_name);
  239. extern DECLSPEC void SDLCALL SDL_AudioQuit(void);
  240. /* @} */
  241. /**
  242. * Get the name of the current audio driver.
  243. *
  244. * The returned string points to internal static memory and thus never becomes
  245. * invalid, even if you quit the audio subsystem and initialize a new driver
  246. * (although such a case would return a different static string from another
  247. * call to this function, of course). As such, you should not modify or free
  248. * the returned string.
  249. *
  250. * \returns the name of the current audio driver or NULL if no driver has been
  251. * initialized.
  252. *
  253. * \since This function is available since SDL 2.0.0.
  254. *
  255. * \sa SDL_AudioInit
  256. */
  257. extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);
  258. /**
  259. * This function is a legacy means of opening the audio device.
  260. *
  261. * This function remains for compatibility with SDL 1.2, but also because it's
  262. * slightly easier to use than the new functions in SDL 2.0. The new, more
  263. * powerful, and preferred way to do this is SDL_OpenAudioDevice().
  264. *
  265. * This function is roughly equivalent to:
  266. *
  267. * ```c++
  268. * SDL_OpenAudioDevice(NULL, 0, desired, obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
  269. * ```
  270. *
  271. * With two notable exceptions:
  272. *
  273. * - If `obtained` is NULL, we use `desired` (and allow no changes), which
  274. * means desired will be modified to have the correct values for silence,
  275. * etc, and SDL will convert any differences between your app's specific
  276. * request and the hardware behind the scenes.
  277. * - The return value is always success or failure, and not a device ID, which
  278. * means you can only have one device open at a time with this function.
  279. *
  280. * \param desired an SDL_AudioSpec structure representing the desired output
  281. * format. Please refer to the SDL_OpenAudioDevice documentation
  282. * for details on how to prepare this structure.
  283. * \param obtained an SDL_AudioSpec structure filled in with the actual
  284. * parameters, or NULL.
  285. * \returns 0 if successful, placing the actual hardware parameters in the
  286. * structure pointed to by `obtained`.
  287. *
  288. * If `obtained` is NULL, the audio data passed to the callback
  289. * function will be guaranteed to be in the requested format, and
  290. * will be automatically converted to the actual hardware audio
  291. * format if necessary. If `obtained` is NULL, `desired` will
  292. * have fields modified.
  293. *
  294. * This function returns a negative error code on failure to open the
  295. * audio device or failure to set up the audio thread; call
  296. * SDL_GetError() for more information.
  297. *
  298. * \sa SDL_CloseAudio
  299. * \sa SDL_LockAudio
  300. * \sa SDL_PauseAudio
  301. * \sa SDL_UnlockAudio
  302. */
  303. extern DECLSPEC int SDLCALL SDL_OpenAudio(SDL_AudioSpec * desired,
  304. SDL_AudioSpec * obtained);
  305. /**
  306. * SDL Audio Device IDs.
  307. *
  308. * A successful call to SDL_OpenAudio() is always device id 1, and legacy
  309. * SDL audio APIs assume you want this device ID. SDL_OpenAudioDevice() calls
  310. * always returns devices >= 2 on success. The legacy calls are good both
  311. * for backwards compatibility and when you don't care about multiple,
  312. * specific, or capture devices.
  313. */
  314. typedef Uint32 SDL_AudioDeviceID;
  315. /**
  316. * Get the number of built-in audio devices.
  317. *
  318. * This function is only valid after successfully initializing the audio
  319. * subsystem.
  320. *
  321. * Note that audio capture support is not implemented as of SDL 2.0.4, so the
  322. * `iscapture` parameter is for future expansion and should always be zero
  323. * for now.
  324. *
  325. * This function will return -1 if an explicit list of devices can't be
  326. * determined. Returning -1 is not an error. For example, if SDL is set up to
  327. * talk to a remote audio server, it can't list every one available on the
  328. * Internet, but it will still allow a specific host to be specified in
  329. * SDL_OpenAudioDevice().
  330. *
  331. * In many common cases, when this function returns a value <= 0, it can still
  332. * successfully open the default device (NULL for first argument of
  333. * SDL_OpenAudioDevice()).
  334. *
  335. * This function may trigger a complete redetect of available hardware. It
  336. * should not be called for each iteration of a loop, but rather once at the
  337. * start of a loop:
  338. *
  339. * ```c++
  340. * // Don't do this:
  341. * for (int i = 0; i < SDL_GetNumAudioDevices(0); i++)
  342. *
  343. * // do this instead:
  344. * const int count = SDL_GetNumAudioDevices(0);
  345. * for (int i = 0; i < count; ++i) { do_something_here(); }
  346. * ```
  347. *
  348. * \param iscapture zero to request playback devices, non-zero to request
  349. * recording devices
  350. * \returns the number of available devices exposed by the current driver or
  351. * -1 if an explicit list of devices can't be determined. A return
  352. * value of -1 does not necessarily mean an error condition.
  353. *
  354. * \since This function is available since SDL 2.0.0.
  355. *
  356. * \sa SDL_GetAudioDeviceName
  357. * \sa SDL_OpenAudioDevice
  358. */
  359. extern DECLSPEC int SDLCALL SDL_GetNumAudioDevices(int iscapture);
  360. /**
  361. * Get the human-readable name of a specific audio device.
  362. *
  363. * This function is only valid after successfully initializing the audio
  364. * subsystem. The values returned by this function reflect the latest call to
  365. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  366. * hardware.
  367. *
  368. * The string returned by this function is UTF-8 encoded, read-only, and
  369. * managed internally. You are not to free it. If you need to keep the string
  370. * for any length of time, you should make your own copy of it, as it will be
  371. * invalid next time any of several other SDL functions are called.
  372. *
  373. * \param index the index of the audio device; valid values range from 0 to
  374. * SDL_GetNumAudioDevices() - 1
  375. * \param iscapture non-zero to query the list of recording devices, zero to
  376. * query the list of output devices.
  377. * \returns the name of the audio device at the requested index, or NULL on
  378. * error.
  379. *
  380. * \sa SDL_GetNumAudioDevices
  381. */
  382. extern DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(int index,
  383. int iscapture);
  384. /**
  385. * Get the preferred audio format of a specific audio device.
  386. *
  387. * This function is only valid after a successfully initializing the audio
  388. * subsystem. The values returned by this function reflect the latest call to
  389. * SDL_GetNumAudioDevices(); re-call that function to redetect available
  390. * hardware.
  391. *
  392. * `spec` will be filled with the sample rate, sample format, and channel
  393. * count. All other values in the structure are filled with 0. When the
  394. * supported struct members are 0, SDL was unable to get the property from the
  395. * backend.
  396. *
  397. * \param index the index of the audio device; valid values range from 0 to
  398. * SDL_GetNumAudioDevices() - 1
  399. * \param iscapture non-zero to query the list of recording devices, zero to
  400. * query the list of output devices.
  401. * \param spec The SDL_AudioSpec to be initialized by this function.
  402. * \returns 0 on success, nonzero on error
  403. *
  404. * \sa SDL_GetNumAudioDevices
  405. */
  406. extern DECLSPEC int SDLCALL SDL_GetAudioDeviceSpec(int index,
  407. int iscapture,
  408. SDL_AudioSpec *spec);
  409. /**
  410. * Open a specific audio device.
  411. *
  412. * SDL_OpenAudio(), unlike this function, always acts on device ID 1. As such,
  413. * this function will never return a 1 so as not to conflict with the legacy
  414. * function.
  415. *
  416. * Please note that SDL 2.0 before 2.0.5 did not support recording; as such,
  417. * this function would fail if `iscapture` was not zero. Starting with SDL
  418. * 2.0.5, recording is implemented and this value can be non-zero.
  419. *
  420. * Passing in a `device` name of NULL requests the most reasonable default
  421. * (and is equivalent to what SDL_OpenAudio() does to choose a device). The
  422. * `device` name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but
  423. * some drivers allow arbitrary and driver-specific strings, such as a
  424. * hostname/IP address for a remote audio server, or a filename in the
  425. * diskaudio driver.
  426. *
  427. * When filling in the desired audio spec structure:
  428. *
  429. * - `desired->freq` should be the frequency in sample-frames-per-second (Hz).
  430. * - `desired->format` should be the audio format (`AUDIO_S16SYS`, etc).
  431. * - `desired->samples` is the desired size of the audio buffer, in
  432. * _sample frames_ (with stereo output, two samples--left and right--would
  433. * make a single sample frame). This number should be a power of two, and
  434. * may be adjusted by the audio driver to a value more suitable for the
  435. * hardware. Good values seem to range between 512 and 8096 inclusive,
  436. * depending on the application and CPU speed. Smaller values reduce
  437. * latency, but can lead to underflow if the application is doing heavy
  438. * processing and cannot fill the audio buffer in time. Note that the
  439. * number of sample frames is directly related to time by the following
  440. * formula: `ms = (sampleframes*1000)/freq`
  441. * - `desired->size` is the size in _bytes_ of the audio buffer, and is
  442. * calculated by SDL_OpenAudioDevice(). You don't initialize this.
  443. * - `desired->silence` is the value used to set the buffer to silence,
  444. * and is calculated by SDL_OpenAudioDevice(). You don't initialize this.
  445. * - `desired->callback` should be set to a function that will be called
  446. * when the audio device is ready for more data. It is passed a pointer
  447. * to the audio buffer, and the length in bytes of the audio buffer.
  448. * This function usually runs in a separate thread, and so you should
  449. * protect data structures that it accesses by calling SDL_LockAudioDevice()
  450. * and SDL_UnlockAudioDevice() in your code. Alternately, you may pass a NULL
  451. * pointer here, and call SDL_QueueAudio() with some frequency, to queue
  452. * more audio samples to be played (or for capture devices, call
  453. * SDL_DequeueAudio() with some frequency, to obtain audio samples).
  454. * - `desired->userdata` is passed as the first parameter to your callback
  455. * function. If you passed a NULL callback, this value is ignored.
  456. *
  457. * `allowed_changes` can have the following flags OR'd together:
  458. *
  459. * - `SDL_AUDIO_ALLOW_FREQUENCY_CHANGE`
  460. * - `SDL_AUDIO_ALLOW_FORMAT_CHANGE`
  461. * - `SDL_AUDIO_ALLOW_CHANNELS_CHANGE`
  462. * - `SDL_AUDIO_ALLOW_ANY_CHANGE`
  463. *
  464. * These flags specify how SDL should behave when a device cannot offer a
  465. * specific feature. If the application requests a feature that the hardware
  466. * doesn't offer, SDL will always try to get the closest equivalent.
  467. *
  468. * For example, if you ask for float32 audio format, but the sound card only
  469. * supports int16, SDL will set the hardware to int16. If you had set
  470. * SDL_AUDIO_ALLOW_FORMAT_CHANGE, SDL will change the format in the
  471. * `obtained` structure. If that flag was *not* set, SDL will prepare to
  472. * convert your callback's float32 audio to int16 before feeding it to the
  473. * hardware and will keep the originally requested format in the `obtained`
  474. * structure.
  475. *
  476. * If your application can only handle one specific data format, pass a zero
  477. * for `allowed_changes` and let SDL transparently handle any differences.
  478. *
  479. * An opened audio device starts out paused, and should be enabled for playing
  480. * by calling SDL_PauseAudioDevice(devid, 0) when you are ready for your audio
  481. * callback function to be called. Since the audio driver may modify the
  482. * requested size of the audio buffer, you should allocate any local mixing
  483. * buffers after you open the audio device.
  484. *
  485. * The audio callback runs in a separate thread in most cases; you can prevent
  486. * race conditions between your callback and other threads without fully
  487. * pausing playback with SDL_LockAudioDevice(). For more information about the
  488. * callback, see SDL_AudioSpec.
  489. *
  490. * \param device a UTF-8 string reported by SDL_GetAudioDeviceName() or a
  491. * driver-specific name as appropriate. NULL requests the most
  492. * reasonable default device.
  493. * \param iscapture non-zero to specify a device should be opened for
  494. * recording, not playback
  495. * \param desired an SDL_AudioSpec structure representing the desired output
  496. * format; see SDL_OpenAudio() for more information
  497. * \param obtained an SDL_AudioSpec structure filled in with the actual output
  498. * format; see SDL_OpenAudio() for more information
  499. * \param allowed_changes 0, or one or more flags OR'd together
  500. * \returns a valid device ID that is > 0 on success or 0 on failure; call
  501. * SDL_GetError() for more information.
  502. *
  503. * For compatibility with SDL 1.2, this will never return 1, since
  504. * SDL reserves that ID for the legacy SDL_OpenAudio() function.
  505. *
  506. * \since This function is available since SDL 2.0.0.
  507. *
  508. * \sa SDL_CloseAudioDevice
  509. * \sa SDL_GetAudioDeviceName
  510. * \sa SDL_LockAudioDevice
  511. * \sa SDL_OpenAudio
  512. * \sa SDL_PauseAudioDevice
  513. * \sa SDL_UnlockAudioDevice
  514. */
  515. extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(
  516. const char *device,
  517. int iscapture,
  518. const SDL_AudioSpec *desired,
  519. SDL_AudioSpec *obtained,
  520. int allowed_changes);
  521. /**
  522. * \name Audio state
  523. *
  524. * Get the current audio state.
  525. */
  526. /* @{ */
  527. typedef enum
  528. {
  529. SDL_AUDIO_STOPPED = 0,
  530. SDL_AUDIO_PLAYING,
  531. SDL_AUDIO_PAUSED
  532. } SDL_AudioStatus;
  533. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioStatus(void);
  534. extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev);
  535. /* @} *//* Audio State */
  536. /**
  537. * \name Pause audio functions
  538. *
  539. * These functions pause and unpause the audio callback processing.
  540. * They should be called with a parameter of 0 after opening the audio
  541. * device to start playing sound. This is so you can safely initialize
  542. * data for your callback function after opening the audio device.
  543. * Silence will be written to the audio device during the pause.
  544. */
  545. /* @{ */
  546. extern DECLSPEC void SDLCALL SDL_PauseAudio(int pause_on);
  547. extern DECLSPEC void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev,
  548. int pause_on);
  549. /* @} *//* Pause audio functions */
  550. /**
  551. * Load the audio data of a WAVE file into memory.
  552. *
  553. * Loading a WAVE file requires `src`, `spec`, `audio_buf` and `audio_len`
  554. * to be valid pointers. The entire data portion of the file is then loaded
  555. * into memory and decoded if necessary.
  556. *
  557. * If `freesrc` is non-zero, the data source gets automatically closed and
  558. * freed before the function returns.
  559. *
  560. * Supported formats are RIFF WAVE files with the formats PCM (8, 16, 24, and
  561. * 32 bits), IEEE Float (32 bits), Microsoft ADPCM and IMA ADPCM (4 bits),
  562. * and A-law and mu-law (8 bits). Other formats are currently unsupported and
  563. * cause an error.
  564. *
  565. * If this function succeeds, the pointer returned by it is equal to `spec`
  566. * and the pointer to the audio data allocated by the function is written to
  567. * `audio_buf` and its length in bytes to `audio_len`. The SDL_AudioSpec
  568. * members `freq`, `channels`, and `format` are set to the values of the
  569. * audio data in the buffer. The `samples` member is set to a sane default
  570. * and all others are set to zero.
  571. *
  572. * It's necessary to use SDL_FreeWAV() to free the audio data returned in
  573. * `audio_buf` when it is no longer used.
  574. *
  575. * Because of the underspecification of the .WAV format, there are many
  576. * problematic files in the wild that cause issues with strict decoders. To
  577. * provide compatibility with these files, this decoder is lenient in regards
  578. * to the truncation of the file, the fact chunk, and the size of the RIFF
  579. * chunk. The hints `SDL_HINT_WAVE_RIFF_CHUNK_SIZE`, `SDL_HINT_WAVE_TRUNCATION`,
  580. * and `SDL_HINT_WAVE_FACT_CHUNK` can be used to tune the behavior of the
  581. * loading process.
  582. *
  583. * Any file that is invalid (due to truncation, corruption, or wrong values in
  584. * the headers), too big, or unsupported causes an error. Additionally, any
  585. * critical I/O error from the data source will terminate the loading process
  586. * with an error. The function returns NULL on error and in all cases (with the
  587. * exception of `src` being NULL), an appropriate error message will be set.
  588. *
  589. * It is required that the data source supports seeking.
  590. *
  591. * Example:
  592. * ```c++
  593. * SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, &spec, &buf, &len);
  594. * ```
  595. *
  596. * Note that the SDL_LoadWAV macro does this same thing for you, but in a less
  597. * messy way:
  598. *
  599. * ```c++
  600. * SDL_LoadWAV("sample.wav", &spec, &buf, &len);
  601. * ```
  602. *
  603. * \param src The data source for the WAVE data
  604. * \param freesrc If non-zero, SDL will _always_ free the data source
  605. * \param spec An SDL_AudioSpec that will be filled in with the wave file's
  606. * format details
  607. * \param audio_buf A pointer filled with the audio data, allocated by the function.
  608. * \param audio_len A pointer filled with the length of the audio data buffer in bytes
  609. * \returns This function, if successfully called, returns `spec`, which will
  610. * be filled with the audio data format of the wave source data.
  611. * `audio_buf` will be filled with a pointer to an allocated buffer
  612. * containing the audio data, and `audio_len` is filled with the
  613. * length of that audio buffer in bytes.
  614. *
  615. * This function returns NULL if the .WAV file cannot be opened, uses
  616. * an unknown data format, or is corrupt; call SDL_GetError() for
  617. * more information.
  618. *
  619. * When the application is done with the data returned in
  620. * `audio_buf`, it should call SDL_FreeWAV() to dispose of it.
  621. *
  622. * \sa SDL_FreeWAV
  623. * \sa SDL_LoadWAV
  624. */
  625. extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,
  626. int freesrc,
  627. SDL_AudioSpec * spec,
  628. Uint8 ** audio_buf,
  629. Uint32 * audio_len);
  630. /**
  631. * Loads a WAV from a file.
  632. * Compatibility convenience function.
  633. */
  634. #define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
  635. SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
  636. /**
  637. * Free data previously allocated with SDL_LoadWAV() or SDL_LoadWAV_RW().
  638. *
  639. * After a WAVE file has been opened with SDL_LoadWAV() or SDL_LoadWAV_RW()
  640. * its data can eventually be freed with SDL_FreeWAV(). It is safe to call
  641. * this function with a NULL pointer.
  642. *
  643. * \param audio_buf a pointer to the buffer created by SDL_LoadWAV() or
  644. * SDL_LoadWAV_RW()
  645. *
  646. * \sa SDL_LoadWAV
  647. * \sa SDL_LoadWAV_RW
  648. */
  649. extern DECLSPEC void SDLCALL SDL_FreeWAV(Uint8 * audio_buf);
  650. /**
  651. * Initialize an SDL_AudioCVT structure for conversion.
  652. *
  653. * Before an SDL_AudioCVT structure can be used to convert audio data it must
  654. * be initialized with source and destination information.
  655. *
  656. * This function will zero out every field of the SDL_AudioCVT, so it must be
  657. * called before the application fills in the final buffer information.
  658. *
  659. * Once this function has returned successfully, and reported that a
  660. * conversion is necessary, the application fills in the rest of the fields in
  661. * SDL_AudioCVT, now that it knows how large a buffer it needs to allocate,
  662. * and then can call SDL_ConvertAudio() to complete the conversion.
  663. *
  664. * \param cvt an SDL_AudioCVT structure filled in with audio conversion
  665. * information
  666. * \param src_format the source format of the audio data; for more info see
  667. * SDL_AudioFormat
  668. * \param src_channels the number of channels in the source
  669. * \param src_rate the frequency (sample-frames-per-second) of the source
  670. * \param dst_format the destination format of the audio data; for more info
  671. * see SDL_AudioFormat
  672. * \param dst_channels the number of channels in the destination
  673. * \param dst_rate the frequency (sample-frames-per-second) of the
  674. * destination
  675. * \returns 1 if the audio filter is prepared, 0 if no conversion is needed,
  676. * or a negative error code on failure; call SDL_GetError() for more
  677. * information.
  678. *
  679. * \sa SDL_ConvertAudio
  680. */
  681. extern DECLSPEC int SDLCALL SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
  682. SDL_AudioFormat src_format,
  683. Uint8 src_channels,
  684. int src_rate,
  685. SDL_AudioFormat dst_format,
  686. Uint8 dst_channels,
  687. int dst_rate);
  688. /**
  689. * Convert audio data to a desired audio format.
  690. *
  691. * This function does the actual audio data conversion, after the application
  692. * has called SDL_BuildAudioCVT() to prepare the conversion information and
  693. * then filled in the buffer details.
  694. *
  695. * Once the application has initialized the `cvt` structure using
  696. * SDL_BuildAudioCVT(), allocated an audio buffer and filled it with audio
  697. * data in the source format, this function will convert the buffer, in-place,
  698. * to the desired format.
  699. *
  700. * The data conversion may go through several passes; any given pass may
  701. * possibly temporarily increase the size of the data. For example, SDL might
  702. * expand 16-bit data to 32 bits before resampling to a lower frequency,
  703. * shrinking the data size after having grown it briefly. Since the supplied
  704. * buffer will be both the source and destination, converting as necessary
  705. * in-place, the application must allocate a buffer that will fully contain
  706. * the data during its largest conversion pass. After SDL_BuildAudioCVT()
  707. * returns, the application should set the `cvt->len` field to the size, in
  708. * bytes, of the source data, and allocate a buffer that is
  709. * `cvt->len * cvt->len_mult` bytes long for the `buf` field.
  710. *
  711. * The source data should be copied into this buffer before the call to
  712. * SDL_ConvertAudio(). Upon successful return, this buffer will contain the
  713. * converted audio, and `cvt->len_cvt` will be the size of the converted data,
  714. * in bytes. Any bytes in the buffer past `cvt->len_cvt` are undefined once
  715. * this function returns.
  716. *
  717. * \param cvt an SDL_AudioCVT structure that was previously set up by
  718. * SDL_BuildAudioCVT().
  719. * \returns 0 if the conversion was completed successfully or a negative error
  720. * code on failure; call SDL_GetError() for more information.
  721. *
  722. * \sa SDL_BuildAudioCVT
  723. */
  724. extern DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT * cvt);
  725. /* SDL_AudioStream is a new audio conversion interface.
  726. The benefits vs SDL_AudioCVT:
  727. - it can handle resampling data in chunks without generating
  728. artifacts, when it doesn't have the complete buffer available.
  729. - it can handle incoming data in any variable size.
  730. - You push data as you have it, and pull it when you need it
  731. */
  732. /* this is opaque to the outside world. */
  733. struct _SDL_AudioStream;
  734. typedef struct _SDL_AudioStream SDL_AudioStream;
  735. /**
  736. * Create a new audio stream.
  737. *
  738. * \param src_format The format of the source audio
  739. * \param src_channels The number of channels of the source audio
  740. * \param src_rate The sampling rate of the source audio
  741. * \param dst_format The format of the desired audio output
  742. * \param dst_channels The number of channels of the desired audio output
  743. * \param dst_rate The sampling rate of the desired audio output
  744. * \returns 0 on success, or -1 on error.
  745. *
  746. * \sa SDL_AudioStreamPut
  747. * \sa SDL_AudioStreamGet
  748. * \sa SDL_AudioStreamAvailable
  749. * \sa SDL_AudioStreamFlush
  750. * \sa SDL_AudioStreamClear
  751. * \sa SDL_FreeAudioStream
  752. */
  753. extern DECLSPEC SDL_AudioStream * SDLCALL SDL_NewAudioStream(const SDL_AudioFormat src_format,
  754. const Uint8 src_channels,
  755. const int src_rate,
  756. const SDL_AudioFormat dst_format,
  757. const Uint8 dst_channels,
  758. const int dst_rate);
  759. /**
  760. * Add data to be converted/resampled to the stream.
  761. *
  762. * \param stream The stream the audio data is being added to
  763. * \param buf A pointer to the audio data to add
  764. * \param len The number of bytes to write to the stream
  765. * \returns 0 on success, or -1 on error.
  766. *
  767. * \sa SDL_NewAudioStream
  768. * \sa SDL_AudioStreamGet
  769. * \sa SDL_AudioStreamAvailable
  770. * \sa SDL_AudioStreamFlush
  771. * \sa SDL_AudioStreamClear
  772. * \sa SDL_FreeAudioStream
  773. */
  774. extern DECLSPEC int SDLCALL SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len);
  775. /**
  776. * Get converted/resampled data from the stream
  777. *
  778. * \param stream The stream the audio is being requested from
  779. * \param buf A buffer to fill with audio data
  780. * \param len The maximum number of bytes to fill
  781. * \returns the number of bytes read from the stream, or -1 on error
  782. *
  783. * \sa SDL_NewAudioStream
  784. * \sa SDL_AudioStreamPut
  785. * \sa SDL_AudioStreamAvailable
  786. * \sa SDL_AudioStreamFlush
  787. * \sa SDL_AudioStreamClear
  788. * \sa SDL_FreeAudioStream
  789. */
  790. extern DECLSPEC int SDLCALL SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len);
  791. /**
  792. * Get the number of converted/resampled bytes available. The stream may be
  793. * buffering data behind the scenes until it has enough to resample
  794. * correctly, so this number might be lower than what you expect, or even
  795. * be zero. Add more data or flush the stream if you need the data now.
  796. *
  797. * \sa SDL_NewAudioStream
  798. * \sa SDL_AudioStreamPut
  799. * \sa SDL_AudioStreamGet
  800. * \sa SDL_AudioStreamFlush
  801. * \sa SDL_AudioStreamClear
  802. * \sa SDL_FreeAudioStream
  803. */
  804. extern DECLSPEC int SDLCALL SDL_AudioStreamAvailable(SDL_AudioStream *stream);
  805. /**
  806. * Tell the stream that you're done sending data, and anything being buffered
  807. * should be converted/resampled and made available immediately.
  808. *
  809. * It is legal to add more data to a stream after flushing, but there will
  810. * be audio gaps in the output. Generally this is intended to signal the
  811. * end of input, so the complete output becomes available.
  812. *
  813. * \sa SDL_NewAudioStream
  814. * \sa SDL_AudioStreamPut
  815. * \sa SDL_AudioStreamGet
  816. * \sa SDL_AudioStreamAvailable
  817. * \sa SDL_AudioStreamClear
  818. * \sa SDL_FreeAudioStream
  819. */
  820. extern DECLSPEC int SDLCALL SDL_AudioStreamFlush(SDL_AudioStream *stream);
  821. /**
  822. * Clear any pending data in the stream without converting it
  823. *
  824. * \sa SDL_NewAudioStream
  825. * \sa SDL_AudioStreamPut
  826. * \sa SDL_AudioStreamGet
  827. * \sa SDL_AudioStreamAvailable
  828. * \sa SDL_AudioStreamFlush
  829. * \sa SDL_FreeAudioStream
  830. */
  831. extern DECLSPEC void SDLCALL SDL_AudioStreamClear(SDL_AudioStream *stream);
  832. /**
  833. * Free an audio stream
  834. *
  835. * \sa SDL_NewAudioStream
  836. * \sa SDL_AudioStreamPut
  837. * \sa SDL_AudioStreamGet
  838. * \sa SDL_AudioStreamAvailable
  839. * \sa SDL_AudioStreamFlush
  840. * \sa SDL_AudioStreamClear
  841. */
  842. extern DECLSPEC void SDLCALL SDL_FreeAudioStream(SDL_AudioStream *stream);
  843. #define SDL_MIX_MAXVOLUME 128
  844. /**
  845. * This function is a legacy means of mixing audio.
  846. *
  847. * This function is equivalent to calling
  848. *
  849. * ```c++
  850. * SDL_MixAudioFormat(dst, src, format, len, volume);
  851. * ```
  852. *
  853. * where `format` is the obtained format of the audio device from the legacy
  854. * SDL_OpenAudio() function.
  855. *
  856. * \param dst the destination for the mixed audio
  857. * \param src the source audio buffer to be mixed
  858. * \param len the length of the audio buffer in bytes
  859. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  860. * for full audio volume
  861. *
  862. * \sa SDL_MixAudioFormat
  863. */
  864. extern DECLSPEC void SDLCALL SDL_MixAudio(Uint8 * dst, const Uint8 * src,
  865. Uint32 len, int volume);
  866. /**
  867. * Mix audio data in a specified format.
  868. *
  869. * This takes an audio buffer `src` of `len` bytes of `format` data and
  870. * mixes it into `dst`, performing addition, volume adjustment, and overflow
  871. * clipping. The buffer pointed to by `dst` must also be `len` bytes of
  872. * `format` data.
  873. *
  874. * This is provided for convenience -- you can mix your own audio data.
  875. *
  876. * Do not use this function for mixing together more than two streams of
  877. * sample data. The output from repeated application of this function may be
  878. * distorted by clipping, because there is no accumulator with greater range
  879. * than the input (not to mention this being an inefficient way of doing it).
  880. *
  881. * It is a common misconception that this function is required to write audio
  882. * data to an output stream in an audio callback. While you can do that,
  883. * SDL_MixAudioFormat() is really only needed when you're mixing a single
  884. * audio stream with a volume adjustment.
  885. *
  886. * \param dst the destination for the mixed audio
  887. * \param src the source audio buffer to be mixed
  888. * \param format the SDL_AudioFormat structure representing the desired audio
  889. * format
  890. * \param len the length of the audio buffer in bytes
  891. * \param volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
  892. * for full audio volume
  893. */
  894. extern DECLSPEC void SDLCALL SDL_MixAudioFormat(Uint8 * dst,
  895. const Uint8 * src,
  896. SDL_AudioFormat format,
  897. Uint32 len, int volume);
  898. /**
  899. * Queue more audio on non-callback devices.
  900. *
  901. * If you are looking to retrieve queued audio from a non-callback capture
  902. * device, you want SDL_DequeueAudio() instead. SDL_QueueAudio() will return
  903. * -1 to signify an error if you use it with capture devices.
  904. *
  905. * SDL offers two ways to feed audio to the device: you can either supply a
  906. * callback that SDL triggers with some frequency to obtain more audio (pull
  907. * method), or you can supply no callback, and then SDL will expect you to
  908. * supply data at regular intervals (push method) with this function.
  909. *
  910. * There are no limits on the amount of data you can queue, short of
  911. * exhaustion of address space. Queued data will drain to the device as
  912. * necessary without further intervention from you. If the device needs audio
  913. * but there is not enough queued, it will play silence to make up the
  914. * difference. This means you will have skips in your audio playback if you
  915. * aren't routinely queueing sufficient data.
  916. *
  917. * This function copies the supplied data, so you are safe to free it when the
  918. * function returns. This function is thread-safe, but queueing to the same
  919. * device from two threads at once does not promise which buffer will be
  920. * queued first.
  921. *
  922. * You may not queue audio on a device that is using an application-supplied
  923. * callback; doing so returns an error. You have to use the audio callback or
  924. * queue audio with this function, but not both.
  925. *
  926. * You should not call SDL_LockAudio() on the device before queueing; SDL
  927. * handles locking internally for this function.
  928. *
  929. * \param dev the device ID to which we will queue audio
  930. * \param data the data to queue to the device for later playback
  931. * \param len the number of bytes (not samples!) to which `data` points
  932. * \returns 0 on success or a negative error code on failure; call
  933. * SDL_GetError() for more information.
  934. *
  935. * \since This function is available since SDL 2.0.4.
  936. *
  937. * \sa SDL_ClearQueuedAudio
  938. * \sa SDL_GetQueuedAudioSize
  939. */
  940. extern DECLSPEC int SDLCALL SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len);
  941. /**
  942. * Dequeue more audio on non-callback devices.
  943. *
  944. * If you are looking to queue audio for output on a non-callback playback
  945. * device, you want SDL_QueueAudio() instead. SDL_DequeueAudio() will always
  946. * return 0 if you use it with playback devices.
  947. *
  948. * SDL offers two ways to retrieve audio from a capture device: you can either
  949. * supply a callback that SDL triggers with some frequency as the device
  950. * records more audio data, (push method), or you can supply no callback, and
  951. * then SDL will expect you to retrieve data at regular intervals (pull
  952. * method) with this function.
  953. *
  954. * There are no limits on the amount of data you can queue, short of
  955. * exhaustion of address space. Data from the device will keep queuing as
  956. * necessary without further intervention from you. This means you will
  957. * eventually run out of memory if you aren't routinely dequeueing data.
  958. *
  959. * Capture devices will not queue data when paused; if you are expecting to
  960. * not need captured audio for some length of time, use SDL_PauseAudioDevice()
  961. * to stop the capture device from queueing more data. This can be useful
  962. * during, say, level loading times. When unpaused, capture devices will start
  963. * queueing data from that point, having flushed any capturable data available
  964. * while paused.
  965. *
  966. * This function is thread-safe, but dequeueing from the same device from two
  967. * threads at once does not promise which thread will dequeue data first.
  968. *
  969. * You may not dequeue audio from a device that is using an
  970. * application-supplied callback; doing so returns an error. You have to use
  971. * the audio callback, or dequeue audio with this function, but not both.
  972. *
  973. * You should not call SDL_LockAudio() on the device before dequeueing; SDL
  974. * handles locking internally for this function.
  975. *
  976. * \param dev the device ID from which we will dequeue audio
  977. * \param data a pointer into where audio data should be copied
  978. * \param len the number of bytes (not samples!) to which (data) points
  979. * \returns number of bytes dequeued, which could be less than requested; call
  980. * SDL_GetError() for more information.
  981. *
  982. * \since This function is available since SDL 2.0.5.
  983. *
  984. * \sa SDL_ClearQueuedAudio
  985. * \sa SDL_GetQueuedAudioSize
  986. */
  987. extern DECLSPEC Uint32 SDLCALL SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len);
  988. /**
  989. * Get the number of bytes of still-queued audio.
  990. *
  991. * For playback devices: this is the number of bytes that have been queued
  992. * for playback with SDL_QueueAudio(), but have not yet been sent to the
  993. * hardware.
  994. *
  995. * Once we've sent it to the hardware, this function can not decide the exact
  996. * byte boundary of what has been played. It's possible that we just gave the
  997. * hardware several kilobytes right before you called this function, but it
  998. * hasn't played any of it yet, or maybe half of it, etc.
  999. *
  1000. * For capture devices, this is the number of bytes that have been captured by
  1001. * the device and are waiting for you to dequeue. This number may grow at any
  1002. * time, so this only informs of the lower-bound of available data.
  1003. *
  1004. * You may not queue or dequeue audio on a device that is using an
  1005. * application-supplied callback; calling this function on such a device
  1006. * always returns 0. You have to use the audio callback or queue audio, but
  1007. * not both.
  1008. *
  1009. * You should not call SDL_LockAudio() on the device before querying; SDL
  1010. * handles locking internally for this function.
  1011. *
  1012. * \param dev the device ID of which we will query queued audio size
  1013. * \returns the number of bytes (not samples!) of queued audio.
  1014. *
  1015. * \since This function is available since SDL 2.0.4.
  1016. *
  1017. * \sa SDL_ClearQueuedAudio
  1018. * \sa SDL_QueueAudio
  1019. * \sa SDL_DequeueAudio
  1020. */
  1021. extern DECLSPEC Uint32 SDLCALL SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev);
  1022. /**
  1023. * Drop any queued audio data waiting to be sent to the hardware.
  1024. *
  1025. * Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For
  1026. * output devices, the hardware will start playing silence if more audio isn't
  1027. * queued. For capture devices, the hardware will start filling the empty
  1028. * queue with new data if the capture device isn't paused.
  1029. *
  1030. * This will not prevent playback of queued audio that's already been sent to
  1031. * the hardware, as we can not undo that, so expect there to be some fraction
  1032. * of a second of audio that might still be heard. This can be useful if you
  1033. * want to, say, drop any pending music or any unprocessed microphone input
  1034. * during a level change in your game.
  1035. *
  1036. * You may not queue or dequeue audio on a device that is using an
  1037. * application-supplied callback; calling this function on such a device
  1038. * always returns 0. You have to use the audio callback or queue audio, but
  1039. * not both.
  1040. *
  1041. * You should not call SDL_LockAudio() on the device before clearing the
  1042. * queue; SDL handles locking internally for this function.
  1043. *
  1044. * This function always succeeds and thus returns void.
  1045. *
  1046. * \param dev the device ID of which to clear the audio queue
  1047. *
  1048. * \since This function is available since SDL 2.0.4.
  1049. *
  1050. * \sa SDL_GetQueuedAudioSize
  1051. * \sa SDL_QueueAudio
  1052. * \sa SDL_DequeueAudio
  1053. */
  1054. extern DECLSPEC void SDLCALL SDL_ClearQueuedAudio(SDL_AudioDeviceID dev);
  1055. /**
  1056. * \name Audio lock functions
  1057. *
  1058. * The lock manipulated by these functions protects the callback function.
  1059. * During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
  1060. * the callback function is not running. Do not call these from the callback
  1061. * function or you will cause deadlock.
  1062. */
  1063. /* @{ */
  1064. extern DECLSPEC void SDLCALL SDL_LockAudio(void);
  1065. extern DECLSPEC void SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);
  1066. extern DECLSPEC void SDLCALL SDL_UnlockAudio(void);
  1067. extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);
  1068. /* @} *//* Audio lock functions */
  1069. /**
  1070. * This function is a legacy means of closing the audio device.
  1071. *
  1072. * This function is equivalent to calling
  1073. *
  1074. * ```c++
  1075. * SDL_CloseAudioDevice(1);
  1076. * ```
  1077. *
  1078. * and is only useful if you used the legacy SDL_OpenAudio() function.
  1079. *
  1080. * \sa SDL_OpenAudio
  1081. */
  1082. extern DECLSPEC void SDLCALL SDL_CloseAudio(void);
  1083. extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);
  1084. /* Ends C function definitions when using C++ */
  1085. #ifdef __cplusplus
  1086. }
  1087. #endif
  1088. #include "close_code.h"
  1089. #endif /* SDL_audio_h_ */
  1090. /* vi: set ts=4 sw=4 expandtab: */