SDL  2.0
SDL_audio.h File Reference
#include "SDL_stdinc.h"
#include "SDL_error.h"
#include "SDL_endian.h"
#include "SDL_mutex.h"
#include "SDL_thread.h"
#include "SDL_rwops.h"
#include "begin_code.h"
#include "close_code.h"
+ Include dependency graph for SDL_audio.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDL_AudioSpec
 
struct  SDL_AudioCVT
 

Macros

#define SDL_AUDIOCVT_PACKED
 
#define SDL_LoadWAV(file, spec, audio_buf, audio_len)   SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)
 
#define SDL_MIX_MAXVOLUME   128
 
Audio flags
#define SDL_AUDIO_MASK_BITSIZE   (0xFF)
 
#define SDL_AUDIO_MASK_DATATYPE   (1<<8)
 
#define SDL_AUDIO_MASK_ENDIAN   (1<<12)
 
#define SDL_AUDIO_MASK_SIGNED   (1<<15)
 
#define SDL_AUDIO_BITSIZE(x)   (x & SDL_AUDIO_MASK_BITSIZE)
 
#define SDL_AUDIO_ISFLOAT(x)   (x & SDL_AUDIO_MASK_DATATYPE)
 
#define SDL_AUDIO_ISBIGENDIAN(x)   (x & SDL_AUDIO_MASK_ENDIAN)
 
#define SDL_AUDIO_ISSIGNED(x)   (x & SDL_AUDIO_MASK_SIGNED)
 
#define SDL_AUDIO_ISINT(x)   (!SDL_AUDIO_ISFLOAT(x))
 
#define SDL_AUDIO_ISLITTLEENDIAN(x)   (!SDL_AUDIO_ISBIGENDIAN(x))
 
#define SDL_AUDIO_ISUNSIGNED(x)   (!SDL_AUDIO_ISSIGNED(x))
 
Audio format flags

Defaults to LSB byte order.

#define AUDIO_U8   0x0008
 
#define AUDIO_S8   0x8008
 
#define AUDIO_U16LSB   0x0010
 
#define AUDIO_S16LSB   0x8010
 
#define AUDIO_U16MSB   0x1010
 
#define AUDIO_S16MSB   0x9010
 
#define AUDIO_U16   AUDIO_U16LSB
 
#define AUDIO_S16   AUDIO_S16LSB
 
int32 support
#define AUDIO_S32LSB   0x8020
 
#define AUDIO_S32MSB   0x9020
 
#define AUDIO_S32   AUDIO_S32LSB
 
float32 support
#define AUDIO_F32LSB   0x8120
 
#define AUDIO_F32MSB   0x9120
 
#define AUDIO_F32   AUDIO_F32LSB
 
Native audio byte ordering
#define AUDIO_U16SYS   AUDIO_U16LSB
 
#define AUDIO_S16SYS   AUDIO_S16LSB
 
#define AUDIO_S32SYS   AUDIO_S32LSB
 
#define AUDIO_F32SYS   AUDIO_F32LSB
 
Allow change flags

Which audio format changes are allowed when opening a device.

#define SDL_AUDIO_ALLOW_FREQUENCY_CHANGE   0x00000001
 
#define SDL_AUDIO_ALLOW_FORMAT_CHANGE   0x00000002
 
#define SDL_AUDIO_ALLOW_CHANNELS_CHANGE   0x00000004
 
#define SDL_AUDIO_ALLOW_ANY_CHANGE   (SDL_AUDIO_ALLOW_FREQUENCY_CHANGE|SDL_AUDIO_ALLOW_FORMAT_CHANGE|SDL_AUDIO_ALLOW_CHANNELS_CHANGE)
 

Typedefs

typedef Uint16 SDL_AudioFormat
 Audio format flags. More...
 
typedef void(* SDL_AudioCallback) (void *userdata, Uint8 *stream, int len)
 
typedef void(* SDL_AudioFilter) (struct SDL_AudioCVT *cvt, SDL_AudioFormat format)
 
typedef Uint32 SDL_AudioDeviceID
 

Functions

const char * SDL_GetCurrentAudioDriver (void)
 
int SDL_OpenAudio (SDL_AudioSpec *desired, SDL_AudioSpec *obtained)
 
int SDL_GetNumAudioDevices (int iscapture)
 
const char * SDL_GetAudioDeviceName (int index, int iscapture)
 
SDL_AudioDeviceID SDL_OpenAudioDevice (const char *device, int iscapture, const SDL_AudioSpec *desired, SDL_AudioSpec *obtained, int allowed_changes)
 
SDL_AudioSpecSDL_LoadWAV_RW (SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
 
void SDL_FreeWAV (Uint8 *audio_buf)
 
int SDL_BuildAudioCVT (SDL_AudioCVT *cvt, SDL_AudioFormat src_format, Uint8 src_channels, int src_rate, SDL_AudioFormat dst_format, Uint8 dst_channels, int dst_rate)
 
int SDL_ConvertAudio (SDL_AudioCVT *cvt)
 
void SDL_MixAudio (Uint8 *dst, const Uint8 *src, Uint32 len, int volume)
 
void SDL_MixAudioFormat (Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, int volume)
 
int SDL_QueueAudio (SDL_AudioDeviceID dev, const void *data, Uint32 len)
 
Uint32 SDL_DequeueAudio (SDL_AudioDeviceID dev, void *data, Uint32 len)
 
Uint32 SDL_GetQueuedAudioSize (SDL_AudioDeviceID dev)
 
void SDL_ClearQueuedAudio (SDL_AudioDeviceID dev)
 
void SDL_CloseAudio (void)
 
void SDL_CloseAudioDevice (SDL_AudioDeviceID dev)
 
Driver discovery functions

These functions return the list of built in audio drivers, in the order that they are normally initialized by default.

int SDL_GetNumAudioDrivers (void)
 
const char * SDL_GetAudioDriver (int index)
 
Initialization and cleanup

These functions are used internally, and should not be used unless you have a specific need to specify the audio driver you want to use. You should normally use SDL_Init() or SDL_InitSubSystem().

int SDL_AudioInit (const char *driver_name)
 
void SDL_AudioQuit (void)
 
Pause audio functions

These functions pause and unpause the audio callback processing. They should be called with a parameter of 0 after opening the audio device to start playing sound. This is so you can safely initialize data for your callback function after opening the audio device. Silence will be written to the audio device during the pause.

void SDL_PauseAudio (int pause_on)
 
void SDL_PauseAudioDevice (SDL_AudioDeviceID dev, int pause_on)
 
Audio lock functions

The lock manipulated by these functions protects the callback function. During a SDL_LockAudio()/SDL_UnlockAudio() pair, you can be guaranteed that the callback function is not running. Do not call these from the callback function or you will cause deadlock.

void SDL_LockAudio (void)
 
void SDL_LockAudioDevice (SDL_AudioDeviceID dev)
 
void SDL_UnlockAudio (void)
 
void SDL_UnlockAudioDevice (SDL_AudioDeviceID dev)
 

Audio state

Get the current audio state.

enum  SDL_AudioStatus {
  SDL_AUDIO_STOPPED = 0,
  SDL_AUDIO_PLAYING,
  SDL_AUDIO_PAUSED
}
 
SDL_AudioStatus SDL_GetAudioStatus (void)
 
SDL_AudioStatus SDL_GetAudioDeviceStatus (SDL_AudioDeviceID dev)
 

Detailed Description

Access to the raw audio mixing buffer for the SDL library.

Definition in file SDL_audio.h.

Macro Definition Documentation

◆ AUDIO_F32

#define AUDIO_F32   AUDIO_F32LSB

◆ AUDIO_F32LSB

◆ AUDIO_F32MSB

◆ AUDIO_F32SYS

#define AUDIO_F32SYS   AUDIO_F32LSB

Definition at line 125 of file SDL_audio.h.

Referenced by audio_initOpenCloseQuitAudio(), audio_pauseUnpauseAudio(), and main().

◆ AUDIO_S16

◆ AUDIO_S16LSB

◆ AUDIO_S16MSB

◆ AUDIO_S16SYS

◆ AUDIO_S32

#define AUDIO_S32   AUDIO_S32LSB

◆ AUDIO_S32LSB

◆ AUDIO_S32MSB

◆ AUDIO_S32SYS

#define AUDIO_S32SYS   AUDIO_S32LSB

Definition at line 124 of file SDL_audio.h.

◆ AUDIO_S8

◆ AUDIO_U16

#define AUDIO_U16   AUDIO_U16LSB

◆ AUDIO_U16LSB

◆ AUDIO_U16MSB

◆ AUDIO_U16SYS

#define AUDIO_U16SYS   AUDIO_U16LSB

Definition at line 122 of file SDL_audio.h.

◆ AUDIO_U8

◆ SDL_AUDIO_ALLOW_ANY_CHANGE

◆ SDL_AUDIO_ALLOW_CHANNELS_CHANGE

#define SDL_AUDIO_ALLOW_CHANNELS_CHANGE   0x00000004

Definition at line 142 of file SDL_audio.h.

Referenced by open_audio_device().

◆ SDL_AUDIO_ALLOW_FORMAT_CHANGE

#define SDL_AUDIO_ALLOW_FORMAT_CHANGE   0x00000002

Definition at line 141 of file SDL_audio.h.

Referenced by open_audio_device().

◆ SDL_AUDIO_ALLOW_FREQUENCY_CHANGE

#define SDL_AUDIO_ALLOW_FREQUENCY_CHANGE   0x00000001

Definition at line 140 of file SDL_audio.h.

Referenced by open_audio_device().

◆ SDL_AUDIO_BITSIZE

◆ SDL_AUDIO_ISBIGENDIAN

#define SDL_AUDIO_ISBIGENDIAN (   x)    (x & SDL_AUDIO_MASK_ENDIAN)

Definition at line 77 of file SDL_audio.h.

Referenced by SDL_ConvertMono(), SDL_ConvertSurround(), and SDL_ConvertSurround_4().

◆ SDL_AUDIO_ISFLOAT

#define SDL_AUDIO_ISFLOAT (   x)    (x & SDL_AUDIO_MASK_DATATYPE)

Definition at line 76 of file SDL_audio.h.

◆ SDL_AUDIO_ISINT

#define SDL_AUDIO_ISINT (   x)    (!SDL_AUDIO_ISFLOAT(x))

Definition at line 79 of file SDL_audio.h.

◆ SDL_AUDIO_ISLITTLEENDIAN

#define SDL_AUDIO_ISLITTLEENDIAN (   x)    (!SDL_AUDIO_ISBIGENDIAN(x))

Definition at line 80 of file SDL_audio.h.

◆ SDL_AUDIO_ISSIGNED

#define SDL_AUDIO_ISSIGNED (   x)    (x & SDL_AUDIO_MASK_SIGNED)

Definition at line 78 of file SDL_audio.h.

Referenced by SDL_BuildAudioCVT().

◆ SDL_AUDIO_ISUNSIGNED

#define SDL_AUDIO_ISUNSIGNED (   x)    (!SDL_AUDIO_ISSIGNED(x))

Definition at line 81 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_BITSIZE

#define SDL_AUDIO_MASK_BITSIZE   (0xFF)

Definition at line 71 of file SDL_audio.h.

Referenced by SDL_ConvertMono(), SDL_ConvertSurround(), and SDL_ConvertSurround_4().

◆ SDL_AUDIO_MASK_DATATYPE

#define SDL_AUDIO_MASK_DATATYPE   (1<<8)

Definition at line 72 of file SDL_audio.h.

Referenced by SDL_ConvertMono(), SDL_ConvertSurround(), and SDL_ConvertSurround_4().

◆ SDL_AUDIO_MASK_ENDIAN

#define SDL_AUDIO_MASK_ENDIAN   (1<<12)

Definition at line 73 of file SDL_audio.h.

◆ SDL_AUDIO_MASK_SIGNED

#define SDL_AUDIO_MASK_SIGNED   (1<<15)

Definition at line 74 of file SDL_audio.h.

Referenced by SDL_ConvertMono(), SDL_ConvertSurround(), and SDL_ConvertSurround_4().

◆ SDL_AUDIOCVT_PACKED

#define SDL_AUDIOCVT_PACKED

A structure to hold a set of audio conversion filters and buffers.

Definition at line 197 of file SDL_audio.h.

◆ SDL_LoadWAV

#define SDL_LoadWAV (   file,
  spec,
  audio_buf,
  audio_len 
)    SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"),1, spec,audio_buf,audio_len)

