| 1 |
<!DOCTYPE html> |
| 2 |
<html><head><title>Developing and Debugging in 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 = "part13.html">Previous Section</a> | <a href = "part15.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 = "154"><h2>Developing and Debugging in Nyquist</h2></a><a name="index1139"></a><a name="index1140"></a> |
| 11 |
<p>There are a number of tools, functions, and techniques that can help to debug Nyquist programs. Since these are described in many places |
| 12 |
throughout this manual, this chapter brings together many suggestions and techniques for developing code and debugging. You <i>really</i> |
| 13 |
should read this chapter before you spend too much time with Nyquist. Many problems that you will certainly run into are addressed here.</p> |
| 14 |
<a name = "155"><h3>Debugging</h3></a> |
| 15 |
<p>Probably the most important debugging tool is the backtrace. There are |
| 16 |
two kinds of backtrace: one for SAL, and one for Lisp.</p> |
| 17 |
|
| 18 |
<p>SAL mode is actually just an XLISP function (<code>sal</code>) that reads |
| 19 |
input and evaluates it. When SAL encounters an error, it normally |
| 20 |
prints a trace of the SAL stack (all the active functions written in SAL), |
| 21 |
exists the current command, and reads the next command.</p> |
| 22 |
|
| 23 |
<p>If you call XLISP functions from SAL, including most Nyquist sound |
| 24 |
processing functions, and an error occurs within these XLISP functions, |
| 25 |
you will only see the SAL function that called the XLISP functions |
| 26 |
listed in the stack trace. Sometimes you need more details.</p> |
| 27 |
|
| 28 |
<p>When Nyquist encounters an error when it is not running SAL, it |
| 29 |
normally suspends execution and prints an |
| 30 |
error message. To find out where in the program the error occurred |
| 31 |
and how you got there, start by typing <code>(bt)</code>. This will print |
| 32 |
out the last several function calls and their arguments, which is |
| 33 |
usually sufficient to see what is going on.</p> |
| 34 |
|
| 35 |
<p>In order for <code>(bt)</code> to work, you must have a couple of global |
| 36 |
variables set: <code>*tracenable*</code> is ordinarily set to <code>NIL</code>. |
| 37 |
If it is true, then a backtrace is automatically printed when an |
| 38 |
error occurs; <code>*breakenable*</code> must be set to <code>T</code>, as |
| 39 |
it enables the execution to be suspended when an error is |
| 40 |
encountered. If <code>*breakenable*</code> is <code>NIL</code> (false), |
| 41 |
then execution stops when an error occurs but the stack is |
| 42 |
not saved and you cannot get a backtrace. Finally, <code>bt</code> |
| 43 |
is just a macro to save typing. The actual backtrace function |
| 44 |
is <code>baktrace</code>, which takes an integer argument telling how |
| 45 |
many levels to print. All of these things are set up by default |
| 46 |
when you start Nyquist.</p> |
| 47 |
|
| 48 |
<p>To get this XLISP backtrace behavior when SAL encounters an error, |
| 49 |
you need to have <code>*breakenable*</code> set while SAL is running. The |
| 50 |
best way to do this is to run within the NyquistIDE program, |
| 51 |
open the Preferences dialog, and choose the desired settings, e.g. |
| 52 |
“Enable XLISP break on SAL error.”</p> |
| 53 |
|
| 54 |
<p>Since Nyquist sounds are executed with a lazy evaluation scheme, some |
| 55 |
errors are encountered when samples are being generated. In this |
| 56 |
case, it may not be clear which expression is in error. Sometimes, it |
| 57 |
is best to explore a function or set of functions by examining |
| 58 |
intermediate results. Any expression that yields a sound can be |
| 59 |
assigned to a variable and examined using one or more of: |
| 60 |
<code>s-plot</code>, <code>snd-print-tree</code>, and of course <code>play</code>. The |
| 61 |
<code>snd-print-tree</code> function prints a lot of detail about the inner |
| 62 |
representaion of the sound. Keep in mind that if you assign a sound |
| 63 |
to a global variable and then look at the samples (e.g. with |
| 64 |
<code>play</code> or <code>s-plot</code>), the samples will be retained in |
| 65 |
memory. At 4 bytes per sample, a big sound may use all of your |
| 66 |
memory and cause a crash.</p> |
| 67 |
|
| 68 |
<p>Another technique is to use low sample rates so that it is easier to |
| 69 |
plot results or look at samples directly. The calls: |
| 70 |
</p> |
| 71 |
<pre> |
| 72 |
set-sound-srate(100) |
| 73 |
set-control-srate(100) |
| 74 |
</pre> |
| 75 |
|
| 76 |
<p> |
| 77 |
set the default sample rates to 100, which is too slow for audio, but useful for examining programs and results. The function |
| 78 |
</p> |
| 79 |
<pre> |
| 80 |
snd-samples(<i>sound</i>, <i>limit</i>) |
| 81 |
</pre> |
| 82 |
|
| 83 |
<p> |
| 84 |
will convert up to <i>limit</i> samples from <i>sound</i> into a Lisp |
| 85 |
array. This is another way to look at results in detail.</p> |
| 86 |
|
| 87 |
<p>The <code>trace</code> function is sometimes useful. It prints the name of |
| 88 |
a function and its arguments everytimg the function is called, and the |
| 89 |
result is printed when the function exits. To trace the osc function, |
| 90 |
type: |
| 91 |
</p> |
| 92 |
<pre> |
| 93 |
trace(osc) |
| 94 |
</pre> |
| 95 |
|
| 96 |
<p> |
| 97 |
and to stop tracing, type <code>untrace(osc)</code>.</p> |
| 98 |
|
| 99 |
<p>If a variable needs a value or a function is undefined, and if <code>*breakenable*</code> was set, you will get a prompt where you can fix |
| 100 |
the error (by setting the variable or loading the function definition) |
| 101 |
and keep going. At the debug (or break) prompt, your input must |
| 102 |
be in XLISP, not SAL syntax. Use <code>(co)</code>, short for <code>(continue)</code> to |
| 103 |
reevaluate the variable or function and continue execution.</p> |
| 104 |
|
| 105 |
<p>When you finish debugging a particular call, you can “pop” |
| 106 |
up to the top level by typing <code>(top)</code>, a short name for <code>(top-level)</code>. |
| 107 |
There is a button named "Top" in the NyquistIDE that takes you back to the |
| 108 |
top level (ready to accept XLISP expressions), |
| 109 |
and another button named "SAL" that puts you back in SAL mode.</p> |
| 110 |
<a name = "156"><h3>Useful Functions</h3></a><dl> |
| 111 |
<dt> |
| 112 |
<code>grindef(<a name="index1141"></a><a name="index1142"></a><i>name</i>)</code> [SAL]<br> |
| 113 |
|
| 114 |
<code>(grindef <i>name</i>)</code> [LISP]</dt> |
| 115 |
<dd>Prints |
| 116 |
a formatted listing of a lisp function. This is often useful to quickly inspect |
| 117 |
a function without searching for it in source files. Do not forget to quote the |
| 118 |
name, e.g. <code>(grindef 'prod)</code>.<br><br> |
| 119 |
<dt><code>args(<a name="index1143"></a><a name="index1144"></a><i>name</i>)</code> [SAL]<br> |
| 120 |
|
| 121 |
<code>(args <i>name</i>)</code> [LISP]</dt> |
| 122 |
<dd>Similar |
| 123 |
to <code>grindef</code>, this function prints the arguments to a function. This may |
| 124 |
be faster than looking up a function in the documentation if you just need a |
| 125 |
reminder. For example, <code>(args 'lp)</code> prints “(LP S C),” which may help you |
| 126 |
to remember that the arguments are a sound (S) followed by the cutoff (C) |
| 127 |
frequency. |
| 128 |
</dd></dl> |
| 129 |
<p>The following functions are useful short-cuts that might have been included in |
| 130 |
XLISP. They are so useful that they are defined as part of Nyquist.</p> |
| 131 |
<dl> |
| 132 |
<dt> |
| 133 |
<code>incf(<a name="index1145"></a><a name="index1146"></a><i>symbol</i>)</code> [SAL]<br> |
| 134 |
|
| 135 |
<code>(incf <i>symbol</i>)</code> [LISP]</dt> |
| 136 |
<dd>Increment <i>symbol</i> |
| 137 |
by one. This is a macro, and <i>symbol</i> can be anything that can be set by |
| 138 |
<code>setf</code>. Typically, <i>symbol</i> is a variable: “<code>(incf i)</code>,” but |
| 139 |
<i>symbol</i> can also be an array element: “<code>(incf (aref myarray i))</code>.”<br><br> |
| 140 |
<dt><code>decf(<a name="index1147"></a><a name="index1148"></a><i>symbol</i>)</code> [SAL]<br> |
| 141 |
|
| 142 |
<code>(decf <i>symbol</i>)</code> [LISP]</dt> |
| 143 |
<dd>Decrement <i>symbol</i> |
| 144 |
by one. (See <code>incf</code>, above.)<br><br> |
| 145 |
<dt><code>push(<a name="index1149"></a><i>val</i>, <i>lis</i>)</code> [SAL]<br> |
| 146 |
|
| 147 |
<code>(push <i>val</i> <i>lis</i>)</code> [LISP]</dt> |
| 148 |
<dd>Push <i>val</i> onto <i>lis</i> (a Lisp |
| 149 |
list). This is a macro that is equivalent to writing (in Lisp) |
| 150 |
<code>(setf <i>lis</i> (cons <i>val</i> <i>lis</i>))</code>.<br><br> |
| 151 |
<dt><code>pop(<a name="index1150"></a><i>lis</i>)</code> [SAL]<br> |
| 152 |
|
| 153 |
<code>(pop <i>lis</i>)</code> [LISP]</dt> |
| 154 |
<dd>Remove (pop) the first item from <i>lis</i> (a |
| 155 |
Lisp list). This is a macro that is equivalent to writing (in Lisp) |
| 156 |
<code>(setf <i>lis</i> (cdr <i>lis</i>))</code>. Note that the remaining list is returned, |
| 157 |
not the head of the list that has been popped. Retrieve the head of the list |
| 158 |
(i.e. the top of the stack) using <code>first</code> or, equivalently, <code>car</code>. |
| 159 |
</dd></dl> |
| 160 |
<p>The following macros are useful control constructs.</p> |
| 161 |
<dl> |
| 162 |
<dt> |
| 163 |
<code>while(<a name="index1151"></a><i>test</i>, <i>expr1</i>, <i>expr2</i>, <span style="font-style:normal">...</span>)</code> [SAL]<br> |
| 164 |
|
| 165 |
<code>(while <i>test</i> <i>expr1</i> <i>expr2</i> <span style="font-style:normal">...</span>)</code> [LISP]</dt> |
| 166 |
<dd>A conventional |
| 167 |
“while” loop. If <i>test</i> is true, evaluate expressions |
| 168 |
(<i>expr1</i>, <i>expr2</i>, etc.) and repeat. If <i>test</i> is false, return. This |
| 169 |
expression evaluates to NIL unless the expression <code>(return <i>expr</i>)</code> |
| 170 |
is evaluated, in which case the value of <i>expr</i> is returned. In SAL, the |
| 171 |
loop statement is preferred.<br><br> |
| 172 |
<dt><code>when(<a name="index1152"></a><i>test</i>, <i>action</i>)</code> [SAL]<br> |
| 173 |
|
| 174 |
<code>(when <i>test</i> <i>action</i>)</code> [LISP]</dt> |
| 175 |
<dd>A conventional “if-then” |
| 176 |
statement. If <i>test</i> is true, <i>action</i> is evaluated and returned. Otherwise, |
| 177 |
NIL is returned. (Use <code>if</code> or <code>cond</code> to implement |
| 178 |
“if-then-else” and more complex conditional forms. |
| 179 |
</dd></dl> |
| 180 |
<p>It is often necessary to load a file <i>only if</i> it has not already been |
| 181 |
loaded. For example, the <code>pianosyn</code> library loads somewhat slowly, so if |
| 182 |
some other file already loaded it, it would be good to avoid loading it |
| 183 |
again. How can you load a file once? Nyquist does not keep track of files |
| 184 |
that are loaded, but you must be loading a file to define some function, |
| 185 |
so the idea is to tell Nyquist "I require <i>function</i> from <i>file</i>"; if |
| 186 |
the function does not yet exist, Nyquist satisfies the requirement by loading |
| 187 |
the file. |
| 188 |
<dl> |
| 189 |
<dt> |
| 190 |
<code>require-from(<a name="index1153"></a><a name="index1154"></a><i>fnsymbol</i>, |
| 191 |
<i>filename</i> [, <i>path</i>])</code> [SAL]<br> |
| 192 |
|
| 193 |
<code>(require-from <i>fnsymbol</i> <i>filename</i> [<i>path</i>])</code> [LISP]</dt> |
| 194 |
<dd>Tests whether <i>fnsymbol</i>, an unquoted |
| 195 |
function name, is defined. If |
| 196 |
not, <i>filename</i>, a <code>STRING</code>, |
| 197 |
is loaded. Normally <i>fnsymbol</i> is a function that will be called from |
| 198 |
within the current file, and <i>filename</i> is the file that defines |
| 199 |
<i>fnsymbol</i>. The <i>path</i>, if a <code>STRING</code>, is prepended to <i>filename</i>. |
| 200 |
If <i>path</i> is <code>t</code> (true), then the directory of the current file is |
| 201 |
used as the path. |
| 202 |
</dd></dl></p> |
| 203 |
|
| 204 |
<p>Sometimes it is important to load files relative to the current file. For example, |
| 205 |
the <code>lib/piano.lsp</code> library loads data files from the <code>lib/piano</code> directory, |
| 206 |
but how can we find out the full path of <code>lib</code>? The solution is: |
| 207 |
<dl> |
| 208 |
<dt> |
| 209 |
<code>current-path(<a name="index1155"></a><a name="index1156"></a><a name="index1157"></a>)</code> [SAL]<br> |
| 210 |
|
| 211 |
<code>(current-path)</code> [LISP]</dt> |
| 212 |
<dd>Returns the full path name of the file that is |
| 213 |
currently being loaded (see <code>load</code>). Returns NIL if no file is being loaded. |
| 214 |
</dd></dl> </p> |
| 215 |
|
| 216 |
<p>Finally, there are some helpful math functions: |
| 217 |
<dl> |
| 218 |
<dt> |
| 219 |
<code>real-random(<a name="index1158"></a><a name="index1159"></a><a name="index1160"></a><i>from</i>, <i>to</i>)</code> [SAL]<br> |
| 220 |
|
| 221 |
<code>(real-random <i>from</i> <i>to</i>)</code> [LISP]</dt> |
| 222 |
<dd>Returns a random <code>FLONUM</code> between <i>from</i> and <i>to</i>. (See also <code>rrandom</code>, which is equivalent to <code>(real-random 0 1</code>)).</p> |
| 223 |
<dt><code>power(<a name="index1161"></a><a name="index1162"></a><i>x</i>, <i>y</i>)</code> [SAL]<br> |
| 224 |
|
| 225 |
<code>(power <i>x</i> <i>y</i>)</code> [LISP]</dt> |
| 226 |
<dd>Returns <i>x</i> raised to |
| 227 |
the <i>y</i> power. |
| 228 |
</dd></dl><hr> |
| 229 |
<a href = "part13.html">Previous Section</a> | <a href = "part15.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> |
| 230 |
</body></html> |