Use Non-Frames Version Previous Page Next Page
Instrument Control: Macros

#define, $NAME, #undef

    #define  NAME # replacement text #
    #define  NAME(a’ b’ c’) # replacement text #
    $NAME.
    #undef  NAME

Description

Macros are textual replacements which are made in the orchestra as it is being read. The macro system in Csound is a very simple one, and uses the characters # and $ to define and call macros. This can save typing, and can lead to a coherent structure and consistent style. This is similar to, but independent of, the macro system in the score language.

#define NAME - defines a simple macro. The name of the macro must begin with a letter and can consist of any combination of letters and numbers. Case is significant. This form is limiting, in that the variable names are fixed. More flexibility can be obtained by using a macro with arguments, described below.

#define NAME(a’ b’ c’) - defines a macro with arguments. This can be used in more complex situations. The name of the macro must begin with a letter and can consist of any combination of letters and numbers. Within the replacement text, the arguments can be substituted by the form: $A. In fact, the implementation defines the arguments as simple macros. There may be up to 5 arguments, and the names may be any choice of letters. Remember that case is significant in macro names.

$NAME. - calls a defined macro. To use a macro, the name is used following a $ character. The name is terminated by the first character which is neither a letter nor a number. If it is necessary for the name not to terminate with a space, a period, which will be ignored, can be used to terminate the name. The string, $NAME., is replaced by the replacement text from the definition. The replacement text can also include macro calls.

#undef NAME - undefines a macro name. If a macro is no longer required, it can be undefined with #undef NAME.

Initialization

# replacement text # - The replacement text is any character string (not containing a #) and can extend over mutliple lines. The replacement text is enclosed within the # characters, which ensure that additional characters are not inadvertently captured.

Performance

Some care is needed with textual replacement macros, as they can sometimes do strange things. They take no notice of any meaning, so spaces are significant. This is why, unlike the C programming language, the definition has the replacement text surrounded by # characters. Used carefully, this simple macro system is a powerful concept, but it can be abused.

Examples

Simple Macro

#define REVERB #ga = ga+a1
    out a1#

instr 1
  a1    oscil
  $REVERB.
endin

instr 2
  a1    repluck
  $REVERB.
endin

This will get expanded before compilation into:

instr 1
  a1    oscil
  ga = ga+a1
  out a1
endin

instr 2
  a1    repluck
  ga = ga+a1
  out a1
endin




Macro With Arguments

  #define REVERB(A) #ga = ga+$A.
    out $A.#
  instr 1
    a1    oscil
    $REVERB(a1)
  endin

  instr 2
    a2    repluck
    $REVERB(a2)
  endin

This will get expanded before compilation into:

  instr 1
    a1    oscil
    ga = ga+a1
    out a1
  endin

  instr 2
    a2    repluck
    ga = ga+a2
    out a2
  endin
  
  

Author

John ffitch
University of Bath/Codemist Ltd.
Bath, UK
April, 1998 (New in Csound version 3.48)


Use Non-Frames Version Previous Page Next Page
Instrument Control: Macros