Extending Csound

Adding Unit Generators

If the existing Csound unit generators do not suit your needs, it is relatively easy to extend Csound by writing new unit generators in C or C++. The translator, loader, and run-time monitor will treat your module just like any other provided you follow some conventions.

Historically, this has been done with builtin unit generators, that is, with code that is statically linked with the rest of the Csound executable.

Today, the preferred method is to create plugin unit generators. These are dynamic link libraries (DLLs) on Windows, and loadable modules (shared libraries that are dlopened) on Linux. Csound searches for and loads these plugins at run time. The advantage of this method, of course, is that plugins created by any developer at any time can be used with already existing versions of Csound.

Creating a Builtin Unit Generator

You need a structure defining the inputs, outputs and workspace, plus some initialization code and some perf-time code. Let's put an example of these in two new files, newgen.h and newgen.c. The examples given are for Csound 5. For earlier versions, all opcode functions omit the first parameter (ENVIRON *csound).

/* newgen.h  -  define a structure */

typedef struct
{
  OPDS h;  /* required header */
  float *result, *istrt, *incr, *itime, *icontin; /* addr outarg, inargs */
  float curval, vincr;  /* private dataspace */
  long countdown;  /* ditto */
} RMP;


/* newgen.c -  init and perf code */
/* Declares Csound structures and functions. */
#include "cs.h"
/* Declares RMP structure. */
#include "newgen.h"

void rampset (ENVIRON *csound, RMP * p)  /* at note initialization: */
{
  if (*p - icontin == 0.)
    p - curval = *p - istrt;  /* optionally get new start value */
  p - vincr = *p - incr / esr;  /* set s-rate increment per sec. */
  p - countdown = *p - itime * esr;  /* counter for itime seconds */
}

void ramp (ENVIRON *csound, RMP * p)  /* during note performance: */
{
  float *rsltp = p - result;  /* init an output array pointer */
  int nn = ksmps;  /* array size from orchestra */
  do
    {
      *rsltp++ = p - curval;  /* copy current value to output */
      if (--p - countdown = 0)  /* for the first itime seconds, */
        p - curval += p - vincr;  /* ramp the value */
    }
  while (--nn);
}

Now we add this module to the translator table in entry.c, under the opcode name rampt:

#include "newgen.h"

void rampset(ENVIRON*,RMP*), ramp(ENVIRON*,RMP*);

/*   opcode    dspace  thread    outarg    inargs      isub       ksub     asub    */

{ "rampt",  S(RMP),  5,        "a",      "iiio",     rampset,   NULL,    ramp  },

Finally you must relink Csound with the new module. Add the name of the C file to the libCsoundSources list in the SConstruct file:

libCsoundSources = Split('''
Engine/auxfd.c
...
OOps/newgen.c
...
Top/threads.c
''')

Run scons just as you would for any other Csound build, and the new module will be built into your Csound.

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:

ar rampt istart, islope, itime [, icontin]
      

istart -- beginning value of an audio-rate linear ramp. Optionally overridden by a continue flag.

islope -- slope of ramp, expressed as the y-interval change per second.

itime -- ramp time in seconds, after which the value is held for the remainder of the note.

icontin (optional) -- continue flag. If zero, ramping will proceed from input istart . If non-zero, ramping will proceed from the last value of the previous note. The default value is zero.

The file newgen.h includes a one-line list of output and input parameters. These are the ports through which the new generator will communicate with the other generators in an instrument. Communication is by address, not value, and this is a list of pointers to floats. There are no restrictions on names, but the input-output argument types are further defined by character strings in entry.c (inargs, outargs). Inarg types are commonly x, a, k, and i, in the normal Csound manual conventions; also available are o (optional, defaulting to 0), p (optional, defaulting to 1). Outarg types include a, k, i and s (asig or ksig). It is important that all listed argument names be assigned a corresponding argument type in entry.c. Also, i-type args are valid only at initialization time, and other-type args are available only at perf time. Subsequent lines in the RMP structure declare the work space needed to keep the code re-entrant. These enable the module to be used multiple times in multiple instrument copies while preserving all data.

