Posted by & filed under KSP Scripting (NI Kontakt) - Tutorials, KSP: Basics.

The Basic code structure is very simple. We basicaly put most of our code into  callbacks

In general the code inside callbacks gets executed when specific events happen. For example playing a note on our keyboard always executes the “on note” callback.

So callbacks build a framework for our ui and for communicating with different parts of the Kontakt engine.

Imagine it like that: an event triggers the related callback which is executing all our code inside it. Thereby sometimes the same callback can be called multiple times. For example playing 2 notes on our keyboard will execute 2 instances of the on note ... end on callback.

There are more callbacks as introduced in this article but with the ones introduced here we can already create awesome instruments.

Note: some commands & constants can only be used inside specific callbacks. For example the  wait() & purge() command can’t be used inside the init callback.

on init […] end on

the on init callback gets executed automatically on every start of our instrument. It also gets executed when hitting the apply button, when loading snapshots or when restarting the Kontakt engine (panic button).

The init callback will be the starting point for most of our scripts. Because inside on init ... end on  we have to put in all the initial code (like variables, ui elements, etc) we are even only allowed to declare new variables, arrays and UI controls (knobs, buttons, menus,…) here.

So the init callback is probably the most used callback and part of most of our scripts.

Example: simple init

on init
   set_ui_height_px(540)
   make_perfview
   declare ui_switch $switch
   declare ui_knob $knob (0,1000000,100)
   make_persistent($knob)
   message ("")
end on

on note […] end on

this callback is executed when we play a note. Several instances can be called at once when we play multiple notes on our keyboard.

We can use it to trigger other events whenever playing a note (like led lights for our ui) or of course we can use it to manipulaute the played notes themselves.

For example we can create a round robin application for more realistic instruments or we can create simple intervals & chords, like in the example below.

Example (this will add two additional notes, creating a chord)

on note
   play_note($EVENT_NOTE+4,$EVENT_VELOCITY,0,-1)
   play_note($EVENT_NOTE+7,$EVENT_VELOCITY,0,-1)
end on

on release […] end on

same as on note but executed when releasing a key

on ui_control(<variable>) […] end on

gets executed when using the associated UI elements (knobs, buttons, switches, menus etc.)

To associate an ui element we simply have to put its variable name after on ui_control. But we first need to declare the ui elements inside the on init. We can assign any variable we want like declare ui_button $MySuperButtonToControlTheWorld (of course you don’t wanna use that long variable names unless you like wounded fingers).

on init
  make_perfview
  declare ui_button $MySuperButtonToControlTheWorld
end on


on ui_control ($MySuperButtonToControlTheWorld) 
  {our master piece of a code waiting to be executed when pushing the button}
end on

For each of our already declared controls we can add an individual on ui_controlWe can add as many on ui_control .. end on callbacks as we want.

Once we have assigned an ui element we can then use the on control callback to control almost anything we want with our custom ui (we will learn more about this later on).

In general we use the on ui_control to adjust engine parameters like FX settings (e.g. reverb time, attack time, filter cutoffs etc.). But we can also use it to change other settings like UI behaviors, or to activate other functions, to play a note, midi files and many more.

Example – this sets the attack and release time of an AHDSR, named “ENV_AHDSR”:

on ui_control ($attack)
   set_engine_par($ENGINE_PAR_ATTACK, $attack, $group, find_mod($group,"ENV_AHDSR"), -1)
end on

on ui_control ($release) 
  set_engine_par($ENGINE_PAR_RELEASE, $release, $group, find_mod($group,"ENV_AHDSR"), -1) 
end on

NOTE: You can find a full list of all callbacks in your KSP reference manual.

 

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 *