Search code examples
mathvolumeequationexponential

Exponential volume control with a specified midpoint


I have a slider that returns values from 0 to 100. I am using this to control the gain of an oscillator.

When the slider is at 0, I would like the gain to be 0.0 When the slider is at 50, I would like the gain to be 0.1 When the slider is at 100, I would like the gain to be 0.5

So I need to find an equation to get a smooth curve which passes through all of these points.

I've got the following equation which gives an exponential curve and gets the start and end points correct, but I don't know how to force the curve through the middle point. Can anyone help?

function logSlider(position){
    var minP = 0;
    var maxP = 100;

    var minV = Math.log(0.0001);
    var maxV = Math.log(0.5);

    var scale = (maxV - minV) / (maxP - minP);

    return Math.exp(minV + scale*(position-minP));


}

Solution

  • Here's a derivation of the function. All it takes is some algebra.

    Model and Constraints

    We want an exponential function of the following form, that takes number between 0 and 1:

    f(t) = a * bt + c

    The function must satisfy these constraints you gave:

    f(0) = 0 = a + c
    f(1/2) = 0.1 = a * b1/2 + c
    f(1) = 0.5 = a * b + c

    Solving for a, b, c

    Let z2 = b.

    a + c = 0
    a * z + c = 0.1
    a * z * z + c = 0.5

    a * z - a = 0.1
    a * z * z - a = 0.5

    a * z * z - a * z = 0.4

    (a * z - a) * z = 0.4

    0.1 * z = 0.4

    z = 4

    b = 16

    a * 4 - a = 0.1

    a = 0.1 / 3

    c = -0.1 / 3

    Solution

    f(t) = (0.1 / 3) * (16t - 1)

    Here's a plot.

    Plot

    If you want to pass in values between 0 and 100, simply divide by 100 first.