All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_audio.c File Reference
#include "SDL_config.h"
#include "SDL.h"
#include "SDL_audio.h"
#include "SDL_audio_c.h"
#include "SDL_audiomem.h"
#include "SDL_sysaudio.h"

Go to the source code of this file.


#define _THIS   SDL_AudioDevice *_this
#define CHECK_FMT_STRING(x)   if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
#define DEFAULT_INPUT_DEVNAME   "System audio capture device"
#define DEFAULT_OUTPUT_DEVNAME   "System audio output device"
#define FILL_STUB(x)
#define NUM_FORMATS   10


static void close_audio_device (SDL_AudioDevice *device)
static void finalize_audio_entry_points (void)
static void free_device_list (char ***devices, int *devCount)
static SDL_AudioDeviceget_audio_device (SDL_AudioDeviceID id)
static SDL_AudioDeviceID open_audio_device (const char *devname, int iscapture, const SDL_AudioSpec *desired, SDL_AudioSpec *obtained, int allowed_changes, int min_id)
static int prepare_audiospec (const SDL_AudioSpec *orig, SDL_AudioSpec *prepared)
static void SDL_AddCaptureAudioDevice (const char *_name)
static void SDL_AddOutputAudioDevice (const char *_name)
static void SDL_AudioCloseDevice_Default (_THIS)
static void SDL_AudioDeinitialize_Default (void)
static void SDL_AudioDetectDevices_Default (int iscapture, SDL_AddAudioDevice addfn)
static Uint8SDL_AudioGetDeviceBuf_Default (_THIS)
int SDL_AudioInit (const char *driver_name)
static void SDL_AudioLockDevice_Default (SDL_AudioDevice *device)
static int SDL_AudioOpenDevice_Default (_THIS, const char *devname, int iscapture)
static void SDL_AudioPlayDevice_Default (_THIS)
void SDL_AudioQuit (void)
static void SDL_AudioThreadInit_Default (_THIS)
static void SDL_AudioUnlockDevice_Default (SDL_AudioDevice *device)
static void SDL_AudioWaitDevice_Default (_THIS)
static void SDL_AudioWaitDone_Default (_THIS)
void SDL_CalculateAudioSpec (SDL_AudioSpec *spec)
void SDL_CloseAudio (void)
void SDL_CloseAudioDevice (SDL_AudioDeviceID devid)
SDL_AudioFormat SDL_FirstAudioFormat (SDL_AudioFormat format)
const char * SDL_GetAudioDeviceName (int index, int iscapture)
SDL_AudioStatus SDL_GetAudioDeviceStatus (SDL_AudioDeviceID devid)
const char * SDL_GetAudioDriver (int index)
SDL_AudioStatus SDL_GetAudioStatus (void)
const char * SDL_GetCurrentAudioDriver ()
int SDL_GetNumAudioDevices (int iscapture)
int SDL_GetNumAudioDrivers (void)
void SDL_LockAudio (void)
void SDL_LockAudioDevice (SDL_AudioDeviceID devid)
void SDL_MixAudio (Uint8 *dst, const Uint8 *src, Uint32 len, int volume)
SDL_AudioFormat SDL_NextAudioFormat (void)
int SDL_OpenAudio (SDL_AudioSpec *desired, SDL_AudioSpec *obtained)
SDL_AudioDeviceID SDL_OpenAudioDevice (const char *device, int iscapture, const SDL_AudioSpec *desired, SDL_AudioSpec *obtained, int allowed_changes)
static SDL_AudioFormat SDL_ParseAudioFormat (const char *string)
void SDL_PauseAudio (int pause_on)
void SDL_PauseAudioDevice (SDL_AudioDeviceID devid, int pause_on)
int SDLCALL SDL_RunAudio (void *devicep)
static void SDL_StreamDeinit (SDL_AudioStreamer *stream)
static int SDL_StreamLength (SDL_AudioStreamer *stream)
static void SDL_StreamRead (SDL_AudioStreamer *stream, Uint8 *buf, int length)
static void SDL_StreamWrite (SDL_AudioStreamer *stream, Uint8 *buf, int length)
void SDL_UnlockAudio (void)
void SDL_UnlockAudioDevice (SDL_AudioDeviceID devid)


