actual version
[sdl-headers.git] / sdlaudio.inc
1 //from sdl_audio.h
2  {**
3    *   Audio format flags.
4    *
5    *  These are what the 16 bits in SDL_AudioFormat currently mean...
6    *  (Unspecified bits are always zero).
7    *
8    *
9       ++-----------------------sample is signed if set
10       ||
11       ||       ++-----------sample is bigendian if set
12       ||       ||
13       ||       ||          ++---sample is float if set
14       ||       ||          ||
15       ||       ||          || +---sample bit size---+
16       ||       ||          || |                     |
17       15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
18    *
19    *  There are macros in SDL 2.0 and later to query these bits.
20    *}
21 type
22   TSDL_AudioFormat = UInt16;
23
24   {**
25    *   Audio flags
26    *}
27 const
28   SDL_AUDIO_MASK_BITSIZE      = ($FF);
29   SDL_AUDIO_MASK_DATATYPE     = (1 shl 8);
30   SDL_AUDIO_MASK_ENDIAN       = (1 shl 12);
31   SDL_AUDIO_MASK_SIGNED       = (1 shl 15);
32
33 function SDL_AUDIO_BITSIZE(x: Cardinal): Cardinal;
34 function SDL_AUDIO_ISFLOAT(x: Cardinal): Cardinal;
35 function SDL_AUDIO_ISBIGENDIAN(x: Cardinal): Cardinal;
36 function SDL_AUDIO_ISSIGNED(x: Cardinal): Cardinal;
37 function SDL_AUDIO_ISINT(x: Cardinal): Cardinal;
38 function SDL_AUDIO_ISLITTLEENDIAN(x: Cardinal): Cardinal;
39 function SDL_AUDIO_ISUNSIGNED(x: Cardinal): Cardinal;
40
41   {**
42    *   Audio format flags
43    *
44    *  Defaults to LSB byte order.
45    *}
46 const
47   AUDIO_U8      = $0008;  {**< Unsigned 8-bit samples *}
48   AUDIO_S8      = $8008;  {**< Signed 8-bit samples *}
49   AUDIO_U16LSB  = $0010;  {**< Unsigned 16-bit samples *}
50   AUDIO_S16LSB  = $8010;  {**< Signed 16-bit samples *}
51   AUDIO_U16MSB  = $1010;  {**< As above, but big-endian byte order *}
52   AUDIO_S16MSB  = $9010;  {**< As above, but big-endian byte order *}
53   AUDIO_U16     = AUDIO_U16LSB;
54   AUDIO_S16     = AUDIO_S16LSB;
55
56   {**
57    *   int32 support
58    *}
59 const
60   AUDIO_S32LSB  = $8020;  {**< 32-bit integer samples *}
61   AUDIO_S32MSB  = $9020;  {**< As above, but big-endian byte order *}
62   AUDIO_S32     = AUDIO_S32LSB;
63
64   {**
65    *   float32 support
66    *}
67 const
68   AUDIO_F32LSB  = $8120;  {**< 32-bit floating point samples *}
69   AUDIO_F32MSB  = $9120;  {**< As above, but big-endian byte order *}
70   AUDIO_F32     = AUDIO_F32LSB;
71
72   {**
73    *   Native audio byte ordering
74    *}
75          {
76 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
77 #define AUDIO_U16SYS    AUDIO_U16LSB
78 #define AUDIO_S16SYS    AUDIO_S16LSB
79 #define AUDIO_S32SYS    AUDIO_S32LSB
80 #define AUDIO_F32SYS    AUDIO_F32LSB
81 #else
82 #define AUDIO_U16SYS    AUDIO_U16MSB
83 #define AUDIO_S16SYS    AUDIO_S16MSB
84 #define AUDIO_S32SYS    AUDIO_S32MSB
85 #define AUDIO_F32SYS    AUDIO_F32MSB
86 #endif}
87
88   {**
89    *   Allow change flags
90    *
91    *  Which audio format changes are allowed when opening a device.
92    *}
93 const
94   SDL_AUDIO_ALLOW_FREQUENCY_CHANGE  = $00000001;
95   SDL_AUDIO_ALLOW_FORMAT_CHANGE     = $00000002;
96   SDL_AUDIO_ALLOW_CHANNELS_CHANGE   = $00000004;
97   SDL_AUDIO_ALLOW_ANY_CHANGE        = (SDL_AUDIO_ALLOW_FREQUENCY_CHANGE or
98                                        SDL_AUDIO_ALLOW_FORMAT_CHANGE or
99                                                                                              SDL_AUDIO_ALLOW_CHANNELS_CHANGE);
100
101   {*Audio flags*}
102
103   {**
104    *  This function is called when the audio device needs more data.
105    *
106    *   userdata An application-specific parameter saved in
107    *                  the SDL_AudioSpec structure
108    *   stream A pointer to the audio data buffer.
109    *   len    The length of that buffer in bytes.
110    *
111    *  Once the callback returns, the buffer will no longer be valid.
112    *  Stereo samples are stored in a LRLRLR ordering.
113    *}
114 type
115   TSDL_AudioCallback = procedure(userdata: Pointer; stream: PUInt8; len: Integer);
116
117   {**
118    *  The calculated values in this structure are calculated by SDL_OpenAudio().
119    *}
120 type
121   PSDL_AudioSpec = ^TSDL_AudioSpec;
122   TSDL_AudioSpec = record
123     freq: Integer;                {**< DSP frequency -- samples per second *}
124     format: TSDL_AudioFormat;     {**< Audio data format *}
125     channels: UInt8;              {**< Number of channels: 1 mono, 2 stereo *}
126     silence: UInt8;               {**< Audio buffer silence value (calculated) *}
127     samples: UInt16;              {**< Audio buffer size in samples (power of 2) *}
128     padding: UInt16;              {**< Necessary for some compile environments *}
129     size: UInt32;                 {**< Audio buffer size in bytes (calculated) *}
130     callback: TSDL_AudioCallback;
131     userdata: Pointer;
132   end;
133
134   PSDL_AudioCVT = ^TSDL_AudioCVT;
135   TSDL_AudioFilter = procedure(cvt: PSDL_AudioCVT; format: TSDL_AudioFormat);
136
137   {**
138    *  A structure to hold a set of audio conversion filters and buffers.
139    *}
140   TSDL_AudioCVT = record
141     needed: Integer;                                      {**< Set to 1 if conversion possible *}
142     src_format: TSDL_AudioFormat;                       {**< Source audio format *}
143     dst_format: TSDL_AudioFormat;                         {**< Target audio format *}
144     rate_incr: Double;                                    {**< Rate conversion increment *}
145     buf: PUInt8;                                      {**< Buffer to hold entire audio data *}
146     len: Integer;                                     {**< Length of original audio buffer *}
147     len_cvt: Integer;                                 {**< Length of converted audio buffer *}
148     len_mult: Integer;                                {**< buffer must be len*len_mult big *}
149     len_ratio: Double;                                    {**< Given len, final size is len*len_ratio *}
150     filters: array[0..9] of TSDL_AudioFilter; {**< Filter list *}
151     filter_index: Integer;                            {**< Current audio conversion function *}
152   end;
153
154
155   {* Function prototypes *}
156
157   {**
158    *   Driver discovery functions
159    *
160    *  These functions return the list of built in audio drivers, in the
161    *  order that they are normally initialized by default.
162    *}
163
164 function SDL_GetNumAudioDrivers: Integer cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetNumAudioDrivers' {$ENDIF} {$ENDIF};
165 function SDL_GetAudioDriver(index: Integer): PAnsiChar cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetAudioDriver' {$ENDIF} {$ENDIF};
166
167   {**
168    *   Initialization and cleanup
169    *
170    *  These functions are used internally, and should not be used unless
171    *  you have a specific need to specify the audio driver you want to
172    *  use.  You should normally use SDL_Init() or SDL_InitSubSystem().
173    *}
174  
175 function SDL_AudioInit(driver_name: PAnsiChar): Integer cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AudioInit' {$ENDIF} {$ENDIF};
176 procedure SDL_AudioQuit cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AudioQuit' {$ENDIF} {$ENDIF};
177
178   {**
179    *  This function returns the name of the current audio driver, or NULL
180    *  if no driver has been initialized.
181    *}
182 function SDL_GetCurrentAudioDriver: PAnsiChar cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetCurrentAudioDriver' {$ENDIF} {$ENDIF};
183
184   {**
185    *  This function opens the audio device with the desired parameters, and
186    *  returns 0 if successful, placing the actual hardware parameters in the
187    *  structure pointed to by obtained.  If obtained is NULL, the audio
188    *  data passed to the callback function will be guaranteed to be in the
189    *  requested format, and will be automatically converted to the hardware
190    *  audio format if necessary.  This function returns -1 if it failed
191    *  to open the audio device, or couldn't set up the audio thread.
192    *
193    *  When filling in the desired audio spec structure,
194    *    - desired->freq should be the desired audio frequency in samples-per-
195    *      second.
196    *    - desired->format should be the desired audio format.
197    *    - desired->samples is the desired size of the audio buffer, in
198    *      samples.  This number should be a power of two, and may be adjusted by
199    *      the audio driver to a value more suitable for the hardware.  Good values
200    *      seem to range between 512 and 8096 inclusive, depending on the
201    *      application and CPU speed.  Smaller values yield faster response time,
202    *      but can lead to underflow if the application is doing heavy processing
203    *      and cannot fill the audio buffer in time.  A stereo sample consists of
204    *      both right and left channels in LR ordering.
205    *      Note that the number of samples is directly related to time by the
206    *      following formula:  ms := (samples*1000)/freq;
207    *    - desired->size is the size in bytes of the audio buffer, and is
208    *      calculated by SDL_OpenAudio().
209    *    - desired->silence is the value used to set the buffer to silence,
210    *      and is calculated by SDL_OpenAudio().
211    *    - desired->callback should be set to a function that will be called
212    *      when the audio device is ready for more data.  It is passed a pointer
213    *      to the audio buffer, and the length in bytes of the audio buffer.
214    *      This function usually runs in a separate thread, and so you should
215    *      protect data structures that it accesses by calling SDL_LockAudio()
216    *      and SDL_UnlockAudio() in your code.
217    *    - desired->userdata is passed as the first parameter to your callback
218    *      function.
219    *
220    *  The audio device starts out playing silence when it's opened, and should
221    *  be enabled for playing by calling SDL_PauseAudio(0) when you are ready
222    *  for your audio callback function to be called.  Since the audio driver
223    *  may modify the requested size of the audio buffer, you should allocate
224    *  any local mixing buffers after you open the audio device.
225    *}
226 function SDL_OpenAudio(desired: PSDL_AudioSpec; obtained: PSDL_AudioSpec): Integer cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_OpenAudio' {$ENDIF} {$ENDIF};
227
228   {**
229    *  SDL Audio Device IDs.
230    *
231    *  A successful call to SDL_OpenAudio() is always device id 1, and legacy
232    *  SDL audio APIs assume you want this device ID. SDL_OpenAudioDevice() calls
233    *  always returns devices >= 2 on success. The legacy calls are good both
234    *  for backwards compatibility and when you don't care about multiple,
235    *  specific, or capture devices.
236    *}
237 type
238   TSDL_AudioDeviceID = UInt32;
239
240   {**
241    *  Get the number of available devices exposed by the current driver.
242    *  Only valid after a successfully initializing the audio subsystem.
243    *  Returns -1 if an explicit list of devices can't be determined; this is
244    *  not an error. For example, if SDL is set up to talk to a remote audio
245    *  server, it can't list every one available on the Internet, but it will
246    *  still allow a specific host to be specified to SDL_OpenAudioDevice().
247    *
248    *  In many common cases, when this function returns a value <= 0, it can still
249    *  successfully open the default device (NULL for first argument of
250    *  SDL_OpenAudioDevice()).
251    *}
252 function SDL_GetNumAudioDevices(iscapture: Integer): Integer cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetNumAudioDevices' {$ENDIF} {$ENDIF};
253
254   {**
255    *  Get the human-readable name of a specific audio device.
256    *  Must be a value between 0 and (number of audio devices-1).
257    *  Only valid after a successfully initializing the audio subsystem.
258    *  The values returned by this function reflect the latest call to
259    *  SDL_GetNumAudioDevices(); recall that function to redetect available
260    *  hardware.
261    *
262    *  The string returned by this function is UTF-8 encoded, read-only, and
263    *  managed internally. You are not to free it. If you need to keep the
264    *  string for any length of time, you should make your own copy of it, as it
265    *  will be invalid next time any of several other SDL functions is called.
266    *}
267 function SDL_GetAudioDeviceName(index: Integer; iscapture: Integer): PAnsiChar cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetAudioDeviceName' {$ENDIF} {$ENDIF};
268
269   {**
270    *  Open a specific audio device. Passing in a device name of NULL requests
271    *  the most reasonable default (and is equivalent to calling SDL_OpenAudio()).
272    *
273    *  The device name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but
274    *  some drivers allow arbitrary and driver-specific strings, such as a
275    *  hostname/IP address for a remote audio server, or a filename in the
276    *  diskaudio driver.
277    *
278    *   0 on error, a valid device ID that is >= 2 on success.
279    *
280    *  SDL_OpenAudio(), unlike this function, always acts on device ID 1.
281    *}
282 function SDL_OpenAudioDevice(device: PAnsiChar;
283                              iscapture: Integer;
284                              desired: PSDL_AudioSpec;
285                              obtained: PSDL_AudioSpec;
286                                                                  allowed_changes: Integer): TSDL_AudioDeviceID cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_OpenAudioDevice' {$ENDIF} {$ENDIF};
287
288   {**
289    *   Audio state
290    *
291    *  Get the current audio state.
292    *}
293
294 type
295   TSDL_AudioStatus = (SDL_AUDIO_STOPPED,SDL_AUDIO_PLAYING,SDL_AUDIO_PAUSED);
296
297 function SDL_GetAudioStatus: TSDL_AudioStatus cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetAudioStatus' {$ENDIF} {$ENDIF};
298
299 function SDL_GetAudioDeviceStatus(dev: TSDL_AudioDeviceID): TSDL_AudioStatus cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetAudioDeviceStatus' {$ENDIF} {$ENDIF};
300   {*Audio State*}
301
302   {**
303    *   Pause audio functions
304    *
305    *  These functions pause and unpause the audio callback processing.
306    *  They should be called with a parameter of 0 after opening the audio
307    *  device to start playing sound.  This is so you can safely initialize
308    *  data for your callback function after opening the audio device.
309    *  Silence will be written to the audio device during the pause.
310    *}
311
312 procedure SDL_PauseAudio(pause_on: Integer) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_PauseAudio' {$ENDIF} {$ENDIF};
313 procedure SDL_PauseAudioDevice(dev: TSDL_AudioDeviceID; pause_on: Integer) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_PauseAudioDevice' {$ENDIF} {$ENDIF};
314   {*Pause audio functions*}
315
316   {**
317    *  This function loads a WAVE from the data source, automatically freeing
318    *  that source if freesrc is non-zero.  For example, to load a WAVE file,
319    *  you could do:
320    *
321    *      SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, ...);
322    *
323    *
324    *  If this function succeeds, it returns the given SDL_AudioSpec,
325    *  filled with the audio data format of the wave data, and sets
326    *   *audio_buf to a malloc()'d buffer containing the audio data,
327    *  and sets  *audio_len to the length of that audio buffer, in bytes.
328    *  You need to free the audio buffer with SDL_FreeWAV() when you are
329    *  done with it.
330    *
331    *  This function returns NULL and sets the SDL error message if the
332    *  wave file cannot be opened, uses an unknown data format, or is
333    *  corrupt.  Currently raw and MS-ADPCM WAVE files are supported.
334    *}
335 function SDL_LoadWAV_RW(src: PSDL_RWops; freesrc: Integer; spec: PSDL_AudioSpec; audio_buf: PPUInt8; audio_len: PUInt32): PSDL_AudioSpec cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_LoadWAV_RW' {$ENDIF} {$ENDIF};
336
337   {**
338    *  Loads a WAV from a file.
339    *  Compatibility convenience function.
340    *}
341
342    function SDL_LoadWAV(_file: PAnsiChar; spec: PSDL_AudioSpec; audio_buf: PPUInt8; audio_len: PUInt32): PSDL_AudioSpec;
343
344   {**
345    *  This function frees data previously allocated with SDL_LoadWAV_RW()
346    *}
347   procedure SDL_FreeWAV(audio_buf: PUInt8) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_FreeWAV' {$ENDIF} {$ENDIF};
348
349   {**
350    *  This function takes a source format and rate and a destination format
351    *  and rate, and initializes the cvt structure with information needed
352    *  by SDL_ConvertAudio() to convert a buffer of audio data from one format
353    *  to the other.
354    *
355    *   -1 if the format conversion is not supported, 0 if there's
356    *  no conversion needed, or 1 if the audio filter is set up.
357    *}
358 function SDL_BuildAudioCVT(cvt: PSDL_AudioCVT;
359                            src_format: TSDL_AudioFormat;
360                            src_channels: UInt8;
361                            src_rate: Integer;
362                            dst_format: TSDL_AudioFormat;
363                            dst_channels: UInt8;
364                            dst_rate: Integer): Integer cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_BuildAudioCVT' {$ENDIF} {$ENDIF};
365
366 {**
367  *  Once you have initialized the cvt structure using SDL_BuildAudioCVT(),
368  *  created an audio buffer cvt->buf, and filled it with cvt->len bytes of
369  *  audio data in the source format, this function will convert it in-place
370  *  to the desired format.
371  *
372  *  The data conversion may expand the size of the audio data, so the buffer
373  *  cvt->buf should be allocated after the cvt structure is initialized by
374  *  SDL_BuildAudioCVT(), and should be cvt->len*cvt->len_mult bytes long.
375  *}
376 function SDL_ConvertAudio(cvt: PSDL_AudioCVT): Integer cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_ConvertAudio' {$ENDIF} {$ENDIF};
377
378 const
379   SDL_MIX_MAXVOLUME = 128;
380
381   {**
382    *  This takes two audio buffers of the playing audio format and mixes
383    *  them, performing addition, volume adjustment, and overflow clipping.
384    *  The volume ranges from 0 - 128, and should be set to ::SDL_MIX_MAXVOLUME
385    *  for full audio volume.  Note this does not change hardware volume.
386    *  This is provided for convenience -- you can mix your own audio data.
387    *}
388 procedure SDL_MixAudio(dst: PUInt8; src: PUInt8; len: UInt32; volume: Integer) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_MixAudio' {$ENDIF} {$ENDIF};
389
390   {**
391    *  This works like SDL_MixAudio(), but you specify the audio format instead of
392    *  using the format of audio device 1. Thus it can be used when no audio
393    *  device is open at all.
394    *}
395 procedure SDL_MixAudioFormat(dst: PUInt8; src: PUInt8; format: TSDL_AudioFormat; len: UInt32; volume: Integer) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_MixAudioFormat' {$ENDIF} {$ENDIF};
396
397   {**
398    *   Audio lock functions
399    *
400    *  The lock manipulated by these functions protects the callback function.
401    *  During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that
402    *  the callback function is not running.  Do not call these from the callback
403    *  function or you will cause deadlock.
404    *}
405
406 procedure SDL_LockAudio cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_LockAudio' {$ENDIF} {$ENDIF};
407 procedure SDL_LockAudioDevice(dev: TSDL_AudioDeviceID) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_LockAudioDevice' {$ENDIF} {$ENDIF};
408 procedure SDL_UnlockAudio cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Unlock' {$ENDIF} {$ENDIF};
409 procedure SDL_UnlockAudioDevice(dev: TSDL_AudioDeviceID) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_UnlockAudioDevice' {$ENDIF} {$ENDIF};
410   {*Audio lock functions*}
411
412   {**
413    *  This function shuts down audio processing and closes the audio device.
414    *}
415 procedure SDL_CloseAudio cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CloseAudio' {$ENDIF} {$ENDIF};
416 procedure SDL_CloseAudioDevice(dev: TSDL_AudioDeviceID) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CloseAudioDevice' {$ENDIF} {$ENDIF};
417
418   {**
419    *  1 if audio device is still functioning, zero if not, -1 on error.
420    *}
421 function SDL_AudioDeviceConnected(dev: TSDL_AudioDeviceID): Integer cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AudioDeviceConnected' {$ENDIF} {$ENDIF};
422