Loads a WAV from a file. Compatibility convenience function.

Definition at line 425 of file SDL_audio.h.

Referenced by main().

◆ SDL_MIX_MAXVOLUME

#define SDL_MIX_MAXVOLUME   128

Definition at line 462 of file SDL_audio.h.

Referenced by SDL_MixAudioFormat().

Typedef Documentation

◆ SDL_AudioCallback

typedef void( * SDL_AudioCallback) (void *userdata, Uint8 *stream, int len)

This function is called when the audio device needs more data.

Parameters
userdataAn application-specific parameter saved in the SDL_AudioSpec structure
streamA pointer to the audio data buffer.
lenThe length of that buffer in bytes.

Once the callback returns, the buffer will no longer be valid. Stereo samples are stored in a LRLRLR ordering.

You can choose to avoid callbacks and use SDL_QueueAudio() instead, if you like. Just open your audio device with a NULL callback.

Definition at line 162 of file SDL_audio.h.

◆ SDL_AudioDeviceID

SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 return SDL_AudioCVT SDL_AudioFormat Uint8 int SDL_AudioFormat Uint8 int return Uint8 const Uint8 Uint32 int const char return return return return return return return return Uint32 return Uint32 SDL_Event return SDL_Event int return SDL_EventFilter void SDL_EventFilter void SDL_EventFilter void int return const char const char return SDL_JoystickGUID return int return int return SDL_GameController return int return const char return SDL_GameController SDL_GameControllerAxis return const char return SDL_GameController SDL_GameControllerButton return SDL_GameController SDL_RWops return SDL_TouchID SDL_RWops return int return int return return SDL_Joystick return SDL_Haptic SDL_Haptic return SDL_Haptic return SDL_Haptic SDL_HapticEffect return SDL_Haptic int Uint32 return SDL_Haptic int SDL_Haptic int return SDL_Haptic return SDL_Haptic return SDL_Haptic return SDL_Haptic return const char const char return const char SDL_HintCallback void int return SDL_Joystick return SDL_Joystick return const char return SDL_Joystick return SDL_Joystick return SDL_Joystick return int return SDL_Joystick int return SDL_Joystick int return return return SDL_Scancode return SDL_Scancode return SDL_Keycode return return const char return void int SDL_LogPriority SDL_LogOutputFunction void Uint32 const char const char SDL_Window return int int return SDL_Window int int return SDL_Surface int int return SDL_Cursor return int return SDL_mutex return SDL_mutex return Uint32 return SDL_sem return SDL_sem Uint32 return SDL_sem return SDL_cond SDL_cond return SDL_cond SDL_mutex Uint32 return Uint32 int Uint32 Uint32 Uint32 Uint32 return Uint32 return int return SDL_Palette const SDL_Color int int return const SDL_PixelFormat Uint8 Uint8 Uint8 return Uint32 const SDL_PixelFormat Uint8 Uint8 Uint8 float Uint16 int int return const SDL_Rect const SDL_Rect SDL_Rect return const SDL_Point int const SDL_Rect SDL_Rect return return int int Uint32 SDL_Window SDL_Renderer return SDL_Surface return SDL_Renderer SDL_RendererInfo return SDL_Renderer Uint32 int int int return SDL_Texture Uint32 int int int return SDL_Texture Uint8 Uint8 Uint8 return SDL_Texture Uint8 return SDL_Texture SDL_BlendMode return SDL_Texture const SDL_Rect const Uint8 int const Uint8 int const Uint8 int return SDL_Texture SDL_Renderer SDL_Texture return SDL_Renderer int int return SDL_Renderer const SDL_Rect return SDL_Renderer const SDL_Rect return SDL_Renderer float float return SDL_Renderer Uint8 Uint8 Uint8 Uint8 return SDL_Renderer SDL_BlendMode return SDL_Renderer return SDL_Renderer const SDL_Point int return SDL_Renderer const SDL_Point int return SDL_Renderer const SDL_Rect int return SDL_Renderer const SDL_Rect int return SDL_Renderer SDL_Texture const SDL_Rect const SDL_Rect const double const SDL_Point const SDL_RendererFlip return SDL_Renderer SDL_Renderer SDL_Texture return void int return return SDL_RWops return SDL_RWops return SDL_RWops return SDL_RWops return SDL_RWops Uint16 return SDL_RWops Uint32 return SDL_RWops Uint64 return const char unsigned int unsigned int unsigned int unsigned int Uint32 return SDL_Window SDL_Surface SDL_WindowShapeMode return size_t return void size_t return const char return void size_t size_t int(*) a int return int return int size_t return size_t return const wchar_t return const wchar_t size_t return const char size_t return const char size_t return char return char return const char int return int char int return long char int return Sint64 char int return const char return const char char int return const char char int return const char char return const char const char size_t return const char const char size_t return double return double return double return double return double return double return double int return float return const char const char return SDL_iconv_t const char size_t char size_t return Uint32 int int int Uint32 Uint32 Uint32 Uint32 return SDL_Surface SDL_Surface return SDL_RWops int return SDL_Surface int return SDL_Surface Uint32 return SDL_Surface Uint8 Uint8 Uint8 return SDL_Surface Uint8 return SDL_Surface SDL_BlendMode return SDL_Surface SDL_Rect SDL_Surface Uint32 Uint32 return SDL_Surface const SDL_Rect Uint32 return SDL_Surface const SDL_Rect SDL_Surface SDL_Rect return SDL_Surface const SDL_Rect SDL_Surface const SDL_Rect return SDL_Surface SDL_Rect SDL_Surface SDL_Rect return SDL_Thread return SDL_Thread return SDL_Thread int return SDL_TLSID const void void(*) return return Uint32 SDL_TimerID return int return SDL_TouchID int return return return const char return return int return int return int SDL_DisplayMode return int const SDL_DisplayMode SDL_DisplayMode return SDL_Window const SDL_DisplayMode return SDL_Window return const void return Uint32 return SDL_Window const char SDL_Window SDL_Surface SDL_Window const char return SDL_Window int int SDL_Window int int SDL_Window int int SDL_Window int int SDL_Window SDL_Window SDL_Window SDL_Window Uint32 return SDL_Window return SDL_Window SDL_bool SDL_Window float return SDL_Window const Uint16 const Uint16 const Uint16 return SDL_Window const char return SDL_GLattr int return SDL_Window return return SDL_Window int int return SDL_GLContext SDL_RWops int return return void return int int return double return SDL_bool return int int return SDL_AudioDeviceID const void Uint32 return SDL_AudioDeviceID int float float float return SDL_JoystickID return int SDL_Rect return SDL_Window float return SDL_Window return SDL_Renderer SDL_bool return SDL_AudioDeviceID

SDL Audio Device IDs.

A successful call to SDL_OpenAudio() is always device id 1, and legacy SDL audio APIs assume you want this device ID. SDL_OpenAudioDevice() calls always returns devices >= 2 on success. The legacy calls are good both for backwards compatibility and when you don't care about multiple, specific, or capture devices.

Definition at line 304 of file SDL_audio.h.

◆ SDL_AudioFilter

typedef void( * SDL_AudioFilter) (struct SDL_AudioCVT *cvt, SDL_AudioFormat format)

Definition at line 183 of file SDL_audio.h.

◆ SDL_AudioFormat

Audio format flags.

These are what the 16 bits in SDL_AudioFormat currently mean... (Unspecified bits are always zero).

++-----------------------sample is signed if set
||
||       ++-----------sample is bigendian if set
||       ||
||       ||          ++---sample is float if set
||       ||          ||
||       ||          || +---sample bit size---+
||       ||          || |                     |
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00

There are macros in SDL 2.0 and later to query these bits.

Definition at line 64 of file SDL_audio.h.

Enumeration Type Documentation

◆ SDL_AudioStatus

Enumerator
SDL_AUDIO_STOPPED 
SDL_AUDIO_PLAYING 
SDL_AUDIO_PAUSED 

Definition at line 369 of file SDL_audio.h.

Function Documentation

◆ SDL_AudioInit()

int SDL_AudioInit ( const char *  driver_name)

Definition at line 917 of file SDL_audio.c.

References AudioBootStrap::demand_only, SDL_AudioDriver::desc, AudioBootStrap::desc, SDL_AudioDriverImpl::DetectDevices, SDL_AudioDriver::detectionLock, finish_audio_entry_points_init(), i, SDL_AudioDriver::impl, AudioBootStrap::init, SDL_AudioDriver::name, AudioBootStrap::name, NULL, SDL_AudioQuit(), SDL_CreateMutex, SDL_getenv, SDL_INIT_AUDIO, SDL_SetError, SDL_strlen, SDL_strncasecmp, SDL_WasInit, and SDL_zero.

