### Re: Inverting audio using Nyquit?

Posted:

**Tue Dec 05, 2017 10:35 am**I think you're missing how program flow works in Nyquist / Lisp. It's actually not much different from most other scripting languages.8bit_coder wrote:But I still can't figure out why nothing works, other than the tube distortion

(The examples below may all be run from the Nyquist Prompt effect)

The script progresses from the top of the page to the bottom of the page.

Function definitions are ignored until the function is called. When called, the function is interpreted, starting at the top and working its way down, following the directives of any flow control directives that it encounters (such as loops and conditionals).

Let's take an example that can be run in the Nyquist Prompt.

Example 1:

Code: Select all

```
(defun x (abc)
;; This function would return the number '42' if called
;; but it is not called in this script, so it is redundant (does nothing)
42)
(defun y ()
;; This function returns the number 99 when called.
99)
(setf val 1) ; sets 'val' to 1
(setf val 2) ; sets 'val' to 2. The previous line is now redundant
(print 3) ; prints 99, which can be seen in the debug window
(y) ; calls the function 'y', but we haven't done anything with the returned value
(+ 2 3) ; returns the number 5.
;; Nyquist scripts return only the final value to Audacity. In this case, the number 5.
;; The rest of the code is valid, so there are no error messages in the debug window,
;; but the only two lines that actually do anything are the PRINT statement (which gives
;; us the number 3 in the debug window), and the last line (which returns the value 5)
```

Code: Select all

```
;nyquist plug-in
;version 4
;type process
;preview linear
;name "Clipper..."
;action "Applying Clipper..."
;author "8bit_coder"
;copyright "No copyright"
;control amt "Length of clipping(0 is most, 1 is least)" real "0-1" 0 0 1
;control side "Clipping type" choice "Positive Only,Negative only,Both,None" 0
;control tube "Tube distortion" choice "Yes,No" 0
;control rectify "Rectification" choice "Rectify,None" 0
(case side
(0 (s-min *track* amt))
(1 (mult -1 amt)(s-min *track* amt)(mult -1 amt))
(2 (clip *track* amt)))
(case rectify
(0 (s-abs *track*)))
(case tube
(0 (setq drive 0.2)(hp (sim (mult (- 1 drive )(s-min *track* 0))(mult (+ 1 drive)(s-max *track* 0)))10)))
```

(reformatted for readability)

Code: Select all

```
(setq drive 0.2)
(hp (sim (mult (- 1 drive )(s-min *track* 0))
(mult (+ 1 drive)(s-max *track* 0)))
10)
```

The expressions:

Code: Select all

```
(case side
(0 (s-min *track* amt))
(1 (mult -1 amt)(s-min *track* amt)(mult -1 amt))
(2 (clip *track* amt)))
```

Code: Select all

```
(case rectify
(0 (s-abs *track*)))
```

If you want to return clipped audio, OR, tube distorted audio, OR, rectified audio, then you need to somehow select which one to return to Audacity.

Example 2:

Code: Select all

```
;control function-choice "Choose a function to run" choice "Add,Subtract,Multiply" 0
;control val1 "First value" int "" 5 0 10
;control val2 "Second value" int "" 5 0 10
(defun my-add (x y)
"Returns the sum of x and y."
(+ x y))
(defun my-diff (x y)
"Returns the difference of x and y."
(- x y))
(defun my-mult (x y)
"Returns the product of x and y."
(* x y))
;; Debug: which choice was selected:
(format t "Selected function: ~a" function-choice)
;; Select which function to run, according to the
;; value of 'function-choice'.
;; The CASE special form will return the value of
;; the last expression of the matching case
;; http://www.audacity-forum.de/download/edgar/nyquist/nyquist-doc/xlisp/xlisp-ref/xlisp-ref-047.htm
(case function-choice
; When value is 0, call the function 'my-add' with arguments 'val1' and 'val2'
(0 (my-add val1 val2))
; When value is 1, call the function 'my-diff' with arguments 'val1' and 'val2'
(1 (my-diff val1 val2))
; Ootherwise (catch all), call the function 'my-mult' with arguments 'val1' and 'val2'
(t (my-mult val1 val2)))
```

Example 3:

Code: Select all

```
;control function-choice "Choose a function to run" choice "X+(Y*Z),Y+(X*Z),Z+(X*Y)" 0
;control val1 "X value" int "" 1 0 10
;control val2 "Y value" int "" 2 0 10
;control val3 "Z value" int "" 3 0 10
(defun my-add (x y)
"Returns the sum of x and y."
(+ x y))
(defun my-mult (x y)
"Returns the product of x and y."
(* x y))
;; Debug: which choice was selected:
(format t "Selected function: ~a" function-choice)
;; Select how to combine the functions according to
;; the value of 'function-choice'.
(case function-choice
(0 (my-add val1 (my-mult val2 val3)))
(1 (my-add val2 (my-mult val1 val3)))
(t (my-add val3 (my-mult val1 val2))))
```