Documents
Writing Docs Gigedit SFZ Instrument Scripts NKSP Language NKSP Reference

NKSP Reference

This document gives you an overview of all built-in functions and built-in variables provided by the NKSP real-time instrument script language. If you are new to NKSP, then you may want to read the NKSP language tour first.

Built-In Functions

These are the built-in functions available with the NKSP real-time instrument script language.

Core Language Functions

Most fundamental NKSP functions, independent from any purpose of being used in a sampler.

Function Description
abort() Stops execution of a script callback.
acos() Arc cosine function.
abs() Calculates the absolute value of a given value.
array_equal() Check whether two arrays are equal.
asin() Arc sine function.
atan() Arc tangens function.
callback_status() Returns the current status of a callback (thread).
ceil() Rounds the given real number up.
cos() Cosine function.
dec() Decrements the passed integer variable by one.
exit() Returns from current execution block.
exp() Calculates e raised by given power.
floor() Rounds the given real number down.
fork() Creates new execution instances (threads).
inc() Increments the passed integer variable by one.
int() Short-hand alias function for function real_to_int().
int_to_real() Type cast from integer number to real number.
in_range() Checks whether a value is between two other values.
log() Calculates the natural logarithm of given number.
log10() Calculates the logarithm to base 10 of given number.
log2() Calculates the logarithm to base 2 of given number.
lsb() Returns least significant byte of a 14 bit value.
max() Calculates the maximum value of two given values.
message() Prints text to the sampler's terminal.
min() Calculates the minimum value of two given values.
msb() Returns most significant byte of a 14-bit value.
num_elements() Returns the size of the requested array variable.
pow() Calculates given base raised by given power.
random() Random number generator.
real() Short-hand alias function for function int_to_real().
real_to_int() Type cast from real number to integer number.
round() Rounds the given real number.
search() Search for a certain value within an array.
sh_left() Calculates a left bit shifted value.
sh_right() Calculates a right bit shifted value.
sin() Sine function.
sort() Sort the given array.
sqrt() Calculates the square root of given number.
stop_wait() Resumes execution of a suspended script callback.
tan() Tangens function.
wait() Pauses execution for a certain amount of time.

Common Sampler Functions

Basic sampler related functions, independent from a particular sampler format or sampler engine.

Function Description
by_marks() Returns all events of an event group.
change_amp_lfo_depth() Modifies the amplitude LFO depth.
change_amp_lfo_freq() Modifies the amplitude LFO frequency.
change_attack() Modifies the amplitude attack time of voices.
change_cutoff() Changes filter cutoff frequency of voices.
change_cutoff_attack() Modifies the filter cutoff attack time of voices.
change_cutoff_decay() Modifies the filter cutoff decay time of voices.
change_cutoff_lfo_depth() Modifies the filter cutoff LFO depth.
change_cutoff_lfo_freq() Modifies the filter cutoff LFO frequency.
change_cutoff_release() Modifies the filter cutoff release time of voices.
change_cutoff_sustain() Modifies the filter cutoff sustain level of voices.
change_decay() Modifies the amplitude decay time of voices.
change_note() Change MIDI note number of note.
change_pan() Changes panning of voices (stereo balance).
change_pan_curve() Changes the curve type of panning (stereo balance) changes.
change_pan_time() Changes the duration of panning (stereo balance) changes.
change_pitch_lfo_depth() Modifies the pitch LFO depth.
change_pitch_lfo_freq() Modifies the pitch LFO frequency.
change_play_pos() Change the sample playback position.
change_release() Modifies the amplitude release time of voices.
change_reso() Changes filter resonance of voices.
change_sustain() Modifies the amplitude sustain level of voices.
change_tune() Changes the tuning of voices.
change_tune_curve() Changes the curve type of tuning changes.
change_tune_time() Changes the duration of tuning changes.
change_velo() Change MIDI velocity of note.
change_vol() Changes the volume of voices.
change_vol_curve() Changes the curve type of volume changes.
change_vol_time() Changes the duration of volume changes.
delete_event_mark() Removes an event from some event group.
event_status() Checks and returns whether a particular note is still alive.
fade_in() Fade the requested note in.
fade_out() Fade the requested note out.
get_event_par() Get the current value of a specific note parameter.
ignore_controller() Drops the given MIDI control change event.
ignore_event() Drops the given event.
note_off() Releases the requested note.
play_note() Triggers a new note.
set_controller() Creates a MIDI control change event.
set_event_mark() Adds an event to an event group.
set_event_par() Change the value of a specific note parameter.
set_nrpn() Creates a MIDI NRPN event.
set_rpn() Creates a MIDI RPN event.

