| 1 |
<!DOCTYPE html> |
| 2 |
<html><head><title>More Examples</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 = "part5.html">Previous Section</a> | <a href = "part7.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 = "43"><h2>More Examples</h2></a> |
| 11 |
<p>This chapter explores Nyquist through additional examples. The reader may |
| 12 |
wish to browse through these and move on to Chapter <a href = "part8.html#82">Nyquist Functions</a>, which |
| 13 |
is a reference section describing Nyquist functions.</p> |
| 14 |
<a name = "44"><h3>Stretching Sampled Sounds</h3></a><a name="index160"></a> |
| 15 |
<p>This example illustrates how to stretch a sound, resampling it in the process. |
| 16 |
Because sounds in Nyquist are <i>values</i> that contain the sample rate, start |
| 17 |
time, etc., use <code>sound</code> to convert a sound into a behavior that can be |
| 18 |
stretched, e.g. <code>sound(a-snd)</code>. This behavior stretches a sound according |
| 19 |
to the stretch factor in the environment, set using <code>stretch</code>. For |
| 20 |
accuracy and efficiency, Nyquist does not resample a stretched sound until |
| 21 |
absolutely necessary. The <code>force-srate</code> function is used to resample |
| 22 |
the result so that we end up with a “normal” sample rate that is playable |
| 23 |
on ordinary sound cards.</p> |
| 24 |
|
| 25 |
<p></p> |
| 26 |
<pre> |
| 27 |
<i>; if a-snd is not loaded, load sound sample: |
| 28 |
;</i> |
| 29 |
if not(boundp(quote(a-snd))) then |
| 30 |
set a-snd = s-read("demo-snd.aiff") |
| 31 |
</pre> |
| 32 |
|
| 33 |
<p> |
| 34 |
</p> |
| 35 |
<pre> |
| 36 |
<i>; the SOUND operator shifts, stretches, clips and scales |
| 37 |
; a sound according to the current environment |
| 38 |
;</i> |
| 39 |
define function ex23() |
| 40 |
play force-srate(*default-sound-srate*, sound(a-snd) ~ 3.0) |
| 41 |
|
| 42 |
define function down() |
| 43 |
return force-srate(*default-sound-srate*, |
| 44 |
seq(sound(a-snd) ~ 0.2, |
| 45 |
sound(a-snd) ~ 0.3, |
| 46 |
sound(a-snd) ~ 0.4, |
| 47 |
sound(a-snd) ~ 0.6)) |
| 48 |
play down() |
| 49 |
</pre> |
| 50 |
|
| 51 |
<p> |
| 52 |
</p> |
| 53 |
<pre> |
| 54 |
<i>; that was so much fun, let's go back up: |
| 55 |
;</i> |
| 56 |
define function up() |
| 57 |
return force-srate(*default-sound-srate*, |
| 58 |
seq(sound(a-snd) ~ 0.5, |
| 59 |
sound(a-snd) ~ 0.4, |
| 60 |
sound(a-snd) ~ 0.3, |
| 61 |
sound(a-snd) ~ 0.2)) |
| 62 |
</pre> |
| 63 |
|
| 64 |
<p> |
| 65 |
</p> |
| 66 |
<pre> |
| 67 |
<i>; and write a sequence |
| 68 |
;</i> |
| 69 |
play seq(down(), up(), down()) |
| 70 |
</pre> |
| 71 |
|
| 72 |
<p></p> |
| 73 |
|
| 74 |
<p>Notice the use of the <code>sound</code> behavior as opposed to <code>cue</code>. The |
| 75 |
<code>cue</code> behavior shifts and scales its sound according to <code>*warp*</code> |
| 76 |
and <code>*loud*</code>, but it does not change the duration or resample the |
| 77 |
sound. In contrast, <code>sound</code> not only shifts and scales its sound, but |
| 78 |
it also stretches it by resampling or changing the effective sample rate |
| 79 |
according to <code>*warp*</code>. If |
| 80 |
<code>*warp*</code> is a continuous warping function, then the sound will be |
| 81 |
stretched by time-varying amounts. |
| 82 |
(The <code>*transpose*</code> element of the environment is |
| 83 |
ignored by both <code>cue</code> and <code>sound</code>.) </p> |
| 84 |
<b><i>Note:</i></b> <code>sound</code> may use linear interpolation rather than a high-quality resampling algorithm. In some cases, this may introduce errors audible as noise. Use <code>resample</code> (see Section <a href = "part8.html#90">Sound Synthesis</a>) for high-quality interpolation. |
| 85 |
<p>In the functions <code>up</code> and <code>down</code>, the <code>*warp*</code> is set by |
| 86 |
<i>stretch</i> (<code>~</code>), which simply scales time by a constant scale factor. In this case, |
| 87 |
<code>sound</code> can “stretch” the signal simply by changing the sample rate without |
| 88 |
any further computation. When <code>seq</code> tries to add the signals together, it |
| 89 |
discovers the sample rates do not match and uses linear interpolation to adjust |
| 90 |
all sample rates to match that of the first sound in the sequence. The result of |
| 91 |
<code>seq</code> is then converted using <code>force-srate</code> to convert the sample rate, |
| 92 |
again using linear interpolation. |
| 93 |
It would be slightly better, from a computational |
| 94 |
standpoint, to apply <code>force-srate</code> individually |
| 95 |
to each stretched sound rather |
| 96 |
than applying <code>force-srate</code> after <code>seq</code>.</p> |
| 97 |
|
| 98 |
<p>Notice that the overall duration of <code>sound(a-snd) ~ 0.5</code> will |
| 99 |
be half the duration of <code>a-snd</code>.</p> |
| 100 |
<a name = "45"><h3>Saving Sound Files</h3></a><a name="index161"></a> |
| 101 |
<p>So far, we have used the <code>play</code> command to play a sound. The |
| 102 |
<code>play</code> command works by writing a sound to a file while |
| 103 |
simultaneously playing it. |
| 104 |
This can be done one step at a time, and |
| 105 |
it is often convenient to save a sound to a particular file for later use: |
| 106 |
</p> |
| 107 |
<pre> |
| 108 |
<i>; write the sample to a file, |
| 109 |
; the file name can be any Unix filename. Prepending a "./" tells |
| 110 |
; s-save to not prepend *default-sf-dir* |
| 111 |
;</i> |
| 112 |
exec s-save(a-snd, 1000000000, "./a-snd-file.snd") |
| 113 |
</pre> |
| 114 |
|
| 115 |
<p> |
| 116 |
</p> |
| 117 |
<pre> |
| 118 |
<i>; play a file |
| 119 |
; play command normally expects an expression for a sound |
| 120 |
; but if you pass it a string, it will open and play a |
| 121 |
; sound file</i> |
| 122 |
play "./a-snd-file.snd" |
| 123 |
</pre> |
| 124 |
|
| 125 |
<p> |
| 126 |
</p> |
| 127 |
<pre> |
| 128 |
<i>; delete the file (do this with care!) |
| 129 |
; only works under Unix (not Windows)</i> |
| 130 |
exec system("rm ./a-snd-file.snd") |
| 131 |
</pre> |
| 132 |
|
| 133 |
<p> |
| 134 |
</p> |
| 135 |
<pre> |
| 136 |
<i>; now let's do it using a variable as the file name |
| 137 |
;</i> |
| 138 |
set my-sound-file = "./a-snd-file.snd" |
| 139 |
exec s-save(a-snd, 1000000000, my-sound-file) |
| 140 |
</pre> |
| 141 |
|
| 142 |
<p> |
| 143 |
</p> |
| 144 |
<pre> |
| 145 |
<i>; play-file is a function to open and play a sound file</i> |
| 146 |
exec play-file(my-sound-file) |
| 147 |
exec system(strcat("rm ", my-sound-file)) |
| 148 |
</pre> |
| 149 |
|
| 150 |
<p> |
| 151 |
This example shows how <code>s-save</code> can be used to save a sound to a file.</p> |
| 152 |
|
| 153 |
<p>The last line of this example shows how the <code>system</code> function can |
| 154 |
be used to invoke |
| 155 |
Unix shell commands, such as a command to play a file or remove it. |
| 156 |
Finally, notice that <code>strcat</code> can be used to concatenate a command name |
| 157 |
to a file name to create a complete command that is then passed to |
| 158 |
<code>system</code>. (This is convenient if the sound file name is stored in a |
| 159 |
parameter or variable.)</p> |
| 160 |
<a name = "46"><h3>Memory Space and Normalization</h3></a><a name="index162"></a><a name="index163"></a><a name="index164"></a><a name="index165"></a><a name="index166"></a><a name="index167"></a><a name="index168"></a><a name="index169"></a> |
| 161 |
<p>Sound samples take up lots of memory, and often, there is not enough primary (RAM) memory to hold a complete composition. For this reason, Nyquist can compute sounds incrementally, saving the final result on disk. <i>However,</i> Nyquist can also save sounds in memory so that they can be reused efficiently. In general, if a sound is saved in a global variable, memory will be allocated as needed to save and reuse it.</p> |
| 162 |
|
| 163 |
<p>The standard way to compute a sound and write it to disk is to pass an expression to the <code>play</code> command: |
| 164 |
</p> |
| 165 |
<pre> |
| 166 |
play my-composition() |
| 167 |
</pre> |
| 168 |
|
| 169 |
<p></p> |
| 170 |
|
| 171 |
Often it is nice to <i>normalize</i> sounds so that they use the full available |
| 172 |
dynamic range of 16 bits. Nyquist has an automated facility to help with |
| 173 |
normalization. By default, Nyquist computes up to 1 million samples (using |
| 174 |
about 4MB of memory) looking for the peak. The entire sound is normalized so |
| 175 |
that this peak will not cause clipping. If the sound has less than 1 million |
| 176 |
samples, or if the first million samples are a good indication of the overall |
| 177 |
peak, then the signal will not clip. |
| 178 |
<p>With this automated normalization technique, you can choose the desired |
| 179 |
peak value by setting <code>*autonorm-target*</code>, which is initialized to 0.9. |
| 180 |
The number of samples examined is <code>*autonorm-max-samples*</code>, initially |
| 181 |
1 million. You can turn this feature off by executing: |
| 182 |
</p> |
| 183 |
<pre> |
| 184 |
exec autonorm-off()<a name="index170"></a> |
| 185 |
</pre> |
| 186 |
|
| 187 |
<p> |
| 188 |
and turn it back on by typing: |
| 189 |
</p> |
| 190 |
<pre> |
| 191 |
exec autonorm-on()<a name="index171"></a> |
| 192 |
</pre> |
| 193 |
|
| 194 |
<p> |
| 195 |
This normalization technique is in effect when <code>*autonorm-type*</code> is |
| 196 |
<code>quote(lookahead</code>), which is the default.</p> |
| 197 |
|
| 198 |
<p>An alternative normalization method uses the peak value from the previous |
| 199 |
call to <code>play</code>. After playing a file, Nyquist can adjust an internal |
| 200 |
scale factor so that if you play the same file again, the peak amplitude |
| 201 |
will be <code>*autonorm-target*</code>, which is initialized to 0.9. This can |
| 202 |
be useful if you want to carefully normalize a big sound that does not |
| 203 |
have its peak near the beginning. To select this style of normalization, |
| 204 |
set <code>*autonorm-type*</code> to the (quoted) atom <code>quote(previous</code>). </p> |
| 205 |
|
| 206 |
<p>You can also create your own normalization method in Nyquist. |
| 207 |
The <code>peak</code> function computes the maximum value of a sound. |
| 208 |
The peak value is also returned from the <code>play</code> macro. You can |
| 209 |
normalize in memory if you have enough memory; otherwise you can compute |
| 210 |
the sound twice. The two techniques are illustrated here: |
| 211 |
</p> |
| 212 |
<pre> |
| 213 |
<i>; normalize in memory. First, assign the sound to a variable so |
| 214 |
; it will be retained:</i> |
| 215 |
set mysound = sim(osc(c4), osc(c5)) |
| 216 |
<i>; now compute the maximum value (ny:all is 1 giga-samples, you may want a |
| 217 |
; smaller constant if you have less than 4GB of memory:</i> |
| 218 |
set mymax = snd-max(mysound, NY:ALL) |
| 219 |
display "Computed max", mymax |
| 220 |
<i>; now write out and play the sound from memory with a scale factor:</i> |
| 221 |
play mysound * (0.9 / mymax) |
| 222 |
</pre> |
| 223 |
|
| 224 |
<p> |
| 225 |
</p> |
| 226 |
<pre> |
| 227 |
<i>; if you don't have space in memory, here's how to do it:</i> |
| 228 |
define function myscore() |
| 229 |
return sim(osc(c4), osc(c5)) |
| 230 |
<i>; compute the maximum:</i> |
| 231 |
set mymax = snd-max(list(quote(myscore)), NY:ALL) |
| 232 |
display "Computed max", mymax |
| 233 |
<i>; now we know the max, but we don't have a the sound (it was garbage |
| 234 |
; collected and never existed all at once in memory). Compute the sound |
| 235 |
; again, this time with a scale factor:</i> |
| 236 |
play myscore() * (0.9 / mymax) |
| 237 |
</pre> |
| 238 |
|
| 239 |
<p></p> |
| 240 |
|
| 241 |
<p>You can also write a sound as a floating point file. This |
| 242 |
file can then be converted to 16-bit integer with the proper scaling |
| 243 |
applied. If a long computation was involved, it should be much faster |
| 244 |
to scale the saved sound file than to recompute the sound from scratch. |
| 245 |
Although not implemented yet in Nyquist, some header formats can |
| 246 |
store maximum amplitudes, and some soundfile player programs can |
| 247 |
rescale floating point files on the fly, allowing normalized |
| 248 |
soundfile playback without an extra normalization pass (but at a cost |
| 249 |
of twice the disk space of 16-bit samples). |
| 250 |
You can use Nyquist to rescale a floating point file and |
| 251 |
convert it to 16-bit samples for playback.</p> |
| 252 |
<a name = "47"><h3>Frequency Modulation</h3></a><a name="index172"></a> |
| 253 |
<p>The next example uses the Nyquist frequency modulation behavior <code>fmosc</code> |
| 254 |
to generate various sounds. The parameters to <code>fmosc</code> are: |
| 255 |
</p> |
| 256 |
<pre> |
| 257 |
fmosc(<i>pitch</i>, <i>modulator</i>, <i>table</i>, <i>phase</i>) |
| 258 |
</pre> |
| 259 |
|
| 260 |
<p> |
| 261 |
Note that pitch is the number of half-steps, e.g. <code>c4</code> has the value of 60 which is middle-C, and phase is in degrees. Only the first two parameters are required: |
| 262 |
</p> |
| 263 |
<pre> |
| 264 |
<i>; make a short sine tone with no frequency modulation |
| 265 |
;</i> |
| 266 |
play fmosc(c4, pwl(0.1)) |
| 267 |
|
| 268 |
<i>; make a longer sine tone -- note that the duration of |
| 269 |
; the modulator determines the duration of the tone |
| 270 |
;</i> |
| 271 |
play fmosc(c4, pwl(0.5)) |
| 272 |
</pre> |
| 273 |
|
| 274 |
<p> |
| 275 |
In the example above, <code>pwl</code> (for Piece-Wise Linear) is used to generate |
| 276 |
sounds that are zero for the durations of <code>0.1</code> and <code>0.5</code> seconds, |
| 277 |
respectively. In effect, we are using an FM oscillator with no modulation |
| 278 |
input, and the result is a sine tone. The duration of the modulation |
| 279 |
determines the duration of the generated tone (when the modulation signal |
| 280 |
ends, the oscillator stops).</p> |
| 281 |
|
| 282 |
<p>The next example uses a more interesting modulation function, a ramp from |
| 283 |
zero to C<sub>4</sub>, expressed in hz. More explanation of <code>pwl</code> is in |
| 284 |
order. This operation constructs a piece-wise linear function sampled at |
| 285 |
the <code>*control-srate*</code>. The first breakpoint is always at <code>(0, |
| 286 |
0)</code>, so the first two parameters give the time and value of the second |
| 287 |
breakpoint, the second two parameters give the time and value of the third |
| 288 |
breakpoint, and so on. The last breakpoint has a value of <code>0</code>, so only |
| 289 |
the time of the last breakpoint is given. In this case, we want the ramp to |
| 290 |
end at C<sub>4</sub>, so we cheat a bit by having the ramp return to zero |
| 291 |
“almost” instantaneously between times <code>0.5</code> and <code>0.501</code>.</p> |
| 292 |
|
| 293 |
<p>The <code>pwl</code> behavior always expects an odd number of parameters. The |
| 294 |
resulting function is shifted and stretched linearly according to |
| 295 |
<code>*warp*</code> in the environment. Now, here is the example: |
| 296 |
</p> |
| 297 |
<pre> |
| 298 |
<i>; make a frequency sweep of one octave; the piece-wise linear function |
| 299 |
; sweeps from 0 to (step-to-hz c4) because, when added to the c4 |
| 300 |
; fundamental, this will double the frequency and cause an octave sweep. |
| 301 |
;</i> |
| 302 |
play fmosc(c4, pwl(0.5, step-to-hz(c4), 0.501)) |
| 303 |
</pre> |
| 304 |
|
| 305 |
<p></p> |
| 306 |
|
| 307 |
<p>The same idea can be applied to a non-sinusoidal carrier. Here, we assume that <code>*fm-voice*</code> is predefined (the next section shows how to define it): |
| 308 |
</p> |
| 309 |
<pre> |
| 310 |
<i>; do the same thing with a non-sine table |
| 311 |
;</i> |
| 312 |
play fmosc(cs2, pwl(0.5, step-to-hz(cs2), 0.501), |
| 313 |
*fm-voice*, 0.0) |
| 314 |
</pre> |
| 315 |
|
| 316 |
<p></p> |
| 317 |
|
| 318 |
<p>The next example shows how a function can be used to make a special |
| 319 |
frequency modulation contour. In this case the contour generates a sweep |
| 320 |
from a starting pitch to a destination pitch: |
| 321 |
</p> |
| 322 |
<pre> |
| 323 |
<i>; make a function to give a frequency sweep, starting |
| 324 |
; after <delay> seconds, then sweeping from <pitch-1> |
| 325 |
; to <pitch-2> in <sweep-time> seconds and then |
| 326 |
; holding at <pitch-2> for <hold-time> seconds. |
| 327 |
;</i> |
| 328 |
define function sweep(delay, pitch-1, sweep-time, |
| 329 |
pitch-2, hold-time) |
| 330 |
begin |
| 331 |
with interval = step-to-hz(pitch-2) - step-to-hz(pitch-1) |
| 332 |
return pwl(delay, 0.0, |
| 333 |
<i>; sweep from pitch 1 to pitch 2</i> |
| 334 |
delay + sweep-time, interval, |
| 335 |
<i>; hold until about 1 sample from the end</i> |
| 336 |
delay + sweep-time + hold-time - 0.0005, |
| 337 |
interval, |
| 338 |
<i>; quickly ramp to zero (pwl always does this,</i> |
| 339 |
<i>; so make it short)</i> |
| 340 |
delay + sweep-time + hold-time) |
| 341 |
end |
| 342 |
|
| 343 |
|
| 344 |
<i>; now try it out |
| 345 |
;</i> |
| 346 |
play fmosc(cs2, sweep(0.1, cs2, 0.6, gs2, 0.5), |
| 347 |
*fm-voice*, 0.0) |
| 348 |
</pre> |
| 349 |
|
| 350 |
<p></p> |
| 351 |
|
| 352 |
<p>FM can be used for vibrato as well as frequency sweeps. Here, we use the |
| 353 |
<code>lfo</code> function to generate vibrato. The <code>lfo</code> operation is |
| 354 |
similar to <code>osc</code>, except it generates sounds at the |
| 355 |
<code>*control-srate*</code>, and the parameter is hz rather than a pitch: |
| 356 |
</p> |
| 357 |
<pre> |
| 358 |
play fmosc(cs2, 10.0 * lfo(6.0), *fm-voice*, 0.0) |
| 359 |
</pre> |
| 360 |
|
| 361 |
<p></p> |
| 362 |
|
| 363 |
<p>What kind of manual would this be without the obligatory FM sound? Here, a |
| 364 |
sinusoidal modulator (frequency C<sub>4</sub>) is multiplied by a slowly increasing |
| 365 |
ramp from zero to <code>1000.0</code>. |
| 366 |
</p> |
| 367 |
<pre> |
| 368 |
set modulator = pwl(1.0, 1000.0, 1.0005) * |
| 369 |
osc(c4) |
| 370 |
<i>; make the sound</i> |
| 371 |
play fmosc(c4, modulator) |
| 372 |
</pre> |
| 373 |
|
| 374 |
<p></p> |
| 375 |
|
| 376 |
<p>For more simple examples of FM in Nyquist, see |
| 377 |
<a name="index173"></a><a name="index174"></a><a name="index175"></a><a name="index176"></a> |
| 378 |
<code>nyquist/lib/warble/warble_tutorial.htm</code>.</p> |
| 379 |
<a name = "48"><h3>Building a Wavetable</h3></a> |
| 380 |
<p>In Section <a href = "part2.html#11">Non-Sinusoidal Waveforms</a>, we saw how to synthesize a wavetable. A |
| 381 |
wavetable for <code>osc</code> also can be extracted from any sound. This is |
| 382 |
especially interesting if the sound is digitized from some external sound |
| 383 |
source and loaded using the <code>s-read</code> function. Recall that a table |
| 384 |
is a list consisting of a sound, the pitch of that sound, and T (meaning the |
| 385 |
sound is periodic).</p> |
| 386 |
|
| 387 |
<p>In the following, a sound is first read from the file <code>demo-snd.nh</code>. |
| 388 |
Then, the <code>extract</code> function is used |
| 389 |
to extract the portion of the sound between 0.110204 and 0.13932 seconds. |
| 390 |
(These numbers might be obtained by first plotting the sound and estimating |
| 391 |
the beginning and end of a period, or by using some software to look for |
| 392 |
good zero crossings.) The result of <code>extract</code> becomes the first |
| 393 |
element of a list. The next element is the pitch (24.848422), and the last |
| 394 |
element is <code>T</code>. The list is assigned to <code>*fm-voice*</code>. |
| 395 |
</p> |
| 396 |
<pre> |
| 397 |
if not(boundp(quote(a-snd))) then |
| 398 |
set a-snd = s-read("demo-snd.aiff") |
| 399 |
|
| 400 |
set *fm-voice* = list(extract(0.110204, 0.13932, cue(a-snd)), |
| 401 |
24.848422, |
| 402 |
#T) |
| 403 |
</pre> |
| 404 |
|
| 405 |
<p></p> |
| 406 |
|
| 407 |
<p>The file |
| 408 |
<i>nyquist/lib/examples.sal</i> contains an extensive example of how to locate |
| 409 |
zero-crossings, extract a period, build a waveform, and generate a tone from it. (See <code>ex37</code> through <code>ex40</code> in the file.)</p> |
| 410 |
<a name = "49"><h3>Filter Examples</h3></a> |
| 411 |
<p>Nyquist provides a variety of filters. All of these filters take either |
| 412 |
real numbers or signals as parameters. If you pass a signal as a filter |
| 413 |
parameter, the filter coefficients are recomputed at the sample rate of the |
| 414 |
<i>control</i> signal. Since filter coefficients are generally expensive to |
| 415 |
compute, you may want to select filter control rates carefully. Use |
| 416 |
<code>control-srate-abs</code> (Section <a href = "part8.html#98">Transformations</a>) to specify |
| 417 |
the default control sample rate, or use <code>force-srate</code> (Section |
| 418 |
<a href = "part8.html#90">Sound Synthesis</a>) to resample a signal before passing it to a filter. </p> |
| 419 |
|
| 420 |
<p>Before presenting examples, let's generate some unfiltered white noise: |
| 421 |
</p> |
| 422 |
<pre> |
| 423 |
play noise() |
| 424 |
</pre> |
| 425 |
|
| 426 |
<p> |
| 427 |
Now low-pass filter the noise with a 1000Hz cutoff: |
| 428 |
</p> |
| 429 |
<pre> |
| 430 |
play lp(noise(), 1000.0) |
| 431 |
</pre> |
| 432 |
|
| 433 |
<p> |
| 434 |
The high-pass filter is the inverse of the low-pass: |
| 435 |
</p> |
| 436 |
<pre> |
| 437 |
play hp(noise(), 1000.0) |
| 438 |
</pre> |
| 439 |
|
| 440 |
<p></p> |
| 441 |
|
| 442 |
<p>Here is a low-pass filter sweep from 100Hz to 2000Hz: |
| 443 |
</p> |
| 444 |
<pre> |
| 445 |
play lp(noise(), pwl(0.0, 100.0, 1.0, 2000.0, 1.0)) |
| 446 |
</pre> |
| 447 |
|
| 448 |
<p> |
| 449 |
And a high-pass sweep from 50Hz to 4000Hz: |
| 450 |
</p> |
| 451 |
<pre> |
| 452 |
play hp(noise(), pwl(0.0, 50.0, 1.0, 4000.0, 1.0)) |
| 453 |
</pre> |
| 454 |
|
| 455 |
<p></p> |
| 456 |
|
| 457 |
<p>The band-pass filter takes a center frequency and a bandwidth parameter. |
| 458 |
This example has a 500Hz center frequency with a 20Hz bandwidth. The scale |
| 459 |
factor is necessary because, due to the resonant peak of the filter, the |
| 460 |
signal amplitude exceeds 1.0: |
| 461 |
</p> |
| 462 |
<pre> |
| 463 |
play reson(10.0 * noise(), 500.0, 20.0, 1) |
| 464 |
</pre> |
| 465 |
|
| 466 |
<p> |
| 467 |
In the next example, the center frequency is swept from 100 to 1000Hz, using a constant 20Hz bandwidth: |
| 468 |
</p> |
| 469 |
<pre> |
| 470 |
play reson(0.04 * noise(), |
| 471 |
pwl(0.0, 200.0, 1.0, 1000.0, 1.0), |
| 472 |
20.0) |
| 473 |
</pre> |
| 474 |
|
| 475 |
<p></p> |
| 476 |
|
| 477 |
<p>For another example with explanations, see |
| 478 |
<a name="index177"></a><a name="index178"></a><a name="index179"></a> |
| 479 |
<a name="index180"></a> |
| 480 |
<code>nyquist/lib/wind/wind_tutorial.htm</code>.</p> |
| 481 |
<a name = "50"><h3>DSP in Lisp</h3></a><a name="index181"></a><a name="index182"></a> |
| 482 |
<p>In almost any |
| 483 |
signal processing system, the vast majority of computation |
| 484 |
takes place in the inner loops of DSP algorithms, and Nyquist is designed so |
| 485 |
that these time-consuming inner loops are in highly-optimized |
| 486 |
machine code rather than relatively slow interpreted lisp code. As a result, |
| 487 |
Nyquist typically spends 95% of its time in these inner loops; the overhead |
| 488 |
of using a Lisp interpreter is negligible.</p> |
| 489 |
|
| 490 |
<p>The drawback is that Nyquist must provide the DSP operations you need, or |
| 491 |
you are out of luck. When Nyquist is found lacking, you can either write a |
| 492 |
new primitive signal operation, or you can perform DSP in Lisp code. Neither |
| 493 |
option is recommended for inexperienced programmers. Instructions for |
| 494 |
extending Nyquist are given in Appendix <a href = "part17.html#207">Appendix 1: Extending Nyquist</a>. This section |
| 495 |
describes the process of writing a new signal processing function in Lisp.</p> |
| 496 |
|
| 497 |
<p>Before implementing a new DSP function, you should decide which approach is |
| 498 |
best. First, figure out how much of the new function can be implemented |
| 499 |
using existing Nyquist functions. For example, you might think that a |
| 500 |
tapped-delay line would require a new function, but in fact, it can be |
| 501 |
implemented by composing sound transformations to accomplish delays, scale |
| 502 |
factors for attenuation, and additions to combine the intermediate results. |
| 503 |
This can all be packaged into a new Lisp function, making it easy to use. |
| 504 |
If the function relies on built-in DSP primitives, it will execute very |
| 505 |
efficiently.</p> |
| 506 |
|
| 507 |
<p>Assuming that built-in functions cannot be used, try to define a new |
| 508 |
operation that will be both simple and general. Usually, it makes sense to |
| 509 |
implement only the kernel of what you need, combining it with existing |
| 510 |
functions to build a complete instrument or operation. For example, if you |
| 511 |
want to implement a physical model that requires a varying breath pressure |
| 512 |
with noise and vibrato, plan to use Nyquist functions to add a basic |
| 513 |
pressure envelope to noise and vibrato signals to come up with a composite |
| 514 |
pressure signal. Pass that signal into the physical model rather than |
| 515 |
synthesizing the envelope, noise, and vibrato within the model. This not |
| 516 |
only simplifies the model, but gives you the flexibility to use all of |
| 517 |
Nyquist's operations to synthesize a suitable breath pressure signal.</p> |
| 518 |
|
| 519 |
<p>Having designed the new “kernel” DSP operation that must be implemented, |
| 520 |
decide whether to use C or Lisp. (At present, SAL is not a good option |
| 521 |
because it has no support for object-oriented programming.) |
| 522 |
To use C, you must have a C compiler, the |
| 523 |
full source code for Nyquist, and you must learn about extending Nyquist by |
| 524 |
reading Appendix <a href = "part17.html#207">Appendix 1: Extending Nyquist</a>. This is the more complex approach, but |
| 525 |
the result will be very efficient. A C implementation will deal properly |
| 526 |
with sounds that are not time-aligned or matched in sample rates. |
| 527 |
To use Lisp, you must learn something |
| 528 |
about the XLISP object system, and the result will be about 50 times slower |
| 529 |
than C. Also, it is more difficult to deal with time alignment and |
| 530 |
differences in sample rates. |
| 531 |
The remainder of this section gives an example of a Lisp version of |
| 532 |
<code>snd-prod</code> to illustrate how to write DSP functions for Nyquist in Lisp.</p> |
| 533 |
|
| 534 |
<p>The <code>snd-prod</code> function is the low-level multiply routine. It has two |
| 535 |
sound parameters and returns a sound which is the product of the two. To |
| 536 |
keep things simple, we will assume that two sounds to be multiplied have a |
| 537 |
matched sample rate and matching start times. The DSP algorithm for each |
| 538 |
output sample is simply to fetch a sample from each sound, multiply them, |
| 539 |
and return the product.</p> |
| 540 |
|
| 541 |
<p>To implement <code>snd-prod</code> in Lisp, three components are required: |
| 542 |
<ol> |
| 543 |
<li> |
| 544 |
An object is used to store the two parameter sounds. This object will be |
| 545 |
called upon to yield samples of the result sound;</li> |
| 546 |
<li>Within the object, the <code>snd-fetch</code> routine is used to fetch samples |
| 547 |
from the two input sounds as needed;</li> |
| 548 |
<li>The result must be of type <code>SOUND</code>, so <code>snd-fromobject</code> is used |
| 549 |
to create the result sound. |
| 550 |
</li></ol></p> |
| 551 |
|
| 552 |
<p>The combined solution will work as follows: The result is a value of type |
| 553 |
<code>sound</code> that retains a reference to the object. When Nyquist needs |
| 554 |
samples from the sound, it invokes the sound's “fetch” function, which in |
| 555 |
turn sends an XLISP message to the object. The object will use |
| 556 |
<code>snd-fetch</code> to get a sample from each stored sound, multiply the |
| 557 |
samples, and return a result.</p> |
| 558 |
|
| 559 |
<p>Thus the goal is to design an XLISP object that, in response to a |
| 560 |
<code>:next</code> message will return a proper sequence of samples. When the |
| 561 |
sound reaches the termination time, simply return <code>NIL</code>.</p> |
| 562 |
|
| 563 |
<p>The XLISP manual (see Appendix <a href = "part19.html#224">Appendix 3: XLISP: An Object-oriented Lisp</a>) describes the object system, |
| 564 |
but in a very terse style, so this example will include some explanation of |
| 565 |
how the object system is used. First, we need to define a class for the |
| 566 |
objects that will compute sound products. Every class is a subclass of class |
| 567 |
<code>class</code>, and you create a subclass by sending <code>:new</code> to a class. |
| 568 |
</p> |
| 569 |
<pre> |
| 570 |
(setf product-class (send class :new '(s1 s2))) |
| 571 |
</pre> |
| 572 |
|
| 573 |
<p> |
| 574 |
The parameter <code>'(s1 s2)</code> says that the new class will have two instance |
| 575 |
variables, <code>s1</code> and <code>s2</code>. In other words, every object which is an |
| 576 |
instance of class <code>product-class</code> will have its own copy of |
| 577 |
these two variables.</p> |
| 578 |
|
| 579 |
<p>Next, we will define the <code>:next</code> method for <code>product-class</code>: |
| 580 |
</p> |
| 581 |
<pre> |
| 582 |
(send product-class :answer :next '() |
| 583 |
'((let ((f1 (snd-fetch s1)) |
| 584 |
(f2 (snd-fetch s2))) |
| 585 |
(cond ((and f1 f2) |
| 586 |
(* f1 f2)) |
| 587 |
(t nil))))) |
| 588 |
</pre> |
| 589 |
|
| 590 |
<p> |
| 591 |
The <code>:answer</code> message is used to insert a new method into our new |
| 592 |
<code>product-class</code>. The method is described in three parts: the name |
| 593 |
(<code>:next</code>), a parameter list (empty in this case), and a list of |
| 594 |
expressions to be evaluated. In this case, we fetch samples from <code>s1</code> |
| 595 |
and <code>s2</code>. If both are numbers, we return their product. If either is |
| 596 |
<code>NIL</code>, we terminate the sound by returning <code>nil</code>.</p> |
| 597 |
|
| 598 |
<p>The <code>:next</code> method assumes that <code>s1</code> and <code>s2</code> hold the sounds |
| 599 |
to be multiplied. These must be installed when the object is created. |
| 600 |
Objects are created by sending <code>:new</code> to a class. A new object is |
| 601 |
created, and any parameters passed to <code>:new</code> are then sent in a |
| 602 |
<code>:isnew</code> message to the new object. Here is the <code>:isnew</code> |
| 603 |
definition for <code>product-class</code>: |
| 604 |
</p> |
| 605 |
<pre> |
| 606 |
(send product-class :answer :isnew '(p1 p2) |
| 607 |
'((setf s1 (snd-copy p1)) |
| 608 |
(setf s2 (snd-copy p2)))) |
| 609 |
</pre> |
| 610 |
|
| 611 |
<p> |
| 612 |
Take careful note of the use of <code>snd-copy</code> in this initialization. The |
| 613 |
sounds <code>s1</code> and <code>s2</code> are modified when accessed by |
| 614 |
<code>snd-fetch</code> in the <code>:next</code> method defined above, but this destroys |
| 615 |
the illusion that sounds are immutable values. The solution is to copy the |
| 616 |
sounds before accessing them; the original sounds are therefore unchanged. |
| 617 |
(This copy also takes place implicitly in most Nyquist sound functions.)</p> |
| 618 |
|
| 619 |
<p>To make this code safer for general use, we should add checks that <code>s1</code> |
| 620 |
and <code>s2</code> are sounds with identical starting times and sample rates; |
| 621 |
otherwise, an incorrect result might be computed.</p> |
| 622 |
|
| 623 |
<p>Now we are ready to write <code>snd-product</code>, an approximate replacement for |
| 624 |
<code>snd-prod</code>: |
| 625 |
</p> |
| 626 |
<pre> |
| 627 |
(defun snd-product (s1 s2) |
| 628 |
(let (obj) |
| 629 |
(setf obj (send product-class :new s1 s2)) |
| 630 |
(snd-fromobject (snd-t0 s1) (snd-srate s1) obj))) |
| 631 |
</pre> |
| 632 |
|
| 633 |
<p> |
| 634 |
This code first creates <code>obj</code>, an instance of <code>product-class</code>, to |
| 635 |
hold <code>s1</code> and <code>s2</code>. Then, it uses <code>obj</code> to create a sound |
| 636 |
using <code>snd-fromobject</code>. This sound is returned from |
| 637 |
<code>snd-product</code>. Note that in <code>snd-fromobject</code>, you must also |
| 638 |
specify the starting time and sample rate as the first two parameters. These |
| 639 |
are copied from <code>s1</code>, again assuming that <code>s1</code> and <code>s2</code> have |
| 640 |
matching starting times and sample rates.</p> |
| 641 |
|
| 642 |
<p>Note that in more elaborate DSP algorithms we could expect the object to |
| 643 |
have a number of instance variables to hold things such as previous samples, |
| 644 |
waveform tables, and other parameters.</p> |
| 645 |
<hr> |
| 646 |
<a href = "part5.html">Previous Section</a> | <a href = "part7.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> |
| 647 |
</body></html> |