Introduction To Scripting in Kontakt, Part 1
Toby Pitman on Oct 01, 2012 in Native Instruments • 4 comments
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 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.
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.
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.
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.
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!
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.
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")
When I play a note on my keyboard I see this in the bottom of Kontakt.
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.
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.
So now I could add this to my original on note callback inside an on release ... end on callback.
message ("Now You're Not!")
When I press a note I get the original on note message and when I release it I get this. All good!
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.
Introduction To Scripting in Kontakt, Part 1 (you're reading it)
Introduction To Scripting in Kontakt, Part 5