Customise Kontakt Patches | How To Create Scripts In NI Kontakt, Part 1
Primer
Technique : Sampling
The key to unlocking the power of Native Instruments Kontakt sampler lies in its Script Processor. Take advantage of this powerful feature to customise your Kontakt patches any way you like. This is the first article in a four-part series. Read
Part 2,
Part 3 and
Part 4.
Mark Wherry
Over the last few years, Native Instruments Kontakt has become one of the most widely used software samplers. Its popular with content developers and end users alike, and the number of instrument libraries available in Kontakt format seems to grow almost daily.
One of Kontakts most powerful features is the Kontakt Script Processor (KSP). It enables you to write Scripts, which can be thought of as lists of instructions that manipulate the way a Kontakt instrument works. You might write instructions to add harmonies for notes that are played, control the built-in effects used in an instrument, or even create your own arpeggiators and step sequencers. And although I wouldnt go so far as to use a cliché like “anything is possible”, KSP can certainly make a large number of your ideas for sample-based instruments achievable.
While the idea of writing your own Scripts for Kontakt may seem appealing, you might be worrying about the complexity of such an endeavour. However, you dont need to be a programming wizard in order to write a Script. The whole idea of scripting is that it allows you to add functionality to Kontakt without having to know anything about the programs code. And while scripting for Kontakt can, indeed, become quite complex, even the most seemingly cryptic Script is built on simple fundamentals that are easy for anyone to learn. The goal of this workshop is to get you started with those fundamentals.
Before we begin, I should mention that the following text is written with Kontakt 4, the current version, in mind. Most of the discussion should also apply to Kontakt 3, but be warned that there are some subtle and some not-so-subtle differences between versions 3 and 4 when it comes to scripting and the built-in Script Editor.
Meet The Editor
Lets begin by loading an instrument.

Like most instruments in the Kontakt 4 Factory library, Subtle Bell includes a multi-tabbed Performance View where parameters for the instrument can be adjusted.
Like most instruments in the Kontakt 4 Factory library, Subtle Bell includes a multi-tabbed Performance View where parameters for the instrument can be adjusted.
For this example, well use the Subtle Bell.nki instrument thats part of Kontakt 4s Factory library, from the Synth folder. This might seem a curious choice of instrument, but theres a reason. Although you can add scripts to any instrument you like, its important when adding a Script to an instrument already containing a Script — as were about to do — that the instructions in the existing Scripts dont interfere with the instructions you want to add. Subtle Bell allows us to add our own Script without having to worry. (Should you want to see an example of such interference, try the following tutorial with Kontakts Grand Piano.nki instrument instead.)
Youll notice that the Subtle Bell instrument has a fairly elaborate Performance View, where you can adjust various parameters for the instrument. If you cant see the Instruments Performance View, click the PV button at the far right of the Instrument Header area in the Rack. If an instrument Header doesnt contain a PV button, it means that the instrument doesnt have a Performance View.
As you may already know, Performance Views and the parameters they display are defined within a Script. Like most instruments in Kontakts factory library, the Performance View of Subtle Bell has two tabs: Options and Instrument. Each tab represents a different Script, and a Kontakt instrument may contain up to five Scripts. No matter how complicated you might think a Script can be to display such a Performance View, remember that its still just a list of instructions. One instruction might be to place a knob labelled transpose on the Performance View, while another might tell Kontakt how to adjust the pitch of notes to be played, based on the value of the knob.
To view or edit a Script, click the Edit button in the top left of the instrument Header. This puts the instrument into Instrument Edit mode. Now, directly underneath the Header, click the Script Editor button.

