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

Inverting audio using Nyquit?

Permanent link to this post Posted by 8bit_coder » Sun Dec 03, 2017 4:57 am

Hello all,

I'm working on a nyquist plugin that uses
Code: Select all
(s-min s 0.5)

to cut off anything above the .5 mark of audio.

Now I'm trying to have an option if you want to remove the underside .5 of the audio, the plugin inverts the audio first, applies the effect, then inverts the audio again, essentially making the bottom side cut off. I've looked everywhere and examined different plugins, and none seem to show how to invert the audio using nyquist.

Like, what command in nyquist simply inverts the audio?
8bit_coder
 
Posts: 6
Joined: Sun Dec 03, 2017 4:31 am
Operating System: Windows 8 or 8.1

Re: Inverting audio using Nyquist?

Permanent link to this post Posted by steve » Sun Dec 03, 2017 12:03 pm

8bit_coder wrote:Like, what command in nyquist simply inverts the audio?

It's probably simpler than you are expecting.

I guess that you know that (PCM) digital audio is just a series of numeric "sample" values, that represent the voltage of an audio signal at points in time:

Image

As shown by the (default) vertical scale on the left end of the audio track, Audacity treats samples as values between +1 and -1. Dead silence is when all samples have a value of zero.

When inverting a waveform, all we are doing is to changing positive values to negative, and negative values to positive. In other words, multiply by -1 (minus one).
Code: Select all
(mult -1 sig) ; where 'sig' is the signal being inverted


If you want to clip both the top and bottom of the waveform, use the CLIP function:
Code: Select all
(clip sig 0.4)


Aside:
I notice that you are using the legacy syntax of "s" to represent the track audio, or at least I assume that you are using "S" to represent the track audio. Generally it's better to use the new (version 4) syntax. The "*track*" keyword avoids conflicting with the Nyquist global value 0.25 (http://www.audacity-forum.de/download/e ... ml#index49), and it avoids ambiguity. Also, most of the Audacity "property list" variable are only available when using version 4 (or later) syntax (http://wiki.audacityteam.org/wiki/Nyqui ... erty_Lists)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 45371
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 » Sun Dec 03, 2017 9:15 pm

Well, so far the plugin looks like this now:
Code: Select all
;nyquist plug-in
;version 4
;type process
;preview linear
;name "Diode Clipping..."
;action "Applying Diode Clipping..."
;author "8bit-coder"
;copyright "No copyright"

;control amt "Amount of clipping" real "0-1" 0.5 0 1

(clip sig amt)


Now it looks like this when loaded up into audacity:

Image

But when I click OK to apply the effect, it spits out:

Image

There's also another problem, I want to be able to have a choice on whether to clip one side, or both, and also to have positive or negative side clipped as a result of one side only.

For clipping one side or both, I figured out by reading other plugins that you need to have:

Code: Select all
;control side "Clipping type" choice "Positive Only,Negative only,Both" 0


but I can't figure out how to take the variable side and have conditions for it to work. I am a java programmer however, but it's still difficult.

The other problem is how to take the amt variable from:

Code: Select all
;control amt "Amount of clipping" real "0-1" 0.5 0 1


and use it in

Code: Select all
(clip sig amt)


and

Code: Select all
(s-min s amt)


(the reason this old code is because I still want to have the ability to also only clip one side)
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 » Mon Dec 04, 2017 2:51 am

