Category Archives: Kontakt Scripting

Kontakt Scripting Tools

I’ve been doing a lot of work lately within Native Instruments’ Kontakt sampler which has its own scripting language. It’s quite powerful, works in pro tools and other DAWs and means you don’t have to worry about the pitfalls of dealing with Core audio and all that fun.
I thought I would share a couple of the tools that I find indispensable for working with Kontakt scripting.

Nils Liberg’s KScript Editor is a script editor (you probably guessed that part) with an integrated compiler and is a lot easier to work in that typing your code directly into the Kontakt scripting engine. By pressing F5 you can compile your code which is also copied to the clipboard to be pasted into Knotakt. It makes life so much easier.
You can download it from Nil’s website below
http://nilsliberg.se/ksp/

Ken’s GUIGenerator is a graphic tool to help you to create Kontakt performance views, or user interfaces, without having to code at all. It has a lot of great features and lets you name all your variables so you can easily hook them up to your script.
You can download it from musikbits below
http://www.musikbits.com/

Playing a Random Set of Samples in Kontakt

For a project I am working on at the moment I am using Native Instruments Kontakt’s scripting language (KSP) to trigger random samples from a range within the sampler using the script I posted in a previous post. (Here)
This was all working well but when the sample size was getting to small the script was getting locked in a while loop trying to find notes that it had not used before which was very inefficient.
The solution I came up with was to use an array to store all the notes which gets shuffled randomly whenever it reaches the sample limit.
Below is an example of the script I created.

on init
  
  message("Loaded...")
  
declare $temp := 0
declare $random_temp := 0
declare $counter := 0
declare $lower_limit := 0
declare $upper_limit := 10  
declare $play_head := 0

declare %array_one[10]

  
  
  while($counter < num_elements(%array_one))
      
      %array_one[$counter] := $counter
      
  inc($counter)
      end while
  
  
  {shuffle array 1}
  
  $counter := 0
  
while($counter < num_elements(%array_one))
    
    $random_temp := random(0, num_elements(%array_one) - 1)
    
    $temp := %array_one[$counter]
    %array_one[$counter] := %array_one[$random_temp]
    %array_one[$random_temp] := $temp
    
    
    inc($counter)
    end while
 
       
  
end on

function shuffleArray
   
    $counter := 0
    
  while($counter < num_elements(%array_one))
    
    $random_temp := random(0, num_elements(%array_one) - 1)
    
    $temp := %array_one[$counter]
    %array_one[$counter] := %array_one[$random_temp]
    %array_one[$random_temp] := $temp
    
    
    inc($counter)
    end while  
    
    
    
end function



on note
    
if ($play_head < 10) 
message("Playing....")    
play_note(%array_one[$play_head],100,0,-0)
inc($play_head)

else 
message("Shuffle....") 
call shuffleArray
$play_head := 0
play_note(%array_one[$play_head],100,0,-0)
inc($play_head)

end if    
    
    
end on

Play a random sample Kontakt with scripting

Over the last year or so I have been working on creating a sample based software instrument in Native Instruments’ Kontakt, which has its own scripting language Kontakt Script Processing.  I had to learn the language from scratch but found the KSP reference  pdf  bundled with Kontakt and information on the Kontakt scripting forums at the Native Instruments website great resources.

The instrument I am currently working on is now over 3000 lines of code and I have started dreaming of $EVENT_NOTE variables but one of the first requirements was to trigger a sample from a range of samples randomly. This was pretty easy to do but because it was random it often played the same sample twice in a row. I then added a couple of extra lines to make sure that it didn’t play the same notes twice. I thought I would upload this code as it may prove useful to people who are leaning KSP.  It also shows how to create and call functions and declare variables.

 

on init
       
    {declare variables to hold previous values}
 declare polyphonic $random
 declare $last1 := 0
 declare $last2 := 0

 
 
declare $lastTime := 0
declare polyphonic $velocity := 1
    
    
{Declare keys for Random Triggers ie the note that will trigger the random sample  }
 
declare $key_trigger := 108
   
{declare the range of the samples that will be triggered randomly}    
    
declare $lower_limit := 0
declare $upper_limit := 23    
    
    
{Set key colours}

set_key_color($key_trigger, $KEY_COLOR_RED)     
set_key_color($lower_limit, $KEY_COLOR_GREEN)
set_key_color($upper_limit, $KEY_COLOR_GREEN)   
    
    
end on

{Random sample Function}


function playRandomSample
    
     
            
            $random := (random($lower_limit, $upper_limit))
        
           
        {check to see if the next step has been used in the last two }
                
                while ($random = $last1 or $random = $last2)
               
                $random := (random($lower_limit, $upper_limit))
            
                end while
                
                play_note ($random, $velocity, 0, 0)
  
        
            $last2 := $last1
            $last1 := $random
       
   end function

on note
  if ($EVENT_NOTE = $key_trigger)
      
      $velocity := $EVENT_VELOCITY
      
      call playRandomSample
      
      end if  
    
    
    
    
end on

 

feel free to leave any questions/comments below