918 {
919  int i = 0;
920  int initialized = 0;
921  int tried_to_init = 0;
922 
924  SDL_AudioQuit(); /* shutdown driver if already running. */
925  }
926 
929 
930  /* Select the proper audio driver */
931  if (driver_name == NULL) {
932  driver_name = SDL_getenv("SDL_AUDIODRIVER");
933  }
934 
935  for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
936  /* make sure we should even try this driver before doing so... */
937  const AudioBootStrap *backend = bootstrap[i];
938  if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) ||
939  (!driver_name && backend->demand_only)) {
940  continue;
941  }
942 
943  tried_to_init = 1;
945  current_audio.name = backend->name;
946  current_audio.desc = backend->desc;
947  initialized = backend->init(&current_audio.impl);
948  }
949 
950  if (!initialized) {
951  /* specific drivers will set the error message if they fail... */
952  if (!tried_to_init) {
953  if (driver_name) {
954  SDL_SetError("Audio target '%s' not available", driver_name);
955  } else {
956  SDL_SetError("No available audio device");
957  }
958  }
959 
961  return -1; /* No driver was available, so fail. */
962  }
963 
965 
967 
968  /* Make sure we have a list of devices available at startup. */
970 
971  return 0;
972 }
static SDL_AudioDevice * open_devices[16]
Definition: SDL_audio.c:34
const char * name
Definition: SDL_sysaudio.h:120
void(* DetectDevices)(void)
Definition: SDL_sysaudio.h:75
const char * name
Definition: SDL_sysaudio.h:197
#define SDL_CreateMutex
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
#define SDL_strncasecmp
SDL_mutex * detectionLock
Definition: SDL_sysaudio.h:129
const char * desc
Definition: SDL_sysaudio.h:124
void SDL_AudioQuit(void)
Definition: SDL_audio.c:1560
#define SDL_zero(x)
Definition: SDL_stdinc.h:361
#define SDL_getenv
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
static const AudioBootStrap *const bootstrap[]
Definition: SDL_audio.c:67
#define NULL
Definition: begin_code.h:143
#define SDL_SetError
#define SDL_INIT_AUDIO
Definition: SDL.h:76
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:126
#define SDL_strlen
int(* init)(SDL_AudioDriverImpl *impl)
Definition: SDL_sysaudio.h:199
const char * desc
Definition: SDL_sysaudio.h:198
#define SDL_WasInit
static void finish_audio_entry_points_init(void)
Definition: SDL_audio.c:268

◆ SDL_AudioQuit()

void SDL_AudioQuit ( void  )

Definition at line 1560 of file SDL_audio.c.

References close_audio_device(), SDL_AudioDriverImpl::Deinitialize, SDL_AudioDriver::detectionLock, free_device_list(), i, SDL_AudioDriver::impl, SDL_AudioDriver::inputDeviceCount, SDL_AudioDriver::inputDevices, SDL_AudioDriver::name, SDL_AudioDriver::outputDeviceCount, SDL_AudioDriver::outputDevices, SDL_arraysize, SDL_DestroyMutex, and SDL_zero.

Referenced by SDL_AudioInit().

1561 {
1563 
1564  if (!current_audio.name) { /* not initialized?! */
1565  return;
1566  }
1567 
1568  for (i = 0; i < SDL_arraysize(open_devices); i++) {
1570  }
1571 
1574 
1575  /* Free the driver data */
1577 
1579 
1582 }
static SDL_AudioDevice * open_devices[16]
Definition: SDL_audio.c:34
const char * name
Definition: SDL_sysaudio.h:120
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
SDL_mutex * detectionLock
Definition: SDL_sysaudio.h:129
void(* Deinitialize)(void)
Definition: SDL_sysaudio.h:89
#define SDL_zero(x)
Definition: SDL_stdinc.h:361
SDL_AudioDeviceItem * outputDevices
Definition: SDL_sysaudio.h:134
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
static void free_device_list(SDL_AudioDeviceItem **devices, int *devCount)
Definition: SDL_audio.c:347
#define SDL_DestroyMutex
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:126
Uint32 SDL_AudioDeviceID
Definition: SDL_audio.h:304
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:90
SDL_AudioDeviceItem * inputDevices
Definition: SDL_sysaudio.h:135
static void close_audio_device(SDL_AudioDevice *device)
Definition: SDL_audio.c:1079

◆ SDL_BuildAudioCVT()

int SDL_BuildAudioCVT ( SDL_AudioCVT cvt,
SDL_AudioFormat  src_format,
Uint8  src_channels,
int  src_rate,
SDL_AudioFormat  dst_format,
Uint8  dst_channels,
int  dst_rate 
)

This function takes a source format and rate and a destination format and rate, and initializes the cvt structure with information needed by SDL_ConvertAudio() to convert a buffer of audio data from one format to the other.

Returns
-1 if the format conversion is not supported, 0 if there's no conversion needed, or 1 if the audio filter is set up.

Definition at line 998 of file SDL_audiocvt.c.

References SDL_AudioCVT::buf, SDL_AudioCVT::dst_format, SDL_AudioCVT::filter_index, SDL_AudioCVT::filters, SDL_AudioCVT::len, SDL_AudioCVT::len_mult, SDL_AudioCVT::len_ratio, SDL_AudioCVT::needed, NULL, SDL_AudioCVT::rate_incr, SDL_AUDIO_BITSIZE, SDL_AUDIO_ISSIGNED, SDL_BuildAudioResampleCVT(), SDL_BuildAudioTypeCVT(), SDL_ConvertMono(), SDL_ConvertStereo(), SDL_ConvertStrip(), SDL_ConvertStrip_2(), SDL_ConvertSurround(), SDL_ConvertSurround_4(), SDL_InvalidParamError, SDL_SetError, SDL_zerop, and SDL_AudioCVT::src_format.