AudioBootStrap ALSA_bootstrap
AudioBootStrap ANDROIDAUD_bootstrap
AudioBootStrap ARTS_bootstrap
AudioBootStrap BEOSAUDIO_bootstrap
static const AudioBootStrap *const bootstrap []
AudioBootStrap BSD_AUDIO_bootstrap
AudioBootStrap COREAUDIO_bootstrap
static SDL_AudioDriver current_audio
AudioBootStrap DART_bootstrap
AudioBootStrap DCAUD_bootstrap
AudioBootStrap DISKAUD_bootstrap
AudioBootStrap DSOUND_bootstrap
AudioBootStrap DSP_bootstrap
AudioBootStrap DUMMYAUD_bootstrap
AudioBootStrap ESD_bootstrap
static int format_idx
static int format_idx_sub
static SDL_AudioFormat format_list [NUM_FORMATS][NUM_FORMATS]
AudioBootStrap FUSIONSOUND_bootstrap
AudioBootStrap NAS_bootstrap
AudioBootStrap NDSAUD_bootstrap
static SDL_AudioDeviceopen_devices [16]
AudioBootStrap PAUDIO_bootstrap
AudioBootStrap PSPAUD_bootstrap
AudioBootStrap PULSEAUDIO_bootstrap
AudioBootStrap QSAAUDIO_bootstrap
AudioBootStrap SNDIO_bootstrap
AudioBootStrap SNDMGR_bootstrap
AudioBootStrap SUNAUDIO_bootstrap
AudioBootStrap WINMM_bootstrap
AudioBootStrap XAUDIO2_bootstrap

Macro Definition Documentation

#define _THIS   SDL_AudioDevice *_this

Definition at line 31 of file SDL_audio.c.

#define CHECK_FMT_STRING (   x)    if (SDL_strcmp(string, #x) == 0) return AUDIO_##x

Referenced by SDL_ParseAudioFormat().

#define DEFAULT_INPUT_DEVNAME   "System audio capture device"

Definition at line 38 of file SDL_audio.c.

Referenced by open_audio_device(), and SDL_GetAudioDeviceName().

#define DEFAULT_OUTPUT_DEVNAME   "System audio output device"

Definition at line 37 of file SDL_audio.c.

Referenced by open_audio_device(), and SDL_GetAudioDeviceName().

