--- doc/docbase/instrument_scripts/nksp/01_nksp.html 2016/12/16 17:20:55 3061 +++ doc/docbase/instrument_scripts/nksp/01_nksp.html 2020/01/04 13:10:46 3698 @@ -11,7 +11,10 @@ your own instrument scripts in short time. It concentrates on describing the script language. If you rather want to learn how to modify and attach scripts to your sounds, then please refer to the gigedit manual for - how to manage instrument scripts with gigedit. + how to manage instrument scripts with gigedit + for Gigasampler/GigaStudio format sounds, or refer to the SFZ opcode + script for attaching NKSP scripts with + SFZ format sounds.

At a Glance

@@ -64,7 +67,7 @@ end on

- There are currently four events available: + There are currently six events available:

@@ -74,12 +77,18 @@ - + + + + + + +
on note This event handler is executed when a new note was triggered, i.e. when hitting a key on a MIDI keyboard.
on release This event handler is executed when a new note was released, i.e. when releasing a key on a MIDI keyboard.on release This event handler is executed when a note was released, i.e. when releasing a key on a MIDI keyboard.
on controller This event handler is executed when a MIDI control change event occurred. For instance when turning the modulation wheel at a MIDI keyboard.
on rpn This event handler is executed when a MIDI RPN event occurred.
on nrpn This event handler is executed when a MIDI NRPN event occurred.
on init Executed only once, as very first event handler, right after the script had been loaded. This code block is usually used to initialize variables in your script with some initial, useful data.
@@ -144,7 +153,7 @@ Please note that you can hardly find MIDI keyboards which support release velocity. So with most keyboards this value will be 127.

- +

Controller Events

Now let's extend the first script to not only show note-on and note-off @@ -177,17 +186,67 @@

There is some special aspect you need to be aware about: in contrast to the MIDI standard, - monophonic aftertouch (a.k.a. channel pressure) and pitch beend wheel are + monophonic aftertouch (a.k.a. channel pressure) and pitch bend wheel are handled by NKSP as if they were regular MIDI controllers. So a value change of one of those two triggers a regular controller event handler to be executed. To obtain the current aftertouch value you can use %CC[$VCC_MONO_AT], and to get the current pitch bend wheel value use %CC[$VCC_PITCH_BEND].

- + +

RPN / NRPN Events

+

+ There are also dedicated event handlers for + MIDI RPN and + NRPN + events: +

+ +on rpn + message("RPN address msb=" & msb($RPN_ADDRESS) & ",lsb=" & lsb($RPN_ADDRESS) & + "-> value msb=" & msb($RPN_VALUE) & ",lsb=" & lsb($RPN_VALUE)) + if ($RPN_ADDRESS = 2) + message("Standard Coarse Tuning RPN received") + end if +end on + +on nrpn + message("NRPN address msb=" & msb($RPN_ADDRESS) & ",lsb=" & lsb($RPN_ADDRESS) & + "-> value msb=" & msb($RPN_VALUE) & ",lsb=" & lsb($RPN_VALUE)) +end on + +

+ Since MIDI RPN and NRPN events are actually MIDI controller events, + you might as well handle these with the previous + controller event handler. But since RPN and NRPN messages + are not just one MIDI message, but rather always handled by a set of + individual MIDI messages, and since the + precise set and sequence of actual MIDI commands sent varies between + vendors and even among individual of their products, it highly makes sense to + use these two specialized event handlers for these instead, because the + sampler will already relief you from that burden to deal with all those + low-level MIDI event processing issues and all their wrinkles involved + when handling RPNs and NRPNs. +

+ + Even though there are two separate, dedicated event handlers for RPN and NRPN events, + they both share the same built-in variable names as you can see in the + example above. + +

+ So by reading $RPN_ADDRESS you get the RPN / NRPN parameter + number that had been changed, and $RPN_VALUE represents the + new value of that RPN / NRPN parameter. Note that these two built-in + variables are a 14-bit representation of the parameter number and new + value. So their possible value range is 0 .. 16383. If you + rather want to use their (in MIDI world) more common separated two 7 bit + values instead, then you can easily do that by wrapping them into either + msb() or lsb() calls like also demonstrated above. +

+

Script Load Event

- As the last one of the four event types available with NKSP, the following + As the last one of the six event types available with NKSP, the following is an example of an init event handler.

@@ -251,7 +310,8 @@

The left hand side's ??variable-name?? is an arbitrary name you can chose for your variable. That name might consist of English - letters A to Z (lower and upper case) and the underscore character "_". + letters A to Z (lower and upper case), digits (0 to 9), + and the underscore character "_". Variable names must be unique. So you can neither declare several variables with the same name, nor can you use a name for your variable that is already been reserved by built-in variables. @@ -414,12 +474,6 @@ triggered on a MIDI keyboard. The following example demonstrates how that could be achieved.