1001 {
1002  /*
1003  * !!! FIXME: reorder filters based on which grow/shrink the buffer.
1004  * !!! FIXME: ideally, we should do everything that shrinks the buffer
1005  * !!! FIXME: first, so we don't have to process as many bytes in a given
1006  * !!! FIXME: filter and abuse the CPU cache less. This might not be as
1007  * !!! FIXME: good in practice as it sounds in theory, though.
1008  */
1009 
1010  /* Sanity check target pointer */
1011  if (cvt == NULL) {
1012  return SDL_InvalidParamError("cvt");
1013  }
1014 
1015  /* there are no unsigned types over 16 bits, so catch this up front. */
1016  if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
1017  return SDL_SetError("Invalid source format");
1018  }
1019  if ((SDL_AUDIO_BITSIZE(dst_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(dst_fmt))) {
1020  return SDL_SetError("Invalid destination format");
1021  }
1022 
1023  /* prevent possible divisions by zero, etc. */
1024  if ((src_channels == 0) || (dst_channels == 0)) {
1025  return SDL_SetError("Source or destination channels is zero");
1026  }
1027  if ((src_rate == 0) || (dst_rate == 0)) {
1028  return SDL_SetError("Source or destination rate is zero");
1029  }
1030 #ifdef DEBUG_CONVERT
1031  printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
1032  src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate);
1033 #endif
1034 
1035  /* Start off with no conversion necessary */
1036  SDL_zerop(cvt);
1037  cvt->src_format = src_fmt;
1038  cvt->dst_format = dst_fmt;
1039  cvt->needed = 0;
1040  cvt->filter_index = 0;
1041  cvt->filters[0] = NULL;
1042  cvt->len_mult = 1;
1043  cvt->len_ratio = 1.0;
1044  cvt->rate_incr = ((double) dst_rate) / ((double) src_rate);
1045 
1046  /* Convert data types, if necessary. Updates (cvt). */
1047  if (SDL_BuildAudioTypeCVT(cvt, src_fmt, dst_fmt) == -1) {
1048  return -1; /* shouldn't happen, but just in case... */
1049  }
1050 
1051  /* Channel conversion */
1052  if (src_channels != dst_channels) {
1053  if ((src_channels == 1) && (dst_channels > 1)) {
1054  cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
1055  cvt->len_mult *= 2;
1056  src_channels = 2;
1057  cvt->len_ratio *= 2;
1058  }
1059  if ((src_channels == 2) && (dst_channels == 6)) {
1060  cvt->filters[cvt->filter_index++] = SDL_ConvertSurround;
1061  src_channels = 6;
1062  cvt->len_mult *= 3;
1063  cvt->len_ratio *= 3;
1064  }
1065  if ((src_channels == 2) && (dst_channels == 4)) {
1067  src_channels = 4;
1068  cvt->len_mult *= 2;
1069  cvt->len_ratio *= 2;
1070  }
1071  while ((src_channels * 2) <= dst_channels) {
1072  cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
1073  cvt->len_mult *= 2;
1074  src_channels *= 2;
1075  cvt->len_ratio *= 2;
1076  }
1077  if ((src_channels == 6) && (dst_channels <= 2)) {
1078  cvt->filters[cvt->filter_index++] = SDL_ConvertStrip;
1079  src_channels = 2;
1080  cvt->len_ratio /= 3;
1081  }
1082  if ((src_channels == 6) && (dst_channels == 4)) {
1083  cvt->filters[cvt->filter_index++] = SDL_ConvertStrip_2;
1084  src_channels = 4;
1085  cvt->len_ratio /= 2;
1086  }
1087  /* This assumes that 4 channel audio is in the format:
1088  Left {front/back} + Right {front/back}
1089  so converting to L/R stereo works properly.
1090  */
1091  while (((src_channels % 2) == 0) &&
1092  ((src_channels / 2) >= dst_channels)) {
1093  cvt->filters[cvt->filter_index++] = SDL_ConvertMono;
1094  src_channels /= 2;
1095  cvt->len_ratio /= 2;
1096  }
1097  if (src_channels != dst_channels) {
1098  /* Uh oh.. */ ;
1099  }
1100  }
1101 
1102  /* Do rate conversion, if necessary. Updates (cvt). */
1103  if (SDL_BuildAudioResampleCVT(cvt, dst_channels, src_rate, dst_rate) ==
1104  -1) {
1105  return -1; /* shouldn't happen, but just in case... */
1106  }
1107 
1108  /* Set up the filter information */
1109  if (cvt->filter_index != 0) {
1110  cvt->needed = 1;
1111  cvt->src_format = src_fmt;
1112  cvt->dst_format = dst_fmt;
1113  cvt->len = 0;
1114  cvt->buf = NULL;
1115  cvt->filters[cvt->filter_index] = NULL;
1116  }
1117  return (cvt->needed);
1118 }
Uint8 * buf
Definition: SDL_audio.h:206
int filter_index
Definition: SDL_audio.h:212
double len_ratio
Definition: SDL_audio.h:210
#define SDL_AUDIO_ISSIGNED(x)
Definition: SDL_audio.h:78
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
#define SDL_zerop(x)
Definition: SDL_stdinc.h:362
static int SDL_BuildAudioResampleCVT(SDL_AudioCVT *cvt, int dst_channels, int src_rate, int dst_rate)
Definition: SDL_audiocvt.c:944
static void SDL_ConvertStrip_2(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Definition: SDL_audiocvt.c:239
SDL_AudioFilter filters[10]
Definition: SDL_audio.h:211
#define SDL_AUDIO_BITSIZE(x)
Definition: SDL_audio.h:75
SDL_AudioFormat src_format
Definition: SDL_audio.h:203
static void SDL_ConvertSurround(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Definition: SDL_audiocvt.c:328
#define NULL
Definition: begin_code.h:143
static void SDL_ConvertStereo(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Definition: SDL_audiocvt.c:285
#define SDL_SetError
SDL_AudioFormat dst_format
Definition: SDL_audio.h:204
static void SDL_ConvertSurround_4(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Definition: SDL_audiocvt.c:587
double rate_incr
Definition: SDL_audio.h:205
static void SDL_ConvertMono(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Definition: SDL_audiocvt.c:34
static void SDL_ConvertStrip(SDL_AudioCVT *cvt, SDL_AudioFormat format)
Definition: SDL_audiocvt.c:195
static int SDL_BuildAudioTypeCVT(SDL_AudioCVT *cvt, SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
Definition: SDL_audiocvt.c:856

◆ SDL_ClearQueuedAudio()

void SDL_ClearQueuedAudio ( SDL_AudioDeviceID  dev)

Drop any queued audio data. For playback devices, this is any queued data still waiting to be submitted to the hardware. For capture devices, this is any data that was queued by the device that hasn't yet been dequeued by the application.

Immediately after this call, SDL_GetQueuedAudioSize() will return 0. For playback devices, the hardware will start playing silence if more audio isn't queued. Unpaused capture devices will start filling the queue again as soon as they have more data available (which, depending on the state of the hardware and the thread, could be before this function call returns!).

This will not prevent playback of queued audio that's already been sent to the hardware, as we can not undo that, so expect there to be some fraction of a second of audio that might still be heard. This can be useful if you want to, say, drop any pending music during a level change in your game.

You may not queue audio on a device that is using an application-supplied callback; calling this function on such a device is always a no-op. You have to queue audio with SDL_QueueAudio()/SDL_DequeueAudio(), or use the audio callback, but not both.

You should not call SDL_LockAudio() on the device before clearing the queue; SDL handles locking internally for this function.

This function always succeeds and thus returns void.

Parameters
devThe device ID of which to clear the audio queue.
See also
SDL_QueueAudio
SDL_GetQueuedAudioSize

Definition at line 668 of file SDL_audio.c.

References SDL_AudioDevice::buffer_queue_head, SDL_AudioDevice::buffer_queue_pool, SDL_AudioDevice::buffer_queue_tail, free_audio_queue(), get_audio_device(), SDL_AudioDriver::impl, SDL_AudioDriverImpl::LockDevice, SDL_AudioBufferQueue::next, NULL, SDL_AudioDevice::queued_bytes, SDLCALL, and SDL_AudioDriverImpl::UnlockDevice.

669 {
670  SDL_AudioDevice *device = get_audio_device(devid);
671  SDL_AudioBufferQueue *packet;
672 
673  if (!device) {
674  return; /* nothing to do. */
675  }
676 
677  /* Blank out the device and release the mutex. Free it afterwards. */
678  current_audio.impl.LockDevice(device);
679 
680  /* merge the available pool and the current queue into one list. */
681  packet = device->buffer_queue_head;
682  if (packet) {
683  device->buffer_queue_tail->next = device->buffer_queue_pool;
684  } else {
685  packet = device->buffer_queue_pool;
686  }
687 
688  /* Remove the queued packets from the device. */
689  device->buffer_queue_tail = NULL;
690  device->buffer_queue_head = NULL;
691  device->queued_bytes = 0;
692  device->buffer_queue_pool = packet;
693 
694  /* Keep up to two packets in the pool to reduce future malloc pressure. */
695  if (packet) {
696  if (!packet->next) {
697  packet = NULL; /* one packet (the only one) for the pool. */
698  } else {
699  SDL_AudioBufferQueue *next = packet->next->next;
700  packet->next->next = NULL; /* two packets for the pool. */
701  packet = next; /* rest will be freed. */
702  }
703  }
704 
706 
707  /* free any extra packets we didn't keep in the pool. */
708  free_audio_queue(packet);
709 }
const struct SDL_AssertData * next
Definition: SDL_assert.h:117
SDL_AudioBufferQueue * buffer_queue_pool
Definition: SDL_sysaudio.h:184
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
void(* UnlockDevice)(_THIS)
Definition: SDL_sysaudio.h:87
SDL_AudioBufferQueue * buffer_queue_head
Definition: SDL_sysaudio.h:182
SDL_AudioBufferQueue * buffer_queue_tail
Definition: SDL_sysaudio.h:183
struct SDL_AudioBufferQueue * next
Definition: SDL_sysaudio.h:70
void(* LockDevice)(_THIS)
Definition: SDL_sysaudio.h:86
#define NULL
Definition: begin_code.h:143
static void free_audio_queue(SDL_AudioBufferQueue *packet)
Definition: SDL_audio.c:453
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:126
static SDL_AudioDevice * get_audio_device(SDL_AudioDeviceID id)
Definition: SDL_audio.c:141

◆ SDL_CloseAudio()

void SDL_CloseAudio ( void  )

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

Definition at line 1554 of file SDL_audio.c.

References SDL_CloseAudioDevice().

1555 {
1557 }
void SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
Definition: SDL_audio.c:1548

◆ SDL_CloseAudioDevice()

void SDL_CloseAudioDevice ( SDL_AudioDeviceID  dev)

Definition at line 1548 of file SDL_audio.c.

References close_audio_device(), and get_audio_device().

Referenced by SDL_CloseAudio().

1549 {
1551 }
static SDL_AudioDevice * get_audio_device(SDL_AudioDeviceID id)
Definition: SDL_audio.c:141
static void close_audio_device(SDL_AudioDevice *device)
Definition: SDL_audio.c:1079

◆ SDL_ConvertAudio()

int SDL_ConvertAudio ( SDL_AudioCVT cvt)

Once you have initialized the cvt structure using SDL_BuildAudioCVT(), created an audio buffer cvt->buf, and filled it with cvt->len bytes of audio data in the source format, this function will convert it in-place to the desired format.

The data conversion may expand the size of the audio data, so the buffer cvt->buf should be allocated after the cvt structure is initialized by SDL_BuildAudioCVT(), and should be cvt->len*cvt->len_mult bytes long.

Definition at line 814 of file SDL_audiocvt.c.

References SDL_AudioCVT::buf, SDL_AudioCVT::filter_index, SDL_AudioCVT::filters, SDL_AudioCVT::len, SDL_AudioCVT::len_cvt, NULL, SDL_SetError, and SDL_AudioCVT::src_format.

815 {
816  /* !!! FIXME: (cvt) should be const; stack-copy it here. */
817  /* !!! FIXME: (actually, we can't...len_cvt needs to be updated. Grr.) */
818 
819  /* Make sure there's data to convert */
820  if (cvt->buf == NULL) {
821  SDL_SetError("No buffer allocated for conversion");
822  return (-1);
823  }
824  /* Return okay if no conversion is necessary */
825  cvt->len_cvt = cvt->len;
826  if (cvt->filters[0] == NULL) {
827  return (0);
828  }
829 
830  /* Set up the conversion and go! */
831  cvt->filter_index = 0;
832  cvt->filters[0] (cvt, cvt->src_format);
833  return (0);
834 }
Uint8 * buf
Definition: SDL_audio.h:206
int filter_index
Definition: SDL_audio.h:212
SDL_AudioFilter filters[10]
Definition: SDL_audio.h:211
SDL_AudioFormat src_format
Definition: SDL_audio.h:203
#define NULL
Definition: begin_code.h:143
#define SDL_SetError

◆ SDL_DequeueAudio()

Uint32 SDL_DequeueAudio ( SDL_AudioDeviceID  dev,
void data,
Uint32  len 
)

Dequeue more audio on non-callback devices.

(If you are looking to queue audio for output on a non-callback playback device, you want SDL_QueueAudio() instead. This will always return 0 if you use it with playback devices.)

SDL offers two ways to retrieve audio from a capture device: you can either supply a callback that SDL triggers with some frequency as the device records more audio data, (push method), or you can supply no callback, and then SDL will expect you to retrieve data at regular intervals (pull method) with this function.

There are no limits on the amount of data you can queue, short of exhaustion of address space. Data from the device will keep queuing as necessary without further intervention from you. This means you will eventually run out of memory if you aren't routinely dequeueing data.

Capture devices will not queue data when paused; if you are expecting to not need captured audio for some length of time, use SDL_PauseAudioDevice() to stop the capture device from queueing more data. This can be useful during, say, level loading times. When unpaused, capture devices will start queueing data from that point, having flushed any capturable data available while paused.

This function is thread-safe, but dequeueing from the same device from two threads at once does not promise which thread will dequeued data first.

You may not dequeue audio from a device that is using an application-supplied callback; doing so returns an error. You have to use the audio callback, or dequeue audio with this function, but not both.

You should not call SDL_LockAudio() on the device before queueing; SDL handles locking internally for this function.

Parameters
devThe device ID from which we will dequeue audio.
dataA pointer into where audio data should be copied.
lenThe number of bytes (not samples!) to which (data) points.
Returns
number of bytes dequeued, which could be less than requested.
See also
SDL_GetQueuedAudioSize
SDL_ClearQueuedAudio

Definition at line 625 of file SDL_audio.c.

References SDL_AudioSpec::callback, dequeue_audio_from_device(), get_audio_device(), SDL_AudioDriver::impl, SDL_AudioDevice::iscapture, SDL_AudioDriverImpl::LockDevice, SDL_BufferQueueFillCallback(), SDL_AudioDevice::spec, and SDL_AudioDriverImpl::UnlockDevice.

626 {
627  SDL_AudioDevice *device = get_audio_device(devid);
628  Uint32 rc;
629 
630  if ( (len == 0) || /* nothing to do? */
631  (!device) || /* called with bogus device id */
632  (!device->iscapture) || /* playback devices can't dequeue */
633  (device->spec.callback != SDL_BufferQueueFillCallback) ) { /* not set for queueing */
634  return 0; /* just report zero bytes dequeued. */
635  }
636 
637  current_audio.impl.LockDevice(device);
638  rc = dequeue_audio_from_device(device, data, len);
640  return rc;
641 }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
SDL_AudioSpec spec
Definition: SDL_sysaudio.h:156
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
void(* UnlockDevice)(_THIS)
Definition: SDL_sysaudio.h:87
GLenum GLsizei len
SDL_bool iscapture
Definition: SDL_sysaudio.h:169
static Uint32 dequeue_audio_from_device(SDL_AudioDevice *device, Uint8 *stream, Uint32 len)
Definition: SDL_audio.c:531
SDL_AudioCallback callback
Definition: SDL_audio.h:177
void(* LockDevice)(_THIS)
Definition: SDL_sysaudio.h:86
static void SDL_BufferQueueFillCallback(void *userdata, Uint8 *stream, int len)
Definition: SDL_audio.c:586
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:126
static SDL_AudioDevice * get_audio_device(SDL_AudioDeviceID id)
Definition: SDL_audio.c:141

◆ SDL_FreeWAV()

void SDL_FreeWAV ( Uint8 audio_buf)

This function frees data previously allocated with SDL_LoadWAV_RW()

Definition at line 626 of file SDL_wave.c.

References SDL_free().

627 {
628  SDL_free(audio_buf);
629 }
void SDL_free(void *mem)

◆ SDL_GetAudioDeviceName()

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 1039 of file SDL_audio.c.

References SDL_AudioDriver::detectionLock, SDL_AudioDriverImpl::HasCaptureSupport, i, SDL_AudioDriver::impl, SDL_AudioDriver::inputDeviceCount, SDL_AudioDriver::inputDevices, SDL_AudioDeviceItem::next, NULL, SDL_AudioDriver::outputDeviceCount, SDL_AudioDriver::outputDevices, retval, SDL_assert, SDL_INIT_AUDIO, SDL_LockMutex, SDL_SetError, SDL_UnlockMutex, and SDL_WasInit.

1040 {
1041  const char *retval = NULL;
1042 
1043  if (!SDL_WasInit(SDL_INIT_AUDIO)) {
1044  SDL_SetError("Audio subsystem is not initialized");
1045  return NULL;
1046  }
1047 
1048  if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
1049  SDL_SetError("No capture support");
1050  return NULL;
1051  }
1052 
1053  if (index >= 0) {
1054  SDL_AudioDeviceItem *item;
1055  int i;
1056 
1060  if (index < i) {
1061  for (i--; i > index; i--, item = item->next) {
1062  SDL_assert(item != NULL);
1063  }
1064  SDL_assert(item != NULL);
1065  retval = item->name;
1066  }
1068  }
1069 
1070  if (retval == NULL) {
1071  SDL_SetError("No such device");
1072  }
1073 
1074  return retval;
1075 }
#define SDL_LockMutex
struct SDL_AudioDeviceItem * next
Definition: SDL_sysaudio.h:107
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
SDL_mutex * detectionLock
Definition: SDL_sysaudio.h:129
SDL_bool retval
GLuint index
SDL_AudioDeviceItem * outputDevices
Definition: SDL_sysaudio.h:134
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define SDL_assert(condition)
Definition: SDL_assert.h:167
#define NULL
Definition: begin_code.h:143
#define SDL_SetError
#define SDL_INIT_AUDIO
Definition: SDL.h:76
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:126
#define SDL_UnlockMutex
#define SDL_WasInit
SDL_AudioDeviceItem * inputDevices
Definition: SDL_sysaudio.h:135

◆ SDL_GetAudioDeviceStatus()

SDL_AudioStatus SDL_GetAudioDeviceStatus ( SDL_AudioDeviceID  dev)

Definition at line 1476 of file SDL_audio.c.

References SDL_AudioDevice::enabled, get_audio_device(), SDL_AudioDevice::paused, SDL_AtomicGet, SDL_AUDIO_PAUSED, SDL_AUDIO_PLAYING, and SDL_AUDIO_STOPPED.

Referenced by SDL_GetAudioStatus().

1477 {
1478  SDL_AudioDevice *device = get_audio_device(devid);
1480  if (device && SDL_AtomicGet(&device->enabled)) {
1481  if (SDL_AtomicGet(&device->paused)) {
1482  status = SDL_AUDIO_PAUSED;
1483  } else {
1484  status = SDL_AUDIO_PLAYING;
1485  }
1486  }
1487  return status;
1488 }
SDL_atomic_t enabled
Definition: SDL_sysaudio.h:167
SDL_atomic_t paused
Definition: SDL_sysaudio.h:168
SDL_AudioStatus
Definition: SDL_audio.h:369
#define SDL_AtomicGet
static SDL_AudioDevice * get_audio_device(SDL_AudioDeviceID id)
Definition: SDL_audio.c:141

◆ SDL_GetAudioDriver()

const char* SDL_GetAudioDriver ( int  index)

Definition at line 908 of file SDL_audio.c.

References AudioBootStrap::name, NULL, and SDL_GetNumAudioDrivers().

909 {
910  if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
911  return bootstrap[index]->name;
912  }
913  return NULL;
914 }
int SDL_GetNumAudioDrivers(void)
Definition: SDL_audio.c:902
const char * name
Definition: SDL_sysaudio.h:197
GLuint index
static const AudioBootStrap *const bootstrap[]
Definition: SDL_audio.c:67
#define NULL
Definition: begin_code.h:143

◆ SDL_GetAudioStatus()

SDL_AudioStatus SDL_GetAudioStatus ( void  )

Definition at line 1492 of file SDL_audio.c.

References SDL_GetAudioDeviceStatus().

1493 {
1494  return SDL_GetAudioDeviceStatus(1);
1495 }
SDL_AudioStatus SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid)
Definition: SDL_audio.c:1476

◆ SDL_GetCurrentAudioDriver()

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 978 of file SDL_audio.c.

References SDL_AudioDriver::name.

979 {
980  return current_audio.name;
981 }
const char * name
Definition: SDL_sysaudio.h:120
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33

◆ SDL_GetNumAudioDevices()

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 1013 of file SDL_audio.c.

References SDL_AudioDriver::captureDevicesRemoved, clean_out_device_list(), SDL_AudioDriver::detectionLock, SDL_AudioDriver::inputDeviceCount, SDL_AudioDriver::inputDevices, SDL_AudioDriver::outputDeviceCount, SDL_AudioDriver::outputDevices, SDL_AudioDriver::outputDevicesRemoved, retval, SDL_FALSE, SDL_INIT_AUDIO, SDL_LockMutex, SDL_UnlockMutex, and SDL_WasInit.

1014 {
1015  int retval = 0;
1016 
1017  if (!SDL_WasInit(SDL_INIT_AUDIO)) {
1018  return -1;
1019  }
1020 
1022  if (iscapture && current_audio.captureDevicesRemoved) {
1024  }
1025 
1026  if (!iscapture && current_audio.outputDevicesRemoved) {
1029  }
1030 
1033 
1034  return retval;
1035 }
#define SDL_LockMutex
SDL_bool captureDevicesRemoved
Definition: SDL_sysaudio.h:130
static void clean_out_device_list(SDL_AudioDeviceItem **devices, int *devCount, SDL_bool *removedFlag)
Definition: SDL_audio.c:985
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
SDL_mutex * detectionLock
Definition: SDL_sysaudio.h:129
SDL_bool retval
SDL_AudioDeviceItem * outputDevices
Definition: SDL_sysaudio.h:134
SDL_bool outputDevicesRemoved
Definition: SDL_sysaudio.h:131
#define SDL_INIT_AUDIO
Definition: SDL.h:76
#define SDL_UnlockMutex
#define SDL_WasInit
SDL_AudioDeviceItem * inputDevices
Definition: SDL_sysaudio.h:135

◆ SDL_GetNumAudioDrivers()

int SDL_GetNumAudioDrivers ( void  )

Definition at line 902 of file SDL_audio.c.

References SDL_arraysize.

Referenced by SDL_GetAudioDriver().

903 {
904  return SDL_arraysize(bootstrap) - 1;
905 }
static const AudioBootStrap *const bootstrap[]
Definition: SDL_audio.c:67
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:90

◆ SDL_GetQueuedAudioSize()

Uint32 SDL_GetQueuedAudioSize ( SDL_AudioDeviceID  dev)

Get the number of bytes of still-queued audio.

For playback device:

This is the number of bytes that have been queued for playback with SDL_QueueAudio(), but have not yet been sent to the hardware. This number may shrink at any time, so this only informs of pending data.

Once we've sent it to the hardware, this function can not decide the exact byte boundary of what has been played. It's possible that we just gave the hardware several kilobytes right before you called this function, but it hasn't played any of it yet, or maybe half of it, etc.

For capture devices:

This is the number of bytes that have been captured by the device and are waiting for you to dequeue. This number may grow at any time, so this only informs of the lower-bound of available data.

You may not queue audio on a device that is using an application-supplied callback; calling this function on such a device always returns 0. You have to queue audio with SDL_QueueAudio()/SDL_DequeueAudio(), or use the audio callback, but not both.

You should not call SDL_LockAudio() on the device before querying; SDL handles locking internally for this function.

Parameters
devThe device ID of which we will query queued audio size.
Returns
Number of bytes (not samples!) of queued audio.
See also
SDL_QueueAudio
SDL_ClearQueuedAudio

Definition at line 644 of file SDL_audio.c.

References SDL_AudioSpec::callback, get_audio_device(), SDL_AudioDriverImpl::GetPendingBytes, SDL_AudioDriver::impl, SDL_AudioDriverImpl::LockDevice, SDL_AudioDevice::queued_bytes, retval, SDL_BufferQueueDrainCallback(), SDL_BufferQueueFillCallback(), SDL_AudioDevice::spec, and SDL_AudioDriverImpl::UnlockDevice.

645 {
646  Uint32 retval = 0;
647  SDL_AudioDevice *device = get_audio_device(devid);
648 
649  if (!device) {
650  return 0;
651  }
652 
653  /* Nothing to do unless we're set up for queueing. */
654  if (device->spec.callback == SDL_BufferQueueDrainCallback) {
655  current_audio.impl.LockDevice(device);
656  retval = device->queued_bytes + current_audio.impl.GetPendingBytes(device);
658  } else if (device->spec.callback == SDL_BufferQueueFillCallback) {
659  current_audio.impl.LockDevice(device);
660  retval = device->queued_bytes;
662  }
663 
664  return retval;
665 }
SDL_AudioSpec spec
Definition: SDL_sysaudio.h:156
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
void(* UnlockDevice)(_THIS)
Definition: SDL_sysaudio.h:87
SDL_bool retval
SDL_AudioCallback callback
Definition: SDL_audio.h:177
void(* LockDevice)(_THIS)
Definition: SDL_sysaudio.h:86
static void SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int len)
Definition: SDL_audio.c:565
static void SDL_BufferQueueFillCallback(void *userdata, Uint8 *stream, int len)
Definition: SDL_audio.c:586
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:126
int(* GetPendingBytes)(_THIS)
Definition: SDL_sysaudio.h:80
static SDL_AudioDevice * get_audio_device(SDL_AudioDeviceID id)
Definition: SDL_audio.c:141

◆ SDL_LoadWAV_RW()

SDL_AudioSpec* SDL_LoadWAV_RW ( SDL_RWops src,
int  freesrc,
SDL_AudioSpec spec,
Uint8 **  audio_buf,
Uint32 audio_len 
)

This function loads a WAVE from the data source, automatically freeing that source if freesrc is non-zero. For example, to load a WAVE file, you could do:

SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, ...);

If this function succeeds, it returns the given SDL_AudioSpec, filled with the audio data format of the wave data, and sets *audio_buf to a malloc()'d buffer containing the audio data, and sets *audio_len to the length of that audio buffer, in bytes. You need to free the audio buffer with SDL_FreeWAV() when you are done with it.

This function returns NULL and sets the SDL error message if the wave file cannot be opened, uses an unknown data format, or is corrupt. Currently raw and MS-ADPCM WAVE files are supported.

Definition at line 431 of file SDL_wave.c.

References AUDIO_F32, AUDIO_S16, AUDIO_S32, AUDIO_U8, BEXT, WaveFMT::bitspersample, WaveFMT::channels, SDL_AudioSpec::channels, DATA, Chunk::data, done, WaveFMT::encoding, FACT, FMT, SDL_AudioSpec::format, SDL_AudioSpec::freq, WaveFMT::frequency, IEEE_FLOAT_CODE, IMA_ADPCM_CODE, IMA_ADPCM_decode(), InitIMA_ADPCM(), InitMS_ADPCM(), JUNK, Chunk::length, LIST, Chunk::magic, MP3_CODE, MS_ADPCM_CODE, MS_ADPCM_decode(), NULL, PCM_CODE, ReadChunk(), RIFF, RW_SEEK_CUR, SDL_AudioSpec::samples, SDL_AUDIO_BITSIZE, SDL_free(), SDL_ReadLE32, SDL_RWclose, SDL_RWseek, SDL_SetError, SDL_SwapLE16, SDL_SwapLE32, SDL_zero, SDL_zerop, and WAVE.

433 {
434  int was_error;
435  Chunk chunk;
436  int lenread;
437  int IEEE_float_encoded, MS_ADPCM_encoded, IMA_ADPCM_encoded;
438  int samplesize;
439 
440  /* WAV magic header */
441  Uint32 RIFFchunk;
442  Uint32 wavelen = 0;
443  Uint32 WAVEmagic;
444  Uint32 headerDiff = 0;
445 
446  /* FMT chunk */
447  WaveFMT *format = NULL;
448 
449  SDL_zero(chunk);
450 
451  /* Make sure we are passed a valid data source */
452  was_error = 0;
453  if (src == NULL) {
454  was_error = 1;
455  goto done;
456  }
457 
458  /* Check the magic header */
459  RIFFchunk = SDL_ReadLE32(src);
460  wavelen = SDL_ReadLE32(src);
461  if (wavelen == WAVE) { /* The RIFFchunk has already been read */
462  WAVEmagic = wavelen;
463  wavelen = RIFFchunk;
464  RIFFchunk = RIFF;
465  } else {
466  WAVEmagic = SDL_ReadLE32(src);
467  }
468  if ((RIFFchunk != RIFF) || (WAVEmagic != WAVE)) {
469  SDL_SetError("Unrecognized file type (not WAVE)");
470  was_error = 1;
471  goto done;
472  }
473  headerDiff += sizeof(Uint32); /* for WAVE */
474 
475  /* Read the audio data format chunk */
476  chunk.data = NULL;
477  do {
478  SDL_free(chunk.data);
479  chunk.data = NULL;
480  lenread = ReadChunk(src, &chunk);
481  if (lenread < 0) {
482  was_error = 1;
483  goto done;
484  }
485  /* 2 Uint32's for chunk header+len, plus the lenread */
486  headerDiff += lenread + 2 * sizeof(Uint32);
487  } while ((chunk.magic == FACT) || (chunk.magic == LIST) || (chunk.magic == BEXT) || (chunk.magic == JUNK));
488 
489  /* Decode the audio data format */
490  format = (WaveFMT *) chunk.data;
491  if (chunk.magic != FMT) {
492  SDL_SetError("Complex WAVE files not supported");
493  was_error = 1;
494  goto done;
495  }
496  IEEE_float_encoded = MS_ADPCM_encoded = IMA_ADPCM_encoded = 0;
497  switch (SDL_SwapLE16(format->encoding)) {
498  case PCM_CODE:
499  /* We can understand this */
500  break;
501  case IEEE_FLOAT_CODE:
502  IEEE_float_encoded = 1;
503  /* We can understand this */
504  break;
505  case MS_ADPCM_CODE:
506  /* Try to understand this */
507  if (InitMS_ADPCM(format) < 0) {
508  was_error = 1;
509  goto done;
510  }
511  MS_ADPCM_encoded = 1;
512  break;
513  case IMA_ADPCM_CODE:
514  /* Try to understand this */
515  if (InitIMA_ADPCM(format,lenread) < 0) {
516  was_error = 1;
517  goto done;
518  }
519  IMA_ADPCM_encoded = 1;
520  break;
521  case MP3_CODE:
522  SDL_SetError("MPEG Layer 3 data not supported");
523  was_error = 1;
524  goto done;
525  default:
526  SDL_SetError("Unknown WAVE data format: 0x%.4x",
527  SDL_SwapLE16(format->encoding));
528  was_error = 1;
529  goto done;
530  }
531  SDL_zerop(spec);
532  spec->freq = SDL_SwapLE32(format->frequency);
533 
534  if (IEEE_float_encoded) {
535  if ((SDL_SwapLE16(format->bitspersample)) != 32) {
536  was_error = 1;
537  } else {
538  spec->format = AUDIO_F32;
539  }
540  } else {
541  switch (SDL_SwapLE16(format->bitspersample)) {
542  case 4:
543  if (MS_ADPCM_encoded || IMA_ADPCM_encoded) {
544  spec->format = AUDIO_S16;
545  } else {
546  was_error = 1;
547  }
548  break;
549  case 8:
550  spec->format = AUDIO_U8;
551  break;
552  case 16:
553  spec->format = AUDIO_S16;
554  break;
555  case 32:
556  spec->format = AUDIO_S32;
557  break;
558  default:
559  was_error = 1;
560  break;
561  }
562  }
563 
564  if (was_error) {
565  SDL_SetError("Unknown %d-bit PCM data format",
566  SDL_SwapLE16(format->bitspersample));
567  goto done;
568  }
569  spec->channels = (Uint8) SDL_SwapLE16(format->channels);
570  spec->samples = 4096; /* Good default buffer size */
571 
572  /* Read the audio data chunk */
573  *audio_buf = NULL;
574  do {
575  SDL_free(*audio_buf);
576  *audio_buf = NULL;
577  lenread = ReadChunk(src, &chunk);
578  if (lenread < 0) {
579  was_error = 1;
580  goto done;
581  }
582  *audio_len = lenread;
583  *audio_buf = chunk.data;
584  if (chunk.magic != DATA)
585  headerDiff += lenread + 2 * sizeof(Uint32);
586  } while (chunk.magic != DATA);
587  headerDiff += 2 * sizeof(Uint32); /* for the data chunk and len */
588 
589  if (MS_ADPCM_encoded) {
590  if (MS_ADPCM_decode(audio_buf, audio_len) < 0) {
591  was_error = 1;
592  goto done;
593  }
594  }
595  if (IMA_ADPCM_encoded) {
596  if (IMA_ADPCM_decode(audio_buf, audio_len) < 0) {
597  was_error = 1;
598  goto done;
599  }
600  }
601 
602  /* Don't return a buffer that isn't a multiple of samplesize */
603  samplesize = ((SDL_AUDIO_BITSIZE(spec->format)) / 8) * spec->channels;
604  *audio_len &= ~(samplesize - 1);
605 
606  done:
607  SDL_free(format);
608  if (src) {
609  if (freesrc) {
610  SDL_RWclose(src);
611  } else {
612  /* seek to the end of the file (given by the RIFF chunk) */
613  SDL_RWseek(src, wavelen - chunk.length - headerDiff, RW_SEEK_CUR);
614  }
615  }
616  if (was_error) {
617  spec = NULL;
618  }
619  return (spec);
620 }
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
#define PCM_CODE
Definition: SDL_wave.h:36
#define RIFF
Definition: SDL_wave.h:28
#define LIST
Definition: SDL_wave.h:31
#define MP3_CODE
Definition: SDL_wave.h:40
Definition: SDL_wave.h:60
#define SDL_ReadLE32
Uint32 length
Definition: SDL_wave.h:63
Uint16 samples
Definition: SDL_audio.h:174
static int InitIMA_ADPCM(WaveFMT *format, int length)
Definition: SDL_wave.c:250
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
Uint16 encoding
Definition: SDL_wave.h:51
#define SDL_zerop(x)
Definition: SDL_stdinc.h:362
static int MS_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
Definition: SDL_wave.c:119
Uint16 channels
Definition: SDL_wave.h:52
#define FMT
Definition: SDL_wave.h:34
#define SDL_RWseek(ctx, offset, whence)
Definition: SDL_rwops.h:185
#define AUDIO_U8
Definition: SDL_audio.h:89
#define DATA
Definition: SDL_wave.h:35
#define FACT
Definition: SDL_wave.h:30
Uint8 channels
Definition: SDL_audio.h:172
static int IMA_ADPCM_decode(Uint8 **audio_buf, Uint32 *audio_len)
Definition: SDL_wave.c:362
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
void SDL_free(void *mem)
#define SDL_AUDIO_BITSIZE(x)
Definition: SDL_audio.h:75
#define SDL_SwapLE32(X)
Definition: SDL_endian.h:212
int done
Definition: checkkeys.c:28
#define AUDIO_S32
Definition: SDL_audio.h:105
#define WAVE
Definition: SDL_wave.h:29
#define JUNK
Definition: SDL_wave.h:33
#define SDL_zero(x)
Definition: SDL_stdinc.h:361
Uint8 * data
Definition: SDL_wave.h:64
#define NULL
Definition: begin_code.h:143
#define IEEE_FLOAT_CODE
Definition: SDL_wave.h:38
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
#define IMA_ADPCM_CODE
Definition: SDL_wave.h:39
#define SDL_SetError
SDL_AudioFormat format
Definition: SDL_audio.h:171
static int InitMS_ADPCM(WaveFMT *format)
Definition: SDL_wave.c:49
Uint32 magic
Definition: SDL_wave.h:62
#define AUDIO_S16
Definition: SDL_audio.h:96
Uint32 frequency
Definition: SDL_wave.h:53
#define RW_SEEK_CUR
Definition: SDL_rwops.h:175
Uint16 bitspersample
Definition: SDL_wave.h:56
#define AUDIO_F32
Definition: SDL_audio.h:114
#define SDL_SwapLE16(X)
Definition: SDL_endian.h:211
#define BEXT
Definition: SDL_wave.h:32
static int ReadChunk(SDL_RWops *src, Chunk *chunk)
Definition: SDL_wave.c:632
#define MS_ADPCM_CODE
Definition: SDL_wave.h:37

◆ SDL_LockAudio()

void SDL_LockAudio ( void  )

Definition at line 1526 of file SDL_audio.c.

References SDL_LockAudioDevice().

1527 {
1529 }
void SDL_LockAudioDevice(SDL_AudioDeviceID devid)
Definition: SDL_audio.c:1516

◆ SDL_LockAudioDevice()

void SDL_LockAudioDevice ( SDL_AudioDeviceID  dev)

Definition at line 1516 of file SDL_audio.c.

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

Referenced by SDL_LockAudio().

1517 {
1518  /* Obtain a lock on the mixing buffers */
1519  SDL_AudioDevice *device = get_audio_device(devid);
1520  if (device) {
1521  current_audio.impl.LockDevice(device);
1522  }
1523 }
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
void(* LockDevice)(_THIS)
Definition: SDL_sysaudio.h:86
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:126
static SDL_AudioDevice * get_audio_device(SDL_AudioDeviceID id)
Definition: SDL_audio.c:141

◆ SDL_MixAudio()

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 1653 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.

1654 {
1655  /* Mix the user-level audio format */
1656  SDL_AudioDevice *device = get_audio_device(1);
1657  if (device != NULL) {
1659  if (device->convert.needed) {
1660  format = device->convert.src_format;
1661  } else {
1662  format = device->spec.format;
1663  }
1664  SDL_MixAudioFormat(dst, src, format, len, volume);
1665  }
1666 }
GLenum GLenum dst
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
#define SDL_MixAudioFormat
SDL_AudioSpec spec
Definition: SDL_sysaudio.h:156
Uint16 SDL_AudioFormat
Audio format flags.
Definition: SDL_audio.h:64
GLenum GLsizei len
SDL_AudioFormat src_format
Definition: SDL_audio.h:203
#define NULL
Definition: begin_code.h:143
SDL_AudioFormat format
Definition: SDL_audio.h:171
SDL_AudioCVT convert
Definition: SDL_sysaudio.h:159
GLenum src
static SDL_AudioDevice * get_audio_device(SDL_AudioDeviceID id)
Definition: SDL_audio.c:141

◆ SDL_MixAudioFormat()

void SDL_MixAudioFormat ( Uint8 dst,
const Uint8 src,
SDL_AudioFormat  format,
Uint32  len,
int  volume 
)

This works like SDL_MixAudio(), but you specify the audio format instead of using the format of audio device 1. Thus it can be used when no audio device is open at all.

Definition at line 90 of file SDL_mixer.c.

References ADJUST_VOLUME, ADJUST_VOLUME_U8, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S8, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, F, mix8, SDL_MIX_MAXVOLUME, SDL_SetError, SDL_SwapBE32, SDL_SwapFloatBE, SDL_SwapFloatLE, and SDL_SwapLE32.

92 {
93  if (volume == 0) {
94  return;
95  }
96 
97  switch (format) {
98 
99  case AUDIO_U8:
100  {
101 #if defined(__GNUC__) && defined(__M68000__) && !defined(__mcoldfire__) && defined(SDL_ASSEMBLY_ROUTINES)
102  SDL_MixAudio_m68k_U8((char *) dst, (char *) src,
103  (unsigned long) len, (long) volume,
104  (char *) mix8);
105 #else
106  Uint8 src_sample;
107 
108  while (len--) {
109  src_sample = *src;
110  ADJUST_VOLUME_U8(src_sample, volume);
111  *dst = mix8[*dst + src_sample];
112  ++dst;
113  ++src;
114  }
115 #endif
116  }
117  break;
118 
119  case AUDIO_S8:
120  {
121  Sint8 *dst8, *src8;
122  Sint8 src_sample;
123  int dst_sample;
124  const int max_audioval = ((1 << (8 - 1)) - 1);
125  const int min_audioval = -(1 << (8 - 1));
126 
127  src8 = (Sint8 *) src;
128  dst8 = (Sint8 *) dst;
129  while (len--) {
130  src_sample = *src8;
131  ADJUST_VOLUME(src_sample, volume);
132  dst_sample = *dst8 + src_sample;
133  if (dst_sample > max_audioval) {
134  *dst8 = max_audioval;
135  } else if (dst_sample < min_audioval) {
136  *dst8 = min_audioval;
137  } else {
138  *dst8 = dst_sample;
139  }
140  ++dst8;
141  ++src8;
142  }
143  }
144  break;
145 
146  case AUDIO_S16LSB:
147  {
148  Sint16 src1, src2;
149  int dst_sample;
150  const int max_audioval = ((1 << (16 - 1)) - 1);
151  const int min_audioval = -(1 << (16 - 1));
152 
153  len /= 2;
154  while (len--) {
155  src1 = ((src[1]) << 8 | src[0]);
156  ADJUST_VOLUME(src1, volume);
157  src2 = ((dst[1]) << 8 | dst[0]);
158  src += 2;
159  dst_sample = src1 + src2;
160  if (dst_sample > max_audioval) {
161  dst_sample = max_audioval;
162  } else if (dst_sample < min_audioval) {
163  dst_sample = min_audioval;
164  }
165  dst[0] = dst_sample & 0xFF;
166  dst_sample >>= 8;
167  dst[1] = dst_sample & 0xFF;
168  dst += 2;
169  }
170  }
171  break;
172 
173  case AUDIO_S16MSB:
174  {
175 #if defined(__GNUC__) && defined(__M68000__) && !defined(__mcoldfire__) && defined(SDL_ASSEMBLY_ROUTINES)
176  SDL_MixAudio_m68k_S16MSB((short *) dst, (short *) src,
177  (unsigned long) len, (long) volume);
178 #else
179  Sint16 src1, src2;
180  int dst_sample;
181  const int max_audioval = ((1 << (16 - 1)) - 1);
182  const int min_audioval = -(1 << (16 - 1));
183 
184  len /= 2;
185  while (len--) {
186  src1 = ((src[0]) << 8 | src[1]);
187  ADJUST_VOLUME(src1, volume);
188  src2 = ((dst[0]) << 8 | dst[1]);
189  src += 2;
190  dst_sample = src1 + src2;
191  if (dst_sample > max_audioval) {
192  dst_sample = max_audioval;
193  } else if (dst_sample < min_audioval) {
194  dst_sample = min_audioval;
195  }
196  dst[1] = dst_sample & 0xFF;
197  dst_sample >>= 8;
198  dst[0] = dst_sample & 0xFF;
199  dst += 2;
200  }
201 #endif
202  }
203  break;
204 
205  case AUDIO_U16LSB:
206  {
207  Uint16 src1, src2;
208  int dst_sample;
209  const int max_audioval = 0xFFFF;
210 
211  len /= 2;
212  while (len--) {
213  src1 = ((src[1]) << 8 | src[0]);
214  ADJUST_VOLUME(src1, volume);
215  src2 = ((dst[1]) << 8 | dst[0]);
216  src += 2;
217  dst_sample = src1 + src2;
218  if (dst_sample > max_audioval) {
219  dst_sample = max_audioval;
220  }
221  dst[0] = dst_sample & 0xFF;
222  dst_sample >>= 8;
223  dst[1] = dst_sample & 0xFF;
224  dst += 2;
225  }
226  }
227  break;
228 
229  case AUDIO_U16MSB:
230  {
231  Uint16 src1, src2;
232  int dst_sample;
233  const int max_audioval = 0xFFFF;
234 
235  len /= 2;
236  while (len--) {
237  src1 = ((src[0]) << 8 | src[1]);
238  ADJUST_VOLUME(src1, volume);
239  src2 = ((dst[0]) << 8 | dst[1]);
240  src += 2;
241  dst_sample = src1 + src2;
242  if (dst_sample > max_audioval) {
243  dst_sample = max_audioval;
244  }
245  dst[1] = dst_sample & 0xFF;
246  dst_sample >>= 8;
247  dst[0] = dst_sample & 0xFF;
248  dst += 2;
249  }
250  }
251  break;
252 
253  case AUDIO_S32LSB:
254  {
255  const Uint32 *src32 = (Uint32 *) src;
256  Uint32 *dst32 = (Uint32 *) dst;
257  Sint64 src1, src2;
258  Sint64 dst_sample;
259  const Sint64 max_audioval = ((((Sint64) 1) << (32 - 1)) - 1);
260  const Sint64 min_audioval = -(((Sint64) 1) << (32 - 1));
261 
262  len /= 4;
263  while (len--) {
264  src1 = (Sint64) ((Sint32) SDL_SwapLE32(*src32));
265  src32++;
266  ADJUST_VOLUME(src1, volume);
267  src2 = (Sint64) ((Sint32) SDL_SwapLE32(*dst32));
268  dst_sample = src1 + src2;
269  if (dst_sample > max_audioval) {
270  dst_sample = max_audioval;
271  } else if (dst_sample < min_audioval) {
272  dst_sample = min_audioval;
273  }
274  *(dst32++) = SDL_SwapLE32((Uint32) ((Sint32) dst_sample));
275  }
276  }
277  break;
278 
279  case AUDIO_S32MSB:
280  {
281  const Uint32 *src32 = (Uint32 *) src;
282  Uint32 *dst32 = (Uint32 *) dst;
283  Sint64 src1, src2;
284  Sint64 dst_sample;
285  const Sint64 max_audioval = ((((Sint64) 1) << (32 - 1)) - 1);
286  const Sint64 min_audioval = -(((Sint64) 1) << (32 - 1));
287 
288  len /= 4;
289  while (len--) {
290  src1 = (Sint64) ((Sint32) SDL_SwapBE32(*src32));
291  src32++;
292  ADJUST_VOLUME(src1, volume);
293  src2 = (Sint64) ((Sint32) SDL_SwapBE32(*dst32));
294  dst_sample = src1 + src2;
295  if (dst_sample > max_audioval) {
296  dst_sample = max_audioval;
297  } else if (dst_sample < min_audioval) {
298  dst_sample = min_audioval;
299  }
300  *(dst32++) = SDL_SwapBE32((Uint32) ((Sint32) dst_sample));
301  }
302  }
303  break;
304 
305  case AUDIO_F32LSB:
306  {
307  const float fmaxvolume = 1.0f / ((float) SDL_MIX_MAXVOLUME);
308  const float fvolume = (float) volume;
309  const float *src32 = (float *) src;
310  float *dst32 = (float *) dst;
311  float src1, src2;
312  double dst_sample;
313  /* !!! FIXME: are these right? */
314  const double max_audioval = 3.402823466e+38F;
315  const double min_audioval = -3.402823466e+38F;
316 
317  len /= 4;
318  while (len--) {
319  src1 = ((SDL_SwapFloatLE(*src32) * fvolume) * fmaxvolume);
320  src2 = SDL_SwapFloatLE(*dst32);
321  src32++;
322 
323  dst_sample = ((double) src1) + ((double) src2);
324  if (dst_sample > max_audioval) {
325  dst_sample = max_audioval;
326  } else if (dst_sample < min_audioval) {
327  dst_sample = min_audioval;
328  }
329  *(dst32++) = SDL_SwapFloatLE((float) dst_sample);
330  }
331  }
332  break;
333 
334  case AUDIO_F32MSB:
335  {
336  const float fmaxvolume = 1.0f / ((float) SDL_MIX_MAXVOLUME);
337  const float fvolume = (float) volume;
338  const float *src32 = (float *) src;
339  float *dst32 = (float *) dst;
340  float src1, src2;
341  double dst_sample;
342  /* !!! FIXME: are these right? */
343  const double max_audioval = 3.402823466e+38F;
344  const double min_audioval = -3.402823466e+38F;
345 
346  len /= 4;
347  while (len--) {
348  src1 = ((SDL_SwapFloatBE(*src32) * fvolume) * fmaxvolume);
349  src2 = SDL_SwapFloatBE(*dst32);
350  src32++;
351 
352  dst_sample = ((double) src1) + ((double) src2);
353  if (dst_sample > max_audioval) {
354  dst_sample = max_audioval;
355  } else if (dst_sample < min_audioval) {
356  dst_sample = min_audioval;
357  }
358  *(dst32++) = SDL_SwapFloatBE((float) dst_sample);
359  }
360  }
361  break;
362 
363  default: /* If this happens... FIXME! */
364  SDL_SetError("SDL_MixAudioFormat(): unknown audio format");
365  return;
366  }
367 }
#define SDL_MIX_MAXVOLUME
Definition: SDL_audio.h:462
GLenum GLenum dst
#define ADJUST_VOLUME(s, v)
Definition: SDL_mixer.c:85
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
#define SDL_SwapFloatBE(X)
Definition: SDL_endian.h:218
#define AUDIO_S32MSB
Definition: SDL_audio.h:104
#define AUDIO_U16LSB
Definition: SDL_audio.h:91
#define SDL_SwapFloatLE(X)
Definition: SDL_endian.h:214
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
GLenum GLsizei len
#define SDL_SwapBE32(X)
Definition: SDL_endian.h:216
#define AUDIO_F32MSB
Definition: SDL_audio.h:113
static const Uint8 mix8[]
Definition: SDL_mixer.c:34
#define AUDIO_U8
Definition: SDL_audio.h:89
int8_t Sint8
A signed 8-bit integer type.
Definition: SDL_stdinc.h:139
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
#define ADJUST_VOLUME_U8(s, v)
Definition: SDL_mixer.c:86
#define SDL_SwapLE32(X)
Definition: SDL_endian.h:212
#define AUDIO_F32LSB
Definition: SDL_audio.h:112
#define AUDIO_S32LSB
Definition: SDL_audio.h:103
int32_t Sint32
Definition: SDL_stdinc.h:157
#define SDL_SetError
#define AUDIO_S16MSB
Definition: SDL_audio.h:94
#define AUDIO_S16LSB
Definition: SDL_audio.h:92
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:151
#define F(x, y, z)
Definition: SDL_test_md5.c:73
int64_t Sint64
A signed 64-bit integer type.
Definition: SDL_stdinc.h:166
#define AUDIO_S8
Definition: SDL_audio.h:90
GLenum src
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:147
#define AUDIO_U16MSB
Definition: SDL_audio.h:93

◆ SDL_OpenAudio()

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. Alternately, you may pass a NULL pointer here, and call SDL_QueueAudio() with some frequency, to queue more audio samples to be played (or for capture devices, call SDL_DequeueAudio() with some frequency, to obtain audio samples).
  • desired->userdata is passed as the first parameter to your callback function. If you passed a NULL callback, this value is ignored.

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 1438 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.

1439 {
1440  SDL_AudioDeviceID id = 0;
1441 
1442  /* Start up the audio driver, if necessary. This is legacy behaviour! */
1443  if (!SDL_WasInit(SDL_INIT_AUDIO)) {
1444  if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
1445  return -1;
1446  }
1447  }
1448 
1449  /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */
1450  if (open_devices[0] != NULL) {
1451  SDL_SetError("Audio device is already opened");
1452  return -1;
1453  }
1454 
1455  if (obtained) {
1456  id = open_audio_device(NULL, 0, desired, obtained,
1458  } else {
1459  id = open_audio_device(NULL, 0, desired, NULL, 0, 1);
1460  }
1461 
1462  SDL_assert((id == 0) || (id == 1));
1463  return (id == 0) ? -1 : 0;
1464 }
static SDL_AudioDevice * open_devices[16]
Definition: SDL_audio.c:34
#define SDL_InitSubSystem
#define SDL_AUDIO_ALLOW_ANY_CHANGE
Definition: SDL_audio.h:143
#define SDL_assert(condition)
Definition: SDL_assert.h:167
#define NULL
Definition: begin_code.h:143
#define SDL_SetError
static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture, const SDL_AudioSpec *desired, SDL_AudioSpec *obtained, int allowed_changes, int min_id)
Definition: SDL_audio.c:1179
#define SDL_INIT_AUDIO
Definition: SDL.h:76
Uint32 SDL_AudioDeviceID
Definition: SDL_audio.h:304
#define SDL_WasInit

