Home arrow Multimedia arrow Page 3 - Learning Sound for Game Programming using SDL

Playing the Audio - Multimedia

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.

  1. Learning Sound for Game Programming using SDL
  2. Opening the audio
  3. Playing the Audio
  4. Playing the sound in the real world
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 6
March 27, 2007

print this article



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 */

/* Open the audio device */
if ( SDL_OpenAudio(desired, obtained) < 0 ){
  fprintf(stderr, "Couldn't open audio: %sn", SDL_GetError());

/* Load the test.wav */
if( SDL_LoadWAV("test.wav", &wav_spec, &wav_buf,
&wav_len) == NULL ){

  fprintf(stderr, "Could not open test.wav: %sn",


/* 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 */
  fprintf(stderr, "Couldn't build converter!n");

/* Setup for conversion */
wav_cvt.buf=(Uint8 *)malloc(wav_len*wav_cvt.len_mult);
memcpy(wav_cvt.buf, wav_buf, wav_len);

/* We can delete to original WAV data now It is coming up


/* And now we're ready to convert */

/* 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:


That covers the functions. The next section will show how to use then to play the sound.

>>> More Multimedia Articles          >>> More By A.P.Rajshekhar

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Basic Lighting in OpenGL and SDL Game Progra...
- Working with Colors in OpenGL for Game Progr...
- Animation in OpenGL for Game Programming usi...
- Game Programming with SDL: Getting Started w...
- Using OpenGL with SDL for Game Programming
- Learning Sound for Game Programming using SDL
- Game Programming using SDL: Raw Graphics and...
- Game Programming using SDL: Getting Started
- Network Radio With Icecast
- Learning To SMILe

Developer Shed Affiliates


Dev Shed Tutorial Topics: