SDL_ConvertAudio

Name

SDL_ConvertAudio -- Convert audio data to a desired audio format.

Synopsis

uses
SDL;

function SDL_ConvertAudio( cvt : PSDL_AudioCVT ) : Integer;

Description

SDL_ConvertAudio takes one parameter, cvt, which was previously initilized. Initilizing a TSDL_AudioCVT is a two step process. First of all, the structure must be passed to SDL_BuildAudioCVT along with source and destination format parameters. Secondly, the cvt.buf and cv.len fields must be setup. cvt-> buf should point to the audio data and cvt->len should be set to the length of the audio data in bytes. Remember, the length of the buffer pointed to by buf show be len*len_mult bytes in length.

Once the TSDL_AudioCVT structure is initilized then we can pass it to SDL_ConvertAudio, which will convert the audio data pointer to by cv.buf . If SDL_ConvertAudio returned 0 then the conversion was completed successfully, otherwise -1 is returned.

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 cv.len* cv.len_ratio.

Examples

// Converting some WAV data to hardware format 
procedure my_audio_callback( userdata : Pointer; stream : PUInt8; len : integer );
begin
.
.
.
desired, obtained : PSDL_AudioSpec;
wav_spec : TSDL_AudioSpec;
wav_cvt : TSDL_AudioCVT;
wav_len : UInt32;
wav_buf : PUint8;
ret : integer;

// Allocated audio specs
desired := PSDL_AudioSpec( malloc( SizeOf( TSDL_AudioSpec ) ) );
obtained := PSDL_AudioSpec( malloc( SizeOf( TSDL_AudioSpec ) ) );

// Set desired format
desired.freq := 22050;
desired.format := AUDIO_S16LSB;
desired.samples := 8192;
desired.callback := my_audio_callback;
desired.userdata := nil;

// Open the audio device
if ( SDL_OpenAudio( desired, obtained ) < 0 ) then
begin
MessageBox( 0, PChar( Format( 'Couldn''t open Audio : %s', [SDL_GetError] ) ), 'Error', MB_OK or MB_ICONHAND );
halt( -1 );
end;

free( desired );

// Load the test.wav
if ( SDL_LoadWAV( 'test.wav', @wav_spec, @wav_buf, @wav_len) = nil ) then
begin
MessageBox( 0, PChar( Format( 'Couldn''t open test.wav : %s', [SDL_GetError] ) ), 'Error', MB_OK or MB_ICONHAND );
SDL_CloseAudio;
free( obtained );
halt( -1 );
end;

// Build AudioCVT
ret := SDL_BuildAudioCVT( @wav_cvt,
wav_spec.format, wav_spec.channels, wav_spec.freq,
obtained.format, obtained.channels, obtained.freq );

// Check that the convert was built
if ( ret = -1 ) then
begin
MessageBox( 0, 'Coundn''t build converter!', 'Information', MB_OK or MB_ICONHAND );
SDL_CloseAudio;
free( obtained );
SDL_FreeWAV( wav_buf );
end;

// Setup for conversion
wav_cvt.buf := PUint8( malloc( wav_len * wav_cvt.len_mult ) );
wav_cvt.len := wav_len;
Move( wav_buf, wav_cvf.buf, wav_len );

// We can delete to original WAV data now
SDL_FreeWAV( wav_buf );

// And now we're ready to convert
SDL_ConvertAudio( @wav_cvt );

// do whatever
.
.
.
.
end;

See Also

SDL_BuildAudioCVT , TSDL_AudioCVT