Ask Audio Logo

Get the Fall 2014 Issue

+3 more Issues FREE!

Save $34 on a 1 Year Subscription to the ultimate print mag for musicians & producers.

*Shipping & handling costs apply.

Ask Audio Logo

with you wherever you are

iPhone and iPad

Download our FREE iOS app and enjoy the ultimate mag for digital musicians, DJs and producers on your iPad and iPhone.

Download App Now

Introduction To Scripting in Kontakt, Part 1

Toby Pitman on Oct 01, 2012 in Native Instruments 4 comments

In this 5 Part MPVHub Series, Toby Pitman explores the wonderful world of scripting in Native Instruments' Kontakt sampler. Part 1 covers the basics. Read the next part on The Hub tomorrow!

In this series of tutorials we're going to delve into the world of Kontakt scripting. If you’ve never tried this, don’t worry as we're going to start at the top and hopefully show you some simple but cool things you can do with this powerful feature in Kontakt. 

For these tutorials I’ll be using Kontakt 5 but most, if not all, of the examples will work in Kontakt 4 as well.


What Is Kontakt Scripting?

The KSP (Kontakt Script Processor) is a powerful feature in Kontakt that lets developers or users build custom instruments by addressing and manipulating the internal workings of Kontakt using a dedicated scripting language.

Every instrument that ships with Kontakt and all 3rd party libraries you buy have some sort of scripting going on. Whether it’s a user interface with custom graphics to advanced Legato functions, scripting is the driving force behind Kontakt's status as the world’s most powerful software sampler.

The language itself is similar to most scripting languages like JavaScript, ActionScript, PHP etc. and includes familiar traits like variables, arrays, functions, if else and while statements. If you have any experience in scripting you should find it pretty simple to pick up. 

The language features its own set of defined commands and reference names for parameters all of which can be found in the KSP Reference Manual that comes with Kontakt. This manual is very useful for finding naming conventions and has a load of useful tutorial information. It can be a bit daunting if you're totally new to this though. 

So this series of tutorials is aimed at Kontakt users that aren’t familiar with any scripting language. Hopefully at the end you’ll be able to perform some basic jobs that most people will want to use. These include:

  • Creating custom interfaces
  • Connecting UI elements to parameters
  • Triggering events with notes
  • Building custom graphics


The Script Editor

The Script window is where all the magic happens. You’ll find this by clicking the Edit Instrument Icon (the Spanner) and clicking ‘Script Editor’. Once this is open, click the ‘Edit’ button to open the text area. This is where we write our scripts.

Script Editor


You’ll notice there are five script tabs along the top of the window. This allows for a more modular approach with scripting. Each script preset or ones you write can have a specific task, these can be combined using the five script tabs. You can save any script you write as a preset for later use. You can even import scripts from other instruments in the Kontakt library. So let’s start.


The Basics - Callbacks

Every script starts with a callback. These are like opening and closing tags for your code. These are very important as nothing works without them! There are a few different types of callback that address certain tasks. You’ll find a list of them in the KSP manual in the Callbacks chapter. There are ones that deal with note events, program changes or User Interface interaction. All callbacks are finished with end on.

I’m going to show you three useful callbacks to start with. Here’s the first.


on init ... end on

This is the most basic callback. It tells Kontakt to run whatever is inside on init ... end on when the instrument is loaded or initalized. This is generally where you define variables, arrays and create elements like custom UI controls and give them commands. It’s basically where you set up your ingredients.

on init… end on


Let’s try out a basic script! Between my on init ... end on I’ll write this simple line of code that prints the message “Hello Toby!!”.

message ("Hello Toby”)

I then click ‘Apply’ to run the script. Every time you update your script you’ll need to press ‘Apply’ to see the changes.

Apply


When I do this, the message appears in the bottom of the Kontakt interface! Success!! These messages are useful for user feedback and also troubleshooting.

Message


The Status window will tell me there are no errors in my script as well. 

Here’s another simple example that uses a built in command in the KSP language called set_script_title(). This will name my script in the tabs at the top. The title goes between the brackets in quotation marks.

set_script_title(“My First Script”)

When I hit ‘Apply’ I see my title!

Title


I’ll come back to on init later. Let’s move on.


on note ... end on

This is the callback that deals with ‘Note On’ MIDI messages. Whenever you hit a note this callback is listening for those notes and will run any code inside the callback. 

on note ... end on


You can specify it to react to just one note or a range of notes using if statements (we’ll get to that later). You can use multiple callbacks for different notes, it’s very flexible. For now let’s do something simple. 

I’ll write a message inside my on note callback.

message ("You're Playing A Note")


Script


When I play a note on my keyboard I see this in the bottom of Kontakt.

You're playing a note message


That’s good then! The only thing with this is that Kontakt doesn’t know if I take my finger off the key as on note only deals with the note on message. To deal with this, I need to pair it with another callback, on release.


on release

If I want Kontakt to perform a specific function when I play a note, I can use on release ... end on to stop that function when I release the note.

on release


So now I could add this to my original on note callback inside an on release ... end on callback.

message ("Now You're Not!")

Script


When I press a note I get the original on note message and when I release it I get this. All good!

Now you're not! message


Try this example for yourself. 

So we’ve seen how to use some basic callbacks. This is essentially how all of your scripts will work, albeit a bit more complex! Next time we’ll take this concept and expand on it. We’ll look at some variables and built in functions and start to make a note Triggered FX script.

Quick Links:

Introduction To Scripting in Kontakt, Part 1 (you're reading it)

Introduction To Scripting in Kontakt, Part 2

Introduction To Scripting in Kontakt, Part 3 

Introduction To Scripting in Kontakt, Part 4

Introduction To Scripting in Kontakt, Part 5

For the past 20 years Toby has worked as a professional guitarist, programmer and producer. Clients include Sir Paul McCartney, George Michael, Shirley Bassey, Yusuf Islam, Giles Martin as well as the London 2012 Olympic Ceremonies. He has also worked extensively in TV, Advertising and Film. As well as composing himself he has also ... Read More

Discussion

producer1.1
great tutorial, great idea to add this here on MPV. I am looking forward to get more information...I know that there is scripting in Kontakt but never did any scripting in Kontakt, although I am a pro in html, php, java or action script...shame on me ;-)
"now the Kontakt party can begin" :-)
...thank you Toby
appsoundcentral
great to see this on here! not many great scripting sources out there...
appsoundcentral
a scripting video series would be amazing, because nothing exists....
gashi
ciao voglio sapere se voi fate questo lavoro sul Kontakt Scripting perche o bisogno di aiuto grazie