include/SDL_audio.h
changeset 0 74212992fb08
child 1 cf2af46e9e2a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/include/SDL_audio.h	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,260 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* Access to the raw audio mixing buffer for the SDL library */
    1.32 +
    1.33 +#ifndef _SDL_audio_h
    1.34 +#define _SDL_audio_h
    1.35 +
    1.36 +#include <stdio.h>
    1.37 +
    1.38 +#include "SDL_main.h"
    1.39 +#include "SDL_types.h"
    1.40 +#include "SDL_error.h"
    1.41 +#include "SDL_rwops.h"
    1.42 +#include "SDL_byteorder.h"
    1.43 +
    1.44 +#include "begin_code.h"
    1.45 +/* Set up for C function definitions, even when using C++ */
    1.46 +#ifdef __cplusplus
    1.47 +extern "C" {
    1.48 +#endif
    1.49 +
    1.50 +/* The calculated values in this structure are calculated by SDL_OpenAudio() */
    1.51 +typedef struct {
    1.52 +	int freq;		/* DSP frequency -- samples per second */
    1.53 +	Uint16 format;		/* Audio data format */
    1.54 +	Uint8  channels;	/* Number of channels: 1 mono, 2 stereo */
    1.55 +	Uint8  silence;		/* Audio buffer silence value (calculated) */
    1.56 +	Uint16 samples;		/* Audio buffer size in samples */
    1.57 +	Uint16 padding;		/* Necessary for some compile environments */
    1.58 +	Uint32 size;		/* Audio buffer size in bytes (calculated) */
    1.59 +	/* This function is called when the audio device needs more data.
    1.60 +	   'stream' is a pointer to the audio data buffer
    1.61 +	   'len' is the length of that buffer in bytes.
    1.62 +	   Once the callback returns, the buffer will no longer be valid.
    1.63 +	   Stereo samples are stored in a LRLRLR ordering.
    1.64 +	*/
    1.65 +	void (*callback)(void *userdata, Uint8 *stream, int len);
    1.66 +	void  *userdata;
    1.67 +} SDL_AudioSpec;
    1.68 +
    1.69 +/* Audio format flags (defaults to LSB byte order) */
    1.70 +#define AUDIO_U8	0x0008	/* Unsigned 8-bit samples */
    1.71 +#define AUDIO_S8	0x8008	/* Signed 8-bit samples */
    1.72 +#define AUDIO_U16LSB	0x0010	/* Unsigned 16-bit samples */
    1.73 +#define AUDIO_S16LSB	0x8010	/* Signed 16-bit samples */
    1.74 +#define AUDIO_U16MSB	0x1010	/* As above, but big-endian byte order */
    1.75 +#define AUDIO_S16MSB	0x9010	/* As above, but big-endian byte order */
    1.76 +#define AUDIO_U16	AUDIO_U16LSB
    1.77 +#define AUDIO_S16	AUDIO_S16LSB
    1.78 +
    1.79 +/* Native audio byte ordering */
    1.80 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
    1.81 +#define AUDIO_U16SYS	AUDIO_U16LSB
    1.82 +#define AUDIO_S16SYS	AUDIO_S16LSB
    1.83 +#else
    1.84 +#define AUDIO_U16SYS	AUDIO_U16MSB
    1.85 +#define AUDIO_S16SYS	AUDIO_S16MSB
    1.86 +#endif
    1.87 +
    1.88 +
    1.89 +/* A structure to hold a set of audio conversion filters and buffers */
    1.90 +typedef struct SDL_AudioCVT {
    1.91 +	int needed;			/* Set to 1 if conversion possible */
    1.92 +	Uint16 src_format;		/* Source audio format */
    1.93 +	Uint16 dst_format;		/* Target audio format */
    1.94 +	double rate_incr;		/* Rate conversion increment */
    1.95 +	Uint8 *buf;			/* Buffer to hold entire audio data */
    1.96 +	int    len;			/* Length of original audio buffer */
    1.97 +	int    len_cvt;			/* Length of converted audio buffer */
    1.98 +	int    len_mult;		/* buffer must be len*len_mult big */
    1.99 +	double len_ratio; 	/* Given len, final size is len*len_ratio */
   1.100 +	void (*filters[10])(struct SDL_AudioCVT *cvt, Uint16 format);
   1.101 +	int filter_index;		/* Current audio conversion function */
   1.102 +} SDL_AudioCVT;
   1.103 +
   1.104 +
   1.105 +/* Function prototypes */
   1.106 +
   1.107 +/* These functions are used internally, and should not be used unless you
   1.108 + * have a specific need to specify the audio driver you want to use.
   1.109 + * You should normally use SDL_Init() or SDL_InitSubSystem().
   1.110 + */
   1.111 +extern DECLSPEC int SDL_AudioInit(const char *driver_name);
   1.112 +extern DECLSPEC void SDL_AudioQuit(void);
   1.113 +
   1.114 +/* This function fills the given character buffer with the name of the
   1.115 + * current audio driver, and returns a pointer to it if the audio driver has
   1.116 + * been initialized.  It returns NULL if no driver has been initialized.
   1.117 + */
   1.118 +extern DECLSPEC char *SDL_AudioDriverName(char *namebuf, int maxlen);
   1.119 +
   1.120 +/*
   1.121 + * This function opens the audio device with the desired parameters, and
   1.122 + * returns 0 if successful, placing the actual hardware parameters in the
   1.123 + * structure pointed to by 'obtained'.  If 'obtained' is NULL, the audio
   1.124 + * data passed to the callback function will be guaranteed to be in the
   1.125 + * requested format, and will be automatically converted to the hardware
   1.126 + * audio format if necessary.  This function returns -1 if it failed 
   1.127 + * to open the audio device, or couldn't set up the audio thread.
   1.128 + *
   1.129 + * When filling in the desired audio spec structure,
   1.130 + *  'desired->freq' should be the desired audio frequency in samples-per-second.
   1.131 + *  'desired->format' should be the desired audio format.
   1.132 + *  'desired->samples' is the desired size of the audio buffer, in samples.
   1.133 + *     This number should be a power of two, and may be adjusted by the audio
   1.134 + *     driver to a value more suitable for the hardware.  Good values seem to
   1.135 + *     range between 512 and 8096 inclusive, depending on the application and
   1.136 + *     CPU speed.  Smaller values yield faster response time, but can lead
   1.137 + *     to underflow if the application is doing heavy processing and cannot
   1.138 + *     fill the audio buffer in time.  A stereo sample consists of both right
   1.139 + *     and left channels in LR ordering.
   1.140 + *     Note that the number of samples is directly related to time by the
   1.141 + *     following formula:  ms = (samples*1000)/freq
   1.142 + *  'desired->size' is the size in bytes of the audio buffer, and is
   1.143 + *     calculated by SDL_OpenAudio().
   1.144 + *  'desired->silence' is the value used to set the buffer to silence,
   1.145 + *     and is calculated by SDL_OpenAudio().
   1.146 + *  'desired->callback' should be set to a function that will be called
   1.147 + *     when the audio device is ready for more data.  It is passed a pointer
   1.148 + *     to the audio buffer, and the length in bytes of the audio buffer.
   1.149 + *     This function usually runs in a separate thread, and so you should
   1.150 + *     protect data structures that it accesses by calling SDL_LockAudio()
   1.151 + *     and SDL_UnlockAudio() in your code.
   1.152 + *  'desired->userdata' is passed as the first parameter to your callback
   1.153 + *     function.
   1.154 + *
   1.155 + * The audio device starts out playing silence when it's opened, and should
   1.156 + * be enabled for playing by calling SDL_PauseAudio(0) when you are ready
   1.157 + * for your audio callback function to be called.  Since the audio driver
   1.158 + * may modify the requested size of the audio buffer, you should allocate
   1.159 + * any local mixing buffers after you open the audio device.
   1.160 + */
   1.161 +extern DECLSPEC int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained);
   1.162 +
   1.163 +/*
   1.164 + * Get the current audio state:
   1.165 + */
   1.166 +typedef enum {
   1.167 +	SDL_AUDIO_STOPPED = 0,
   1.168 +	SDL_AUDIO_PLAYING,
   1.169 +	SDL_AUDIO_PAUSED
   1.170 +} SDL_audiostatus;
   1.171 +extern DECLSPEC SDL_audiostatus SDL_GetAudioStatus(void);
   1.172 +
   1.173 +/*
   1.174 + * This function pauses and unpauses the audio callback processing.
   1.175 + * It should be called with a parameter of 0 after opening the audio
   1.176 + * device to start playing sound.  This is so you can safely initialize
   1.177 + * data for your callback function after opening the audio device.
   1.178 + * Silence will be written to the audio device during the pause.
   1.179 + */
   1.180 +extern DECLSPEC void SDL_PauseAudio(int pause_on);
   1.181 +
   1.182 +/*
   1.183 + * This function loads a WAVE from the data source, automatically freeing
   1.184 + * that source if 'freesrc' is non-zero.  For example, to load a WAVE file,
   1.185 + * you could do:
   1.186 + *	SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, ...);
   1.187 + *
   1.188 + * If this function succeeds, it returns the given SDL_AudioSpec,
   1.189 + * filled with the audio data format of the wave data, and sets
   1.190 + * 'audio_buf' to a malloc()'d buffer containing the audio data,
   1.191 + * and sets 'audio_len' to the length of that audio buffer, in bytes.
   1.192 + * You need to free the audio buffer with SDL_FreeWAV() when you are 
   1.193 + * done with it.
   1.194 + *
   1.195 + * This function returns NULL and sets the SDL error message if the 
   1.196 + * wave file cannot be opened, uses an unknown data format, or is 
   1.197 + * corrupt.  Currently raw and MS-ADPCM WAVE files are supported.
   1.198 + */
   1.199 +extern DECLSPEC SDL_AudioSpec *SDL_LoadWAV_RW(SDL_RWops *src, int freesrc,
   1.200 +		 SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);
   1.201 +
   1.202 +/* Compatibility convenience function -- loads a WAV from a file */
   1.203 +#define SDL_LoadWAV(file, spec, audio_buf, audio_len) \
   1.204 +	SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
   1.205 +
   1.206 +/*
   1.207 + * This function frees data previously allocated with SDL_LoadWAV_RW()
   1.208 + */
   1.209 +extern DECLSPEC void SDL_FreeWAV(Uint8 *audio_buf);
   1.210 +
   1.211 +/*
   1.212 + * This function takes a source format and rate and a destination format
   1.213 + * and rate, and initializes the 'cvt' structure with information needed
   1.214 + * by SDL_ConvertAudio() to convert a buffer of audio data from one format
   1.215 + * to the other.
   1.216 + * This function returns 0, or -1 if there was an error.
   1.217 + */
   1.218 +extern DECLSPEC int SDL_BuildAudioCVT(SDL_AudioCVT *cvt,
   1.219 +		Uint16 src_format, Uint8 src_channels, int src_rate,
   1.220 +		Uint16 dst_format, Uint8 dst_channels, int dst_rate);
   1.221 +
   1.222 +/* Once you have initialized the 'cvt' structure using SDL_BuildAudioCVT(),
   1.223 + * created an audio buffer cvt->buf, and filled it with cvt->len bytes of
   1.224 + * audio data in the source format, this function will convert it in-place
   1.225 + * to the desired format.
   1.226 + * The data conversion may expand the size of the audio data, so the buffer
   1.227 + * cvt->buf should be allocated after the cvt structure is initialized by
   1.228 + * SDL_BuildAudioCVT(), and should be cvt->len*cvt->len_mult bytes long.
   1.229 + */
   1.230 +extern DECLSPEC int SDL_ConvertAudio(SDL_AudioCVT *cvt);
   1.231 +
   1.232 +/*
   1.233 + * This takes two audio buffers of the playing audio format and mixes
   1.234 + * them, performing addition, volume adjustment, and overflow clipping.
   1.235 + * The volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
   1.236 + * for full audio volume.  Note this does not change hardware volume.
   1.237 + * This is provided for convenience -- you can mix your own audio data.
   1.238 + */
   1.239 +#define SDL_MIX_MAXVOLUME 128
   1.240 +extern DECLSPEC void SDL_MixAudio(Uint8 *dst, Uint8 *src, Uint32 len, int volume);
   1.241 +
   1.242 +/*
   1.243 + * The lock manipulated by these functions protects the callback function.
   1.244 + * During a LockAudio/UnlockAudio pair, you can be guaranteed that the
   1.245 + * callback function is not running.  Do not call these from the callback
   1.246 + * function or you will cause deadlock.
   1.247 + */
   1.248 +extern DECLSPEC void SDL_LockAudio(void);
   1.249 +extern DECLSPEC void SDL_UnlockAudio(void);
   1.250 +
   1.251 +/*
   1.252 + * This function shuts down audio processing and closes the audio device.
   1.253 + */
   1.254 +extern DECLSPEC void SDL_CloseAudio(void);
   1.255 +
   1.256 +
   1.257 +/* Ends C function definitions when using C++ */
   1.258 +#ifdef __cplusplus
   1.259 +}
   1.260 +#endif
   1.261 +#include "close_code.h"
   1.262 +
   1.263 +#endif /* _SDL_audio_h */