ksp

Last modifiedby & filed under Kontakt Scripting (KSP) - Basics, UIs, Tutorials, Scripts and Tools, Kontakt Scripting (KSP) :: Basics, KSP: References.

The on ui_control callback gets executed whenever any ui element is used. So it is an important callback when it comes to creating a custom UI. This articles explains all you need to know about this callback.

content

basic knowledge

The code in an on ui_control callback gets executed whenever an ui element is used (like hitting buttons, turning knobs, moving sliders, changing tables, xy-pads, etc)

The on ui_control callback is probably the most important callback when it comes to creating custom UIs as this callback is used for communication between the visual and the Kontakt engine.

Of course it is used a lot to build dynamic UIs (like shown below)

the on ui_control() is also used to build dynamic UI’s:

Remove the row

Column: 1

ksp-kontakt-draggables
Draggables
full script + nki

Column: 2

ksp-dialog-animated-screenshot
Dialog Windows
full script + nki

Column: 3

ksp-full-drawer-animated-screenshot
Drawer
full script + nki

Remove the row

Column: 1

ksp-dynamic-wallpaper-animated-screenshot

Dynamic Wallpapers
full script + nki

Column: 2

ksp-scrollbar-vertical-animated-screenshot

Scroll-Bars
full script + nki

Column: 3

 

common usage

syntax

on ui_control(<ui_element_name>)
  // code in here gets executed when moving or changing the associated ui_element
end on

first we declare an ui_element like a knob, button, slider etc inside the on init callback like so:

on init       
    declare ui_button $my_button
    declare ui_slider $my_slider(0,10000000)
    declare ui_table %my_table[8](400, 100, 16)
end on

 


on ui_control($my_button)

on ui_control($my_slider)

on ui_control(%my_table)
then we simply put it’s variable name inside the brackets to associate the ui_element with the on ui_control callback.
When moving or changing the ui_element, the associated ui_control and all code in it is executed.

examples

A) output of a message when pressing a button

this is a very basic example

on init 
  declare ui_switch $hit_me
  make_perfview
end on

on ui_control($hit_me)
  message("ouch, you hit me")
end on

declare ui_switch $hit_me

first we declare a simple button or switch


make_perfview

keeps the UI visible outside the edit mode


on ui_control($hit_me) ... end on

we associate the button to the ui_control by just putting the buttons variable name between the brackets.
now being associated, the callback and the message command in it will be executed whenever the button is pressed:

message("ouch, you hit me")

in this case it is just a simple message displayed in the lower status bar of Kontakt

B) displaying a Knob’s / slider’s value

A label called $value shows a slider’s current value whenever the slider is moved.

 

on init
    declare ui_slider $my_slider(0,1000000)
    declare ui_label $value(1, 1)
    make_perfview
end on

on ui_control($my_slider)
    set_control_par_str(get_ui_id($value),$CONTROL_PAR_TEXT,$my_slider)
end on

declare ui_slider $my_knob(0,1000000) declares a slider. 0 is the min value, when the slider is on left position. 1000000 is the max value when the slider is on the right position.

declare ui_label $value(1, 1) declares a label which will display the knob’s current value

make_perfview makes the ui visible outside the edit mode

on ui_control($my_knob) defines the ui control which is “linked” with $my_knob. To link the ui control to any other element simply change $my_knob to your ui variable. We can add as many on ui_controls as we want. Well almost as many. There is a limit due to Kontakt resources but its hard to reach that limit so we don’t care right now.

set_control_par_str(get_ui_id($value),$CONTROL_PAR_TEXT,$my_knob) this displays the currents knob’s value as label text. This line gets executed again and again whenever the knob is turned. So the label text gets always updated.

C) knob to control the main volume via set_engine_par

ksp-on-ui_control-mainvolume

 

on init       
    declare ui_knob $volume(0, 1000000,10000)    
    set_knob_unit($volume,$KNOB_UNIT_DB)
    declare @disp
    make_perfview      
end on      

on ui_control($volume)                 
    set_engine_par($ENGINE_PAR_VOLUME,$volume,-1,-1,-1)
    @disp := get_engine_par_disp($ENGINE_PAR_VOLUME,-1,-1,-1)
    set_knob_label($volume, @disp)
end on

declare ui_knob $volume(0, 1000000,500) 

first we declare a simple knob with the range 0 to 1.000.000.
0 is the min value and 1.000.000 is the max value.

Note: almost all the Kontakt engine parameters like volume, pan, fx, EG, LFOs work with values from 0 to 1.000.000 So in this case a value of 0 for the volume represents  -inf dB and 1.000.000 is +12.dB. Also read more about this in the set_engine_par() article


set_knob_unit($volume,$KNOB_UNIT_DB)

then we set the knob’s unit to dB.


declare @disp

we declare a helper variable for storing the real volume coming from the Kontakt engine.


on ui_control($volume) ... end on

once again, when turning the $volume knob, this callback and all the code in it gets executed:

 

set_engine_par($ENGINE_PAR_VOLUME,$volume,-1,-1,-1)

first we use the set_engine_par to send the knob’s current value, to the Kontakt engine.
The knob’s current value is hold inside $volume and it gets automatically updated when we turn the knob.

the Kontakt engine then interprets all the values that that we submit and knows which engine parameter we want to address and of course it also gets updated with the new volume value that we submit via $volume

 

@disp := get_engine_par_disp($ENGINE_PAR_VOLUME,-1,-1,-1)

actually we want to show the real volume data when turning the knob. But remember our knob’s still holds some inconclusive values from 0 to 1.000.000
So if we set the main volume to 3 dB our knob would show something like 707143 instead of +3dB
No one would be able to tell the actual Volume then. Even worse, remember the knob already shows the unit dB. So it says 707143 dB. Imagine 707143 dB hitting your ears

Long story short via get_engine_par_disp() we fetch the the real volume data directly from the Kontakt engine and store it into the @disp variable.

 

set_knob_label($volume, @disp)

lastly we simply pass the correct volume data to our knob’s label

Of course we can also directly pass the correct volume data without using @disp at all, like so:

set_knob_label($volume, get_engine_par_disp($ENGINE_PAR_VOLUME,-1,-1,-1))

D) Controlling FX, ADSR, BUSES LFO’s etc

The on ui_control part works exactly like with the other examples above.

Please proceed to the set_engine_par article now, which explains these scenarios in detail.

to the set_engine_par article

How useful was this article?

something you didn't like? Please tell us before you rate!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

NOTE: highly negative votes may be removed without any reasonable given feedback!

Tell us how we can improve this post?

Leave a Reply

use <pre> </pre> to wrap code blocks

use <code> </code> to wrap small code snippets

use basic html to style your comment

Your email address will not be published. Required fields are marked *