# Specific key switch



## candyman99 (Feb 11, 2016)

Hi,

Can anyone help me, I search (need) a KS script for following:

I have 2 sample maps in Kontakt and I want to switch between them via keyswitch WITHOUT RELEASING A NOTE KEY. i got it simple via group start options, but with this option I must always press again a note key.

Please help!!!!
Thanks


----------



## szcz (Feb 11, 2016)

Do you mean, you have two groups and you want to switch to change between them?


----------



## candyman99 (Feb 11, 2016)

szcz said:


> Do you mean, you have two groups and you want to switch to change between them?


yes but I forgot to tell, both groups should start at the same time.

example:
I have 2 different loops in 2 groups.

loop range is from C1 up to C4. (if this is important to know?!)

keyswitch C0, only group 1 should playing (group 2 should play in background) then I press D0, only group 2 should play but without releasing a note key, you understand? sorry for the bad explanation, hope you or someone know what I'm need.


----------



## tonaliszt (Feb 11, 2016)

I would probably do it this way: Set it so both groups play always. Assign the volume mod of each group to a cc (100 or whatever). Set the volume mod so that for group 1 when the cc = 0 the volume is 100% and when the cc = 127 the volume is 0%. Do the opposite for group 2. Then you just script a key-switch that changes the value of the cc from 0 to 127 when pressed. Is that helpful?


----------



## candyman99 (Feb 12, 2016)

n.h said:


> I would probably do it this way: Set it so both groups play always. Assign the volume mod of each group to a cc (100 or whatever). Set the volume mod so that for group 1 when the cc = 0 the volume is 100% and when the cc = 127 the volume is 0%. Do the opposite for group 2. Then you just script a key-switch that changes the value of the cc from 0 to 127 when pressed. Is that helpful?


thank you, but how I do a script like this?


----------



## Lindon (Feb 12, 2016)

I dont have Kontakt with me right now but something like this?

```
on init
    declare group_one_real_volume
    declare group_two_real_volume

    group_one_real_volume := get_engine_par($ENGINE_PAR_VOLUME,0,-1,-1)
    group_two_real_volume := get_engine_par($ENGINE_PAR_VOLUME,1,-1,-1)

    declare key_switch_Group1 := 24  {C0}
    declare key_switch_Group2 := 26   {D0}

    {default to start with group 1}
    {turn off group 2}
    set_engine_par($ENGINE_PAR_VOLUME,0,1,-1,-1)
end on

on note
    if $EVENT_NOTE = key_switch_Group1 or $EVENT_NOTE = key_switch_Group2
        ignore_event($EVENT_ID)
    end if

    if $EVENT_NOTE = key_switch_Group1
        {turn off group 2}
        set_engine_par($ENGINE_PAR_VOLUME,0,1,-1,-1)
        {turn on group 1}
        set_engine_par($ENGINE_PAR_VOLUME,group_one_real_volume,0,-1,-1)
    end if

    if $EVENT_NOTE = key_switch_Group2
        {turn off group 1}
        set_engine_par($ENGINE_PAR_VOLUME,1,1,-1,-1)
        {turn on group 2}
        set_engine_par($ENGINE_PAR_VOLUME,group_two_real_volume,0,-1,-1)
    end if
end on
```


----------



## candyman99 (Feb 12, 2016)

Lindon said:


> I dont have Kontakt with me right now but something like this?
> 
> ```
> on init
> ...


LINDON!! You're the master!!, thank you very much, this seems like what I need, but kontakt tells me at the first line (declare group_one_real_volume) variable expected!! why??


----------



## candyman99 (Feb 12, 2016)

candyman99 said:


> LINDON!! You're the master!!, thank you very much, this seems like what I need, but kontakt tells me at the first line (declare group_one_real_volume) variable expected!! why??


OK, some "$", "()" expressions was missed, I fixed it and the scipt editor shows no error now (see below), but it doesn't work right but I think this should probably do the job what I'm searching for

```
on init
    declare $group_one_real_volume
    declare $group_two_real_volume

    $group_one_real_volume := get_engine_par($ENGINE_PAR_VOLUME,0,-1,-1)
    $group_two_real_volume := get_engine_par($ENGINE_PAR_VOLUME,1,-1,-1)

    declare $key_switch_Group1 := 24  {C0}
    declare $key_switch_Group2 := 26   {D0}

    {default to start with group 1}
    {turn off group 2}
    set_engine_par($ENGINE_PAR_VOLUME,0,1,-1,-1)
end on

on note
    if ($EVENT_NOTE = $key_switch_Group1 or $EVENT_NOTE = $key_switch_Group2)
        ignore_event($EVENT_ID)
    end if

    if ($EVENT_NOTE = $key_switch_Group1)
        {turn off group 2}
        set_engine_par($ENGINE_PAR_VOLUME,0,1,-1,-1)
        {turn on group 1}
        set_engine_par($ENGINE_PAR_VOLUME,$group_one_real_volume,0,-1,-1)
    end if

    if ($EVENT_NOTE = $key_switch_Group2)
        {turn off group 1}
        set_engine_par($ENGINE_PAR_VOLUME,1,1,-1,-1)
        {turn on group 2}
        set_engine_par($ENGINE_PAR_VOLUME,$group_two_real_volume,0,-1,-1)
    end if
