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

Slider Range & Value Arrays

Discussion in 'Scripting Workshop' started by Will VDC, Jan 27, 2016.

  1. Will VDC

    Will VDC NI Product Owner

    Messages:
    57
    Hey folks!

    I have a square modulator connected to volume with a label that gives the value of the pulse note (set to triplet 64ths). I need my pulse value (frequency) slider to have 12 possible sync points with the corresponding values in my text array:

    Note---------Corresponding Modulator Label
    1--------------1/64T
    2--------------1/32T
    3--------------1/32
    4--------------1/16T
    6--------------1/16
    8--------------1/8T
    9--------------1/16T
    12------------1/8
    16------------1/4T
    18------------1/8D
    24------------1/4
    48------------1/1

    Here's what I have thus far:
    Code:
    on init
      message("My Instrument")
      make_perfview
      set_script_title("Kontakt Skin")
      set_control_par_str($INST_WALLPAPER_ID,$CONTROL_PAR_PICTURE,"skin")
      set_ui_height_px(380)
      declare $i
      declare $DEPTH_CC := 1
      declare ui_button $invisible_button
      declare $fg4rw
      set_control_par(get_ui_id($invisible_button),$CONTROL_PAR_WIDTH,150)
      set_control_par(get_ui_id($invisible_button),$CONTROL_PAR_HEIGHT,150)
      set_control_par(get_ui_id($invisible_button),$CONTROL_PAR_POS_X,240)
      set_control_par(get_ui_id($invisible_button),$CONTROL_PAR_POS_Y,220)
      set_control_par_str(get_ui_id($invisible_button),$CONTROL_PAR_PICTURE,"blank_png")
      set_text($invisible_button,"")
      declare ui_slider $ywzts(0, 127)
      set_control_par_str(get_ui_id($ywzts),$CONTROL_PAR_PICTURE,"source_orb")
      move_control_px($ywzts,240,200)
    
    {Create a string text array}
      declare !note_values[12]
      !note_values[0] := "1/64T"
      !note_values[1] := "1/32T"
      !note_values[2] := "1/32"
      !note_values[3] := "1/16T"
      !note_values[4] := "1/16"
      !note_values[5] := "1/8T"
      !note_values[6] := "1/16D"
      !note_values[7] := "1/8"
      !note_values[8] := "1/4T"
      !note_values[9] := "1/8D"
      !note_values[10] := "1/4"
      !note_values[11] := "1/1"
    
    {Make the slider}
    declare ui_slider $note(0, 1000000)
      make_persistent($note)
      set_control_par_str(get_ui_id($note),$CONTROL_PAR_PICTURE,"sourceknob_1")
      move_control_px($note,76,290)
      set_control_par(get_ui_id($note),$CONTROL_PAR_MOUSE_BEHAVIOUR,-500)
    
    {Place the Label}
    declare ui_label $lbl_note(1, 1)
      move_control_px($lbl_note,40,327)
      set_control_par_str(get_ui_id($lbl_note),$CONTROL_PAR_TEXT,"")
      set_control_par(get_ui_id($lbl_note),$CONTROL_PAR_HIDE,$HIDE_PART_BG)
      set_control_par_str(get_ui_id($lbl_note),$CONTROL_PAR_PICTURE,"")
      set_control_par(get_ui_id($lbl_note),$CONTROL_PAR_WIDTH,50)
      set_control_par(get_ui_id($lbl_note),$CONTROL_PAR_HEIGHT,50)
      set_control_par(get_ui_id($lbl_note),$CONTROL_PAR_FONT_TYPE,1)
    
    {Alter the range of the slider and attach the label declared above}
      declare $_old_range
      declare $_new_range
      $_old_range := 1000000-0
      if ($_old_range=0)
        $i := 12
      else
        $_new_range := 0-12
        $i := ($note-0)*$_new_range/$_old_range+12
      end if
      set_control_par_str(get_ui_id($lbl_note),$CONTROL_PAR_TEXT,!note_values[$i])
    end on
    
    {Connect the slider to the modulator}
    on ui_control($note)
      $i := 0
      while ($i<$NUM_GROUPS)
        set_engine_par($ENGINE_PAR_INTMOD_FREQUENCY,$note,$i,find_mod($i,"LFO_RECT"),-1)
        inc($i)
      end while
      $_old_range := 1000000-0
      if ($_old_range=0)
        $i := 12
      else
        $_new_range := 0-12
        $i := ($note-0)*$_new_range/$_old_range+12
      end if
      set_control_par_str(get_ui_id($lbl_note),$CONTROL_PAR_TEXT,!note_values[$i])
    end on
    
    I figure I'll have to create an array of values to connect to $new_range, but I have NO idea how to do this. Any help would be appreciated. Thanks!

    WVDC

     
    Last edited: Jan 27, 2016
  2. Lindon

    Lindon NI Product Owner

    Messages:
    322
    What was wrong with:
    Code:
    declare ui_slider $note(0, 11)
    ?
     
  3. Will VDC

    Will VDC NI Product Owner

    Messages:
    57
    Nothing is technically wrong with the code above, it just doesn't perform all of the functions.

    We're trying to get 12 possible knob settings to sync to specific note frequency values on our square LFO.

    Lowest setting on the slider/knob triggers whole note oscillations.
    Second setting on the slider/knob triggers quarter note oscillations.
    Third setting on the slider/knob triggers dotted eighth note oscillations.
    Etc...

    Remember, the number of notes in the oscillator doesn't rise one at a time 1 through 12. Notice on the chart above that we skip note numbers at given points (4 to 6, 6 to 8, 9 to 12, 12 to 16, 16 to 18, 18 to 24, and 24 to 48). As the knob/slider currently functions, we only can go one note value at a time 1, 2, 3, 4, 5, 6, 7, .....up to 12.

    Thoughts? Thanks, Lindon!
     
  4. Lindon

    Lindon NI Product Owner

    Messages:
    322
    So what you want to do is set your modulator_frequency to one of 12 values so you need an array with 12 slots....

    So you end up saying:

    Code:
    on init
        ....
        declare %modulator_engine_values[12] := (aaaaaa,bbbbbb,ccccc,etc etc,)
        ....
    end on
    
    (and these numbers will be somewhere between 0-1,000,000)

    and

    Code:
    on ui_control($note)
          ....
         set_engine_par($ENGINE_PAR_INTMOD_FREQUENCY,%modulator_engine_values[$note],$i,find_mod($i,"LFO_RECT"),-1)
         ....
    end on
    
    so your only problem now is to "know" what these aaaaaaa, bbbbbbb,numbers are... so write a little script that creates a button, and when the button is pressed it sends a message() with the current value of your target modulator frequency. Load the script, move the modulators "native" frequency control to a value you want, and press your tester button...read the value you need to use in the displayed message()
     
  5. Will VDC

    Will VDC NI Product Owner

    Messages:
    57
    Hey Lindon,

    Good thoughts here, mate. Thanks!

    If an array stores integer values, aren't the integer values I'm looking for 1,2,3,4,6,8,9, 12, 16, 18, 24, and 48? These will be the values needed by the modulator to get the resultant note values (1/64 Triplet, 1/32 Triplet, 1/32, 1/16T, 1/8, etc...).

    So I would think that these numbers would actually be the ones to take the place of aaaaaa, bbbbb, ccccc, etc...

    Does that sound right?
     
  6. Lindon

    Lindon NI Product Owner

    Messages:
    322
    No, you are trying to set a parameter in Kontakt, all parameters hold values from 0(zero) to 1,000,000(1 million), you've asked Kontakt to *display* the frequency in some note-style notation, but it holds its values in this range (and you set them with set_engine_param(...))
     
  7. Will VDC

    Will VDC NI Product Owner

    Messages:
    57
    Yep, copy that.

    Here's where I'm still confused...haven't we already set a different range for that slider with code below?

    Code:
    {Alter the range of the slider and attach the label declared above}
    declare $_old_range
    declare $_new_range
    $_old_range := 1000000-0
    if ($_old_range=0)
    $i := 12
    else
    $_new_range := 0-12
    $i := ($note-0)*$_new_range/$_old_range+12
    end if
    set_control_par_str(get_ui_id($lbl_note),$CONTROL_PAR_TEXT,!note_values[$i])
    end on
    
    {Connect the slider to the modulator}
    on ui_control($note)
    $i := 0
    while ($i<$NUM_GROUPS)
    set_engine_par($ENGINE_PAR_INTMOD_FREQUENCY,$note,$i,find_mod($i,"LFO_RECT"),-1)
    inc($i)
    end while
    $_old_range := 1000000-0
    if ($_old_range=0)
    $i := 12
    else
    $_new_range := 0-12
    $i := ($note-0)*$_new_range/$_old_range+12
    end if
    set_control_par_str(get_ui_id($lbl_note),$CONTROL_PAR_TEXT,!note_values[$i])
    end on
    
    You'll notice on the Frequency knob in the engine that the lowest point is 1 and the highest is 12. We just need each point on the knob to equate to a different value that doesn't change one at a time. So hypothetically, would there be a way to equate a different note value for each of the 12 points on the slider? If not, I definitely see now we could set a knob to equate values 0-1,000,000 to equate to a note value.

    Sorry...I am trying to understand...my skull is just thicker than most ;-)
    Screen Shot 2016-01-28 at 11.02.50 AM.png Screen Shot 2016-01-28 at 11.02.57 AM.png
     
  8. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    You don't need that code if you set the slider to (0,11)... that's just unnecessary math there. Just a table readout will suffice.
     
  9. Will VDC

    Will VDC NI Product Owner

    Messages:
    57
    Hey Evil D,

    Could you elaborate just a bit? I'm a composer who can count only count to four on a good day...I'm all for avoiding unnecessary math! As always, thank you.
     
  10. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Just go by Lindon's suggestions. He explained it pretty well.
     
  11. Will VDC

    Will VDC NI Product Owner

    Messages:
    57
    Alright! i'll do some investigating in the script and see what I can figure out. Thanks, gents!
     
  12. gwhizoflc

    gwhizoflc New Member

    Messages:
    20
    Hi Lindon... I'm trying to follow your suggestion by using
    $ep_lfo_freq := get_engine_par($ENGINE_PAR_INTMOD_FREQUENCY, $groupno, $ramp_lfo, -1)
    but it always returns 0. Any idea what is wrong?
     
  13. Lindon

    Lindon NI Product Owner

    Messages:
    322
    You are unlikely to be addressing the correct modulator, read the sticky thread at the top of the forum, from EvilDragon about KSP and modulators...
     
  14. gwhizoflc

    gwhizoflc New Member

    Messages:
    20
    Thanks Lindon. The LFO is an Internal modulator and the Freq is a knob on the modulator itself, not on the mod target strip. So the relevant code from the sticky thread is this, yes?
    Code:
    Code:
    set_engine_par($ENGINE_PAR_INTMOD_FREQUENCY,$LFOFreq,1,find_mod(1,"LFO 1"),-1)
    My code is:
    Code:
       set_engine_par($ENGINE_PAR_INTMOD_FREQUENCY, $LFOBeats, $groupno, $ramp_lfo, -1)
        $ramp_lfo := find_mod($groupno, @lfo_modname)
        $ramp_lfo_targ := find_target($groupno, $ramp_lfo, @lfo_modtargname)
    
        $ep_lfo_freq := get_engine_par($ENGINE_PAR_INTMOD_FREQUENCY, $groupno, $ramp_lfo, $ramp_lfo_targ{also tried -1 here})
    
     
  15. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Why do you complicate your life by using additional variables for something you can simply write within get_engine_par()? What's @lfo_modname set to?

    Attach a NKI so we can check if naming is somehow off.
     
  16. Lindon

    Lindon NI Product Owner

    Messages:
    322
    ..as ED says, attach an nki and we can have a look, but in any case as a useful tip:

    Open the script editor in your nki (you wont use it but it needs to be open) go to the modulator you want to influence, and right click in any empty area of the modulator(not on a control) and you will see (in addition to the usual "Delete" option) a list of the parameters you need to use to make changes to this modulator...
     
  17. Maestro Records

    Maestro Records New Member

    Messages:
    1
    in my case I was looking to convert knob or slider range from 0 - 1000000 to 0 - 127 so it works fine when mapping with midi controller as I can't modify the range of the controller.
    Thanks to Lindon answer above, I was able to find a way to do that

    here's the code for that

    Code:
    on init
    ...
    
        declare ui_slider $LFO(1, 127)
        make_persistent($LFO)
        declare $LFOId
        $LFOId := get_ui_id($LFO)
    
      declare %modulator_engine_values[127] := (7874,15748,23622,31496,39370,47244,55118,62992,70866,78740,86614,94488,102362,110236,118110,125984,133858,141732,149606,157480,165354,173228,181102,188976,196850,204724,212598,220472,228346,236220,244094,251969,259843,267717,275591,283465,291339,299213,307087,314961,322835,330709,338583,346457,354331,362205,370079,377953,385827,393701,401575,409449,417323,425197,433071,440945,448819,456693,464567,472441,480315,488189,496063,503937,511811,519685,527559,535433,543307,551181,559055,566929,574803,582677,590551,598425,606299,614173,622047,629921,637795,645669,653543,661417,669291,677165,685039,692913,700787,708661,716535,724409,732283,740157,748031,755906,763780,771654,779528,787402,795276,803150,811024,818898,826772,834646,842520,850394,858268,866142,874016,881890,889764,897638,905512,913386,921260,929134,937008,944882,952756,960630,968504,976378,984252,992126,1000000)
    ...
    end on
    
    on ui_control($LFO)
        $mod_idx := find_mod(0,"LFO_SINE")
        set_engine_par($ENGINE_PAR_INTMOD_FREQUENCY, %modulator_engine_values[$LFO],0,$mod_idx,1)
        message($LFO) { will output the range of 1 - 127 }
    end on
    
    
    what I did is convert the range from 0 - 1000000 to 0 - 127 by a simple formula

    Code:
    1000000 / 127 = 7874
    7874 * 2 = 15748
    7874 * 3 = 23622
    .
    .
    .
    7874 * 127 = 1000000
    
    Now the slider/knob works perfectly & smooth when mapped to midi controller
     
  18. EvilDragon

    EvilDragon Well-Known Member

    Messages:
    19,938
    Unfortunately by doing that you are reducing the parameter resolution greatly for other purposes (i.e. host automation). Kontakt already internally maps MIDI learned CC data to the proper values, you don't need to do it in its stead...