GigaStudio Format Functions

Sampler format specific functions, dedicated to the individual features of the GigaStudio format engine.

Function Description
gig_set_dim_zone() Changes the currently active dimension zone.
same_region() Check whether two keys are mapped to the same region.

Built-In Variables

These are the built-in variables and built-in constants available with the NKSP realt-time instrument script language.

Core Language Variables

Most fundamental NKSP built-in variables, independent from any purpose of being used in a sampler.

Variable Description
$CALLBACK_STATUS_QUEUE Built-in constant reflecting the status of a callback to be alive but suspended. See callback_status() for details.
$CALLBACK_STATUS_RUNNING Built-in constant reflecting the status of a callback to be alive and currently executing. See callback_status() for details.
$CALLBACK_STATUS_TERMINATED Built-in constant reflecting the status of a callback to be not alive. See callback_status() for details.
$KSP_TIMER Preserved for compatiblity reasons with KSP, returns the same value as $NKSP_REAL_TIMER (refer to the latter for details). Note that KSP's reset_ksp_timer() function is not available with NKSP. However when calculating time differences between two time stamps taken with $NKSP_REAL_TIMER, calling such a reset function is not required, because the underlying clock does not stop when it reached its value limit (which happens every 71 minutes), instead the clock will automatically restart from zero and the calculated time difference even between such transitions will reflect correct durations.
$NKSP_PERF_TIMER Returns the current performance time stamp (in microseconds) of the script running. You may read this variable from time to time to take time stamps which can be used to calculate the time difference (in microseconds) which elapsed between them. A performance time stamp is based on the script's actual CPU execution time. So the internal clock which is used for generating such time stamps is only running forward if the respective script is actually executed by the CPU. Whenever your script is not really executed by the CPU (i.e. because your script got suspended by a wait() call or got forcely suspended due to real-time constraints, or when the entire sampler application got suspended by the OS for other applications or OS tasks) then the underlying internal clock is paused as well. You should only use this built-in variable for script development purposes (i.e. for bench marking the performance of your script). You should not use it with your final production sounds! It is not appropriate for being used in a musical context, because when an offline bounce is performed for instance, the musical timing will be completely unrelated to the CPU execution time. Plus using this variable may cause audio drop outs on some systems. In a musical context you should use $ENGINE_UPTIME instead, which is also safe for offline bounces. On some systems $NKSP_REAL_TIMER and $NKSP_PERF_TIMER will actually return the same value. So the difference between them is not implemented for all systems at the moment.
%NKSP_CALLBACK_CHILD_ID[] Reflects the callback IDs of all child threads which the current script callback instance spawned by having called fork() before. See the latter function for details about this array variable.
$NKSP_CALLBACK_PARENT_ID If the current execution thread is a child thread spawned by a fork() call before, then this variable reflects the callback ID of the parent thread which created this child thread. Otherwise this variable is 0. See fork() for more details about this variable.
$NKSP_REAL_TIMER Returns the current time stamp in reality (in microseconds). You may read this variable from time to time to take time stamps which can be used to calculate the time difference (in microseconds) which elapsed between them. A "real" time stamp is based on an internal clock which constantly proceeds, so this internal clock also continues counting while your script is either suspended (i.e. because your script got suspended by a wait() call or got forcely suspended due to real-time constraints) and it also continues counting even if the entire sampler application got suspended by the OS (i.e. to execute other applications for multi-tasking or to perform OS tasks). You should only use this built-in variable for script development purposes (i.e. for bench marking the performance of your script). You should not use it with your final production sounds! It is not appropriate for being used in a musical context, because when an offline bounce is performed for instance, the musical timing will be completely unrelated to the CPU execution time. Plus using this variable may cause audio drop outs on some systems. In a musical context you should use $ENGINE_UPTIME instead, which is also safe for offline bounces. On some systems $NKSP_REAL_TIMER and $NKSP_PERF_TIMER will actually return the same value. So the difference between them is not implemented for all systems at the moment.
$NI_CALLBACK_ID Reflects the current event handler instance's unique callback ID. For the same event type there may be more than one event handler instances running. Each one of them has its own callback ID. You can get the current event handler instance's callback ID by reading this built-in variable.
$NI_CALLBACK_TYPE Reflects the event type of the current event handler. This variable may reflect one of the following built-in constants: $NI_CB_TYPE_INIT, $NI_CB_TYPE_NOTE, $NI_CB_TYPE_RELEASE, $NI_CB_TYPE_CONTROLLER.
$NI_CB_TYPE_INIT Built-in constant reflecting an init event handler type.
$NI_CB_TYPE_NOTE Built-in constant reflecting a note event handler type.
$NI_CB_TYPE_RELEASE Built-in constant reflecting a release event handler type.
$NI_CB_TYPE_CONTROLLER Built-in constant reflecting a controller event handler type.
$NI_CB_TYPE_RPN Built-in constant reflecting a rpn event handler type.
$NI_CB_TYPE_NRPN Built-in constant reflecting a nrpn event handler type.
$NI_MATH_E Base of the natural logarithm (approximately being 2.71828)
$NI_MATH_PI Natural constant reflecting the ratio of a circle's circumference to its diameter (approximately being 3.14159).
$NKSP_IGNORE_WAIT If this boolean built-in variable is 1 then all calls of your event handler instance to function wait() will be ignored. This may for example be the case if another event handler instance resumed your paused script by calling stop_wait() and passing 1 to the 2nd argument of that function.

