Adjust the volume for audio files loaded with SDL_LoadWav() and SDL_OpenAudio() function

Hello everyone,

I am new to libsdl. My application MUST USE SDL_LoadWav and SDL_OpenAudio to
load the audio file.

After loading the audio file, how to adjust the playing volume? What are the
codes/functions to do this?

Note: I do not want to use SDL_Mixer library.

Regards,

There is no codec, mixer or anything like that in SDL.

There is, however, the SDL_MixAudio() call:
http://sdl.beuc.net/sdl.wiki/SDL_MixAudio

Apart from that, the SDL audio API just wraps the basic audio output
functionality, which means a fixed rate stream at “maximum” volume. You’re
supposed to implement your own DSP code over this, or use an add-on library,
like SDL_mixer.

Anyway, volume is just sample by sample multiplication; that is, scaling the
samples.On Monday 28 February 2011, at 14.18.32, Chin Shi Hong wrote:

Hello everyone,

I am new to libsdl. My application MUST USE SDL_LoadWav and SDL_OpenAudio
to load the audio file.

After loading the audio file, how to adjust the playing volume? What are
the codes/functions to do this?


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’

Make sure your volume control is logarithmic (and not linear).

See here
http://en.wikipedia.org/wiki/Potentiometer#Audio_control
or here
http://sound.westhost.com/project01.htm
for an explanation.

–AndreasOn 2/28/11 7:45 AM, David Olofson wrote:

On Monday 28 February 2011, at 14.18.32, Chin Shi Hong wrote:

Hello everyone,

I am new to libsdl. My application MUST USE SDL_LoadWav and SDL_OpenAudio
to load the audio file.

After loading the audio file, how to adjust the playing volume? What are
the codes/functions to do this?
There is no codec, mixer or anything like that in SDL.

There is, however, the SDL_MixAudio() call:
http://sdl.beuc.net/sdl.wiki/SDL_MixAudio

Apart from that, the SDL audio API just wraps the basic audio output
functionality, which means a fixed rate stream at “maximum” volume. You’re
supposed to implement your own DSP code over this, or use an add-on library,
like SDL_mixer.

Anyway, volume is just sample by sample multiplication; that is, scaling the
samples.

Good point - although this is mostly relevant for user interfaces.

BTW, there is generally no point in implementing this relatively expensive
transform on the sample-by-sample level. Those cycles are much better spent on
linear or higher order ramping, for click-/buzz-free fades, which IMHO, is
absolutely mandatory even in simple “consumer” applications.

Also, note that logarithmic controls don’t have a natural "absolute silence"
value, so in some applications you may have to decide on a special-cased
threshold level, or add a separate mute control.

So, uhm… this stuff isn’t all that trivial, after all. It comes down to what
kind of requirements you have.On Monday 28 February 2011, at 17.12.36, Andreas Schiffler wrote:

Make sure your volume control is logarithmic (and not linear).

See here
http://en.wikipedia.org/wiki/Potentiometer#Audio_control
or here
http://sound.westhost.com/project01.htm
for an explanation.


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’