/[svn]/doc/docbase/instrument_scripts/nksp/01_nksp.html
ViewVC logotype

Diff of /doc/docbase/instrument_scripts/nksp/01_nksp.html

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3262 by schoenebeck, Wed May 31 23:19:39 2017 UTC revision 3717 by schoenebeck, Mon Jan 13 13:29:05 2020 UTC
# Line 67  on ??event-name?? Line 67  on ??event-name??
67  end on  end on
68      </code>      </code>
69      <p>      <p>
70        There are currently four events available:        There are currently six events available:
71      </p>      </p>
72        <table>        <table>
73          <tr>          <tr>
# Line 77  end on Line 77  end on
77            <td><code>on note</code></td> <td>This event handler is executed when a new note was triggered, i.e. when hitting a key on a MIDI keyboard.</td>            <td><code>on note</code></td> <td>This event handler is executed when a new note was triggered, i.e. when hitting a key on a MIDI keyboard.</td>
78          </tr>          </tr>
79          <tr>          <tr>
80            <td><code>on release</code></td> <td>This event handler is executed when a new note was released, i.e. when releasing a key on a MIDI keyboard.</td>            <td><code>on release</code></td> <td>This event handler is executed when a note was released, i.e. when releasing a key on a MIDI keyboard.</td>
81          </tr>          </tr>
82          <tr>          <tr>
83            <td><code>on controller</code></td> <td>This event handler is executed when a MIDI control change event occurred. For instance when turning the modulation wheel at a MIDI keyboard.</td>            <td><code>on controller</code></td> <td>This event handler is executed when a MIDI control change event occurred. For instance when turning the modulation wheel at a MIDI keyboard.</td>
84          </tr>          </tr>
85          <tr>          <tr>
86              <td><code>on rpn</code></td> <td>This event handler is executed when a MIDI <i>RPN</i> event occurred.</td>
87            </tr>
88            <tr>
89              <td><code>on nrpn</code></td> <td>This event handler is executed when a MIDI <i>NRPN</i> event occurred.</td>
90            </tr>
91            <tr>
92            <td><code>on init</code></td> <td>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.</td>            <td><code>on init</code></td> <td>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.</td>
93          </tr>          </tr>
94        </table>        </table>
# Line 147  end on Line 153  end on
153        Please note that you can hardly find MIDI keyboards which support release        Please note that you can hardly find MIDI keyboards which support release
154        velocity. So with most keyboards this value will be 127.        velocity. So with most keyboards this value will be 127.
155      </p>      </p>
156        
157      <h3>Controller Events</h3>      <h3>Controller Events</h3>
158      <p>        <p>  
159        Now let's extend the first script to not only show note-on and note-off        Now let's extend the first script to not only show note-on and note-off
# Line 180  end on Line 186  end on
186      </p>      </p>
187      <p>      <p>
188        There is some special aspect you need to be aware about: in contrast to the MIDI standard,        There is some special aspect you need to be aware about: in contrast to the MIDI standard,
189        monophonic aftertouch (a.k.a. channel pressure) and pitch beend wheel are        monophonic aftertouch (a.k.a. channel pressure) and pitch bend wheel are
190        handled by NKSP as if they were regular MIDI controllers. So a value change        handled by NKSP as if they were regular MIDI controllers. So a value change
191        of one of those two triggers a regular <code>controller</code> event handler        of one of those two triggers a regular <code>controller</code> event handler
192        to be executed. To obtain the current aftertouch value you can use        to be executed. To obtain the current aftertouch value you can use
193        <code>%CC[$VCC_MONO_AT]</code>, and to get the current pitch bend wheel        <code>%CC[$VCC_MONO_AT]</code>, and to get the current pitch bend wheel
194        value use <code>%CC[$VCC_PITCH_BEND]</code>.        value use <code>%CC[$VCC_PITCH_BEND]</code>.
195      </p>      </p>
196        
197        <h3>RPN / NRPN Events</h3>
198        <p>
199          There are also dedicated event handlers for
200          MIDI <i title="Registered Parameter Number">RPN</i> and
201          <i title="Non-Registered Parameter Number">NRPN</i>
202          events:
203        </p>
204        <code>
205    on rpn
206      message("RPN address msb=" & msb($RPN_ADDRESS) & ",lsb=" & lsb($RPN_ADDRESS) &
207              "-> value msb=" & msb($RPN_VALUE) & ",lsb="  & lsb($RPN_VALUE))
208      if ($RPN_ADDRESS = 2)
209        message("Standard Coarse Tuning RPN received")
210      end if
211    end on
212    
213    on nrpn
214      message("NRPN address msb=" & msb($RPN_ADDRESS) & ",lsb=" & lsb($RPN_ADDRESS) &
215              "-> value msb=" & msb($RPN_VALUE) & ",lsb="  & lsb($RPN_VALUE))
216    end on
217        </code>
218        <p>
219          Since MIDI RPN and NRPN events are actually MIDI controller events,
220          you might as well handle these with the previous
221          <code>controller</code> event handler. But since RPN and NRPN messages
222          are not just one MIDI message, but rather always handled by a set of
223          individual MIDI messages, and since the
224          precise set and sequence of actual MIDI commands sent varies between
225          vendors and even among individual of their products, it highly makes sense to
226          use these two specialized event handlers for these instead, because the
227          sampler will already relief you from that burden to deal with all those
228          low-level MIDI event processing issues and all their wrinkles involved
229          when handling RPNs and NRPNs.
230        </p>
231        <note>
232          Even though there are two separate, dedicated event handlers for RPN and NRPN events,
233          they both share the same built-in variable names as you can see in the
234          example above.
235        </note>
236        <p>
237          So by reading <code>$RPN_ADDRESS</code> you get the RPN / NRPN parameter
238          number that had been changed, and <code>$RPN_VALUE</code> represents the
239          new value of that RPN / NRPN parameter. Note that these two built-in
240          variables are a 14-bit representation of the parameter number and new
241          value. So their possible value range is <code>0 .. 16383</code>. If you
242          rather want to use their (in MIDI world) more common separated two 7 bit
243          values instead, then you can easily do that by wrapping them into either
244          <code>msb()</code> or <code>lsb()</code> calls like also demonstrated above.
245        </p>
246    
247      <h3>Script Load Event</h3>      <h3>Script Load Event</h3>
248      <p>      <p>
249        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
250        is an example of an <code>init</code> event handler.        is an example of an <code>init</code> event handler.
251      </p>      </p>
252      <code>      <code>
# Line 214  end on Line 270  end on
270        had in mind when you wrote a certain script three years ago, and also if        had in mind when you wrote a certain script three years ago, and also if
271        some other developer might need to continue working on your scripts one        some other developer might need to continue working on your scripts one
272        day, you should place as many comments into your scripts as possible. A        day, you should place as many comments into your scripts as possible. A
273        comment in NKSP is everything that is nested into a an opening and closing        comment in NKSP is everything that is nested into an opening and closing
274        pair of curly braces.        pair of curly braces.
275      </p>      </p>
276      <code>{ This is a comment. }</code>      <code>{ This is a comment. }</code>
# Line 299  end on Line 355  end on
355    
356      <h3>Variable Types</h3>      <h3>Variable Types</h3>
357      <p>      <p>
358        There are currently three different variable types, which you can easily        There are currently five different variable types, which you can easily
359        recognize upon their first character.        recognize upon their first character.
360      </p>      </p>
361      <table>      <table>
# Line 313  end on Line 369  end on
369          <td><code>%??variable-name??</code></td> <td>Integer Array</td> <td>Stores a certain amount of integer number values.</td>          <td><code>%??variable-name??</code></td> <td>Integer Array</td> <td>Stores a certain amount of integer number values.</td>
370        </tr>        </tr>
371        <tr>        <tr>
372            <td><code>~??variable-name??</code></td> <td>Real Number Scalar</td> <td>Stores one single real (floating point) number value.</td>
373          </tr>
374          <tr>
375            <td><code>???variable-name??</code></td> <td>Real Number Array</td> <td>Stores a certain amount of real (floating point) number values.</td>
376          </tr>
377          <tr>
378          <td><code>@??variable-name??</code></td> <td>String</td> <td>Stores one text string.</td>          <td><code>@??variable-name??</code></td> <td>String</td> <td>Stores one text string.</td>
379        </tr>        </tr>
380      </table>      </table>
# Line 413  end on Line 475  end on
475      </p>      </p>
476      <p>      <p>
477        Let's assume you wanted to write an instrument script that shall resemble        Let's assume you wanted to write an instrument script that shall resemble
478        a simple delay effect. You could do that by writing an note event handler        a simple delay effect. You could do that by writing a note event handler
479        that automatically triggers several new notes for each note being        that automatically triggers several new notes for each note being
480        triggered on a MIDI keyboard. The following example demonstrates how that        triggered on a MIDI keyboard. The following example demonstrates how that
481        could be achieved.        could be achieved.
482      </p>      </p>
     <note>  
       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  
       <code>wait()</code> function reference documentation for more  
       informations about this issue.  
     </note>  
