Debian Codegraph @

csound5/extending.xml

raw symbols SCIP
1 <chapter id="csound5extending">
2
3 <title>Extending Csound</title>
4
5 <section id="csound5addingopcodes">
6
7 <title>Adding Unit Generators</title>
8
9 <para>If the existing Csound unit generators do not suit your needs,
10 it is relatively easy to extend Csound by writing new unit generators
11 in C or C++. The translator, loader, and run-time monitor will treat
12 your module just like any other, provided you follow some conventions.
13 </para>
14
15 <para>
16 Historically, this has been done with builtin unit generators,
17 that is, with code that is statically linked with the rest of the
18 Csound executable.
19 </para>
20
21 <para>
22 Today, the preferred method is to create plugin unit
23 generators. These are dynamic link libraries (DLLs) on Windows, and
24 loadable modules (shared libraries that are <literal>dlopen</literal>ed) on
25 Linux. Csound searches for and loads these plugins at run time on the path defined in <link linkend="CommandEnvironment"><citetitle>OPCODEDIR</citetitle></link>. You
26 can also load plugin opcodes from the command line using the <link linkend="FlagsMinusOpcodeLib"><citetitle>--opcode-lib</citetitle></link> flag.
27 </para>
28
29 <para>
30 The
31 advantage of this method, of course, is that plugins created by any
32 developer at any time can be used with already existing versions of
33 Csound.
34 </para>
35
36 <section>
37
38 <title>Creating a Builtin Unit Generator</title>
39
40 <para>You need a structure defining the inputs, outputs and
41 workspace, plus some initialization code and some perf-time code. Let's
42 put an example of these in two new files,
43 <filename>newgen.h</filename> and
44 <filename>newgen.c</filename>. The examples given are for Csound 5.
45 For earlier versions, all opcode functions omit the first parameter (<literal>CSOUND *csound</literal>).
46
47 <informalexample>
48 <programlisting>
49 /* newgen.h - define a structure */
50
51 /* Declares Csound structures and functions. */
52 #include &quot;csoundCore.h&quot;
53
54 typedef struct
55 {
56 OPDS h; /* required header */
57 MYFLT *result, *istrt, *incr, *itime, *icontin; /* addr outarg, inargs */
58 MYFLT curval, vincr; /* private dataspace */
59 long countdown; /* ditto */
60 } RMP;
61
62
63 /* newgen.c - init and perf code */
64 /* Declares Csound structures and functions. */
65 #include &quot;csoundCore.h&quot;
66 /* Declares RMP structure. */
67 #include &quot;newgen.h&quot;
68
69 int rampset (CSOUND *csound, RMP * p) /* at note initialization: */
70 {
71 if (*p-&gt;icontin == FL(0.0))
72 p-&gt;curval = *p-&gt;istrt; /* optionally get new start value */
73 p-&gt;vincr = *p-&gt;incr / csound-&gt;esr; /* set s-rate increment per sec. */
74 p-&gt;countdown = *p-&gt;itime * csound-&gt;esr; /* counter for itime seconds */
75 return OK;
76 }
77
78 int ramp (CSOUND *csound, RMP * p) /* during note performance: */
79 {
80 MYFLT *rsltp = p-&gt;result; /* init an output array pointer */
81 int nn = csound-&gt;ksmps; /* array size from orchestra */
82 do
83 {
84 *rsltp++ = p-&gt;curval; /* copy current value to output */
85 if (--p-&gt;countdown &gt; 0) /* for the first itime seconds, */
86 p-&gt;curval += p-&gt;vincr; /* ramp the value */
87 }
88 while (--nn);
89 return OK;
90 }</programlisting>
91 </informalexample>
92 </para>
93
94 <para>Now we add this module to the translator table in
95 <filename>entry1.c</filename>, under the opcode name
96 <literal>rampt</literal>:
97
98 <informalexample>
99 <programlisting>
100 #include &quot;newgen.h&quot;
101
102 int rampset(CSOUND *, RMP *), ramp(CSOUND *, RMP *);
103
104 /* opname dsblksiz thread outypes intypes iopadr kopadr aopadr */
105
106 { &quot;rampt&quot;, S(RMP), 5, &quot;a&quot;, &quot;iiio&quot;, (SUBR) rampset, (SUBR) NULL, (SUBR) ramp },</programlisting>
107 </informalexample>
108 </para>
109
110 <para>Finally you must relink Csound with the new module. Add the name of the C file to the <literal>libCsoundSources</literal> list in the <literal>SConstruct</literal> file:
111
112 <informalexample>
113 <programlisting>
114 libCsoundSources = Split('''
115 Engine/auxfd.c
116 ...
117 OOps/newgen.c
118 ...
119 Top/utility.c
120 ''')</programlisting>
121 </informalexample>
122 </para>
123
124 <para>Run <application>scons</application> just as you would for any
125 other Csound build, and the new module will be built into your Csound.</para>
126
127 <para>
128 The above actions have added a new generator to the Csound language. It is an audio-rate linear ramp function which modifies an input value at a user-defined slope for some period. A ramp can optionally continue from the previous note's last value. The Csound manual entry would look like:
129 <informalexample>
130 <programlisting>ar rampt istart, islope, itime [, icontin]</programlisting>
131 </informalexample>
132 </para>
133
134 <para>
135 <emphasis>istart</emphasis> -- beginning value of an audio-rate linear ramp. Optionally overridden by a continue flag.
136 </para>
137
138 <para>
139 <emphasis>islope</emphasis> -- slope of ramp, expressed as the y-interval change per second.
140 </para>
141
142 <para><emphasis>itime</emphasis> -- ramp time in seconds, after which the value is held for the remainder of the note.
143 </para>
144
145 <para><emphasis>icontin</emphasis> (optional) -- continue flag. If zero, ramping will proceed from input <emphasis>istart</emphasis> . If non-zero, ramping will proceed from the last value of the previous note. The default value is zero.
146 </para>
147
148 <para>
149 The file <filename>newgen.h</filename> includes a one-line list of
150 output and input parameters. These are the ports through which the
151 new generator will communicate with the other generators in an
152 instrument. Communication is by <emphasis>address</emphasis>, not
153 <emphasis>value</emphasis>, and this is a list of pointers to
154 values of type MYFLT (which is double if the macro USE_DOUBLE is
155 defined, and float otherwise). There are no restrictions on names,
156 but the input-output argument types are further defined by character
157 strings in entry1.c (inargs, outargs). Inarg types are commonly
158 <emphasis>x</emphasis>, <emphasis>a</emphasis>,
159 <emphasis>k</emphasis>, and <emphasis>i</emphasis>, in the normal
160 Csound manual conventions; also available are o (optional,
161 defaulting to 0), p (optional, defaulting to 1). Outarg types
162 include <emphasis>a</emphasis>, <emphasis>k</emphasis>,
163 <emphasis>i</emphasis> and <emphasis>s</emphasis> (asig or
164 ksig). It is important that all listed argument names be assigned
165 a corresponding argument type in <filename>entry1.c</filename>.
166 Also, i-type args are valid only at initialization time, and other-type
167 args are available only at perf time. Subsequent lines in the RMP
168 structure declare the work space needed to keep the code re-entrant.
169 These enable the module to be used multiple times in multiple instrument
170 copies while preserving all data.</para>
171
172 <para>
173 The file <emphasis>newgen.c</emphasis> contains two subroutines,
174 each called with a pointer to the Csound instance and a pointer to the uniquely allocated RMP structure and its data. The subroutines can be of three types: note initialization, <literal>k</literal>-rate signal generation, <literal>a</literal>-rate signal generation. A module normally requires two of these: initialization, and either <literal>k</literal>-rate or <literal>a</literal>-rate subroutines which become inserted in various threaded lists of runnable tasks when an instrument is activated. The thread-types appear in entry1.c in two forms: <emphasis>isub</emphasis>, <emphasis>ksub</emphasis> and <emphasis>asub</emphasis> names; and a threading index which is the sum of isub=1, ksub=2, asub=4. The code itself may reference (but should only read) public members of the CSOUND structure defined in <emphasis>csoundCore.h</emphasis>, the most useful of which are:
175
176 <informalexample>
177 <programlisting>
178 OPARMS *oparms
179 MYFLT esr user-defined sampling rate
180 MYFLT ekr user-defined control rate
181 int ksmps user-defined ksmps
182 int nchnls user-defined nchnls
183 int oparms-&gt;odebug command-line -v flag
184 int oparms-&gt;msglevel command-line -m level
185 MYFLT tpidsr 2 * PI / esr</programlisting>
186 </informalexample>
187 </para>
188
189 <section id="CS5AddmodFtables">
190 <title>Function tables</title>
191
192 <para>
193 To access <indexterm id="CS5IndexStoredFuncTables"><primary>tables</primary><secondary>stored function</secondary></indexterm> stored function tables, special help is available. The newly defined structure should include a pointer
194 <informalexample>
195 <programlisting>FUNC *ftp;</programlisting>
196 </informalexample>
197 </para>
198
199 <para>
200 initialized by the statement
201 <informalexample>
202 <programlisting>ftp = csound-&gt;FTFind(csound, p-&gt;ifuncno);</programlisting>
203 </informalexample>
204 </para>
205
206 <para>
207 where MYFLT *ifuncno is an i-type input argument containing the ftable number. The stored table is then at ftp-&gt;ftable, and other data such as length, phase masks, cps-to-incr converters, are also accessed from this pointer. See the FUNC structure in csoundCore.h, the csoundFTFind() code in fgens.c, and the code for oscset() and koscil() in OOps/ugens2.c.
208 </para>
209 </section>
210
211 <section id="AddmodAddSpace">
212 <title>Additional Space</title>
213
214 <para>
215 Sometimes the space requirement of a module is too large to be part of a structure (upper limit 65279 bytes, due to the unsigned short dsblksiz parameter and reserved codes >= 0xFF00), or it is dependent on an i-arg value which is not known until initialization. Additional space can be dynamically allocated and properly managed by including the line
216 <informalexample>
217 <programlisting>AUXCH auxch;</programlisting>
218 </informalexample>
219 </para>
220
221 <para>
222 in the defined structure (*p), then using the following style of code in the init module:
223 <informalexample>
224 <programlisting>
225 csound-&gt;AuxAlloc(csound, npoints * sizeof(MYFLT), &amp;p-&gt;auxch);</programlisting>
226 </informalexample>
227 </para>
228
229 <para>
230 The address of this auxiliary space is kept in a chain of such spaces belonging to this instrument, and is automatically managed while the instrument is being duplicated or garbage-collected during performance. The assignment
231 <informalexample>
232 <programlisting>void *auxp = p-&gt;auxch.auxp;</programlisting>
233 </informalexample>
234 </para>
235
236 <para>
237 will find the allocated space for init-time and perf-time use. See the LINSEG structure in ugens1.h and the code for lsgset() and klnseg() in OOps/ugens1.c.
238 </para>
239 </section>
240
241 <section id="AddmodFsharing">
242 <title>File Sharing</title>
243 <para>
244 When accessing an external file often, or doing it from multiple places, it is often efficient to read the entire file into memory. This is accomplished by including the line
245 <informalexample>
246 <programlisting>MEMFIL *mfp;</programlisting>
247 </informalexample>
248 </para>
249
250 <para>
251 in the defined structure (*p), then using the following style of code in the init module:
252 <informalexample>
253 <programlisting>p-&gt;mfp = csound-&gt;ldmemfile(csound, filname);</programlisting>
254 </informalexample>
255 </para>
256
257 <para>
258 where char *filname is a string name of the file requested. The data read will be found between
259 <informalexample>
260 <programlisting>(char *) p-&gt;mfp-&gt;beginp; and (char *) p-&gt;mfp-&gt;endp;</programlisting>
261 </informalexample>
262 </para>
263
264 <para>
265 Loaded files do not belong to a particular instrument, but are automatically shared for multiple access. See the ADSYN structure in ugens3.h and the code for adset() and adsyn() in OOps/ugens3.c.
266 </para>
267 </section>
268
269 <section id="AddmodStringargs">
270 <title>String arguments</title>
271
272 <para>
273 To permit a string input argument (MYFLT *ifilnam, say) in our defined structure (*p), assign it the argtype <emphasis>S</emphasis> in entry1.c, and include the following code in the init module:
274 <informalexample>
275 <programlisting>strcpy(filename, (char*) p-&gt;ifilnam);</programlisting>
276 </informalexample>
277 </para>
278
279 <para>
280 See the code for adset() in OOps/ugens3.c, lprdset() in OOps/ugens5.c, and pvset() in OOps/ugens8.c.
281 </para>
282
283 </section>
284
285 </section>
286
287 <section>
288
289 <title>Adding a Plugin Unit Generator</title>
290
291 <para>The procedure for creating a plugin unit generator is very
292 similar to the procedure for creating a builtin. The actual unit
293 generator code would normally be identical. The differences are as
294 follows.</para>
295
296 <para>Again supposing that your unit generator is named
297 <literal>newgen</literal>, perform the following steps:</para>
298
299 <orderedlist>
300
301 <listitem><para>Write your <literal>newgen.c</literal> and <literal>newgen.h</literal> file as
302 you would for a builtin unit generator. Put these files in the
303 <literal>csound5/Opcodes</literal> directory.</para></listitem>
304
305 <listitem><para><literal>#include "csdl.h"</literal> in your unit generator
306 sources, instead of csoundCore.h.</para></listitem>
307
308 <listitem><para>Add your <literal>OENTRY</literal> records and unit generator
309 registration functions at the bottom of your C file. Example (but you
310 can have as many unit generators in one plugin as you like):</para>
311
312 <programlisting>
313 #define S sizeof
314
315 static OENTRY localops[] = {
316 { "rampt", S(RMP), 5, "a", "iiio", (SUBR) rampset, (SUBR) NULL, (SUBR)ramp },
317 };
318
319 /*
320 * The following macro from csdl.h defines
321 * the "csound_opcode_init()" opcode registration
322 * function for the localops table.
323 */
324 LINKAGE</programlisting>
325 </listitem>
326
327 <listitem><para>Add your plugin as a new target in the plugin opcodes section of the <literal>SConstruct</literal> build file:</para>
328
329 <programlisting>
330 pluginEnvironment.SharedLibrary('newgen',
331 Split('''Opcodes/newgen.c
332 Opcodes/another_file_used_by_newgen.c
333 Opcodes/yet_another_file_used_by_newgen.c'''))</programlisting>
334 </listitem>
335
336 <listitem><para>Run the Csound 5 build in the regular way.</para></listitem>
337
338 </orderedlist>
339
340 </section>
341
342 <section>
343
344 <title><literal>OENTRY</literal> Reference</title>
345
346 <para>The <literal>OENTRY</literal> structure (see
347 <literal>H/csoundCore.h</literal>, <literal>Engine/entry1.c</literal>,
348 and <literal>Engine/rdorch.c</literal>) contains the following
349 public fields:</para>
350 <programlisting>
351 opname, dsblksiz, thread, outypes, intypes, iopadr, kopadr, aopadr</programlisting>
352
353 <variablelist>
354
355 <varlistentry><term><literal>dsblksiz</literal></term><listitem><para>There
356 are two types of opcodes, polymorphic and non-polymorphic. For
357 non-polymorphic opcodes, the <literal>dsblksiz</literal> flag specifies
358 the size of the opcode structure in bytes, and arguments are always
359 passed to the opcode at the same rate. Polymorphic opcodes can accept
360 arguments at different rates, and those arguments are actually
361 dispatched to other opcodes as determined by the
362 <literal>dsblksiz</literal> flag and the following naming convention
363 (note: the following list is not complete, see Engine/entry1.c
364 for all possible special dsblksiz codes):
365
366 <variablelist>
367
368 <varlistentry><term><literal>0xffff</literal></term><listitem><para>The
369 type of the first output argument determines which unit generator
370 function is actually called: <literal>XXX</literal> -&gt;
371 <literal>XXX.a</literal>, <literal>XXX.i</literal>, or
372 <literal>XXX.k</literal>.</para></listitem></varlistentry>
373
374 <varlistentry><term><literal>0xfffe</literal></term><listitem><para>The
375 types of the first two input arguments determine which unit generator
376 function is actually called: <literal>XXX</literal> -&gt;
377 <literal>XXX.aa</literal>, <literal>XXX.ak</literal>,
378 <literal>XXX.ka</literal>, or <literal>XXX.kk</literal>, as in the
379 <literal>oscil</literal> unit generator.</para></listitem></varlistentry>
380
381 <varlistentry><term><literal>0xfffd</literal></term><listitem><para>Refers
382 to one input argument of type a or k, as in the <literal>peak</literal>
383 unit generator.</para></listitem></varlistentry>
384
385 </variablelist>
386 </para>
387 </listitem>
388
389 </varlistentry>
390
391 <varlistentry><term><literal>thread</literal></term><listitem><para>Specifies
392 the rate(s) at which the unit generator's functions are called, as
393 follows:
394
395 <table>
396 <title>Rate at which ugens are called according to thread parameter</title>
397 <tgroup cols="2">
398 <colspec colname="thread"/>
399 <colspec colname="Description"/>
400 <tbody>
401 <row>
402 <entry><literal>0</literal></entry><entry><literal>i</literal>-rate <emphasis>or</emphasis> <literal>k</literal>-rate (B out only)</entry>
403 </row>
404 <row>
405 <entry><literal>1</literal></entry><entry><literal>i</literal>-rate</entry>
406 </row>
407 <row>
408 <entry><literal>2</literal></entry><entry><literal>k</literal>-rate</entry>
409 </row>
410 <row>
411 <entry><literal>3</literal></entry><entry><literal>i</literal>-rate <emphasis>and</emphasis> <literal>k</literal>-rate</entry>
412 </row>
413 <row>
414 <entry><literal>4</literal></entry><entry><literal>a</literal>-rate</entry>
415 </row>
416 <row>
417 <entry><literal>5</literal></entry><entry><literal>i</literal>-rate <emphasis>and</emphasis> <literal>a</literal>-rate</entry>
418 </row>
419 <row>
420 <entry><literal>7</literal></entry><entry><literal>i</literal>-rate <emphasis>and</emphasis> (<literal>k</literal>-rate <emphasis>or</emphasis> <literal>a</literal>-rate)</entry>
421 </row>
422 </tbody>
423 </tgroup>
424 </table>
425 </para>
426 </listitem>
427 </varlistentry>
428
429 <varlistentry><term><literal>outypes</literal></term><listitem><para>Lists
430 the return values of the unit generator functions, if any. The types
431 allowed are (note: the following list is not complete, see Engine/entry1.c
432 for all possible output types):
433
434 <table><title>List of out types for ugens</title>
435 <tgroup cols="2">
436 <colspec colname="Type"/>
437 <colspec colname="Description"/>
438 <tbody>
439 <row>
440 <entry><literal>i</literal></entry><entry><literal>i</literal>-rate scalar</entry>
441 </row>
442 <row>
443 <entry><literal>k</literal></entry><entry><literal>k</literal>-rate scalar</entry>
444 </row>
445 <row>
446 <entry><literal>a</literal></entry><entry>a-rate vector</entry>
447 </row>
448 <row>
449 <entry><literal>x</literal></entry><entry><literal>k</literal>-rate scalar or a-rate vector</entry>
450 </row>
451 <row>
452 <entry><literal>f</literal></entry><entry><literal>f</literal>-rate streaming pvoc fsig type</entry>
453 </row>
454 <row>
455 <entry><literal>m</literal></entry><entry>multiple a-rate output arguments</entry>
456 </row>
457 </tbody>
458 </tgroup>
459 </table>
460 </para>
461 </listitem>
462 </varlistentry>
463
464 <varlistentry><term><literal>intypes</literal></term><listitem><para>Lists
465 the arguments the unit generator functions take, if any. The types
466 allowed are (note: the following list is not complete, see Engine/entry1.c
467 for all possible input types):
468
469 <table><title>List of in types ofr ugens</title>
470 <tgroup cols="2">
471 <colspec colname="Type"/>
472 <colspec colname="Description"/>
473 <tbody>
474 <row>
475 <entry><literal>i</literal></entry><entry><literal>i</literal>-rate scalar</entry>
476 </row>
477 <row>
478 <entry><literal>k</literal></entry><entry><literal>k</literal>-rate scalar</entry>
479 </row>
480 <row>
481 <entry><literal>a</literal></entry><entry><literal>a</literal>-rate vector</entry>
482 </row>
483 <row>
484 <entry><literal>x</literal></entry><entry><literal>a</literal>-rate scalar or <literal>a</literal>-rate vector</entry>
485 </row>
486 <row>
487 <entry><literal>f</literal></entry><entry><literal>f</literal>-rate streaming pvoc fsig type</entry>
488 </row>
489 <row>
490 <entry><literal>S</literal></entry><entry>String</entry>
491 </row>
492 <row>
493 <entry><literal>B</literal></entry><entry></entry>
494 </row>
495 <row>
496 <entry><literal>l</literal></entry><entry></entry>
497 </row>
498 <row>
499 <entry><literal>m</literal></entry><entry>Begins an indefinite list of <literal>i</literal>-rate arguments
500 (any count)</entry>
501 </row>
502 <row>
503 <entry><literal>M</literal></entry><entry>Begins an indefinite list of arguments (any
504 rate, any count)</entry>
505 </row>
506 <row>
507 <entry><literal>N</literal></entry><entry>Begins an indefinite list of (optional <literal>a</literal>-, <literal>k</literal>-, <literal>i</literal>-, or <literal>S-rate</literal>)-rate arguments (any odd count)</entry>
508 </row>
509 <row>
510 <entry><literal>n</literal></entry><entry>Begins an indefinite list of <literal>i</literal>-rate arguments
511 (any odd count)</entry>
512 </row>
513 <row>
514 <entry><literal>O</literal></entry><entry>Optional k-rate, defaulting to 0</entry>
515 </row>
516 <row>
517 <entry><literal>o</literal></entry><entry>Optional i-rate, defaulting to 0</entry>
518 </row>
519 <row>
520 <entry><literal>p</literal></entry><entry>Optional i-rate, defaulting to 1</entry>
521 </row>
522 <row>
523 <entry><literal>q</literal></entry><entry>Optional i-rate, defaulting to 10</entry>
524 </row>
525 <row>
526 <entry><literal>V</literal></entry><entry>Optional k-rate, defaulting to 0.5</entry>
527 </row>
528 <row>
529 <entry><literal>v</literal></entry><entry>Optional i-rate, defaulting to 0.5</entry>
530 </row>
531 <row>
532 <entry><literal>j</literal></entry><entry>Optional i-rate, defaulting to -1</entry>
533 </row>
534 <row>
535 <entry><literal>h</literal></entry><entry>Optional i-rate, defaulting to 127</entry>
536 </row>
537 <row>
538 <entry><literal>y</literal></entry><entry>Begins an indefinite list of <literal>a</literal>-rate arguments
539 (any count)</entry>
540 </row>
541 <row>
542 <entry><literal>z</literal></entry><entry>Begins an indefinite list of <literal>k</literal>-rate arguments
543 (any count)</entry>
544 </row>
545 <row>
546 <entry><literal>Z</literal></entry><entry>Begins an indefinite list of alternating <literal>k</literal>-rate
547 and <literal>a</literal>-rate arguments (kaka...)
548 (any count)</entry>
549 </row>
550 </tbody>
551 </tgroup>
552 </table>
553 </para>
554 </listitem>
555 </varlistentry>
556
557 <varlistentry><term><literal>iopadr</literal></term><listitem><para>The
558 address of the unit generator function (of type <literal>int
559 (*SUBR)(CSOUND *, void *)</literal>) that is called at i-time, or NULL for no
560 function.</para></listitem></varlistentry>
561
562 <varlistentry><term><literal>kopadr</literal></term><listitem><para>The address of the unit
563 generator function (of type <literal>int (*SUBR)(CSOUND *, void *)</literal>) that is called at <literal>k</literal>-rate, or NULL for no function.</para></listitem></varlistentry>
564
565 <varlistentry><term><literal>aopadr</literal></term><listitem><para>The address of the unit
566 generator function (of type <literal>int (*SUBR)(CSOUND *, void *)</literal>) that is called at a-rate, or NULL for no function.</para></listitem></varlistentry>
567
568 </variablelist>
569
570 </section>
571
572 </section>
573
574 </chapter>
575