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

Leave a Reply

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


*