Previous Section | Next Section | Table of Contents | Index | Title Page

Continuous Transformations and Time Warps

Nyquist transformations were discussed in the previous chapter, but all of the examples used scalar values. For example, we saw the loud transformation used to change loudness by a fixed amount. What if we want to specify a crescendo, where the loudness changes gradually over time?

It turns out that all transformations can accept signals as well as numbers, so transformations can be continuous over time. This raises some interesting questions about how to interpret continuous transformations. Should a loudness transformation apply to the internal details of a note or only affect the initial loudness? It might seem unnatural for a decaying piano note to perform a crescendo. On the other hand, a sustained trumpet sound should probably crescendo continuously. In the case of time warping (tempo changes), it might be best for a drum roll to maintain a steady rate, a trill may or may not change rates with tempo, and a run of sixteenth notes will surely change its rate.

These issues are complex, and Nyquist cannot hope to automatically do the right thing in all cases. However, the concept of behavioral abstraction provides an elegant solution. Since transformations merely modify the environment, behaviors are not forced to implement any particular style of transformation. Nyquist is designed so that the default transformation is usually the right one, but it is always possible to override the default transformation to achieve a particular effect.

Simple Transformations

The "simple" transformations affect some parameter, but have no effect on time itself. The simple transformations that support continuously changing parameters are: sustain, loud, and transpose.

As a first example, Let us use transpose to create a chromatic scale. First define a sequence of tones at a steady pitch: (defun tone-seq () (seqrep (i 16) (stretch 0.25 (osc-note c4)))) Now define a linearly increasing ramp to serve as a transposition function: (defun pitch-rise () (stretch 4.0 (scale 16 (ramp)))) This ramp has a duration of 4 seconds, and over that interval it rises from 0 to 16 (corresponding to the 16 semitones we want to transpose). Now, pitch-rise is used to transpose tone-seq: (defun chromatic-scale () (transpose (pitch-rise) (tone-seq)))

Similar transformations can be constructed to change the sustain or "duty factor" of notes and their loudness. The following expression plays the previously constructed chromatic scale with increasing note durations. The rhythm is unchanged, but the note length changes from staccato to legato: (sustain (stretch 4 (sum 0.2 (ramp))) (chromatic-scale)) The resulting sustain function will ramp from 0.2 to 1.2. A sustain of 1.2 denotes a 20 percent overlap between notes. The sum has a stretch factor of 4, so it will extend over the 4 second duration of chromatic-scale.

What do these transformations mean? How did the system know to produce a pitch rise rather than a continuous glissando? This all relates to the idea of behavioral abstraction. It is possible to design sounds that do glissando under the transpose transform, and you can even make sounds that ignore transpose altogether. As explained in Chapter "Behavioral Abstraction", the transformations modify the environment, and behaviors can reference the environment to determine what signals to generate. All built-in functions, such as osc, have a default behavior.

The default behavior for sound primitives under transpose, sustain, and loud transformations is to sample the environment at the beginning of the note. Transposition is not quantized to semitones or any other scale, but in our example, we arranged for the transposition to work out to integer numbers of semitones, so we got a chromatic scale.

Transposition only applies to the oscillator and sampling primitives osc, partial, sampler, sine, fmosc, and amosc. Sustain applies to osc, env, and pwl. (Note that amosc and fmosc get their durations from the modulation signal, so they may indirectly depend upon the sustain.) Loud applies to osc, sampler, cue, sound, fmosc, and amosc. (But not pwl or env.)

Time Warps

The most interesting transformations have to do with transforming time itself. The warp transformation provides a mapping function from logical (score) time to real time. The slope of this function tells us how many units of real time are covered by one unit of score time. This is proportional to 1/tempo. A higher slope corresponds to a slower tempo.

To demonstrate warp, we will define a time warp function using pwl:

(defun warper ()
  (pwl .25 .4 .75 .6 1.0 1.0 2.0 2.0 2.0))
This function has an initial slope of .4/.25 = 1.6. It may be easier to think in reciprocal terms: the initial tempo is .25/.4 = .625. Between 0.25 and 0.75, the tempo is .5/.2 = 2.5, and from 0.75 to 1.0, the tempo is again .625. It is important for warp functions to completely span the interval of interest (in our case it will be 0 to 1), and it is safest to extend a bit beyond the interval, so we extend the function on to 2.0 with a tempo of 1.0. Next, we stretch and scale the warper function to cover 4 seconds of score time and 4 seconds of real time:
(defun warp4 () (stretch 4 (scale 4 (warper))))

Figure 2: The result of (warp4), intended to map 4 seconds of score time into 4 seconds of real time. The function extends beyond 4 seconds (the dashed lines) to make sure the function is well-defined at location (4, 4). Nyquist sounds are ordinarily open on the right.

Figure 2 shows a plot of this warp function. Now, we can warp the tempo of the tone-seq defined above using warp4:

