# Purge unselected groups



## Old Timer (Jun 2, 2018)

I'm working on a Kontakt instrument that has a menu to select different sounds, each of which has its own group. It works fine. I select a sound. It plays. I select another sound, that plays. However all the sounds are loaded up into RAM, not just the one I've selected.

Could someone point me to the relevant section in the KSP manual that would show me how to purge all but the group I've selected, or only load the group I select? 

Or is there a tutorial out there that has examples of the necessary code to achieve this?

I've tried Googling it but I'm not even sure what search terms to Google.

Thanks (as ever) in advance.


----------



## geronimo (Jun 2, 2018)

Old Timer said:


> Or is there a tutorial out there that has examples of the necessary code to achieve this?
> 
> I've tried Googling it but I'm not even sure what search terms to Google.



Many topics evoke this: https://vi-control.net/community/threads/group-purge-allow-disallow-on-ui-control.56423/

https://vi-control.net/community/th...r-a-lot-of-groups-at-once.43898/#post-3852979

https://vi-control.net/community/threads/purge-group.37264/


----------



## EvilDragon (Jun 2, 2018)

You'll need purge_group() command, see descripting in KSP reference.


----------



## Old Timer (Jun 2, 2018)

At the moment I have this:

on note
disallow_group($ALL_GROUPS)
allow_group($Menu)
end on

I tried changing it to:

on ui_control ($Menu)
purge_group(0,abs($Menu-1))
end on

But that now seems to play all groups at once and nothing is purging. BTW, I have 17 groups...

Don't laugh!


----------



## d.healey (Jun 2, 2018)

You need a loop to purge all of your groups. Then on a separate line you need your statement to unpurge the selected group.

Actually... can you use $ALL_GROUPS in the purge command? I don't recall ever trying this.


----------



## EvilDragon (Jun 2, 2018)

$ALL_GROUPS doesn't work with purge_groups() IIRC. Or at least doesn't work reliable. Just loop it!


----------



## Old Timer (Jun 2, 2018)

Does this make any sense at all?


```
on init
   declare const $First_Group := 0  (defines how many groups I have)
   declare const $Last_Group := 16

 declare ui_menu $Menu (Menu for groups)

   declare $i

   $i := $First_Group
   while ($i<=$Last_Group)
     add_menu_item($Menu_group_name($i),$i) (Add each of the groups as I've named them)
     add_menu_item($Menu_group_name($i),$i)
     add_menu_item($Menu_group_name($i),$i)
     add_menu_item($Menu_group_name($i),$i)
     inc($i)
   end while


on ui_control ($Menu)
   $i := $First_Group
   while ($i<=$Last_Group
     purge_group($i,0)
     inc($i)
   end while
   purge_group($Menu,1)
end on
```


----------



## Ecliptiq Audio (Jun 2, 2018)

Hi,

There is a built-in variable called $NUM_GROUPS, which gives you the number of groups you have in your instrument, so there's no need for the constants, just use the $i from 0 to $i<$NUM_GROUPS.


----------



## Old Timer (Jun 2, 2018)

Like this?


```
on init
 declare ui_menu $Menu

   declare $i

   $i := 0
   while ($i<$NUM_GROUPS
     add_menu_item($Menu_group_name($i),$i)
     add_menu_item($Menu_group_name($i),$i)
     add_menu_item($Menu_group_name($i),$i)
     add_menu_item($Menu_group_name($i),$i)
     inc($i)
   end while


on ui_control ($Menu)
   $i := $0
   while ($i<=$NUM_GROUPS
     purge_group($i,0)
     inc($i)
   end while
   purge_group($Menu,1)
end on
```


----------



## Ecliptiq Audio (Jun 2, 2018)

Yup, just don't forget to correct the $Menu callback too.
It should be: 

```
on ui_control ($Menu)
   $i := 0
   while ($i<$NUM_GROUPS)
     purge_group($i,0)
     inc($i)
   end while
   purge_group($Menu,1)
end on
```


----------



## Old Timer (Jun 2, 2018)

I'm getting 'expression expected' in the fourth line of code. Any ideas. $Plinks is the name of my menu by the way.


```
declare $i

   $i := 0
   while ($i<$NUM_GROUPS)

  add_menu_item($Plinks,"O Coast Soft"($i),$i)
  add_menu_item($Plinks,"0 Coast Bright"($i),$i)
  add_menu_item($Plinks,"SH-09 Pop"($i),$i)
  add_menu_item($Plinks,"Hi Bells"($i),$i)
  add_menu_item($Plinks,"Silver Plink"($i),$i)
  add_menu_item($Plinks,"Mustang Bass"($i),$i)
  add_menu_item($Plinks,"Harmonics"($i),$i)
  add_menu_item($Plinks,"Music Box"($i),$i)
  add_menu_item($Plinks,"Uke Plink"($i),$i)
  add_menu_item($Plinks,"Glass"($i),$i)
  add_menu_item($Plinks,"Simple"($i),$i)
  add_menu_item($Plinks,"Wheel"($i),$i)
  add_menu_item($Plinks,"Telecaster"($i),$i)
  add_menu_item($Plinks,"Banjolele"($i),$i)
     inc($i)
   end while
```


