premium article

Posted by & filed under KSP Scripting (NI Kontakt) - Tutorials, KSP: Custom Scripts & Functions (Plugins).

In this tutorial we will learn how to dynamically change labels and display the knob’s current value as long as it gets turned. After releasing the knob the default label shows up again. See video

It is recommended to follow the User Interface with multiple Screens tutorial first.

Any Questions? Ask here or use the comments below

License: This ksp script is licensed under a Creative Commons Attribution 4.0 International License.
You may use it for your projects (also commercially) and edit it as you like, as long as you keep all credits

Requirements: no requirements, the script works without sublime 3 compiling

Unfortunately in Kontakt we can not receive any mouse event like “mouse over”, “on click”, “on release” etc which would have made this pretty easy. So instead of a mouse event, when a knob is tuned the “on ui_control” callback gets called which stubbornly “reels off” our code. On mouse release the “on ui_control” simply is not executed anymore so there’s no possibility to call any function or code on a mouse release. That’s why we have to make up a workaround using a watchdog or time monitoring and time delays.


Content

  1. how it works (in simple words)
  2. setting ui space, wallpapers & global vars
  3. setting the default label images
  4. setting variables for the dynamic labels
  5. setting all other ui elements like knobs, buttons, etc
  6. the Knobs & Dynamic Label Function (on ui_control)
  7. complete code of all Knobs
  • Open Resource Container: Buttons, Knobs, Wallpapers  premium
  • NKI file: the working script premium
  • the script as txt file

DOWNLOAD


1. This is how it works (in simple words)

  1. a default label is displaying the knob’s units
  2. when turning a knob the label “swaps” displaying the knob’s value now.
  3. after on a certain wait period is added, which pauses the following “fall-back-to-original-label”-code
  4. when the knob is still turned this fall-back gets aborted and the waiting period gets renewed.
  5. when releasing the knob the waiting period runs out and the default label gets restored.

2. Setting ui space, wallpapers & global vars

First we set the ui area and make it visible outside the script tab. Also we set the global vars. You can read this chapter in detail in the User Interface with multiple Screens tutorial.

{#####################################################################################
This script is licensed under a Creative Commons Attribution 4.0 International License.
http://creativecommons.org/licenses/by/4.0/

- yummybeats.com
- more KSP tutorials at blog.yummybeats.com
#######################################################################################}

on init 
    {INIT UI}
        set_ui_height_px(540)
        make_perfview
        set_script_title("Dyanmic Labels")
        set_control_par_str($INST_ICON_ID,$CONTROL_PAR_PICTURE,"yb_ico")

    {DECLARE : Global Vars}
        declare $count
        declare const $TOTAL_KNOB_COUNT := 4 {set the total number of knobs in your UI}

    {SET: WALLPAPERS}
        declare @wp_init
        declare @wp_about
        @wp_init := "bg_main"
        set_control_par_str($INST_WALLPAPER_ID,$CONTROL_PAR_PICTURE,@wp_init)

3. Setting the default labels

 

labelnow we set the knob’s default label images showing the knob’s units. For this demonstration we are using the LoFi FX, so  the knob’s units are: bits (bit crusher), Hz (sampling rate),  dB (noise level) and % (noise color). Of course you can use any units you like or even create icons. Just create your own images.

Also we need a blank image because when the the knob is turned we want to display the knob’s values.

Except the blank image we store all other label images (filenames) into a string array !arr_lbl_units[] since it is easier to work with later on. But you can also use individual @variables for each image

{declare bottom knob label images & text vars}
            declare @display_val {holds the current knob value on mouse event}
            declare @lbl_blank 
            @lbl_blank := "lbl_blank" {set blank bg image when turning the knob}
            declare !arr_lbl_units[4] {declare array which holds allunit images}
            !arr_lbl_units[0] := "lbl_bits" 
            !arr_lbl_units[1] := "lbl_hz"
            !arr_lbl_units[2] := "lbl_db" 
            !arr_lbl_units[3] := "lbl_%"

4. Setting variables for the dynamic labels

now we prepare the watchdog timer or time monitoring and its variables. When a knob is turned the current KSP clock time will be stored into the time stamp array (%arr_timestamp). Thereby each knob uses an individual time stamp so you need to extend that array depending on how many knobs or dynamic labels you are using for your instrument. You can simply edit the $TOTAL_KNOB_COUNT constant above.

Hint :The KSP clock runs endlessly when Kontakt gets started. You can always access the current time via $KSP_TIMER We don’t have to worry about this clock we just need to store it into our array like %arr_timestamp[0]:= $KSP_TIMER Don’t worry, the complete function will be explained later.

The $wait_time and the $wait_flag  will be explained later as well

{SET : Watchdog Timer & dynamic labels}        
        {declare global vars}
            declare $wait_time := 2000 {ms}
            declare %arr_timestamp[$TOTAL_KNOB_COUNT] {array which holds the current engine uptime for each knob (in microseconds). Gets refreshed upon each mouse event}
            declare $wait_flag := 1

            $wait_time := $wait_time * 1000 {convert to milliseconds}

premium content

 

Leave a Reply

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