# [Help] Kontakt - Scripting Help Required



## Jvidia (Mar 24, 2012)

Hey Guys,

I've recently picked up kontakt and have attempted to create my own instrument, its a saxophone sample pack! I'm looking at doing some scripting but am slightly lost at this moment in time!

Heres my issues, I have the basics of scripting down, i have created a menu but want each preset of the menu to change a particular knob. (Hope that makes sense)

Here's a screen shot to show you what i mean!







And heres my current script!


```
on init
    set_ui_height_px(127)
	make_perfview

	declare $count

    
	declare ui_knob $Attack (0,1000000,1)
	declare ui_knob $Hold (0,1000000,1)
	declare ui_knob $Decay (0,1000000,1)
	declare ui_knob $Sustain (0,1000000,1)
	declare ui_knob $Release (0,1000000,1)
	declare ui_knob $Reverb(0,1000000,10000)
    

    declare ui_menu $y11ry
    add_menu_item ($y11ry, "Staccato", 0)
    add_menu_item ($y11ry, "Legato", 1)
    add_menu_item ($y11ry, "Crazy", 2)

move_control($y11ry, 3, 3)

    declare ui_menu $y11rx
    add_menu_item ($y11rx, "Hall", 0)
    add_menu_item ($y11rx, "Church", 1)
    add_menu_item ($y11rx, "Bathroom", 2)

move_control($y11rx, 6, 3)

	set_knob_unit($Attack, $KNOB_UNIT_MS)
	set_knob_unit($Hold, $KNOB_UNIT_MS)
	set_knob_unit($Decay, $KNOB_UNIT_MS)
	set_knob_unit($Sustain, $KNOB_UNIT_DB)
	set_knob_unit($Release, $KNOB_UNIT_MS)
    set_knob_unit($Reverb,$KNOB_UNIT_PERCENT)
	

set_control_help($Reverb,"Reverb: An effect whereby the sound produced by an amplifier or an amplified musical instrument is made to reverberate slightly.
")


    
	$Attack := get_engine_par($ENGINE_PAR_ATTACK, 0, find_mod(0,"ENV_AHDSR"), -1)
	$Hold := get_engine_par($ENGINE_PAR_HOLD, 0, find_mod(0,"ENV_AHDSR"), -1)
	$Decay := get_engine_par($ENGINE_PAR_DECAY, 0, find_mod(0,"ENV_AHDSR"), -1)
	$Sustain := get_engine_par($ENGINE_PAR_SUSTAIN, 0, find_mod(0,"ENV_AHDSR"), -1)
	$Release := get_engine_par($ENGINE_PAR_RELEASE, 0, find_mod(0,"ENV_AHDSR"), -1)
	
	make_persistent($Attack)
	make_persistent($Hold)
	make_persistent($Decay)
	make_persistent($Sustain)
	make_persistent($Release)
	make_persistent($Reverb)

	read_persistent_var($Attack)
	read_persistent_var($Hold)
	read_persistent_var($Decay)
	read_persistent_var($Sustain)
	read_persistent_var($Release)
    read_persistent_var($Reverb)
	
	set_knob_label($Attack, get_engine_par_disp($ENGINE_PAR_ATTACK, 0, find_mod(0,"ENV_AHDSR"), -1))
	set_knob_label($Hold, get_engine_par_disp($ENGINE_PAR_HOLD, 0, find_mod(0,"ENV_AHDSR"), -1))
	set_knob_label($Decay, get_engine_par_disp($ENGINE_PAR_DECAY, 0, find_mod(0,"ENV_AHDSR"), -1))
	set_knob_label($Sustain, get_engine_par_disp($ENGINE_PAR_SUSTAIN, 0, find_mod(0,"ENV_AHDSR"), -1))
	set_knob_label($Release, get_engine_par_disp($ENGINE_PAR_RELEASE, 0, find_mod(0,"ENV_AHDSR"), -1))
	
   
	message("")
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

	set_knob_label($Attack, get_engine_par_disp($ENGINE_PAR_ATTACK, 0, 0, -1))
end on

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

	set_knob_label($Hold, get_engine_par_disp($ENGINE_PAR_HOLD, 0, 0, -1))
end on

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

	set_knob_label($Decay, get_engine_par_disp($ENGINE_PAR_DECAY, 0, 0, -1))
end on

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

	set_knob_label($Sustain, get_engine_par_disp($ENGINE_PAR_SUSTAIN, 0, 0, -1))
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

	set_knob_label($Release, get_engine_par_disp($ENGINE_PAR_RELEASE, 0, 0, -1))
end on

on ui_control($Reverb)
    set_engine_par($ENGINE_PAR_RV_SIZE,$Reverb,-1,3,1)
end on
```

Hope someone can help 

Thanks!


----------



## Jvidia (Mar 24, 2012)

I know its only been a day but im desperate please any help!


----------



## Raptor4 (Mar 24, 2012)

Download the Big Bob's "TodsToms" instrument from this LINK. Open the script editor and have a look at the "Save" & "Recall" which makes the trick.


----------



## Raptor4 (Mar 25, 2012)

OK, while having my morning coffee I created this code for you - just add it at the bottom of your code:

