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

 

 

 

2 thoughts on “Play a random sample Kontakt with scripting

  1. Jorgo De Groof

    Hello

    I’m very much interested in this script of yours where you trigger random samples without repetition. YOu mentioned you uploaded this script, but I don’t see a link in your blogpost. Could you help me out?

    Thanks!

    Jorgo De Groof

    Reply
    1. Sandy Milne Post author

      Hey Jorgo,

      Seems like the plugin i used to display text had been deactivated when I moved my site a while ago. The code should be displayed here now for you. Any questions let me know

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*