The Script Editor consists of two main parts: a preview of the Performance View in the upper area, and the main text editor for writing your Scripts in the lower part.
The Script Editor consists of two main parts: a preview of the Performance View in the upper area, and the main text editor for writing your Scripts in the lower part.
At the very top of the Script Editor, there are five tabs, each representing one of the five Script slots in the instrument. Notice how the first two slots have the names of the Performance View tabs: Options and Instrument. If you click the Options or Instrument slot you can see the Script behind either of these tabs.
The Script Editor basically consists of two parts. The upper part shows a preview of the Performance View, while the lower part is a text editor where you can write and change the instructions in the Script (see screen on previous page). If you cant see the text editor, click the Edit button at the bottom of the grey Script Editor area. Now, take a moment to observe how the Script Editor looks with a ready-made script, because our next step is to build one from scratch.
Click the third Script slot in the Script Editor — one of the two remaining that are titled <empty> — and youll see a completely empty Script. From this blank canvas, were going to build a simple harmoniser; and although this isnt the most adventurous or, indeed, original use of scripting, its a good example to demonstrate some basic ideas.
Hello Script
When starting a Script, its a good idea to think about what you want it to do. In the case of our harmoniser, we want to play an additional note every time a note is sent to the Subtle Bell instrument. To begin with, lets say our additional note will always be middle C. This means that for every note played, we will tell Kontakt to play MIDI note number 60 as well.
A KSP Script works by defining a series of what computer scientists refer to as callbacks. In the case of Kontakt, a callback is a series of instructions that are executed when certain events are triggered. For example, when a note is sent to an instrument, this note is an event that will trigger a certain callback in your Script. As a simple analogy, its a bit like phoning someone and asking them to call you back when they receive the results from an exam. The original phone call registers your interest in the news; the callback happens in the event of the exam results being received by the person you called; and then you respond in a given manner once the callback happens. In KSP, the callbacks for different kinds of events are registered for you, and all you have to do is define their behaviour.
When a note is received, Kontakt looks to see if a script has a callback called on note. Given that our harmoniser needs to do something when a note is played, lets define the instructions to be executed for the on note callback. Click in the Script Editors text editor (the big white area), and, on the top line, type:
on note
(Make sure that you use lower-case characters as indicated, since KSPs keywords and commands are defined using lower-case characters. Using capital letters here would produce an error.)
This instrument tells KSP that were defining the on note callback. Since each instruction in a script needs to be on its own line, youll need to press Enter. Now, lets tell KSP that every time a note is played, we want to also play a middle C, by entering the following instruction:
play_note(60, 100, 0, -1)
Theres no prize for guessing what exactly the play_note command does, but youll notice that after the name of the command are four numbers enclosed in brackets, each separated by a comma. These numbers are known as parameters, and all KSP commands take the basic form command_name(parameters). The only exception is when a command doesnt have any parameters, in which case, all you need to do is enter the name of the command, with no need for brackets.
The play_note command has four parameters: the pitch of the note to play, the velocity of that note, the sample offset to apply, and how long the note should play for. The pitch of the note is specified in semitones with a value between 0 and 127, where middle C is 60 (the C an octave above middle C would be 72, and so on). The velocity is a number between 1 and 127, where 127 is the highest. The sample offset specifies at what time in microseconds the playback should start in the sample. For example, a sample offset of 1000 would start sample playback a second into the actual audio waveform. Finally, the length of the note is also a value in microseconds, but in KSP parlance, this parameter has two special values.
When youre handling notes being played on an instrument in real time, theres no way to know how long a note will be played for — until it stops, of course. So by setting the length of the note to -1, we tell KSP that the new note should last for as long as the original note is held. A value of 0 would tell KSP to play the sample for its maximum length — a one-shot sample, if you will — which can be useful for triggering drums.
So, getting back to our Script, the instruction play_note(60, 100, 0, -1) tells KSP to play a middle C (note number 60) with a velocity of 100, starting at the beginning of the sample (0), and playing for as long as the original note (-1).
After entering the instruction to play a note, we need to tell KSP that weve finished defining our callback. So press Enter and type the final line:
end on
Were now ready to run our Script. To do this, click the Apply button, to the right, just above the text editor. Next to the Apply button is the Change Indicator, and this will be displayed yellow when a change is made to the current Script. After clicking Apply, the Change Indicator should turn black, meaning the Script you just edited is now active. If the Change Indicator stays yellow after clicking Apply, it means there was an error in the Script. Check your listing against the Script on the previous page

