SDL::Audio -- SDL Bindings for Audio
TODO, Core, Audio
Opens the audio device with the desired parameters.
use SDL; use SDL::Audio; SDL::init(SDL_INIT_AUDIO); my $desired = SDL::AudioSpec->new(); my $obtained; SDL::open_audio($desired, $obtained); # $obtained->... (A new SDL::AudioSpec now);
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.
To open the audio device a desired SDL::AudioSpec must be created.
my $desired = SDL::AudioSpec->new();
You must then fill this structure with your desired audio specifications.
$desired->freq
$desired->format
$desired->channels
$desired->samples
THIS IS NOT READY YET
$desired->callback my $callback= sub{ my ($userdata, $stream, $len) = @_; }; $userdata is a reference stored in the userdata field of the SDL::AudioSpec. $stream is a pointer to the audio buffer you want to fill with information and $len is the length of the audio buffer in bytes. $desired->userdata This pointer is passed as the first parameter to the callback function.
SDL::Audio::open reads these fields from the desired SDL::AudioSpec structure passed to the function and attempts to find an audio configuration matching your desired. As mentioned above, if the obtained parameter is NULL then SDL with convert from your desired audio settings to the hardware settings as it plays.
If obtained is NULL then the desired SDL::AudioSpec is your working specification, otherwise the obtained SDL::AudioSpec becomes the working specification and the desired specification can be deleted. The data in the working specification is used when building SDL::AudioCVT's for converting loaded data to the hardware format.
SDL::Audio::open calculates the size and silence fields for both the $desired and $obtained specifications. The size field stores the total size of the audio buffer in bytes, while the silence stores the value used to represent silence in the audio buffer
The audio device starts out playing silence when it's opened, and should be enabled for playing by calling SDL::Audio::pause(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.
Pauses and unpauses the audio callback processing
Gets the current audio state
Loads a WAVE file
Frees previously opened WAV data
SDL::Audio->convert( cvt, data, len )
Converts audio data to a desired audio format.
convert_audio
takes as first parameter cvt
, which was previously initialized. Initializing a SDL::AudioCVT
is a two step process.
First of all, the structure must be created via SDL::AudioCVT-
build> along with source and destination format parameters. Secondly,
the data
and len
fields must be setup. data
should point to the audio data buffer beeing source and destination at
once and len
should be set to the buffer length in bytes. Remember, the length of the buffer pointed to by buf should be
len*len_mult
bytes in length.
Once the SDL::AudioCVT
structure is initialized, we can pass it to convert_audio
, which will convert the audio data pointed to
by data
. If convert_audio
fails undef
is returned, otherwise the converted SDL::AudioCVT
structure.
If the conversion completed successfully then the converted audio data can be read from cvt-
buf>. The amount of valid, converted,
audio data in the buffer is equal to cvt-
len*cvt->len_ratio>.
Example:
use SDL; use SDL::Audio; use SDL::AudioSpec; use SDL::AudioCVT; SDL::init(SDL_INIT_AUDIO); # Converting some WAV data to hardware format my $desired = SDL::AudioSpec->new(); my $obtained = SDL::AudioSpec->new(); # Set desired format $desired->freq(22050); $desired->channels(1); $desired->format(AUDIO_S16); $desired->samples(8192); # Open the audio device if( SDL::Audio::open($desired, $obtained) < 0 ) { printf( STDERR "Couldn't open audio: %s\n", SDL::get_error() ); exit(-1); } # Load the test.wav my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obtained); unless( $wav_ref ) { warn("Could not open sample.wav: %s\n", SDL::get_error() ); SDL::Audio::close_audio(); SDL::quit; exit(-1); } my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref}; # Build AudioCVT my $wav_cvt = SDL::AudioCVT->build( $wav_spec->format, $wav_spec->channels, $wav_spec->freq, $obtained->format, $obtained->channels, $obtained->freq); # Check that the convert was built if( $wav_cvt == -1 ) { warn("Couldn't build converter!\n" ); SDL::Audio::close(); SDL::Audio::free_wav($wav_buf); SDL::quit(); exit(-1); } # And now we're ready to convert SDL::Audio::convert($wav_cvt, $wav_buf, $wav_len); # We can freeto original WAV data now SDL::Audio::free_wav($wav_buf);
TODO: What to do with it? How to use callback? See http://www.libsdl.org/cgi/docwiki.cgi/SDL_ConvertAudio
Mixes audio data
Locks out the callback function
Unlocks the callback function
Shuts down audio processing and closes the audio device.