Logarithmic channel volume control. How to do it?

Generally this would be a little out of scope for the FMOD forums, but I will try to get you pointed in the right direction.
I am not sure what your range of desired values is, but let’s say for now you want a minimum value of 0 and a maximum value of 1. In code such a transformation would look like:

float linear(float x)
{
    if (x < 0)
        return 0;
    else if (x < 1)
        return x;
    else
        return 1;
}

So anything less than 0 we want to map to 0, anything greater than 1 we want to map to 1, and everything between that we want to map directly to x. Here is what that would look like graphing all possible x input values to their corresponding y output values:
image

So you are saying you want a logarithmic function specifically. To do that, we can swap out the x < 1 case for something that uses a log function:

float logarithmic(float x)
{
    if (x < 0)
        return 0;
    else if (x < 1)
        return log2f(x + 1); // +1 for desired max value
    else
        return 1;
}

With this function, any values closer to 0 will change rapidly, and values closer to 1 will change slowly. Then you just need to pass that into setVolume instead of your raw 0 → 1 value.

channel->setVolume(logarithmic(sliderValue));

Hopefully that makes sense. You can experiment with different calculations in the x < 1 case. For a steeper curve you can raise x to the power of a fractional value until you find a curve that suits your needs.

1 Like