◆ SDL_OpenAudioDevice()

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.

Returns
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 1467 of file SDL_audio.c.

References open_audio_device().

1470 {
1471  return open_audio_device(device, iscapture, desired, obtained,
1472  allowed_changes, 2);
1473 }
static SDL_AudioDeviceID open_audio_device(const char *devname, int iscapture, const SDL_AudioSpec *desired, SDL_AudioSpec *obtained, int allowed_changes, int min_id)
Definition: SDL_audio.c:1179

◆ SDL_PauseAudio()

void SDL_PauseAudio ( int  pause_on)

Definition at line 1509 of file SDL_audio.c.

References SDL_PauseAudioDevice().

1510 {
1511  SDL_PauseAudioDevice(1, pause_on);
1512 }
void SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
Definition: SDL_audio.c:1498

◆ SDL_PauseAudioDevice()

void SDL_PauseAudioDevice ( SDL_AudioDeviceID  dev,
int  pause_on 
)

Definition at line 1498 of file SDL_audio.c.

References get_audio_device(), SDL_AudioDriver::impl, SDL_AudioDriverImpl::LockDevice, SDL_AudioDevice::paused, SDL_AtomicSet, and SDL_AudioDriverImpl::UnlockDevice.

Referenced by SDL_PauseAudio().

