SDL_audio.h 47 KB

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