483      <code>      <code>
484  on init  on init
485    { The amount of notes to play }    { The amount of notes to play }
# Line 478  end on Line 534  end on
534        happening when executing that script exactly: Each time you play a note        happening when executing that script exactly: Each time you play a note
535        on your keyboard, a new instance of the <code>note</code> event handler        on your keyboard, a new instance of the <code>note</code> event handler
536        will be spawned and executed by the sampler. In all our examples so far        will be spawned and executed by the sampler. In all our examples so far
537        our scripts were so simple, that in practice only one handler instance        our scripts were so simple, that in practice only one event handler instance
538        was executed at a time. This is different in this case though. Because        was executed at a time. This is different in this case though. Because
539        by calling the <code>wait()</code> function, the respective handler        by calling the <code>wait()</code> function, the respective handler
540        execution instance is paused for a while and in total each handler        execution instance is paused for a while and in total each handler
# Line 487  end on Line 543  end on
543        you play multiple, successive notes on your keyboard in short time, you        you play multiple, successive notes on your keyboard in short time, you
544        will have several instances of the <code>note</code> event handler running        will have several instances of the <code>note</code> event handler running
545        simultaniously. And that's where the problem starts. Because by default,        simultaniously. And that's where the problem starts. Because by default,
546        as said, all variables are global variables. So the handler instances        as said, all variables are global variables. So the event handler instances
547        which are now running in parallel, are all reading and modifying the same        which are now running in parallel, are all reading and modifying the same
548        data. Thus the individual handler instances will modify the        data. Thus the individual event handler instances will modify the
549        <code>$i</code> and <code>$velocity</code> variables of each other, causing        <code>$i</code> and <code>$velocity</code> variables of each other, causing
550        an undesired misbehavior.        an undesired misbehavior.
551      </p>      </p>
# Line 981  end on Line 1037  end on
1037        execution of the script instance by calling <code>exit()</code>. The latter        execution of the script instance by calling <code>exit()</code>. The latter
1038        is important in this example, because otherwise the script execution instances would        is important in this example, because otherwise the script execution instances would
1039        continue to run in this endless loop forever, even after the respectives        continue to run in this endless loop forever, even after the respectives
1040        notes are gone. Which would let your CPU usage to increase with every new note        notes are gone. Which would let your CPU usage increase with every new note
1041        and would never decrease again.        and would never decrease again.
1042        This behavior of the sampler is not a bug, it is intended, since there may        This behavior of the sampler is not a bug, it is intended, since there may
1043        also be cases where you want to do certain things by script even after the        also be cases where you want to do certain things by script even after the
# Line 1021  end on Line 1077  end on
1077    
1078      <h3>Synchronized Blocks</h3>      <h3>Synchronized Blocks</h3>
1079      <p>      <p>
1080        When we introduced the <a href="polyphonic_variables">polyphonic keyword</a>        When we introduced the <a href="#polyphonic_variables">polyphonic keyword</a>
1081        previously, we learned that a script may automatically be suspended by        previously, we learned that a script may automatically be suspended by
1082        the sampler at any time and then your script is thus sleeping for an        the sampler at any time and then your script is thus sleeping for an
1083        arbitrary while. The sampler must do such auto suspensions under certain        arbitrary while. The sampler must do such auto suspensions under certain
# Line 1104  end synchronized Line 1160  end synchronized
1160      </p>      </p>
1161      <note>      <note>
1162        Such <code>synchronized</code> blocks are a language extension which        Such <code>synchronized</code> blocks are a language extension which
1163        is only available with NKSP and requires at least LinuxSampler 2.0.0.svn60        is only available with NKSP. KSP does not support <code>synchronized</code> blocks.
       or higher. KSP does not support <code>synchronized</code> blocks.  
