What algorithms could I use for audio volume level?

evilpenguin picture evilpenguin · Jul 22, 2009 · Viewed 14.4k times · Source

Let's say I have a slider that can go between 0 and 1. The SoundTransform.volume also ranges between 0 (silent) and 1 (full volume), but if I use a linear function, let's say SoundTransform.volume = slider.volume, the result is rather not pleasing.

I really haven't studied the human ear, but I overheard once that human perception is logarithmic, or something similar. What algorithms should I use for setting the SoundTransform.volume?

Answer

back2dos picture back2dos · Jul 22, 2009

human perception in general is logarithmic, also when it comes to things as luminosity, etc. ... this enables us to register small changes to small "input signals" of our environement, or to put it another way: to always percieve a change of a perceivable physical quantity in relation to its value ...

thus, you should modify the volume to grow exponentially, like this:

y = (Math.exp(x)-1)/(Math.E-1)

you can try other bases as well:

y = (Math.pow(base,x)-1)/(base-1)

the bigger the value of base is, the stronger the effect, the slower volume starts growing in the beginning and the faster it grows in the end ...

a slighty simpler approach, giving you similar results (you are only in the interval between 0 and 1, so approximations are quite simple, actually), is to exponantiate the original value, as

y = Math.pow(x, exp);

for exp bigger than 1, the effect is, that the output (i.e. the volume in you case) first goes up slower, and then faster towards the end ... this is very similar to exponential functions ... the bigger exp, the stronger the effect ...

greetz

back2dos