David Cordero

Logarithmic Volume Control

Published on 18 Oct 2021

Have you ever experienced one of the following situations? 

You are watching some video in a quiet environment, you set the volume to the lowest possible value and it is too loud, so you press the volume down button once, and now your device is muted.

On some other day, you are at a party listening to music with the volume set at 100%, you change it to 80% and you can barely notice any difference.

How can it be that a single volume level means too much in the first case, but changing it from 100% to 80% feels like nothing in the second one?

The reason is the difference between the way developers implemented the volume controls in comparison to the way that our ears perceive loudness.

Volume controls are quite often implemented in a linear way, in contrast to the human ear which perceives loudness logarithmically.

This is a well-known issue by audio engineers or game developers who often work with logarithmic audio scales, such as decibels (dB), that take into account logarithmic audio perception.

After years working as a Software Engineer in the TV industry, I can assure you that this is not a very popular knowledge in the sector, and this is probably the reason for having it that often implemented wrong.

Once you know about it, implementing a volume control logarithmically is actually a quite easy task.

As a reference, in the case of iOS Development, we could implement it with a simple extension for AVPlayer:

extension AVPlayer {

    var logarithmicVolume: Float {
        get {
            return sqrt(volume)
        }
        set {
            volume = pow(newValue, 2)
        }
    }
}

From now on we could use logarithmicVolume instead of volume to get a logarithmic volume control.

If you are interested in further details on this topic, I recommend you checking this Wikipedia page out.