1499 {
1500  SDL_AudioDevice *device = get_audio_device(devid);
1501  if (device) {
1502  current_audio.impl.LockDevice(device);
1503  SDL_AtomicSet(&device->paused, pause_on ? 1 : 0);
1505  }
1506 }
SDL_atomic_t paused
Definition: SDL_sysaudio.h:168
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
void(* UnlockDevice)(_THIS)
Definition: SDL_sysaudio.h:87
void(* LockDevice)(_THIS)
Definition: SDL_sysaudio.h:86
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:126
#define SDL_AtomicSet
static SDL_AudioDevice * get_audio_device(SDL_AudioDeviceID id)
Definition: SDL_audio.c:141

◆ SDL_QueueAudio()

int SDL_QueueAudio ( SDL_AudioDeviceID  dev,
const void data,
Uint32  len 
)

Queue more audio on non-callback devices.

(If you are looking to retrieve queued audio from a non-callback capture device, you want SDL_DequeueAudio() instead. This will return -1 to signify an error if you use it with capture devices.)

SDL offers two ways to feed audio to the device: you can either supply a callback that SDL triggers with some frequency to obtain more audio (pull method), or you can supply no callback, and then SDL will expect you to supply data at regular intervals (push method) with this function.

There are no limits on the amount of data you can queue, short of exhaustion of address space. Queued data will drain to the device as necessary without further intervention from you. If the device needs audio but there is not enough queued, it will play silence to make up the difference. This means you will have skips in your audio playback if you aren't routinely queueing sufficient data.

