Debian Codegraph @

cscore/main.xml

raw symbols SCIP
1
2 <section id="CscoreMain">
3 <title>Writing a Cscore Control Program</title>
4
5 <para>
6 The general format for a <emphasis>Cscore</emphasis> control program is:
7
8 <informalexample>
9 <programlisting>
10 #include &quot;cscore.h&quot;
11 void cscore(CSOUND *cs)
12 {
13 /* VARIABLE DECLARATIONS */
14 /* PROGRAM BODY */
15 }</programlisting>
16 </informalexample>
17 </para>
18
19 <para>
20 The <emphasis>include</emphasis> statement will define the event and list structures and all of the <emphasis>Cscore</emphasis> API functions for the program. The name of the user function needs to be <emphasis>cscore</emphasis> if it will be linked with the standard main program in <filename>cscormai.c</filename> or linked as the internal <emphasis>Cscore</emphasis> routine for a personal Csound executable. This <emphasis>cscore()</emphasis> function receives one argument from <filename>cscormai.c</filename> or Csound -- <emphasis>CSOUND *cs</emphasis> -- which is a pointer to a Csound object. The pointer <emphasis>cs</emphasis> must be passed as the first parameter to every <emphasis>Cscore</emphasis> API function that the program calls.
21 </para>
22
23 <para>
24 The following C program will read from a <emphasis>standard numeric score,</emphasis> up to (but not including) the first <emphasis>s</emphasis> or <emphasis>e statement</emphasis>, then write that data (unaltered) as output.
25 </para>
26
27 <para>
28 <informalexample>
29 <programlisting>
30 #include &quot;cscore.h&quot;
31 void cscore(CSOUND *cs)
32 {
33 EVLIST *a; /* a is allowed to point to an event list */
34 a = cscoreListGetSection(cs); /* read events in, return the list pointer */
35 cscoreListPut(cs, a); /* write these events out (unchanged) */
36 cscorePutString(cs, &quot;e&quot;); /* write the string e to output */
37 }</programlisting>
38 </informalexample>
39 </para>
40
41 <para>
42 After execution of <emphasis>cscoreListGetSection()</emphasis>, the variable <emphasis>a</emphasis> points to a list of event addresses, each of which points to a stored event. We have used that same pointer to enable another list function -- <emphasis>cscoreListPut()</emphasis> -- to access and write out all of the events that were read. If we now define another symbol <emphasis>e</emphasis> to be an event pointer, then the statement
43
44 <informalexample>
45 <programlisting>e = a-&gt;e[4];</programlisting>
46 </informalexample>
47
48 will set it to the contents of the 4th slot in the <emphasis>EVLIST</emphasis> structure, <emphasis>a</emphasis>. The contents is a pointer to an event, which is itself comprised of an <emphasis>array</emphasis> of parameter field values. Thus the term <emphasis>e-&gt;p[5]</emphasis> will mean the value of parameter field 5 of the 4th event in the <emphasis>EVLIST</emphasis> denoted by <emphasis>a</emphasis>. The program below will multiply the value of that <emphasis>pfield</emphasis> by 2 before writing it out.
49 </para>
50
51 <para>
52 <informalexample>
53 <programlisting>
54 #include &quot;cscore.h&quot;
55 void cscore(CSOUND *cs)
56 {
57 EVENT *e; /* a pointer to an event */
58 EVLIST *a;
59 a = cscoreListGetSection(cs); /* read a score as a list of events */
60 e = a-&gt;e[4]; /* point to event 4 in event list a */
61 e-&gt;p[5] *= 2; /* find pfield 5, multiply its value by 2 */
62 cscoreListPut(cs, a); /* write out the list of events */
63 cscorePutString(cs, &quot;e&quot;); /* add a &quot;score end&quot; statement */
64 }</programlisting>
65 </informalexample>
66 </para>
67
68 <para>
69 Now consider the following score, in which <emphasis>p[5]</emphasis> contains frequency in Hz.
70
71 <informalexample>
72 <programlisting>
73 <emphasis role="stamnt">f</emphasis> 1 0 257 10 1
74 <emphasis role="stamnt">f</emphasis> 2 0 257 7 0 300 1 212 .8
75 <emphasis role="stamnt">i</emphasis> 1 1 3 0 440 10000
76 <emphasis role="stamnt">i</emphasis> 1 4 3 0 256 10000
77 <emphasis role="stamnt">i</emphasis> 1 7 3 0 880 10000
78 <emphasis role="stamnt">e</emphasis></programlisting>
79 </informalexample>
80 </para>
81
82 <para>
83 If this score were given to the preceding main program, the resulting output would look like this:
84
85 <informalexample>
86 <programlisting>
87 <emphasis role="stamnt">f</emphasis> 1 0 257 10 1
88 <emphasis role="stamnt">f</emphasis> 2 0 257 7 0 300 1 212 .8
89 <emphasis role="stamnt">i</emphasis> 1 1 3 0 440 10000
90 <emphasis role="stamnt">i</emphasis> 1 4 3 0 512 10000 <emphasis role="comment">; p[5] has become 512 instead of 256.</emphasis>
91 <emphasis role="stamnt">i</emphasis> 1 7 3 0 880 10000
92 <emphasis role="stamnt">e</emphasis></programlisting>
93 </informalexample>
94 </para>
95
96 <para>
97 Note that the 4th event is in fact the second note of the score. So far we have not distinguished between notes and function table setup in a numeric score. Both can be classed as events. Also note that our 4th event has been stored in <emphasis>e[4]</emphasis> of the structure. For compatibility with Csound <emphasis>pfield</emphasis> notation, we will ignore <emphasis>p[0]</emphasis> and <emphasis>e[0]</emphasis> of the event and list structures, storing <emphasis>p1</emphasis> in <emphasis>p[1]</emphasis>, event 1 in <emphasis>e[1]</emphasis>, etc. The <emphasis>Cscore</emphasis> functions all adopt this convention.
98 </para>
99
100 <para>
101 As an extension to the above, we could decide to use the same pointers <emphasis>a</emphasis> and <emphasis>e</emphasis> to examine each of the events in the list. Note that <emphasis>e</emphasis> was not set to the numeral 4, but to the location of the 4th slot in the list. To inspect <emphasis>p5</emphasis> of the previous event in the list, we need only redefine <emphasis>e</emphasis> with the assignment
102
103 <informalexample>
104 <programlisting>e = a-&gt;e[3];</programlisting>
105 </informalexample>
106
107 and reference the 5th slot of the <emphasis>pfield</emphasis> array using the expression
108
109 <informalexample>
110 <programlisting>e-&gt;p[5]</programlisting>
111 </informalexample>
112 </para>
113
114 <para>
115 More generally, we can use an integer variable as an index to the array <emphasis>e[]</emphasis>, and access each event in sequence by using a loop and incrementing the index. The number of events stored in an <emphasis>EVLIST</emphasis> is contained in the <emphasis>nevents</emphasis> member of the struct.
116 </para>
117
118 <para>
119 <informalexample>
120 <programlisting>
121 int index; /* start with e[1] because e[0] is not used */
122 for (index = 1; index &lt;= a-&gt;nevents; index++)
123 {
124 e = a-&gt;e[index];
125 /* do something with e */
126 }</programlisting>
127 </informalexample>
128 </para>
129
130 <para>
131 The above example starts with <emphasis>e[1]</emphasis> and increases the index each time through the loop (<emphasis>index++</emphasis>) until it is greater than <emphasis>a-&gt;nevents</emphasis>, the index of the last event in the list. The statements inside the <emphasis>for</emphasis> loop do execute a final time when <emphasis>index</emphasis> equals <emphasis>a-&gt;nevents</emphasis>.
132 </para>
133
134 <para>
135 In the following program we will use the same input score. This time we will separate the <emphasis>ftable</emphasis> statements from the <emphasis>note</emphasis> statements. We will next write the three note-events stored in the list <emphasis>a</emphasis> to the output, then create a second score section consisting of the original pitch set and a transposed version of itself. This will bring about an octave doubling.
136 </para>
137
138 <para>
139 Here, our index to the array is <emphasis>n</emphasis> and we increment <emphasis>n</emphasis> as part of a <emphasis>for</emphasis> block which iterates <emphasis>nevents</emphasis> times, allowing one statement to act upon the same <emphasis>pfield</emphasis> of each successive event.
140 </para>
141
142 <para>
143 <informalexample>
144 <programlisting>
145 #include &quot;cscore.h&quot;
146 void cscore(CSOUND *cs)
147 {
148 EVENT *e, *f;
149 EVLIST *a, *b;
150 int n;
151
152 a = cscoreListGetSection(cs); /* read score into event list &quot;a&quot; */
153 b = cscoreListSeparateF(cs, a); /* separate f statements */
154 cscoreListPut(cs, b); /* write f statements out to score */
155 cscoreListFreeEvents(cs, b); /* and release the spaces used */
156 e = cscoreDefineEvent(cs, &quot;t 0 120&quot;); /* define event for tempo statement */
157 cscorePutEvent(cs, e); /* write tempo statement to score */
158 cscoreListPut(cs, a); /* write the notes */
159 cscorePutString(cs, &quot;s&quot;); /* section end */
160 cscorePutEvent(cs, e); /* write tempo statement again */
161 b = cscoreListCopyEvents(cs, a); /* make a copy of the notes in &quot;a&quot; */
162 for (n = 1; n &lt;= b-&gt;nevents; n++) /* iterate the following lines nevents times: */
163 {
164 f = b-&gt;e[n];
165 f-&gt;p[5] *= 0.5; /* transpose pitch down one octave */
166 }
167 a = cscoreListAppendList(cs, a, b); /* now add these notes to original pitches */
168 cscoreListPut(cs, a);
169 cscorePutString(cs, &quot;e&quot;);
170 }</programlisting>
171 </informalexample>
172 </para>
173
174 <para>
175 The output of this program is:
176 </para>
177
178 <para>
179 <informalexample>
180 <programlisting>
181 <emphasis role="stamnt">f</emphasis> 1 0 257 10 1
182 <emphasis role="stamnt">f</emphasis> 2 0 257 7 0 300 1 212 .8
183 <emphasis role="stamnt">t</emphasis> 0 120
184 <emphasis role="stamnt">i</emphasis> 1 1 3 0 440 10000
185 <emphasis role="stamnt">i</emphasis> 1 4 3 0 256 10000
186 <emphasis role="stamnt">i</emphasis> 1 7 3 0 880 10000
187 <emphasis role="stamnt">s</emphasis>
188 <emphasis role="stamnt">t</emphasis> 0 120
189 <emphasis role="stamnt">i</emphasis> 1 1 3 0 440 10000
190 <emphasis role="stamnt">i</emphasis> 1 4 3 0 256 10000
191 <emphasis role="stamnt">i</emphasis> 1 7 3 0 880 10000
192 <emphasis role="stamnt">i</emphasis> 1 1 3 0 220 10000
193 <emphasis role="stamnt">i</emphasis> 1 4 3 0 128 10000
194 <emphasis role="stamnt">i</emphasis> 1 7 3 0 440 10000
195 <emphasis role="stamnt">e</emphasis></programlisting>
196 </informalexample>
197 </para>
198
199 <para>
200 If the output is only being written to a file, then the unsorted order of the events is not a problem. The output is written to a file (or standard output) whenever the function <emphasis>cscoreListPut()</emphasis> is used. However, if this program were to be called during a Csound performance and the function <emphasis>cscoreListPlay()</emphasis> replaced <emphasis>cscoreListPut()</emphasis>, then the events would be sent to the orchestra instead of to a file and they should then be sorted beforehand by calling the function <emphasis>cscoreListSort()</emphasis>. The details of score output and playing when using <emphasis>Cscore</emphasis> from within Csound are described in the next section.
201 </para>
202
203 <para>
204 Next we extend the above program by using the <emphasis>for</emphasis> loop to look at <emphasis>p[5]</emphasis> and <emphasis>p[6]</emphasis>. In the original score <emphasis>p[6]</emphasis> denotes amplitude. To create a diminuendo in the added lower octave, which is independent from the original set of notes, a variable called <emphasis>dim</emphasis> will be used.
205 </para>
206
207 <para>
208 <informalexample>
209 <programlisting>
210 #include &quot;cscore.h&quot;
211 void cscore(CSOUND *cs)
212 {
213 EVENT *e, *f;
214 EVLIST *a, *b;
215 int n, dim; /* declare two integer variables */
216
217 a = cscoreListGetSection(cs);
218 b = cscoreListSeparateF(cs, a);
219 cscoreListPut(cs, b);
220 cscoreListFreeEvents(cs, b);
221 e = cscoreDefineEvent(cs, &quot;t 0 120&quot;);
222 cscorePutEvent(cs, e);
223 cscoreListPut(cs, a);
224 cscorePutString(cs, &quot;s&quot;);
225 cscorePutEvent(cs, e); /* write out another tempo statement */
226 b = cscoreListCopyEvents(cs, a);
227 dim = 0; /* initialize dim to 0 */
228 for (n = 1; n &lt;= b-&gt;nevents; n++)
229 {
230 f = b-&gt;e[n];
231 f-&gt;p[6] -= dim; /* subtract current value of dim */
232 f-&gt;p[5] *= 0.5; /* transpose pitch down one octave */
233 dim += 2000; /* increase dim for each note */
234 }
235 a = cscoreListAppendList(cs, a, b); /* now add these notes to original pitches */
236 cscoreListPut(cs, a);
237 cscorePutString(cs, &quot;e&quot;);
238 }</programlisting>
239 </informalexample>
240 </para>
241
242 <para>
243 Using the same input score again, the output from this program is:
244
245 <informalexample>
246 <programlisting>
247 <emphasis role="stamnt">f</emphasis> 1 0 257 10 1
248 <emphasis role="stamnt">f</emphasis> 2 0 257 7 0 300 1 212 .8
249 <emphasis role="stamnt">t</emphasis> 0 120
250 <emphasis role="stamnt">i</emphasis> 1 1 3 0 440 10000
251 <emphasis role="stamnt">i</emphasis> 1 4 3 0 256 10000
252 <emphasis role="stamnt">i</emphasis> 1 7 3 0 880 10000
253 <emphasis role="stamnt">s</emphasis>
254 <emphasis role="stamnt">t</emphasis> 0 120
255 <emphasis role="stamnt">i</emphasis> 1 1 3 0 440 10000 <emphasis role="comment">; Three original notes at</emphasis>
256 <emphasis role="stamnt">i</emphasis> 1 4 3 0 256 10000 <emphasis role="comment">; beats 1,4 and 7 with no dim.</emphasis>
257 <emphasis role="stamnt">i</emphasis> 1 7 3 0 880 10000
258 <emphasis role="stamnt">i</emphasis> 1 1 3 0 220 10000 <emphasis role="comment">; three notes transposed down one octave</emphasis>
259 <emphasis role="stamnt">i</emphasis> 1 4 3 0 128 8000 <emphasis role="comment">; also at beats 1,4 and 7 with dim.</emphasis>
260 <emphasis role="stamnt">i</emphasis> 1 7 3 0 440 6000
261 <emphasis role="stamnt">e</emphasis></programlisting>
262 </informalexample>
263 </para>
264
265 <para>
266 In the following program the same three-note sequence will be repeated at various time intervals. The starting time of each group is determined by the values of the array <emphasis>cue</emphasis>. This time the <emphasis>dim</emphasis> will occur for each group of notes rather than each note. Note the position of the statement which increments the variable <emphasis>dim</emphasis> outside the inner <emphasis>for</emphasis> loop.
267 </para>
268
269 <para>
270 <informalexample>
271 <programlisting>
272 #include &quot;cscore.h&quot;
273 int cue[3] = {0,10,17}; /* declare an array of 3 integers */
274 void cscore(CSOUND *cs)
275 {
276 EVENT *e, *f;
277 EVLIST *a, *b;
278 int n, dim, cuecount; /* declare new variable cuecount */
279
280 a = cscoreListGetSection(cs);
281 b = cscoreListSeparateF(cs, a);
282 cscoreListPut(cs, b);
283 cscoreListFreeEvents(cs, b);
284 e = cscoreDefineEvent(cs, &quot;t 0 120&quot;);
285 cscorePutEvent(cs, e);
286 dim = 0;
287 for (cuecount = 0; cuecount &lt;= 2; cuecount++) /* elements of cue are numbered 0, 1, 2 */
288 {
289 for (n = 1; n &lt;= a-&gt;nevents; n++)
290 {
291 f = a-&gt;e[n];
292 f-&gt;p[6] -= dim;
293 f-&gt;p[2] += cue[cuecount]; /* add values of cue */
294 }
295 printf(&quot;; diagnostic: cue = %d\n&quot;, cue[cuecount]);
296 dim += 2000;
297 cscoreListPut(cs, a);
298 }
299 cscorePutString(cs, &quot;e&quot;);
300 }</programlisting>
301 </informalexample>
302 </para>
303
304 <para>
305 Here the inner <emphasis>for</emphasis> loop looks at the events of list <emphasis>a</emphasis> (the notes) and the outer <emphasis>for</emphasis> loop looks at each <emphasis>repetition</emphasis> of the events of list <emphasis>a</emphasis> (the pitch group "cues"). This program also demonstrates a useful trouble-shooting device with the <emphasis>printf</emphasis> function. The <emphasis>semi-colon</emphasis> is first in the character string to produce a comment statement in the resulting score file. In this case the value of <emphasis>cue</emphasis> is being printed in the output to insure that the program is taking the proper <emphasis>array</emphasis> member at the proper time. When output data is wrong or error messages are encountered, the <emphasis>printf</emphasis> function can help to pinpoint the problem.
306 </para>
307
308 <para>
309 Using the same input file, the C program above will generate the following score. Can you determine why the last set of notes starts at the wrong time and how to correct the problem?
310 </para>
311
312 <para>
313 <informalexample>
314 <programlisting>
315 <emphasis role="stamnt">f</emphasis> 1 0 257 10 1
316 <emphasis role="stamnt">f</emphasis> 2 0 257 7 0 300 1 212 .8
317 <emphasis role="stamnt">t</emphasis> 0 120
318 <emphasis role="comment">; diagnostic: cue = 0</emphasis>
319 <emphasis role="stamnt">i</emphasis> 1 1 3 0 440 10000
320 <emphasis role="stamnt">i</emphasis> 1 4 3 0 256 10000
321 <emphasis role="stamnt">i</emphasis> 1 7 3 0 880 10000
322 <emphasis role="comment">; diagnostic: cue = 10</emphasis>
323 <emphasis role="stamnt">i</emphasis> 1 11 3 0 440 8000
324 <emphasis role="stamnt">i</emphasis> 1 14 3 0 256 8000
325 <emphasis role="stamnt">i</emphasis> 1 17 3 0 880 8000
326 <emphasis role="comment">; diagnostic: cue = 17</emphasis>
327 <emphasis role="stamnt">i</emphasis> 1 28 3 0 440 4000
328 <emphasis role="stamnt">i</emphasis> 1 31 3 0 256 4000
329 <emphasis role="stamnt">i</emphasis> 1 34 3 0 880 4000
330 <emphasis role="stamnt">e</emphasis></programlisting>
331 </informalexample>
332 </para>
333 </section>