----------



## Ecliptiq Audio (Jun 2, 2018)

You don't need the while loop here, just simply add the menu items. But in that case you have to manually write the number of the item to the $i. (0 to the 1st item, 1 to the 2nd etc.).
If you want to do it with a while loop, you have to store the menu item names in a string array.

And take out the ($i) from the menu item names. For example just "O Coast Soft", not "O Coast Soft"($i)


----------



## Old Timer (Jun 2, 2018)

That's weird. I've got it to load only samples from the selected group, but now the key colour mapping to the sample range has stopped working.


```
on init
    set_control_par_str($INST_ICON_ID,$CONTROL_PAR_PICTURE,"OT pic")

make_perfview
set_ui_height_px (201)

declare $count
declare $groups_menu
declare $note
declare  $label_id:=4
declare const $DIGrev_slot:=1
    declare $i
    declare %key_low[16] := (36, 24,24, 36, 24,41,24,46,48,60,36,36,36, 53)
    declare %key_high[16] := (89, 96, 96,96,89,84, 96,96, 96,89,96, 72,84, 86)


{----SETUP ARRAYS---}

  declare ui_menu $Plinks
  add_menu_item($Plinks,"O Coast Soft",2) 
  add_menu_item($Plinks,"0 Coast Bright",1) 
  add_menu_item($Plinks,"SH-09 Pop",0)
  add_menu_item($Plinks,"Hi Bells",3)
  add_menu_item($Plinks,"Silver Plink",4)
  add_menu_item($Plinks,"Mustang Bass",5)
  add_menu_item($Plinks,"Harmonics",6)
  add_menu_item($Plinks,"Music Box",7)
  add_menu_item($Plinks,"Uke Plink",8)
  add_menu_item($Plinks,"Glass",9)
  add_menu_item($Plinks,"Simple",10)
  add_menu_item($Plinks,"Wheel",11)
  add_menu_item($Plinks,"Telecaster",12)
  add_menu_item($Plinks,"Banjolele",13)


set_control_par_str(get_ui_id($Plinks),$CONTROL_PAR_PICTURE,"menu")
set_control_par(get_ui_id($Plinks),$CONTROL_PAR_FONT_TYPE, $label_id)
move_control_px($Plinks,105,85)
make_persistent($Plinks)

    message("Love your Plink!")


declare ui_slider $Mid (0, 800000)
hide_part($Mid,  $HIDE_PART_BG .or. $HIDE_PART_MOD_LIGHT .or. $HIDE_PART_TITLE .or. $HIDE_PART_VALUE)
set_control_par_str(get_ui_id($Mid),  $CONTROL_PAR_PICTURE, "knob_100_gold")
move_control_px ($Mid,120,8)
set_control_par(get_ui_id($Mid), $CONTROL_PAR_MOUSE_BEHAVIOUR, -500)


declare ui_slider $Attack (0, 1000000)
hide_part($Attack,  $HIDE_PART_BG .or. $HIDE_PART_MOD_LIGHT .or. $HIDE_PART_TITLE .or. $HIDE_PART_VALUE)
set_control_par_str(get_ui_id($Attack),  $CONTROL_PAR_PICTURE, "knob_100_gold")
move_control_px ($Attack,490,8)
set_control_par(get_ui_id($Attack), $CONTROL_PAR_MOUSE_BEHAVIOUR, -500)

declare ui_slider $Release (0, 1000000)
hide_part($Release,  $HIDE_PART_BG .or. $HIDE_PART_MOD_LIGHT .or. $HIDE_PART_TITLE .or. $HIDE_PART_VALUE)
set_control_par_str(get_ui_id($Release),  $CONTROL_PAR_PICTURE, "knob_100_gold")
move_control_px ($Release,490,77)
set_control_par(get_ui_id($Release), $CONTROL_PAR_MOUSE_BEHAVIOUR, -500)


declare ui_slider $Sat (0, 1000000)
hide_part($Sat,  $HIDE_PART_BG .or. $HIDE_PART_MOD_LIGHT .or. $HIDE_PART_TITLE .or. $HIDE_PART_VALUE)
set_control_par_str(get_ui_id($Sat),  $CONTROL_PAR_PICTURE, "knob_100_gold")
move_control_px ($Sat,305,8)
set_control_par(get_ui_id($Sat), $CONTROL_PAR_MOUSE_BEHAVIOUR, -500)


declare ui_slider $Velocity (0,1000000)
hide_part($Sat,  $HIDE_PART_BG .or. $HIDE_PART_MOD_LIGHT .or. $HIDE_PART_TITLE .or. $HIDE_PART_VALUE)
set_control_par_str(get_ui_id($Velocity),  $CONTROL_PAR_PICTURE, "knob_100_gold")
$Velocity := get_engine_par($ENGINE_PAR_MOD_TARGET_INTENSITY,0, find_mod(0,"VEL_VOLUME"),-1)
move_control_px ($Velocity,305,77)
set_control_par(get_ui_id($Velocity), $CONTROL_PAR_MOUSE_BEHAVIOUR, -500)



set_knob_defval ($Mid,500000) {Sets the Default Values}
set_knob_defval ($Attack,0)
set_knob_defval ($Release,1000000)
set_knob_defval ($Velocity,750000)
set_knob_defval ($Sat,500000)

make_persistent ($Mid)
make_persistent ($Attack)
make_persistent ($Release)
make_persistent ($Velocity)
make_persistent ($Sat)

read_persistent_var($Mid)
read_persistent_var($Attack)
read_persistent_var($Release)
read_persistent_var($Velocity)
read_persistent_var($Sat)

end on


function KeyColor()
    $i := 0
    while ($i < 128)
        if (in_range($i, %key_low[$Plinks], %key_high[$Plinks]))
            set_key_color($i, $KEY_COLOR_BLUE)
        else
            set_key_color($i, $KEY_COLOR_INACTIVE)
        end if

        inc($i)
    end while
end function


{----CALLBACKS---}



on persistence_changed
    call KeyColor()
end on

on ui_control ($Plinks)
    call KeyColor()
end on

on ui_control ($Plinks)
    call KeyColor()
end on


on ui_control ($Plinks)
   $i := 0
   while ($i<$NUM_GROUPS)
     purge_group($i,0)
     inc($i)
   end while
   purge_group($Plinks,1)
end on






{---MODULATION---}

on ui_control ($Velocity)
   $count := 0
   while($count < $NUM_GROUPS)
set_engine_par($ENGINE_PAR_MOD_TARGET_INTENSITY,$Velocity,$count,find_mod($count,"VEL_VOLUME"),-1)
inc($count)
   end while
end on

on ui_control ($Mid)
set_engine_par($ENGINE_PAR_SEQ_HMF_GAIN,$Mid, -1, 0, 1)
end on

on ui_control ($Attack)
$count := 0
while ($count < $NUM_GROUPS)
set_engine_par($ENGINE_PAR_ATTACK, $Attack, $count, find_mod(0,"ENV_AHDSR"), -1)
inc($count)
end while
end on

on ui_control ($Release)
$count := 0
while ($count < $NUM_GROUPS)
set_engine_par($ENGINE_PAR_RELEASE, $Release, $count, find_mod(0,"ENV_AHDSR"), -1)
inc($count)
end while
end on


on ui_control ($Sat)
_set_engine_par ($ENGINE_PAR_SHAPE,$Sat, -1,$DIGrev_slot,1)
set_control_par_str(get_ui_id($Sat),$CONTROL_PAR_LABEL,get_engine_par_disp($ENGINE_PAR_SHAPE, -1,$DIGrev_slot,1) & " dB")

end on
```


----------



## Ecliptiq Audio (Jun 2, 2018)

There are 3 ui_control($Plinks). You can only have one.


----------



## Old Timer (Jun 2, 2018)

PeterMilkovic said:


> There are 3 ui_control($Plinks). You can only have one.



Doh! I am a baboon. Thanks so much Peter. And thanks ED and d.healey too. It actually works!


----------



## P.N. (Jun 2, 2018)

Hi, Old Timer.

I provided an example for a similar case here:

https://vi-control.net/community/threads/help-with-group-menu-solved.70401/

It might need adjustments, but maybe it can be useful, never-the-less.

Cheers,

Paulo


----------



## Old Timer (Jun 2, 2018)

P.N. said:


> Hi, Old Timer.
> 
> I provided an example for a similar case here:
> 
> ...


Thanks Paulo / I'll take a look at it. I have so much to learn!


----------



## geronimo (Jun 2, 2018)

I think we can remove what concerns the $DIGrev_slot constant .


----------



## Old Timer (Jun 2, 2018)

Oh yeh - thanks. I think that's hanging around from an earlier version. Doesn't seem to be doing anything but better not to be there.


----------

