Learning Sound for Game Programming using SDL - Playing the Audio (
Page 3 of 4 )
Playing the audio not only means filling the buffer with required data but also loading the audio file to be played. The functions required are the callback function and the file playing functions. The file playing functions include SDL_LoadWav, SDL_BuildAudioCVT, SDL_ConvertAudio and SDL_FreeWAV.
SDL_LoadWav loads a wav file and returns the given SDL_AudioSpec with the corresponding data filled. The first parameter is the name of the wav file. The second is SDL_AudioSpec. If successful the third parameter would contain a malloc'd buffer that contains the audio data and the last parameter would have the length of the malloc'd audio buffer. In code it would be:
SDL_AudioSpec wave;
Uint8 *data;
Uint32 dlen;
char *file;
SDL_LoadWAV(file, &wave, &data, &dlen);
The above code would load the file represented by file into the data and set its specifications into wave and the length of the buffer into dlen.
To actually use the data it must be converted, for which SDL_AudioCVT structure is used. This structure must be initialized. The function to initialize the structure is SDL_BuildAudioCVT. The parameters are a pointer to the SDL_AudioCVT structure, a format of the source in UInt16, channels in the source in UInt8, the rate of the sample in int, a format of the destination in UInt16, channels in the destination in UInt8, the rate of the sample of destination in int where the source and destination are the formats of conversion. In code:
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16,2, 22050);
where cvt is the SDL_AudioCVT structure, wave.format, wave.channels, wave.freq are the format, channels and frequency of source format and AUDIO_S16,2, 22050 are the format, channels and frequency of destination format. Discussing SDL_AudioCVT is beyond the scope of this article. I will be discussing it in the near future.
The SDL_ConvertAudio function converts one format of audio to another. It takes only one parameter, the previously initialized SDL_AudioCVT. It converts the data pointed to by the buffer of the SDL_AudioCVT member. To understand it fully let's have a look at some detailed code. The comments are self explanatory:
SDL_AudioSpec *desired, *obtained;
SDL_AudioSpec wav_spec;
SDL_AudioCVT wav_cvt;
Uint32 wav_len;
Uint8 *wav_buf;
int ret;
/* Allocated audio specs */
desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
obtained=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
/* Set desired format */
desired->freq=22050;
desired->format=AUDIO_S16LSB;
desired->samples=8192;
desired->callback=my_audio_callback;
desired->userdata=NULL;
/* Open the audio device */
if ( SDL_OpenAudio(desired, obtained) < 0 ){
fprintf(stderr, "Couldn't open audio: %sn", SDL_GetError());
exit(-1);
}
free(desired);
/* Load the test.wav */
if( SDL_LoadWAV("test.wav", &wav_spec, &wav_buf,
&wav_len) == NULL ){
fprintf(stderr, "Could not open test.wav: %sn",
SDL_GetError());
SDL_CloseAudio();
free(obtained);
exit(-1);
}
/* 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){
fprintf(stderr, "Couldn't build converter!n");
SDL_CloseAudio();
free(obtained);
SDL_FreeWAV(wav_buf);
}
/* Setup for conversion */
wav_cvt.buf=(Uint8 *)malloc(wav_len*wav_cvt.len_mult);
wav_cvt.len=wav_len;
memcpy(wav_cvt.buf, wav_buf, wav_len);
/* We can delete to original WAV data now It is coming up
next*/
SDL_FreeWAV(wav_buf);
/* And now we're ready to convert */
SDL_ConvertAudio(&wav_cvt);
/* do whatever */
.
Once building and conversion is done, the file loaded into the user data has to be released, as it is no longer required. The conversion provides it to the application as a part of the buffer of the SDL_AudioCVT buffer member. To release the memory occupied by user data, SDL_FreeWAV has to be used.
In code:
SDL_FreeWAV(wav_buf);
That covers the functions. The next section will show how to use then to play the sound.