- - You need at least LinuxSampler 2.0.0.svn2 or higher for the following - example to work as described and as expected. Refer to the notes of the - wait() function reference documentation for more - informations about this issue. - on init { The amount of notes to play } @@ -768,7 +822,7 @@ @postfix := "nd" case 3 @postfix := "rd" - end if + end select message("This is the " & $numberOfNotes & @postfix & " note triggered so far.") end on @@ -809,7 +863,7 @@ case 3 message("Third note was triggered!") { Will never be printed ! } exit - end if + end select message("Wow, already the " & $numberOfNotes & "th note triggered.") end on @@ -848,7 +902,7 @@ case 1 to 99 message("Less than 100 notes triggered so far") exit - end if + end select message("Wow, already the " & $numberOfNotes & "th note triggered.") end on @@ -933,12 +987,18 @@

We already came across various built-in functions, which you may call by your scripts to perform certain tasks or behavior which is already - provided for you by the sampler. When working on larger scripts, you + provided for you by the sampler. NKSP also allows you to write your + own functions, which you then may call from various places of your + script. +

+

+ When working on larger scripts, you may notice that you easily get to the point where you may have to duplicate portions of your script code, since there are certain things that you may have to do again and again in different parts of your script. Software developers usually try to avoid such code duplications to - keep the overall amount of code as small as possible, since it would + keep the overall amount of code as small as possible, since the + overall amount of code would bloat quickly and would make the software very hard to maintain. One way for you to avoid such script code duplications with NKSP is to write so called User Functions.

@@ -964,14 +1024,15 @@

This script will run an endless loop for each note being triggered. - Every 200ms it will turn the volume alternatingly down and - up to create the audible stuttering effect. After each wait() + Every 200ms it will turn the volume alternatingly down and + up to create the audible stuttering effect. After each wait() call it calls event_status($EVENT_ID) to check whether this note is still alive, and as soon as the note died, it will stop execution of the script instance by calling exit(). The latter - is important in this case, because otherwise the script instances would + is important in this example, because otherwise the script execution instances would continue to run in this endless loop forever, even after the respectives - notes are gone. Which would let your CPU usage to increase with every note. + notes are gone. Which would let your CPU usage to increase with every new note + and would never decrease again. This behavior of the sampler is not a bug, it is intended, since there may also be cases where you want to do certain things by script even after the respective notes are dead and gone. However as you can see, that script is @@ -997,7 +1058,8 @@

The script became in this simple example only slightly smaller, but it also - became easier to read. And in practice, with a more complex script, you can + became easier to read and behaves identically to the previous solution. + And in practice, with a more complex script, you can reduce the overall amount of script code a lot this way. You can choose any name for your own user functions, as long as the name is not already reserved by a built-in function. Note that for calling a user function, @@ -1007,6 +1069,138 @@ substantially differs calling built-in functions from calling user functions.

+

Synchronized Blocks

+

+ When we introduced the polyphonic keyword + previously, we learned that a script may automatically be suspended by + the sampler at any time and then your script is thus sleeping for an + arbitrary while. The sampler must do such auto suspensions under certain + situations in cases where an instrument script may become a hazard for the + sampler's overall real-time stability. If the sampler would not do so, then + instrument scripts might easily cause audio dropouts, or at worst, buggy + instrument scripts might even lock up the entire sampler in an endless + loop. So auto suspension is an essential feature of the sampler's real-time + instrument script engine. +

+

+ Now the problem as a script author is that you don't really know beforehand + why and when your script might get auto suspended by the sampler. And when + you are working on more complex, sophisticated scripts, you will notice + that this might indeed be a big problem in certain sections of your scripts. + Because in practice, a sophisticated script often has at least one certain + consecutive portion of statements which must be executed in strict consecutive order + by the sampler, which might otherwise cause concurrency issues and thus + misbehavior of your script if that sensible code section was auto suspended + in between. A typical example of such concurrency sensible code sections are + statements which are reading and conditionally modifying global variables. + If your script gets auto suspended in such a code section, another + script handler instance might then interfere and change those global + variables in between. +

+

+ To avoid that, you can place such a sensible code section at the very beginning + of your event handler. For example consider you might be writing a custom + glissando + script starting like this: +

+ +on init + declare $keysDown + declare $firstNoteID + declare $firstNoteNr + declare $firstVelocity +end on + +on note + { The concurrency sensible code section for the "first active" note. } + inc($keysDown) + if ($keysDown = 1 or event_status($firstNoteID) = $EVENT_STATUS_INACTIVE) + $firstNoteID = $EVENT_ID + $firstNoteNr = $EVENT_NOTE + $firstVelocity = $EVENT_VELOCITY + exit { return from event handler here } + end if + + { The non-sensible code for all other subsequent notes would go here. } +end on + +on release + dec($keysDown) +end on + +

