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
#undefNAME
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.
<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/207sr=44100ksmps=10nchnls=20dbfs=1#defineFREQ1#440##defineFREQ2#660#instr1; 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.#undefFREQ2iFreq=$FREQ1prints("Instrument %g uses %g Hz\n",p1,iFreq)a1=vco2(0.25,iFreq)out(a1,a1)endininstr2; FREQ2 is already undefined, so Csound keeps the #else branch.#ifdefFREQ2iFreq=$FREQ2#elseiFreq=$FREQ1#endifprints("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.i202i122e</CsScore></CsoundSynthesizer>
<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/207sr=44100ksmps=10nchnls=20dbfs=1#defineFREQ1#440##defineFREQ2#660#instr1; 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.#undefFREQ2iFreq=$FREQ1prints"Instrument %g uses %g Hz\n",p1,iFreqa1vco20.25,iFreqouta1,a1endininstr2; FREQ2 is already undefined, so Csound keeps the #else branch.#ifdefFREQ2iFreq=$FREQ2#elseiFreq=$FREQ1#endifprints"Instrument %g uses %g Hz\n",p1,iFreqa1vco20.25,iFreqouta1,a1endin</CsInstruments><CsScore>; Instrument 2 plays first, but both notes use 440 Hz.; Score order does not change the order of macro processing.i202i122e</CsScore></CsoundSynthesizer>