| 1 |
<!DOCTYPE html> |
| 2 |
<html><head><title>Continuous Transformations and Time Warps</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 = "part4.html">Previous Section</a> | <a href = "part6.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 = "38"><h2>Continuous Transformations and Time Warps</h2></a> |
| 11 |
<p>Nyquist transformations were discussed in the previous chapter, but all of |
| 12 |
the examples used scalar values. For example, we saw the <code>loud</code> |
| 13 |
transformation used to change loudness by a fixed amount. What if we want |
| 14 |
to specify a crescendo, where the loudness changes gradually over time?</p> |
| 15 |
|
| 16 |
<p>It turns out that all transformations can accept signals as well as numbers, |
| 17 |
so transformations can be continuous over time. This raises some |
| 18 |
interesting questions about how to interpret continuous transformations. |
| 19 |
Should a loudness transformation apply to the internal details of a note or |
| 20 |
only affect the initial loudness? It might seem unnatural for a decaying |
| 21 |
piano note to perform a crescendo. On the other hand, a sustained |
| 22 |
trumpet sound should probably crescendo continuously. In the case of time |
| 23 |
warping (tempo changes), it might be best for a drum roll to maintain a |
| 24 |
steady rate, a trill may or may not change rates with tempo, and a run of |
| 25 |
sixteenth notes will surely change its rate.</p> |
| 26 |
|
| 27 |
<p>These issues are complex, and Nyquist cannot hope to automatically do the |
| 28 |
right thing in all cases. However, the concept of behavioral abstraction |
| 29 |
provides an elegant solution. Since transformations merely modify the |
| 30 |
environment, behaviors are not forced to implement any particular style of |
| 31 |
transformation. Nyquist is designed so that the default transformation is |
| 32 |
usually the right one, but it is always possible to override the default |
| 33 |
transformation to achieve a particular effect.</p> |
| 34 |
<a name = "39"><h3>Simple Transformations</h3></a> |
| 35 |
<p>The “simple” transformations affect some parameter, but have no effect on time itself. The simple transformations that support continuously changing parameters are: <code>sustain</code>, <code>loud</code>, and <code>transpose</code>.</p> |
| 36 |
|
| 37 |
<p>As a first example, Let us use <code>transpose</code> to create a chromatic scale. |
| 38 |
First define a sequence of tones at a steady pitch. The <code>seqrep</code> |
| 39 |
“function” works like <code>seq</code> except that it creates copies of a sound |
| 40 |
by evaluating an expression multiple times. Here, <code>i</code> takes on 16 values |
| 41 |
from 0 to 15, and the expression for the sound could potentially use <code>i</code>. |
| 42 |
Technically, <code>seqrep</code> is not really a function but an abbreviation for |
| 43 |
a special kind of loop construct. |
| 44 |
</p> |
| 45 |
<pre> |
| 46 |
define function tone-seq() |
| 47 |
return seqrep(i, 16, |
| 48 |
osc-note(c4) ~ 0.25) |
| 49 |
</pre> |
| 50 |
|
| 51 |
<p> |
| 52 |
Now define a linearly increasing ramp to serve as a transposition function: |
| 53 |
</p> |
| 54 |
<pre> |
| 55 |
define function pitch-rise() |
| 56 |
return sustain-abs(1.0, 16 * ramp() ~ 4) |
| 57 |
</pre> |
| 58 |
|
| 59 |
<p> |
| 60 |
This ramp has a duration of 4 seconds, and over that interval it rises from |
| 61 |
0 to 16 (corresponding to the 16 semitones we want to transpose). The ramp |
| 62 |
is inside a <code>sustain-abs</code> transformation, which prevents a <code>sustain</code> |
| 63 |
transformation from having any effect on the ramp. (One of the drawbacks of |
| 64 |
behavioral abstraction is that built-in behaviors sometimes do the wrong |
| 65 |
thing implicitly, requiring some explicit correction to turn off the |
| 66 |
unwanted transformation.) Now, |
| 67 |
<code>pitch-rise</code> is used to transpose <code>tone-seq</code>: |
| 68 |
</p> |
| 69 |
<pre> |
| 70 |
define function chromatic-scale() |
| 71 |
return transpose(pitch-rise(), tone-seq()) |
| 72 |
</pre> |
| 73 |
|
| 74 |
<p></p> |
| 75 |
|
| 76 |
<p>Similar transformations can be constructed to change the sustain or “duty |
| 77 |
factor” of notes and their loudness. The following expression plays the |
| 78 |
<code>chromatic-scale</code> behavior with increasing note durations. The |
| 79 |
rhythm is unchanged, but the note length changes from staccato to legato: |
| 80 |
</p> |
| 81 |
<pre> |
| 82 |
play sustain((0.2 + ramp()) ~ 4, |
| 83 |
chromatic-scale()) |
| 84 |
</pre> |
| 85 |
|
| 86 |
<p> |
| 87 |
The resulting sustain function will ramp from 0.2 to 1.2. A sustain of 1.2 |
| 88 |
denotes a 20 percent overlap between notes. The <code>sum</code> has a stretch |
| 89 |
factor of 4, so it will extend over the 4 second duration of |
| 90 |
<code>chromatic-scale</code>.</p> |
| 91 |
|
| 92 |
<p>If you try this, you will discover that the <code>chromatic-scale</code> no longer |
| 93 |
plays a chromatic scale. You will hear the first 4 notes going up in intervals |
| 94 |
of 5 semitones (perfect fourths) followed by repeated pitches. What |
| 95 |
is happening is that the <code>sustain</code> operation applies to |
| 96 |
<code>pitch-rise</code> in addition to <code>tone-seq</code>, so now the 4s |
| 97 |
ramp from 0 to 16 becomes a 0.8s ramp. To fix this problem, we need to |
| 98 |
shield <code>pitch-rise</code> from the effect of <code>sustain</code> using the |
| 99 |
<code>sustain-abs</code> transformation. Here is a corrected version of |
| 100 |
<code>chromatic-scale</code>: |
| 101 |
</p> |
| 102 |
<pre> |
| 103 |
define function chromatic-scale() |
| 104 |
return transpose(sustain-abs(1, pitch-rise()), tone-seq()) |
| 105 |
</pre> |
| 106 |
|
| 107 |
<p></p> |
| 108 |
|
| 109 |
<p>What do these transformations mean? How did the system know to produce a |
| 110 |
pitch rise rather than a continuous glissando? This all relates to the idea |
| 111 |
of behavioral abstraction. It is possible to design sounds that <i>do</i> |
| 112 |
glissando under the transpose transform, and you can even make sounds that |
| 113 |
<i>ignore</i> transpose altogether. As explained in Chapter |
| 114 |
<a href = "part4.html#27">Behavioral Abstraction</a>, the transformations modify the |
| 115 |
environment, and behaviors can reference the environment to determine what |
| 116 |
signals to generate. All built-in functions, such as <code>osc</code>, have a |
| 117 |
default behavior.</p> |
| 118 |
|
| 119 |
<p>The default behavior for sound primitives under <code>transpose</code>, |
| 120 |
<code>sustain</code>, and <code>loud</code> transformations is |
| 121 |
to sample the environment at the beginning of the |
| 122 |
note. Transposition is not quantized to semitones or any other scale, |
| 123 |
but in our example, we arranged for the transposition to work out to integer |
| 124 |
numbers of semitones, so we obtained a chromatic scale anyway.</p> |
| 125 |
|
| 126 |
<p>Transposition only applies to the oscillator and sampling primitives |
| 127 |
<code>osc</code>, <code>partial</code>, <code>sampler</code>, <code>sine</code>, <code>fmosc</code>, |
| 128 |
and <code>amosc</code>. Sustain applies to <code>osc</code>, <code>env</code>, <code>ramp</code>, |
| 129 |
and <code>pwl</code>. (Note that <code>partial</code>, <code>amosc</code>, and |
| 130 |
<code>fmosc</code> get their durations |
| 131 |
from the modulation signal, so they may indirectly depend upon the sustain.) |
| 132 |
Loud applies to <code>osc</code>, <code>sampler</code>, <code>cue</code>, <code>sound</code>, |
| 133 |
<code>fmosc</code>, and <code>amosc</code>. (But not <code>pwl</code> or <code>env</code>.)</p> |
| 134 |
<a name = "40"><h3>Time Warps</h3></a> |
| 135 |
<p>The most interesting transformations have to do with transforming time |
| 136 |
itself. The <code>warp</code> transformation provides a mapping function from |
| 137 |
logical (score) time to real time. The slope of this function tells us how |
| 138 |
many units of real time are covered by one unit of score time. This is |
| 139 |
proportional to 1/tempo. A higher slope corresponds to a slower tempo.</p> |
| 140 |
|
| 141 |
<p>To demonstrate <code>warp</code>, we will define a time warp function using |
| 142 |
<code>pwl</code>: |
| 143 |
</p> |
| 144 |
<pre> |
| 145 |
define function warper() |
| 146 |
return pwl(0.25, .4, .75, .6, 1.0, 1.0, 2.0, 2.0, 2.0) |
| 147 |
</pre> |
| 148 |
|
| 149 |
<p> |
| 150 |
This function has an initial slope of .4/.25 = 1.6. It may be easier to |
| 151 |
think in reciprocal terms: the initial tempo is .25/.4 = .625. Between 0.25 |
| 152 |
and 0.75, the tempo is .5/.2 = 2.5, and from 0.75 to 1.0, the tempo is again |
| 153 |
.625. It is important for warp functions to completely span the interval of |
| 154 |
interest (in our case it will be 0 to 1), and it is safest to extend a bit |
| 155 |
beyond the interval, so we extend the function on to 2.0 with a |
| 156 |
tempo of 1.0. Next, we stretch and scale the <code>warper</code> function to |
| 157 |
cover 4 seconds of score time and 4 seconds of real time: |
| 158 |
</p> |
| 159 |
<pre> |
| 160 |
define function warp4() |
| 161 |
return 4 * warper() ~ 4 |
| 162 |
</pre> |
| 163 |
|
| 164 |
<p></p> |
| 165 |
<hr> |
| 166 |
<blockquote></blockquote>) |
| 167 |
|
| 168 |
<img src="fig2.gif"><br><br> |
| 169 |
|
| 170 |
<p><b>Figure 2: </b>The result of <code>(warp4)</code>, intended to map 4 seconds of |
| 171 |
score time into 4 seconds of real time. The function extends beyond 4 |
| 172 |
seconds (the dashed lines) to make sure the function is well-defined at |
| 173 |
location (4, 4). Nyquist sounds are ordinarily open on the right. |
| 174 |
|
| 175 |
</p> |
| 176 |
<hr> |
| 177 |
<p>Figure <a href = "#40">2</a> shows a plot of this warp function. Now, we can |
| 178 |
warp the tempo of the <code>tone-seq</code> defined above using <code>warp4</code>: |
| 179 |
</p> |
| 180 |
<pre> |
| 181 |
play warp(warp4(), tone-seq()) |
| 182 |
</pre> |
| 183 |
|
| 184 |
<p> |
| 185 |
Figure <a href = "#40">3</a> shows the result graphically. Notice that the |
| 186 |
durations of the tones are warped as well as their onsets. Envelopes are |
| 187 |
not shown in detail in the figure. Because of the way <code>env</code> is |
| 188 |
defined, the tones will have constant attack and decay times, and the |
| 189 |
sustain will be adjusted to fit the available time.</p> |
| 190 |
<hr> |
| 191 |
<blockquote> |
| 192 |
</blockquote> |
| 193 |
<img src="fig3.gif"><br><br> |
| 194 |
|
| 195 |
<p><b>Figure 3: </b>When <code>(warp4)</code> is applied to <code>(tone-seq-2)</code>, the note onsets and durations are warped. |
| 196 |
|
| 197 |
</p> |
| 198 |
<hr><a name = "41"><h3>Abstract Time Warps</h3></a> |
| 199 |
<p>We have seen a number of examples where the default behavior did the |
| 200 |
“right thing,” making the code straightforward. This is not always the |
| 201 |
case. Suppose we want to warp the note onsets but not the durations. We |
| 202 |
will first look at an incorrect solution and discuss the error. Then we |
| 203 |
will look at a slightly more complex (but correct) solution. </p> |
| 204 |
|
| 205 |
<p>The default behavior for most Nyquist built-in functions is to sample the |
| 206 |
time warp function at the nominal starting and ending score times of the |
| 207 |
primitive. For many built-in functions, including <code>osc</code>, the starting |
| 208 |
logical time is 0 and the ending logical time is 1, so the time warp |
| 209 |
function is evaluated at these points to yield real starting and stopping |
| 210 |
times, say 15.23 and 16.79. The difference (e.g. 1.56) becomes the signal |
| 211 |
duration, and there is no internal time warping. The <code>pwl</code> function |
| 212 |
behaves a little differently. Here, each breakpoint is warped individually, |
| 213 |
but the resulting function is linear between the breakpoints.</p> |
| 214 |
|
| 215 |
<p>A consequence of the default behavior is that notes stretch when the tempo |
| 216 |
slows down. Returning to our example, recall that we want to warp only the |
| 217 |
note onset times and not the duration. One would think that the following |
| 218 |
would work: |
| 219 |
</p> |
| 220 |
<pre> |
| 221 |
define function tone-seq-2 () |
| 222 |
return seqrep(i, 16, |
| 223 |
osc-note(c4) ~~ 0.25) |
| 224 |
|
| 225 |
play warp(warp4(), tone-seq-2()) |
| 226 |
</pre> |
| 227 |
|
| 228 |
<p> |
| 229 |
Here, we have redefined <code>tone-seq</code>, renaming it to <code>tone-seq-2</code> |
| 230 |
and changing the stretch (<code>~</code>) to absolute stretch (<code>~~</code>). The |
| 231 |
absolute stretch should override the warp function and produce a fixed |
| 232 |
duration.</p> |
| 233 |
|
| 234 |
<p>If you play the example, you will hear steady |
| 235 |
sixteenths and no tempo changes. What is wrong? In a sense, the “fix” |
| 236 |
works too well. Recall that sequences (including <code>seqrep</code>) determine |
| 237 |
the starting time of the next note from the logical stop time of the |
| 238 |
previous sound in the sequence. When we forced the stretch to 0.25, we also |
| 239 |
forced the logical stop time to 0.25 real seconds from the beginning, so |
| 240 |
every note starts 0.25 seconds after the previous one, resulting in a |
| 241 |
constant tempo.</p> |
| 242 |
|
| 243 |
<p>Now let us design a proper solution. The trick is to use absolute |
| 244 |
stretch (<code>~~</code>) |
| 245 |
as before to control the duration, but to restore the logical stop time to a |
| 246 |
value that results in the proper inter-onset time interval: |
| 247 |
</p> |
| 248 |
<pre> |
| 249 |
define function tone-seq-3() |
| 250 |
return seqrep(i, 16, |
| 251 |
set-logical-stop(osc-note(c4) ~~ 0.25, 0.25)) |
| 252 |
|
| 253 |
play warp(warp4(), tone-seq-3()) |
| 254 |
</pre> |
| 255 |
|
| 256 |
<p> |
| 257 |
Notice the addition of <code>set-logical-stop</code> enclosing the |
| 258 |
absolute stretch (<code>~~</code>) expression to set the logical |
| 259 |
stop time. A possible |
| 260 |
point of confusion here is that the logical stop time is set to 0.25, the |
| 261 |
same number given to <code>~~</code>! How does setting the logical stop |
| 262 |
time to 0.25 result in a tempo change? When used within a <code>warp</code> |
| 263 |
transformation, the second argument to <code>set-logical-stop</code> refers to |
| 264 |
<i>score</i> time rather than <i>real</i> time. Therefore, the score duration of |
| 265 |
0.25 is warped into real time, producing tempo changes according to the |
| 266 |
enviroment. Figure <a href = "#41">4</a> illustrates the result graphically.</p> |
| 267 |
<hr> |
| 268 |
<blockquote> |
| 269 |
</blockquote> |
| 270 |
<img src="fig4.gif"><br><br> |
| 271 |
|
| 272 |
<p><b>Figure 4: </b>When <code>(warp4)</code> is applied |
| 273 |
to <code>(tone-seq-3)</code>, the note onsets are warped, but not the duration, |
| 274 |
which remains a constant 0.25 seconds. In the fast middle section, this |
| 275 |
causes notes to overlap. Nyquist will sum (mix) them. |
| 276 |
|
| 277 |
</p> |
| 278 |
<hr><a name = "42"><h3>Nested Transformations</h3></a> |
| 279 |
<p>Transformations can be nested. In particular, a simple transformation such |
| 280 |
as transpose can be nested within a time warp transformation. Suppose we |
| 281 |
want to warp our chromatic scale example with the <code>warp4</code> time warp |
| 282 |
function. As in the previous section, we will show an erroneous simple |
| 283 |
solution followed by a correct one.</p> |
| 284 |
|
| 285 |
<p>The simplest approach to a nested transformation is to simply combine them |
| 286 |
and hope for the best: |
| 287 |
</p> |
| 288 |
<pre> |
| 289 |
play warp(warp4(), |
| 290 |
transpose(pitch-rise(), tone-seq())) |
| 291 |
</pre> |
| 292 |
|
| 293 |
<p> |
| 294 |
This example will not work the way you might expect. Here is why: the warp |
| 295 |
transformation applies to the <code>(pitch-rise)</code> expression, which is |
| 296 |
implemented using the <code>ramp</code> function. The default |
| 297 |
behavior of <code>ramp</code> is to interpolate linearly (in real time) between two points. |
| 298 |
Thus, the “warped” <code>ramp</code> function will not truly reflect the internal |
| 299 |
details of the intended time warp. When the notes are moving faster, they |
| 300 |
will be closer together in pitch, and the result is not chromatic. |
| 301 |
What we need is a way to properly |
| 302 |
compose the warp and ramp functions. If we continuously warp the ramp function |
| 303 |
in the same way as the note sequence, a chromatic scale should be obtained. |
| 304 |
This will lead to a correct solution.</p> |
| 305 |
|
| 306 |
<p>Here is the modified code to properly warp a transposed sequence. Note that |
| 307 |
the original sequence is used without modification. The only complication |
| 308 |
is producing a properly warped transposition function: |
| 309 |
</p> |
| 310 |
<pre> |
| 311 |
play warp(warp4(), |
| 312 |
transpose( |
| 313 |
control-warp(get-warp(), |
| 314 |
warp-abs(nil, pitch-rise())), |
| 315 |
tone-seq())) |
| 316 |
</pre> |
| 317 |
|
| 318 |
<p> |
| 319 |
To properly warp the <code>pitch-rise</code> transposition function, we use |
| 320 |
<code>control-warp</code>, which applies a warp function to a function of score time, |
| 321 |
yielding a function of real time. We need to pass the desired function |
| 322 |
to <code>control-warp</code>, so we fetch it from the environment with |
| 323 |
<code>get-warp()</code>. Finally, since the warping is done here, we want to |
| 324 |
shield the <code>pitch-rise</code> expression from further warping, so we enclose |
| 325 |
it in <code>warp-abs(nil, ...)</code>.</p> |
| 326 |
<i>An aside:</i> This last example illustrates a difficulty in the design of |
| 327 |
Nyquist. To support behavioral abstraction universally, we must rely upon |
| 328 |
behaviors to “do the right thing.” In this case, we would like the |
| 329 |
<code>ramp</code> function to warp continuously according to the environment. But |
| 330 |
this is inefficient and unnecessary in many other cases where <code>ramp</code> |
| 331 |
and especially <code>pwl</code> are used. (<code>pwl</code> warps its breakpoints, but still interpolates linearly between them.) Also, if the default behavior of |
| 332 |
primitives is to warp in a continuous manner, this makes it difficult to |
| 333 |
build custom abstract behaviors. The final vote is not in.<hr> |
| 334 |
<a href = "part4.html">Previous Section</a> | <a href = "part6.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> |
| 335 |
</body></html> |