| 1 | <refentry id="opcode"> |
| 2 | <indexterm id="IndexOpcode"><primary>opcode</primary></indexterm> |
| 3 | <refentryinfo><title>Orchestra Syntax:Block Statements</title></refentryinfo> |
| 4 | <refmeta> |
| 5 | <refentrytitle>opcode</refentrytitle> |
| 6 | </refmeta> |
| 7 | <refnamediv> |
| 8 | <refname>opcode</refname> |
| 9 | <refpurpose> |
| 10 | Defines the start of user-defined opcode block. |
| 11 | </refpurpose> |
| 12 | </refnamediv> |
| 13 | |
| 14 | <refsect1> |
| 15 | <title>Defining opcodes</title> |
| 16 | <para> |
| 17 | The <emphasis>opcode</emphasis> and <emphasis>endop</emphasis> statements allow defining a new opcode that can be used the same way as any of the built-in Csound opcodes. These opcode blocks are very similar to instruments (and are, in fact, implemented as special instruments), but cannot be called as a normal instrument e.g. with the <link linkend="i"><citetitle>i statements</citetitle></link>. |
| 18 | </para> |
| 19 | |
| 20 | <para> |
| 21 | A user-defined opcode block must precede the instrument (or other opcode) from which it is used. But it is possible to call the opcode from itself. This allows recursion of any depth that is limited only by available memory. |
| 22 | </para> |
| 23 | |
| 24 | <para> |
| 25 | Similarly to instruments, the variables and labels of a user-defined opcode block are local and cannot be accessed from the caller instrument (and the opcode cannot access variables of the caller, either). |
| 26 | </para> |
| 27 | |
| 28 | <para> |
| 29 | Some parameters are automatically copied at initialization, however: |
| 30 | |
| 31 | <itemizedlist> |
| 32 | <listitem><para>all p-fields up to and including the |
| 33 | highest-numbered one referenced in the calling |
| 34 | instrument</para></listitem> |
| 35 | <listitem><para>extra time (see also <link linkend="xtratim"><citetitle>xtratim</citetitle></link>, <link linkend="linsegr"><citetitle>linsegr</citetitle></link>, and related opcodes). This may affect the operation of <emphasis>linsegr</emphasis>/<emphasis>expsegr</emphasis>/<emphasis>linenr</emphasis>/<emphasis>envlpxr</emphasis> in the user-defined opcode block.</para></listitem> |
| 36 | <listitem><para>MIDI parameters, if there are any.</para></listitem> |
| 37 | </itemizedlist> |
| 38 | </para> |
| 39 | |
| 40 | <para> |
| 41 | Also, the release flag (see the <link linkend="release"><citetitle>release</citetitle></link> opcode) is copied at performance time. |
| 42 | </para> |
| 43 | |
| 44 | <para> |
| 45 | Modifying the note duration in the opcode definition by assigning to |
| 46 | <emphasis>p3</emphasis>, or using <link linkend="ihold"><citetitle>ihold</citetitle></link>, |
| 47 | <link linkend="turnoff"><citetitle>turnoff</citetitle></link>, |
| 48 | <link linkend="xtratim"><citetitle>xtratim</citetitle></link>, |
| 49 | <link linkend="linsegr"><citetitle>linsegr</citetitle></link>, |
| 50 | or similar opcodes will also affect the caller instrument. |
| 51 | Changes to MIDI controllers (for example with |
| 52 | <link linkend="ctrlinit"><citetitle>ctrlinit</citetitle></link>) will |
| 53 | also apply to the instrument from which the opcode was called. |
| 54 | </para> |
| 55 | |
| 56 | <para> |
| 57 | Use the <link linkend="setksmps"><citetitle>setksmps</citetitle></link> opcode to set the local <link linkend="ksmps"><citetitle>ksmps</citetitle></link> value. |
| 58 | </para> |
| 59 | |
| 60 | <para> |
| 61 | The <link linkend="xin"><citetitle>xin</citetitle></link> and <link linkend="xout"><citetitle>xout</citetitle></link> opcodes copy variables to and from the opcode definition, allowing communication with the calling instrument. |
| 62 | </para> |
| 63 | |
| 64 | <para> |
| 65 | The types of input and output variables are defined by the parameters <emphasis>intypes</emphasis> and <emphasis>outtypes</emphasis>. |
| 66 | <tip><para> |
| 67 | You can create UDOs which take no inputs or outputs by using 0 instead of a string.</para></tip> |
| 68 | </para> |
| 69 | |
| 70 | <note> |
| 71 | <title>Notes</title> |
| 72 | <para> |
| 73 | <itemizedlist> |
| 74 | <listitem><para><emphasis>xin</emphasis> and <emphasis>xout</emphasis> should be called only once, and <emphasis>xin</emphasis> should precede <emphasis>xout</emphasis>, otherwise an init error and deactivation of the current instrument may occur.</para></listitem> |
| 75 | <listitem><para>These opcodes actually run only at i-time. Performance time copying is done by the user opcode call. This means that skipping <emphasis>xin</emphasis> or <emphasis>xout</emphasis> with <link linkend="kgoto"><citetitle>kgoto</citetitle></link> has no effect, while skipping with <link linkend="igoto"><citetitle>igoto</citetitle></link> affects both init and performance time operation.</para></listitem> |
| 76 | </itemizedlist> |
| 77 | </para> |
| 78 | </note> |
| 79 | </refsect1> |
| 80 | |
| 81 | <refsect1> |
| 82 | <title>Syntax</title> |
| 83 | <synopsis><command>opcode</command> name, outtypes, intypes</synopsis> |
| 84 | </refsect1> |
| 85 | |
| 86 | <refsect1> |
| 87 | <title>Initialization</title> |
| 88 | <para> |
| 89 | <emphasis>name</emphasis> -- name of the opcode. It may consist of any combination of letters, digits, and underscore but should not begin with a digit. If an opcode with the specified name already exists, it is redefined (a warning is printed in such cases). Some reserved words (like <emphasis>instr</emphasis> and <emphasis>endin</emphasis>) cannot be redefined. |
| 90 | </para> |
| 91 | |
| 92 | <para> |
| 93 | <emphasis>intypes</emphasis> -- list of input types, any combination of the characters: a, f, k, O, P, V, K, i, o, p, and j. A single 0 character can be used if there are no input arguments. Double quotes and delimiter characters (e.g. comma) are <emphasis>not</emphasis> needed. |
| 94 | </para> |
| 95 | |
| 96 | <para> |
| 97 | The meaning of the various <emphasis>intypes</emphasis> is shown in the following table: |
| 98 | |
| 99 | <informaltable frame="all"> |
| 100 | <tgroup cols="4" colsep="1" rowsep="1"> |
| 101 | <thead> |
| 102 | <row> |
| 103 | <entry>Type</entry> |
| 104 | <entry>Description</entry> |
| 105 | <entry>Variable Types Allowed</entry> |
| 106 | <entry>Updated At</entry> |
| 107 | </row> |
| 108 | </thead> |
| 109 | |
| 110 | <tbody> |
| 111 | <row> |
| 112 | <entry>a</entry> |
| 113 | <entry>a-rate variable</entry> |
| 114 | <entry>a-rate</entry> |
| 115 | <entry>a-rate</entry> |
| 116 | </row> |
| 117 | |
| 118 | <row> |
| 119 | <entry>f</entry> |
| 120 | <entry>f-sig variable</entry> |
| 121 | <entry>f-sig</entry> |
| 122 | <entry>k-rate</entry> |
| 123 | </row> |
| 124 | |
| 125 | <row> |
| 126 | <entry>i</entry> |
| 127 | <entry>i-rate variable</entry> |
| 128 | <entry>i-rate</entry> |
| 129 | <entry>i-time</entry> |
| 130 | </row> |
| 131 | |
| 132 | <row> |
| 133 | <entry>j</entry> |
| 134 | <entry>optional i-time, defaults to -1</entry> |
| 135 | <entry>i-rate, constant</entry> |
| 136 | <entry>i-time</entry> |
| 137 | </row> |
| 138 | |
| 139 | <row> |
| 140 | <entry>k</entry> |
| 141 | <entry>k-rate variable</entry> |
| 142 | <entry>k- and i-rate, constant</entry> |
| 143 | <entry>k-rate</entry> |
| 144 | </row> |
| 145 | |
| 146 | <row> |
| 147 | <entry>O</entry> |
| 148 | <entry>optional k-rate variable, defaults to 0</entry> |
| 149 | <entry>k- and i-rate, constant</entry> |
| 150 | <entry>k-rate</entry> |
| 151 | </row> |
| 152 | |
| 153 | <row> |
| 154 | <entry>P</entry> |
| 155 | <entry>optional k-rate variable, defaults to 1</entry> |
| 156 | <entry>k- and i-rate, constant</entry> |
| 157 | <entry>k-rate</entry> |
| 158 | </row> |
| 159 | |
| 160 | <row> |
| 161 | <entry>V</entry> |
| 162 | <entry>optional k-rate variable, defaults to 0.5</entry> |
| 163 | <entry>k- and i-rate, constant</entry> |
| 164 | <entry>k-rate</entry> |
| 165 | </row> |
| 166 | |
| 167 | <row> |
| 168 | <entry>J</entry> |
| 169 | <entry>optional k-rate variable, defaults to -1</entry> |
| 170 | <entry>k- and i-rate, constant</entry> |
| 171 | <entry>k-rate</entry> |
| 172 | </row> |
| 173 | |
| 174 | <row> |
| 175 | <entry>K</entry> |
| 176 | <entry>k-rate with initialization</entry> |
| 177 | <entry>k- and i-rate, constant</entry> |
| 178 | <entry>i-time and k-rate</entry> |
| 179 | </row> |
| 180 | |
| 181 | <row> |
| 182 | <entry>o</entry> |
| 183 | <entry>optional i-time, defaults to 0</entry> |
| 184 | <entry>i-rate, constant</entry> |
| 185 | <entry>i-time</entry> |
| 186 | </row> |
| 187 | |
| 188 | <row> |
| 189 | <entry>p</entry> |
| 190 | <entry>optional i-time, defaults to 1</entry> |
| 191 | <entry>i-rate, constant</entry> |
| 192 | <entry>i-time</entry> |
| 193 | </row> |
| 194 | |
| 195 | <row> |
| 196 | <entry>S</entry> |
| 197 | <entry>string variable</entry> |
| 198 | <entry>k- and i-rate string, constant</entry> |
| 199 | <entry>i-time and k-rate</entry> |
| 200 | </row> |
| 201 | </tbody> |
| 202 | </tgroup> |
| 203 | </informaltable> |
| 204 | </para> |
| 205 | |
| 206 | <para> |
| 207 | <emphasis>outtypes</emphasis> -- list of output types. The format is the same as in the case of <emphasis>intypes</emphasis>. |
| 208 | </para> |
| 209 | |
| 210 | <para> |
| 211 | Here are the available <emphasis>outtypes</emphasis>: |
| 212 | |
| 213 | <informaltable frame="all"> |
| 214 | <tgroup cols="4" colsep="1" rowsep="1"> |
| 215 | <thead> |
| 216 | <row> |
| 217 | <entry>Type</entry> |
| 218 | <entry>Description</entry> |
| 219 | <entry>Variable Types Allowed</entry> |
| 220 | <entry>Updated At</entry> |
| 221 | </row> |
| 222 | </thead> |
| 223 | |
| 224 | <tbody> |
| 225 | <row> |
| 226 | <entry>a</entry> |
| 227 | <entry>a-rate variable</entry> |
| 228 | <entry>a-rate</entry> |
| 229 | <entry>a-rate</entry> |
| 230 | </row> |
| 231 | |
| 232 | <row> |
| 233 | <entry>f</entry> |
| 234 | <entry>f-sig variable</entry> |
| 235 | <entry>f-sig</entry> |
| 236 | <entry>k-rate</entry> |
| 237 | </row> |
| 238 | |
| 239 | <row> |
| 240 | <entry>i</entry> |
| 241 | <entry>i-rate variable</entry> |
| 242 | <entry>i-rate</entry> |
| 243 | <entry>i-time</entry> |
| 244 | </row> |
| 245 | |
| 246 | <row> |
| 247 | <entry>k</entry> |
| 248 | <entry>k-rate variable</entry> |
| 249 | <entry>k-rate</entry> |
| 250 | <entry>k-rate</entry> |
| 251 | </row> |
| 252 | |
| 253 | <row> |
| 254 | <entry>K</entry> |
| 255 | <entry>k-rate with initialization</entry> |
| 256 | <entry>k-rate</entry> |
| 257 | <entry>i-time and k-rate</entry> |
| 258 | </row> |
| 259 | <row> |
| 260 | <entry>S</entry> |
| 261 | <entry>string variable</entry> |
| 262 | <entry>k- and i-rate string</entry> |
| 263 | <entry>i-time and k-rate</entry> |
| 264 | </row> |
| 265 | </tbody> |
| 266 | </tgroup> |
| 267 | </informaltable> |
| 268 | </para> |
| 269 | |
| 270 | <para> |
| 271 | The maximum allowed number of output arguments is 256. |
| 272 | </para> |
| 273 | |
| 274 | |
| 275 | <para> |
| 276 | The input parameters can be read with <emphasis>xin</emphasis>, and the |
| 277 | output is written by <emphasis>xout</emphasis> opcode. Only one instance |
| 278 | of these units should be used, as <emphasis>xout</emphasis> overwrites |
| 279 | and does not accumulate the output. The number and type of arguments for |
| 280 | <emphasis>xin</emphasis> and <emphasis>xout</emphasis> must be the same |
| 281 | as in the declaration of the user-defined opcode block (see tables above). |
| 282 | </para> |
| 283 | |
| 284 | </refsect1> |
| 285 | |
| 286 | <refsect1> |
| 287 | <title>Performance</title> |
| 288 | <para> |
| 289 | The syntax of a user-defined opcode block is as follows: |
| 290 | |
| 291 | <programlisting> |
| 292 | <emphasis role="oblock">opcode</emphasis> <command>name</command>, outtypes, intypes |
| 293 | xinarg1 [, xinarg2] [, xinarg3] ... [xinargN] <emphasis role="opc">xin</emphasis> |
| 294 | [<emphasis role="opc">setksmps</emphasis> iksmps] |
| 295 | ... the rest of the instrument's code. |
| 296 | <emphasis role="opc">xout</emphasis> xoutarg1 [, xoutarg2] [, xoutarg3] ... [xoutargN] |
| 297 | <emphasis role="oblock">endop</emphasis></programlisting> |
| 298 | </para> |
| 299 | |
| 300 | <para> |
| 301 | The new opcode can then be used with the usual syntax: |
| 302 | |
| 303 | <programlisting> |
| 304 | [xoutarg1] [, xoutarg2] ... [xoutargN] <command>name</command> [xinarg1] [, xinarg2] ... [xinargN]</programlisting> |
| 305 | </para> |
| 306 | |
| 307 | <note> |
| 308 | <title>Note</title> |
| 309 | <para> |
| 310 | The opcode call is always executed both at initialization and |
| 311 | performance time, even if there are no a- or k-rate arguments. |
| 312 | If there are many user opcode calls that are known to have no |
| 313 | effect at performance time in an instrument, then it may save some |
| 314 | CPU time to jump over groups of such opcodes with |
| 315 | <link linkend="kgoto"><citetitle>kgoto</citetitle></link>. |
| 316 | </para> |
| 317 | </note> |
| 318 | </refsect1> |
| 319 | |
| 320 | <refsect1> |
| 321 | <title>Examples</title> |
| 322 | <para> |
| 323 | Here is an example of a user-defined opcode. It uses the file |
| 324 | <ulink |
| 325 | url="examples/opcode_example.csd"><citetitle>opcode.csd</citetitle></ulink>. |
| 326 | |
| 327 | <example> |
| 328 | <title>Example of a user-defined opcode.</title> |
| 329 | |
| 330 | <para>See the sections <link |
| 331 | linkend="UsingRealTime"><citetitle>Real-time |
| 332 | Audio</citetitle></link> and <link |
| 333 | linkend="CommandFlags"><citetitle>Command Line |
| 334 | Flags</citetitle></link> for more information on using command |
| 335 | line flags.</para> |
| 336 | <xi:include href="examples-xml/opcode_example.csd.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/> |
| 337 | </example> |
| 338 | </para> |
| 339 | |
| 340 | <para> |
| 341 | Here is another example of a user-defined opcode. It uses the file |
| 342 | <ulink |
| 343 | url="examples/opcode_f.csd"><citetitle>opcode_f.csd</citetitle></ulink>. |
| 344 | <example> |
| 345 | <title>Second example of a user-defined opcode.</title> |
| 346 | <xi:include href="examples-xml/opcode_f.csd.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/> |
| 347 | </example> |
| 348 | </para> |
| 349 | </refsect1> |
| 350 | |
| 351 | <refsect1> |
| 352 | <title>See Also</title> |
| 353 | <para> |
| 354 | <link linkend="OrchUDO"><citetitle>User Defined Opcodes (UDO)</citetitle></link> |
| 355 | </para> |
| 356 | </refsect1> |
| 357 | |
| 358 | <refsect1> |
| 359 | <title>Credits</title> |
| 360 | <para>Author: Istvan Varga, 2002; based on code by Matt J. Ingalls</para> |
| 361 | <para>New in version 4.22</para> |
| 362 | </refsect1> |
| 363 | </refentry> |