#ifdef — Conditional reading of code.


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.

If a macro is defined then #ifdef can incorporate text into an orchestra upto the next #end.


#ifdef NAME


Note that the #ifdef can be nested, like in the C preprocessor language.


Here is a simple example of the conditional.

Example 16. Simple example of the #ifdef form.

#define debug ##
     instr 1
#ifdef debug
     print "calling oscil"
     a1   oscil 32000,440,1
     out  a1

Here is another example of the #ifdef conditional. It uses the file ifdef.csd.

Example 17. Detailed example of the #ifdef form.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

; Select audio/midi flags here according to platform
-odac     ;;;realtime audio out
;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o oscil.wav -W ;;; for file output any platform
sr = 44100
ksmps = 10
nchnls = 2
0dbfs = 1

; Determines which instrument outputs debug statements with defines:
; Change which one is commented out to alter behavior before execution
#define debug1 ##
; #define debug2 ##

 instr 1
 	iFreq = p4
	; Outputs text if debug1 is defined
	; This one should print by default
	#ifdef debug1
	     prints "instr 1 debug called\n"
     a1   vco2 .25, iFreq
     outs  a1, a1

 instr 2
 	iFreq = p4
	; Outputs text if debug2 is defined
	; This one should not print by default
	#ifdef debug2
	     prints "instr 2 debug called\n"
     a1   vco2 .25, iFreq
     outs  a1, a1

i1 0 2 440
i2 0 2 660

See Also

#define, $NAME, #ifndef, #include, #undef


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

New in Csound5 (and 4.23f13)