+ Because the earlier statements are executed in an event handler, the higher + the chance that they will never get auto suspended. And with those couple of + lines in the latter example you might even be lucky that it won't ever get + suspended in that sensible code section at least. However when it comes to live + concerts you don't really want to depend on luck, and in practice such a + sensible code section might be bigger than this one. +

+

+ That's why we introduced synchronized code blocks for the + NKSP language, which have the following form: +

+ +synchronized + + ??statements?? + +end synchronized + +

+ All ??statements?? which you put into such a synchronized + code block are guaranteed that they will never get auto suspended by + the sampler. +

+ + Such synchronized blocks are a language extension which + is only available with NKSP. KSP does not support synchronized blocks. + +

+ So to make our previous example concurrency safe, we would + change it like this: +

+ +on init + declare $keysDown + declare $firstNoteID + declare $firstNoteNr + declare $firstVelocity +end on + +on note + { The concurrency sensible code section for the "first active" note. } + synchronized + inc($keysDown) + if ($keysDown = 1 or event_status($firstNoteID) = $EVENT_STATUS_INACTIVE) + $firstNoteID = $EVENT_ID + $firstNoteNr = $EVENT_NOTE + $firstVelocity = $EVENT_VELOCITY + exit { return from event handler here } + end if + end synchronized + + { The non-sensible code for all other subsequent notes would go here. } +end on + +on release + dec($keysDown) +end on + +

+ If you are already familiar with some programming languages, then you + might already have seen such synchronized code block concepts + in languages like i.e. Java. This technique really provides an easy way + to protect certain sections of your script against concurrency issues. +

+ + You must use such synchronized code blocks only with great + care! If the amount of statements being executed in your synchronized block + is too large, then you will get audio dropouts. If you even use loops in + synchronized code blocks, then the entire sampler might even become + unresponsive in case your script is buggy! + +

Operators

A programming language provides so called operators to perform @@ -1052,7 +1246,7 @@ Keep in mind that with logical operators shown above, all integer values other than 0 are interpreted as boolean true while an integer value of - precisely 0 is interpreted of being boolean false. + precisely 0 is interpreted as being boolean false.

So the logical operators shown above always look at numbers at a whole. @@ -1099,7 +1293,7 @@

All these operations yield in a boolean result which could then - by used i.e. with if or while loop statements. + be used i.e. with if or while loop statements.

String Operators

@@ -1355,7 +1549,64 @@ use the preprocessor instead for such things. And like stated above, there are certain things which you can only achieve with the preprocessor.

- + +

Disable Messages

+

+ Since it is quite common to switch a script between a development version + and a production version, you actually don't need to wrap all your + message() calls into preprocessor statements like in the + previous example just to disable messages. There is actually a built-in + preprocessor condition dedicated to perform that task much more conveniently for you. + To disable all messages in your script, simply add SET_CONDITION(NKSP_NO_MESSAGE) + i.e. at the very beginning of your script. + So the previous example can be simplified like this: +

+ +{ Enable debug mode, so show all debug messages. } +SET_CONDITION(DEBUG_MODE) + +{ If our user declared condition "DEBUG_MODE" is not set ... } +USE_CODE_IF_NOT(DEBUG_MODE) + { ... then enable this built-in condition to disable all message() calls. } + SET_CONDITION(NKSP_NO_MESSAGE) +END_USE_CODE + +on init + declare const %primes[12] := ( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 ) + declare $i + + message("This script has just been loaded.") + + USE_CODE_IF(DEBUG_MODE) + $i := 0 + while ($i < num_elements(%primes)) + message("Prime " & $i & " is " & %primes[$i]) + $i := $i + 1 + end while + END_USE_CODE +end on + +on note + message("Note " & $EVENT_NOTE & " was triggered with velocity " & $EVENT_VELOCITY) +end on + +on release + message("Note " & $EVENT_NOTE & " was released with release velocity " & $EVENT_VELOCITY) +end on + +on controller + message("MIDI Controller " & $CC_NUM " changed its value to " & %CC[$CC_NUM]) +end on + +

+ You can then actually also add RESET_CONDITION(NKSP_NO_MESSAGE) + at another section of your script, which will cause all subsequent + message() calls to be processed again. So that way you can + easily enable and disable message() calls of entire individual + sections of your script, without having to wrap all message() + calls into preprocessor statements. +

+

What Next?

You have completed the introduction of the NKSP real-time instrument @@ -1365,6 +1616,13 @@ Which provides you an overview and quick access to the details of all built-in functions, built-in variables and more.

+

+ You might also be interested to look at new NKSP core language + features being added to the latest development version of the sampler: + + Real Numbers, Units and Finalness ... + +