end on
```


----------



## Lindon (Feb 12, 2016)

Oh sorry, yes I should have said... written in Sublime Text with the Nils plug-in, so you would need to add the $s etc. if you are "going native"


----------



## candyman99 (Feb 12, 2016)

but it does not work, have I change the ENGINE_PAR_VOLUME numbers too?


----------



## Lindon (Feb 12, 2016)

what "ENGINE_PAR_VOLUME" numbers?


----------



## Lindon (Feb 12, 2016)

hmm... my silly mistake:

```
if $EVENT_NOTE = key_switch_Group1
        {turn off group 2}
        set_engine_par($ENGINE_PAR_VOLUME,0,1,-1,-1)
        {turn on group 1}
        set_engine_par($ENGINE_PAR_VOLUME,group_one_real_volume,0,-1,-1)
    end if

    if $EVENT_NOTE = key_switch_Group2
        {turn off group 1}
        set_engine_par($ENGINE_PAR_VOLUME,0,0,-1,-1)
        {turn on group 2}
        set_engine_par($ENGINE_PAR_VOLUME,group_two_real_volume,1,-1,-1)
    end if
```


----------



## candyman99 (Feb 12, 2016)

How Can I say THANK YOU, LINDON!!!!
It works pretty fine, exact what I want!!!

One last question:

If I do something in the script editor, for example i delete empty spaces or delete your infos, the script doesn't works anymore... 

but if delete the whole script,
reset both group volumes to default
->apply
->paste script in again
->apply, 
the script works again! is this normal or is this a bug in kontakt?


----------



## Lindon (Feb 13, 2016)

these two lines are reading the current volume for each group at the start of the script:

$group_one_real_volume := get_engine_par($ENGINE_PAR_VOLUME,0,-1,-1)
$group_two_real_volume := get_engine_par($ENGINE_PAR_VOLUME,1,-1,-1)


take them out if you want to.

You should be able to remove stuff like empty spaces without it stopping working.


----------



## candyman99 (Feb 13, 2016)

thank you again!!! btw is this script working just with 2 groups or can I use it anytime? if I have 7,8,10 sample groups, this is only works in group one and two, right?


----------



## Lindon (Feb 13, 2016)

Yes only groups 1 and 2, you could add 2 menus to allow you to decide which groups to be using from all those in the instrument...


----------



## candyman99 (Feb 13, 2016)

you mean

declare $group_six_real_volume
declare $group_seven_real_volume

for example I have 10 groups and want this between group six and seven? of course to change the respective parameters, variables etc


----------



## Lindon (Feb 14, 2016)

nope. Lets say you have lots of groups but you want to switch between any two of them:

you will need to declare two menus, each of which will need to be populated with the number and name of each group so in your on init:



```
declare $idx

    declare menu_one
    for $idx := 0 to ($NUM_GROUPS -1 )
        add_menu_item (menu_one, group_name($idx),$idx)
    end for

    declare menu_two
    for $idx := 0 to ($NUM_GROUPS -1 )
        add_menu_item (menu_two, group_name($idx),$idx)
    end for
```

Lets also say you want to set the volumes of these "switching" groups when you switch to them, you will need two sliders(or knobs) to define the volumes you want

Again in your init:

```
declare ui_slider volume_one(0,1000)

    declare ui_slider volume_two(0,1000)
```

So your all up finished code looks like this, check where I have removed fixed numbers (like 0 and 1) and replaced them with values from these newly declared widgets:


```
on init
  declare $idx

  declare $menu_one
  $idx := 0
  while ($idx<$NUM_GROUPS)
    add_menu_item($menu_one,group_name($idx),$idx)
    inc($idx)
  end while
  make_persistent($menu_one)

  declare $menu_two
  $idx := 0
  while ($idx<$NUM_GROUPS)
    add_menu_item($menu_two,group_name($idx),$idx)
    inc($idx)
  end while
  make_persistent($menu_two)
  read_persistent_var($menu_two)

  declare ui_slider $volume_one(0, 1000) 
  make_persistent($volume_one)

  declare ui_slider $volume_two(0, 1000) 
  make_persistent($volume_two)

  declare $key_switch_Group1 := 24
  declare $key_switch_Group2 := 26

  set_engine_par($ENGINE_PAR_VOLUME,0,1,-1,-1)
  set_engine_par($ENGINE_PAR_VOLUME,$volume_one*1000,$menu_one,-1,-1)

end on

on ui_control($menu_one)
  $idx := 0
  while ($idx<$NUM_GROUPS)
    if ($idx # $menu_one and ($idx # $menu_two))
      set_engine_par($ENGINE_PAR_VOLUME,$idx,0,-1,-1)
    end if
    inc($idx)
  end while
end on

on ui_control($menu_two)
  $idx := 0
  while ($idx<$NUM_GROUPS)
    if ($idx # $menu_one and ($idx # $menu_two))
      set_engine_par($ENGINE_PAR_VOLUME,$idx,0,-1,-1)
    end if
    inc($idx)
  end while
end on

on note
  if ($EVENT_NOTE=$key_switch_Group1 or ($EVENT_NOTE=$key_switch_Group2))
    ignore_event($EVENT_ID)
  end if
  if ($EVENT_NOTE=$key_switch_Group1)
    set_engine_par($ENGINE_PAR_VOLUME,0,$menu_two,-1,-1)
    set_engine_par($ENGINE_PAR_VOLUME,$volume_one*1000,$menu_one,-1,-1)
  end if
  if ($EVENT_NOTE=$key_switch_Group2)
    set_engine_par($ENGINE_PAR_VOLUME,0,$menu_one,-1,-1)
    set_engine_par($ENGINE_PAR_VOLUME,$volume_two*1000,$menu_two,-1,-1)
  end if
end on
```


----------



## candyman99 (Feb 14, 2016)

thank you very very much!!!!


----------

