Path: blob/main/system/include/SDL/SDL_audio.h
6169 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2011 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021/**22* \file SDL_audio.h23*24* Access to the raw audio mixing buffer for the SDL library.25*/2627#ifndef _SDL_audio_h28#define _SDL_audio_h2930#include "SDL_stdinc.h"31#include "SDL_error.h"32#include "SDL_endian.h"33#include "SDL_mutex.h"34#include "SDL_thread.h"35#include "SDL_rwops.h"3637#include "begin_code.h"38/* Set up for C function definitions, even when using C++ */39#ifdef __cplusplus40/* *INDENT-OFF* */41extern "C" {42/* *INDENT-ON* */43#endif4445/**46* \brief Audio format flags.47*48* These are what the 16 bits in SDL_AudioFormat currently mean...49* (Unspecified bits are always zero).50*51* \verbatim52++-----------------------sample is signed if set53||54|| ++-----------sample is bigendian if set55|| ||56|| || ++---sample is float if set57|| || ||58|| || || +---sample bit size---+59|| || || | |6015 14 13 12 11 10 09 08 07 06 05 04 03 02 01 0061\endverbatim62*63* There are macros in SDL 1.3 and later to query these bits.64*/65typedef Uint16 SDL_AudioFormat;6667/**68* \name Audio flags69*/70/*@{*/7172#define SDL_AUDIO_MASK_BITSIZE (0xFF)73#define SDL_AUDIO_MASK_DATATYPE (1<<8)74#define SDL_AUDIO_MASK_ENDIAN (1<<12)75#define SDL_AUDIO_MASK_SIGNED (1<<15)76#define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)77#define SDL_AUDIO_ISFLOAT(x) (x & SDL_AUDIO_MASK_DATATYPE)78#define SDL_AUDIO_ISBIGENDIAN(x) (x & SDL_AUDIO_MASK_ENDIAN)79#define SDL_AUDIO_ISSIGNED(x) (x & SDL_AUDIO_MASK_SIGNED)80#define SDL_AUDIO_ISINT(x) (!SDL_AUDIO_ISFLOAT(x))81#define SDL_AUDIO_ISLITTLEENDIAN(x) (!SDL_AUDIO_ISBIGENDIAN(x))82#define SDL_AUDIO_ISUNSIGNED(x) (!SDL_AUDIO_ISSIGNED(x))8384/**85* \name Audio format flags86*87* Defaults to LSB byte order.88*/89/*@{*/90#define AUDIO_U8 0x0008 /**< Unsigned 8-bit samples */91#define AUDIO_S8 0x8008 /**< Signed 8-bit samples */92#define AUDIO_U16LSB 0x0010 /**< Unsigned 16-bit samples */93#define AUDIO_S16LSB 0x8010 /**< Signed 16-bit samples */94#define AUDIO_U16MSB 0x1010 /**< As above, but big-endian byte order */95#define AUDIO_S16MSB 0x9010 /**< As above, but big-endian byte order */96#define AUDIO_U16 AUDIO_U16LSB97#define AUDIO_S16 AUDIO_S16LSB98/*@}*/99100/**101* \name int32 support102*103* New to SDL 1.3.104*/105/*@{*/106#define AUDIO_S32LSB 0x8020 /**< 32-bit integer samples */107#define AUDIO_S32MSB 0x9020 /**< As above, but big-endian byte order */108#define AUDIO_S32 AUDIO_S32LSB109/*@}*/110111/**112* \name float32 support113*114* New to SDL 1.3.115*/116/*@{*/117#define AUDIO_F32LSB 0x8120 /**< 32-bit floating point samples */118#define AUDIO_F32MSB 0x9120 /**< As above, but big-endian byte order */119#define AUDIO_F32 AUDIO_F32LSB120/*@}*/121122/**123* \name Native audio byte ordering124*/125/*@{*/126#if SDL_BYTEORDER == SDL_LIL_ENDIAN127#define AUDIO_U16SYS AUDIO_U16LSB128#define AUDIO_S16SYS AUDIO_S16LSB129#define AUDIO_S32SYS AUDIO_S32LSB130#define AUDIO_F32SYS AUDIO_F32LSB131#else132#define AUDIO_U16SYS AUDIO_U16MSB133#define AUDIO_S16SYS AUDIO_S16MSB134#define AUDIO_S32SYS AUDIO_S32MSB135#define AUDIO_F32SYS AUDIO_F32MSB136#endif137/*@}*/138139/**140* \name Allow change flags141*142* Which audio format changes are allowed when opening a device.143*/144/*@{*/145#define SDL_AUDIO_ALLOW_FREQUENCY_CHANGE 0x00000001146#define SDL_AUDIO_ALLOW_FORMAT_CHANGE 0x00000002147#define SDL_AUDIO_ALLOW_CHANNELS_CHANGE 0x00000004148#define SDL_AUDIO_ALLOW_ANY_CHANGE (SDL_AUDIO_ALLOW_FREQUENCY_CHANGE|SDL_AUDIO_ALLOW_FORMAT_CHANGE|SDL_AUDIO_ALLOW_CHANNELS_CHANGE)149/*@}*/150151/*@}*//*Audio flags*/152153/**154* This function is called when the audio device needs more data.155*156* \param userdata An application-specific parameter saved in157* the SDL_AudioSpec structure158* \param stream A pointer to the audio data buffer.159* \param len The length of that buffer in bytes.160*161* Once the callback returns, the buffer will no longer be valid.162* Stereo samples are stored in a LRLRLR ordering.163*/164typedef void (SDLCALL * SDL_AudioCallback) (void *userdata, Uint8 * stream,165int len);166167/**168* The calculated values in this structure are calculated by SDL_OpenAudio().169*/170typedef struct SDL_AudioSpec171{172int freq; /**< DSP frequency -- samples per second */173SDL_AudioFormat format; /**< Audio data format */174Uint8 channels; /**< Number of channels: 1 mono, 2 stereo */175Uint8 silence; /**< Audio buffer silence value (calculated) */176Uint16 samples; /**< Audio buffer size in samples (power of 2) */177Uint16 padding; /**< Necessary for some compile environments */178Uint32 size; /**< Audio buffer size in bytes (calculated) */179SDL_AudioCallback callback;180void *userdata;181} SDL_AudioSpec;182183184struct SDL_AudioCVT;185typedef void (SDLCALL * SDL_AudioFilter) (struct SDL_AudioCVT * cvt,186SDL_AudioFormat format);187188/**189* A structure to hold a set of audio conversion filters and buffers.190*/191typedef struct SDL_AudioCVT192{193int needed; /**< Set to 1 if conversion possible */194SDL_AudioFormat src_format; /**< Source audio format */195SDL_AudioFormat dst_format; /**< Target audio format */196double rate_incr; /**< Rate conversion increment */197Uint8 *buf; /**< Buffer to hold entire audio data */198int len; /**< Length of original audio buffer */199int len_cvt; /**< Length of converted audio buffer */200int len_mult; /**< buffer must be len*len_mult big */201double len_ratio; /**< Given len, final size is len*len_ratio */202SDL_AudioFilter filters[10]; /**< Filter list */203int filter_index; /**< Current audio conversion function */204} SDL_AudioCVT;205206207/* Function prototypes */208209/**210* \name Driver discovery functions211*212* These functions return the list of built in audio drivers, in the213* order that they are normally initialized by default.214*/215/*@{*/216extern DECLSPEC int SDLCALL SDL_GetNumAudioDrivers(void);217extern DECLSPEC const char *SDLCALL SDL_GetAudioDriver(int index);218/*@}*/219220/**221* \name Initialization and cleanup222*223* \internal These functions are used internally, and should not be used unless224* you have a specific need to specify the audio driver you want to225* use. You should normally use SDL_Init() or SDL_InitSubSystem().226*/227/*@{*/228extern DECLSPEC int SDLCALL SDL_AudioInit(const char *driver_name);229extern DECLSPEC void SDLCALL SDL_AudioQuit(void);230/*@}*/231232/**233* This function returns the name of the current audio driver, or NULL234* if no driver has been initialized.235*/236extern DECLSPEC const char *SDLCALL SDL_GetCurrentAudioDriver(void);237238/**239* This function opens the audio device with the desired parameters, and240* returns 0 if successful, placing the actual hardware parameters in the241* structure pointed to by \c obtained. If \c obtained is NULL, the audio242* data passed to the callback function will be guaranteed to be in the243* requested format, and will be automatically converted to the hardware244* audio format if necessary. This function returns -1 if it failed245* to open the audio device, or couldn't set up the audio thread.246*247* When filling in the desired audio spec structure,248* - \c desired->freq should be the desired audio frequency in samples-per-249* second.250* - \c desired->format should be the desired audio format.251* - \c desired->samples is the desired size of the audio buffer, in252* samples. This number should be a power of two, and may be adjusted by253* the audio driver to a value more suitable for the hardware. Good values254* seem to range between 512 and 8096 inclusive, depending on the255* application and CPU speed. Smaller values yield faster response time,256* but can lead to underflow if the application is doing heavy processing257* and cannot fill the audio buffer in time. A stereo sample consists of258* both right and left channels in LR ordering.259* Note that the number of samples is directly related to time by the260* following formula: \code ms = (samples*1000)/freq \endcode261* - \c desired->size is the size in bytes of the audio buffer, and is262* calculated by SDL_OpenAudio().263* - \c desired->silence is the value used to set the buffer to silence,264* and is calculated by SDL_OpenAudio().265* - \c desired->callback should be set to a function that will be called266* when the audio device is ready for more data. It is passed a pointer267* to the audio buffer, and the length in bytes of the audio buffer.268* This function usually runs in a separate thread, and so you should269* protect data structures that it accesses by calling SDL_LockAudio()270* and SDL_UnlockAudio() in your code.271* - \c desired->userdata is passed as the first parameter to your callback272* function.273*274* The audio device starts out playing silence when it's opened, and should275* be enabled for playing by calling \c SDL_PauseAudio(0) when you are ready276* for your audio callback function to be called. Since the audio driver277* may modify the requested size of the audio buffer, you should allocate278* any local mixing buffers after you open the audio device.279*/280extern DECLSPEC int SDLCALL SDL_OpenAudio(SDL_AudioSpec * desired,281SDL_AudioSpec * obtained);282283/**284* SDL Audio Device IDs.285*286* A successful call to SDL_OpenAudio() is always device id 1, and legacy287* SDL audio APIs assume you want this device ID. SDL_OpenAudioDevice() calls288* always returns devices >= 2 on success. The legacy calls are good both289* for backwards compatibility and when you don't care about multiple,290* specific, or capture devices.291*/292typedef Uint32 SDL_AudioDeviceID;293294/**295* Get the number of available devices exposed by the current driver.296* Only valid after a successfully initializing the audio subsystem.297* Returns -1 if an explicit list of devices can't be determined; this is298* not an error. For example, if SDL is set up to talk to a remote audio299* server, it can't list every one available on the Internet, but it will300* still allow a specific host to be specified to SDL_OpenAudioDevice().301*302* In many common cases, when this function returns a value <= 0, it can still303* successfully open the default device (NULL for first argument of304* SDL_OpenAudioDevice()).305*/306extern DECLSPEC int SDLCALL SDL_GetNumAudioDevices(int iscapture);307308/**309* Get the human-readable name of a specific audio device.310* Must be a value between 0 and (number of audio devices-1).311* Only valid after a successfully initializing the audio subsystem.312* The values returned by this function reflect the latest call to313* SDL_GetNumAudioDevices(); recall that function to redetect available314* hardware.315*316* The string returned by this function is UTF-8 encoded, read-only, and317* managed internally. You are not to free it. If you need to keep the318* string for any length of time, you should make your own copy of it, as it319* will be invalid next time any of several other SDL functions is called.320*/321extern DECLSPEC const char *SDLCALL SDL_GetAudioDeviceName(int index,322int iscapture);323324325/**326* Open a specific audio device. Passing in a device name of NULL requests327* the most reasonable default (and is equivalent to calling SDL_OpenAudio()).328*329* The device name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but330* some drivers allow arbitrary and driver-specific strings, such as a331* hostname/IP address for a remote audio server, or a filename in the332* diskaudio driver.333*334* \return 0 on error, a valid device ID that is >= 2 on success.335*336* SDL_OpenAudio(), unlike this function, always acts on device ID 1.337*/338extern DECLSPEC SDL_AudioDeviceID SDLCALL SDL_OpenAudioDevice(const char339*device,340int iscapture,341const342SDL_AudioSpec *343desired,344SDL_AudioSpec *345obtained,346int347allowed_changes);348349350351/**352* \name Audio state353*354* Get the current audio state.355*/356/*@{*/357typedef enum358{359SDL_AUDIO_STOPPED = 0,360SDL_AUDIO_PLAYING,361SDL_AUDIO_PAUSED362} SDL_AudioStatus;363extern DECLSPEC SDL_AudioStatus SDLCALL SDL_GetAudioStatus(void);364365extern DECLSPEC SDL_AudioStatus SDLCALL366SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev);367/*@}*//*Audio State*/368369/**370* \name Pause audio functions371*372* These functions pause and unpause the audio callback processing.373* They should be called with a parameter of 0 after opening the audio374* device to start playing sound. This is so you can safely initialize375* data for your callback function after opening the audio device.376* Silence will be written to the audio device during the pause.377*/378/*@{*/379extern DECLSPEC void SDLCALL SDL_PauseAudio(int pause_on);380extern DECLSPEC void SDLCALL SDL_PauseAudioDevice(SDL_AudioDeviceID dev,381int pause_on);382/*@}*//*Pause audio functions*/383384/**385* This function loads a WAVE from the data source, automatically freeing386* that source if \c freesrc is non-zero. For example, to load a WAVE file,387* you could do:388* \code389* SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, ...);390* \endcode391*392* If this function succeeds, it returns the given SDL_AudioSpec,393* filled with the audio data format of the wave data, and sets394* \c *audio_buf to a malloc()'d buffer containing the audio data,395* and sets \c *audio_len to the length of that audio buffer, in bytes.396* You need to free the audio buffer with SDL_FreeWAV() when you are397* done with it.398*399* This function returns NULL and sets the SDL error message if the400* wave file cannot be opened, uses an unknown data format, or is401* corrupt. Currently raw and MS-ADPCM WAVE files are supported.402*/403extern DECLSPEC SDL_AudioSpec *SDLCALL SDL_LoadWAV_RW(SDL_RWops * src,404int freesrc,405SDL_AudioSpec * spec,406Uint8 ** audio_buf,407Uint32 * audio_len);408409/**410* Loads a WAV from a file.411* Compatibility convenience function.412*/413#define SDL_LoadWAV(file, spec, audio_buf, audio_len) \414SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)415416/**417* This function frees data previously allocated with SDL_LoadWAV_RW()418*/419extern DECLSPEC void SDLCALL SDL_FreeWAV(Uint8 * audio_buf);420421/**422* This function takes a source format and rate and a destination format423* and rate, and initializes the \c cvt structure with information needed424* by SDL_ConvertAudio() to convert a buffer of audio data from one format425* to the other.426*427* \return -1 if the format conversion is not supported, 0 if there's428* no conversion needed, or 1 if the audio filter is set up.429*/430extern DECLSPEC int SDLCALL SDL_BuildAudioCVT(SDL_AudioCVT * cvt,431SDL_AudioFormat src_format,432Uint8 src_channels,433int src_rate,434SDL_AudioFormat dst_format,435Uint8 dst_channels,436int dst_rate);437438/**439* Once you have initialized the \c cvt structure using SDL_BuildAudioCVT(),440* created an audio buffer \c cvt->buf, and filled it with \c cvt->len bytes of441* audio data in the source format, this function will convert it in-place442* to the desired format.443*444* The data conversion may expand the size of the audio data, so the buffer445* \c cvt->buf should be allocated after the \c cvt structure is initialized by446* SDL_BuildAudioCVT(), and should be \c cvt->len*cvt->len_mult bytes long.447*/448extern DECLSPEC int SDLCALL SDL_ConvertAudio(SDL_AudioCVT * cvt);449450#define SDL_MIX_MAXVOLUME 128451/**452* This takes two audio buffers of the playing audio format and mixes453* them, performing addition, volume adjustment, and overflow clipping.454* The volume ranges from 0 - 128, and should be set to ::SDL_MIX_MAXVOLUME455* for full audio volume. Note this does not change hardware volume.456* This is provided for convenience -- you can mix your own audio data.457*/458extern DECLSPEC void SDLCALL SDL_MixAudio(Uint8 * dst, const Uint8 * src,459Uint32 len, int volume);460461/**462* This works like SDL_MixAudio(), but you specify the audio format instead of463* using the format of audio device 1. Thus it can be used when no audio464* device is open at all.465*/466extern DECLSPEC void SDLCALL SDL_MixAudioFormat(Uint8 * dst,467const Uint8 * src,468SDL_AudioFormat format,469Uint32 len, int volume);470471/**472* \name Audio lock functions473*474* The lock manipulated by these functions protects the callback function.475* During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that476* the callback function is not running. Do not call these from the callback477* function or you will cause deadlock.478*/479/*@{*/480extern DECLSPEC void SDLCALL SDL_LockAudio(void);481extern DECLSPEC void SDLCALL SDL_LockAudioDevice(SDL_AudioDeviceID dev);482extern DECLSPEC void SDLCALL SDL_UnlockAudio(void);483extern DECLSPEC void SDLCALL SDL_UnlockAudioDevice(SDL_AudioDeviceID dev);484/*@}*//*Audio lock functions*/485486/**487* This function shuts down audio processing and closes the audio device.488*/489extern DECLSPEC void SDLCALL SDL_CloseAudio(void);490extern DECLSPEC void SDLCALL SDL_CloseAudioDevice(SDL_AudioDeviceID dev);491492/**493* \return 1 if audio device is still functioning, zero if not, -1 on error.494*/495extern DECLSPEC int SDLCALL SDL_AudioDeviceConnected(SDL_AudioDeviceID dev);496497498/* Ends C function definitions when using C++ */499#ifdef __cplusplus500/* *INDENT-OFF* */501}502/* *INDENT-ON* */503#endif504#include "close_code.h"505506#endif /* _SDL_audio_h */507508/* vi: set ts=4 sw=4 expandtab: */509510511