| 1 |
<!DOCTYPE html> |
| 2 |
<html><head><title>Appendix 1: Extending Nyquist</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 = "part16.html">Previous Section</a> | <a href = "part18.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 = "207"><h2>Appendix 1: Extending Nyquist</h2></a><b><i>WARNING:</i></b> |
| 11 |
<p>Nyquist sound functions look almost like a human wrote them; they even have |
| 12 |
a fair number of comments for human readers. Don't be fooled: virtually all |
| 13 |
Nyquist functions are written by a special translator. If you try to write |
| 14 |
a new function by hand, you will probably not succeed, and even if you do, |
| 15 |
you will waste a great deal of time. (End of Warning.)</p> |
| 16 |
<a name = "208"><h3>Translating Descriptions to C Code</h3></a> |
| 17 |
<p>The translator code used to extend Nyquist resides in the <code>trnsrc</code> |
| 18 |
directory. This directory also contains a special <code>init.lsp</code>, so if |
| 19 |
you start XLisp or Nyquist in this directory, it will automatically read |
| 20 |
<code>init.lsp</code>, which in turn will load the translator code (which resides |
| 21 |
in several files).</p> |
| 22 |
|
| 23 |
<p>Also in the <code>trnsrc</code> directory are a number of <code>.alg</code> files, which |
| 24 |
contain the source code for the translator (more on these will follow), and |
| 25 |
a number of corresponding <code>.h</code> and <code>.c</code> files. </p> |
| 26 |
|
| 27 |
<p>To translate a <code>.alg</code> file to <code>.c</code> and <code>.h</code> files, you start |
| 28 |
XLisp or Nyquist in the <code>trnsrc</code> directory and type |
| 29 |
</p> |
| 30 |
<pre> |
| 31 |
(translate "prod") |
| 32 |
</pre> |
| 33 |
|
| 34 |
<p> |
| 35 |
where <code>"prod"</code> should really be replaced by the filename (without a |
| 36 |
suffix) you want to translate. Be sure you have a saved, working copy of |
| 37 |
Nyquist or Xlisp before you recompile! </p> |
| 38 |
<b><i>Note:</i></b> On the Macintosh, just run Nyquist out of the <code>runtime</code> directory and then use the <code>Load</code> menu command to load <code>init.lsp</code> from the <code>trnsrc</code> directory. This will load the translation code and change Nyquist's current directory to <code>trnsrc</code> so that commands like <code>(translate "prod")</code> will work.<a name = "209"><h3>Rebuilding Nyquist</h3></a> |
| 39 |
<p>After generating <code>prod.c</code> and |
| 40 |
<code>prod.h</code>, you need to recompile Nyquist. For Unix systems, you will |
| 41 |
want to generate a new Makefile. Modify <code>transfiles.lsp</code> in your main |
| 42 |
Nyquist directory, run Xlisp or Nyquist and load <code>makefile.lsp</code>. |
| 43 |
Follow the instructions to set your machine type, etc., and |
| 44 |
execute <code>(makesrc)</code> and <code>(makefile)</code>.</p> |
| 45 |
<a name = "210"><h3>Accessing the New Function</h3></a> |
| 46 |
<p>The new Lisp function will generally be named with a <code>snd-</code> prefix, |
| 47 |
e.g. <code>snd-prod</code>. You can test this by running Nyquist. Debugging is |
| 48 |
usually a combination of calling the code from within the interpreter, |
| 49 |
reading the generated code when things go wrong, and using a C debugger to |
| 50 |
step through the inner loop of the generated code. An approach I like is to |
| 51 |
set the default sample rate to 10 hertz. Then, a one-second sound has only |
| 52 |
10 samples, which are easy to print and study on a text console.</p> |
| 53 |
|
| 54 |
<p>For some functions, |
| 55 |
you must write some Lisp code to impose |
| 56 |
ordinary Nyquist behaviors such as stretching and time shifting. A |
| 57 |
good approach is to find some structurally similar functions and see how |
| 58 |
they are implemented. Most of the Lisp code for Nyquist is in |
| 59 |
<code>nyquist.lsp</code>.</p> |
| 60 |
|
| 61 |
<p>Finally, do not forget to write up some documentation. Also, contributions are |
| 62 |
welcome. Send your <code>.alg</code> file, documentation, Lisp support |
| 63 |
functions for <code>nyquist.lsp</code>, and examples or test programs |
| 64 |
to <code>[email protected]</code>. I will either put them in the next release or |
| 65 |
make them available at a public ftp site.</p> |
| 66 |
<a name = "211"><h3>Why Translation?</h3></a> |
| 67 |
<p>Many of the Nyquist signal processing operations are similar in form, but |
| 68 |
they differ in details. This code is complicated by many factors: Nyquist |
| 69 |
uses lazy evaluation, so the operator must check to see that input samples |
| 70 |
are available before trying to access them. Nyquist signals can have |
| 71 |
different sample rates, different block sizes, different block boundaries, |
| 72 |
and different start times, all of which must be taken into account. The |
| 73 |
number of software tests is enormous. (This may sound like a lot of |
| 74 |
overhead, but the overhead is amortized over many iterations of the inner |
| 75 |
loop. Of course setting up the inner loop to run efficiently is one more |
| 76 |
programming task.)</p> |
| 77 |
|
| 78 |
<p>The main idea behind the translation is that all of the checks and setup |
| 79 |
code are similar and relatively easy to generate automatically. Programmers |
| 80 |
often use macros for this sort of task, but the C macro processor is too |
| 81 |
limited for the complex translation required here. To tell the translator |
| 82 |
how to generate code, you write <code>.alg</code> files, which provide many |
| 83 |
details about the operation in a declarative style. For example, the code |
| 84 |
generator can make some optimizations if you declare that two input signals |
| 85 |
are commutative (they can be exchanged with one another). The main part of |
| 86 |
the <code>.alg</code> file is the inner loop which is the heart of the signal |
| 87 |
processing code.</p> |
| 88 |
<a name = "212"><h3>Writing a .alg File</h3></a><b><i>WARNING:</i></b> Translation relies heavily on string substitution, which |
| 89 |
<p>is fragile. In particular, variables with names that are substrings of |
| 90 |
other variables will cause problems. For example if you declare STATE |
| 91 |
variables "phase" and "iphase", then the translator will globally |
| 92 |
substitute "phase_reg" for "phase", converting "phase" to "phase_reg" |
| 93 |
and iphase" to "iphase_reg". Then it will substitute "iphase_reg" for |
| 94 |
iphase" which will convert the existing "iphase_reg" to |
| 95 |
"iphase_reg_reg". This will be confusing and will not compile. |
| 96 |
(End of WARNING)</p> |
| 97 |
|
| 98 |
<p>To give you some idea how functions are specified, here is the |
| 99 |
specification for <code>snd-prod</code>, which generates over 250 lines of C code: |
| 100 |
</p> |
| 101 |
<pre> |
| 102 |
(PROD-ALG |
| 103 |
(NAME "prod") |
| 104 |
(ARGUMENTS ("sound_type" "s1") ("sound_type" "s2")) |
| 105 |
(START (MAX s1 s2)) |
| 106 |
(COMMUTATIVE (s1 s2)) |
| 107 |
(INNER-LOOP "output = s1 * s2") |
| 108 |
(LINEAR s1 s2) |
| 109 |
(TERMINATE (MIN s1 s2)) |
| 110 |
(LOGICAL-STOP (MIN s1 s2)) |
| 111 |
) |
| 112 |
</pre> |
| 113 |
|
| 114 |
<p> |
| 115 |
A <code>.alg</code> file is always of the form: |
| 116 |
</p> |
| 117 |
<pre> |
| 118 |
(<i>name</i> |
| 119 |
(<i>attribute</i> <i>value</i>) |
| 120 |
(<i>attribute</i> <i>value</i>) |
| 121 |
... |
| 122 |
) |
| 123 |
</pre> |
| 124 |
|
| 125 |
<p> |
| 126 |
There should be just one of these algorithms descriptions per file. |
| 127 |
The <i>name</i> field is arbitrary: it is a Lisp symbol whose property list is |
| 128 |
used to save the following attribute/value pairs. There are many |
| 129 |
attributes described below. For more examples, see the <code>.alg</code> files in |
| 130 |
the <code>trnsrc</code> directory.</p> |
| 131 |
|
| 132 |
<p>Understanding what the attributes do is not |
| 133 |
easy, so here are three recommendations for implementors. First, if there is |
| 134 |
an existing Nyquist operator that is structurally similar to something you |
| 135 |
want to implement, make a copy of the corresponding <code>.alg</code> file and |
| 136 |
work from there. In some cases, you can merely rename the parameters and |
| 137 |
substitute a new inner loop. Second, read the generated code, especially |
| 138 |
the generated inner loop. It may not all make sense, but sometimes you can |
| 139 |
spot obvious errors and work your way back to the error in the <code>.alg</code> |
| 140 |
file. Third, if you know where something bad is generated, see if you can |
| 141 |
find where the code is generated. (The code generator files are listed in |
| 142 |
<code>init.lsp</code>.) This code is poorly written and poorly documented, but in |
| 143 |
some cases it is fairly straightforward to determine what attribute in the |
| 144 |
<code>.alg</code> file is responsible for the erroneous output.</p> |
| 145 |
<a name = "213"><h3>Attributes</h3></a> |
| 146 |
<p>Here are the attributes used for code generation. Attributes and values may |
| 147 |
be specified in any order. |
| 148 |
<dl> |
| 149 |
<dt> |
| 150 |
<code>(NAME "<i>string</i>")</code></dt> |
| 151 |
<dd>specifies a base name for many identifiers. In |
| 152 |
particular, the generated filenames will be <i>string</i><code>.c</code> and |
| 153 |
<i>string</i><code>.h</code>, and the XLisp function generated will be |
| 154 |
<code>snd-</code><i>string</i>.</p> |
| 155 |
<dt><code>(ARGUMENTS <i>arglist</i>)</code></dt> |
| 156 |
<dd>describes the arguments to be passed from |
| 157 |
XLisp. <i>Arglist</i> has the form: <code>(<i>type1</i> <i>name1</i>) (<i>type2</i> |
| 158 |
<i>name2</i>) ...</code>, where <i>type</i> and <i>name</i> are strings in double quotes, |
| 159 |
e.g. ("sound_type" "s") specifies a SOUND parameter named <code>s</code>. Note that <i>arglist</i> is not surrounded by parentheses. As seen |
| 160 |
in this example, the type names and parameter names are C identifiers. Since |
| 161 |
the parameters are passed in from XLisp, they must be chosen from a |
| 162 |
restricted set. Valid type names are: <code>"sound_type"</code>, <code>"rate_type"</code>, <code>"double"</code>, |
| 163 |
<code>"long"</code>, <code>"string"</code>, and <code>"LVAL"</code>.<br><br> |
| 164 |
<dt><code>(STATE <i>statelist</i>)</code></dt> |
| 165 |
<dd>describes additional state (variables) needed |
| 166 |
to perform the computation. A <i>statelist</i> is similar to an <i>arglist</i> |
| 167 |
(see <code>ARGUMENTS</code> above), and has the form: <code>(<i>type1</i> <i>name1</i> |
| 168 |
<i>init1</i> [TEMP]) (<i>type2</i> <i>name2</i> <i>init2</i> [TEMP]) ...</code>. |
| 169 |
The types and names are as |
| 170 |
in <i>arglist</i>, and the "inits" are double-quoted initial values. Initial |
| 171 |
values may be any C expression. State is initialized in the order implied by |
| 172 |
<i>statelist</i> when the operation is first called from XLisp. If <code>TEMP</code> |
| 173 |
is omitted the state is preserved in a structure until the sound computation |
| 174 |
completes. Otherwise, the state variable only exists at state |
| 175 |
initialization time.<br><br> |
| 176 |
<dt><code>(INNER-LOOP <i>innerloop-code</i>)</code></dt> |
| 177 |
<dd>describes the inner loop, written as |
| 178 |
C code. The <i>innerloop-code</i> is in double quotes, and may extend over |
| 179 |
multiple lines. To make generated code extra-beautiful, prefix each line of |
| 180 |
<i>innerloop-code</i> with 12 spaces. Temporary variables should not |
| 181 |
be declared at the beginning of <i>innerloop-code</i>. Use the |
| 182 |
<code>INNER-LOOP-LOCALS</code> attribute instead. Within <i>innerloop-code</i>, |
| 183 |
<i>each ARGUMENT of type sound_type</i> <b><i>must</i></b> <i>be referenced exactly one |
| 184 |
time.</i> If you need to use a signal value twice, assign it once to a |
| 185 |
temporary and use the temporary twice. The inner loop must also assign |
| 186 |
<i>one</i> time to the psuedo-variable <i>output</i>. The model here is that the |
| 187 |
name of a sound argument denotes the value of the corresponding signal at |
| 188 |
the current output sample time. The inner loop code will be called once for |
| 189 |
each output sample. In practice, the code generator will substitute some |
| 190 |
expression for each signal name. For example, |
| 191 |
<code>prod.alg</code> specifies |
| 192 |
<blockquote><code>(INNER-LOOP "output = s1 * s2")</code></blockquote> |
| 193 |
(<code>s1</code> and <code>s2</code> are <code>ARGUMENTS</code>.) This expands to the |
| 194 |
following inner loop in <code>prod.c</code>: |
| 195 |
<blockquote><code>*out_ptr_reg++ = *s1_ptr_reg++ * *s2_ptr_reg++;</code></blockquote> |
| 196 |
In cases where arguments have different sample rates, sample interpolation |
| 197 |
is in-lined, and the expressions can get very complex. The translator is |
| 198 |
currently very simple-minded about substituting access code in the place of |
| 199 |
parameter names, and <i>this is a frequent source of bugs.</i> Simple string |
| 200 |
substitution is performed, so <i>you must not use a parameter or state name |
| 201 |
that is a substring of another</i>. For example, if two sound parameters were |
| 202 |
named <code>s</code> and <code>s2</code>, the translator might substitute for “s” in two |
| 203 |
places rather than one. If this problem occurs, you will almost certainly |
| 204 |
get a C compiler syntax error. The fix is to use “more unique” parameter |
| 205 |
and state variable names.<br><br> |
| 206 |
<dt><code>(INNER-LOOP-LOCALS "<i>innerloop-code</i>")</code></dt> |
| 207 |
<dd>The <i>innerloop-code</i> |
| 208 |
contains C declarations of local variables set and referenced in the inner |
| 209 |
loop.<br><br> |
| 210 |
<dt><code>(SAMPLE-RATE "<i>expr</i>")</code></dt> |
| 211 |
<dd>specifies the output sample rate; <i>expr</i> |
| 212 |
can be any C expression, including a parameter from the <code>ARGUMENTS</code> |
| 213 |
list. You can also write <code>(SAMPLE-RATE (MAX <i>name1</i> <i>name2</i> ...))</code> |
| 214 |
where names are unquoted names of arguments.<br><br> |
| 215 |
<dt><code>(SUPPORT-HEADER "<i>c-code</i>")</code></dt> |
| 216 |
<dd>specifies arbitrary C code to be |
| 217 |
inserted in the generated <code>.h</code> file. The code typically contains |
| 218 |
auxiliarly function declarations and definitions of constants.<br><br> |
| 219 |
<dt><code>(SUPPORT-FUNCTIONS "<i>c-code</i>")</code></dt> |
| 220 |
<dd>specifies arbitrary C code to be |
| 221 |
inserted in the generated <code>.c</code> file. The code typically contains |
| 222 |
auxiliarly functions and definitions of constants.<br><br> |
| 223 |
<dt><code>(FINALIZATION "<i>c-code</i>")</code></dt> |
| 224 |
<dd>specifies code to execute when the sound |
| 225 |
has been fully computed and the state variables are about to be |
| 226 |
decallocated. This is the place to deallocate buffer memory, etc.<br><br> |
| 227 |
<dt><code>(CONSTANT "<i>name1</i>" "<i>name2</i>" ...)</code></dt> |
| 228 |
<dd>specifies state variables that |
| 229 |
do not change value in the inner loop. The values of state |
| 230 |
variables are loaded into registers before entering the inner loop so that |
| 231 |
access will be fast within the loop. On exiting the inner loop, the final |
| 232 |
register values are preserved in a “suspension” structure. If state |
| 233 |
values do not change in the inner loop, this <code>CONSTANT</code> declaration can |
| 234 |
eliminate the overhead of storing these registers.<br><br> |
| 235 |
<dt><code>(START <i>spec</i>)</code></dt> |
| 236 |
<dd>specifies when the output sound should start (a |
| 237 |
sound is zero and no processing is done before the start time). The <i>spec</i> |
| 238 |
can take several forms: <code>(MIN <i>name1</i> <i>name2</i> ...)</code> means the start |
| 239 |
time is the minimum of the start times of input signals <i>name1</i>, |
| 240 |
<i>name2</i>, .... Note that these names are not quoted.<br><br> |
| 241 |
<dt><code>(TERMINATE <i>spec</i>)</code></dt> |
| 242 |
<dd>specifies when the output |
| 243 |
sound terminates (a sound is |
| 244 |
zero after this termination time and no more samples are computed). The |
| 245 |
<i>spec</i> can take several forms: <code>(MIN <i>name1</i> <i>name2</i> ...)</code> means |
| 246 |
the terminate time is the minimum of the terminate times of input arguments |
| 247 |
<i>name1</i>, <i>name2</i>, .... Note that these names are not quoted. To |
| 248 |
terminate at the time of a single argument <code>s1</code>, specify <code>(MIN |
| 249 |
s1)</code>. To terminate after a specific duration, use <code>(AFTER "<i>c-expr</i>")</code>, |
| 250 |
where <i>c-expr</i> is a C variable or expression. To terminate at a |
| 251 |
particular time, use <code>(AT "<i>c-expr</i>")</code>. <i>spec</i> may also be |
| 252 |
<code>COMPUTED</code>, which means to use the maximum sample rate of any input |
| 253 |
signal.<br><br> |
| 254 |
<dt><code>(LOGICAL-STOP <i>spec</i>)</code></dt> |
| 255 |
<dd>specifies the logical stop time of the output |
| 256 |
sound. This <i>spec</i> is just like the one for <code>TERMINATE</code>. If no |
| 257 |
<code>LOGICAL-STOP</code> attribute is present, the logical stop will coincide |
| 258 |
with the terminate time.<br><br> |
| 259 |
<dt><code>(ALWAYS-SCALE <i>name1</i> <i>name2</i> ...)</code></dt> |
| 260 |
<dd>says that the named sound |
| 261 |
arguments (not in quotes) should always be multiplied by a scale factor. |
| 262 |
This is a space-time tradeoff. When Nyquist sounds are scaled, the scale |
| 263 |
factor is merely stored in a structure. It is the responsibility of |
| 264 |
the user of the samples to actually scale them (unless the scale factor is |
| 265 |
exactly 1.0). The default is to generate code with and without scaling and |
| 266 |
to select the appropriate code at run time. If there are N signal inputs, |
| 267 |
this will generate 2<sup T>N</sup> versions of the code. To avoid this code |
| 268 |
explosion, use the <code>ALWAYS-SCALE</code> attribute.<br><br> |
| 269 |
<dt><code>(INLINE-INTERPOLATION <i>flag</i>)</code></dt> |
| 270 |
<dd>controls when sample rate interpolation |
| 271 |
should be performed in-line in the inner loop. There are two forms of sample |
| 272 |
rate interpolation. One is intended for use when the rate change is large |
| 273 |
and many points will be interpolated. This form uses a divide instruction |
| 274 |
and some setup at the low sample rate, |
| 275 |
but the inner loop overhead is just an add. The |
| 276 |
other form, intended for less drastic sample rate changes, performs |
| 277 |
interpolation with 2 multiplies and several adds per sample at the high |
| 278 |
sample rate. If inline interpolation is enabled, Nyquist generates various |
| 279 |
inner loops and selects the appropriate one at run-time. (This can cause a |
| 280 |
combinatorial explosion if there are multiple sound arguments.) If inline |
| 281 |
interpolation is not enabled, much less code is generated and interpolation |
| 282 |
is performed as necessary |
| 283 |
by instantiating a separate signal processing operation. The value of <i>flag</i> |
| 284 |
is <code>YES</code> to generate inline interpolation, <code>NO</code> to disable |
| 285 |
inline interpolation, and <code>NIL</code> to take the default set by the |
| 286 |
global variable <code>*INLINE-INTERPOLATION*</code>. The default is also taken |
| 287 |
if no <i>INLINE-INTERPOLATION</i> attribute is specified.<br><br> |
| 288 |
<dt><code>(STEP-FUNCTION <i>name1</i> <i>name2</i> ...)</code></dt> |
| 289 |
<dd>Normally all argument |
| 290 |
signals are |
| 291 |
linearly interpolated to the output sample rate. The linear interpolation |
| 292 |
can be turned off with this attribute. This is used, for example, in Nyquist |
| 293 |
variable filters so that filter coefficients are computed at low sample |
| 294 |
rates. In fact, this attribute was added for the special case of filters.<br><br> |
| 295 |
<dt><code>(DEPENDS <i>spec1</i> <i>spec2</i> ...)</code></dt> |
| 296 |
<dd>Specifies dependencies. This |
| 297 |
attribute was also introduced to handle the case of filter coefficients (but |
| 298 |
may have other applications.) Use it when a state variable is a function of |
| 299 |
a potentially low-sample-rate input where the input is in the |
| 300 |
<code>STEP-FUNCTION</code> list. Consider a filter coefficient that depends upon |
| 301 |
an input signal such as bandwidth. In this case, you want to compute the |
| 302 |
filter coefficient only when the input signal changes rather than every |
| 303 |
output sample, since output may occur at a much higher sample rate. A |
| 304 |
<i>spec</i> is of the form |
| 305 |
<blockquote><code>("<i>name</i>" "<i>arg</i>" "<i>expr</i>" [TEMP <i>"type"</i>])</code></blockquote> |
| 306 |
which is interpreted as follows: <i>name</i> depends upon <i>arg</i>; when <i>arg</i> |
| 307 |
changes, recompute <i>expr</i> and assign it to <i>name</i>. The <i>name</i> must be |
| 308 |
declared as a <code>STATE</code> variable unless <code>TEMP</code> is present, in which |
| 309 |
case <i>name</i> is not preserved and is used only to compute other state. |
| 310 |
Variables are updated in the order of the <code>DEPENDS</code> list.<br><br> |
| 311 |
<dt><code>(FORCE-INTO-REGISTER <i>name1</i> <i>name2</i> ...)</code></dt> |
| 312 |
<dd>causes <i>name1</i>, |
| 313 |
<i>name2</i>, ... to be loaded into registers before entering the inner loop. |
| 314 |
If the inner loop references a state variable or argument, this happens |
| 315 |
automatically. Use this attribute only if references are “hidden” in a |
| 316 |
<code>#define</code>'d macro or referenced in a <code>DEPENDS</code> specification. <br><br> |
| 317 |
<dt><code>(NOT-REGISTER <i>name1</i> <i>name2</i> ...)</code></dt> |
| 318 |
<dd>specifies state and arguments |
| 319 |
that should not be loaded into registers before entering an inner loop. |
| 320 |
This is sometimes an optimization for infrequently accessed state.<br><br> |
| 321 |
<dt><code>(NOT-IN-INNER-LOOP "<i>name1</i>" "<i>name2</i>" ...)</code></dt> |
| 322 |
<dd>says that certain |
| 323 |
arguments are not used in the inner loop. Nyquist assumes all arguments |
| 324 |
are used in the inner loop, so specify them here if not. For example, |
| 325 |
tables are passed into functions as sounds, but these sounds are not read |
| 326 |
sample-by-sample in the inner loop, so they should be listed here.<br><br> |
| 327 |
<dt><code>(MAINTAIN ("<i>name1</i>" "<i>expr1</i>") ("<i>name2</i>" "<i>expr2</i>") ... |
| 328 |
)</code></dt> |
| 329 |
<dd>Sometimes the IBM XLC compiler generates better loop code if a variable |
| 330 |
referenced in the loop is not referenced outside of the loop after the loop |
| 331 |
exit. Technically, optimization is better when variables are dead upon loop |
| 332 |
exit. Sometimes, there is an efficient way to compute the final value of a |
| 333 |
state variable without actually referencing it, in which case the variable |
| 334 |
and the computation method are given as a pair in the <code>MAINTAIN</code> |
| 335 |
attribute. This suppresses a store of the value of the named variable, |
| 336 |
making it a dead variable. Where the store would have been, the expression |
| 337 |
is computed and assigned to the named variable. See <code>partial.alg</code> for |
| 338 |
an example. This optimization is never necessary and is only for |
| 339 |
fine-tuning.<br><br> |
| 340 |
<dt><code>(LINEAR <i>name1</i> <i>name2</i> ...)</code></dt> |
| 341 |
<dd>specifies that named arguments |
| 342 |
(without quotes) are linear with respect to the output. What this |
| 343 |
<i>really</i> means is that it is numerically OK to eliminate a scale factor from |
| 344 |
the named argument and store it in the output sound descriptor, avoiding a |
| 345 |
potential multiply in this inner loop. For example, both arguments to |
| 346 |
<code>snd-prod</code> (signal multiplication) are “linear.” The inner loop has |
| 347 |
a single multiplication operator to multiply samples vs. a potential 3 |
| 348 |
multiplies if each sample were also scaled. To handle scale factors on the |
| 349 |
input signals, the scale factors are automatically multiplied and the |
| 350 |
product becomes the scale factor of the resulting output. (This effectively |
| 351 |
“passes the buck” to some other, or perhaps more than one, signal |
| 352 |
processing function, which is not always optimal. On the other hand, it |
| 353 |
works great if you multiply a number of scaled signals together: all the |
| 354 |
scale factors are ultimately handled with a single multiply.)<br><br> |
| 355 |
<dt><code>(INTERNAL-SCALING <i>name1</i> <i>name2</i> ...)</code></dt> |
| 356 |
<dd>indicates that scaling is |
| 357 |
handled in code that is hidden from the code generator for <i>name1</i>, |
| 358 |
<i>name2</i>, ..., which are sound arguments. Although it is the responsibility |
| 359 |
of the reader of samples to apply any given scale factor, sometimes scaling |
| 360 |
can be had for free. For example, the <code>snd-recip</code> operation computes |
| 361 |
the reciprocal of the input samples by peforming a division. The simple |
| 362 |
approach would be to specify an inner loop of <code>output = 1.0/s1</code>, where |
| 363 |
<code>s1</code> is the input. With scaling, this would generate an inner loop |
| 364 |
something like this: |
| 365 |
<blockquote><code>*output++ = 1.0 / (s1_scale_factor * *s1++);</code></blockquote> |
| 366 |
but a much better approach would be the following: |
| 367 |
<blockquote><code>*output++ = my_scale_factor / *s1++</code></blockquote> |
| 368 |
where <code>my_scale_factor</code> is initialized to <code>1.0 / s1->scale</code>. |
| 369 |
Working backward from the desired inner loop to the <code>.alg</code> inner loop |
| 370 |
specification, a first attempt might be to specify: |
| 371 |
<blockquote><code>(INNER-LOOP "output = my_scale_factor / s1")</code></blockquote> |
| 372 |
but this will generate the following: |
| 373 |
<blockquote><code>*output++=my_scale_factor/(s1_scale_factor * *s1++);</code></blockquote> |
| 374 |
Since the code generator does not know that scaling is handled elsewhere, |
| 375 |
the scaling is done twice! The solution is to put <code>s1</code> in the |
| 376 |
<code>INTERNAL-SCALING</code> list, which essentially means “I've already |
| 377 |
incorporated scaling into the algorithm, so suppress the multiplication by a |
| 378 |
scale factor.”<br><br> |
| 379 |
<dt><code>(COMMUTATIVE (<i>name1</i> <i>name2</i> ...))</code></dt> |
| 380 |
<dd>specifies that the results |
| 381 |
will not be affected by interchanging any of the listed arguments. When |
| 382 |
arguments are commutative, Nyquist rearranges them at run-time into |
| 383 |
decreasing order of sample rates. If interpolation is in-line, this can |
| 384 |
dramatically reduce the amount of code generated to handle all the different |
| 385 |
cases. The prime example is <code>prod.alg</code>.<br><br> |
| 386 |
<dt><code>(TYPE-CHECK "<i>code</i>")</code></dt> |
| 387 |
<dd>specifies checking code to be inserted after |
| 388 |
argument type checking at initialization time. See <code>downproto.alg</code> for |
| 389 |
an example where a check is made to guarantee that the output sample rate is |
| 390 |
not greater than the input sample rate. Otherwise an error is raised. |
| 391 |
</dd></dl><a name = "214"><h3>Generated Names</h3></a> |
| 392 |
<p>The resulting <code>.c</code> file defines a number of procedures. The procedures |
| 393 |
that do actual sample computation are named something like |
| 394 |
<i>name</i>_<i>interp-spec</i>_FETCH, where <i>name</i> is the NAME |
| 395 |
attribute from the <code>.alg</code> file, and <i>interp-spec</i> is an interpolation |
| 396 |
specification composed of a string of the following letters: n, s, i, and r. |
| 397 |
One letter corresponds to each sound argument, indicating no interpolation |
| 398 |
(r), scaling only (s), ordinary linear interpolation with scaling (i), and |
| 399 |
ramp (incremental) interpolation with scaling (r). The code generator |
| 400 |
determines all the combinations of n, s, i, and r that are necessary and |
| 401 |
generates a separate fetch function for each.</p> |
| 402 |
|
| 403 |
<p>Another function is <i>name</i><code>_toss_fetch</code>, which is called when sounds |
| 404 |
are not time-aligned and some initial samples must be discarded from one or |
| 405 |
more inputs.</p> |
| 406 |
|
| 407 |
<p>The function that creates a sound is <code>snd_make_</code><i>name</i>. This is |
| 408 |
where state allocation and initialization takes place. The proper fetch |
| 409 |
function is selected based on the sample rates and scale factors of the |
| 410 |
sound arguments, and a <code>sound_type</code> is returned.</p> |
| 411 |
|
| 412 |
<p>Since Nyquist is a functional language, sound operations are not normally allowed to |
| 413 |
modify their arguments through side effects, but even reading samples from a |
| 414 |
<code>sound_type</code> causes side effects. To hide these from the Nyquist |
| 415 |
programmer, <code>sound_type</code> arguments are first copied (this only copies a small structure. The samples themselves are on a shared list). The function |
| 416 |
<code>snd_</code><i>name</i> performs the necessary copies and calls |
| 417 |
<code>snd_make_</code><i>name</i>. It is the <code>snd_</code><i>name</i> function that is |
| 418 |
called by XLisp. The XLisp name for the function is <code>SND-</code><i>NAME</i>. |
| 419 |
Notice that the underscore in C is converted to a dash in XLisp. Also, |
| 420 |
XLisp converts identifiers to upper case when they are read, so normally, |
| 421 |
you would type <code>snd</code>-<i>name</i> to call the function.</p> |
| 422 |
<a name = "215"><h3>Scalar Arguments</h3></a> |
| 423 |
<p>If you want the option of passing either a number (scalar) or a signal as |
| 424 |
one of the arguments, you have two choices, neither of which is automated. |
| 425 |
Choice 1 is to coerce the constant into a signal from within XLisp. The |
| 426 |
naming convention would be to <code>DEFUN</code> a new function named |
| 427 |
<i>NAME</i> or <code>S-</code><i>NAME</i> for ordinary use. The <i>NAME</i> function tests |
| 428 |
the arguments using XLisp functions such as <code>TYPE-OF</code>, <code>NUMBERP</code>, |
| 429 |
and <code>SOUNDP</code>. Any number is converted to a <code>SOUND</code>, e.g. using |
| 430 |
<code>CONST</code>. Then <code>SND-</code><i>NAME</i> is called with all sound arguments. |
| 431 |
The disadvantage of this scheme is that scalars are expanded into a sample |
| 432 |
stream, which is slower than having a special inner loop where the scalar |
| 433 |
is simply kept in a register, avoiding loads, stores, and addressing |
| 434 |
overhead.</p> |
| 435 |
|
| 436 |
<p>Choice 2 is to generate a different sound operator for each case. The |
| 437 |
naming convention here is to append a string of c's and v's, indicating |
| 438 |
constant (scalar) or variable (signal) inputs. For example, the |
| 439 |
<code>reson</code> operator comes in four variations: <code>reson</code>, |
| 440 |
<code>resoncv</code>, <code>resonvc</code>, and <code>resonvv</code>. The <code>resonvc</code> |
| 441 |
version implements a resonating filter with a variable center frequency (a |
| 442 |
sound type) and a constant bandwidth (a <code>FLONUM</code>). The <code>RESON</code> |
| 443 |
function in Nyquist is an ordinary Lisp function that checks types and calls |
| 444 |
one of <code>SND-RESON</code>, <code>SND-RESONCV</code>, <code>SND-RESONVC</code>, or |
| 445 |
<code>SND-RESONVV</code>. </p> |
| 446 |
|
| 447 |
<p>Since each of these <code>SND-</code> functions performs further selection of |
| 448 |
implementation based on sample rates and the need for scaling, there are 25 |
| 449 |
different functions for computing RESON! So far, however, Nyquist is |
| 450 |
smaller than Common Lisp and it's about half the size of Microsoft Word. |
| 451 |
Hopefully, exponential growth in memory density will outpace linear (as a |
| 452 |
function of programming effort) growth of Nyquist.</p> |
| 453 |
|
| 454 |
<p></p> |
| 455 |
<hr> |
| 456 |
<a href = "part16.html">Previous Section</a> | <a href = "part18.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> |
| 457 |
</body></html> |