| 1 | |
| 2 | <section id="OrchUDO"> |
| 3 | <title>User Defined Opcodes (UDO)</title> |
| 4 | <para> |
| 5 | Csound allows the definition of opcodes inside the orchestra header using the opcodes <link linkend="opcode"><citetitle>opcode</citetitle></link> and <link linkend="endop"><citetitle>endop</citetitle></link>. The defined opcode may run with a different number of control samples (<link linkend="ksmps"><citetitle>ksmps</citetitle></link>) using <link linkend="setksmps"><citetitle>setksmps</citetitle></link>. |
| 6 | </para> |
| 7 | <para> |
| 8 | To connect inputs and outputs for the UDO, use <link linkend="xin"><citetitle>xin</citetitle></link> and <link linkend="xout"><citetitle>xout</citetitle></link>. |
| 9 | </para> |
| 10 | <para> |
| 11 | An UDO looks like this: |
| 12 | </para> |
| 13 | <programlisting> |
| 14 | <emphasis role="oblock">opcode</emphasis> Lowpass, a, akk |
| 15 | |
| 16 | <emphasis role="opc">setksmps</emphasis> 1 <emphasis role="comment">; need sr=kr</emphasis> |
| 17 | ain, ka1, ka2 <emphasis role="opc">xin</emphasis> <emphasis role="comment">; read input parameters</emphasis> |
| 18 | aout <emphasis role="opc">init</emphasis> 0 <emphasis role="comment">; initialize output</emphasis> |
| 19 | aout <emphasis role="op">=</emphasis> ain<emphasis role="op">*</emphasis>ka1 <emphasis role="op">+</emphasis> aout<emphasis role="op">*</emphasis>ka2 <emphasis role="comment">; simple tone-like filter</emphasis> |
| 20 | <emphasis role="opc">xout</emphasis> aout <emphasis role="comment">; write output</emphasis> |
| 21 | |
| 22 | <emphasis role="oblock">endop</emphasis></programlisting> |
| 23 | <para> |
| 24 | This UDO called <emphasis>Lowpass</emphasis> takes 3 inputs (the first is a-rate, and the next two are k-rate), and delivers 1 a-rate output. Notice the use of <link linkend="xin"><citetitle>xin</citetitle></link> to receive inputs and <link linkend="xout"><citetitle>xout</citetitle></link> to deliver outputs. Also note the use of <link linkend="setksmps"><citetitle>setksmps</citetitle></link>, which is needed for the filter to work properly. |
| 25 | </para> |
| 26 | <para>To use this UDO within an instrument, you would do something like:</para> |
| 27 | <programlisting> |
| 28 | afiltered Lowpass asource, kvalue1, kvalue2</programlisting> |
| 29 | <para>See the entry for <link linkend="opcode"><citetitle>opcode</citetitle></link> for detailed information on UDO definition. |
| 30 | </para> |
| 31 | |
| 32 | <para> |
| 33 | Other opcodes that are to be used in a UDO are: |
| 34 | <itemizedlist> |
| 35 | <listitem><para><link linkend="oversample"><citetitle>oversample</citetitle></link></para> |
| 36 | </listitem> |
| 37 | <listitem><para><link linkend="undersample"><citetitle>undersample</citetitle></link></para> |
| 38 | </listitem> |
| 39 | </itemizedlist> |
| 40 | </para> |
| 41 | |
| 42 | <para> |
| 43 | You can find many ready-made UDO's (or contribute your own) at Csound's <ulink url="https://github.com/csudo/csudo"><citetitle>User Defined Opcode Database</citetitle></ulink>. |
| 44 | </para> |
| 45 | </section> |