How to change volume of SDL Audio buffer?

Hey guys,

for reasons I cannot work with the SDL Mixer. To work with audio data, I use something like this

SDL_AudioSpec wav_spec;
Uint32 wav_length;
Uint8 *wav_buffer;

/* Load the WAV */
if (SDL_LoadWAV("test.wav", &wav_spec, &wav_buffer, &wav_length) == NULL) {
    fprintf(stderr, "Could not open test.wav: %s\n", SDL_GetError());
} else {
    /* Do stuff with the WAV data, and then... */
    SDL_FreeWAV(wav_buffer);
}

So, the way I understand this, they crop the 16bit int data into bytes (8bit ints). I want to change volume and obviously I cannot do this on the byte chunks. The way I do it is to construct a 16bit int, change the volume and convert it back.

So my question is: Is there any better method to do so, bc I find it 2 complex to convert the entire buffer into 16bit ints and back to 8?!

And one more thing: Is it better to convert ints to float if I change the volume more often?

SDL_MixAudioFormat() can change the volume of an audio buffer, the final parameter is in the range 0 (silence) to 128 (100% volume). It cannot increase the volume, only decrease it.

Since you do not need the ‘mixing’ functionality, initialise the destination buffer with zeros.

2 Likes