Common Sampler Variables

Basic sampler related built-in variables and constants, independent from a particular sampler format or sampler engine.

Variable Description
%ALL_EVENTS Note IDs of all currently active notes of the current sampler part (a.k.a. sampler channel). This may be passed to many built-in functions like note_off(). This array variable only contains IDs of notes which were launched due to MIDI note-on events. This variable does not contain IDs of child notes (i.e. notes which were launched programmatically by calling play_note()). In contrast to KSP this variable is an integer array type, whereas KSP's pendent of this built-in variable is an integer constant (scalar) called $ALL_EVENTS. Using the latter with NKSP will cause a parser warning, the behavior will be the same though.
$CC_NUM MIDI controller number that caused the controller handler to be executed (only useful in the context of a controller handler).
%CC[] Provides access to all current MIDI controller values. This can be used in any context. Use the respective MIDI controller number as index to this integer array variable. For instance %CC[1] would give you the current value of the modulation wheel.
$EVENT_ID ID of the event that caused the current event handler to be executed. In the context of a note handler this would be the event ID of the note, within a controller handler it would be the controller event ID, etc.
$EVENT_NOTE MIDI note number that caused a note related handler to be executed (only useful in the context of a note or release handler).
$EVENT_VELOCITY MIDI velocity value of the note that caused that note related handler to be executed (only useful in the context of a note or release handler).
$EVENT_STATUS_INACTIVE Constant bit flag used as possible return value by event_status() in case the requested note is not "alive".
$EVENT_STATUS_NOTE_QUEUE Constant bit flag used as possible return value by event_status() in case the requested note is still "alive".
$EVENT_PAR_NOTE Constant value symbolizing the "note number" parameter. See get_event_par() and set_event_par() for details.
$EVENT_PAR_TUNE Constant value symbolizing the "tune" parameter. See get_event_par() and set_event_par() for details.
$EVENT_PAR_VELOCITY Constant value symbolizing the "note velocity" parameter. See get_event_par() and set_event_par() for details.
$EVENT_PAR_VOLUME Constant value symbolizing the "volume" parameter. See get_event_par() and set_event_par() for details.
$EVENT_PAR_0 to $EVENT_PAR_3 Four constant values symbolizing the 4 possible user specific parameters, which i.e. might be used to pass data from one script (slot) to another script (slot). See get_event_par() and set_event_par() for details.
%KEY_DOWN[] This can be used in any context to check whether a certain MIDI key is currently pressed down. Use the respective MIDI note number as index to this array variable (see also event_status()).
$NKSP_EASE_IN_EASE_OUT Used to select a fade curve with "ease in and ease out" shape.
$NKSP_LINEAR Used to select a fade curve with linear shape.
$RPN_ADDRESS Used to get the RPN / NRPN parameter number that had been changed in a rpn or nrpn event handler.
$RPN_VALUE Used to get the new RPN / NRPN value in a rpn or nrpn event handler.
$VCC_MONO_AT Constant identifying the MIDI monophonic aftertouch controller (also called MIDI channel pressure ). This is somewhat different than in the MIDI standard. With NKSP aftertouch is handled like an additional "regular" MIDI CC controller. Therefore use %CC[$VCC_MONO_AT] to obtain the current aftertouch value in the context of a controller event handler.
$VCC_PITCH_BEND Constant identifying the pitch bend wheel controller. This is somewhat different than in the MIDI standard. With NKSP pitch bend is handled like an additional "regular" MIDI CC controller. Therefore use %CC[$VCC_PITCH_BEND] to obtain the current pitch bend wheel value in the context of a controller event handler.
$MARK_1 to $MARK_28 Used to select one of the available 28 event groups. See set_event_mark() for details.
$ENGINE_UPTIME Returns the current time stamp (in milliseconds) for being used in a musical context. You may read this variable from time to time to take time stamps which can be used to calculate the time difference (in milliseconds) which elapsed between them. These timing values are based on the internal sample rate and thus it can safely be used to perform musical timing related tasks in your scripts. Especially your script will also continue to behave correctly when an offline bounce of a song is performed.

