| 1 |
<!DOCTYPE html> |
| 2 |
<html><head><title>Behavioral Abstraction</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 = "part3.html">Previous Section</a> | <a href = "part5.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 = "27"><h2>Behavioral Abstraction</h2></a><a name="index137"></a> |
| 11 |
<p>In Nyquist, all functions are subject to |
| 12 |
transformations<a name="index138"></a>. You can think of transformations as |
| 13 |
additional parameters to every function, and functions are free to use these |
| 14 |
additional parameters in any way. The set of transformation parameters is |
| 15 |
captured in what is referred to as the <i>transformation |
| 16 |
environment<a name="index139"></a></i>. (Note that the term |
| 17 |
<i>environment</i> is heavily overloaded in computer science. This is yet |
| 18 |
another usage of the term.)</p> |
| 19 |
|
| 20 |
<p>Behavioral abstraction is the ability of functions to adapt their behavior |
| 21 |
to the transformation environment. This environment may contain certain |
| 22 |
abstract notions, such as loudness, stretching a sound in time, etc. These |
| 23 |
notions will mean different things to different functions. For example, an |
| 24 |
oscillator should produce more periods of oscillation in order to stretch |
| 25 |
its output. An envelope, on the other hand, might only change the |
| 26 |
duration of the sustain portion of the envelope in order to stretch. |
| 27 |
Stretching a sample could mean resampling it to change its duration by the |
| 28 |
appropriate amount.</p> |
| 29 |
|
| 30 |
<p>Thus, transformations in Nyquist are not simply operations on signals. For |
| 31 |
example, if I want to stretch a note, it does not make sense to compute the |
| 32 |
note first and then stretch the signal. Doing so would cause a drop in the |
| 33 |
pitch. Instead, a transformation modifies the <i>transformation |
| 34 |
environment</i> in which the note is computed. Think of transformations as |
| 35 |
making requests to functions. It is up to the function to carry out the |
| 36 |
request. Since the function is always in complete control, it is possible |
| 37 |
to perform transformations with “intelligence;” that is, the function can |
| 38 |
perform an appropriate transformation, such as maintaining the desired pitch |
| 39 |
and stretching only the ”sustain” portion |
| 40 |
of an envelope to obtain a longer note.</p> |
| 41 |
<a name = "28"><h3>The Environment</h3></a><a name="index140"></a> |
| 42 |
<p>The transformation environment consists of a set of special variables. |
| 43 |
These variables should not be read directly and should <i>never</i> be set |
| 44 |
directly by the programmer. Instead, there are functions to read them, and |
| 45 |
they are automatically set and restored by |
| 46 |
transformation operators, which will be described below.</p> |
| 47 |
|
| 48 |
<p>The transformation environment consists of the following elements. Although |
| 49 |
each element has a “standard interpretation,” the designer of an |
| 50 |
instrument or the composer of a complex behavior is free to interpret the |
| 51 |
environment in any way. For example, a change in <code>*loud*</code> may change |
| 52 |
timbre more than amplitude, and <code>*transpose*</code> may be ignored by |
| 53 |
percussion instruments:</p> |
| 54 |
<dl> |
| 55 |
<dt> |
| 56 |
<code>*warp*<a name="index141"></a></code></dt> |
| 57 |
<dd>Time transformation, including time shift, |
| 58 |
time stretch, and continuous time warp. The value of <code>*warp*</code> is |
| 59 |
interpreted as a function from logical (local score) time to physical |
| 60 |
(global real) time. Do not access <code>*warp*</code> directly. Instead, use |
| 61 |
<code>local-to-global(<i>t</i>)</code> to |
| 62 |
convert from a logical (local) time to real (global) time. Most often, |
| 63 |
you will call <code>local-to-global(0)</code>. Several transformation operators |
| 64 |
operate on <code>*warp*</code>, including <i>at</i> (<code>@</code>), <i>stretch</i> (<code>~</code>), |
| 65 |
and <code>warp</code>. See also <code>get-duration()</code> and <code>get-warp()</code>.<br><br> |
| 66 |
<dt><code>*loud*<a name="index142"></a></code></dt> |
| 67 |
<dd>Loudness, expressed in decibels. The default |
| 68 |
(nominal) loudness is 0.0 dB (no change). Do not access <code>*loud*</code> |
| 69 |
directly. Instead, use <code>get-loud()</code> to get the current value of |
| 70 |
<code>*loud*</code> and either <code>loud</code> or <code>loud-abs</code> to modify it.<br><br> |
| 71 |
<dt><code>*transpose*<a name="index143"></a></code></dt> |
| 72 |
<dd>Pitch transposition, |
| 73 |
expressed in |
| 74 |
semitones. (Default: 0.0). Do not access <code>*transpose*</code> directly. |
| 75 |
Instead, use <code>get-transpose()</code> to get the current value of |
| 76 |
<code>*transpose*</code> and either <code>transpose</code> or <code>transpose-abs</code> to |
| 77 |
modify it.<br><br> |
| 78 |
<dt><code>*sustain*<a name="index144"></a></code></dt> |
| 79 |
<dd>The “sustain,” |
| 80 |
“articulation,” “duty factor,” or amount by which to |
| 81 |
separate or overlap sequential notes. For |
| 82 |
example, staccato might be expressed with a <code>*sustain*</code> of 0.5, while very |
| 83 |
legato playing might be expressed with a <code>*sustain*</code> of 1.2. |
| 84 |
Specifically, <code>*sustain*</code> stretches the duration of notes (sustain) |
| 85 |
without affecting the inter-onset time (the rhythm). Do not access |
| 86 |
<code>*sustain*</code> directly. Instead, use <code>get-sustain()</code> to get the |
| 87 |
current value of <code>*sustain*</code> and either <code>sustain</code> or |
| 88 |
<code>sustain-abs</code> to modify it.<br><br> |
| 89 |
<dt><code>*start*<a name="index145"></a></code></dt> |
| 90 |
<dd>Start |
| 91 |
time of a clipping region. <i>Note:</i> |
| 92 |
unlike the previous elements of the environment, <code>*start*</code> has a |
| 93 |
precise interpretation: no sound should be generated before <code>*start*</code>. |
| 94 |
This is implemented in all the low-level sound functions, so it can |
| 95 |
generally be ignored. You can read <code>*start*</code> directly, but use |
| 96 |
<code>extract</code> or <code>extract-abs</code> to modify it. <b><i>Note 2:</i></b> Due |
| 97 |
to some internal confusion between the specified starting time and |
| 98 |
the actual starting time of a signal after clipping, <code>*start*</code> |
| 99 |
is not fully implemented.<br><br> |
| 100 |
<dt><code>*stop*<a name="index146"></a></code></dt> |
| 101 |
<dd>Stop time of clipping |
| 102 |
region. By analogy to |
| 103 |
<code>*start*</code>, no sound should be generated after this time. |
| 104 |
<code>*start*</code> and |
| 105 |
<code>*stop*</code> allow a composer to preview a small section of a work |
| 106 |
without computing it from beginning to end. You can read <code>*stop*</code> |
| 107 |
directly, but use <code>extract</code> or <code>extract-abs</code> to modify it. |
| 108 |
<b><i>Note:</i></b> Due to some internal confusion between the specified |
| 109 |
starting time and the actual starting time of a signal after |
| 110 |
clipping, <code>*stop*</code> is not fully implemented.<br><br> |
| 111 |
<dt><code>*control-srate*<a name="index147"></a></code></dt> |
| 112 |
<dd>Sample |
| 113 |
rate of control signals. This environment |
| 114 |
element provides the default sample rate for control signals. There is no |
| 115 |
formal distinction between a control signal and an audio signal. |
| 116 |
You can read <code>*control-srate*</code> directly, but |
| 117 |
use <code>control-srate</code> or <code>control-srate-abs</code> to modify it.<br><br> |
| 118 |
<dt><code>*sound-srate*<a name="index148"></a></code></dt> |
| 119 |
<dd>Sample |
| 120 |
rate of musical sounds. This environment element provides the |
| 121 |
default sample rate for musical sounds. You can |
| 122 |
read <code>*sound-srate*</code> directly, but use <code>sound-srate</code> |
| 123 |
or <code>sound-srate-abs</code> to modify it. |
| 124 |
</dd></dl><a name = "29"><h3>Sequential Behavior</h3></a><a name="index149"></a> |
| 125 |
<p>Previous examples have shown the use of <code>seq</code>, the sequential behavior |
| 126 |
operator. We can now explain <code>seq</code> in terms of transformations. |
| 127 |
Consider the simple expression: |
| 128 |
</p> |
| 129 |
<pre> |
| 130 |
play seq(my-note(c4, q), my-note(d4, i)) |
| 131 |
</pre> |
| 132 |
|
| 133 |
<p> |
| 134 |
The idea is to create the first note at time 0, and to start the next |
| 135 |
note when the first one finishes. This is all accomplished by manipulating |
| 136 |
the environment. In particular, <code>*warp*</code> is modified so that what is |
| 137 |
locally time 0 for the second note is transformed, or warped, to the logical |
| 138 |
stop time of the first note.</p> |
| 139 |
|
| 140 |
<p>One way to understand this in detail is to imagine how it |
| 141 |
might be executed: first, <code>*warp*</code> is set to an initial value that has no |
| 142 |
effect on time, and <code>my-note(c4, q)</code> is evaluated. A sound is returned and |
| 143 |
saved. The sound has an ending time, which in this case will be <code>1.0</code> |
| 144 |
because the duration <code>q</code> is <code>1.0</code>. This ending time, <code>1.0</code>, |
| 145 |
is used to construct a new <code>*warp*</code> that has the effect of shifting |
| 146 |
time by 1.0. The second note is evaluated, and will start |
| 147 |
at time 1. The sound that is |
| 148 |
returned is now added to the first sound to form a composite sound, whose |
| 149 |
duration will be <code>2.0</code>. <code>*warp*</code> is restored to its initial value.</p> |
| 150 |
|
| 151 |
<p>Notice that the semantics of <code>seq</code> can be expressed in terms of |
| 152 |
transformations. To generalize, the operational rule for <code>seq</code> is: |
| 153 |
evaluate the first behavior according to the current <code>*warp*</code>. |
| 154 |
Evaluate each successive behavior with <code>*warp*</code> modified to shift the |
| 155 |
new note's starting time to the ending time of the previous behavior. |
| 156 |
Restore <code>*warp*</code> to its original value and return a sound which is the |
| 157 |
sum of the results. </p> |
| 158 |
|
| 159 |
<p>In the Nyquist implementation, audio samples are only computed when they are |
| 160 |
needed, and the second part of the <code>seq</code> is not evaluated until the |
| 161 |
ending time (called the logical stop time) of the first part. It is still |
| 162 |
the case that when the second part is evaluated, it will see <code>*warp*</code> |
| 163 |
bound to the ending time of the first part. </p> |
| 164 |
|
| 165 |
<p>A language detail: Even though Nyquist defers evaluation of the second part of the <code>seq</code>, the expression can reference variables according to ordinary |
| 166 |
Lisp/SAL scope rules. This is because the <code>seq</code> captures the expression in a closure, which retains all of the variable bindings.</p> |
| 167 |
<a name = "30"><h3>Simultaneous Behavior</h3></a><a name="index150"></a> |
| 168 |
<p>Another operator is <code>sim</code>, which invokes multiple behaviors at the same |
| 169 |
time. For example, |
| 170 |
</p> |
| 171 |
<pre> |
| 172 |
play 0.5 * sim(my-note(c4, q), my-note(d4, i)) |
| 173 |
</pre> |
| 174 |
|
| 175 |
<p> |
| 176 |
will play both notes starting at the same time.</p> |
| 177 |
|
| 178 |
<p>The operational rule for <code>sim</code> is: evaluate each behavior at the |
| 179 |
current <code>*warp*</code> and return the sum of the results. (In SAL, the |
| 180 |
<code>sim</code> function applied to sounds is equivalent to adding them |
| 181 |
with the infix <code>+</code> operator. The following section |
| 182 |
illustrates two concepts: first, a <i>sound</i> is not a |
| 183 |
<i>behavior</i>, and second, the <code>sim</code> operator and the <code>at</code> |
| 184 |
transformation can be used to place sounds in time.</p> |
| 185 |
<a name = "31"><h3>Sounds vs. Behaviors</h3></a><a name="index151"></a> |
| 186 |
<p>The following example loads a sound from a file in the current directory and stores it in <code>a-snd</code>: |
| 187 |
</p> |
| 188 |
<pre> |
| 189 |
<i>; load a sound |
| 190 |
;</i> |
| 191 |
set a-snd = s-read(strcat(current-path(), "demo-snd.aiff")) |
| 192 |
|
| 193 |
<i>; play it |
| 194 |
;</i> |
| 195 |
play a-snd |
| 196 |
</pre> |
| 197 |
|
| 198 |
<p></p> |
| 199 |
|
| 200 |
<p>One |
| 201 |
might then be tempted to write the following: |
| 202 |
</p> |
| 203 |
<pre> |
| 204 |
play seq(a-snd, a-snd) <i>; WRONG!</i> |
| 205 |
play seq(a-snd, a-snd @ 3) <i>; ALSO WRONG!</i> |
| 206 |
</pre> |
| 207 |
|
| 208 |
<p> |
| 209 |
Why is this wrong? Recall |
| 210 |
that <code>seq</code> works by modifying <code>*warp*</code>, not by operating on |
| 211 |
sounds. So, <code>seq</code> will proceed by evaluating <code>a-snd</code> with |
| 212 |
different values of <code>*warp*</code>. However, the result of evaluating |
| 213 |
<code>a-snd</code> (a variable) is always the same sound, regardless of the |
| 214 |
environment; in both of these cases, |
| 215 |
the second <code>a-snd</code> <i>should</i> start at time |
| 216 |
<code>0.0</code>, just like the first. After the first sound ends, |
| 217 |
Nyquist is unable to “back up” to time zero, so in fact, this <i>will</i> |
| 218 |
play two sounds in sequence, but that is a result of an implementation |
| 219 |
detail rather than correct program execution. The second case |
| 220 |
emphasizes this: Nyquist will return a sound duration of only 2 seconds |
| 221 |
because the <code>@</code> transform does not apply to a sound. |
| 222 |
In fact, a future version of |
| 223 |
Nyquist might (correctly) stop and report an error when it detects that the |
| 224 |
second sound in the sequence has a real start time that is before the |
| 225 |
requested one.</p> |
| 226 |
|
| 227 |
<p>How then do we obtain a sequence of two sounds properly? |
| 228 |
What we really need here is a |
| 229 |
behavior that transforms a given sound according to the current |
| 230 |
transformation environment. That job is performed by <code>cue</code>. For |
| 231 |
example, the following will behave as expected, producing a sequence of two |
| 232 |
sounds: |
| 233 |
</p> |
| 234 |
<pre> |
| 235 |
play seq(cue(a-snd), cue(a-snd)) <i>; CORRECT!</i> |
| 236 |
play seq(cue(a-snd), cue(a-snd) @ 3) <i>; CORRECT! Duration = 2 seconds</i> |
| 237 |
</pre> |
| 238 |
|
| 239 |
<p> |
| 240 |
These examples are correct because the second <code>cue</code> |
| 241 |
expressions will shift the sound |
| 242 |
stored in <code>a-snd</code> to start at the desired times.</p> |
| 243 |
|
| 244 |
<p>The lesson here is very important: <b><i>sounds are not behaviors!</i></b> Behaviors |
| 245 |
are computations that generate sounds according to the transformation |
| 246 |
environment. Once a sound has been generated, it can be stored, copied, |
| 247 |
added to other sounds, and used in many other operations, but sounds are |
| 248 |
<i>not</i> subject to transformations. To transform a sound, use <code>cue</code>, |
| 249 |
<code>sound</code>, or <code>control</code>. The differences between these operations |
| 250 |
are discussed later. For now, here is a “cue sheet” style score that |
| 251 |
plays 4 copies of <code>a-snd</code>:</p> |
| 252 |
|
| 253 |
<p></p> |
| 254 |
<pre> |
| 255 |
<i>; use sim and at to place sounds in time |
| 256 |
;</i> |
| 257 |
play sim(cue(a-snd) @ 0.0, |
| 258 |
cue(a-snd) @ 0.7, |
| 259 |
cue(a-snd) @ 1.0, |
| 260 |
cue(a-snd) @ 1.2) |
| 261 |
</pre> |
| 262 |
|
| 263 |
<p></p> |
| 264 |
<a name = "32"><h3>The At Transformation</h3></a><a name="index152"></a> |
| 265 |
<p>The second concept introduced by the previous example is the <code>@</code> |
| 266 |
operation, which shifts the <code>*warp*</code> component of the environment. For |
| 267 |
example, |
| 268 |
</p> |
| 269 |
<pre> |
| 270 |
cue(a-snd) @ 0.7 |
| 271 |
</pre> |
| 272 |
|
| 273 |
<p> |
| 274 |
can be explained operationally as follows: modify <code>*warp*</code> by shifting |
| 275 |
it by <code>0.7</code> and evaluate <code>cue(a-snd)</code>. Return the resulting sound |
| 276 |
after restoring <code>*warp*</code> to its original value. Notice how <code>@</code> |
| 277 |
is used inside a <code>sim</code> construct to locate copies of <code>a-snd</code> in |
| 278 |
time. This is the standard way to represent a note-list or a cue-sheet in |
| 279 |
Nyquist.</p> |
| 280 |
|
| 281 |
<p>This also explains why sounds need to be <code>cue</code>'d in order to be shifted |
| 282 |
in time or arranged in sequence. Imagine a different design where <code>sim</code> |
| 283 |
takes all of its parameters (a set of sounds) and lines them up to start |
| 284 |
at the same time. That would make it almost impossible to arrange sounds |
| 285 |
with different start times. You might try using <code>@</code> and write |
| 286 |
<code>cue(a-snd) @ 0.7</code>, but this expression just creates a particular sound, |
| 287 |
and we are assuming that <code>sim</code> aligns <i>all</i> sounds to start together, |
| 288 |
so this design of <code>sim</code> would “undo” the shift (<code>@</code>)! |
| 289 |
This design is no good, so in Nyquist, <code>sim</code> respects the intrinsic |
| 290 |
starting times of sounds. This creates a new problem when you want to reuse |
| 291 |
an existing sound, except you want it to start at a different time. The solution is |
| 292 |
the <code>cue</code> operation, which makes a copy of the sound with the starting time |
| 293 |
determined by the environment. (This is a virtual copy that avoids memory allocation |
| 294 |
or actually copying samples.)</p> |
| 295 |
<a name = "33"><h3>The Stretch Transformation</h3></a><a name="index153"></a> |
| 296 |
<p>In addition to At (denoted in SAL by the <code>@</code> operator, the Stretch |
| 297 |
transformation is very important. It appeared in the introduction, and |
| 298 |
it is denoted in SAL by the <code>~</code> operator (or in LISP by the <code>stretch</code> |
| 299 |
special form). Stretch also operates on the <code>*warp*</code> component of |
| 300 |
the environment. For example, |
| 301 |
</p> |
| 302 |
<pre> |
| 303 |
osc(c4) ~ 3 |
| 304 |
</pre> |
| 305 |
|
| 306 |
<p> |
| 307 |
does the following: modify <code>*warp*</code>, scaling the degree of |
| 308 |
"stretch" by 3, and evaluate <code>osc(c4)</code>. The <code>osc</code> behavior |
| 309 |
uses the stretch factor to determime the duration, so it will return |
| 310 |
a sound that is 3 seconds long. Restore <code>*warp*</code> to its original |
| 311 |
value. Like At, Stretch only affects behaviors. <code>a-snd ~ 10</code> is |
| 312 |
equivalent to <code>a-snd</code> because <code>a-snd</code> is a sound, not a |
| 313 |
behavior. Behaviors are functions that compute sounds according to |
| 314 |
the environment and return a sound.</p> |
| 315 |
<a name = "34"><h3>Nested Transformations</h3></a><a name="index154"></a> |
| 316 |
<p>Transformations can be combined using nested expressions. For example, |
| 317 |
</p> |
| 318 |
<pre> |
| 319 |
sim(cue(a-snd), |
| 320 |
loud(6.0, cue(a-snd) @ 3)) |
| 321 |
</pre> |
| 322 |
|
| 323 |
<p> |
| 324 |
scales the amplitude as well as shifts the second entrance of <code>a-snd</code>.</p> |
| 325 |
|
| 326 |
<p>Why use <code>loud</code> instead of simply multiplying <code>a-snd</code> by some |
| 327 |
scale factor? Using <code>loud</code> gives |
| 328 |
the behavior the chance to implement the abstract |
| 329 |
property <i>loudness</i> in an appropriate way, e.g. by including timbral |
| 330 |
changes. In this case, the behavior is <code>cue</code>, which implements |
| 331 |
<i>loudness</i> by simple amplitude scaling, so the result is equivalent |
| 332 |
to multiplication by <code>db-to-linear(6.0</code>).</p> |
| 333 |
|
| 334 |
<p>Transformations can also be applied to groups of behaviors: |
| 335 |
</p> |
| 336 |
<pre> |
| 337 |
loud(6.0, sim(cue(a-snd) @ 0.0, |
| 338 |
cue(a-snd) @ 0.7)) |
| 339 |
</pre> |
| 340 |
|
| 341 |
<p></p> |
| 342 |
<a name = "35"><h3>Defining Behaviors</h3></a><a name="index155"></a> |
| 343 |
<p>Groups of behaviors can be named using <code>define</code> (we already saw this |
| 344 |
in the definitions of <code>my-note</code> and <code>env-note</code>). Here is another example |
| 345 |
of a behavior definition and its use. The definition has one parameter: |
| 346 |
</p> |
| 347 |
<pre> |
| 348 |
define function snds(dly) |
| 349 |
return sim(cue(a-snd) @ 0.0, |
| 350 |
cue(a-snd) @ 0.7, |
| 351 |
cue(a-snd) @ 1.0, |
| 352 |
cue(a-snd) @ (1.2 + dly)) |
| 353 |
|
| 354 |
play snds(0.1) |
| 355 |
play loud(0.25, snds(0.3) ~ 0.9) |
| 356 |
</pre> |
| 357 |
|
| 358 |
<p> |
| 359 |
In the last line, <code>snds</code> is transformed: the transformations will apply |
| 360 |
to the <code>cue</code> behaviors within <code>snds</code>. The <code>loud</code> |
| 361 |
transformation will scale the sounds by <code>0.25</code>, and the <i>stretch</i> |
| 362 |
(<code>~</code>) will |
| 363 |
apply to the shift (<code>@</code>) amounts <code>0.0</code>, <code>0.7</code>, <code>1.0</code>, |
| 364 |
and <code>1.2 + dly</code>. The sounds themselves (copies of <code>a-snd</code>) will |
| 365 |
not be stretched because <code>cue</code> never stretches sounds.</p> |
| 366 |
|
| 367 |
<p>Section <a href = "part8.html#98">Transformations</a> describes the full set of transformations.</p> |
| 368 |
<a name = "36"><h3>Overriding Default Transformations</h3></a> |
| 369 |
<p>In Nyquist, behaviors are <i>the</i> important abstraction mechanism. |
| 370 |
A behavior represents a class of related functions or sounds. For example, |
| 371 |
a behavior can represent a musical note. When a note is stretched, it |
| 372 |
usually means that the tone sustains for more oscillations, but if the |
| 373 |
“note” is a drum roll, the note sustains by more repetitions of the |
| 374 |
component drum strokes. The concept of sustain is so fundamental that |
| 375 |
we do not really think of different note durations as being different |
| 376 |
instances of an abstract behavior, but in a music programming language, |
| 377 |
we need a way to model these abtract behaviors. As the tone and drum |
| 378 |
roll examples show, there is no one right way to “stretch,” so the |
| 379 |
language must allow users to define exactly what it means to stretch. |
| 380 |
By extension, the Nyquist programmer can define how all of the |
| 381 |
transformations affect different behaviors.</p> |
| 382 |
|
| 383 |
<p>To make programming easier, almost all Nyquist sounds are constructed |
| 384 |
from primitive behaviors that obey the environment in obvious ways: |
| 385 |
Stretch transformations make things longer and At transformations shift |
| 386 |
things in time. But sometimes you have to override the default behaviors. |
| 387 |
Maybe the attack phase of an envelope should not stretch when the note |
| 388 |
is stretched, or maybe when you stretch a trill, you should get more |
| 389 |
notes rather than a slower trill.</p> |
| 390 |
|
| 391 |
<p>To override default behaviors, you almost always follow the same |
| 392 |
programming pattern: first, capture the environment in a local variable; |
| 393 |
then, use one of the absolute transformations to “turn off” the |
| 394 |
environment's effect and compute the sound as desired. The following |
| 395 |
example creates a very simple envelope with a fixed rise time to |
| 396 |
illustrate the technique. |
| 397 |
</p> |
| 398 |
<pre> |
| 399 |
define function two-phase-env(rise-time) |
| 400 |
begin |
| 401 |
with dur = get-duration(1) |
| 402 |
return pwl(rise-time, 1, dur) ~~ 1.0 |
| 403 |
end |
| 404 |
</pre> |
| 405 |
|
| 406 |
<p> |
| 407 |
To “capture the environment in a local variable,” a <code>with</code> |
| 408 |
construct is used to create the local variable <code>dur</code> and set |
| 409 |
it to the value of <code>get-duration(1)</code>, which answers the question: |
| 410 |
“If I apply use the environment to stretch something whose nominal |
| 411 |
duration is 1, what is the resulting duration?” (Since time transformations |
| 412 |
can involve continuous time deformations, this question is not as |
| 413 |
simple as it may sound, so please use the provided function rather |
| 414 |
than peeking inside the <code>*warp*</code> structure and trying to do it |
| 415 |
yourself.) Next, we “turn off” stretching using the <code>stretch-abs</code> |
| 416 |
form, which in SAL is denoted by the <code>~~</code> operator. |
| 417 |
Finally, we are ready to compute the envelope using <code>pwl</code>. Here, |
| 418 |
we use absolute durations. The first breakpoint is at <code>rise-time</code>, |
| 419 |
so the attack time is given by the <code>rise-time</code> parameter. The |
| 420 |
<code>pwl</code> decays back to zero at time <code>dur</code>, so the overall |
| 421 |
duration matches the duration expected from the environment encountered |
| 422 |
by this instance of <code>two-phase-env</code>. Note, however, that since |
| 423 |
the <code>pwl</code> is evaluated in a different environment established |
| 424 |
by <code>~~</code>, it is not stretched (or perhaps more accurately, it is |
| 425 |
stretched by 1.0). This is good because it means <code>rise-time</code> will |
| 426 |
not be stretched, but we must be careful to extend the envelope to |
| 427 |
<code>dur</code> so that it has the expected duration.</p> |
| 428 |
<a name = "37"><h3>Sampling Rates</h3></a><a name="index156"></a> |
| 429 |
<p>The global environment contains <code>*sound-srate*</code> and |
| 430 |
<code>*control-srate*</code>, which determine the sample rates of sounds and |
| 431 |
control signals. These can be overridden at any point by the |
| 432 |
transformations <code>sound-srate-abs</code> and <code>control-srate-abs</code>; for |
| 433 |
example, |
| 434 |
</p> |
| 435 |
<pre> |
| 436 |
sound-srate-abs(44100.0, osc(c4)) |
| 437 |
</pre> |
| 438 |
|
| 439 |
<p> |
| 440 |
will compute a tone using a 44.1Khz sample rate even if the default rate |
| 441 |
is set to something different.</p> |
| 442 |
<a name="index157"></a> |
| 443 |
As with other components of the environment, you should <i>never</i> change |
| 444 |
<code>*sound-srate*</code> or <code>*control-srate*</code> directly. |
| 445 |
The global environment is determined by two additional |
| 446 |
variables: <code>*default-sound-srate*</code> and <code>*default-control-srate*</code>. |
| 447 |
You can add lines like the following to your <code>init.lsp</code> file to change |
| 448 |
the default global environment: |
| 449 |
|
| 450 |
<p></p> |
| 451 |
<pre> |
| 452 |
(setf *default-sound-srate* 44100.0) |
| 453 |
(setf *default-control-srate* 1102.5) |
| 454 |
</pre> |
| 455 |
|
| 456 |
<p> |
| 457 |
You can also do this using preferences in NyquistIDE. |
| 458 |
If you have already started Nyquist and want to change the defaults, the |
| 459 |
preferences or the following functions can be used: |
| 460 |
</p> |
| 461 |
<pre> |
| 462 |
exec set-control-srate(1102.5)<a name="index158"></a> |
| 463 |
exec set-sound-srate(22050.0)<a name="index159"></a> |
| 464 |
</pre> |
| 465 |
|
| 466 |
<p> |
| 467 |
These modify the default values and reinitialize the Nyquist environment.</p> |
| 468 |
<hr> |
| 469 |
<a href = "part3.html">Previous Section</a> | <a href = "part5.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> |
| 470 |
</body></html> |