The file newgen.c contains two subroutines, 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, k-rate signal generation, a-rate signal generation. A module normally requires two of these initialization, and either k-rate or a-rate subroutines which become inserted in various threaded lists of runnable tasks when an instrument is activated. The thread-types appear in entry.c in two forms: isub, ksub and asub names; and a threading index which is the sum of isub=1, ksub=2, asub=4. The code itself may reference global variables defined in cs.h and oload.c, the most useful of which are:

  extern  OPARMS  O ;          float   esr
      user-defined sampling rate   float   ekr
      user-defined control rate    float   ensmps
      user-defined ksmps           int     ksmps
      user-defined ksmps           int     nchnls
      user-defined nchnls          int     O.odebug
      command-line -v flag         int     O.msglevel
      command-line -m level        float   pi, twopi    obvious
      constants                    float   tpidsr    twopi / esr float
      sstrcod                      special code for string arguments
      

Function tables

To access stored function tables, special help is available. The newly defined structure should include a pointer

FUNC        *ftp;
        

initialized by the statement

ftp = ftpfind(p-ifuncno);
        

where float *ifuncno is an i-type input argument containing the ftable number. The stored table is then at ftp-ftable, and other data such as length, phase masks, cps-to-incr converters, are also accessed from this pointer. See the FUNC structure in cs.h, the ftfind() code in fgens.c, and the code for oscset() and koscil() in opcodes2.c.

Additional Space

Sometimes the space requirement of a module is too large to be part of a structure (upper limit 65535 bytes), 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

AUXCH      auxch;
        

in the defined structure (*p), then using the following style of code in the init module:

if (p-auxch.auxp == NULL)
  auxalloc(npoints * sizeof(float), &p-auxch);
        

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

char *auxp = p-auxch.auxp;
        

will find the allocated space for init-time and perf-time use. See the LINSEG structure in opcodes1.h and the code for lsgset() and klnseg() in opcodes1.c.

File Sharing

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

MEMFIL    *mfp;
        

in the defined structure (*p), then using the following style of code in the init module:

if (p-mfp == NULL)
  p-mfp = ldmemfile(filname);
        

where char *filname is a string name of the file requested. The data read will be found between

(char *)  p-mfp-beginp; and (char *) p-mfp-endp;
        

Loaded files do not belong to a particular instrument, but are automatically shared for multiple access. See the ADSYN structure in opcodes3.h and the code for adset() and adsyn() in opcodes3.c.

String arguments

To permit a quoted string input argument (float *ifilnam, say) in our defined structure (*p), assign it the argtype S in entry.c, include another member char *strarg in the structure, insert a line

TSTRARG( "rampt", RMP) \
        

in the file oload.h, and include the following code in the init module:

if (*p-ifilnam == sstrcod)
  strcpy(filename, unquote(p-strarg));
        

See the code for adset() in opcodes3.c, lprdset() in opcodes5.c, and pvset() in opcodes8.c.

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

MEMFIL    *mfp;
        

in the defined structure (*p), then using the following style of code in the init module:

if (p-mfp == NULL)
  p-mfp = ldmemfile(filname);
        

where char *filname is a string name of the file requested. The data read will be found between

(char *)  p-mfp-beginp; and (char *) p-mfp-endp;
        

Loaded files do not belong to a particular instrument, but are automatically shared for multiple access. See the ADSYN structure in opcodes3.h and the code for adset() and adsyn() in opcodes3.c.

Adding a Plugin Unit Generator

The procedure for creating a plugin unit generator is very similar to the procedure for creating a builtin. The actual unit generator code would normally be identical. The differences are as follows.

