CALLBACKS: Kontakt Scripting Elements

What are Callbacks?

In the Kontakt script, nearly all code happens within a callback of one type or another. Essentially, a callback is like an event. That could be a simple event, like simply opening the library for the first time, or something more complex, like when a note is pressed or a controller knob is moved. When that event takes place, you want something to happen. So, Kontakt uses a callback to call into action a series of commands you have set inside that callback.

Some types of callbacks can only be used once, like the “on initialisation” callback. Others can be used multiple times in a script, like the “on uicontrol” callback. However, they all have a similar structure. Every callback starts with an “on” and ends with an “end on”. This creates a block header that you can enter your script inside. See below an example of an “on initialisation” callback being used. The “on init” text denotes the use of the “on initialisation” callback and the “end on” denotes the end of that callback. The “make_perfview” callback then creates a performance (see this article) view whenever the instrument is initialised, which happens when you first load it into Kontakt.

 
 
on init     {Start of the on init callback.}

	make_perfview     {Creates the "performance view" or GUI for your instrument.} 

end on    {end of the on init callback.}
 

*Note: All text inside {} are comments. See this article.

 

Commonly Used Callbacks

Note: If any terminology is unfamiliar to you, check over the Kontakt Knowledge Base articles for the item you are unsure on. Many examples here are discussed in further detail in other articles.

 

On Init (On Initialisation)

The on init block is one of the most important callback blocks for your instrument’s UI script. When an instrument is loaded into Kontakt, the instrument is initialised, and during this event you want the interface to be fully created and some specific variables, constants, arrays or other parameters to be set and assigned values. Elements like your knobs, sliders, buttons, interface background and your performance view (the area containing all of your controls) need to be declared and adjusted in the on init block.

In the example below, the performance view is created and the UI height and width are set. A slider is created, assigned a value and then positioned on the UI interface. All of this is done in the on init callback so that the UI is created as soon as the instrument loads and the slider is placed on it.

 
 
on init

	make_perfview     {Creating the performance view/UI.}
	set_ui_height_px (200)     {Setting the UI Height in pixels.}
	set_ui_width_px (580)     {Setting the UI Width in pixels.}

	declare ui_slider $Vol (0, 600000)     {Declaring a slider for Volume.}
	move_control_px($Vol, 200, 100)     {Moving the volume slider 200 pixels from the left and 100 down.}
	
end on 
 

Common elements that need to be used inside the on init callback include:

  • Knobs, sliders, buttons, switches and other UI controls, skinning them and moving them into position.

  • Creating the performance view, assigning the dimensions, adding a wallpaper, etc.

  • Declaring any variables, constants or arrays.

  • Clearing the message box.

 

On Note

The on note callback is fairly self explanatory. When a note is played on the keyboard, any note at all, this callback is then used to action commands within it. In the example below, when Middle C is played on the keyboard the message bar (see this article) used at the bottom of Kontakt for testing will read “Middle C”. It does this by using an IF function (see this article) to check if the note being played is a Middle C. If it is, then it will update the message bar.

 
 
on note 

	if ($EVENT_NOTE=60)
		message ("Middle C")
	end if

end on
 

Common ways the on note callback is used include:

  • Creating keyswitches for different articulations.

  • Turning on and off groups.

 

On Release

The on release callback is used to trigger something when a note, any note, is released. Similar to on note, however, this time it is when the key is lifted and let go by the player. In the example below, the message is again updated (see on note above), but this time it will be updated when the key is released.

 
 
on release 

	if ($EVENT_NOTE=60)
		message ("Middle C")
	end if

end on
 

Common ways the on release callback is used include:

  • Creating release trigger samples.

  • Ending processes created in the on note callback.

 

On UI_Control Callback

The on ui_control callback is used constantly throughout the script and is one type of callback that can be used multiple times. This is because each time the callback is used, it is tied to a specific UI element. In the example below, the UI control callback has been used to tie functionality to the Low Pass Cutoff knob on the UI. The callback is tied to the $LPCut knob and then uses the set engine par command (see this article) to make that knob control the LP Cutoff.

 
 
on ui_control ($LPCut)

	set_engine_par ($ENGINE_PAR_CUTOFF,$LPCut,-1,0,1)

end on
 

On Controller Callback

The on controller callback is used to map physical midi controller knobs and sliders to a function inside the instrument. For example, you may want the mod wheel to control the cutoff amount. This is the case in the example below. The on controller callback is called into action if any control is touched, similar to the on note and on release controls, meaning we will again need to use an IF function to check which controller type is being used. In this example, the mod wheel (which is a MIDI CC #1 control) is being checked for movement. If it does move, the value of the UI knob is being updated and the engine parameter is then being set.

 
 
on controller 

	if ($CC_NUM=1)     {Checks to see if the Mod Wheel is being moved.}

		$LPCut := %CC[1]*7874     {Updates the UI knob. Also, necessary to multiple due to MIDI CC range}
		set_engine_par ($ENGINE_PAR_CUTOFF,$LPCut,-1,0,1)     {Set's the LP Cutoff to the knob value.}

	end if

end on
 
Previous
Previous

Using the Message Bar in Kontakt

Next
Next

How to Write Comments in Your Kontakt Script