GigaStudio Format Variables

Sampler format specific built-in variables and constants, dedicated to the individual features of the GigaStudio format engine.

Variable Description
$GIG_DIM_CHANNEL Constant that identifies the stereo dimension.
$GIG_DIM_LAYER Constant that identifies the layer dimension.
$GIG_DIM_VELOCITY Constant that identifies the velocity dimension.
$GIG_DIM_AFTERTOUCH Constant that identifies the aftertouch dimension.
$GIG_DIM_RELEASE Constant that identifies the release trigger dimension.
$GIG_DIM_KEYBOARD Constant that identifies the keyboard position dimension.
$GIG_DIM_ROUNDROBIN Constant that identifies the round robin dimension.
$GIG_DIM_RANDOM Constant that identifies the random dimension.
$GIG_DIM_SMARTMIDI Constant that identifies the start MIDI dimension (a.k.a iMIDI rules).
$GIG_DIM_ROUNDROBINKEY Constant that identifies the round robin key dimension.
$GIG_DIM_MODWHEEL Constant that identifies the modulation wheel dimension.
$GIG_DIM_SUSTAIN Constant that identifies the sustain pedal dimension (a.k.a. hold pedal).
$GIG_DIM_PORTAMENTO Constant that identifies the portamento MIDI controller dimension.
$GIG_DIM_SOSTENUTO Constant that identifies the sostenuto MIDI controller dimension.
$GIG_DIM_SOFT Constant that identifies the soft pedal dimension.
$GIG_DIM_BREATH Constant that identifies the breath controller dimension.
$GIG_DIM_FOOT Constant that identifies the foot pedal dimension.
$GIG_DIM_PORTAMENTOTIME Constant that identifies the portamento time controller dimension.
$GIG_DIM_EFFECT1 Constant that identifies the effect 1 MIDI controller dimension.
$GIG_DIM_EFFECT2 Constant that identifies the effect 2 MIDI controller dimension.
$GIG_DIM_EFFECT1DEPTH Constant that identifies the effect 1 depth MIDI controller dimension.
$GIG_DIM_EFFECT2DEPTH Constant that identifies the effect 2 depth MIDI controller dimension.
$GIG_DIM_EFFECT3DEPTH Constant that identifies the effect 3 depth MIDI controller dimension.
$GIG_DIM_EFFECT4DEPTH Constant that identifies the effect 4 depth MIDI controller dimension.
$GIG_DIM_EFFECT5DEPTH Constant that identifies the effect 5 depth MIDI controller dimension.
$GIG_DIM_GENPURPOSE1 Constant that identifies the general purpose 1 MIDI controller dimension.
$GIG_DIM_GENPURPOSE2 Constant that identifies the general purpose 2 MIDI controller dimension.
$GIG_DIM_GENPURPOSE3 Constant that identifies the general purpose 3 MIDI controller dimension.
$GIG_DIM_GENPURPOSE4 Constant that identifies the general purpose 4 MIDI controller dimension.
$GIG_DIM_GENPURPOSE5 Constant that identifies the general purpose 5 MIDI controller dimension.
$GIG_DIM_GENPURPOSE6 Constant that identifies the general purpose 6 MIDI controller dimension.
$GIG_DIM_GENPURPOSE7 Constant that identifies the general purpose 7 MIDI controller dimension.
$GIG_DIM_GENPURPOSE8 Constant that identifies the general purpose 8 MIDI controller dimension.

Built-In Preprocessor Conditions

These are the built-in preprocessor conditions available with the NKSP realt-time instrument script language.

Core Language Preprocessor Conditions

Most fundamental NKSP built-in preprocessor conditions, independent from any purpose of being used in a sampler.

Condition Description
NKSP_NO_MESSAGE By default this condition is not set. By explicitly enabling this condition with SET_CONDITION(NKSP_NO_MESSAGE) it causes all subsequent message() calls to be ignored and filtered out on preprocessor level. See function message() for details.
Document Updated:  2021-05-09  |  Author:  Christian Schoenebeck