This is how the Script Editor should look after youve finished entering the first Script.
This is how the Script Editor should look after youve finished entering the first Script.
, and try again.
Now, when you play notes on the Subtle Bell instrument, youll always hear a middle C harmonising with whatever note you play. This is kind of fun, and could actually be useful: on a drum instrument, for example, you might want to hear a hi-hat sound no matter which drum in the kit is played. But for our next trick, lets see how we can adjust our Script so that the harmonising note is always a minor third below the note thats played. In order to do this, we need to first understand another KSP fundamental: the variable.
Minor Variables
A variable is a container for a value that can change depending on circumstances. In our first example, when we wanted to play middle C, we simply entered the number 60 as the pitch parameter of the play_note command. But another approach would have been to create a variable called, for example, pitch, and make this variable store the number 60. The name of the variable isnt important — we could equally well have called it potato — except that giving a variable an appropriate name makes it easier to understand its role in a Script.
Using a variable, our play_note instruction could have been written as play_note($pitch, 100, 0, -1). Notice that the name of the variable is written with a preceding dollar sign. When using variables in Kontakt, you must always put a dollar sign before the name of the variable, making sure theres no space between the sign and the name. This tells KSP that whatever follows the dollar sign is the name of a variable.
So whats the advantage in using a variable to store the value 60, rather than writing the number 60 explicitly? The reason is that, by using a variable, we can change the value of the variable and play a note with a different pitch without having to change the play_note command itself. This is important for our harmoniser, because the pitch of the note we trigger will need to be based on the pitch of the original note played.
For this example, were not going to define our own variable. Kontakt already includes a number of built-in variables that contain useful numbers. One such variable is $EVENT_NOTE, which always contains the pitch of the note event that the on note callback was triggered with. (The names of built-in variables are always written in capital letters, making them easy to differentiate from variables you create yourself.)
If we adjust our play_note command to be play_note($EVENT_NOTE, 100, 0, -1), every time we play a note, a duplicate note of the same pitch will be played. However, we can also use some simple mathematics in conjunction with the variable. For example, try changing the play_note command to play_note($EVENT_NOTE+12, 100, 0, -1). This means that the harmony note will now be an octave higher than the played note. Click Apply and try it.

Your Script should look like this after replacing the pitch value with the built-in variable.
Your Script should look like this after replacing the pitch value with the built-in variable.
However, our goal was to have the note a minor third lower, so instead of writing $EVENT_NOTE+12, type $EVENT_NOTE-3 and click Apply.
Sweet Harmony
Weve pretty much achieved our goal of writing the simplest harmoniser, but theres one thing left to change. Because our play_note command has 100 as the velocity parameter, our harmony note is always played with a velocity of 100, regardless of the velocity of the original note. In the same way that KSP uses a built-in variable to store the pitch of the note that triggered the on note callback, theres another that stores the velocity of the event: $EVENT_VELOCITY. So if you replace 100 with $EVENT_VELOCITY, the harmony note will now play with the same velocity as the original note. And you could always write $EVENT_VELOCITY-10 if you want the harmony note to be played slightly more quietly.

The finished Script of the simplest harmoniser.
The finished Script of the simplest harmoniser.
Thats all we have time for this month. Next time, well learn about writing our own variables, and also how to start creating Performance Views to make our harmoniser adjustable. 0
Name That Script
When youve created a Script, its a good idea to give it a name. You can enter a name in the Title for this Script edit field, and this will then appear in the slot containing that Script in the Script Editor. When we later create Performance Views, the Script name will also be used for the appropriate tab in the Performance View.
Although Scripts you create in an instrument are always saved with that particular instrument, its also possible to save them independently. This can be useful if you want to use the same Script in different instruments, for example. There are a number of ways to go about this, the simplest being to just copy and paste your script into a text editor. But the Script Editor also uses a preset system like other parts of Kontakt, so you can save a Script by clicking the Preset button in the upper area of the Script Editor and selecting Save Preset from the pop-up menu.
A file selector will appear, and youll be pointed to the Scripts folder in your Kontakt 4 User folder. This folder contains a number of empty folders that have been created with the same names used in Kontakts own Factory Script library. You can use the root of the Scripts folder, one of the existing sub-folders, or create your own, and save the preset Script.
Scripts can be reloaded from the Script Editors Preset menu, where you can choose between Factory and User Scripts. If you want to initialise a Script slot in the Editor, you can use the empty Factory Preset to be sure that youre starting with a blank slate.
This is the first article in a four-part series. Read Part 2, Part 3 and Part 4.