Again supposing that your unit generator is named newgen, perform the following steps:

  1. Write your newgen.c and newgen.h file as you would for a builtin unit generator. Put these files in the csound5/Opcodes directory.

  2. #include "csdl.h" in your unit generator sources. This causes the plugin development environment to emulate the development environment for builtin Csound unit generators.

  3. Add your OENTRY records and unit generator registration functions at the bottom of your C file. Example (but you can have as many unit generators in one plugin as you like):

    #define S sizeof
    
    static OENTRY localops[] = {
    {
    	{ "rampt",  S(RMP),  5, "a", "iiio",  (SUBR) rampset, (SUBR) NULL, (SUBR)ramp  },
    };
    
    /*
     * The following macro from csdl.h defines
     * the "opcode_size()" and "opcode_init()"
     * opcode registration functions for the localops table.
     */
    LINKAGE
    
  4. Add your plugin as a new target in the plugin opcodes section of the SConstruct build file:

    pluginEnvironment.SharedLibrary('newgen',
        Split('''Opcodes/newgen.c
        Opcodes/another_file_used_by_newgen.c
        Opcodes/yet_another_file_used_by_newgen.c'''))
    
  5. Run the Csound 5 build in the regular way.

OENTRY Reference

The OENTRY structure (see H/csoundCore.h, Engine/entry1.c, and Engine/rdorch.c) contains the following fields:

name, dspace, thread, outarg, inargs, isub, ksub, asub, dsub
dspace
There are two types of opcodes, polymorphic and non-polymorphic. For non-polymorphic opcodes, the dspace flag specifies the size of the opcode structure in bytes, and arguments are always passed to the opcode at the same rate. Polymorphic opcodes can accept arguments at different rates, and those arguments are actually dispatched to other opcodes as determined by the dspace flag and the following naming convention:
0xffff
The type of the first argument determines which unit generator function is actually called: XXX $\Longrightarrow$ XXX\_a, XXX\_i, or XXX\_k.
0xfffe
The types of the first two arguments determine which unit generator function is actually called: XXX $\Longrightarrow$ XXX\_aa, XXX\_ak, XXX\_ka, or XXX\_kk, as in the oscil unit generator.
0xfffd
Refers to one argument, but does not allow i type, as in the peak unit generator.
0xfffc
Similar to 0xfffe, but deals with division by zero - thus, allows a, k and i type arguments.
thread
Specifies the rate(s) at which the unit generator's functions are called, as follows:

Table 1. 

0i-rate or k-rate (B out only)
1i-rate
2k-rate
3i-rate and k-rate
4a-rate
5i-rate and a-rate
7i-rate and (k-rate or a-rate)
outargs
Lists the return values of the unit generator functions, if any. The types allowed are:

Table 2. 

ii-rate scalar
kk-rate scalar
aa-rate vector
xk-rate vector or a-rate vector
ff-rate streaming pvoc fsig type
mmultiple output arguments (1 to 4 allowed)
outargs
Lists the arguments the unit generator functions take, if any. The types allowed are:

Table 3. 

ii-rate scalar
kk-rate scalar
aa-rate vector
xk-rate vector or a-rate vector
ff-rate streaming pvoc fsig type
SString
B 
l 
mBegins an indefinite list of i-rate arguments (any count)
MBegins an indefinite list of arguments (any rate, any count)
nBegins an indefinite list of i-rate arguments (any odd count)
oOptional, defaulting to 0
pOptional, defaulting to 1
qOptional, defaulting to 10
vOptional, defaulting to 0.5
jOptional, defaulting to -1
hOptional, defaulting to 127
yBegins an indefinite list of a-rate arguments (any count)
zBegins an indefinite list of k-rate arguments (any count)
ZBegins an indefinite list of alternating k-rate and a-rate arguments (kaka...) (any count)
isub
The address of the unit generator function (of type int (*SUBR)(ENVIRON *, void *)) that is called at i-time, or null for no function.
ksub
The address of the unit generator function (of type int (*SUBR)(ENVIRON *, void *)) that is called at k-rate, or null for no function.
asub
The address of the unit generator function (of type int (*SUBR)(ENVIRON *, void *)) that is called at a-rate, or null for no function.
dsub
The address of the unit generator function (of type int (*SUBR)(ENVIRON *, void *)) that is called after performance, or null for no function.