Debian Codegraph @

doc/part15.html

raw
1 <!DOCTYPE html>
2 <html><head><title>Xmusic and Algorithmic Composition</title>
3 <link rel="stylesheet" type="text/css" href="nyquiststyle.css">
4 <link rel="icon" href="nyquist-icon.png" />
5 <link rel="shortcut icon" href="nyquist-icon.png" />
6 </head>
7 <body bgcolor="ffffff">
8 <a href = "part14.html">Previous Section</a> | <a href = "part16.html">Next Section</a> | <a href = "title.html#toc">Table of Contents</a> | <a href = "indx.html">Index</a> | <a href = "title.html">Title Page</a>
9 <hr>
10 <a name = "157"><h2>Xmusic and Algorithmic Composition</h2></a><a name="index1163"></a><a name="index1164"></a>
11 <p>Several Nyquist libraries offer support for algorithmic composition. Xmusic
12 is a library for generating sequences and patterns of data. Included in Xmusic
13 are:
14 <ul>
15 <li>
16 pattern objects, used to generate interesting sequences of parameter values,</li>
17 <li>random number generators, used to create random sequences from
18 different distributions (contained in the <code>distributions.lsp</code>
19 library) </li>
20 <li>a standard representation for "note lists" called <i>scores</i> and
21 functions to render them as sounds, </li>
22 <li>the <code>score-gen</code> macro which helps to generate scores from patterns, </li>
23 <li>score manipulation functions to select, transform, shift, and perform
24 other operations on scores.
25 </li></ul></p>
26 <a name = "158"><h3>Xmusic Basics</h3></a>
27 <p>Xmusic is inspired by and based on Common Music by Rick Taube.
28 Common Music supports MIDI and
29 various other synthesis languages and includes a graphical interface, some
30 visualization tools, and many other features. Common Music runs in Common
31 Lisp and Scheme, but not XLISP (the base language for Nyquist). </p>
32
33 <p>The Xmusic libraries in Nyquist offer an interesting subset of the
34 tools in Common Music. One important feature of Xmusic is that it is
35 integrated with all of the Nyquist synthesis functions, so you can use
36 Xmusic patterns and scores to control fine details of sound
37 synthesis. </p>
38 <a name = "159"><h3>Xmusic Patterns</h3></a><a name="index1165"></a>
39 <p>Xmusic patterns are objects that generate data streams. For example,
40 the <code>cycle-class</code> of objects generate cyclical patterns such as
41 "1 2 3 1 2 3 1 2 3 ...", or "1 2 3 4 3 2 1 2 3 4 ...". Patterns can
42 be used to specify pitch sequences, rhythm, loudness, and other parameters.</p>
43
44 <p>Xmusic functions are automatically loaded when you start Nyquist.
45 To use a pattern object, you first create the pattern, e.g.
46 </p>
47 <pre>
48 set pitch-source = make-cycle(list(c4, d4, e4, f4))
49 </pre>
50
51 <p>
52 <a name="index1166"></a><a name="index1167"></a>
53 In this example, <code>pitch-source</code> is an object of class
54 <code>cycle-class</code> which inherits from <code>pattern-class</code>. </p>
55 <small>
56 Because SAL is not an object-oriented language, these classes and
57 their methods are not directly accessible from SAL.
58 Instead, Xmusic defines a functional interface, e.g. <code>make-cycle</code>
59 creates an instance of <code>cycle-class</code>, and the <code>next</code>
60 function, introduced below, retrieves the next value from any instance
61 of <code>pattern-class</code>. Using LISP syntax, you can have full access
62 to the methods of all objects (see the source code in <code>xm.lsp</code>,
63 but the functional interface described here should be sufficient and
64 it is recommended that you limit your access to this interface.
65 </small>
66
67 <p>After creating the pattern, you can access it repeatedly
68 with <code>next</code><a name="index1168"></a> to generate data, e.g.
69 </p>
70 <pre>
71 play seqrep(i, 13, pluck(next(pitch-source), 0.2))
72 </pre>
73
74 <p>
75 This will create a sequence of notes with the following pitches: c, d,
76 e, f, c, d, e, f, c, d, e, f, c. If you evaluate this again, the
77 pitch sequence will continue, starting on "d".</p>
78
79 <p>It is very important not to confuse the creation of a sequence with
80 its access. Consider this example:
81 </p>
82 <pre>
83 play seqrep(i, 13,
84 pluck(next(make-cycle(list(c4, d4, e4, f4))), 0.2))
85 </pre>
86
87 <p>
88 This looks very much like the previous example, but it only repeats notes
89 on middle-C. The reason is that every time <code>pluck</code> is evaluated,
90 <code>make-cycle</code> is called and creates a new pattern object. After the
91 first item of the pattern is extracted with <code>next</code>, the cycle is not
92 used again, and no other items are generated.</p>
93
94 <p>To summarize this important point, there are two steps to using a pattern.
95 First, the pattern is created and stored in a
96 variable. Second, the pattern is accessed (multiple
97 times) using <code>next</code>.</p>
98 <dl>
99 <dt>
100 <code>next(<a name="index1169"></a><i>pattern-object</i> [, #t])</code> [SAL]<br>
101
102 <code>(next <i>pattern-object</i> [t])</code> [LISP]</dt>
103 <dd>Returns the next element from a pattern generator object. If the optional second argument is true (default value is false), then an entire period is returned as a list.
104 </dd></dl> <a name = "160"><h4>Nested Patterns</h4></a>
105 <p>Patterns can be nested, that is, you can write patterns of patterns.
106 In general, the <code>next</code> function does not return patterns. Instead,
107 if the next item in a pattern is a (nested) pattern, <code>next</code> recursively
108 gets the next item of the nested pattern.</p>
109
110 <p>While you might expect that each call to <code>next</code> would advance the
111 top-level pattern to the next item, and descend recursively if necessary
112 to the inner-most nesting level, this is not how <code>next</code> works. Instead,
113 <code>next</code> remembers the last top-level item, and if it was a pattern,
114 <code>next</code> continues to generate items from that same inner pattern
115 until the end of the inner pattern's <i>period</i> is reached. The next
116 paragraph explains the concept of the <i>period</i>.</p>
117 <a name = "161"><h4>Periods</h4></a>
118 <p>The data returned by a pattern object is structured into logical groups
119 called <i>periods</i>. You can get an entire period (as a list) by calling
120 <code>next(<i>pattern</i>, t)</code><a name="index1170"></a>. For example:
121 </p>
122 <pre>
123 set pitch-source = make-cycle(list(c4, d4, e4, f4))
124 print next(pitch-source, t)
125 </pre>
126
127 <p>
128 This prints the list <code>(60 62 64 65)</code>, which is one period
129 of the cycle.</p>
130
131 <p>You can also get explicit markers that
132 delineate periods by calling <code>send(<i>pattern</i>, :next)</code>. In this
133 case, the value returned is either the next item of the pattern, or the
134 symbol <code>+eop+</code> if the end of a period has been reached. What
135 determines a period? This is up to the specific pattern class, so see the
136 documentation for specifics. You can override the &ldquo;natural&rdquo; period
137 using the keyword <code>for:</code>, e.g.
138 </p>
139 <pre>
140 set pitch-source = make-cycle(list(c4, d4, e4, f4), for: 3)
141 print next(pitch-source, t)
142 print next(pitch-source, t)
143 </pre>
144
145 <p>
146 This prints the lists <code>(60 62 64) (60 62 64)</code>. Notice that
147 the cycle starts from the beginning after only 3 items and the
148 fourth is never reached in this case. The <code>for:</code> parameter
149 <i>could itself be a pattern</i>, in which case the a new cycle length would
150 be computed at the beginning of every cycle.</p>
151
152 <p>A variation on this restructures the stream of items
153 into groups of 3:
154 </p>
155 <pre>
156 set pitch-source = make-length(make-cycle(list(c4, d4, e4 f4)), 3)
157 print next(pitch-source, t)
158 print next(pitch-source, t)
159 </pre>
160
161 <p>
162 This prints the lists <code>(60 62 64) (65 60 62)</code>. Note that in
163 this case the cycle pattern is producing default cycles of length 4
164 because there is no <code>for:</code> specification, and these are regrouped
165 by the length pattern.</p>
166
167 <p>Nested patterns are probably easier to understand by example than by
168 specification. Here is a simple nested pattern of cycles:
169 </p>
170 <pre>
171 set cycle-1 = make-cycle({a b c})
172 set cycle-2 = make-cycle({x y z})
173 set cycle-3 = make-cycle(list(cycle-1, cycle-2))
174 loop repeat 9 exec format(t, "~A ", next(cycle-3)) end
175 </pre>
176
177 <p>
178 This will print "A B C X Y Z A B C". Notice that the inner-most
179 cycles <code>cycle-1</code> and <code>cycle-2</code> generate a period of items
180 before the top-level <code>cycle-3</code> advances to the next pattern.</p>
181 <a name = "162"><h4>General Parameters for Creating Pattern objects</h4></a>
182 <p>Before describing specific pattern classes, there are several optional
183 parameters that apply in the creating of any pattern object. These are:
184 <dl>
185 <dt>
186 <code>for:</code></dt>
187 <dd>The length of a period. This overrides the default
188 by providing a numerical length. The value of this optional
189 parameter may be a pattern that generates a sequence of integers
190 that determine the length of each successive period. A period
191 length may not be negative, but it may be zero.</p>
192 <dt><code>name:</code></dt>
193 <dd>A pattern object may be given a name. This is useful
194 if the <code>trace:</code> option is used.<br><br>
195 <dt><code>trace:</code></dt>
196 <dd>If non-null, this optional parameter causes information
197 about the pattern to be printed each time an item is generated
198 from the pattern.
199 </dd></dl>
200 The built-in pattern classes are described in the following section.<a name = "163"><h4>cycle</h4></a>
201 <p>The <code>cycle-class</code> iterates repeatedly through a list of items.
202 For example, two periods of <code>make-cycle({a b c})</code> would be
203 <code>(A B C) (A B C)</code>. </p>
204 <dl>
205 <dt>
206 <code>make-cycle(<a name="index1171"></a><a name="index1172"></a><a name="index1173"></a><i>items</i>, for: <i>for</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
207
208 <code>(make-cycle <i>items</i> :for <i>for</i> :name <i>name</i>
209 :trace <i>trace</i>)</code> [LISP]</dt>
210 <dd>Make a cycle
211 pattern that iterates over <i>items</i>. The default period length is the
212 length of <i>items</i>. (See above for a description of the
213 optional parameters.) The
214 list is replaced and restarted every period of the cycle, which
215 defaults to the whole list, but may be specified by the <code>for:</code> keyword.
216 If <i>items</i> is a pattern, a new period of <i>items</i>
217 becomes the list from which items are generated for each cycle pattern.
218 Note that the <i>items</i> list may be truncated by the use of <code>for:</code>.
219 Different groupings and repetitions can be obtained by nesting <code>make-cycle</code>
220 within <code>make-length</code> and/or <code>make-copier</code> patterns.
221 </dd></dl><a name = "164"><h4>line</h4></a>
222 <p>The <code>line-class</code> is similar to the cycle class, but when it reaches the
223 end of the list of items, it simply repeats the last item in the list.
224 For example, two periods of <code>make-line({a b c})</code> would be
225 <code>(A B C) (C C C)</code>. </p>
226 <dl>
227 <dt>
228 <code>make-line(<a name="index1174"></a><a name="index1175"></a><a name="index1176"></a><i>items</i>, for: <i>for</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
229
230 <code>(make-line <i>items</i> :for <i>for</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
231 <dd>Make a line
232 pattern that iterates over <i>items</i>. The default period length is the
233 length of <i>items</i>. As with <code>make-cycle</code>, <i>items</i> may be a
234 pattern. (See above for a description of the optional parameters.)
235 </dd></dl><a name = "165"><h4>random</h4></a>
236 <p>The <code>random-class</code> generates items at random from a list. The default
237 selection is uniform random with replacement, but items may be further
238 specified with a weight, a minimum repetition count, and a maximum
239 repetition count. Weights give the relative probability of the selection
240 of the item (with a default weight of one). The minimum count specifies how
241 many times an item, once selected at random, will be repeated. The maximum
242 count specifies the maximum number of times an item can be selected in a row.
243 If an item has been generated <i>n</i> times in succession, and the maximum
244 is equal to <i>n</i>, then the item is disqualified in the next random selection.
245 Weights (but not currently minima and maxima) can be patterns. The patterns
246 (thus the weights) are recomputed every period. </p>
247 <dl>
248 <dt>
249 <code>make-random(<a name="index1177"></a><a name="index1178"></a><a name="index1179"></a><i>items</i>, for: <i>for</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
250
251 <code>(make-random <i>items</i> :for <i>for</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
252 <dd>Make a random
253 pattern that selects from <i>items</i>. Any (or all) element(s) of <i>items</i>
254 may be lists of the following form: <code>(<i>value</i> :weight <i>weight</i>
255 :min <i>mincount</i> :max <i>maxcount</i>)</code>, where <i>value</i> is the item
256 (or pattern) to be generated, <i>weight</i> is the (optional)
257 relative probability of
258 selecting this item, <i>mincount</i> is the (optional) minimum number of repetitions
259 when this item is selected, and <i>maxcount</i> is the (optional) maximum number of
260 repetitions allowed before selecting some other item. The default period
261 length is the length of <i>items</i>. If <i>items</i> is a pattern, a period
262 from that pattern becomes the list from which random selections are
263 made, and a new list is generated every period.
264 </dd></dl><a name = "166"><h4>palindrome</h4></a>
265 <p>The <code>palindrome-class</code> repeatedly traverses a list forwards and then
266 backwards. For example, two periods of <code>make-palindrome({a b c})</code>
267 would be <code>(A B C C B A) (A B C C B A)</code>. The <code>elide:</code>
268 keyword parameter controls whether the first and/or last elements are
269 repeated:
270 </p>
271 <pre>
272 make-palindrome({a b c}, elide: nil)
273 ;; generates A B C C B A A B C C B A ...
274
275 make-palindrome({a b c}, elide: t)
276 ;; generates A B C B A B C B ...
277
278 make-palindrome({a b c}, elide: :first)
279 ;; generates A B C C B A B C C B ...
280
281 make-palindrome({a b c}, elide: :last)
282 ;; generates A B C B A A B C B A ...
283 </pre>
284
285 <p></p>
286 <dl>
287 <dt>
288 <code>make-palindrome(<a name="index1180"></a><a name="index1181"></a><a name="index1182"></a><i>items</i>, elide: <i>elide</i>, for: <i>for</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
289
290 <code>(make-palindrome <i>items</i> :elide <i>elide</i> :for <i>for</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
291 <dd>Generate items
292 from list alternating in-order and reverse-order sequencing. The keyword
293 parameter <i>elide</i> can have the values <code>:first</code>, <code>:last</code>,
294 <code>t</code>, or <code>nil</code> to control repetition of the first and last elements.
295 The <i>elide</i> parameter can also be a pattern, in which case it is evaluated
296 every period. One period is one complete forward and backward traversal
297 of the list. If <i>items</i> is a pattern, a period
298 from that pattern becomes the list from which random selections are
299 made, and a new list is generated every period.
300 </dd></dl><a name = "167"><h4>heap</h4></a>
301 <p>The <code>heap-class</code> selects items in random order from a list
302 without replacement, which means that all items are generated once before
303 any item is repeated. For example, two periods of <code>make-heap({a b c})</code>
304 might be <code>(C A B) (B A C)</code>. Normally, repetitions can occur
305 even if all list elements are distinct. This happens when the last element
306 of a period is chosen first in the next period. To avoid repetitions, the
307 <code>max:</code> keyword argument can be set to 1. The <code>max:</code> keyword only
308 controls repetitions from the end of one period to the beginning of the next.
309 If the list contains more than one copy of the same value, it may be repeated
310 within a period regardless of the value of <code>max:</code>.</p>
311 <dl>
312 <dt>
313 <code>make-heap(<a name="index1183"></a><a name="index1184"></a><a name="index1185"></a><i>items</i>, for: <i>for</i>, max: <i>max</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
314
315 <code>(make-heap <i>items</i> :for <i>for</i> :max <i>max</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
316 <dd>Generate items
317 randomly from list without replacement. If <i>max</i> is 1, the first element of
318 a new period will not be the same as the last element of the previous period,
319 avoiding repetition. The default value of <i>max</i> is 2, meaning repetition is
320 allowed. The period length is the length
321 of <i>items</i>. If <i>items</i> is a pattern, a period
322 from that pattern becomes the list from which random selections are
323 made, and a new list is generated every period.
324 </dd></dl><a name = "168"><h4>accumulation</h4></a>
325 <p>The <code>accumulation-class</code> takes a list of values and returns
326 the first, followed by the first two, followed by the first three,
327 etc. In other words, for each list item, return all items from the
328 first through the item. For example, if the list is (A B C), each
329 generated period is (A A B A B C).</p>
330 <dl>
331 <dt>
332 <code>make-accumulation(<a name="index1186"></a><a name="index1187"></a><a name="index1188"></a><i>items</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
333
334 <code>(make-accumulation <i>items</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
335 <dd>Return
336 the prefixes of items, e.g. the first element followed by the
337 first and second elements, then the first three, until the
338 entire list is returned. The period length is (<i>n</i><sup T>2</sup> + <i>n</i>) / 2
339 where <i>n</i> is the length of <i>items</i>. If <i>items</i> is a pattern, a period
340 from that pattern becomes the list from which items are generated,
341 and a new list is generated every period. Note that this is similar in
342 name but different from <code>make-accumulate</code>.
343 </dd></dl><a name = "169"><h4>copier</h4></a>
344 <p>The <code>copier-class</code> makes copies of periods from a sub-pattern.
345 For example, three periods
346 of <code>make-copier(make-length(make-cycle({a b c}), 1), repeat: 2, merge: t)</code>
347 would be <code>(A A) (B B) (C C)</code>. Note that entire periods (not
348 individual items) are repeated, so in this example, <code>make-length</code>
349 is used to re-group the cycle into periods of length one so that
350 each item is repeated by the <code>repeat:</code> count.</p>
351 <dl>
352 <dt>
353 <code>make-copier(<a name="index1189"></a><a name="index1190"></a><a name="index1191"></a><i>sub-pattern</i>, repeat: <i>repeat</i>, merge: <i>merge</i>, for: <i>for</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
354
355 <code>(make-copier <i>sub-pattern</i> :repeat <i>repeat</i> :merge <i>merge</i> :for <i>for</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
356 <dd>Generate a period
357 from <i>sub-pattern</i> and repeat it <i>repeat</i> times. If <i>merge</i> is false
358 (the default), each repetition of a period from <i>sub-pattern</i> results
359 in a period by default. If <i>merge</i> is true (non-null), then all
360 <i>repeat</i> repetitions of the period are merged into one result
361 period by default. If the <code>for:</code> keyword is used, the same items
362 are generated, but the items are grouped into periods determined by
363 the <code>for:</code> parameter. If the <code>for:</code> parameter is a pattern,
364 it is evaluated every result period. The <i>repeat</i> and <i>merge</i> values
365 may be patterns that return a repeat count and a boolean value, respectively.
366 If so, these patterns are evaluated initially and after each <i>repeat</i>
367 copies are made (independent of the <code>for:</code> keyword parameter, if any).
368 The <i>repeat</i> value returned by a pattern can also be negative. A negative
369 number indicates how many periods of <i>sub-pattern</i> to skip. After skipping
370 these patterns, new <i>repeat</i> and <i>merge</i> values are generated.
371 </dd></dl><a name = "170"><h4>accumulate</h4></a>
372 <p>The <code>accumulate-class</code> forms the sum of numbers returned by another
373 pattern. For example, each period
374 of <code>make-accumulate(make-cycle({1 2 -3}))</code> is <code>(1 3 0)</code>.
375 The default output period length is the length of the input period.</p>
376 <dl>
377 <dt>
378 <code>make-accumulate(<a name="index1192"></a><a name="index1193"></a><a name="index1194"></a><i>sub-pattern</i>, for: <i>for</i>, max: <i>maximum</i>, min: <i>minimum</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
379
380 <code>(make-accumulate <i>sub-pattern</i> :for <i>for</i> :max <i>maximum</i> :min <i>minimum</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
381 <dd>Keep
382 a running sum of numbers generated by <i>sub-pattern</i>. The default
383 period lengths match the period lengths from <i>sub-pattern</i>.
384 If <i>maximum</i> (a pattern or a number) is specified, and the
385 running sum exceeds <i>maximum</i>, the running sum is reset
386 to <i>maximum</i>. If <i>minimum</i> (a pattern or a number) is
387 specified, and the running sum falls below <i>minimum</i>, the
388 running sum is reset to <i>minimum</i>. If <i>minimum</i> is greater
389 than <i>maximum</i>, the running sum will be set to one of the two
390 values. Minimum and maximum patterns are reevaluated every cycle.
391 Note that this is similar in name but not in function to <code>make-accumulation</code>.
392 </dd></dl><a name = "171"><h4>sum</h4></a>
393 <p>The <code>sum-class</code> forms the sum of numbers, one from each of two other
394 patterns. For example, each period
395 of <code>make-sum(make-cycle({1 2 3}), make-cycle({4 5 6}))</code>
396 is <code>(5 7 9)</code>.
397 The default output period length is the length of the input period of the
398 first argument. Therefore, the first argument must be a pattern, but the
399 second argument can be a pattern or a number.</p>
400 <dl>
401 <dt>
402 <code>make-sum(<a name="index1195"></a><a name="index1196"></a><a name="index1197"></a><i>x</i>, <i>y</i>, for: <i>for</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
403
404 <code>(make-sum <i>x</i> <i>y</i> :for <i>for</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
405 <dd>Form
406 sums of items (which must be numbers) from pattern
407 <i>x</i> and pattern or number <i>y</i>. The default
408 period lengths match the period lengths from <i>x</i>.
409 </dd></dl><a name = "172"><h4>product</h4></a>
410 <p>The <code>product-class</code> forms the product of numbers, one
411 from each of two other
412 patterns. For example, each period
413 of <code>make-product(make-cycle({1 2 3}), make-cycle({4 5 6}))</code>
414 is <code>(4 10 18)</code>.
415 The default output period length is the length of the input period of the
416 first argument. Therefore, the first argument must be a pattern, but the
417 second argument can be a pattern or a number.</p>
418 <dl>
419 <dt>
420 <code>make-product(<a name="index1198"></a><a name="index1199"></a><a name="index1200"></a><i>x</i>, <i>y</i>, for: <i>for</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
421
422 <code>(make-product <i>x</i> <i>y</i> :for <i>for</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
423 <dd>Form
424 products of items (which must be numbers) from pattern
425 <i>x</i> and pattern or number <i>y</i>. The default
426 period lengths match the period lengths from <i>x</i>.
427 </dd></dl><a name = "173"><h4>eval</h4></a>
428 <p>The <code>eval-class</code> evaluates an expression to produce each output item.
429 The default output period length is 1 if the expression is an expression;
430 otherwise, the expression must be a pattern that returns expressions to
431 be evaluated, and the output periods match the periods of the expression pattern.</p>
432 <dl>
433 <dt>
434 <code>make-eval(<a name="index1201"></a><a name="index1202"></a><a name="index1203"></a><a name="index1204"></a><a name="index1205"></a><i>expr</i>, for: <i>for</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
435
436 <code>(make-eval <i>expr</i> :for <i>for</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
437 <dd>Evaluate
438 <i>expr</i> to generate each item. If <i>expr</i> is a pattern, each item is generated
439 by getting the next item from <i>expr</i> and evaluating it. Note that <i>expr</i> is
440 evaluated as a Lisp expression, which is a list consisting of the function name followed
441 by parameter expressions (which may also be lists). In the simplest case (recommended),
442 create a SAL function with no parameters that performs the computation. For example, to
443 create a pattern in SAL that calls the parameterless function <code>rrandom</code>, write
444 <code>make-eval({rrandom})</code>. In SAL, <code>{rrandom}</code> creates a list with the
445 (unevaluated) symbol <code>rrandom</code>, equivalent to <code>list(quote(rrandom))</code>.
446 </dd></dl><a name = "174"><h4>length</h4></a>
447 <p>The <code>length-class</code> generates periods of a specified length from
448 another pattern. This is similar to using the <code>for:</code> keyword, but
449 for many patterns, the <code>for:</code> parameter alters the points at which
450 other patterns are generated. For example, if the palindrome pattern
451 has an <code>elide:</code> pattern parameter, the value will be computed every
452 period. If there is also a <code>for:</code> parameter with a value of 2, then
453 <code>elide:</code> will be recomputed every 2 items. In contrast, if the
454 palindrome (without a <code>for:</code> parameter) is embedded in a <i>length</i>
455 pattern with a lenght of 2, then the periods will all be of length 2, but
456 the items will come from default periods of the palindrome, and therefore
457 the <code>elide:</code> values will be recomputed at the beginnings of
458 default palindrome periods.</p>
459 <dl>
460 <dt>
461 <code>make-length(<a name="index1206"></a><a name="index1207"></a><a name="index1208"></a><i>pattern</i>, <i>length-pattern</i>,
462 name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
463
464 <code>(make-length <i>pattern</i> <i>length-pattern</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
465 <dd>Make a pattern of class
466 <code>length-class</code> that regroups items generated by a
467 <i>pattern</i> according to pattern lengths given by <i>length-pattern</i>.
468 Note that <i>length-pattern</i> is not optional: There is no default
469 pattern length and no <code>for:</code> keyword.
470 </dd></dl><a name = "175"><h4>window</h4></a>
471 <p>The <code>window-class</code> groups items from another pattern by using a sliding
472 window. If the <i>skip</i> value is 1, each output period is formed
473 by dropping the first item of the previous perioda and appending the next item
474 from the pattern. The <i>skip</i> value and the output period length can change
475 every period. For a simple example, if the period length is 3 and the
476 skip value is 1, and the input pattern generates the sequence A, B, C, ...,
477 then the output periods will be (A B C), (B C D), (C D E), (D E F), ....</p>
478 <dl>
479 <dt>
480 <code>make-window(<a name="index1209"></a><a name="index1210"></a><a name="index1211"></a><i>pattern</i>, <i>length-pattern</i>,
481 <i>skip-pattern</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
482
483 <code>(make-window <i>pattern</i> <i>length-pattern</i> <i>skip-pattern</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
484 <dd>Make
485 a pattern of class
486 <code>window-class</code> that regroups items generated by a
487 <i>pattern</i> according to pattern lengths given by <i>length-pattern</i>
488 and where the period advances by the number of items given by
489 <i>skip-pattern</i>.
490 Note that <i>length-pattern</i> is not optional: There is no default
491 pattern length and no <code>for:</code> keyword.
492 </dd></dl><a name = "176"><h4>markov</h4></a>
493 <p>The <code>markov-class</code> generates items from a Markov model. A Markov model
494 generates a sequence of <i>states</i> according to rules which specify possible
495 future states
496 given the most recent states in the past. For example, states might be
497 pitches, and each pitch might lead to a choice of pitches for the next state.
498 In the <code>markov-class</code>, states can be either symbols or numbers, but
499 not arbitrary values or patterns. This makes it easier to specify rules.
500 However, symbols can be mapped to arbitrary values including pattern
501 objects, and these become the actual generated items.
502 By default, all future states are weighted equally, but weights
503 may be associated with future states. A Markov model must be
504 initialized with
505 a sequence of past states using the <code>past:</code> keyword.
506 The most common form of Markov model is a "first
507 order Markov model" in which the future item depends only upon one
508 past item. However, higher order models where the future items depend on
509 two or more past items are possible. A "zero-order" Markov model, which
510 depends on no past states, is essentially equivalent to the random pattern.
511 As an example of a first-order Markov pattern,
512 two periods of <code>make-markov({{a -&gt; b c} {b -&gt; c} {c -&gt; a}}, past: {a})</code>
513 might be <code>(C A C) (A B C)</code>.</p>
514 <dl>
515 <dt>
516 <code>make-markov(<a name="index1212"></a><a name="index1213"></a><a name="index1214"></a><i>rules</i>, past: <i>past</i>, produces: <i>produces</i>, for: <i>for</i>, name: <i>name</i>, trace: <i>trace</i>)</code> [SAL]<br>
517
518 <code>(make-markov <i>rules</i> <i>past</i> :produces <i>produces</i> :for <i>for</i> :name <i>name</i> :trace <i>trace</i>)</code> [LISP]</dt>
519 <dd>Generate a sequence
520 of items from a Markov process. The <i>rules</i> parameter is a list of rules where each rule has the form:
521 <code>(<i>prev1</i> <i>prev2</i> ... <i>prevn</i> -&gt; <i>next1</i> <i>next2</i> ... <i>nextn</i>)</code>
522 where <i>prev1</i> through <i>prevn</i> represent a sequence of most recent
523 (past) states. The symbol <code>*</code> is treated specially: it matches any
524 previous state. If <i>prev1</i> through <i>prevn</i> (which may be just one state
525 as in the example above) match the previously generated states, this
526 rule applies. Note that every rule must specify the same number of previous
527 states; this number is known as the order of the Markov model.
528 The first rule in <i>rules</i> that applies is used to select the
529 next state. If no rule applies, the next state is <code>NIL</code> (which is
530 a valid state that can be used in rules).
531 Assuming a rule applies, the list of possible next
532 states is specified by <i>next1</i>
533 through <i>nextn</i>. Notice that these are alternative choices for the
534 next state, not a sequence of future states, and each rule can have
535 any number of choices. Each choice may be the state
536 itself (a symbol or a number), or the choice may be a list consisting of
537 the state and a weight. The weight may be given by a pattern, in which
538 case the next item of the pattern is obtained every time the rule is
539 applied. For example, this rules says that if the previous states were
540 A and B, the next state can be A with a weight of 0.5 or C with an
541 implied weight of 1: <code>(A B -&gt; (A 0.5) C)</code>. The
542 default length of the period is the length of <i>rules</i>. The
543 <i>past</i> parameter must be provided. It is a list of states whose length
544 matches the order of the Markov model. The keyword parameter <i>produces</i>
545 may be used to map from state symbols or numbers to other values or
546 patterns. The parameter is a list of alternating symbols and values. For
547 example, to map A to 69 and B to 71, use <code>list(quote(a), 69, quote(b), 71)</code>.
548 You can
549 also map symbols to patterns, for example
550 <code>list(quote(a), make-cycle({57 69}), quote(b), make-random({59 71}))</code>. The
551 next item of the pattern is is generated each time the Markov model generates
552 the corresponding state. Finally, the <i>produces</i> keyword can be
553 <code>:eval</code>, which means to evaluate the Markov model state. This could
554 be useful if states are Nyquist global variables such as
555 <code>C4, CS4, D4, ]..., which evaluate to numerical
556 values (60, 61, 62, ...</code>.<br><br>
557 <dt><code>markov-create-rules(<a name="index1215"></a><a name="index1216"></a><i>sequence</i>, <i>order</i> [, <i>generalize</i>])</code> [SAL]<br>
558
559 <code>(markov-create-rules <i>sequence</i> <i>order</i> [<i>generalize</i>])</code> [LISP]</dt>
560 <dd>Generate a set of rules suitable for the
561 <code>make-markov</code> function. The <i>sequence</i> is a &ldquo;typical&rdquo; sequence
562 of states, and <i>order</i> is the order of the Markov model. It is often the
563 case that a sample sequence will not have a transition from the last state
564 to any other state, so the generated Markov model can reach a &ldquo;dead end&rdquo;
565 where no rule applies. This might lead to an infinite stream of NIL's. To
566 avoid this, the optional parameter <i>generalize</i> can be set to <code>t</code>
567 (true), indicating that there should be a fallback rule that matches any
568 previous states and whose future states are weighted according to their
569 frequency in <i>sequence</i>. For example, if sequence contains 5 A's, 5 B's and
570 10 G's, the default rule will be <code>(* -&gt; (A 5) (B 5) (G 10))</code>. This
571 rule will be appended to the end so it will only apply if no other rule does.
572 </dd></dl><a name = "177"><h3>Random Number Generators</h3></a><a name="index1217"></a><a name="index1218"></a><a name="index1219"></a><a name="index1220"></a>
573 <p>The <code>distributions.lsp</code> library implements random number generators that return random values with various probability distributions. Without this library, you can generate random numbers with <i>uniform</i> distributions. In a uniform distribution, all values are equally likely. To generate a random integer in some range, use <code>random</code>. To generate a real number (FLONUM) in some range, use <code>real-random</code> (or <code>rrandom</code> if the range is 0-1). But there are other interesting distributions. For example, the Gaussian distribution is often used to model
574 real-world errors and fluctuations where values are clustered around some central value and large deviations are more unlikely than small ones. See Dennis Lorrain, "A Panoply of Stochastic 'Canons'," <i>Computer Music Journal</i> vol. 4, no. 1, 1980, pp. 53-81.
575 Further discussion and examples can be found in
576 <code>nyquist/demos/probability_distributions.htm</code><a name="index1221"></a>. </p>
577
578 <p>In most of the random number generators described below, there are optional parameters to indicate a maximum and/or minimum value. These can be used to truncate the distribution. For example, if you basically want a Gaussian distribution, but you never want a value greater than 5, you can specify 5 as the maximum value.
579 The upper and lower bounds are implemented simply by drawing a random number from the full distribution repeatedly until a number falling into the desired range is obtained. Therefore, if you select an acceptable range that is unlikely, it may take Nyquist a long time to find each acceptable random number. The intended use of the upper and lower bounds is to weed out values that are already fairly unlikely.</p>
580 <dl>
581 <dt>
582 <code>linear-dist(<a name="index1222"></a><a name="index1223"></a><i>g</i>)</code> [SAL]<br>
583
584 <code>(linear-dist <i>g</i>)</code> [LISP]</dt>
585 <dd>Return a <code>FLONUM</code> value from a linear distribution, where the probability of a value decreases linearly from zero to <i>g</i> which must be greater than zero. (See Figure <a href = "#177">7</a>.) The linear distribution is useful for generating for generating time and pitch intervals.
586 </dd></dl><hr>
587 <blockquote>
588 </blockquote>
589 <img src="linear-fig.gif"><br><br>
590
591 <p><b>Figure 7: </b>The Linear Distribution, <i>g</i> = 1.
592
593 </p>
594 <hr><dl>
595 <dt>
596 <code>exponential-dist(<a name="index1224"></a><a name="index1225"></a><i>delta</i> [, <i>high</i>])</code> [SAL]<br>
597
598 <code>(exponential-dist <i>delta</i> [<i>high</i>])</code> [LISP]</dt>
599 <dd>Return a <code>FLONUM</code> value from an exponential distribution. The initial downward slope is steeper with larger values of <i>delta</i>, which must be greater than zero. (See Figure <a href = "#177">8</a>. The optional <i>high</i> parameter puts an artificial upper bound on the return value.
600 The exponential distribution generates values greater
601 than 0, and can be used to generate time intervals. Natural random intervals such as the time intervals between the release of atomic particles or the passing of yellow volkswagons in traffic have exponential distributions. The
602 exponential distribution is memory-less: knowing that a random number from this distribution is greater than some value (e.g. a note duration is at least 1 second) tells you nothing new about how soon the note will end. This
603 is a continuous distribution, but <code>geometric-dist</code> (described below) implements the discrete form.
604 </dd></dl><hr>
605 <blockquote>
606 </blockquote>
607 <img src="exponential-fig.gif"><br><br>
608
609 <p><b>Figure 8: </b>The Exponential Distribution, <i>delta</i> = 1.
610
611 </p>
612 <hr><dl>
613 <dt>
614 <code>gamma-dist(<a name="index1226"></a><i>nu</i> [, <i>high</i>])</code> [SAL]<br>
615
616 <code>(gamma-dist <i>nu</i> [<i>high</i>])</code> [LISP]</dt>
617 <dd>Return a <code>FLONUM</code> value from a Gamma distribution. The value is greater than zero, has a mean of <i>nu</i> (a <code>FIXNUM</code> greater than zero), and a mode (peak) of around <i>nu</i> - 1.
618 The optional <i>high</i> parameter puts an artificial upper bound on the return value.
619 </dd></dl><hr>
620 <blockquote>
621 </blockquote>
622 <img src="gamma-fig.gif"><br><br>
623
624 <p><b>Figure 9: </b>The Gamma Distribution, <i>nu</i> = 4.
625
626 </p>
627 <hr><dl>
628 <dt>
629 <code>bilateral-exponential-dist(<a name="index1227"></a><a name="index1228"></a><i>xmu</i>,
630 <i>tau</i> [, <i>low</i>, <i>high</i>])</code> [SAL]<br>
631
632 <code>(bilateral-exponential-dist <i>xmu</i> <i>tau</i> [<i>low</i> <i>high</i>])</code> [LISP]</dt>
633 <dd>Returns a <code>FLONUM</code> value from a bilateral exponential distribution, where <i>xmu</i> is the center of the double exponential and <i>tau</i> controls the spread of the distribution. A larger <i>tau</i> gives a wider distribution (greater variance), and <i>tau</i> must be greater than zero. The <i>low</i> and <i>high</i> parameters give optional artificial bounds on the minimum and maximum output values, respectively.
634 This distribution is similar to the exponential, except
635 it is centered at 0 and can output negative values as well. Like
636 the exponential, it can be used to generate time intervals; however, it might be necessary to add a lower bound so as not to compute a negative time interval.
637 </dd></dl><hr>
638 <blockquote>
639 </blockquote>
640 <img src="bilateral-fig.gif"><br><br>
641
642 <p><b>Figure 10: </b>The Bilateral Exponential Distribution.
643
644 </p>
645 <hr><dl>
646 <dt>
647 <code>cauchy-dist(<a name="index1229"></a><a name="index1230"></a><i>tau</i> [, <i>low</i>, <i>high</i>])</code> [SAL]<br>
648
649 <code>(cauchy-dist <i>tau</i> [<i>low</i> <i>high</i>])</code> [LISP]</dt>
650 <dd>Returns a <code>FLONUM</code> from the Cauchy distribution, a symmetric distribution with a high peak at zero and a width (variance) that increases with parameter <i>tau</i>, which must be greater than zero. The <i>low</i> and <i>high</i> parameters give optional artificial bounds on the minimum and maximum output values, respectively.
651 </dd></dl><hr>
652 <blockquote>
653 </blockquote>
654 <img src="cauchy-fig.gif"><br><br>
655
656 <p><b>Figure 11: </b>The Cauchy Distribution, <i>tau</i> = 1.
657
658 </p>
659 <hr><dl>
660 <dt>
661 <code>hyperbolic-cosine-dist(<a name="index1231"></a>[<i>low</i>, <i>high</i>])</code> [SAL]<br>
662
663 <code>(hyperbolic-cosine-dist [<i>low</i> <i>high</i>])</code> [LISP]</dt>
664 <dd>Returns a <code>FLONUM</code> value from the hyperbolic cosine distribution, a symmetric distribution with its peak at zero. The <i>low</i> and <i>high</i> parameters give optional artificial bounds on the minimum and maximum output values, respectively.
665 </dd></dl><hr>
666 <blockquote>
667 </blockquote>
668 <img src="hyperbolic-fig.gif"><br><br>
669
670 <p><b>Figure 12: </b>The Hyperbolic Cosine Distribution.
671
672 </p>
673 <hr><dl>
674 <dt>
675 <code>logistic-dist(<a name="index1232"></a><a name="index1233"></a><i>alpha</i>, <i>beta</i> [, <i>low</i>, <i>high</i>])</code> [SAL]<br>
676
677 <code>(logistic-dist <i>alpha</i> <i>beta</i> [<i>low</i> <i>high</i>])</code> [LISP]</dt>
678 <dd>Returns a <code>FLONUM</code> value from the logistic distribution, which is symmetric about the mean. The <i>alpha</i> parameter primarily affects dispersion (variance), with larger values resulting in values closer to the mean (less variance), and the <i>beta</i> parameter primarily influences the mean. The <i>low</i> and <i>high</i> parameters give optional artificial bounds on the minimum and maximum output values, respectively.
679 </dd></dl><hr>
680 <blockquote>
681 </blockquote>
682 <img src="logistic-fig.gif"><br><br>
683
684 <p><b>Figure 13: </b>The Logistic Distribution, alpha = 1, beta = 2.
685
686 </p>
687 <hr><dl>
688 <dt>
689 <code>arc-sine-dist(<a name="index1234"></a><a name="index1235"></a>)</code> [SAL]<br>
690
691 <code>(arc-sine-dist)</code> [LISP]</dt>
692 <dd>Returns a <code>FLONUM</code> value from the arc sine distribution, which outputs values between 0 and 1. It is symmetric about the mean of 1/2, but is more likely to generate values closer to 0 and 1.
693 </dd></dl><hr>
694 <blockquote>
695 </blockquote>
696 <img src="arcsine-fig.gif"><br><br>
697
698 <p><b>Figure 14: </b>The Arc Sine Distribution.
699
700 </p>
701 <hr><dl>
702 <dt>
703 <code>gaussian-dist(<a name="index1236"></a><a name="index1237"></a><i>xmu</i>, <i>sigma</i> [, <i>low</i>, <i>high</i>])</code> [SAL]<br>
704
705 <code>(gaussian-dist <i>xmu</i> <i>sigma</i> [<i>low</i> <i>high</i>])</code> [LISP]</dt>
706 <dd>Returns a <code>FLONUM</code> value from the Gaussian or Gauss-Laplace distribution, a linear function of the normal distribution. It is symmetric about the mean of <i>xmu</i>, with a standard deviation of <i>sigma</i>, which must be greater than zero. The <i>low</i> and <i>high</i> parameters give optional artificial bounds on the minimum and maximum output values, respectively.
707 </dd></dl><hr>
708 <blockquote>
709 </blockquote>
710 <img src="gaussian-fig.gif"><br><br>
711
712 <p><b>Figure 15: </b>The Gauss-Laplace (Gaussian) Distribution, <i>xmu</i> = 0, <i>sigma</i> = 1.
713
714 </p>
715 <hr><dl>
716 <dt>
717 <code>beta-dist(<a name="index1238"></a><a name="index1239"></a><i>a</i>, <i>b</i>)</code> [SAL]<br>
718
719 <code>(beta-dist <i>a</i> <i>b</i>)</code> [LISP]</dt>
720 <dd>Returns a <code>FLONUM</code> value from the Beta distribution. This distribution outputs values between 0 and 1, with outputs more likely to be close to 0 or 1. The parameter <i>a</i> controls the height (probability) of the right side of the distribution (at 1) and <i>b</i> controls the height of the left side (at 0). The distribution is symmetric about 1/2 when <i>a</i> = <i>b</i>.
721 </dd></dl><hr>
722 <blockquote>
723 </blockquote>
724 <img src="beta-fig.gif"><br><br>
725
726 <p><b>Figure 16: </b>The Beta Distribution, <i>alpha</i> = .5, <i>beta</i> = .25.
727
728 </p>
729 <hr><dl>
730 <dt>
731 <code>bernoulli-dist(<a name="index1240"></a><a name="index1241"></a><i>px1</i> [, <i>x1</i>, <i>x2</i>])</code> [SAL]<br>
732
733 <code>(bernoulli-dist <i>px1</i> [<i>x1</i> <i>x2</i>])</code> [LISP]</dt>
734 <dd>Returns either <i>x1</i> (default value is 1) with probability <i>px1</i> or <i>x2</i> (default value is 0) with probability 1 - <i>px1</i>. The value of <i>px1</i> should be between 0 and 1. By
735 convention, a result of <i>x1</i> is viewed as a success while <i>x2</i> is viewed as
736 a failure.
737 </dd></dl><hr>
738 <blockquote>
739 </blockquote>
740 <img src="bernoulli-fig.gif"><br><br>
741
742 <p><b>Figure 17: </b>The Bernoulli Distribution, <i>px1</i> = .75.
743
744 </p>
745 <hr><dl>
746 <dt>
747 <code>binomial-dist(<a name="index1242"></a><a name="index1243"></a><i>n</i>, <i>p</i>)</code> [SAL]<br>
748
749 <code>(binomial-dist <i>n</i> <i>p</i>)</code> [LISP]</dt>
750 <dd>Returns a <code>FIXNUM</code> value from the binomial distribution, where <i>n</i> is the number of Bernoulli trials run (a <code>FIXNUM</code>) and <i>p</i> is the probability of success in the Bernoulli trial (a <code>FLONUM</code> from 0 to 1). The mean is the product of <i>n</i> and <i>p</i>.
751 </dd></dl><hr>
752 <blockquote>
753 </blockquote>
754 <img src="binomial-fig.gif"><br><br>
755
756 <p><b>Figure 18: </b>The Binomial Distribution, <i>n</i> = 5, <i>p</i> = .5.
757
758 </p>
759 <hr><dl>
760 <dt>
761 <code>geometric-dist(<a name="index1244"></a><a name="index1245"></a><i>p</i>)</code> [SAL]<br>
762
763 <code>(geometric-dist <i>p</i>)</code> [LISP]</dt>
764 <dd>Returns a <code>FIXNUM</code> value from the geometric distribution, which is defined as the number of failures before a success is achieved in a Bernoulli trial with probability of success <i>p</i> (a <code>FLONUM</code> from 0 to 1).
765 </dd></dl><hr>
766 <blockquote>
767 </blockquote>
768 <img src="geometric-fig.gif"><br><br>
769
770 <p><b>Figure 19: </b>The Geometric Distribution, <i>p</i> = .4.
771
772 </p>
773 <hr><dl>
774 <dt>
775 <code>poisson-dist(<a name="index1246"></a><a name="index1247"></a><i>delta</i>)</code> [SAL]<br>
776
777 <code>(poisson-dist <i>delta</i>)</code> [LISP]</dt>
778 <dd>Returns a <code>FIXNUM</code> value from the Poisson distribution with a mean of <i>delta</i> (a <code>FIXNUM</code>). The Poisson distribution is often used to generate a sequence of time intervals, resulting in random but often pleasing rhythms.
779 </dd></dl><hr>
780 <blockquote>
781 </blockquote>
782 <img src="poisson-fig.gif"><br><br>
783
784 <p><b>Figure 20: </b>The Poisson Distribution, <i>delta</i> = 3.
785
786 </p>
787 <hr><a name = "178"><h3>Score Generation and Manipulation</h3></a>
788 <p>A common application of pattern generators is to specify parameters
789 for notes. (It should be understood that &ldquo;notes&rdquo; in this context
790 means any Nyquist behavior, whether it represents a conventional note,
791 an abstract sound object, or even some micro-sound event that is just
792 a low-level component of a hierarchical sound organization. Similarly,
793 &ldquo;score&rdquo; should be taken to mean a specification for a
794 sequence of these &ldquo;notes.&rdquo;)
795 The <code>score-gen</code> macro (defined by
796 loading <code>xm.lsp</code>) establishes a convention for representing
797 scores and for generating them using patterns.</p>
798
799 <p>The <code>timed-seq</code> macro, described in Section <a href = "part8.html#99">Combination and Time Structure</a>,
800 already provides a way to represent a &ldquo;score&rdquo; as a list of expressions.
801 The Xmusic representation goes a bit further by specifying that
802 <i>all notes are specified by an alternation of keywords and values, where
803 some keywords have specific meanings and interpretations.</i></p>
804
805 <p>The basic idea of <code>score-gen</code><a name="index1248"></a> is you provide a template for notes in
806 a score as a set of keywords and values. For example,
807 </p>
808 <pre>
809 set pitch-pattern = make-cycle(list(c4, d4, e4, f4))
810 score-gen(dur: 0.4, name: quote(my-sound),
811 pitch: next(pitch-pattern), score-len: 9)
812 </pre>
813
814 <p>
815 generates a score of 9 notes as follows:
816 </p>
817 <pre>
818 ((0 0 (SCORE-BEGIN-END 0 3.6))
819 (0 0.4 (MY-SOUND :PITCH 60))
820 (0.4 0.4 (MY-SOUND :PITCH 62))
821 (0.8 0.4 (MY-SOUND :PITCH 64))
822 (1.2 0.4 (MY-SOUND :PITCH 65))
823 (1.6 0.4 (MY-SOUND :PITCH 60))
824 (2 0.4 (MY-SOUND :PITCH 62))
825 (2.4 0.4 (MY-SOUND :PITCH 64))
826 (2.8 0.4 (MY-SOUND :PITCH 65))
827 (3.2 0.4 (MY-SOUND :PITCH 60)))
828 </pre>
829
830 <p>
831 The use of keywords like <code>:PITCH</code> helps to make scores
832 readable and easy to process without specific knowledge of
833 about the functions called in the score. For example, one
834 could write a transpose operation to transform all the
835 <code>:pitch</code> parameters in a score without having to know
836 that pitch is the first parameter of <code>pluck</code> and the
837 second parameter of <code>piano-note</code>. Keyword parameters are
838 also used to give flexibility to note specification with
839 <code>score-gen</code>. Since this approach requires the use of
840 keywords, the next section
841 is a brief explanation of how to define functions that use
842 keyword parameters.</p>
843 <a name = "179"><h4>Keyword Parameters</h4></a><a name="index1249"></a><a name="index1250"></a>
844 <p>Keyword parameters are parameters whose presence is
845 indicated by a special symbol, called a keyword, followed
846 by the actual parameter. Keyword parameters in SAL have
847 default values that are used if no actual parameter is
848 provided by the caller of the function. (See Appendix
849 <a href = "part19.html#224">Appendix 3: XLISP: An Object-oriented Lisp</a> to learn about keywords in XLISP.)</p>
850
851 <p>To specify that a parameter is a keyword parameter,
852 use a keyword symbol (one that ends in a colon) followed
853 by a default value.
854 For example, here is a function that
855 accepts keyword parameters and invokes the <code>pluck</code>
856 function:
857 </p>
858 <pre>
859 define function k-pluck(pitch: 60, dur: 1)
860 return pluck(pitch, dur)
861 </pre>
862
863 <p>
864 Now, we can call k-pluck with keyword parameters. The
865 keywords are simply the formal parameter names with
866 a prepended colon character (<code>:pitch</code> and <code>:dur</code>
867 in this example), so a function call would look like:
868 </p>
869 <pre>
870 k-pluck(pitch: c3, dur: 3)
871 </pre>
872
873 <p>
874 Usually, it is best to give keyword parameters useful
875 default values. That way, if a parameter such as <code>dur:</code>
876 is missing, a reasonable default value (1) can be used
877 automatically.
878 It is never an error to omit a keyword parameter, but the
879 called function can check to see if a keyword parameter
880 was supplied or not.
881 Because of default values, we can call
882 <code>k-pluck(pitch: c3)</code> with no duration,
883 <code>k-pluck(dur: 3)</code> with only a duration,
884 or even <code>k-pluck()</code> with no parameters.</p>
885
886 <p>In XLISP, there is additional syntax to specify an alternate symbol
887 to be used as the keyword and to allow the called function
888 to determine whether or not a keyword parameter was
889 supplied, but these features are little-used. See the XLISP
890 manual for details.</p>
891 <a name = "180"><h4>Using score-gen</h4></a><a name="index1251"></a>
892 <p>The <code>score-gen</code> macro computes a score based on keyword parameters.
893 Some keywords have a special meaning, while others are not interpreted
894 but merely placed in the score. The resulting score can be synthesized
895 using <code>timed-seq</code> (see Section <a href = "part8.html#99">Combination and Time Structure</a>).</p>
896
897 <p>The form of a call to <code>score-gen</code> is simply:
898 <dl>
899 <dt>
900 <code>score-gen(<a name="index1252"></a><i>k1:</i> <i>e1</i>, <i>k2:</i> <i>e2</i>, <span style="font-style:normal">...</span>)</code> [SAL]<br>
901
902 <code>(score-gen <i>:k1</i> <i>e1</i> <i>:k2</i> <i>e2</i> <span style="font-style:normal">...</span>)</code> [LISP]</dt>
903 <dd>where the <i>k</i>'s
904 are keywords and the <i>e</i>'s are
905 expressions. A score is generated by evaluating the expressions once for
906 each note and constructing a list of keyword-value pairs. A number
907 of keywords have special interpretations. The rules for interpreting
908 these parameters will be explained through a set of
909 questions and answers below.
910 </dd></dl></p>
911
912 <p> <i>How many notes will be generated?</i> The keyword
913 parameter <code>score-len:</code> specifies an upper bound on the number
914 of notes. (Note: in LISP syntax, keywords
915 are always <i>preceded</i> by colons, so you would write
916 <code>:score-len</code> instead.) The keyword <code>score-dur:</code> specifies an upper bound
917 on the starting time of the last note in the score. (To be more
918 precise, the <code>score-dur:</code> bound is reached when the
919 default starting time of the next note is greater than or equal
920 to the <code>score-dur:</code> value. This definition is necessary because
921 note times are not strictly increasing.) When either bound
922 is reached, score generation ends. At least one of these two
923 parameters must be specified or an error is raised. These keyword
924 parameters are evaluated just once and are not copied into the
925 parameter lists of generated notes.</p>
926
927 <p> <i>What is the duration of generated notes?</i> The
928 keyword <code>dur:</code> defaults to 1 and specifies the nominal duration
929 in seconds. Since the generated note list is compatible with
930 <code>timed-seq</code>, the starting time and duration (to be precise, the
931 <i>stretch factor</i>) are not passed as parameters to the notes. Instead,
932 they control the Nyquist environment in which the note will be evaluated.</p>
933
934 <p> <i>What is the start time of a note?</i> The default start time of the
935 first note is zero. Given a note, the default start time of the next note is
936 the start time plus the inter-onset time, which is given by the <code>ioi:</code>
937 parameter. If no <code>ioi:</code> parameter is specified, the inter-onset time
938 defaults to the duration, given by <code>dur:</code>. In all cases, the default
939 start time of a note can be overridden by the keyword parameter <code>time:</code>.</p>
940
941 <p> <i>When does the score begin and end?</i> The behavior <code>SCORE-BEGIN-END</code>
942 contains the beginning and ending of the
943 score (these are used for score manipulations, e.g. when scores are merged,
944 their begin times can be aligned.) When <code>timed-seq</code> is used to
945 synthesize a score, the <code>SCORE-BEGIN-END</code> marker is
946 not evaluated. The <code>score-gen</code> macro inserts a &ldquo;note&rdquo; of the form
947 <code>(0 0 (SCORE-BEGIN-END <i>begin-time</i> <i>end-time</i>))</code>
948 at the time given by the <code>begin:</code> keyword, with <i>begin-time</i> and
949 <i>end-time</i> determined by the <code>begin:</code> and <code>end:</code>
950 keyword parameters, respectively. If the <i>begin:</i> keyword is not
951 provided, the score begins at zero. If the <code>end:</code> keyword
952 is not provided, the score ends at the default start time
953 of what would be the next note after the last note in the score
954 (as described in the previous paragraph). Note: if <code>time:</code> is used to
955 compute note starting times, and these times are not increasing, it is
956 strongly advised to use <code>end:</code> to specify an end time for the score,
957 because the default end time may be anywhere in the middle of the
958 generated sequence.</p>
959
960 <p> <i>What function is called to synthesize the note?</i> The <code>name:</code>
961 parameter names the function. Like other parameters, the value can be any
962 expression, including something like <code>next(fn-name-pattern)</code>,
963 allowing function names to be recomputed for each note. The default value
964 is <code>note</code>.</p>
965
966 <p> <i>Can I make parameters depend upon the starting time or the duration
967 of the note?</i> Parameter expressions can use the variable <code>sg:start</code>
968 to access the start time of the note, <code>sg:ioi</code> to access the
969 inter-onset time, and <code>sg:dur</code> to access the
970 duration (stretch factor) of the note. Also, <code>sg:count</code> counts how
971 many notes have been computed so far, starting at 0. The order of
972 computation is: <code>sg:start</code> first, then <code>sg:ioi</code> and <code>sg:dur</code>,
973 so for example, an expression to compute <code>sg:dur</code> can
974 depend on <code>sg:ioi</code>.</p>
975
976 <p> <i>Can parameters depend on each other?</i> The keyword <code>pre:</code>
977 introduces an expression that is evaluated before each note, and
978 <code>post:</code> provides an expression to be evaluated after each note.
979 The <code>pre:</code> expression can assign one or more global variables
980 which are then used in one or more expressions for parameters.</p>
981
982 <p> <i>How do I debug <code>score-gen</code> expressions?</i> You can set the
983 <code>trace:</code> parameter to true (<code>t</code>) to enable a print statement
984 for each generated note.</p>
985
986 <p> <i>How can I save scores generated by <code>score-gen</code> that I like?</i> If the
987 keyword parameter <code>save:</code> is set to a symbol, the global variable
988 named by the symbol is set to the value of the generated sequence. Of
989 course, the value returned by <code>score-gen</code> is just an ordinary list that
990 can be saved like any other value.</p>
991
992 <p>In summary, the following keywords have special interpretations
993 in <code>score-gen</code>:
994 <code>begin:</code>, <code>end:</code>, <code>time:</code>, <code>dur:</code>, <code>name:</code>,
995 <code>ioi:</code>, <code>trace:</code>,
996 <code>save:</code>, <code>score-len:</code>, <code>score-dur:</code>, <code>pre:</code>, <code>post:</code>.
997 All other keyword
998 parameters are expressions that are evaluated once for each note
999 and become the parameters of the notes.</p>
1000 <a name = "181"><h4>Score Manipulation</h4></a><a name="index1253"></a><a name="index1254"></a>
1001 <p>Nyquist encourages the representation of music as
1002 executable programs, or <i>behaviors</i>, and there are various
1003 ways to modify behaviors, including time stretching,
1004 transposition, etc. An alternative to composing executable
1005 programs is to manipulate scores as editable data. Each
1006 approach has its strengths and weaknesses. This section
1007 describes functions intended to manipulate Xmusic scores
1008 as generated by, or at least in the form generated by,
1009 <code>score-gen</code>. Recall that this means scores are lists
1010 of events (e.g. notes), where events are three-element lists of the form
1011 (<i>time</i> <i>duration</i> <i>expression</i>, and where <i>expression</i>
1012 is a standard lisp function call where all parameters are
1013 keyword parameters. In addition, the first &ldquo;note&rdquo; may be
1014 the special <code>SCORE-BEGIN-END</code> expression. If this is
1015 missing, the score begins at zero and ends at the end of the
1016 last note.</p>
1017
1018 <p>For convenience, a set of functions is offered to access properties
1019 of events (or notes) in scores. Although lisp functions such as
1020 <code>car</code>, <code>cadr</code>, and <code>caddr</code> can be used, code is more
1021 readable when more mnemonic functions are used to access events.</p>
1022 <dl>
1023 <dt>
1024 <code>event-time(<a name="index1255"></a><i>event</i>)</code> [SAL]<br>
1025
1026 <code>(event-time <i>event</i>)</code> [LISP]</dt>
1027 <dd>Retrieve the time field from
1028 an event.<br><br>
1029 <dt><code>event-set-time(<a name="index1256"></a><i>event</i>, <i>time</i>)</code> [SAL]<br>
1030
1031 <code>(event-set-time <i>event</i> <i>time</i>)</code> [LISP]</dt>
1032 <dd>Construct
1033 a new event where the time of <i>event</i> is replaced by <i>time</i>.<br><br>
1034 <dt><code>event-dur(<a name="index1257"></a><i>event</i>)</code> [SAL]<br>
1035
1036 <code>(event-dur <i>event</i>)</code> [LISP]</dt>
1037 <dd>Retrieve the duration
1038 (i.e. the stretch factor) field from an event.<br><br>
1039 <dt><code>event-set-dur(<a name="index1258"></a><i>event</i>, <i>dur</i>)</code> [SAL]<br>
1040
1041 <code>(event-set-dur <i>event</i> <i>dur</i>)</code> [LISP]</dt>
1042 <dd>Construct
1043 a new event where the duration (or stretch factor) of <i>event</i> is
1044 replaced by <i>dur</i>.<br><br>
1045 <dt><code>event-expression(<a name="index1259"></a><i>event</i>)</code> [SAL]<br>
1046
1047 <code>(event-expression <i>event</i>)</code> [LISP]</dt>
1048 <dd>Retrieve the expression
1049 field from an event.<br><br>
1050 <dt><code>event-set-expression(<a name="index1260"></a><i>event</i>,
1051 <i>dur</i>)</code> [SAL]<br>
1052
1053 <code>(event-set-expression <i>event</i> <i>dur</i>)</code> [LISP]</dt>
1054 <dd>Construct
1055 a new event where the expression of <i>event</i> is replaced by <i>expression</i>.<br><br>
1056 <dt><code>event-end(<a name="index1261"></a><i>event</i>)</code> [SAL]<br>
1057
1058 <code>(event-end <i>event</i>)</code> [LISP]</dt>
1059 <dd>Retrieve the end time
1060 of <i>event</i>, its time plus its duration.<br><br>
1061 <dt><code>expr-has-attr(<a name="index1262"></a><i>expression</i>, <i>attribute</i>)</code> [SAL]<br>
1062
1063 <code>(expr-has-attr <i>expression</i> <i>attribute</i>)</code> [LISP]</dt>
1064 <dd>Test
1065 whether a score event <i>expression</i> has the given <i>attribute</i>.<br><br>
1066 <dt><code>expr-get-attr(<a name="index1263"></a><i>expression</i>, <i>attribute</i> [, <i>default</i>])</code> [SAL]<br>
1067
1068 <code>(expr-get-attr <i>expression</i> <i>attribute</i> [<i>default</i>])</code> [LISP]</dt>
1069 <dd>Get the value of the given <i>attribute</i> from a score event
1070 <i>expression</i>. If <i>attribute</i> is not present, return <i>default</i> if
1071 specified, and otherwise <code>nil</code>. (See the example
1072 after <code>score-apply</code> below for an example using <code>expr-get-attr</code>.)<br><br>
1073 <dt><code>expr-set-attr(<a name="index1264"></a><i>expr</i>, <i>attribute</i>, <i>value</i>)</code> [SAL]<br>
1074
1075 <code>(expr-set-attr <i>expr</i> <i>attribute</i> <i>value</i>)</code> [LISP]</dt>
1076 <dd>Construct a new expression identical to <i>expr</i> except that the <i>attribute</i> has <i>value</i>. (See the example after <code>score-apply</code> below for an example
1077 using <code>expr-set-attr</code>.)<br><br>
1078 <dt><code>event-has-attr(<a name="index1265"></a><i>event</i>, <i>attribute</i>)</code> [SAL]<br>
1079
1080 <code>(event-has-attr <i>event</i> <i>attribute</i>)</code> [LISP]</dt>
1081 <dd>Test
1082 whether a given score <i>event</i>'s expression has the given <i>attribute</i>.<br><br>
1083 <dt><code>event-get-attr(<a name="index1266"></a><i>event</i>, <i>attribute</i>,
1084 [<i>default</i>])</code> [SAL]<br>
1085
1086 <code>(event-get-attr <i>event</i> <i>attribute</i> [<i>default</i>])</code> [LISP]</dt>
1087 <dd>Get the value of the given <i>attribute</i> from a score
1088 <i>event</i>'s expression. If <i>attribute</i> is not present, return <i>default</i> if
1089 specified, and otherwise <code>nil</code>.<br><br>
1090 <dt><code>event-set-attr(<a name="index1267"></a><i>event</i>, <i>attribute</i>, <i>value</i>)</code> [SAL]<br>
1091
1092 <code>(event-set-attr <i>event</i> <i>attribute</i> <i>value</i>)</code> [LISP]</dt>
1093 <dd>Construct a new event identical to <i>event</i> except that the <i>attribute</i> has <i>value</i>.
1094 </dd></dl>
1095 <p>Functions are provided to shift the starting times of notes,
1096 stretch times and durations, stretch only durations,
1097 add an offset to a keyword parameter, scale a keyword parameter, and
1098 other manipulations. Functions are also provided to extract
1099 ranges of notes, notes that match criteria, and to combine scores.
1100 Most of these functions (listed below in detail)
1101 share a set of keyword parameters that optionally limit the range over which
1102 the transformation operates. The <code>from-index:</code> and <code>to-index:</code>
1103 parameters specify the index of the first note and the index of the
1104 last note to be changed, where 1 (not zero) denotes the first note.
1105 If indices are in range, the number of note selected is
1106 <code>to-index</code> <code>-</code> <code>from-index</code> <code>+</code> <code>1</code>). Out-of-range
1107 indices are ignored.
1108 If these numbers are negative, they are offsets
1109 from the end of the score, e.g. -1 denotes the last note of the score. The
1110 <code>from-time:</code> and <code>to-time:</code> indicate a range of starting times
1111 of notes that will be affected by the manipulation. Only notes whose time
1112 is greater than or equal to the <i>from-time</i> and <i>strictly less than</i>
1113 the <i>to-time</i> are modified. If both index and time ranges are specified,
1114 only notes that satisfy <i>both</i> constraints are selected. (Note: in
1115 LISP syntax, colons <i>precede</i> the keyword, so use
1116 <code>:from-index</code>, <code>:to-index</code>,
1117 <code>:from-time</code>, and <code>:to-time</code>.)</p>
1118 <dl>
1119 <dt>
1120 <code>score-sorted(<a name="index1268"></a><i>score</i>)</code> [SAL]<br>
1121
1122 <code>(score-sorted <i>score</i>)</code> [LISP]</dt>
1123 <dd>Test if <i>score</i> is sorted.<br><br>
1124 <dt><code>score-sort(<a name="index1269"></a><i>score</i> [, <i>copy-flag</i>])</code> [SAL]<br>
1125
1126 <code>(score-sort <i>score</i> [<i>copy-flag</i>])</code> [LISP]</dt>
1127 <dd>Sort
1128 the notes in a
1129 score into start-time order. If copy-flag is nil, this is a destructive
1130 operation which should only be performed if the top-level score list
1131 is a fresh copy that is not shared by any other variables. (The
1132 <i>copy-flag</i> is intended for internal system use only.)
1133 For the following operations, it is assumed
1134 that scores are sorted, and all operations return a sorted score.<br><br>
1135 <dt><code>score-shift(<a name="index1270"></a><i>score</i>, <i>offset</i>, from-index: <i>i</i>, to-index: <i>j</i>, from-time: <i>x</i>,
1136 to-time: <i>y</i>)</code> [SAL]<br>
1137
1138 <code>(score-shift <i>score</i> <i>offset</i>
1139 :from-index <i>i</i> :to-index <i>j</i> :from-time <i>x</i>
1140 :to-time <i>y</i>)</code> [LISP]</dt>
1141 <dd>Add a constant
1142 <i>offset</i> to the starting time of a set of notes in <i>score</i>. By default,
1143 all notes are modified, but the range of notes can be limited with the
1144 keyword parameters. The begin time of the score is decreased if necessary to
1145 the minimum time of any event that is moved to an earlier time (by a negative
1146 <i>offset</i>), and the end time of the score is increased if necessary
1147 to the maximum end time of any event that is moved to a later time. If
1148 all shifted events remain within the score's begin-to-end range, the begin
1149 and end times are not changed.
1150 The original score is not modified, and a new score is returned.<br><br>
1151 <dt><code>score-stretch(<a name="index1271"></a><i>score</i>, <i>factor</i>, dur: <i>dur-flag</i>, time: <i>time-flag</i>, from-index: <i>i</i>,
1152 to-index: <i>j</i>, from-time: <i>x</i>, to-time: <i>y</i>)</code> [SAL]<br>
1153
1154 <code>(score-stretch <i>score</i> <i>factor</i>
1155 :dur <i>dur-flag</i> :time <i>time-flag</i> :from-index <i>i</i>
1156 :to-index <i>j</i> :from-time <i>x</i> :to-time <i>y</i>)</code> [LISP]</dt>
1157 <dd>Stretch
1158 note times and durations by <i>factor</i>. The default <i>dur-flag</i> is
1159 non-null, but if <i>dur-flag</i> is null, the original durations are retained
1160 and only times are stretched. Similarly, the default <i>time-flag</i> is
1161 non-null, but if <i>time-flag</i> is null, the original times are retained
1162 and only durations are stretched. If both <i>dur-flag</i> and <i>time-flag</i>
1163 are null, the score is not changed. If a range
1164 of notes is specified, times are scaled within that range, and
1165 notes after the range are shifted so that the stretched region does not
1166 create a "hole" or overlap with notes that follow. If the range begins
1167 or ends with a time (via <code>from-time:</code> and <code>to-time:</code>), time
1168 stretching
1169 takes place over the indicated time interval independent of whether
1170 any notes are present or where they start. In other words, the
1171 &ldquo;rests&rdquo; are stretched along with the notes.
1172 The original score is not modified, and a new score is returned.<br><br>
1173 <dt><code>score-transpose(<a name="index1272"></a><i>score</i>,
1174 <i>keyword</i>, <i>amount</i>, from-index: <i>i</i>, to-index: <i>j</i>, from-time: <i>x</i>, to-time: <i>y</i>)</code> [SAL]<br>
1175
1176 <code>(score-transpose <i>score</i>
1177 <i>keyword</i> <i>amount</i> :from-index <i>i</i> :to-index <i>j</i> :from-time <i>x</i> :to-time <i>y</i>)</code> [LISP]</dt>
1178 <dd>For
1179 each note in the score and in any indicated range, if there is a keyword
1180 parameter matching <i>keyword</i> and the
1181 parameter value is a number, increment
1182 the parameter value by <i>amount</i>. For example, to tranpose up by a whole
1183 step, write <code>(score-transpose 2 :pitch <i>score</i>)</code>. The
1184 original score is not modified, and a new score
1185 is returned. If <i>keyword</i> is <code>:pitch</code> and a corresponding parameter value
1186 is a list, each element of the list is incremented by <i>amount</i>. This special
1187 case is in keeping with the convention of <code>timed-seq</code> in which score
1188 events with lists for the <code>:pitch</code> attribute are expanded into "chords"
1189 by instantiating an event for each element (pitch) in the list (chord).<br><br>
1190 <dt><code>score-scale(<a name="index1273"></a><i>score</i>, <i>keyword</i>, <i>amount</i>, from-index: <i>i</i>, to-index: <i>j</i>, from-time: <i>x</i>,
1191 to-time: <i>y</i>)</code> [SAL]<br>
1192
1193 <code>(score-scale <i>score</i> <i>keyword</i> <i>amount</i>
1194 :from-index <i>i</i> :to-index <i>j</i> :from-time <i>x</i> :to-time <i>y</i>)</code> [LISP]</dt>
1195 <dd>For each note
1196 in the score and in any indicated range, if there is a keyword
1197 parameter matching <i>keyword</i> and the
1198 parameter value is a number, multiply
1199 the parameter value by <i>amount</i>. The original score is not modified,
1200 and a new score is returned.<br><br>
1201 <dt><code>score-sustain(<a name="index1274"></a><i>score</i>, <i>factor</i>, from-index: <i>i</i>, to-index: <i>j</i>, from-time: <i>x</i>,
1202 to-time: <i>y</i>)</code> [SAL]<br>
1203
1204 <code>(score-sustain <i>score</i> <i>factor</i>
1205 :from-index <i>i</i> :to-index <i>j</i> :from-time <i>x</i>
1206 :to-time <i>y</i>)</code> [LISP]</dt>
1207 <dd>For each note
1208 in the score and in any indicated range, multiply
1209 the duration (stretch factor) by <i>amount</i>. This can be used to
1210 make notes sound more legato or staccato, and does not change their
1211 starting times. The original score is not modified, and
1212 a new score is returned.<br><br>
1213 <dt><code>score-voice(<a name="index1275"></a><i>score</i>,
1214 <i>replacement-list</i>, from-index: <i>i</i>, to-index: <i>j</i>, from-time: <i>x</i>, to-time: <i>y</i>)</code> [SAL]<br>
1215
1216 <code>(score-voice <i>score</i>
1217 <i>replacement-list</i> :from-index <i>i</i> :to-index <i>j</i> :from-time <i>x</i> :to-time <i>y</i>)</code> [LISP]</dt>
1218 <dd>For each note
1219 in the score and in any indicated range, replace the behavior (function)
1220 name using <i>replacement-list</i>, which has the format:
1221 <code>((<i>old1 new1</i>) (<i>old2 new2</i>) <span style="font-style:normal">...</span>)</code>, where <i>oldi</i> indicates
1222 a current behavior name and <i>newi</i> is the replacement. If <i>oldi</i>
1223 is <code>*</code>, it matches anything. For example, to
1224 replace <code>my-note-1</code> by <code>trombone</code> and <code>my-note-2</code> by
1225 <code>horn</code>, use <code>score-voice(<i>score</i>, {{my-note-1 trombone}
1226 {my-note-2 horn}})</code>. To replace all instruments with
1227 <code>piano</code>, use <code>score-voice(<i>score</i>, {{* piano}})</code>.
1228 The original score is not modified, and a
1229 new score is returned.<br><br>
1230 <dt><code>score-merge(<a name="index1276"></a><i>score1</i>, <i>score2</i>, <span style="font-style:normal">...</span>)</code> [SAL]<br>
1231
1232 <code>(score-merge <i>score1</i> <i>score2</i> <span style="font-style:normal">...</span>)</code> [LISP]</dt>
1233 <dd>Create
1234 a new score containing all the notes of the parameters, which are all
1235 scores. The resulting notes retain their original times and durations. The
1236 merged score begin time is the minimum of the begin times of the parameters
1237 and the merged score end time is the maximum of the end times of
1238 the parameters. The original scores are not modified, and a new
1239 score is returned.<br><br>
1240 <dt><code>score-append(<a name="index1277"></a><i>score1</i>, <i>score2</i>, <span style="font-style:normal">...</span>)</code> [SAL]<br>
1241
1242 <code>(score-append <i>score1</i> <i>score2</i> <span style="font-style:normal">...</span>)</code> [LISP]</dt>
1243 <dd>Create
1244 a new score containing all the notes of the parameters, which are all
1245 scores. The begin time of the first score is unaltered. The begin time of
1246 each other score is aligned to the end time of the
1247 previous score; thus, scores are &ldquo;spliced&rdquo; in sequence. The original
1248 scores are not modified, and a new score is returned.<br><br>
1249 <dt><code>score-select(<a name="index1278"></a><a name="index1279"></a><i>score</i>,
1250 <i>predicate</i>, from-index: <i>i</i>, to-index: <i>j</i>, from-time: <i>x</i>,
1251 to-time: <i>y</i>, reject: <i>flag</i>, extract: <i>ex</i></code> [SAL]<br>
1252
1253 <code>(score-select <i>score</i>
1254 <i>predicate</i> :from-index <i>i</i> :to-index <i>j</i> :from-time <i>x</i>
1255 :to-time <i>y</i> :reject <i>flag</i> :extract <i>ex</i>)</code> [LISP]</dt>
1256 <dd>Select
1257 (or reject) notes to form a new score. Notes are selected if they fall
1258 into the given ranges of index and time <i>and</i> they satisfy
1259 <i>predicate</i>, a function of three parameters that is applied to the
1260 start time, duration, and the expression of the note. Alternatively,
1261 <i>predicate</i> may be <code>t</code>, indicating that all notes in range are
1262 to be selected. Note that <code>from-index:</code> <i>i</i> and <code>to-index:</code>
1263 <i>j</i> are 1-based: A value of 1 refers to the first note, etc., because
1264 the zeroth element of the score is a <code>SCORE-BEGIN-END</code> event.
1265 For consistency, if a <code>SCORE-BEGIN-END</code> event is missing
1266 from <i>score</i>, one is inserted before any further processing.
1267 The selected notes are combined to form a new
1268 score. By default, <i>ex</i>, the value of the <code>extract:</code> keyword,
1269 is false, and the begin and end markers are retained from
1270 <i>score</i>. Conceptually, the score and its timing are retained as an
1271 object, but only some of its constituent sound events are retained.
1272 Otherwise, if <i>ex</i> is
1273 true (non-nil), the begin and end times are computed based on the
1274 selected (extracted) events: The begin time is <code>from-time</code> <i>x</i>,
1275 if present, and otherwise the time of the first event (if any), and
1276 otherwise the begin time of <i>score</i>. Similarly, the end time is the
1277 <code>to-time</code> <i>y</i>, if present, and otherwise the end time of the
1278 last event (if any), and otherwise the new start time. Alternatively,
1279 if the <code>reject:</code> parameter is non-null, the notes <i>not</i>
1280 selected form the new score. In other words the selected notes are
1281 rejected or removed to form the new score. The begin and end times of
1282 <i>score</i> are
1283 retained and the <code>extract</code> parameter (<i>ex</i>) is ignored. In all
1284 cases, the original score is not modified, and a new score is
1285 returned.<br><br>
1286 <dt><code>score-set-begin(<a name="index1280"></a><i>score</i>, <i>time</i>)</code> [SAL]<br>
1287
1288 <code>(score-set-begin <i>score</i> <i>time</i>)</code> [LISP]</dt>
1289 <dd>The begin
1290 time
1291 from the <i>score</i>'s <code>SCORE-BEGIN-END</code> marker is set to <i>time</i>. The
1292 original score is not modified, and a new score is returned.<br><br>
1293 <dt><code>score-get-begin(<a name="index1281"></a><i>score</i>)</code> [SAL]<br>
1294
1295 <code>(score-get-begin <i>score</i>)</code> [LISP]</dt>
1296 <dd>Return the begin
1297 time of the <i>score</i>.<br><br>
1298 <dt><code>score-set-end(<a name="index1282"></a><i>score</i>, <i>time</i>)</code> [SAL]<br>
1299
1300 <code>(score-set-end <i>score</i> <i>time</i>)</code> [LISP]</dt>
1301 <dd>The end time
1302 from the <i>score</i>'s <code>SCORE-BEGIN-END</code> marker is set to <i>time</i>. The
1303 original score is not modified, and a new score is returned.<br><br>
1304 <dt><code>score-get-end(<a name="index1283"></a><i>score</i>)</code> [SAL]<br>
1305
1306 <code>(score-get-end <i>score</i>)</code> [LISP]</dt>
1307 <dd>Return the end
1308 time of the <i>score</i>.<br><br>
1309 <dt><code>score-must-have-begin-end(<a name="index1284"></a><i>score</i>)</code> [SAL]<br>
1310
1311 <code>(score-must-have-begin-end <i>score</i>)</code> [LISP]</dt>
1312 <dd>If
1313 <i>score</i> does not have a begin and end time, construct a score with a
1314 <code>SCORE-BEGIN-END</code> expression and return it. If score already has a begin
1315 and end time, just return the score. The orignal score is not modified.<br><br>
1316 <dt><code>score-filter-length(<a name="index1285"></a><i>score</i>,
1317 <i>cutoff</i>)</code> [SAL]<br>
1318
1319 <code>(score-filter-length <i>score</i> <i>cutoff</i>)</code> [LISP]</dt>
1320 <dd>Remove
1321 notes that extend beyond the <i>cutoff</i> time. This
1322 is similar to <code>score-select</code>, but the here, events are removed when
1323 their nominal ending time (start time plus duration) exceeds the <i>cutoff</i>,
1324 whereas the <code>to-time:</code> parameter is compared to the note's start time.
1325 The original score is not modified, and a new score is returned.<br><br>
1326 <dt><code>score-repeat(<a name="index1286"></a><i>score</i>, <i>n</i>)</code> [SAL]<br>
1327
1328 <code>(score-repeat <i>score</i> <i>n</i>)</code> [LISP]</dt>
1329 <dd>Make a sequence
1330 of <i>n</i> copies of <i>score</i>. Each copy is shifted to that it's begin
1331 time aligns with the end time of the previous copy, as in <code>score-append</code>.
1332 The original score is not modified, and a new score is returned.<br><br>
1333 <dt><code>score-stretch-to-length(<a name="index1287"></a><i>score</i>,
1334 <i>length</i>)</code> [SAL]<br>
1335
1336 <code>(score-stretch-to-length <i>score</i> <i>length</i>)</code> [LISP]</dt>
1337 <dd>Stretch the score so that the end time of the score is
1338 the score's begin time plus <i>length</i>.
1339 The original score is not modified, and a new score is returned.<br><br>
1340 <dt><code>score-filter-overlap(<a name="index1288"></a><i>score</i>)</code> [SAL]<br>
1341
1342 <code>(score-filter-overlap <i>score</i>)</code> [LISP]</dt>
1343 <dd>Remove
1344 overlapping notes (based on the note start time and duration), giving
1345 priority to the
1346 positional order within the note list (which is also time order).
1347 The original score is not modified,
1348 and a new score is returned.<br><br>
1349 <dt><code>score-print(<a name="index1289"></a><i>score</i>, [<i>lines</i>])</code> [SAL]<br>
1350
1351 <code>(score-print <i>score</i> [<i>lines</i>])</code> [LISP]</dt>
1352 <dd>Print a score with
1353 one note per line. Returns <code>nil</code>. If <i>lines</i> (optional FIXNUM) is given, print a maximum of that many lines (but the minimum is at least 3). The format is first <i>lines</i>-2 score events, the line "<code>...</code>", and the last score event.<br><br>
1354 <dt><code>score-play(<a name="index1290"></a><i>score</i>)</code> [SAL]<br>
1355
1356 <code>(score-play <i>score</i>)</code> [LISP]</dt>
1357 <dd>Play <i>score</i>
1358 using <code>timed-seq</code> to convert the score to a sound, and
1359 <code>play</code> to play the sound.<br><br>
1360 <dt><code>score-adjacent-events(<a name="index1291"></a><i>score</i>,
1361 <i>function</i>,
1362 from-index: <i>i</i>, to-index: <i>j</i>, from-time: <i>x</i>, to-time: <i>y</i>)</code> [SAL]<br>
1363
1364 <code>(score-adjacent-events <i>score</i> <i>function</i> :from-index <i>i</i> :to-index <i>j</i> :from-time <i>x</i> :to-time <i>y</i>)</code> [LISP]</dt>
1365 <dd>Call
1366 <code>(<i>function</i> <i>A</i> <i>B</i> <i>C</i>)</code>, where
1367 <i>A</i>, <i>B</i>, and <i>C</i> are consecutive notes in the score. The result
1368 replaces <i>B</i>. If the result is <code>nil</code>, <i>B</i> is deleted, and the
1369 next call will be <code>(<i>function A C D</i>)</code>, etc. The first call is
1370 to <code>(<i>function</i> nil <i>A B</i>)</code> and the last is to
1371 <code>(<i>function</i> <i>Y Z</i> nil)</code>. If there is just one note in the
1372 score, <code>(<i>function</i> nil <i>A</i> nil)</code> is called. Function calls
1373 are not made if the note is outside of the indicated range.
1374 This function
1375 allows notes and their parameters to be adjusted according to their
1376 immediate context. The original score is not modified,
1377 and a new score is returned.<br><br>
1378 <dt><code>score-apply(<a name="index1292"></a><i>score</i>, <i>function</i>,
1379 from-index: <i>i</i>, to-index: <i>j</i>, from-time: <i>x</i>, to-time: <i>y</i>)</code>
1380 [SAL]<br>
1381
1382 <code>(score-apply <i>score</i> <i>function</i> :from-index <i>i</i> :to-index <i>j</i> :from-time <i>x</i> :to-time <i>y</i>)</code> [LISP]</dt>
1383 <dd>Replace
1384 each note in the score with the result of
1385 <code>(<i>function time dur expression</i>)</code> (in Lisp) or
1386 <code><i>function</i>(<i>time</i>, <i>dur</i>, <i>expression</i>)</code> (in SAL),
1387 where <i>time</i>, <i>dur</i>,
1388 and <i>expression</i> are the time, duration, and expression of the note.
1389 If a range is indicated, only notes in the range are replaced.
1390 The original score is not modified, and a new score is returned.
1391 </dd></dl>
1392 For example, the following uses <code>score-apply</code> to insert an
1393 <code>accent:</code> attribute with value 100 on every sound event
1394 (note) where <code>pitch:</code> is greater than 70. Notice that
1395 <code>add-accents</code> must be quoted to pass the <i>name</i> of the function
1396 to <code>score-apply</code> (without the <code>quote</code>,
1397 <code>add-accents</code> denotes the value of a variable, but as a variable,
1398 <code>add-accents</code> is undefined). Also, note that <code>add-accents</code>
1399 must construct and return a valid score event, hence the expression
1400 <code>list(time, dur, sound))</code>.
1401
1402 <p></p>
1403 <pre>
1404 set my-score = {{0 1 {note pitch: 60}}
1405 {1 1 {note pitch: 72}}}
1406
1407 function add-accents(time, dur, sound)
1408 begin
1409 if expr-get-attr(sound, :pitch, 70) &gt; 70 then
1410 set sound = expr-set-attr(sound, :accent, 100)
1411 return list(time, dur, sound)
1412 end
1413
1414 exec score-print(score-apply(my-score, quote(add-accents)))
1415 </pre>
1416
1417 <p>
1418 The output will be:
1419 </p>
1420 <pre>
1421 ((0 0 (SCORE-BEGIN-END 0 2))
1422 (0 1 (NOTE :PITCH 60))
1423 (1 1 (NOTE :PITCH 72 :ACCENT 100))
1424 )
1425 </pre>
1426
1427 <p></p>
1428 <dl>
1429 <dt>
1430 <code>score-indexof(<a name="index1293"></a><i>score</i>, <i>function</i>,
1431 from-index: <i>i</i>, to-index: <i>j</i>, from-time: <i>x</i>, to-time: <i>y</i>)</code> [SAL]<br>
1432
1433 <code>(score-indexof <i>score</i> <i>function</i> :from-index <i>i</i> :to-index <i>j</i> :from-time <i>x</i> :to-time <i>y</i>)</code> [LISP]</dt>
1434 <dd>Return the index (position)
1435 of the first score event (in range) for which applying <i>function</i>
1436 using <code>(<i>function time dur expression</i>)</code> returns true.<br><br>
1437 <dt><code>score-last-indexof(<a name="index1294"></a><i>score</i>,
1438 <i>function</i>, from-index: <i>i</i>, to-index: <i>j</i>, from-time: <i>x</i>,
1439 to-time: <i>y</i>)</code> [SAL]<br>
1440
1441 <code>(score-last-indexof <i>score</i> <i>function</i>
1442 :from-index <i>i</i> :to-index <i>j</i> :from-time <i>x</i> :to-time <i>y</i>)</code> [LISP]</dt>
1443 <dd>Return
1444 the index (position)
1445 of the last score event (in range) for which applying <i>function</i>
1446 using <code>(<i>function time dur expression</i>)</code> returns true.<br><br>
1447 <dt><code>score-randomize-start(<a name="index1295"></a><a name="index1296"></a><a name="index1297"></a><a name="index1298"></a><i>score</i>, <i>amt</i>, from-index: <i>i</i>, to-index: <i>j</i>, from-time: <i>x</i>, to-time: <i>y</i>)</code> [SAL]<br>
1448
1449 <code>(score-randomize-start <i>score</i> <i>amt</i>
1450 :from-index <i>i</i> :to-index <i>j</i> :from-time <i>x</i> :to-time <i>y</i>)</code> [LISP]</dt>
1451 <dd>Alter the
1452 start times of notes by a
1453 random amount up to plus or minus <i>amt</i>.
1454 The original score is not modified, and a new score is returned.
1455 </dd></dl><a name = "182"><h4>Xmusic and Standard MIDI Files</h4></a><a name="index1299"></a><a name="index1300"></a>
1456 <p>Nyquist has a general facility to read and write MIDI files.
1457 You can even translate to and from a text representation, as described
1458 in Chapter <a href = "part12.html#123">MIDI, Adagio, and Sequences</a>. It is also useful sometimes to read notes
1459 from Standard MIDI Files into Xmusic scores and vice versa. </p>
1460
1461 <p>Using <code>score-read-smf</code> and <code>score-read</code>, MIDI notes are
1462 translated to Xmusic score events as follows:
1463 <blockquote>
1464 <code>(<i>time</i> <i>dur</i> (NOTE :chan <i>channel</i> :pitch <i>keynum</i> :vel <i>velocity</i>))</code>,<br>
1465
1466 </blockquote> where <i>channel</i>, <i>keynum</i>, and <i>velocity</i> come directly
1467 from the MIDI message (channels are numbered starting from zero).
1468 Note also that note-off messages are implied by the stretch factor
1469 <i>dur</i> which is duration in seconds.</p>
1470
1471 <p>The <code>score-from-seq</code> function allows you to obtain control changes
1472 and other MIDI messages.</p>
1473 <dl>
1474 <dt>
1475 <code>score-read-smf(<a name="index1301"></a><a name="index1302"></a><i>filename</i>)</code> [SAL]<br>
1476
1477 <code>(score-read-smf <i>filename</i>)</code> [LISP]</dt>
1478 <dd>Read a
1479 standard MIDI file from <i>filename</i>. Return an Xmusic score, or <code>nil</code>
1480 if the file could not be opened. The
1481 start time is zero, and the end time is the maximum end time of all
1482 notes. A very limited interface is offered to extract MIDI program numbers
1483 from the file: The global variable <code>*rslt*</code> is set to a list of MIDI
1484 program numbers for each channel. E.g. if <code>*rslt*</code> is <code>(0 20 77)</code>,
1485 then program for channel 0 is 0, for channel 1 is 20, and for channel 2 is 77.
1486 Program changes were not found on other channels. The default program number is
1487 0, so in this example, it is not known whether the program 0 on channel 0
1488 is the result of a real MIDI program change command or just a default value.
1489 If more than one program change exists on a channel, the <i>last</i> program
1490 number is recorded and returned, so this information will only be completely
1491 correct when the MIDI file sends single program change per channel before
1492 any notes are played. This, however, is a fairly common practice. Note that
1493 the list returned as <code>*rslt*</code> can be passed
1494 to <code>score-write-smf</code>, described below.<br><br>
1495 <dt><code>score-read(<a name="index1303"></a><a name="index1304"></a><i>filename</i>)</code> [SAL]<br>
1496
1497 <code>(score-read <i>filename</i>)</code> [LISP]</dt>
1498 <dd>Read an Adagio
1499 file from <i>filename</i>. Return an Xmusic score, or <code>nil</code>
1500 if the file could not be opened. See Chapter <a href = "part12.html#123">MIDI, Adagio, and Sequences</a> for details on
1501 Adagio, a text-based score language. See <code>score-read-smf</code> for
1502 details on handling program changes.<br><br>
1503 <dt><code>score-from-seq(<a name="index1305"></a><a name="index1306"></a><i>seq</i>, prog: <i>pflag</i>, synths: <i>synths</i>, bend: <i>bend</i>, cpress: <i>cpress</i>, ctrl: <i>ctrls</i>)</code> [SAL]<br>
1504
1505 <code>(score-from-seq <i>seq</i> :prog <i>pflag</i> :synths <i>synths</i> :bend <i>bend</i> :cpress <i>cpress</i> :ctrl <i>ctrls</i>)</code> [LISP]</dt>
1506 <dd>Produce
1507 a score from a sequence (see Section <a href = "part12.html#124">The SEQ Type</a>) type
1508 <i>seq</i>. The optional <i>pflag</i>, if non-null, will insert program
1509 changes as event <code>:prog</code> attributes in score events. The
1510 <i>bend</i> and <i>cpress</i> (channel pressure) values may be
1511 <code>:onset</code> to introduce <code>:bend</code> or <code>:cpress</code> attributes
1512 into score events, or <code>:contin</code> to encode these MIDI messages as
1513 SOUNDs available through event <code>:contin</code> attributes. The
1514 <i>ctrl</i> parameter is a list where the first element is either
1515 <code>:onset</code> or <code>:contin</code> and the remaining elements are
1516 controller numbers to be encoded. In all cases <code>:contin</code> values
1517 appear in score events as an object. You can access the SOUNDs that
1518 encode control changes using the functions
1519 <code>ctrlfn-bend(<i>contin</i>)</code><a name="index1307"></a>,
1520 <code>ctrlfn-cpress(<i>contin</i>)</code><a name="index1308"></a> or
1521 <code>ctrlfn-ctrl(<i>contin</i>, <i>number</i>)</code><a name="index1309"></a>, which
1522 will return <code>const(0)</code> if no corresponding MIDI messages were
1523 found. (As a special case the default for controller 7 (volume pedal)
1524 is <code>const(1)</code>).
1525 See <code>lib/midi/midi_tutorial.htm</code> for more details and code
1526 examples. To test for the presence of MIDI messages and avoid the
1527 construction of <code>const(0)</code>, use
1528 <code>ctrlfn-bend?(<i>contin</i>)</code><a name="index1310"></a>,
1529 <code>ctrlfn-cpress?(<i>contin</i>)</code><a name="index1311"></a> or
1530 <code>ctrlfn-ctrl?(<i>contin</i>, <i>number</i>)</code><a name="index1312"></a>.<br><br>
1531 <dt><code>score-write-smf(<a name="index1313"></a><a name="index1314"></a><i>score</i>, <i>filename</i>,
1532 [<i>programs</i> <i>as-adagio</i>])</code> [SAL]<br>
1533
1534 <code>(score-write-smf <i>score</i> <i>filename</i> [<i>programs</i> <i>as-adagio</i>])</code> [LISP]</dt>
1535 <dd>Write a standard MIDI file to <i>filename</i>
1536 with notes in <i>score</i>. In this function,
1537 <i>every</i> event in the score with a <code>pitch:</code> attribute, regardless of the
1538 &ldquo;instrument&rdquo; (or function name), generates a
1539 MIDI note, using the <code>chan:</code> attribute for the channel (default 0) and
1540 the <code>vel:</code> attribute for velocity (default 100). There is no facility
1541 (in the current implementation) to issue control changes, but to allow
1542 different instruments, MIDI programs may be set in two ways. The simplest is
1543 to associate programs with channels using
1544 the optional <i>programs</i> parameter, which is simply a list of up to 16 MIDI
1545 program numbers. Corresponding program change commands are added to the
1546 beginning of the MIDI file. If <i>programs</i> has less than 16 elements, program
1547 change commands are only sent on the first <i>n</i> channels. The second way to
1548 issue MIDI program changes is to add a <code>program:</code> keyword parameter to
1549 a note in the score. Typically, the note will have a <code>pitch:</code> of
1550 <code>nil</code> so that no actual MIDI note-on message is generated. If program
1551 changes and notes have the same starting times, their relative playback
1552 order is undefined, and the note may be cut off by an immediately
1553 following program change. Therefore, program changes should occur slightly,
1554 e.g. 1 ms, before any notes. <i>Program numbers and channels are numbered
1555 starting at zero, matching the internal MIDI representation. This may be
1556 one less than displayed on MIDI hardware, sequencers, etc.</i> The
1557 <i>as-adagio</i> optional parameter should normally be omitted.
1558 If non-nil, the file is written in Adagio format, but if you
1559 want to do that, call <code>score-write</code> instead. Xmusic scores do not specify
1560 tempo, so the MIDI file is written with a fixed tempo of 100bpm. If you create
1561 scores or stretch scores so that each beat is exactly 0.6s (100bpm), sequencers
1562 and score editors will quantize your scores correctly. Otherwise, the <i>timing</i>
1563 will be correct, but for example a score with one note every second will be
1564 notated as 1 note every 1 2/3 beats.<br><br>
1565 <dt><code>score-write(<a name="index1315"></a><a name="index1316"></a><i>score</i>, <i>filename</i>,
1566 [<i>programs</i>, <i>absolute</i>])</code> [SAL]<br>
1567
1568 <code>(score-write <i>score</i> <i>filename</i> [<i>programs</i> <i>absolute</i>)]</code> [LISP]</dt>
1569 <dd>Write an Adagio format file to <i>filename</i> with notes in <i>score</i>, using absolute times if <i>absolute</i> is true, otherwise write relative times (the default). See Chapter <a href = "part12.html#123">MIDI, Adagio, and Sequences</a> for details on Adagio, a text-based score language. See <code>score-write-smf</code> for details on MIDI program changes.
1570 </dd></dl><a name = "183"><h4>Workspaces</h4></a><a name="index1317"></a>
1571 <p>When working with scores, you may find it necessary to save
1572 them in files between work sessions. This is not an issue
1573 with functions because they are
1574 normally edited in files and loaded from them. In contrast,
1575 scores are created as Lisp data, and unless you take care to
1576 save them, they will be destroyed when you exit the Nyquist
1577 program.</p>
1578
1579 <p>A simple mechanism called a workspace has been created
1580 to manage scores (and any other Lisp data, for that matter).
1581 A workspace is just a set of lisp global variables. These
1582 variables are stored in the file <code>workspace.lsp</code>.
1583 For simplicity, there is only one workspace, and no backups
1584 or versions are maintained, but the user is free to make
1585 backups and copies of <code>workspace.lsp</code>.
1586 To help remember what each variable is for, you can also
1587 associate and retrieve a text string with each variable.
1588 The following functions manage workspaces.</p>
1589
1590 <p>In addition, when a workspace is loaded, you can request that
1591 functions be called. For example, the workspace might store
1592 descriptions of a graphical interface. When the workspace is
1593 loaded, a function might run to convert saved data into a
1594 graphical interface. (This is how sliders are saved by the IDE.)</p>
1595 <dl>
1596 <dt>
1597 <code>add-to-workspace(<a name="index1318"></a><i>symbol</i>)</code> [SAL]<br>
1598
1599 <code>(add-to-workspace <i>symbol</i>)</code> [LISP]</dt>
1600 <dd>Adds
1601 a global variable to the workspace. The <i>symbol</i> should be a (quoted)
1602 symbol.<br><br>
1603 <dt><code>save-workspace(<a name="index1319"></a>)</code> [SAL]<br>
1604
1605 <code>(save-workspace)</code> [LISP]</dt>
1606 <dd>All global variables
1607 in the workspace are saved to <code>workspace.lsp</code> (in the current
1608 directory), overwriting the previous file.<br><br>
1609 <dt><code>describe(<a name="index1320"></a><i>symbol</i> [, <i>description</i>])</code>
1610 [SAL]<br>
1611
1612 <code>(describe <i>symbol</i> [<i>description</i>])</code> [LISP]</dt>
1613 <dd>If <i>description</i>, a text string, is present,
1614 associate <i>description</i> with the variable named by the
1615 <i>symbol</i>. If <i>symbol</i> is not already in the workspace,
1616 it is added. If <i>description</i> is omitted, the function returns
1617 the current description (from a previous call) for <i>symbol</i>.<br><br>
1618 <dt><code>add-action-to-workspace(<a name="index1321"></a><i>symbol</i>)</code> [SAL]<br>
1619
1620 <code>(add-action-to-workspace <i>symbol</i>)</code> [LISP]</dt>
1621 <dd>Requests that the function named by <i>symbol</i> be
1622 called when the workspace is loaded (if the function is defined).
1623 </dd></dl>
1624 <p>To restore a workspace, use the command <code>load "workspace"</code>. This restores
1625 the values of the workspace variables to the values they had when
1626 <code>save-workspace</code> was last called. It also restores the documentation
1627 strings, if set, by <code>describe</code>. If you load two or more
1628 <code>workspace.lsp</code> files, the variables will be merged into a
1629 single workspace. The current set of workspace variables are saved in
1630 the list <code>*workspace*</code>. To clear the workspace, set <code>*workspace*</code>
1631 to <code>nil</code>. This does not delete any variables, but means that
1632 no variables will be saved by <code>save-workspace</code> until variables are
1633 added again.</p>
1634
1635 <p>Functions to be called are saved in the list <code>*workspace-actions*</code>.
1636 to clear the functions, set <code>*workspace-actions*</code> to <code>nil</code>.
1637 Restore functions to the list with <code>add-action-to-workspace</code>.</p>
1638 <a name = "184"><h4>Utility Functions</h4></a>
1639 <p>This chapter concludes with details of various utility functions for score
1640 manipulation.</p>
1641 <dl>
1642 <dt>
1643 <code>patternp(<a name="index1322"></a><i>expression</i>)</code> [SAL]<br>
1644
1645 <code>(patternp <i>expression</i>)</code> [LISP]</dt>
1646 <dd>Test if <i>expression</i> is
1647 an Xmusic pattern.<br><br>
1648 <dt><code>params-transpose(<a name="index1323"></a><i>params</i>, <i>keyword</i>,
1649 <i>amount</i>)</code> [SAL]<br>
1650
1651 <code>(params-transpose <i>params</i> <i>keyword</i> <i>amount</i>)</code> [LISP]</dt>
1652 <dd>Add a transposition amount to a score event parameter. The
1653 <i>params</i>
1654 parameter is a list of keyword/value pairs (not preceded by a function name).
1655 The <i>keyword</i> is the keyword of the value to be altered, and <i>amount</i>
1656 is a number to be added to the value. If no matching keyword is present
1657 in <i>params</i>, then <i>params</i> is returned. Otherwise, a new parameter
1658 list is constructed and returned. The original <i>params</i> is not changed.<br><br>
1659 <dt><code>params-scale(<a name="index1324"></a><i>params</i>, <i>keyword</i>,
1660 <i>amount</i>)</code> [SAL]<br>
1661
1662 <code>(params-scale <i>params</i> <i>keyword</i> <i>amount</i>)</code> [LISP]</dt>
1663 <dd>Scale a score event parameter by some factor. This is like
1664 <code>params-transpose</code>, only using multiplication. The <i>params</i>
1665 list is a list of
1666 keyword/value pairs, <i>keyword</i> is the parameter keyword,
1667 and <i>amount</i> is the scale factor. <br><br>
1668 <dt><code>interpolate(<a name="index1325"></a><a name="index1326"></a><i>x</i>, <i>x1</i>, <i>y1</i>, <i>x2</i>, <i>y2</i>)</code> [SAL]<br>
1669
1670 <code>(interpolate <i>x</i> <i>x1</i> <i>y1</i> <i>x2</i> <i>y2</i>)</code> [LISP]</dt>
1671 <dd>Linearly interpolate (or extrapolate)
1672 between points
1673 (<i>x1</i>, <i>y1</i>) and (<i>x2</i>, <i>y2</i>) to compute the y value
1674 corresponding to <i>x</i>.<br><br>
1675 <dt><code>intersection(<a name="index1327"></a><a name="index1328"></a><i>a</i>,
1676 <i>b</i>)</code> [SAL]<br>
1677
1678 <code>(intersection <i>a</i> <i>b</i>)</code> [LISP]</dt>
1679 <dd>Compute the set intersection of lists <i>a</i> and <i>b</i>.<br><br>
1680 <dt><code>union(<a name="index1329"></a><a name="index1330"></a><i>a</i>, <i>b</i>)</code> [SAL]<br>
1681
1682 <code>(union <i>a</i> <i>b</i>)</code> [LISP]</dt>
1683 <dd>Compute
1684 the set union of lists <i>a</i> and <i>b</i>.<br><br>
1685 <dt><code>set-difference(<a name="index1331"></a><a name="index1332"></a><i>a</i>,
1686 <i>b</i>)</code> [SAL]<br>
1687
1688 <code>(set-difference <i>a</i> <i>b</i>)</code> [LISP]</dt>
1689 <dd>Compute the set of all elements that are in <i>a</i> but not in <i>b</i>.<br><br>
1690 <dt><code>subsetp(<a name="index1333"></a><a name="index1334"></a><i>a</i>, <i>b</i>)</code> [SAL]<br>
1691
1692 <code>(subsetp <i>a</i> <i>b</i>)</code> [LISP]</dt>
1693 <dd>Returns true iff
1694 <i>a</i> is a subset of <i>b</i>, that is, each element of <i>a</i> is a member
1695 of <i>b</i>.
1696 </dd></dl><hr>
1697 <a href = "part14.html">Previous Section</a> | <a href = "part16.html">Next Section</a> | <a href = "title.html#toc">Table of Contents</a> | <a href = "indx.html">Index</a> | <a href = "title.html">Title Page</a>
1698 </body></html>