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

Key Mapping Feature *HELP*

Discussion in 'Scripting Workshop' started by Mr.Lion, Apr 18, 2015.

  1. Mr.Lion

    Mr.Lion NI Product Owner

    Messages:
    159
    Good Afternoon Bob!

    Tried to make some changes in the script but I only get error messages, I must do something wrong haha ..

    The spacing between I would like to have like this for example:

    DS0
    50
    DS1
    50
    DS2
    17
    DS3
    17
    DS4

    Best Regards
    Robin
     
  2. Big Bob

    Big Bob Forum Member

    Messages:
    606
    OK Robin,

    I edited KeyLearnDemo#3 to produce KeyLearnDemo#4 which looks like this.
    Code:
    { Key Learn Demo Script by RDV(4) 5-11-15 }
    on init
      message('')
      set_ui_height_px(220)
      make_perfview
      {------------------------- Configuration Constants -----------------------}
      declare const X := 50   { sub-panel coordinates }
      declare const Y := 30
      declare const KeyColorMode := 0  { 0=leave default coloring, #0=remove }
      { ksp key colors: 0=default, 1=Wht, 2=Yel, 3=Grn, 4=Red, 5=Blu, 6=Cyan, 7=Blk }
      declare const MonoKey := 3 { Green for one assigned sound }
      declare const PolyKey := 2 { Yellow for two or more assigned sounds }
      declare const NoColor := Bool(KeyColorMode)      { Default or White }
    
           { Default instrument drum-sound/trigger keys }
      declare const NumSounds := 5
      declare const DS0 := 38    { D1  Rimshot }
      declare const DS1 := 35    { B0  Rim Click }
      declare const DS2 := 34    { A#0 SideStick }
      declare const DS3 := 33    { A0  ? }
      declare const DS4 := 32    { G#0 ? }
                  { arrays }
      declare DTKey[NumSounds] := (DS0,DS1,DS2,DS3,DS4) { default trigger keys }
      declare dY[NumSounds] := (20,70,120,137,154)  { dY[n] = Y offset to control set n }
      declare EBoxID[NumSounds]     { ids for value edits }
      declare KeyColor[3] := (NoColor,MonoKey,PolyKey)  { unassigned, mono, or poly }
      declare LBtnID[NumSounds]     { ids for Learn Btns }
      declare TKey[NumSounds] := (DS0,DS1,DS2,DS3,DS4)  { current trigger keys }
        make_persistent(TKey)
                 { simple variables }
      declare idx               { general index }
      declare key               { color_key function parameter }
      declare learn_idx := -1   { learn index initially inactive }
      for idx := 0 to 127
        set_key_color(idx,NoColor)  { clear keyboard coloring }
      end for
      { Fixed caption labels, these can all be in the wallpaper instead }
      declare ui_label Header(1,1)
        set_text(Header,'Drum Sound Trigger Keys')
        move_control_px(Header,X,Y)
        Header->width := 155
        Header->hide := 1
        Header->font_type := 16
        Header->text_alignment := 2
      declare_line_caption(0,'Rimshot')
      declare_line_caption(1,'Rim Click')
      declare_line_caption(2,'Side Stick')
      declare_line_caption(3,'?')
      declare_line_caption(4,'?')
      declare_key_learn(0)  { Key/Learn btn sets }
      declare_key_learn(1)
      declare_key_learn(2)
      declare_key_learn(3)
      declare_key_learn(4)
      { set the edit box and learn btn attributes }
      read_persistent_var(TKey)
      for idx := 0 to NumSounds - 1
        key := TKey[idx]  { assigned key for this sound }
        EBoxID[idx]->text := ''
        EBoxID[idx]->pos_x := X+65
        EBoxID[idx]->pos_y := Y + dY[idx]
        EBoxID[idx]->width := 45
        EBoxID[idx]->value := key
        LBtnID[idx]->text := 'Learn'
        LBtnID[idx]->pos_x := X+115
        LBtnID[idx]->pos_y := Y + dY[idx]
        LBtnID[idx]->text_alignment := 1
        LBtnID[idx]->width := 40
        TKey[idx] := -1  { remove key, then test for more hits }
        { if more set to poly color, if not use mono color }
        set_key_color(key,KeyColor[hit(key)+2])
        TKey[idx] := key { replace removed key }
      end for
    end on { init callback }
    
    on_learn_btn(0)   { learn btn callback handlers }
    on_learn_btn(1)
    on_learn_btn(2)
    on_learn_btn(3)
    on_learn_btn(4)
    
    on_learn_edit(0)  { edit box callback handlers }
    on_learn_edit(1)
    on_learn_edit(2)
    on_learn_edit(3)
    on_learn_edit(4)
    
    on note
      ignore_event(EVENT_ID)   { recreate or replace as necessary }
      if learn_idx = -1  { in play mode }
        idx := search(TKey,EVENT_NOTE)
        if idx = -1
          exit   { note is not assigned, do nothing }
        end if
        while idx < NumSounds  { play all sounds layered on this key }
          if TKey[idx] = EVENT_NOTE
            play_note(DTKey[idx],EVENT_VELOCITY,0,-1)
          end if
          inc(idx)
        end while
      else { learn mode active }
        EBoxID[learn_idx]->value := EVENT_NOTE  { update edit box }
        key := TKey[learn_idx]   { remember prior key }
        TKey[learn_idx] := EVENT_NOTE  { update TKey with new key }
        call color_key           { color prior key }  
        key := EVENT_NOTE
        call color_key           { color new key }
        LBtnID[learn_idx]->value := 0 { cancel Learn btn }
        learn_idx := -1          { de-activate learn index }
      end if
    end on { note callback }
    
    {-------------------------------------------------------------------------
                         Support Functions and Macros
    --------------------------------------------------------------------------}
    function Bool(X) -> result   { 0 if X=0, 1 otherwise }
      result := 1+sh_right(abs(X)-1,31)
    end function
    
    function color_key { key }
      declare cx
      cx := search(TKey,key)+1  { cx=0 if key not in TKey }
      if cx # 0  { one or more occurences of key exist }
        TKey[cx-1] := -1              { remove 1st hit }
        { compute cx=1 if no more hits, cx=2 if more }
        cx := hit(key) + 2 { cx=1 if no more hits, cx=2 otherwise }
        TKey[search(TKey,-1)] := key  { replace 1st hit }
      end if
      set_key_color(key,KeyColor[cx]) { none, mono, or poly}
    end function
    
    function hit(tnote)->result  { 0 if tnote is in TKey, -1 otherwise }
      result := sh_right(search(TKey,tnote),31)
    end function
    
    function learn_btn_change
      if LBtnID[idx]->value # 0  { btn on }
        if learn_idx # -1   { turn off any prior on btn }
          LBtnID[learn_idx]->value := 0
        end if
        learn_idx := idx    { update learn index }
      else  { btn off }
        learn_idx := -1     { de-activate learn index }
      end if
    end function
    
    function learn_edit_change
      key := TKey[idx] { old key }
      TKey[idx] := EBoxID[idx]->value
      call color_key   { color old key }
      key := TKey[idx]
      call color_key   { color new key }
    end function
    
    macro declare_key_learn(#index#)  { create assigned key and learn btn combo }
      declare ui_value_edit LE#index# (0,127,0)
        EBoxID[#index#] := get_ui_id(LE#index#)  { learn edit box }
      declare ui_switch LB#index#
        LBtnID[#index#] := get_ui_id(LB#index#)  { learn btn }
    end macro
    
    macro declare_line_caption(#index#,caption)  { not needed if on wallpaper }
      declare ui_label CL#index#(1,1)
      set_text(CL#index#,caption)
      move_control_px(CL#index#,X,Y + dY[#index#])
      CL#index#->hide := 1           { transparent }
      CL#index#->font_type := 16
      CL#index#->text_alignment := 2 { right justified }
      CL#index#->width := 65
    end macro
    
    macro on_learn_btn(#index#)  { create learn btn handler }
      on ui_control(LB#index#)
        idx := #index#
        call learn_btn_change
      end on
    end macro
    
    macro on_learn_edit(#index#) { create edit box handler }
      on ui_control(LE#index#)
        idx := #index#
        call learn_edit_change
      end on
    end macro 
    I presume the spacing you specified was from top to top (of the edits and buttons)? If not, edit the dY array accordingly. I also assume that your pix depicted the default key assignments? If not, you will also need to edit the DS0 to DS4 constants.

    I'll let you study the code to find the changes I made:).

    I should also mention that whenever you revise a script and then reload it into the same Kontakt instrument, you should always first load the 'Empty' script to clear the persistence buffer before pasting the edited version into Kontakt. Otherwise, the persistence machinery will mess you up.

    Also, I should ask you whether or not you plan to put the captions on your wallpaper? I coded the scripts with that assumption. But, if you are going to have the script generate these captions, we can do it a little more efficiently if it's going to become a permanent part of your code.

    Rejoice,

    Bob
     
    Last edited: May 11, 2015
  3. Mr.Lion

    Mr.Lion NI Product Owner

    Messages:
    159
    Hello Bob!

    Thank you very much, I see now what some of the problems were when I look at this. I placed the array code in the wrong part haha ..

    The spacing is PERFECT, just what I needed.

    So, I should ''Apply'' a 'Empty' code to like 'reset' it?

    I tend to do both, for now I'm using the captions on my wallpaper like this:
    [​IMG]

    Best Regards
    Robin
     
  4. Big Bob

    Big Bob Forum Member

    Messages:
    606
    Yes, you should do this whenever you 're-use' an instrument with a new script. Otherwise, intentional or accidental name matches for the prersistent stuff will assume 'old' remembered values. This can produce 'confusing to diagnose' problems. Best to avoid it:)

    OK then you can remove the temporary captioning code.

    Rejoice,

    Bob
     
  5. Mr.Lion

    Mr.Lion NI Product Owner

    Messages:
    159
    Sooooo that's why I've had problems at times but when I've opened up a new instrument it has worked properly haha .. I fell minded right now haha.

    I have just checked into your EasySlider stuff and figured out how to make a stock preset function! Next part is a ''Save Preset''.

    There's really really good reading you've done there, love it.


    I've done that!

    Thank you, Best Regards
    Robin
     
  6. Mr.Lion

    Mr.Lion NI Product Owner

    Messages:
    159
    Hello Again Bob, sorry for all the fuzz.

    If I would like to hide the function, am I able to do something like the 'move_control ($demo, 0, 0)' ?
    Or will I have to make something more advanced?

    EDIT:
    Like a ui_switch that can hide the whole control.

    Best Regards
    Robin
     
    Last edited: May 12, 2015
  7. Big Bob

    Big Bob Forum Member

    Messages:
    606
    Hi Robin,

    Hiding a control by moving it to grid position 0,0 is somewhat passe (old school). Using both pixel and grid coordinates in the same script can sometimes cause problems. The 'modern' way to show/hide controls is to use a hide mask. The syntax with Nils' editor is:

    name -> hide := mask

    where name is the name of the control (or a variable containing its id) and mask is a bit mask specifying what parts of the control you want to hide. To hide the whole control use mask = 16 and to show the whole control use mask = 0. To make a label's background transparent, use mask = 1.

    The bit value functions, for those controls which respond, are as follows:

    0 - none
    1 - background
    2 - title
    4 - value
    8 - light (mod ring)
    16 - hide all

    Rejoice,

    Bob
     
  8. Mr.Lion

    Mr.Lion NI Product Owner

    Messages:
    159
    Oh dear, then I have to replace all my 'move_control' !

    But in this case I'll make it like this?;

    EBoxID[NumSounds] -> hide := 16
    LBtnID[NumSounds] -> hide := 16

    I'm going to try this when I get to my project.

    Best Regards
    Robin
     
  9. Big Bob

    Big Bob Forum Member

    Messages:
    606
    I hope you don't mean that literally:)

    You probably meant to use some specific control's index. NumSounds is actually one higher than the highest index!

    Rejoice,

    Bob
     
  10. Mr.Lion

    Mr.Lion NI Product Owner

    Messages:
    159
    No it was just as an example that I took in the hurry! :)

    I'm gonna try figure out how to make the Key Learn hide when using a ui_switch and the way ^ when I get home.

    Best Regards
    Robin