Inverting audio using Nyquit?

Using Nyquist scripts in Audacity.
Post and download new plug-ins.

If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like

Re: Inverting audio using Nyquit?

Permanent link to this post Posted by steve » Tue Dec 05, 2017 10:35 am

8bit_coder wrote:But I still can't figure out why nothing works, other than the tube distortion

I think you're missing how program flow works in Nyquist / Lisp. It's actually not much different from most other scripting languages.
(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)


So now looking at your script:
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)))

The value that is returned to Audacity is (only) the result from:
(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)))

and
Code: Select all
(case rectify
   (0 (s-abs *track*)))

both have return values, but they are redundant because you don't do anything with those values. They are in no way included in the value that is returned to Audacity.

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)))



If you want to return a combination of the clipped, tube distorted and rectified audio, then you need to decide in what way you want to combine them.

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))))

9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 45379
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Inverting audio using Nyquit?

Permanent link to this post Posted by steve » Tue Dec 05, 2017 11:07 am

8bit_coder wrote:What I'm trying to do is to flip the waveform, clip it, then flip it back, so it only clips the underside

Writing it out in longhand:
Code: Select all
;version 4
(setf threshold 0.5)

(setf inverted (mult -1 *track*))
(setf clipped (s-min threshold inverted))
(mult -1 clipped)  ; Flip it back. This is the returned value


Writing it out all in one statement (bad style - not very readable)
Code: Select all
;version 4
(setf threshold 0.5)
(mult -1 (s-min threshold (mult -1 *track*)))


Create a temporary local variable to hold the intermediate "inverted" value:
Code: Select all
;version 4
(setf threshold 0.5)

(let ((inverted (mult -1 *track*)))
  (mult -1 (s-min threshold inverted)))


But better than any of the above:
Code: Select all
;version 4
(setf threshold 0.5)

(s-max (- threshold) *track*)



Extending the idea:

Define a function to clip either positive or negative peaks
Code: Select all
;version 4
;control threshold "Clip track at value" float "" -0.5 -1 1

(defun clip-at (sig thresh)
  "Clip either +ve or -ve peaks of SIG at value THRESH"
  (if (> thresh 0)
      (s-min sig thresh)
      (s-max sig (- thresh))))

; Call the function
(clip-at *track* threshold)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 45379
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Inverting audio using Nyquit?

Permanent link to this post Posted by 8bit_coder » Thu Dec 14, 2017 3:04 am

Ok, so I changed the code so it chooses which option to do:

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 option "Which option to do" choice "Clipping Positive,Both,Tube Distortion,Rectification" 0

(defun clipp
  (s-min *track* amt))
 
(defun clipb
  (clip *track* amt))

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

(defun rectifyd
  (s-abs *track*))
   
(case option
   (0 clipp)
   (1 clipb)
   (2 tubed)
   (3 rectifyd))


but the problem now is that audacity gives the error(via using the debug button :D )

Code: Select all
error: bad formal argument list
Function: #<FSubr-DEFUN: #13331288>
Arguments:
  TUBED
  (SETQ DRIVE 0.2)
  (HP (SIM (MULT (- 1 DRIVE) (S-MIN *TRACK* 0)) (MULT (+ 1 DRIVE) (S-MAX *TRACK* 0))) 10)
1> RECTIFYD
1> error: unbound variable - CLIPP
if continued: try evaluating symbol again
Function: #<FSubr-CASE: #13331138>
Arguments:
  OPTION
  (0 CLIPP)
  (1 CLIPB)
  (2 TUBED)
  (3 RECTIFYD)
Function: #<FSubr-DEFUN: #13331288>
Arguments:
  TUBED
  (SETQ DRIVE 0.2)
  (HP (SIM (MULT (- 1 DRIVE) (S-MIN *TRACK* 0)) (MULT (+ 1 DRIVE) (S-MAX *TRACK* 0))) 10)
2> 1>


and I tried to comprehend the error, but I can't really understand it, other than unbound variable CLIPP, which is strange. And that's as far as I've gotten. Sorry for the late reply as well.
8bit_coder
 
Posts: 6
Joined: Sun Dec 03, 2017 4:31 am
Operating System: Windows 8 or 8.1

Re: Inverting audio using Nyquit?

Permanent link to this post Posted by steve » Thu Dec 14, 2017 11:45 am

8bit_coder wrote:
Code: Select all
error: bad formal argument list

That means that there's something wrong with the "arguments" (the "parameters") of a function.
All functions are defined with a list of arguments, though in some cases it can be an empty list.

Example:
Code: Select all
(+ 3 2) ; The function is "+" and there are two arguments, "3" and "2"
(+ 1 2 3 4 5) ; The same function with a list of 5 arguments
(terpri)  ; Function with a list of no arguments - prints a new line


When defining a function. the syntax is:
Code: Select all
(defun name-of-function (list-of-arguments)
  function-body


When defining a function that tales no arguments (a list of length zero), the syntax is:
Code: Select all
(defun name-of-function ()
  function-body


8bit_coder wrote:
Code: Select all
Arguments:
  TUBED
  (SETQ DRIVE 0.2)


Seems to indicate a problem with "tubed", so let's look at that in your code:

8bit_coder wrote:
Code: Select all
(defun tubed
  (setq drive 0.2)
(hp (sim (mult (- 1 drive )(s-min *track* 0))
         (mult (+ 1 drive)(s-max *track* 0)))
    10))


You are defining a function called "tubed", but where is the list of arguments? Nyquist will look at this code and think that "(setq drive 0.2)" is the list, which will confuse the heck out of Nyquist, and trigger an error.

Same thing with your other function definitions - you need to define the argument list.
More information here: http://www.audacity-forum.de/download/e ... ef-087.htm
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 45379
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Previous

Return to Nyquist



Who is online

Users browsing this forum: No registered users and 2 guests