Introduction to Nyquist and Lisp Programming

From Audacity Wiki
Revision as of 17:03, 24 December 2016 by Galeandrews (talk | contribs) (I can't see the point linking to copy of the pages, so changed to internal links)
Jump to: navigation, search


  1. Introduction to Nyquist and Lisp Programming
  2. Programming in Nyquist
  3. Creating Nyquist Plug-ins

Beginning with version 1.1.1, Audacity 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 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:

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:

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.


Nyquist is based on Lisp. If you have programmed in Lisp before, you can skim this section or go directly to the next page. 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

(+ a b) addition
(- a b) subtraction
(* a b) multiplication
(/ a b) division
(truncate a b) round down to integer (floor)
(float a b) integer to floating-point
(rem a b c ...) remainder
(min a b c ...) minimum
(max a b c ...) maximum
(abs a) absolute value
(random n) random integer between 1 and n-1
(sin a b) sine
(cos a b) cosine
(tan a b) tangent
(expt a b) exponent (a to the power of b)
(sqrt a b) square root
(< a b) test for a less than b
(<= a b) test for a less than or equal to b
(> a b) test for a greater than b
(>= a b) test for a greater than or equal to b
(= a b) test for equality
(/= a b) test for inequality

List functions

(first l) first element of a list (car)
(rest l) rest of the list (cdr)
(reverse l) reverse a list
(list a b ...) construct a list
(append l1 l2) append two lists
(length l) length of a list
(maplist function l) apply a function to every element in a list


(if expr expr1 expr2) if expr is true, evaluates expr1, otherwise evaluates expr2

Programming in Nyquist