# Non-latching keyswitch script



## Leon Willett (Nov 26, 2012)

Hey everyone, 

I'm looking for a couple of scripts. If you know of a script that does this, please let me know! 

- A script that mutes all midi note on messages UNLESS specified keyswitches are held down 
- A script that mutes all midi note on messages WHEN specified keyswitches are held down

The type of keyswitch non-latching (it is only active while actually held down). 

Thanks a lot for any help or links


----------



## DynamicK (Nov 26, 2012)

Maybe *Mind Control* from* Orange Tree Samples* will be capable of this. I suggest dropping Gregg an email.


----------



## mk282 (Nov 26, 2012)

Only one g in Greg Schlaepfer from OTS.


----------



## Raptor4 (Nov 26, 2012)

Hi Leon,
I had some time to create some scrip for you. I hope it will match some of your requirements.

```
{***********************************************
Mute ONs v1.0
Author: www.audiogrocery.com
Written by: Ivan Kovachev 
Modified: November 26, 2012
*************************************************}
on init
  make_perfview
  message("AUDIOGROCERY TM")
  set_script_title("Mute ONs")
  set_ui_height(1)
  declare $i
  declare $ks_flag
  declare %note_reg[128]
  declare %note_reg2[128]
  declare %note_id[128]
  declare const $dB_center := 0
  declare ui_switch $On
  declare ui_value_edit $KS1(0, 127, 0) 
  set_text($KS1,"N.Mute KS")
  declare ui_knob $Mute_DB(0, 100, 1) 
  set_text($Mute_DB,"Suppr.dB")
  set_knob_unit($Mute_DB,$KNOB_UNIT_DB)
  declare ui_value_edit $Min(0, 127, 0) 
  declare ui_value_edit $Max(0, 127, 0) 
  declare ui_label $play_key_rng(2, 0) 
  set_text($play_key_rng,"Play notes key range")
  set_control_par(get_ui_id($play_key_rng),$CONTROL_PAR_TEXT_ALIGNMENT,1)
  $i := 0
  while ($i<127)
    set_key_color($i,$KEY_COLOR_NONE)
    inc($i)
  end while
  _read_persistent_var($KS1)
  make_persistent($Mute_DB)
  set_key_color($KS1,$KEY_COLOR_BLACK)
  move_control($KS1,2,1)
  move_control($Mute_DB,6,1)
  move_control($On,1,1)
  move_control($play_key_rng,4,1)
  move_control($Min,4,2)
  move_control($Max,5,2)
  make_persistent($On)
  make_persistent($KS1)
  make_persistent($Min)
  make_persistent($Max)
end on

on controller
  if ($CC_NUM=123)
    ignore_controller
  end if
end on

on ui_control($On)
  if ($On=0)
    note_off($ALL_EVENTS)
  end if
end on

on ui_control($KS1)
  $i := 0
  while ($i<127)
    set_key_color($i,$KEY_COLOR_NONE)
    inc($i)
  end while
  set_key_color($KS1,$KEY_COLOR_BLACK)
end on

on note
  if ($On=1)
    if ($EVENT_NOTE=$KS1)
      $ks_flag := 1
    end if
    if ($ks_flag>0 and in_range($EVENT_NOTE,$Min,$Max))
      ignore_event($EVENT_ID)
    end if
    if ($ks_flag=0 and in_range($EVENT_NOTE,$Min,$Max))
      %note_id[$EVENT_NOTE] := $EVENT_ID
      %note_reg[$EVENT_NOTE] := 127
    end if
    if ($EVENT_NOTE=$KS1)
      ignore_event($EVENT_ID)
      $i := 0
      while ($i<128)
        %note_reg2[$i] := %note_reg[$i]
        inc($i)
      end while
      $i := search(%note_reg,127)
      while ($i # -1)
        change_vol(%note_id[$i],$dB_center-($Mute_DB*1000),1)
        %note_reg[$i] := 0
        $i := search(%note_reg,127)
      end while
    end if
  end if
end on

on release
  if ($On=1)
    if ($EVENT_NOTE=$KS1)
      $ks_flag := 0
      ignore_event($EVENT_ID)
      $i := 0
      while ($i<128)
        %note_reg[$i] := %note_reg2[$i]
        inc($i)
      end while
      $i := search(%note_reg2,127)
      while ($i # -1)
        change_vol(%note_id[$i],$dB_center+($Mute_DB*1000),1)
        %note_reg2[$i] := 0
        $i := search(%note_reg2,127)
      end while
    end if
    if (in_range($EVENT_NOTE,$Min,$Max))
      %note_reg[$EVENT_NOTE] := 0
    end if
  end if
end on
```

How to operate:
1. Copy & paste the code in the KSP editor and hit "Apply".
2. Switch ON the "On" button.
3. Roll the "N.Mute KS" box to assign the key switch you want to use - it will be colored in black.
3. Set the musical playing note key range in the Min/Max boxes.
4. Assign the "Suppression" dB in the Knob to the right. Try some low setting 20-50 after that you can set it to full "Mute" setting 100 for example.
5. Save the script as an nrp or nki file to store your settings.

The KS behavior:
1. If the KS is held down and you try to play some note events which cover the Min/Max range, then the note events will be ignored.
2. If there are some playing notes and you hold down the KS then the volume of these notes will be set to the "Suppression" level you have set in the Suppr.dB Knob (if 100 they will be muted).
3. If there are some layering notes and you have been suppressed them (see step 2) after you release the KS the volume of these notes will be set back to their original.
Regards,

R4


----------



## Leon Willett (Nov 27, 2012)

Incredible! What a thing to wake up to in the morning!! :O

R4, sincerely thanks for your kindness. Installing now....


----------



## Raptor4 (Nov 27, 2012)

Leon Willett @ Tue Nov 27 said:


> Incredible! What a thing to wake up to in the morning!!


Heh, my grandfather always said "The Night is going to have a baby" :D .
Regards,
R4


----------

