Get normalized slider value



  • I'm using a slider to control the volume of a container. The slider is set to dB mode -100 to 0, but the container volume attribute expects a value between 0 and 1. If I change my slider to be between 0 and 1 the value read out will no longer be correct. Could we have a function to normalize the value of the slider to 0-1?


  • administrators

    Two solutions:

    1. Use the SimpleGain module, which has a dB-Range for the volume. The advantage is that the volume parameter is smoothed there, so you don't get the nasty zipper effects like when controlling the sound generators gain directly.

    2. Use Engine.getGainFactorForDecibels() in your control callback to convert the dB Value of the slider to a gain factor from 0...1.

    For a mixer type control I highly recommend the first solution because of not-zippering.



  • Aha that's good. Does the simple gain module add much CPU overhead? I'm using simple gains for my main mixer anyway but this container volume control was so I can tweak the volume of individual articulations from the UI.


  • administrators

    I've just added 10 Containers with 10 SimpleGain modules and it uses 15% - 20% CPU, so you have 0.1% - 0.3% CPU usage per simple gain module. Shouldn't be too problematic 🙂

    EDIT: However, if you rarely use it and have to write a custom callback anyway (which I think is the case since you are using a dynamic target), the Engine.getGainFactorForDecibel thing might be more practical in this scenario



  • Is there a handy function to convert a CC value (0-127) to an attack time slider (0-20000)? I've been using v = (Message.getControllerValue() * 20000) / 127; but the resolution is poor.


  • administrators

    Why is the resolution poor? If you want to make sure it uses floating point precision, use decimal numbers like this:

    v = (Message.getControllerValue() * 20000.0) / 127.0
    

    There's nothing that can be more precise in Javascript 🙂



  • Maybe resolution is the wrong term. Basically if my CC is at 1 (the lowest it can be above 0) then the slider will be at 157ms which is not very good for an attack slider.


  • administrators

    Ah, you need a skew factor:

    // values > 1 will yield more resolution at the lower end
    var skewFactor = 5.0; 
    var normalised = Message.getControllerValue() / 127.0;
    
    // this will "bend" the line towards the lower end
    var skewed = Math.pow(normalised, skewFactor);
    var value = skewed * 20000.0;
    

    There are more sophisticated methods, but this is a quick'n dirty formula to give you more resolution on the lower end. Also, exchange var with the suitable variable type (I used var to be as generic as possible in this example).



  • Yes that looks like what I'm after. Thank you!


Log in to reply
 

2
Online

277
Users

739
Topics

4.8k
Posts

Looks like your connection to Forum was lost, please wait while we try to reconnect.