1164      </note>      </note>
1165      <p>      <p>
1166        So to make our previous example concurrency safe, we would        So to make our previous example concurrency safe, we would
# Line 1141  end on Line 1196  end on
1196      <p>      <p>
1197        If you are already familiar with some programming languages, then you        If you are already familiar with some programming languages, then you
1198        might already have seen such synchronized code block concepts        might already have seen such synchronized code block concepts
1199        in languages like i.e. Java. This technique really provides an easy way        in languages like e.g. Java. This technique really provides an easy way
1200        to protect certain sections of your script against concurrency issues.        to protect certain sections of your script against concurrency issues.
1201      </p>      </p>
1202      <note class="important">      <note class="important">
# Line 1197  end on Line 1252  end on
1252        Keep in mind that with logical operators shown above,        Keep in mind that with logical operators shown above,
1253        all integer values other than <code>0</code>        all integer values other than <code>0</code>
1254        are interpreted as boolean <i>true</i> while an integer value of        are interpreted as boolean <i>true</i> while an integer value of
1255        precisely <code>0</code> is interpreted of being boolean <i>false</i>.        precisely <code>0</code> is interpreted as being boolean <i>false</i>.
1256      </p>      </p>
1257      <p>      <p>
1258        So the logical operators shown above always look at numbers at a whole.        So the logical operators shown above always look at numbers at a whole.
# Line 1244  end on Line 1299  end on
1299      </code>      </code>
1300      <p>      <p>
1301        All these operations yield in a <i>boolean</i> result which could then        All these operations yield in a <i>boolean</i> result which could then
1302        by used i.e. with <code>if</code> or <code>while</code> loop statements.        be used e.g. with <code>if</code> or <code>while</code> loop statements.
1303      </p>      </p>
1304            
1305      <h3>String Operators</h3>      <h3>String Operators</h3>
# Line 1332  RESET_CONDITION(??condition-name??) Line 1387  RESET_CONDITION(??condition-name??)
1387        You should only reset a preprocessor condition that way if you did set it        You should only reset a preprocessor condition that way if you did set it
1388        with <code>SET_CONDITION(??condition-name??)</code> before. Trying to        with <code>SET_CONDITION(??condition-name??)</code> before. Trying to
1389        reset a condition that has not been set before, or trying to reset a        reset a condition that has not been set before, or trying to reset a
1390        condition that has already been reset, will both be ignored by the samlper,        condition that has already been reset, will both be ignored by the sampler,
1391        but again you will get a warning, and you should take care about it.        but again you will get a warning, and you should take care about it.
1392      </p>      </p>
1393            
# Line 1500  end on Line 1555  end on
1555        use the preprocessor instead for such things. And like stated above,        use the preprocessor instead for such things. And like stated above,
1556        there are certain things which you can only achieve with the preprocessor.        there are certain things which you can only achieve with the preprocessor.
1557      </p>      </p>
1558        
1559        <h3>Disable Messages</h3>
1560        <p>
1561          Since it is quite common to switch a script between a development version
1562          and a production version, you actually don't need to wrap all your
1563          <code>message()</code> calls into preprocessor statements like in the
1564          previous example just to disable messages. There is actually a built-in
1565          preprocessor condition dedicated to perform that task much more conveniently for you.
1566          To disable all messages in your script, simply add <code>SET_CONDITION(NKSP_NO_MESSAGE)</code>
1567          e.g. at the very beginning of your script.
1568          So the previous example can be simplified to this:
1569        </p>
1570        <code>
1571    { Enable debug mode, so show all debug messages. }
1572    SET_CONDITION(DEBUG_MODE)
1573    
1574    { If our user declared condition "DEBUG_MODE" is not set ... }
1575    USE_CODE_IF_NOT(DEBUG_MODE)
1576      { ... then enable this built-in condition to disable all message() calls. }
1577      SET_CONDITION(NKSP_NO_MESSAGE)
1578    END_USE_CODE
1579    
1580    on init
1581      declare const %primes[12] :=  ( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 )
1582      declare $i
1583    
1584      message("This script has just been loaded.")
1585    
1586      USE_CODE_IF(DEBUG_MODE)
1587      $i := 0
1588      while ($i < num_elements(%primes))
1589        message("Prime " & $i & " is " & %primes[$i])
1590        $i := $i + 1
1591      end while
1592      END_USE_CODE
1593    end on
1594    
1595    on note
1596      message("Note " & $EVENT_NOTE & " was triggered with velocity " & $EVENT_VELOCITY)
1597    end on
1598    
1599    on release
1600      message("Note " & $EVENT_NOTE & " was released with release velocity " & $EVENT_VELOCITY)
1601    end on
1602    
1603    on controller
1604      message("MIDI Controller " & $CC_NUM " changed its value to " & %CC[$CC_NUM])
1605    end on
1606        </code>
1607        <p>
1608          You can then actually also add <code>RESET_CONDITION(NKSP_NO_MESSAGE)</code>
1609          at another section of your script, which will cause all subsequent
1610          <code>message()</code> calls to be processed again. So that way you can
1611          easily enable and disable <code>message()</code> calls of entire individual
1612          sections of your script, without having to wrap all <code>message()</code>
1613          calls into preprocessor statements.
1614        </p>
1615    
1616      <h2>What Next?</h2>      <h2>What Next?</h2>
1617      <p>      <p>
1618        You have completed the introduction of the NKSP real-time instrument        You have completed the introduction of the NKSP real-time instrument
# Line 1510  end on Line 1622  end on
1622        Which provides you an overview and quick access to the details of all        Which provides you an overview and quick access to the details of all
1623        built-in functions, built-in variables and more.        built-in functions, built-in variables and more.
1624      </p>      </p>
1625        <p>
1626          You might also be interested to look at new <i>NKSP</i> core language
1627          features being added to the latest development version of the sampler:
1628          <a href="real_unit_final/01_nksp_real_unit_final.html">
1629            Real Numbers, Units and Finalness ...
1630          </a>
1631        </p>
1632    
1633    </body>    </body>
1634  </html>  </html>

Legend:
Removed from v.3262  
changed lines
  Added in v.3717

  ViewVC Help
Powered by ViewVC