/[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 2872 by schoenebeck, Sun Apr 10 18:42:55 2016 UTC revision 3119 by schoenebeck, Fri Apr 21 16:02:47 2017 UTC
# Line 11  Line 11 
11        your own instrument scripts in short time. It concentrates on describing        your own instrument scripts in short time. It concentrates on describing
12        the script language. If you rather want to learn how to modify and        the script language. If you rather want to learn how to modify and
13        attach scripts to your sounds, then please refer to the gigedit manual for        attach scripts to your sounds, then please refer to the gigedit manual for
14        <a href="gigedit_scripts.html">how to manage instrument scripts with gigedit</a>.        <a href="gigedit_scripts.html">how to manage instrument scripts with gigedit</a>
15          for Gigasampler/GigaStudio format sounds, or refer to the SFZ opcode
16          <code lang="sfz">script</code> for attaching NKSP scripts with
17          SFZ format sounds.
18      </p>      </p>
19    
20      <h3>At a Glance</h3>      <h3>At a Glance</h3>
21      <p>      <p>
22        <img src="nksp_file.png" style="height:111px; margin-right:12px;">        <img src="nksp_file.png" style="height:111px; margin-right:12px;">
23        NKSP stands for "is <b>N</b>ot <b>KSP</b>", which denotes its distinction        NKSP stands for "is <b>N</b>ot <b>KSP</b>", which denotes its distinction
24        to an existing proprieatary language called <i>KSP</i>.        to an existing proprietary language called <i>KSP</i>.
25        NSKP is a script language specifically designed to write real-time capable        NSKP is a script language specifically designed to write real-time capable
26        software extensions to LinuxSampler's sampler engines that can be bundled        software extensions to LinuxSampler's sampler engines that can be bundled
27        individually with sounds by sound designers themselves.        individually with sounds by sound designers themselves.
# Line 768  on note Line 771  on note
771        @postfix := "nd"        @postfix := "nd"
772      case 3      case 3
773        @postfix := "rd"        @postfix := "rd"
774    end if    end select
775    
776    message("This is the " & $numberOfNotes & @postfix & " note triggered so far.")    message("This is the " & $numberOfNotes & @postfix & " note triggered so far.")
777  end on  end on
# Line 809  on note Line 812  on note
812      case 3      case 3
813        message("Third note was triggered!") { Will never be printed ! }        message("Third note was triggered!") { Will never be printed ! }
814        exit        exit
815    end if    end select
816    
817    message("Wow, already the " & $numberOfNotes & "th note triggered.")    message("Wow, already the " & $numberOfNotes & "th note triggered.")
818  end on  end on
# Line 848  on note Line 851  on note
851      case 1 to 99      case 1 to 99
852        message("Less than 100 notes triggered so far")        message("Less than 100 notes triggered so far")
853        exit        exit
854    end if    end select
855    
856    message("Wow, already the " & $numberOfNotes & "th note triggered.")    message("Wow, already the " & $numberOfNotes & "th note triggered.")
857  end on  end on
# Line 928  end on Line 931  end on
931        loop is thus left at that point and the text message was printed        loop is thus left at that point and the text message was printed
932        three times in total.        three times in total.
933      </p>      </p>
934    
935        <h3>User Functions</h3>
936        <p>
937          We already came across various built-in functions, which you may call
938          by your scripts to perform certain tasks or behavior which is already
939          provided for you by the sampler. NKSP also allows you to write your
940          own functions, which you then may call from various places of your
941          script.
942        <p>  
943        </p>
944          When working on larger scripts, you
945          may notice that you easily get to the point where you may have to
946          duplicate portions of your script code, since there are certain things
947          that you may have to do again and again in different parts of your script.
948          Software developers usually try to avoid such code duplications to
949          keep the overall amount of code as small as possible, since the
950          overall amount of code would bloat quickly and would
951          make the software very hard to maintain. One way for you to avoid such
952          script code duplications with NKSP is to write so called <i>User Functions</s>.
953        </p>
954        <p>
955          Let's assume you wanted to create a simple stuttering effect. You may do so
956          like in the following example.
957        </p>
958        <code>
959    on note
960      while (1)
961        wait(200000)
962        if (not (event_status($EVENT_ID) .and. $EVENT_STATUS_NOTE_QUEUE))
963          exit()
964        end if
965        change_vol($EVENT_ID, -20000)  { Reduce volume by 20 dB. }
966        wait(200000)
967        if (not (event_status($EVENT_ID) .and. $EVENT_STATUS_NOTE_QUEUE))
968          exit()
969        end if
970        change_vol($EVENT_ID, 0)  { Increase volume to 0 dB. }
971      end while
972    end on
973        </code>
974        <p>
975          This script will run an endless loop for each note being triggered.
976          Every <code lang="none">200ms</code> it will turn the volume alternatingly down and
977          up to create the audible stuttering effect. After each <code lang="nksp">wait()</code>
978          call it calls <code>event_status($EVENT_ID)</code> to check whether
979          this note is still alive, and as soon as the note died, it will stop
980          execution of the script instance by calling <code>exit()</code>. The latter
981          is important in this example, because otherwise the script execution instances would
982          continue to run in this endless loop forever, even after the respectives
983          notes are gone. Which would let your CPU usage to increase with every new note
984          and would never decrease again.
985          This behavior of the sampler is not a bug, it is intended, since there may
986          also be cases where you want to do certain things by script even after the
987          respective notes are dead and gone. However as you can see, that script is
988          using the same portions of script code twice. To avoid that, you could also
989          write the same script with a user function like this:
990        </p>
991        <code>
992    function pauseMyScript
993      wait(200000)
994      if (not (event_status($EVENT_ID) .and. $EVENT_STATUS_NOTE_QUEUE))
995        exit()
996      end if
997    end function
998            
999    on note
1000      while (1)
1001        call pauseMyScript
1002        change_vol($EVENT_ID, -20000)  { Reduce volume by 20 dB. }
1003        call pauseMyScript
1004        change_vol($EVENT_ID, 0)  { Increase volume back to 0 dB. }
1005      end while
1006    end on
1007        </code>
1008        <p>
1009          The script became in this simple example only slightly smaller, but it also
1010          became easier to read and behaves identically to the previous solution.
1011          And in practice, with a more complex script, you can
1012          reduce the overall amount of script code a lot this way. You can choose any
1013          name for your own user functions, as long as the name is not already
1014          reserved by a built-in function. Note that for calling a user function,
1015          you must always precede the actual user function name with the
1016          <code>call</code> keyword. Likewise you may however not use the
1017          <code>call</code> keyword for calling any built-in function. So that
1018          substantially differs calling built-in functions from calling user functions.
1019        </p>
1020    
1021      <h2>Operators</h2>      <h2>Operators</h2>
1022      <p>      <p>
1023        A programming language provides so called <i>operators</i> to perform        A programming language provides so called <i>operators</i> to perform
# Line 958  end on Line 1047  end on
1047      <h3>Boolean Operators</h3>      <h3>Boolean Operators</h3>
1048      <p>      <p>
1049        To perform logical transformations of <i>boolean</i> data, you may use the        To perform logical transformations of <i>boolean</i> data, you may use the
1050        following boolean operators:        following logical operators:
1051      </p>      </p>
1052      <code>      <code>
1053  on init  on init
# Line 971  on init Line 1060  on init
1060  end on  end on
1061      </code>      </code>
1062      <p>      <p>
1063        Remember that with boolean operations, all integer values other than <code>0</code>        Keep in mind that with logical operators shown above,
1064          all integer values other than <code>0</code>
1065        are interpreted as boolean <i>true</i> while an integer value of        are interpreted as boolean <i>true</i> while an integer value of
1066        precisely <code>0</code> is interpreted of being boolean <i>false</i>.        precisely <code>0</code> is interpreted of being boolean <i>false</i>.
1067      </p>      </p>
1068        <p>
1069          So the logical operators shown above always look at numbers at a whole.
1070          Sometimes however you might rather need to process numbers bit by bit. For
1071          that purpose the following bitwise operators exist.
1072        </p>
1073        <code>
1074    on init
1075      message("1 .and. 1 is " & 1 .and. 1)  { bitwise "and" }
1076      message("1 .and. 0 is " & 1 .and. 0)  { bitwise "and" }
1077      message("1 .or. 1 is " & 1 .or. 1)    { bitwise "or" }
1078      message("1 .or. 0 is " & 1 .or. 0)    { bitwise "or" }
1079      message(".not. 1 is " & .not. 1)      { bitwise "not" }
1080      message(".not. 0 is " & .not. 0)      { bitwise "not" }
1081    end on
1082        </code>
1083        <p>
1084          Bitwise operators work essentially like logical operators, with the
1085          difference that bitwise operators compare each bit independently.
1086          So a bitwise <code>.and.</code> operator for instance takes the 1st bit
1087          of the left hand's side value, the 1st bit of the right hand's side value,
1088          compares the two bits logically and then stores that result as 1st bit of
1089          the final result value, then it takes the 2nd bit of the left hand's side value
1090          and the 2nd bit of the right hand's side value, compares those two bits logically
1091          and then stores that result as 2nd bit of the final result value, and so on.
1092        </p>
1093    
1094            
1095      <h3>Comparison Operators</h3>      <h3>Comparison Operators</h3>
1096      <p>      <p>

Legend:
Removed from v.2872  
changed lines
  Added in v.3119

  ViewVC Help
Powered by ViewVC