In version 4 syntax, the sound from a selected mono audio track, or an array of two sounds from a selected stereo track, is bound to the variable *TRACK*.
(http://wiki.audacityteam.org/wiki/Nyqui ... 2ATRACK.2A)

To get your "diode clipping" to work, either:
Code: Select all
(setf sig *track*)
(clip sig amt)

or more simply, just:
Code: Select all
(clip *track* amt)


similarly:
Code: Select all
(s-min *track* amt)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 45371
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 » Mon Dec 04, 2017 3:04 am

8bit_coder wrote:
Code: Select all
 ;control side "Clipping type" choice "Positive Only,Negative only,Both" 0

but I can't figure out how to take the variable side and have conditions for it to work.


From the control, the variable "side" will have a value of 0, 1 or 2, depending on which choice is selected.
To translate that into a control construct, there are several options (http://www.audacity-forum.de/download/e ... an-019.htm)
Code: Select all
(if (= side 0)
    (do-something...)
    ; else
    (if (= side 1)
        (do-something-else)
        (do-third-option...)))

(the line beginning with a semicolon is a comment)

Code: Select all
(cond
  ((= side 0) (do-something...))
  ((= side 1) (do-something-else))
  (t (do-third-option...)))


or probably the best way
Code: Select all
(case side
  (0 (do-something...))
  (1 (do-something-else))
  (t (do-third-option...)))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 45371
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 » Mon Dec 04, 2017 4:01 am

Ok, now full plugin with an added "tube distortion" effect:

Code: Select all
;nyquist plug-in
;version 4
;type process
;preview linear
;categories "http://lv2plug.in/ns/lv2core#ModulatorPlugin"
;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

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

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


But now, the only problem is that everything works right(all options) except the negative only. What I did for the negative only, is to flip the waveform, clip it, then flip it back. But the problem is that it doesn't do anything to the waveform. No clip, not even an error. Also, if I change

Code: Select all
(s-min *track* amt)


to
Code: Select all
(s-min *track* -amt)


which sounds logical, but it doesn't do anything. Only removing the (mult -1 sig) parts will allow it to clip successfully, but not the underside, effectively the same as the positive option.

What I think is happening is that you can't have multiple commands in a single case, yet the tube distortion case has many commands inside one condition.

Aside from code, I changed the name of the plugin to a more appropriate Clipper(Since all it does is clip waveforms).

So that's all. All I need is to get the negative part working, then it will be a complete plugin that I will post in the plugins to test section of the forumn.
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 8bit_coder » Mon Dec 04, 2017 4:25 am

I've also run into another problem. As you see, I have added a rectify function(which is really just a diode) to move the negative parts to the top. There are 2 problems:

1. The (mult -1 sig) doesn't work just like my last post said(although putting (mult -1 *track*) in the nyquist prompt works, it doesn't work for the negative clip part still!
2. If I choose tube distortion, nothing happens, but when I switch it's spot around, it works. Vice versa with the rectifier function. It works when the tube distortion doesn't, but if I flip the tube distortion code above the rectifier code, the tube distortion works, but the rectifier doesn't, no matter what options I select.

Anyways, here's the code so far now:

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 to top,rectify to bottom,None" 0

(case side
   (0 (s-min *track* amt))
   (1 (mult -1 sig)(s-min *track* amt)(mult -1 sig))
   (2 (clip *track* amt))
   (3 (s-min *track* 1)))
   
(case rectify
   (0 (s-abs *track*))
   (1 (mult -1 sig)(s-abs *track*)(mult -1 sig))
   (2 (s-min *track* 1)))

(case tube
   (0 (setq drive 0.2)(hp (sim (mult (- 1 drive )(s-min *track* 0))(mult (+ 1 drive)(s-max *track* 0)))10))
   (1 (s-min *track* 1)))
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 » Mon Dec 04, 2017 10:54 am

8bit_coder wrote:
Code: Select all
(1 (s-min *track* 1)))

What does this do?

If the signal is "valid" (not exceeding 0dB), then *track* will be less than 1, so this code does nothing, so why is it there?


8bit_coder wrote:
Code: Select all
(1 (mult -1 sig)(s-min *track* amt)(mult -1 sig))

What is "sig" in this code?

Try running the plug-in with the debug button and the option "Negative only" and you will see the error:
Code: Select all
error: unbound variable - SIG

That indicates that the variable "sig" has not been bound to anything - it doesn't have a value, it's just an undefined symbol.


8bit_coder wrote:
Code: Select all
(s-min *track* -amt)

No that's not logical in Nyquist / LISP.
Try running this code in the Nyquist Prompt with the debug button:
Code: Select all
(setf arg 4)
(print -arg)

The debug output says:
Code: Select all
error: unbound variable - -ARG

The symbol "-arg" is NOT the inverse of "arg". It is just another symbol, unrelated to "arg".
To get the inverse of "arg" you could write:
Code: Select all
(mult -1 arg)

or more simply:
Code: Select all
(- arg)

(in the latter case, note the space between "-", which is the function name, and "arg" which is the argument being passed to the function.)

Even then it does not really make much sense. Let's put in a value:
Code: Select all
(setf amt 0.5)
(s-min *track* (- amt))

What this will do, is to return the minimum of *track* and -0.5. Try it, and I think it will become clear.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 45371
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 » Mon Dec 04, 2017 11:07 am

While developing Nyquist scripts, get in the habit of using the Debug button.
If there is no debug output (empty window), then that shows that no errors (or other debug information) has occurred, which is usually a sign that everything went correctly (or nothing happened at all :? )

When there is an error, don't try to understand all of the debug output, just look at the first few lines - they will often give a clue to what went wrong.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 45371
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 » Mon Dec 04, 2017 6:34 pm

Well, now I updated the code to:
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)))


But I still can't figure out why nothing works, other than the tube distortion. Even using the debug button, there is nothing. When I used each command in the nyquist window, they all worked, but now they don't. I can't figure out what to do at this point. I've even tried removing case rectify and tube, but now the side case won't work anymore. It's just really strange. One note is that the solution you provided for the negative amount of side, only made it clip to the negative part. That's the problem. It clipped all the way down. What I'm trying to do is to flip the waveform, clip it, then flip it back, so it only clips the underside, not all the way from the positive to the under side. :?
8bit_coder
 
Posts: 6
Joined: Sun Dec 03, 2017 4:31 am
Operating System: Windows 8 or 8.1

Next

Return to Nyquist



Who is online

Users browsing this forum: No registered users and 2 guests