```
on ui_control($y11rx) 
if($y11rx =0)
$Reverb :=400000
end if
if ($y11rx =1)
$Reverb :=800000
end if
if ($y11rx =2)
$Reverb :=200000
end if
end on
```

Regards


----------



## Jvidia (Mar 25, 2012)

Raptor4 @ Sun Mar 25 said:


> OK, while having my morning coffee I created this code for you - just add it at the bottom of your code:
> 
> ```
> on ui_control($y11rx)
> ...



Thank you, that works like a dream!

I added some code to make it respond to the reverb unit
on ui_control($y11rx)
if($y11rx =0)
$Reverb :=400000
*set_engine_par($ENGINE_PAR_RV_SIZE,$Reverb,-1,3,1)*
end if


----------



## Jvidia (Mar 26, 2012)

Would this work the same for ADHSR?


----------



## mk282 (Mar 26, 2012)

Pretty much the same:

set_engine_par(<engine parameter>,<value>,<group ID>,find_mod(<group ID>,<modulator name>),-1)

This is for setting attack, hold, decay, sustain, release, attack curve. For modulator intensity sliders (for internal modulators like LFO, envelope, etc.):

set_engine_par($ENGINE_PAR_INTMOD_INTENSITY,<value>,<group ID>,find_mod(<group ID>,<modulator name>),find_target(<group ID>,find_mod(<group ID>,<modulator name>),<modulation intensity slider name>))


----------



## Jvidia (Mar 27, 2012)

set_engine_par(<engine parameter>,<value>,<group ID>,find_mod(<group ID>,<modulator name>),-1) 

So that would be..

on ui_control($y11ry)
if($y11ry =0)
$Attack :=400000
set_engine_par(<engine parameter>,<value>,<group ID>,find_mod(<group ID>,<modulator name>),-1) 
end if


??


----------



## Jvidia (Mar 27, 2012)

Confused.com :(


----------



## Tod (Mar 27, 2012)

Jvidia @ Tue Mar 27 said:


> set_engine_par(<engine parameter>,<value>,<group ID>,find_mod(<group ID>,<modulator name>),-1)
> 
> So that would be..
> 
> ...



Hi Jvidia, which ahdsr envelope are you tying to control, the one on the Amplifier or for something else?


----------



## Jvidia (Mar 27, 2012)

Ive attached an image, sorry for the crappy writing ^^


----------



## Tod (Mar 27, 2012)

Actually I think all you need for that is something like this.  


```
declare ui_knob $Attack (0,1000000,1)
    set_knob_unit($Attack,$KNOB_UNIT_MS)
    set_knob_label($Attack,_get_engine_par_disp($ENGINE_PAR_ATTACK,0 ,0 ,-1))

on ui_control ($Attack)
  _set_engine_par($ENGINE_PAR_ATTACK,($Attack,0,0,-1)
  set_knob_label(($Attack,_get_engine_par_disp($ENGINE_PAR_ATTACK,0 ,0 ,-1))
end on
```


----------



## Raptor4 (Mar 27, 2012)

Tod, the OP has already have set that in his original post Code above (I guess he copied the code from from some other script).

Jvidia,
Here is a simple prototype for your quick menu settings:

```
on ui_control($y11ry) 
if($y11ry =0)
$Attack :=40000  
$count := 0 
   while ($count < $NUM_GROUPS) 
      set_engine_par($ENGINE_PAR_ATTACK, $Attack, $count, find_mod(0,"ENV_AHDSR"), -1) 
      inc($count) 
   end while 
 set_knob_label($Attack, get_engine_par_disp($ENGINE_PAR_ATTACK, 0, 0, -1))  
end if
if($y11ry =1) 
$Attack :=350000  
$count := 0 
   while ($count < $NUM_GROUPS) 
      set_engine_par($ENGINE_PAR_ATTACK, $Attack, $count, find_mod(0,"ENV_AHDSR"), -1) 
      inc($count) 
   end while 
 set_knob_label($Attack, get_engine_par_disp($ENGINE_PAR_ATTACK, 0, 0, -1))  
end if
if($y11ry =2) 
$Attack :=800000  
$count := 0 
   while ($count < $NUM_GROUPS) 
      set_engine_par($ENGINE_PAR_ATTACK, $Attack, $count, find_mod(0,"ENV_AHDSR"), -1) 
      inc($count) 
   end while 
 set_knob_label($Attack, get_engine_par_disp($ENGINE_PAR_ATTACK, 0, 0, -1))  
end if
end on
```
Change the MS time according to your taste. By the way you must re-design the Reverb menu according this example. In the reverb example you can control the UI knob only but not the engine parameter - my idea was to show the menu storage only ...


----------



## mk282 (Mar 28, 2012)

Why are you not using find_mod() in get_engine_par_disp() as well? 


I feel this is very important - naming the modulators and modulator intensity sliders. It does take some time to do it (especially if using a lot of modulators), but it makes scripting easier (since in Kontakt 4 you really DO NOT KNOW what's the actual ID number of a specific modulator or modulator intensity slider - Kontakt 5 adds this info, but I'm still naming my mods), especially if you use the latest KScript with the include library that makes setting and getting engine parameters a cinch:


```
group[0].mods["LFO 1"].target["LFO 1 -> PITCH"].mod_amt := $Knob
```


He he.


----------