(play (warp (warp4) (tone-seq)))
Figure 3 shows the result graphically. Notice that the durations of the tones are warped as well as their onsets. Envelopes are not shown in detail in the figure. Because of the way env is defined, the tones will have constant attack and decay times, and the sustain will be adjusted to fit the available time.

Figure 3: When (warp4) is applied to (tone-seq-2), the note onsets and durations are warped.

Abstract Time Warps

We have seen a number of examples where the default behavior did the "right thing," making the code straightforward. This is not always the case. Suppose we want to warp the note onsets but not the durations. We will first look at an incorrect solution and discuss the error. Then we will look at a slightly more complex (but correct) solution.

The default behavior for most Nyquist built-in functions is to sample the time warp function at the nominal starting and ending score times of the primitive. For many built-in functions, including osc, the starting logical time is 0 and the ending logical time is 1, so the time warp function is evaluated at these points to yield real starting and stopping times, say 15.23 and 16.79. The difference (e.g. 1.56) becomes the signal duration, and there is no internal time warping. The pwl function behaves a little differently. Here, each breakpoint is warped individually, but the resulting function is linear between the breakpoints.

A consequence of the default behavior is that notes stretch when the tempo slows down. Returning to our example, recall that we want to warp only the note onset times and not the duration. One would think that the following would work:

(defun tone-seq-2 ()
  (seqrep (i 16)
	  (stretch-abs 0.25 (osc-note c4))))

(play (warp (warp4) (tone-seq-2)))

Here, we have redefined tone-seq, renaming it to tone-seq-2 and changing the stretch to stretch-abs. The stretch-abs should override the warp function and produce a fixed duration.

If you play the example, you will hear steady sixteenths and no tempo changes. What is wrong? In a sense, the "fix" works too well. Recall that sequences (including seqrep) determine the starting time of the next note from the logical stop time of the previous sound in the sequence. When we forced the stretch to 0.25, we also forced the logical stop time to 0.25 real seconds from the beginning, so every note starts 0.25 seconds after the previous one, resulting in a constant tempo.

Now let us design a proper solution. The trick is to use stretch-abs as before to control the duration, but to restore the logical stop time to a value that results in the proper inter-onset time interval:

(defun tone-seq-3 ()
  (seqrep (i 16)
             (stretch-abs 0.25 (osc-note c4))

(play (warp (warp4) (tone-seq-3)))

Notice the addition of set-logical-stop enclosing the stretch-abs expression to set the logical stop time. A possible point of confusion here is that the logical stop time is set to 0.25, the same number given to stretch-abs! How does setting the logical stop time to 0.25 result in a tempo change? When used within a warp transformation, the second argument to set-logical-stop refers to score time rather than real time. Therefore, the score duration of 0.25 is warped into real time, producing tempo changes according to the enviroment. Figure 4 illustrates the result graphically.

Figure 4: When (warp4) is applied to (tone-seq-3), the note onsets are warped, but not the duration, which remains a constant 0.25 seconds. In the fast middle section, this causes notes to overlap. Nyquist will sum (mix) them.

Nested Transformations

Transformations can be nested. In particular, a simple transformation such as transpose can be nested within a time warp transformation. Suppose we want to warp our chromatic scale example with the warp4 time warp function. As in the previous section, we will show an erroneous simple solution followed by a correct one.

The simplest approach to a nested transformation is to simply combine them and hope for the best:

(play (warp (warp4)
            (transpose (pitch-rise) (tone-seq))))
This example will not work the way you might expect. Here is why: the warp transformation applies to the (pitch-rise) expression, which is implemented using the ramp function. The default behavior of ramp is to interpolate linearly (in real time) between two points. Thus, the "warped" ramp function will not truly reflect the internal details of the intended time warp. What we need is a way to properly compose the warp and ramp functions. This will lead to a correct solution.

Here is the modified code to properly warp a transposed sequence. Note that the original sequence is used without modification. The only complication is producing a properly warped transposition function:

(play (warp (warp4)
                (control-warp (get-warp)
                              (warp-abs nil (pitch-rise)))
To properly warp the pitch-rise transposition function, we use control-warp, which applies a warp function to a function of score time, yielding a function of real time. We need to pass the warp desired function to control-warp, so we fetch it from the environment ith (get-warp). Finally, since the warping is done here, we want to shield the pitch-rise expression from further warping, so we enclose it in (warp-abs nil ...).

An aside: This last example illustrates a difficulty in the design of Nyquist. To support behavioral abstraction universally, we must rely upon behaviors to "do the right thing." In this case, we would like the ramp function to warp continuously according to the environment. But this is inefficient and unnecessary in many other cases where ramp and especially pwl are used. (pwl warps its breakpoints, but still interpolates linearly between them.) Also, if the default behavior of primitives is to warp in a continuous manner, this makes it difficult to build custom abstract behaviors. The final vote is not in.

Previous Section | Next Section | Table of Contents | Index | Title Page