Skip to content

#undef

Un-defines a macro.

Macros are textual replacements which are made in the orchestra as it is being read. The orchestra 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.

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

Syntax

#undef NAME

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

This example removes FREQ2 inside instrument 1. Macro processing follows the order of the orchestra text and happens before any notes play. The #undef therefore affects the code that follows it, including instrument 2.

Instrument 2 plays first in the score, but its #ifdef FREQ2 selects the #else branch. Both instruments use 440 Hz. Comment out #undef FREQ2 and run the example again to hear instrument 2 at 660 Hz, followed by instrument 1 at 440 Hz.

The file undef-modern.csd uses modern syntax.

Example of #undef.
<CsoundSynthesizer>
<CsOptions>
-odac
; For file output, replace -odac with -o undef.wav -W.
</CsOptions>
<CsInstruments>
; Adapted from the example by tgrey1 in csound/manual issue #207.
; https://github.com/csound/manual/issues/207
sr = 44100
ksmps = 10
nchnls = 2
0dbfs = 1

#define FREQ1 #440#
#define FREQ2 #660#

instr 1
  ; Csound reads this before it reads instrument 2.
  ; It removes FREQ2 during preprocessing, before any notes play.
  ; Comment out this line to make instrument 2 use 660 Hz instead.
#undef FREQ2
  iFreq = $FREQ1
  prints("Instrument %g uses %g Hz\n", p1, iFreq)
  a1 = vco2(0.25, iFreq)
  out(a1, a1)
endin

instr 2
  ; FREQ2 is already undefined, so Csound keeps the #else branch.
#ifdef FREQ2
  iFreq = $FREQ2
#else
  iFreq = $FREQ1
#endif
  prints("Instrument %g uses %g Hz\n", p1, iFreq)
  a1 = vco2(0.25, iFreq)
  out(a1, a1)
endin
</CsInstruments>
<CsScore>
; Instrument 2 plays first, but both notes use 440 Hz.
; Score order does not change the order of macro processing.
i2 0 2
i1 2 2
e
</CsScore>
</CsoundSynthesizer>

The file undef.csd uses classic syntax.

Example of #undef.
<CsoundSynthesizer>
<CsOptions>
-odac
; For file output, replace -odac with -o undef.wav -W.
</CsOptions>
<CsInstruments>
; Adapted from the example by tgrey1 in csound/manual issue #207.
; https://github.com/csound/manual/issues/207
sr = 44100
ksmps = 10
nchnls = 2
0dbfs = 1

#define FREQ1 #440#
#define FREQ2 #660#

instr 1
  ; Csound reads this before it reads instrument 2.
  ; It removes FREQ2 during preprocessing, before any notes play.
  ; Comment out this line to make instrument 2 use 660 Hz instead.
#undef FREQ2
  iFreq = $FREQ1
  prints "Instrument %g uses %g Hz\n", p1, iFreq
  a1 vco2 0.25, iFreq
  out a1, a1
endin

instr 2
  ; FREQ2 is already undefined, so Csound keeps the #else branch.
#ifdef FREQ2
  iFreq = $FREQ2
#else
  iFreq = $FREQ1
#endif
  prints "Instrument %g uses %g Hz\n", p1, iFreq
  a1 vco2 0.25, iFreq
  out a1, a1
endin
</CsInstruments>
<CsScore>
; Instrument 2 plays first, but both notes use 440 Hz.
; Score order does not change the order of macro processing.
i2 0 2
i1 2 2
e
</CsScore>
</CsoundSynthesizer>

The console output includes these lines.

Instrument 2 uses 440 Hz
Instrument 1 uses 440 Hz

See also

#define, #ifdef, Orchestra Macros

Credits

Author: John ffitch
University of Bath/Codemist Ltd.
Bath, UK
April 1998

New in Csound version 3.48