Introduction to Nyquist and Lisp Programming
Nyquist was written by Roger B. Dannenberg and was intended to be used as a complete programming language for audio synthesis and analysis, with support for MIDI, audio recording and playback, file I/O, object-oriented programming, profiling, debugging and more. Audacity uses only a subset of Nyquist’s functionality, allowing you to take simple Nyquist functions and use them to process audio data. Audacity doesn’t include any support for debugging Nyquist code, so if you are trying to write a complicated plug-in, you may find it easier to get the full version of Nyquist and develop there, then turn it into an Audacity plug-in. Nyquist is available from the Carnegie Mellon University Computer Music Project.
You can download the full Nyquist from here CMU Computer Music Project Software. |
Contents
Nyquist
Nyquist allows you to use the Nyquist programming language to write your own plug-in effects for Audacity. Unlike VST and LADSPA plug-ins, Nyquist plug-ins can be written using an ordinary text editor and don’t need to be compiled.
Nyquist supports both a Lisp syntax and a more conventional syntax called SAL. Audacity versions prior to 1.3.8 only support Lisp, but the current Audacity 2.0 series supports both Lisp and SAL. To write plug-ins for use with Audacity, choose the appropriate Nyquist Manual for your version of Audacity and preferred syntax:
- Nyquist version 2.37 Manual – entirely using Lisp syntax
- Nyquist 3.1x Reference Manual – the latest Nyquist Manual (mostly using SAL syntax).
Note that you don’t need to download Nyquist in order to write simple plug-ins to use with Audacity. All the basic instructions you need to work with Lisp and the 2.37 Manual are below. If you want to explore SAL as well, and for the latest Nyquist features in Audacity, see Nyquist Documentation on the Wiki.
Lisp
Nyquist is based on Lisp. If you have programmed in Lisp before, you can skim this section or go directly to the next page: Programming in Nyquist . Otherwise, here’s an extremely brief introduction to Lisp:
In Lisp (and therefore Nyquist), everything is an S-Expression, which is just a list of tokens (words) separated by whitespace and enclosed in parentheses. The name of the function is always the first token in an S-Expression, and all of the other tokens are arguments to this function. Here’s a simple example:
(setf area (* 3.14159 (expt radius 2)))
Let’s break down this example. The outermost S-expression has three members. The first one, setf, is the name of the function (it stands for set-field). setf is used to assign a value to a variable. (There are other similar functions, like set and setq, but setf is the most powerful, so it’s the one we’ll use in our examples.) After setf comes area, which is the name of the variable we’re going to set. Next comes the value to assign to this variable, which in this case is another S-expression.
Lisp doesn’t have any special operators for Math functions – they’re all functions like everything else, using prefix notation, where the name of the function (or operator) comes before its arguments. So instead of 3*7 for the product of 3 and 7, in Lisp you would write (* 3 7). In Nyquist, the expt (exponent) function raises its first argument to the power of the second argument. Therefore (* 3.14159 (expt radius 2)) means 3.14159 times the square of radius, or the formula for the area of a circle.
Rather than typing in this full expression every time, let’s define a function for the area of the circle, that we can call every time we need it:
(defun circlearea (radius) (* 3.14159 (expt radius 2)))
The defun function is used to define a new function. The first argument is the name of the function, in this case circlearea. The second argument is a list of arguments to the function to be defined – this is one of the few cases where you have an S-expression that is not interpreted as a function call. Finally the last expression is the value of the function. Now if we want to compute the area of a circle of radius r, we just need to compute:
(setf area (circlearea r))
An S-expression is just a representation of a list. Lisp uses lists to represent just about everything (the name LISP comes from LISt Processing language), so it’s helpful to know how to manipulate lists. Let’s start by assigning a list of numbers to a variable. You can’t quite do this:
(setf mylist (1 2 3 4 5)) <-- error!
The reason this doesn’t work is that whenever Nyquist sees an S-expression, it tries to evaluate it as a function unless you tell it otherwise. Since there’s no function named “1” that takes arguments (2 3 4 5), this will generate an error. To tell Lisp that you want to treat an S-expression literally, and not to evaluate it as a function, you quote it. In Nyquist, you can quote a list by putting a single quotation mark before it, like this:
(setf mylist \'(1 2 3 4 5))
Nyquist also provides a list function that you can use to construct lists – this is useful if some of the elements of the list are functions:
(setf mylist (list 1 2 3 4 (sqrt 25)))
To get things off of a list, you can use the first and rest functions. (Traditionally, these were called car and cdr, respectively, butfirst and rest are much easier to remember. Both sets of names are supported in Nyquist.) The output of (first mylist) is 1, and the output of (rest mylist) is the list (2 3 4 5). So the second element of the list is (first (rest mylist)).
Lisp function reference
Here’s a list of some of the basic lisp functions you might need. For a complete list of Lisp / Nyquist functions, see the Nyquist version 2.37 Reference Manual.
Note: Symbols in Nyquist (like variable names and function names) are not case-sensitive. They are converted to uppercase internally.
Math functions
Lisp | SAL | Description |
(+ a b) | a + b | addition |
(- a b) | a - b | subtraction |
(* a b) | a * b | multiplication |
(/ a b) | a / b | division |
(truncate expr) | truncate (expr) | round down expr to integer (floor) |
(float expr) | float (expr) | integer expr to floating-point |
(rem a b ...) | rem (a b...) | remainder of a list of numbers |
(min a b ...) | min (a b ...) | minimum |
(max a b ...) | max (a b ...) | maximum |
(abs expr) | abs (expr) | absolute value of a number |
(random n) | random (n) | random integer between 1 and n-1 |
(rrandom) | rrandom () | Floating-point random between 0 and 1 |
(sin expr) | sin (expr) | sine |
(cos expr) | cos (expr) | cosine |
(tan expr) | tan (expr) | tangent |
(expt expr) | expt (expr) | exponent (a to the power of b) |
(sqrt expr) | sqrt (expr) | square root |
(< a b) | a < b | test for a less than b |
(<= a b) | a <= b | test for a less than or equal to b |
(> a b) | a > b | test for a greater than b |
(>= a b) | a >= b | test for a greater than or equal to b |
(= a b) | a = b | test for equality |
(/= a b) | a /= b | test for inequality |
List functions
Lisp | SAL | Description |
---|---|---|
(first expr) | first (expr) | first element of a list (car) |
(rest expr) | rest (expr) | rest of the list (cdr) |
(reverse expr) | reverse (expr) | reverse a list |
(list expr ...) | list (expr...) | construct a list of expressions |
(append expr ...) | append (expr...) | append two or more lists |
(length expr) | length (expr) | length of a list |
(maplist fcn list1 list...) | maplist (fcn list1 list...) | apply a function to every element in a list |
Conditional Expressions
Lisp | SAL | Description |
---|---|---|
(if test-expr true-stmt [false-stmt]) | if test-expr then true-stmt [else false-stmt] | If test-expr is true, evaluates true-stmt, otherwise evaluates false-stmt |
(when test-expr statement) | when test-expr statement | When test-expr is true, evaluate statement |
(unless test-expr statement) | unless test-expr statement | When test-expr is false, evaluate statement |
(cond [(pred1 expr1) [(pred2 expr2) ... ]]) | When pred1is true, evaluate expr1, else if pred2 is true... | |
(case test-expr [(value statement) ... ]) | Evaluate statement for the first value that matches test-expr |