When we think of video games, we often think of the music and sounds that accompany them. Providing those special effects used to be very difficult. Keep reading to learn how SDL makes this important task very easy.
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:
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:
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:
/* 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.
That covers the functions. The next section will show how to use then to play the sound.