1. IMPORTANT:
    We launched a new online community and this space is now closed. This community will be available as a read-only resources until further notice.
    JOIN US HERE

Dry/Wet control of inserts

Discussion in 'Scripting Workshop' started by t3am1, Feb 22, 2019.

  1. Cal545

    Cal545 New Member

    Messages:
    19
    Ok Cool, Thanks very much Ill have another crack with this and see what comes of it

    Nice one
     
  2. WocherMusic

    WocherMusic NI Product Owner

    Messages:
    35
    Thank you for that, it is enlightening as always!

    I really wonder why something like this is not written in the official KSP manual though. I'd love to see an actual graph of the scaling curve, provided by NI themselves – or at least just a little hint saying, "sliders are scaled logarithmically, and they are broken into two curves because [bla bla] – see the following graph [etc]". Without any official info and without Big Bob's library, how can we find solutions without hours of trial and error and resorting to "inelegant" alternatives in contrast to mathematically "elegant" possibilities? (And without being a mathematician, even though I like maths.)

    I have been meaning to try and find a formula for the following problem: I have a UI slider that controls the Tape Saturator's Gain knob between the value 28dB and 48dB, this works flawlessly. The same slider is now supposed to adjust the TS Output Gain knob in a way that Gain = 28dB will result in Output Gain = -2dB and Gain = 48dB in Output Gain = 9dB. I am sure it is possible, but right now I am trying to implement an array that changes the Output Gain value in 12 steps, 1dB at a time. It's not elegant and there is no maths involved because I'm not a genius. :(
     
  3. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    You could always do a linear interpolation of values. You set the Output knob to -2 dB value, then get_engine_par() on it. Then set it to 9 dB, get_engine_par() again. Then interpolate between these two values.

    You can rough out the values, so -2 dB is 357000, and 9 dB 561000.
     
    • Like Like x 1
  4. WocherMusic

    WocherMusic NI Product Owner

    Messages:
    35
    Sorry, what do you mean by interpolation?
    We now have the min and max values (min for -2dB is btw 367000 for me, I don't know why the numbers seem to be different?) , but how do I get the script to "scrub" between these two?
    Right now it looks like this:

    Code:
    on init
    declare ui_slider $fatknob (583000, 1000000) {TS Gain: 583000 = 28dB, 1000000 = 48dB}
        $fatknob := 771000
        make_persistent($fatknob)
        read_persistent_var($fatknob)
        set_engine_par($ENGINE_PAR_TP_GAIN, $fatknob, -1, 2, 1)
        set_engine_par($ENGINE_PAR_INSERT_EFFECT_OUTPUT_GAIN, ???, -1, 2, 1)
        set_engine_par($ENGINE_PAR_TP_WARMTH, 1000000, -1, 2, 1)
        set_engine_par($ENGINE_PAR_TP_HF_ROLLOFF, 0, -1, 2, 1)
    end on
    
    on ui_control($fatknob)
            set_engine_par($ENGINE_PAR_TP_GAIN, $fatknob, -1, 2, 1)
            set_engine_par($ENGINE_PAR_INSERT_EFFECT_OUTPUT_GAIN, ???, -1, 2, 1)
    end on
    Is there a way to translate the $fatknob's values to the min and max values that correspond to the plugin's Output Gain? Like 583000 := 367000 and 1000000 := 561000? (Of course this wouldn't work, I'm just illustrating the thought.)
     
    Last edited: Dec 27, 2021
  5. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Because I made a typo, oops! :oops: It is 367000 indeed.

    So what you need is linear interpolation between the two values. This is a pretty basic math function:

    Code:
    function math.scale(value, in_min, in_max, out_min, out_max) -> return
        return := (((value - in_min) * (out_max - out_min)) / (in_max - in_min)) + out_min
    end function
    value is your $fatknob here, in_min is minimum of $fatknob, in_max is maximum of $fatknob, out_min is minimum of outgain (367000), out_max is, well, you know...

    (Usage of this function requires SublimeKSP, unless you want to rewrite it for your own usage, since it's just $fatknob that you need, the rest are constants that you plug into the equation.)
     
    • Like Like x 1
  6. WocherMusic

    WocherMusic NI Product Owner

    Messages:
    35
    Thank you, ED!
    This morning I found Koala on Github and was kind of feeling it in my guts that something similar like this might be the solution – here is the link for anyone who is interested: Koala by magneto538 (math.scale function is further down the page.)

    I'm getting a syntax error in Sublime Text (I have version 4, though, with the KSP add-on, of course) for some reason in the first line. What I wrote is:
    Code:
    function math.scale($fatknob, 583000, 1000000, 367000, 561000) -> return
            return := ((($fatknob-583000)*(561000-367000))/(1000000-583000)+367000)
    end function
    Probably some very nooby mistake here... :oops:
    I tried deleting "-> return" or only "->"...
    Btw I placed this inside the init callback.
     
  7. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    When you call a function you only write

    math.scale(...)

    You don't repeat the function/end function/return stuff. That's function declaration.

    Also function declarations do NOT go into init callback EVER. They need to be outside of it. But if you have Koala, you just import Koala and you don't have to do the function definition, you just use math.scale() outright.


    (Also, I recommend rolling back to ST3 if at all possible, ST4 seems occasionally buggy with SublimeKSP extension, ST3 is rock solid.)
     
  8. WocherMusic

    WocherMusic NI Product Owner

    Messages:
    35
    Hm. It seems I need to interrupt the init callback for this function and reopen another init callback afterwards. But as far as I can remember init callbacks can only be written once. However there is no way of declaring $fatknob before using a function that actually requires $fatknob...?

    I downgraded to ST3 (thanks for your hint!), but still I'm not getting the script to work. Keep getting errors. :( Omitting the "function" parts didn't change anything, omitting the "-> return" part did, though. One error message said, "function math.scale not declared", so I tried declaring the function "math.scale" and seem to have gotten one step further at least – now I get the error saying, "unknown function: math.scale". Maybe that's a good sign? ;)

    BTW, I don't have Koala – I found it only this morning and think it is interesting, but I'd like to keep things simple as long as possible and not implement it at the moment.

    [EDIT]
    Wait, I got it to compile without errors.... Will keep trying!!
     
  9. WocherMusic

    WocherMusic NI Product Owner

    Messages:
    35
    Aha. Sublime ate it, but Kontakt won't. I am getting an error saying "newline expected". Highlighted code looks like this:
    Code:
    declare ui_slider $fatknob(583000, 1000000)
      $fatknob := 771000
      make_persistent($fatknob)
      read_persistent_var($fatknob)
      set_engine_par($ENGINE_PAR_TP_GAIN,$fatknob,-1,2,1)
    {ERROR LINE}  declare $math__scale($fatknob, 583000, 1000000, 367000, 561000) {END ERROR LINE}
      declare $return_fat
      $return_fat := ($fatknob-583000)*(561000-367000)/(1000000-583000)+367000
      set_engine_par($ENGINE_PAR_INSERT_EFFECT_OUTPUT_GAIN,$return_fat,-1,2,1)
    In ST it looks like this:
    Code:
    declare ui_slider $fatknob (583000, 1000000)
        $fatknob := 771000
        make_persistent($fatknob)
        read_persistent_var($fatknob)
        set_engine_par($ENGINE_PAR_TP_GAIN, $fatknob, -1, 2, 1)
    
        declare math.scale($fatknob, 583000, 1000000, 367000, 561000) {value, in_min, in_max, out_min, out_max}
        declare $return_fat
        $return_fat := ((($fatknob-583000)*(561000-367000))/(1000000-583000)+367000)
    
        set_engine_par($ENGINE_PAR_INSERT_EFFECT_OUTPUT_GAIN, $return_fat, -1, 2, 1)
     
  10. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    You're not supposed to declare math.scale() at all.

    If you're not using Koala, then you just use the function declaration (as written in one of my previous posts) after init callback end. Then you can use it downstream.
     
  11. WocherMusic

    WocherMusic NI Product Owner

    Messages:
    35
    I placed it after the init callback now. I still get errors, whatever I write. :-/
     
  12. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    I can't guess how your code looks if you don't show it here :)
     
  13. WocherMusic

    WocherMusic NI Product Owner

    Messages:
    35
    Sorry. :oops: Thing is, nothing has really changed in the text...
    So, I am on ST3 with KSP add-on and I wrote the function right after init callback end. Right now it looks like this:
    Code:
    math.scale($fatknob, 583000, 1000000, 367000, 561000) {value, in_min, in_max, out_min, out_max} -> return
        return := ((($fatknob-583000)*(561000-367000))/(1000000-583000)+367000)
    But as I said – whatever I change, the upper line always causes a syntax error.
    I know there is something I don't get – I really appreciate your patience and helpfulness!
     
  14. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    So again, that is not a function declaration. What I wrote in post #25 is how your function declaration should look. When you use it in code, you don't use the return keyword at all, you do something like

    var := math.scale(your arguments)

    then use that variable to set the output gain.
     
  15. WocherMusic

    WocherMusic NI Product Owner

    Messages:
    35
    But doesn't that variable need to be declared in the init callback first? (Still get errors, though.)
    In German we say, "I'm treading the water hose" (blocking the flow). I think that's what's happening here. :D
     
  16. WocherMusic

    WocherMusic NI Product Owner

    Messages:
    35
    So, as I'm not within a callback at this moment, don't I need something like
    Code:
    function
    {CODE HERE}
    end function
    If this function gets placed outside any other callback, then there needs to be some kind of callback, right?

    [EDIT]
    What about this (causes an error, of course):
    Code:
    function
        $return_fat := math.scale((($fatknob-583000)*(561000-367000))/(1000000-583000)+367000)
    end function
    In the init callback (which ends right before this function) I declared $return_fat. (I just kept the name "return" because I got used to it. :p)
     
    Last edited: Dec 28, 2021
  17. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    You do need the variable to store the return of the function declared in init, yes. But the function declaration needs to be right outside of init callback exactly as I posted it in post #25.

    When you USE (call) that function (in your $fatknob UI callback), you just assign it to a variable as I previously showed, using arguments as needed.
     
  18. WocherMusic

    WocherMusic NI Product Owner

    Messages:
    35
    OK, I got it to work, but the values are not calculated correctly.
    What I did is:
    Code:
    declare $return_fat
    in the init callback, then after [end on] I wrote
    Code:
    function math.scale {just for my own info: value = $fatknob, in_min = 583000, in_max = 1000000, out_min = 367000, out_max = 561000}
        $return_fat := ((($fatknob-583000)*(561000-367000))/(1000000-583000)+367000) {this should produce min value 367000 and max value 561000. In theorie at least.}
    end function
    However – something seems off with the maths. The TS Output Gain jumps straight to 0 (which is -inf dB) and stays there forever. I tried adding some values to $return_fat in the UI callback (i.e. "$return_fat + $fatknob*1/2") and at least the Output Gain knob moved, but with the wrong values, of course.

    Another experiment: deleted math.scale completely and just inserted the formula for $return_fat into the UI callback ($return_fat is now redundant). Looks like this:
    Code:
    on ui_control($fatknob)
            set_engine_par($ENGINE_PAR_TP_GAIN, $fatknob, -1, 2, 1)
            set_engine_par($ENGINE_PAR_INSERT_EFFECT_OUTPUT_GAIN, ((($fatknob-583000)*(561000-367000))/(1000000-583000)+367000), -1, 2, 1)
    end on
    Weirdly produces a completely different scenario: the Output Gain knob moves only between -2dB and -2.2dB. :confused:
     
  19. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Oh shoot, we might be in the "we're overflowing the 32-bit signed variable range" territory here. Darn it! So we're dealing with some pretty big values here:

    fatknob - 583000 gives you the input value range of [0, 417000].
    We then multiply this by 561000 - 367000 = 194000

    So at the maximum, we have 417000 * 194000 = 80898000000! This is way outside of 32-bit signed maximum value range (which is 2^31 = 2147483648)...

    We'll have to deal with this differently. So we know that we need to go from 367000 to 561000, which is a difference of 194000. And we need to cover that in 417000 steps (which is the range of our fatknob).

    194000 / 417000 = 0.46522782

    That's the ratio that we have between the two ranges. Thankfully we can use floating point math in KSP now, so this is how you do it, after all...

    Code:
    on init
        declare pers ui_slider fatknob (583000, 1000000)
        declare ~gain
    end on
    
    on ui_control (fatknob)
        gain := 367000.0 + (int_to_real(fatknob - 583000) * 0.46522782)
        set_engine_par(ENGINE_PAR_TP_GAIN, fatknob, -1, 2, NI_INSERT_BUS)
        set_engine_par(ENGINE_PAR_INSERT_EFFECT_OUTPUT_GAIN, real_to_int(gain), -1, 2, NI_INSERT_BUS)
    end on

    It's basically exactly the same formula as math.scale(), just reduced with constants (and using floating point values).
     
    • Like Like x 1
  20. WocherMusic

    WocherMusic NI Product Owner

    Messages:
    35
    ED, you are an absolute god. :) Thank you again for your patience!

    Just as a note for future reference: we need to use a real number as a multiplier instead of an integer in order to keep the result at a reasonable value, then convert real to int.