| 1 | |
| 2 | <section id="ScoreMacros"> |
| 3 | <title>Score Macros</title> |
| 4 | |
| 5 | <simplesect> |
| 6 | <title>Description</title> |
| 7 | <para> |
| 8 | Macros are textual replacements which are made in the score as it is being presented to the system. The macro system in Csound is a very simple one, and uses the characters # and $ to define and call macros. This can can allow for simpler score writing, and provide an elementary alternative to full score generation systems.The score macro system is similar to, but independent of, the macro system in the orchestra language. |
| 9 | |
| 10 | <indexterm id="IndexScoreMacros"><primary>score</primary><secondary>macros</secondary></indexterm> |
| 11 | <indexterm id="IndexMacrosScore"><primary>macros</primary><secondary>score</secondary></indexterm> |
| 12 | </para> |
| 13 | |
| 14 | <para> |
| 15 | <emphasis>#define</emphasis> 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. |
| 16 | |
| 17 | <indexterm id="IndexDefineScore"><primary>#define</primary><secondary>score</secondary></indexterm> |
| 18 | <indexterm id="IndexNameScore"><primary>$name</primary><secondary>score</secondary></indexterm> |
| 19 | <indexterm id="IndexUndefScore"><primary>#undef</primary><secondary>score</secondary></indexterm> |
| 20 | </para> |
| 21 | |
| 22 | <para> |
| 23 | <emphasis>#define</emphasis> NAME(<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. There may be up to 5 arguments, and the names may be any choice of letters. Remember that case is significant in macro names. |
| 24 | </para> |
| 25 | |
| 26 | <para> |
| 27 | <emphasis>$NAME.</emphasis> -- calls a defined macro. To |
| 28 | use a macro, the name is used following a $ character. |
| 29 | The name is terminated by the first character which is neither a |
| 30 | letter nor a number. If it is necessary for the name to be |
| 31 | immediately followed by a leter or digit, a period, which will |
| 32 | be ignored, can be used to terminate the name. The string, |
| 33 | <emphasis>$NAME</emphasis>., is replaced by the |
| 34 | replacement text from the definition. The replacement text can |
| 35 | also include macro calls. |
| 36 | </para> |
| 37 | |
| 38 | <para> |
| 39 | <emphasis>#undef</emphasis> NAME -- undefines a macro name. If a macro is no longer required, it can be undefined with <emphasis>#undef</emphasis> NAME. |
| 40 | </para> |
| 41 | </simplesect> |
| 42 | |
| 43 | |
| 44 | <simplesect> |
| 45 | <title>Syntax</title> |
| 46 | <synopsis><command>#define</command> NAME # replacement text #</synopsis> |
| 47 | |
| 48 | <synopsis><command>#define</command> NAME(a' b' c') # replacement text #</synopsis> |
| 49 | |
| 50 | <synopsis><command>$NAME.</command></synopsis> |
| 51 | |
| 52 | <synopsis><command>#undef</command> NAME</synopsis> |
| 53 | </simplesect> |
| 54 | |
| 55 | |
| 56 | <simplesect> |
| 57 | <title>Initialization</title> |
| 58 | <para> |
| 59 | <emphasis># replacement text #</emphasis> -- 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. |
| 60 | </para> |
| 61 | </simplesect> |
| 62 | |
| 63 | <simplesect> |
| 64 | <title>Performance</title> |
| 65 | <para> |
| 66 | 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. |
| 67 | </para> |
| 68 | |
| 69 | <formalpara> |
| 70 | <title>Another Use For Macros</title> |
| 71 | <para> |
| 72 | When writing a complex score it is sometimes all too easy to forget to what the various instrument numbers refer. One can use macros to give names to the numbers. For example |
| 73 | <informalexample> |
| 74 | <programlisting> |
| 75 | <emphasis>#define</emphasis> Flute #i1# |
| 76 | <emphasis>#define</emphasis> Whoop #i2# |
| 77 | |
| 78 | <emphasis>$Flute.</emphasis> 0 10 4000 440 |
| 79 | <emphasis>$Whoop.</emphasis> 5 1</programlisting> |
| 80 | </informalexample> |
| 81 | </para> |
| 82 | </formalpara> |
| 83 | </simplesect> |
| 84 | |
| 85 | <simplesect> |
| 86 | <title>Examples</title> |
| 87 | <para> |
| 88 | <example> |
| 89 | <title>Simple Macro</title> |
| 90 | <para> |
| 91 | A note-event has a set of p-fields which are repeated: |
| 92 | <programlisting> |
| 93 | <emphasis>#define</emphasis> ARGS # 1.01 2.33 138# |
| 94 | <emphasis role="stamnt">i</emphasis>1 0 1 8.00 1000 $ARGS |
| 95 | <emphasis role="stamnt">i</emphasis>1 0 1 8.01 1500 $ARGS |
| 96 | <emphasis role="stamnt">i</emphasis>1 0 1 8.02 1200 $ARGS |
| 97 | <emphasis role="stamnt">i</emphasis>1 0 1 8.03 1000 $ARGS</programlisting> |
| 98 | |
| 99 | This will get expanded before sorting into: |
| 100 | <programlisting> |
| 101 | <emphasis role="stamnt">i</emphasis>1 0 1 8.00 1000 1.01 2.33 138 |
| 102 | <emphasis role="stamnt">i</emphasis>1 0 1 8.01 1500 1.01 2.33 138 |
| 103 | <emphasis role="stamnt">i</emphasis>1 0 1 8.02 1200 1.01 2.33 138 |
| 104 | <emphasis role="stamnt">i</emphasis>1 0 1 8.03 1000 1.01 2.33 138</programlisting> |
| 105 | </para> |
| 106 | </example> |
| 107 | </para> |
| 108 | |
| 109 | <para> |
| 110 | This can save typing, and is makes revisions easier. If there were two sets of p-fields one could have a second macro (there is no real limit on the number of macros one can define). |
| 111 | <informalexample> |
| 112 | <programlisting> |
| 113 | <emphasis>#define</emphasis> ARGS1 # 1.01 2.33 138# |
| 114 | <emphasis>#define</emphasis> ARGS2 # 1.41 10.33 1.00# |
| 115 | <emphasis role="stamnt">i</emphasis>1 0 1 8.00 1000 $ARGS1 |
| 116 | <emphasis role="stamnt">i</emphasis>1 0 1 8.01 1500 $ARGS2 |
| 117 | <emphasis role="stamnt">i</emphasis>1 0 1 8.02 1200 $ARGS1 |
| 118 | <emphasis role="stamnt">i</emphasis>1 0 1 8.03 1000 $ARGS2</programlisting> |
| 119 | </informalexample> |
| 120 | </para> |
| 121 | |
| 122 | <para> |
| 123 | <example> |
| 124 | <title>Macros with arguments</title> |
| 125 | <para> |
| 126 | <informalexample> |
| 127 | <programlisting> |
| 128 | <emphasis>#define</emphasis> ARG(A) # 2.345 1.03 $A 234.9# |
| 129 | <emphasis role="stamnt">i</emphasis>1 0 1 8.00 1000 $ARG(2.0) |
| 130 | <emphasis role="stamnt">i</emphasis>1 + 1 8.01 1200 $ARG(3.0)</programlisting> |
| 131 | </informalexample> |
| 132 | |
| 133 | which expands to |
| 134 | |
| 135 | <informalexample> |
| 136 | <programlisting> |
| 137 | <emphasis role="stamnt">i</emphasis>1 0 1 8.00 1000 2.345 1.03 2.0 234.9 |
| 138 | <emphasis role="stamnt">i</emphasis>1 + 1 8.01 1200 2.345 1.03 3.0 234.9</programlisting> |
| 139 | </informalexample> |
| 140 | </para> |
| 141 | </example> |
| 142 | </para> |
| 143 | </simplesect> |
| 144 | |
| 145 | <simplesect> |
| 146 | <title>Credits</title> |
| 147 | <para>Author: &namejohn;</para> |
| 148 | <para>University of Bath/Codemist Ltd.</para> |
| 149 | <para>Bath, UK</para> |
| 150 | <para>April, 1998 (New in Csound version 3.48)</para> |
| 151 | </simplesect> |
| 152 | </section> |