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 for the time the knob gets moved. After releasing the knob the default label shows up again after a certain delay. See video

Also see these more conveniant solutions, using less code


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

Any Questions? 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. defining the labels for our Images
  4. setting variables for the watchdog timer (dynamic labels)
  5. defining all the other ui elements
  6. The actual watchdog timer function (Dynamic Knob Label’s)
  7. full code of all Knobs

Download all files to this tutorial premium

  • Open Resource Container: Buttons, Knobs, Wallpapers
  • NKI file: the working script
  • the script as txt file

DOWNLOAD


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

 

 


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” and is displaying the knob’s value
  3. a certain wait period prevents a following “fall-back-to-original-label”-code to take action, which is setting the label back to its original state
  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 just runs out and the default label gets restored.

2. Setting ui space, wallpapers & global vars

{#####################################################################################
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)

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

3. Defining the labels for our Images

{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_%"

labelnow we define the knob’s default labels which are holding the images showing the knob’s units. For this tutorial we only take a closer look at 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.

We also need a blank image because when the the knob is turned we want to display the knob’s values suplied by the Kontakt engine.

Except the blank image we store all other label images (their filenames) into a string array !arr_lbl_units[] since it is easier to work with later on.

4. Setting variables for the watchdog timer (dynamic labels)

 

{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}

here we decalre and prepare alle the variables for our watchdog timer. When a knob is turned the current KSP clock time will be stored into a time stamp array (%arr_timestamp). Thereby each knob uses an individual time stamp.

With the $TOTAL_KNOB_COUNT constant above we can easily extend that array depending on how many knobs or dynamic labels we are using for our instrument.

The $wait_time and the $wait_flag  will be explained later

premium content

 

2 Responses to “Kontakt Scripting (KSP) :: watchdog timer – change knob or slider labels on mouse events (on click, on release) [dynamic labels]”

    • YummyBeats

      Thx! No sorry, currently there's no raw material or templates (except the script) included. But the Kontakt resources are always open, so you can access and analyze any png files (like custom knobs, buttons etc) and maybe learn or utilize some techniques to build and design your own instruments. You know, these tutorials are rather about the scripting not about the graphic design, so the instrument design is always very rudimental and nothing you wanna copy :) But if you are missing some technical templates like for measuring and stuff, let me know.

Leave a Reply

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

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

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