| 1 | |
| 2 | <refentry id="define"> |
| 3 | <indexterm id="IndexDefine"><primary>#define</primary></indexterm> |
| 4 | <indexterm id="IndexDefineOrch"><primary>#define</primary><secondary>orchestra</secondary></indexterm> |
| 5 | <indexterm id="IndexDefineMacros"><primary>macros</primary><secondary>orchestra</secondary></indexterm> |
| 6 | <refentryinfo><title>Orchestra Syntax:Macros</title></refentryinfo> |
| 7 | <refmeta> |
| 8 | <refentrytitle>#define</refentrytitle> |
| 9 | </refmeta> |
| 10 | |
| 11 | <refnamediv> |
| 12 | <refname>#define</refname> |
| 13 | <refpurpose> |
| 14 | Defines a macro. |
| 15 | </refpurpose> |
| 16 | </refnamediv> |
| 17 | |
| 18 | <refsect1> |
| 19 | <title>Description</title> |
| 20 | <para> |
| 21 | Macros are textual replacements which are made in the orchestra as it is being read. The <link linkend="OrchMacros"><citetitle>orchestra macro system</citetitle></link> 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 <link linkend="ScoreMacros"><citetitle>macro system in the score language</citetitle></link>. |
| 22 | </para> |
| 23 | |
| 24 | <para> |
| 25 | <emphasis>#define NAME</emphasis> -- 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. |
| 26 | </para> |
| 27 | |
| 28 | <para> |
| 29 | <emphasis>#define NAME</emphasis>(<emphasis>a' b' c'</emphasis>) -- 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. The names may be any choice of letters. Remember that case is significant in macro names. |
| 30 | <note>In earlier versions of Csound there was a limit of 5 arguments, |
| 31 | but this restriction was removed in 2011; memory is the only constraint. |
| 32 | </note> |
| 33 | </para> |
| 34 | </refsect1> |
| 35 | |
| 36 | <refsect1> |
| 37 | <title>Syntax</title> |
| 38 | <synopsis><command>#define</command> NAME # replacement text #</synopsis> |
| 39 | |
| 40 | <synopsis><command>#define</command> NAME(a' b' c') # replacement text #</synopsis> |
| 41 | </refsect1> |
| 42 | |
| 43 | <refsect1> |
| 44 | <title>Initialization</title> |
| 45 | <para> |
| 46 | <emphasis># replacement text #</emphasis> -- The replacement text is any character string (not containing a #) and can extend over multiple lines. The replacement text is enclosed within the # characters, which ensure that additional characters are not inadvertently captured. |
| 47 | </para> |
| 48 | </refsect1> |
| 49 | |
| 50 | <refsect1> |
| 51 | <title>Performance</title> |
| 52 | <para> |
| 53 | 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. |
| 54 | </para> |
| 55 | </refsect1> |
| 56 | |
| 57 | <refsect1> |
| 58 | <title>Examples</title> |
| 59 | <para> |
| 60 | Here is a simple example of the defining a macro. It uses the file <ulink url="examples/define.csd"><citetitle>define.csd</citetitle></ulink>. |
| 61 | |
| 62 | <example> |
| 63 | <title>Simple example of the define macro.</title> |
| 64 | <para>See the sections <link linkend="UsingRealTime"><citetitle>Real-time Audio</citetitle></link> and <link linkend="CommandFlags"><citetitle>Command Line Flags</citetitle></link> for more information on using command line flags.</para> |
| 65 | <xi:include href="examples-xml/define.csd.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/> |
| 66 | </example> |
| 67 | |
| 68 | Its output should include lines like this: |
| 69 | <screen> |
| 70 | Macro definition for VOLUME |
| 71 | Macro definition for CPS |
| 72 | Macro definition for TABLE</screen> |
| 73 | </para> |
| 74 | |
| 75 | <para> |
| 76 | Here is an example of the defining a macro with arguments. It uses the file <ulink url="examples/define_args.csd"><citetitle>define_args.csd</citetitle></ulink>. |
| 77 | |
| 78 | <example> |
| 79 | <title>Example of the define macro with arguments.</title> |
| 80 | <xi:include href="examples-xml/define_args.csd.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/> |
| 81 | </example> |
| 82 | |
| 83 | Its output should include lines like this: |
| 84 | <screen> |
| 85 | Macro definition for OSCMACRO</screen> |
| 86 | </para> |
| 87 | </refsect1> |
| 88 | |
| 89 | <refsect1 id="opcodesDefineMathConstMacros"> |
| 90 | <title>Predefined Math Constant Macros</title> |
| 91 | <para>New in Csound 5.04 are predefined Math Constant Macros. The values |
| 92 | defined are those found in the C header math.h, and are automatically |
| 93 | defined when Csound starts and available for use in orchestras.</para> |
| 94 | |
| 95 | <informaltable frame="all"> |
| 96 | <tgroup cols="3" colsep="1" rowsep="1"> |
| 97 | <thead> |
| 98 | <row> |
| 99 | <entry>Macro</entry> |
| 100 | <entry>Value</entry> |
| 101 | <entry>Equivalent to</entry> |
| 102 | </row> |
| 103 | </thead> |
| 104 | |
| 105 | <tbody> |
| 106 | |
| 107 | <row> |
| 108 | <entry>$M_E</entry> |
| 109 | <entry>2.7182818284590452354</entry> |
| 110 | <entry>e</entry> |
| 111 | </row> |
| 112 | <row> |
| 113 | <entry>$M_LOG2E</entry> |
| 114 | <entry>1.4426950408889634074</entry> |
| 115 | <entry>log_2(e)</entry> |
| 116 | </row> |
| 117 | <row> |
| 118 | <entry>$M_LOG10E</entry> |
| 119 | <entry>0.43429448190325182765</entry> |
| 120 | <entry>log_10(e)</entry> |
| 121 | </row> |
| 122 | <row> |
| 123 | <entry>$M_LN2</entry> |
| 124 | <entry>0.69314718055994530942</entry> |
| 125 | <entry>log_e(2)</entry> |
| 126 | </row> |
| 127 | <row> |
| 128 | <entry>$M_LN10</entry> |
| 129 | <entry>2.30258509299404568402</entry> |
| 130 | <entry>log_e(10)</entry> |
| 131 | </row> |
| 132 | <row> |
| 133 | <entry>$M_PI</entry> |
| 134 | <entry>3.14159265358979323846</entry> |
| 135 | <entry>pi</entry> |
| 136 | </row> |
| 137 | <row> |
| 138 | <entry>$M_PI_2</entry> |
| 139 | <entry>1.57079632679489661923</entry> |
| 140 | <entry>pi/2</entry> |
| 141 | </row> |
| 142 | <row> |
| 143 | <entry>$M_PI_4</entry> |
| 144 | <entry>0.78539816339744830962</entry> |
| 145 | <entry>pi/4</entry> |
| 146 | </row> |
| 147 | <row> |
| 148 | <entry>$M_1_PI</entry> |
| 149 | <entry>0.31830988618379067154</entry> |
| 150 | <entry>1/pi</entry> |
| 151 | </row> |
| 152 | <row> |
| 153 | <entry>$M_2_PI</entry> |
| 154 | <entry>0.63661977236758134308</entry> |
| 155 | <entry>2/pi</entry> |
| 156 | </row> |
| 157 | <row> |
| 158 | <entry>$M_2_SQRTPI</entry> |
| 159 | <entry>1.12837916709551257390</entry> |
| 160 | <entry>2/sqrt(pi)</entry> |
| 161 | </row> |
| 162 | <row> |
| 163 | <entry>$M_SQRT2</entry> |
| 164 | <entry>1.41421356237309504880</entry> |
| 165 | <entry>sqrt(2)</entry> |
| 166 | </row> |
| 167 | <row> |
| 168 | <entry>$M_SQRT1_2</entry> |
| 169 | <entry>0.70710678118654752440</entry> |
| 170 | <entry>1/sqrt(2)</entry> |
| 171 | </row> |
| 172 | |
| 173 | </tbody> |
| 174 | </tgroup> |
| 175 | </informaltable> |
| 176 | </refsect1> |
| 177 | |
| 178 | <refsect1> |
| 179 | <title>See also</title> |
| 180 | <para><link linkend="OrchMacros"><citetitle>Orchestra Macros</citetitle></link></para> |
| 181 | </refsect1> |
| 182 | |
| 183 | <refsect1> |
| 184 | <title>Credits</title> |
| 185 | <para> |
| 186 | <simplelist> |
| 187 | <member>Author: &namejohn;</member> |
| 188 | <member>University of Bath/Codemist Ltd.</member> |
| 189 | <member>Bath, UK</member> |
| 190 | <member>April 1998</member> |
| 191 | </simplelist> |
| 192 | </para> |
| 193 | <para>Examples written by &namekevin;.</para> |
| 194 | |
| 195 | <para>New in Csound version 3.48</para> |
| 196 | </refsect1> |
| 197 | </refentry> |