## Nyquist

**Uvod v programiranje v Nyquistu in Lispu**- Programiranje v jeziku Nyquist
- Ustvarjanje vtičnikov Nyquist

Od različice 1.1.1 naprej Audacity omogoča uporabo programskega jezika Nyquist za pisanje lastnih vtičnikov z učnki za Audacity. Za razliko od vtičnikov VST in LADSPA lahko vtičnike Nyquist napišete v navadnem urejevalniku besedila in jih ni potrebno prevajati.

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:

- CMU Computer Music Project Software - download the full Nyquist here.

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.0x 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. 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, but `first`

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

Sledi seznam nekaterih osnovnih funkcij jezika lisp, ki jih boste potrebovali. Če želite popoln seznam funkcij Lisp / Nyquist, si oglejte priročnik 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.**

#### Matematične funkcije

`(+ a b)` | seštevanje |

`(- a b)` | odštevanje |

`(* a b)` | množenje |

`(/ a b)` | deljenje |

`(truncate a b)` | zaokroževanje navzdol na celo število (dno) |

`(float a b)` | celo število v plavajočo vejico |

`(rem a b c ...)` | ostanek |

`(min a b c ...)` | najmanj |

`(max a b c ...)` | največ |

`(abs a)` | absolutna vrednost |

`(random n)` | naključno celo število med 1 in n-1 |

`(sin a b)` | sinus |

`(cos a b)` | kosinus |

`(tan a b)` | tangens |

`(expt a b)` | exponent (a to the power of b) |

`(sqrt a b)` | kvadratni koren |

`(< a b)` | preveri, če je a manjši od b |

`(<= a b)` | preveri, če je a manjši ali enak b |

`(> a b)` | preveri, če je a večji od b |

`(>= a b)` | preveri, če je a večji ali enak b |

`(= a b)` | preveri enakost |

`(/= a b)` | preveri neenakost |

#### Seznamske funkcije

`(first l)` | prvi element na seznamu (car) |

`(rest l)` | preostanek seznama (cdr) |

`(reverse l)` | preobrni seznam |

`(list a b ...)` | ustvari seznam |

`(append l1 l2)` | pripni dva seznama |

`(length l)` | dolžina seznama |

`(maplist function l)` | izvedi funkcijo na vseh elementih seznama |

#### Kontrolnik

`(if expr expr1 expr2)` | če je expr resnično, oceni izraz expr1, sicer oceni izraz expr2 |