Module cocos.audio.SDL.audio

Access to the raw audio mixing buffer.

Classes

  SDL_AudioSpec
Audio format structure.
  SDL_AudioCVT
Set of audio conversion filters and buffers.

Functions

string SDL_AudioDriverName(maxlen=1024)
Returns the name of the audio driver.
  SDL_OpenAudio(desired, obtained)
Open the audio device with the desired parameters.
(SDL_AudioSpec, SDL_array) SDL_LoadWAV_RW(src, freesrc)
Load a WAVE from the data source.
(SDL_AudioSpec, SDL_array) SDL_LoadWAV(file)
Load a WAVE from a file.
  SDL_FreeWAV(audio_buf)
Free a buffer previously allocated with SDL_LoadWAV_RW or SDL_LoadWAV.
SDL_AudioCVT SDL_BuildAudioCVT(src_format, src_channels, src_rate, dst_format, dst_channels, dst_rate)
Take a source format and rate and a destination format and rate, and return a SDL_AudioCVT structure.
  SDL_MixAudio(dst, src, length, volume)
Mix two audio buffers.

Variables

  __package__ = 'cocos.audio.SDL'

Function Details

SDL_AudioDriverName

SDL_AudioDriverName(maxlen=1024)
Returns the name of the audio driver. Returns None if no driver has been initialised.
Parameters:
maxlen
Maximum length of the returned driver name; defaults to 1024.
Returns: string

SDL_OpenAudio

SDL_OpenAudio(desired, obtained)

Open the audio device with the desired parameters.

If successful, the actual hardware parameters will be set in the instance passed into obtained. If obtained is None, 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.

An exception will be raised if the audio device couldn't be opened, or the audio thread could not be set up.

The fields of desired are interpreted as follows:

desired.freq
desired audio frequency in samples per second
desired.format
desired audio format, i.e., one of AUDIO_U8, AUDIO_S8, AUDIO_U16LSB, AUDIO_S16LSB, AUDIO_U16MSB or AUDIO_S16MSB
desired.samples

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
size in bytes of the audio buffer; calculated by SDL_OpenAudio.
desired.silence
value used to set the buffer to silence; calculated by SDL_OpenAudio.
desired.callback

a function that will be called when the audio device is ready for more data. The signature of the function should be:

callback(userdata: any, stream: SDL_array) -> None

The function is called with the userdata you specify (see below), and an SDL_array of the obtained format which you must fill with audio data.

This function usually runs in a separate thread, so you should protect data structures that it accesses by calling SDL_LockAudio and SDL_UnlockAudio in your code.

desired.userdata
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(False) 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.

Parameters:
desired
: SDL_AudioSpec
obtained
: SDL_AudioSpec or None

SDL_LoadWAV_RW

SDL_LoadWAV_RW(src, freesrc)

Load a WAVE from the data source.

The source is automatically freed if freesrc is non-zero. For example, to load a WAVE file, you could do:

SDL_LoadWAV_RW(SDL_RWFromFile('sample.wav', 'rb'), 1)

You need to free the returned buffer with SDL_FreeWAV when you are done with it.

Parameters:
src
: SDL_RWops
freesrc
: int
Returns:
(SDL_AudioSpec, SDL_array): a tuple (spec, audio_buf) where spec describes the data format and audio_buf is the buffer containing audio data.

SDL_LoadWAV

SDL_LoadWAV(file)
Load a WAVE from a file.
Parameters:
file
: str
Returns: (SDL_AudioSpec, SDL_array)

See Also: SDL_LoadWAV_RW

SDL_FreeWAV

SDL_FreeWAV(audio_buf)
Free a buffer previously allocated with SDL_LoadWAV_RW or SDL_LoadWAV.
Parameters:
audio_buf
: SDL_array

SDL_BuildAudioCVT

SDL_BuildAudioCVT(src_format, src_channels, src_rate, dst_format, dst_channels, dst_rate)

Take a source format and rate and a destination format and rate, and return a SDL_AudioCVT structure.

The SDL_AudioCVT structure is used by SDL_ConvertAudio to convert a buffer of audio data from one format to the other.

Parameters:
src_format
: int
src_channels
: int
src_rate
: int
dst_format
: int
dst_channels
: int
dst_rate
: int
Returns: SDL_AudioCVT

SDL_MixAudio

SDL_MixAudio(dst, src, length, volume)

Mix two audio buffers.

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.

Parameters:
dst
: SDL_array
src
: SDL_array
length
: int
volume
: int

Note: SDL-ctypes doesn't know the current play format, so you must always pass in byte buffers (SDL_array or sequence) to this function, rather than of the native data type.