Macros

Macros can be used in Csound, both in the orchestra and in the score, to define text that will be inserted into your code by the preprocessor, before the code is compiled. This functionality is well explained in The Canonical Csound Reference Manual. Here is the example used in the manual to illustrate the #define command:


  #define VOLUME #5000#
  #define FREQ #440#
  #define TABLE #1#

  instr 1
  ; This will be expanded to “a1 oscil 5000, 440, 1”.
  a1 oscil $VOLUME, $FREQ, $TABLE

A macro is defined using a #define statement, followed by the name of the macro, followed by the text content of the macro surrounded by # symbols. To refer to the macro in your orchestra, use its name preceded by a dollar sign. When the preprocessor is preparing your .csd file for performance or rendering, it simply replaces the dollar sign and the macro name with whatever text you have specified in the macro definition.

Macros can be defined with up to five variable arguments. The variable arguments are separated by apostrophes (). The prototype for a macro with arguments looks like this:


  #define NAME(a’b’c’) #replacement text#

Here is the example code given in the manual:


  #define OSCMACRO(VOLUME’FREQ’TABLE) #oscil $VOLUME, $FREQ, $TABLE#

  instr 1
  ; This will be expanded to “a1 oscil 5000, 440, 1”.
  a1 $OSCMACRO(5000’440’1)

Note the use of dollar signs within the definition to refer to the places where the variables will be inserted when the macro is being “unpacked” by the Csound compiler. The example above is rather trivial, in that it won’t actually save you any typing. The point of the example is to illustrate the syntax in a clear way.

What the manual doesn’t mention explicitly is that the values used as the variable arguments needn’t be constants. In the example above, if the output of an LFO has been created using the symbol klfo, then this code will work:


  a1 $OSCMACRO(5000’klfo’1)

We’ll look further at the uses of macros in Chapter 8, “Writing a Csound Score.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.115.154