#define FILL_STUB (   x)
if (current_audio.impl.x == NULL) { \
current_audio.impl.x = SDL_Audio##x##_Default; \
#define NULL
Definition: ftobjs.h:61
EGLSurface EGLint x
Definition: eglext.h:293
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:71

Referenced by finalize_audio_entry_points().

#define NUM_FORMATS   10

Definition at line 1199 of file SDL_audio.c.

Referenced by SDL_FirstAudioFormat(), and SDL_NextAudioFormat().

Function Documentation

static void finalize_audio_entry_points ( void  )

Definition at line 221 of file SDL_audio.c.

References FILL_STUB.

Referenced by SDL_AudioInit().

static void free_device_list ( char ***  devices,
int devCount 

Definition at line 624 of file SDL_audio.c.

References i, NULL, and SDL_free().

Referenced by SDL_AudioQuit(), and SDL_GetNumAudioDevices().

static SDL_AudioDeviceID open_audio_device ( const char *  devname,
int  iscapture,
const SDL_AudioSpec desired,
SDL_AudioSpec obtained,
int  allowed_changes,
int  min_id 
static void SDL_AddCaptureAudioDevice ( const char *  _name)
static void SDL_AddOutputAudioDevice ( const char *  _name)
static void SDL_AudioCloseDevice_Default ( _THIS  )

Definition at line 186 of file SDL_audio.c.

static void SDL_AudioDeinitialize_Default ( void  )

Definition at line 191 of file SDL_audio.c.

static void SDL_AudioDetectDevices_Default ( int  iscapture,
SDL_AddAudioDevice  addfn 

Definition at line 155 of file SDL_audio.c.

static Uint8* SDL_AudioGetDeviceBuf_Default ( _THIS  )

Definition at line 175 of file SDL_audio.c.

References NULL.

static void SDL_AudioLockDevice_Default ( SDL_AudioDevice device)
static int SDL_AudioOpenDevice_Default ( _THIS  ,
const char *  devname,
int  iscapture 

Definition at line 196 of file SDL_audio.c.

static void SDL_AudioPlayDevice_Default ( _THIS  )

Definition at line 170 of file SDL_audio.c.

static void SDL_AudioThreadInit_Default ( _THIS  )

Definition at line 160 of file SDL_audio.c.

static void SDL_AudioUnlockDevice_Default ( SDL_AudioDevice device)
static void SDL_AudioWaitDevice_Default ( _THIS  )

Definition at line 165 of file SDL_audio.c.

static void SDL_AudioWaitDone_Default ( _THIS  )

Definition at line 181 of file SDL_audio.c.

void SDL_CloseAudio ( void  )

This function shuts down audio processing and closes the audio device.

Definition at line 1169 of file SDL_audio.c.

References SDL_CloseAudioDevice().

void SDL_CloseAudioDevice ( SDL_AudioDeviceID  devid)

Definition at line 1159 of file SDL_audio.c.

References close_audio_device(), get_audio_device(), and NULL.

Referenced by open_audio_device(), SDL_AudioQuit(), and SDL_CloseAudio().

SDL_AudioFormat SDL_FirstAudioFormat ( SDL_AudioFormat  format)

Definition at line 1226 of file SDL_audio.c.

References format_idx, NUM_FORMATS, and SDL_NextAudioFormat().

Referenced by COREAUDIO_OpenDevice().

const char* SDL_GetAudioDeviceName ( int  index,
int  iscapture 

Get the human-readable name of a specific audio device. Must be a value between 0 and (number of audio devices-1). Only valid after a successfully initializing the audio subsystem. The values returned by this function reflect the latest call to SDL_GetNumAudioDevices(); recall that function to redetect available hardware.

The string returned by this function is UTF-8 encoded, read-only, and managed internally. You are not to free it. If you need to keep the string for any length of time, you should make your own copy of it, as it will be invalid next time any of several other SDL functions is called.

Definition at line 708 of file SDL_audio.c.

References DEFAULT_INPUT_DEVNAME, DEFAULT_OUTPUT_DEVNAME, SDL_AudioDriverImpl::HasCaptureSupport, SDL_AudioDriver::impl, SDL_AudioDriver::inputDeviceCount, SDL_AudioDriver::inputDevices, NULL, SDL_AudioDriverImpl::OnlyHasDefaultInputDevice, SDL_AudioDriverImpl::OnlyHasDefaultOutputDevice, SDL_AudioDriver::outputDeviceCount, SDL_AudioDriver::outputDevices, SDL_INIT_AUDIO, SDL_SetError(), and SDL_WasInit().

const char* SDL_GetAudioDriver ( int  index)

Definition at line 553 of file SDL_audio.c.

References NULL, and SDL_GetNumAudioDrivers().

Referenced by SDLTest_CommonInit().

SDL_AudioStatus SDL_GetAudioStatus ( void  )

Definition at line 1103 of file SDL_audio.c.

References SDL_GetAudioDeviceStatus().

const char* SDL_GetCurrentAudioDriver ( void  )

This function returns the name of the current audio driver, or NULL if no driver has been initialized.

Definition at line 618 of file SDL_audio.c.

References SDL_AudioDriver::name.

Referenced by SDLTest_CommonInit().

int SDL_GetNumAudioDevices ( int  iscapture)

Get the number of available devices exposed by the current driver. Only valid after a successfully initializing the audio subsystem. Returns -1 if an explicit list of devices can't be determined; this is not an error. For example, if SDL is set up to talk to a remote audio server, it can't list every one available on the Internet, but it will still allow a specific host to be specified to SDL_OpenAudioDevice().

In many common cases, when this function returns a value <= 0, it can still successfully open the default device (NULL for first argument of SDL_OpenAudioDevice()).

Definition at line 671 of file SDL_audio.c.

References SDL_AudioDriverImpl::DetectDevices, free_device_list(), SDL_AudioDriverImpl::HasCaptureSupport, SDL_AudioDriver::impl, SDL_AudioDriver::inputDeviceCount, SDL_AudioDriver::inputDevices, SDL_AudioDriverImpl::OnlyHasDefaultInputDevice, SDL_AudioDriverImpl::OnlyHasDefaultOutputDevice, SDL_AudioDriver::outputDeviceCount, SDL_AudioDriver::outputDevices, SDL_AddCaptureAudioDevice(), SDL_AddOutputAudioDevice(), SDL_INIT_AUDIO, and SDL_WasInit().

Referenced by open_audio_device().

int SDL_GetNumAudioDrivers ( void  )

Definition at line 547 of file SDL_audio.c.

References SDL_arraysize.

Referenced by SDL_GetAudioDriver(), and SDLTest_CommonInit().

void SDL_LockAudio ( void  )

Definition at line 1137 of file SDL_audio.c.

References SDL_LockAudioDevice().

void SDL_LockAudioDevice ( SDL_AudioDeviceID  devid)

Definition at line 1127 of file SDL_audio.c.

References get_audio_device(), SDL_AudioDriver::impl, and SDL_AudioDriverImpl::LockDevice.

Referenced by SDL_LockAudio().

void SDL_MixAudio ( Uint8 dst,
const Uint8 src,
Uint32  len,
int  volume 

This takes two audio buffers of the playing audio format and mixes them, performing addition, volume adjustment, and overflow clipping. The volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME for full audio volume. Note this does not change hardware volume. This is provided for convenience – you can mix your own audio data.

Definition at line 1268 of file SDL_audio.c.

References SDL_AudioDevice::convert, SDL_AudioSpec::format, get_audio_device(), SDL_AudioCVT::needed, NULL, SDL_MixAudioFormat(), SDL_AudioDevice::spec, and SDL_AudioCVT::src_format.

SDL_AudioFormat SDL_NextAudioFormat ( void  )

Definition at line 1238 of file SDL_audio.c.

References NUM_FORMATS.

Referenced by SDL_FirstAudioFormat().

int SDL_OpenAudio ( SDL_AudioSpec desired,
SDL_AudioSpec obtained 

This function opens the audio device with the desired parameters, and returns 0 if successful, placing the actual hardware parameters in the structure pointed to by obtained. If obtained is NULL, the audio data passed to the callback function will be guaranteed to be in the requested format, and will be automatically converted to the hardware audio format if necessary. This function returns -1 if it failed to open the audio device, or couldn't set up the audio thread.

When filling in the desired audio spec structure,

  • desired->freq should be the desired audio frequency in samples-per- second.
  • desired->format should be the desired audio format.
  • desired->samples is the desired size of the audio buffer, in samples. This number should be a power of two, and may be adjusted by the audio driver to a value more suitable for the hardware. Good values seem to range between 512 and 8096 inclusive, depending on the application and CPU speed. Smaller values yield faster response time, but can lead to underflow if the application is doing heavy processing and cannot fill the audio buffer in time. A stereo sample consists of both right and left channels in LR ordering. Note that the number of samples is directly related to time by the following formula:
    ms = (samples*1000)/freq
  • desired->size is the size in bytes of the audio buffer, and is calculated by SDL_OpenAudio().
  • desired->silence is the value used to set the buffer to silence, and is calculated by SDL_OpenAudio().
  • desired->callback should be set to a function that will be called when the audio device is ready for more data. It is passed a pointer to the audio buffer, and the length in bytes of the audio buffer. This function usually runs in a separate thread, and so you should protect data structures that it accesses by calling SDL_LockAudio() and SDL_UnlockAudio() in your code.
  • desired->userdata is passed as the first parameter to your callback function.

The audio device starts out playing silence when it's opened, and should be enabled for playing by calling SDL_PauseAudio(0) when you are ready for your audio callback function to be called. Since the audio driver may modify the requested size of the audio buffer, you should allocate any local mixing buffers after you open the audio device.

Definition at line 1049 of file SDL_audio.c.

References NULL, open_audio_device(), SDL_assert, SDL_AUDIO_ALLOW_ANY_CHANGE, SDL_INIT_AUDIO, SDL_InitSubSystem(), SDL_SetError(), and SDL_WasInit().

Referenced by SDLTest_CommonInit().

SDL_AudioDeviceID SDL_OpenAudioDevice ( const char *  device,
int  iscapture,
const SDL_AudioSpec desired,
SDL_AudioSpec obtained,
int  allowed_changes 

Open a specific audio device. Passing in a device name of NULL requests the most reasonable default (and is equivalent to calling SDL_OpenAudio()).

The device name is a UTF-8 string reported by SDL_GetAudioDeviceName(), but some drivers allow arbitrary and driver-specific strings, such as a hostname/IP address for a remote audio server, or a filename in the diskaudio driver.

0 on error, a valid device ID that is >= 2 on success.

SDL_OpenAudio(), unlike this function, always acts on device ID 1.

Definition at line 1078 of file SDL_audio.c.

References open_audio_device().

static SDL_AudioFormat SDL_ParseAudioFormat ( const char *  string)

Definition at line 521 of file SDL_audio.c.

References CHECK_FMT_STRING, and S32.

Referenced by prepare_audiospec().

void SDL_PauseAudio ( int  pause_on)

Definition at line 1120 of file SDL_audio.c.

References SDL_PauseAudioDevice().

void SDL_PauseAudioDevice ( SDL_AudioDeviceID  devid,
int  pause_on 
static void SDL_StreamDeinit ( SDL_AudioStreamer stream)

Definition at line 301 of file SDL_audio.c.

References SDL_AudioStreamer::buffer, and SDL_free().

Referenced by SDL_RunAudio().

static int SDL_StreamLength ( SDL_AudioStreamer stream)
static void SDL_StreamRead ( SDL_AudioStreamer stream,
Uint8 buf,
int  length 

Definition at line 261 of file SDL_audio.c.

References SDL_AudioStreamer::buffer, i, and SDL_AudioStreamer::read_pos.

Referenced by SDL_RunAudio().

static void SDL_StreamWrite ( SDL_AudioStreamer stream,
Uint8 buf,
int  length 

Definition at line 249 of file SDL_audio.c.

References SDL_AudioStreamer::buffer, i, and SDL_AudioStreamer::write_pos.

Referenced by SDL_RunAudio().

void SDL_UnlockAudio ( void  )

Definition at line 1153 of file SDL_audio.c.

References SDL_UnlockAudioDevice().

void SDL_UnlockAudioDevice ( SDL_AudioDeviceID  devid)

Variable Documentation

AudioBootStrap ALSA_bootstrap
AudioBootStrap ANDROIDAUD_bootstrap
AudioBootStrap ARTS_bootstrap
AudioBootStrap BEOSAUDIO_bootstrap
const AudioBootStrap* const bootstrap[]

Definition at line 73 of file SDL_audio.c.

AudioBootStrap BSD_AUDIO_bootstrap
AudioBootStrap COREAUDIO_bootstrap

Definition at line 555 of file SDL_coreaudio.c.

SDL_AudioDriver current_audio

Definition at line 33 of file SDL_audio.c.

AudioBootStrap DART_bootstrap
AudioBootStrap DCAUD_bootstrap
AudioBootStrap DISKAUD_bootstrap
AudioBootStrap DSOUND_bootstrap
AudioBootStrap DSP_bootstrap
AudioBootStrap DUMMYAUD_bootstrap

Definition at line 44 of file SDL_dummyaudio.c.

AudioBootStrap ESD_bootstrap
int format_idx

Definition at line 1200 of file SDL_audio.c.

Referenced by SDL_FirstAudioFormat().

int format_idx_sub

Definition at line 1201 of file SDL_audio.c.

SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS]
Initial value:
= {
#define AUDIO_S32MSB
Definition: SDL_audio.h:104
#define AUDIO_U16LSB
Definition: SDL_audio.h:91
#define AUDIO_F32MSB
Definition: SDL_audio.h:113
#define AUDIO_U8
Definition: SDL_audio.h:89
#define AUDIO_F32LSB
Definition: SDL_audio.h:112
#define AUDIO_S32LSB
Definition: SDL_audio.h:103
#define AUDIO_S16MSB
Definition: SDL_audio.h:94
#define AUDIO_S16LSB
Definition: SDL_audio.h:92
#define AUDIO_S8
Definition: SDL_audio.h:90
#define AUDIO_U16MSB
Definition: SDL_audio.h:93

Definition at line 1202 of file SDL_audio.c.

AudioBootStrap FUSIONSOUND_bootstrap
AudioBootStrap NAS_bootstrap
AudioBootStrap NDSAUD_bootstrap
SDL_AudioDevice* open_devices[16]

Definition at line 34 of file SDL_audio.c.

AudioBootStrap PAUDIO_bootstrap
AudioBootStrap PSPAUD_bootstrap

Definition at line 188 of file SDL_pspaudio.c.

AudioBootStrap PULSEAUDIO_bootstrap
AudioBootStrap QSAAUDIO_bootstrap
AudioBootStrap SNDIO_bootstrap
AudioBootStrap SNDMGR_bootstrap
AudioBootStrap SUNAUDIO_bootstrap
AudioBootStrap WINMM_bootstrap
AudioBootStrap XAUDIO2_bootstrap