This function copies the supplied data, so you are safe to free it when the function returns. This function is thread-safe, but queueing to the same device from two threads at once does not promise which buffer will be queued first.

You may not queue audio on a device that is using an application-supplied callback; doing so returns an error. You have to use the audio callback or queue audio with this function, but not both.

You should not call SDL_LockAudio() on the device before queueing; SDL handles locking internally for this function.

Parameters
devThe device ID to which we will queue audio.
dataThe data to queue to the device for later playback.
lenThe number of bytes (not samples!) to which (data) points.
Returns
zero on success, -1 on error.
See also
SDL_GetQueuedAudioSize
SDL_ClearQueuedAudio

Definition at line 602 of file SDL_audio.c.

References SDL_AudioSpec::callback, get_audio_device(), SDL_AudioDriver::impl, SDL_AudioDevice::iscapture, SDL_AudioDriverImpl::LockDevice, queue_audio_to_device(), SDL_BufferQueueDrainCallback(), SDL_SetError, SDL_AudioDevice::spec, and SDL_AudioDriverImpl::UnlockDevice.

603 {
604  SDL_AudioDevice *device = get_audio_device(devid);
605  int rc = 0;
606 
607  if (!device) {
608  return -1; /* get_audio_device() will have set the error state */
609  } else if (device->iscapture) {
610  return SDL_SetError("This is a capture device, queueing not allowed");
611  } else if (device->spec.callback != SDL_BufferQueueDrainCallback) {
612  return SDL_SetError("Audio device has a callback, queueing not allowed");
613  }
614 
615  if (len > 0) {
616  current_audio.impl.LockDevice(device);
617  rc = queue_audio_to_device(device, data, len);
619  }
620 
621  return rc;
622 }
static int queue_audio_to_device(SDL_AudioDevice *device, const Uint8 *data, Uint32 len)
Definition: SDL_audio.c:464
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
SDL_AudioSpec spec
Definition: SDL_sysaudio.h:156
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
void(* UnlockDevice)(_THIS)
Definition: SDL_sysaudio.h:87
GLenum GLsizei len
SDL_bool iscapture
Definition: SDL_sysaudio.h:169
SDL_AudioCallback callback
Definition: SDL_audio.h:177
void(* LockDevice)(_THIS)
Definition: SDL_sysaudio.h:86
static void SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int len)
Definition: SDL_audio.c:565
#define SDL_SetError
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:126
static SDL_AudioDevice * get_audio_device(SDL_AudioDeviceID id)
Definition: SDL_audio.c:141

◆ SDL_UnlockAudio()

void SDL_UnlockAudio ( void  )

Definition at line 1542 of file SDL_audio.c.

References SDL_UnlockAudioDevice().

1543 {
1545 }
void SDL_UnlockAudioDevice(SDL_AudioDeviceID devid)
Definition: SDL_audio.c:1532

◆ SDL_UnlockAudioDevice()

void SDL_UnlockAudioDevice ( SDL_AudioDeviceID  dev)

Definition at line 1532 of file SDL_audio.c.

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

Referenced by SDL_UnlockAudio().

1533 {
1534  /* Obtain a lock on the mixing buffers */
1535  SDL_AudioDevice *device = get_audio_device(devid);
1536  if (device) {
1538  }
1539 }
static SDL_AudioDriver current_audio
Definition: SDL_audio.c:33
void(* UnlockDevice)(_THIS)
Definition: SDL_sysaudio.h:87
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:126
static SDL_AudioDevice * get_audio_device(SDL_AudioDeviceID id)
Definition: SDL_audio.c:141