Spectral Bandpass Filter Plugin

Share your Audacity/Nyquist plug-ins here, or test drive the latest plug-ins submitted by Audacity users.

After testing a plug-in from this forum, please post feedback for the plug-in author.
Paul2
Posts: 164
Joined: Wed Sep 04, 2019 1:17 pm
Operating System: macOS 10.15 Catalina or later

Re: Spectral Bandpass Filter Plugin

Post by Paul2 » Wed May 12, 2021 2:45 pm

Thanks for the links, very useful.

Right, I have made the recommended changes to include some error trapping/messaging.
I didn't include one for "center" as it will never have a nil.
It may be a wrong center frequency (Audacity and Nyquist can't guess what the right one should be), but never a nil.

Screenshot below.
If you are happy, will update the download link.

Thanks,
Paul
E2.png
E2.png (45.64 KiB) Viewed 124 times

steve
Site Admin
Posts: 81337
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Spectral Bandpass Filter Plugin

Post by steve » Wed May 12, 2021 11:14 pm

Paul2 wrote:
Wed May 12, 2021 2:45 pm
I didn't include one for "center" as it will never have a nil.
That's not actually true, but it is true that "center-hz" will never be nil unless "bandwidth" is also nil.

Here's an example of where both "center-hz" and "bandwidth" are both NIL:

First Track000.png
First Track000.png (21.47 KiB) Viewed 100 times
Paul2 wrote:
Wed May 12, 2021 2:45 pm
If you are happy
Very happy :) though perhaps you could mention in the comments that the frequency range of the selection sets the (-3dB) bandwidth of the filter, or something like that.

Nice that it has error checking. Many of the early Nyquist plug-ins had no error checking and would just fail silently if used inappropriately, or with unhandled settings.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Paul2
Posts: 164
Joined: Wed Sep 04, 2019 1:17 pm
Operating System: macOS 10.15 Catalina or later

Re: Spectral Bandpass Filter Plugin

Post by Paul2 » Thu May 13, 2021 10:36 am

Good point Steve.

Below, some error checking for "center" as well.

I'm trying to understand the syntax well.
Hence, decided to use nested "if's" instead of "Cond".
There is a problem though, the "Error" print out line also terminates the script,
which I assume is different to a "print" command that will print out the message then continue.

Over and above, there is the pesky detail about using a closing bracket for the first "If".

So the approach taken, I set a dummy variable which then gives me the opportunity to properly end the first "if".
Could probably have also used just a (progn))
Hope that makes sense.

No doubt there are more efficient ways to code this (ahem "Cond"), but for now just need to wrap my head around doing it this way.

Your thoughts?

Code: Select all

(setf center (get '*selection* 'center-hz))
(setf octaves (get '*selection* 'bandwidth))
(if (not octaves)
    "Error.\nA spectral selection is required."
    (setf dummy 1))
(if (not center)
    "Error.\nNo center frequency selected."
    (progn
      (setf q (/ (sqrt (power 2.0 octaves))(- (power 2.0 octaves) 1)))
      (bandpass2 *track* center q)))
 
EDIT:

Thinking about it a bit more, would doing the following be OK?

Code: Select all

(bandpass2 *track* center q))))
Extra ")" to properly terminate the first "if" and thus, negating the need for setf dummy ?

steve
Site Admin
Posts: 81337
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Spectral Bandpass Filter Plugin

Post by steve » Thu May 13, 2021 11:48 am

Paul2 wrote:
Thu May 13, 2021 10:36 am
There is a problem though, the "Error" print out line also terminates the script,
which I assume is different to a "print" command that will print out the message then continue.
An important feature of Nyquist plug-ins / scripts is that they return ONE result to Audacity. This is called "the return value".
They can't display a message and then continue.
If the return value is text (or a number), then Audacity will display that in a message window.
If the return value is a sound, then Audacity will replace the selected audio with the sound.
For full details, see: https://wiki.audacityteam.org/wiki/Nyqu ... urn_Values

Paul2 wrote:
Thu May 13, 2021 10:36 am
Below, some error checking for "center" as well.
Error checking for both center and bandwidth is superfluous. If center is NIL, then bandwidth will also be NIL, and vice versa.

Code: Select all

(if (not octaves)
    "Error.\nA spectral selection is required."
    (setf dummy 1))
(if (not center)
    "Error.\nNo center frequency selected."
    (progn
      (setf q (/ (sqrt (power 2.0 octaves))(- (power 2.0 octaves) 1)))
      (bandpass2 *track* center q)))

If "octaves" is nil, then the first IF clause returns the string, "Error.\nA spectral selection is required.". We're not doing anything with that string (we're not printing it) and there is no unhandled error, so the script continues to the next IF clause.

If "center" is NIL (which it will be when "octaves" is nil), then the second IF clause returns "Error.\nNo center frequency selected.".
The PROGN block is not reached when "center" is nil, so the script has completed and the final return value ("Error.\nNo center frequency selected.") is returned to Audacity.
Because the return value from the entire script is a string value, Audacity displays it in a message box.

You can just leave out the first IF clause because it will never do anything.


About PRINT:
The PRINT function prints to the debug stream and returns the string.

Consider this code:

Code: Select all

(setf my-val (print "Hello World"))
(print "Goodbye")
my-val
Now run the code in the Nyquist Prompt and use the Debug button.

The return value from the script is "my-val", which evaluates to "Hello World", so Audacity displays "Hello World".
Because we used the Debug button, Audacity will then open the Debug window, and we will see:

Code: Select all

"Hello World"
"Goodbye"

"Hello World" was printed to the debug window by the PRINT statement in the first line of the script.
"Goodbye" was printed to the debug window by the PRINT statement in the second line of the script.

If you run the code with the "OK" button rather than the "Debug" button, then you only see the script's return value and not the Debug window.

Hopefully this will make sense when you try it ;)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Paul2
Posts: 164
Joined: Wed Sep 04, 2019 1:17 pm
Operating System: macOS 10.15 Catalina or later

Re: Spectral Bandpass Filter Plugin

Post by Paul2 » Thu May 13, 2021 12:27 pm

OK, I think I got it.

So, Nyquist will only "return" a single thing (text, values, sound) and terminate, essentially then a print also contains an end or terminate, so to speak.
Because the return value from the entire script is a string value, Audacity displays it in a message box.
Got it.

So if the two variables were completely independent, then a "print" (or just "Some Warning Text") if any one was a nil, would stop the script.

I was under the impression that Nyquist was a "dynamic" thing that ran in the background and Audacity was just the GUI for it and it could interact (i.e. output multiple things) and Audacity would display them either as a message or prompt for user input.
By prompt, I mean a prompt after the initial start of execution, for example:

A user selects a certain filter, the script prompts for certain values.
(I know that sliders can set limits but let's overlook that for now).

The script starts and for some reason, one of the values don't make sense, the script could then re-prompt the user with two choices:

- Correct the value, or
- Terminate.

Understand now that Audacity waits for the first output from Nyquist and at that point it takes over again and the script stops.
At this point, all script data in RAM is cleared except for "scratch".
If the above is true, then is it a fair assumption that Nyquist does all the garbage collecting and no need for the user/script to invoke it?

Wow, this is more fun than I expected...seriously, I know, I'm a nerd, what can I say.

Will update the script accordingly.

steve
Site Admin
Posts: 81337
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Spectral Bandpass Filter Plugin

Post by steve » Thu May 13, 2021 1:24 pm

Paul2 wrote:
Thu May 13, 2021 12:27 pm
So, Nyquist will only "return" a single thing (text, values, sound) and terminate, essentially then a print also contains an end or terminate, so to speak.
Yes, Nyquist will only return one single thing. (a stereo sound is ONE array containing two sounds).

PRINT returns a string value, and also has a "side effect". The side effect is that it prints to the debug stream.

If you run this in the Nyquist Prompt:

Code: Select all

(print "Hello")
you will see that it returns "Hello", which Audacity displays in a message window.

If you run this in the Nyquist Prompt:

Code: Select all

(print "Hello")
(print "World")
You will see that the script returns the final value: "World",
but if you use the Debug button, you will see both side effects:

Code: Select all

"Hello"
"World"
(This can be extremely useful when debugging a script).
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

steve
Site Admin
Posts: 81337
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Spectral Bandpass Filter Plugin

Post by steve » Thu May 13, 2021 1:36 pm

Paul2 wrote:
Thu May 13, 2021 12:27 pm
I was under the impression that Nyquist was a "dynamic" thing that ran in the background and Audacity was just the GUI for it and it could interact
Not yet, though that "may" come in the future.

Nyquist is really a separate application (a computer language), that was shoehorned into Audacity way back in the early history of Audacity's development.

The basic model is:
Audacity passes information to Nyquist.
Nyquist runs, then passes something back to Audacity.
Audacity tries to do something sensible with what it gets back from Nyquist - if it can't, then it throws an error.

An example of Nyquist returning something that Audacity can't handle:

Code: Select all

(list 1  2)  ;a list with two integer elements
;; Audacity does not know what to do with this, so 
;; pops up an error message: "Nyquist returned a list."
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Paul2
Posts: 164
Joined: Wed Sep 04, 2019 1:17 pm
Operating System: macOS 10.15 Catalina or later

Re: Spectral Bandpass Filter Plugin

Post by Paul2 » Thu May 13, 2021 1:47 pm

(This can be extremely useful when debugging a script).
Absolutely.
Strategically placed print commands in the script will be very helpful.

You have helped me tremendously, thank you.
I'm sure it will be equally as helpful to others wanting to start with Nyquist.
Nyquist is really a separate application (a computer language), that was shoehorned into Audacity way back in the early history of Audacity's development.
And thank you to the person/people that made that decision.
It gives Audacity so much more versatility.

I am enjoying Lisp but, still of the opinion that if a "simpler" language was included, or some kind of converter/parser from say Basic or C to Lisp/Xlisp, it would have a much greater take-up.
Even if that converter/parser was external to Audacity, it would be great.
Open the converter, write the code in Basic or C, it generates the Lisp code, copy and paste into the Nyquist prompt.
Not that much of a hassle.

I did look at SAL, but since Lisp seems to be more common, there are many more examples and references for Lisp.
Last edited by Paul2 on Thu May 13, 2021 1:49 pm, edited 1 time in total.

steve
Site Admin
Posts: 81337
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Spectral Bandpass Filter Plugin

Post by steve » Thu May 13, 2021 1:48 pm

Paul2 wrote:
Thu May 13, 2021 12:27 pm
is it a fair assumption that Nyquist does all the garbage collecting and no need for the user/script to invoke it?
In most cases you don't need to worry about garbage collection. Nyquist handles this itself.

There are some cases where garbage collection are a concern, but that's probably best left 'till later as it gets quite complex and technical. The classic case is that when trying to process very long tracks, you have to be careful to allow Nyquist to garbage collect while processing the track - many of the early Nyquist plug-ins failed to do this, and are unable to process very long tracks.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

steve
Site Admin
Posts: 81337
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Spectral Bandpass Filter Plugin

Post by steve » Thu May 13, 2021 1:50 pm

Paul2 wrote:
Thu May 13, 2021 1:47 pm
And thank you to the person/people that made that decision.
It gives Audacity so much more versatility.
That was Dominic Mazzoni and Professor Roger Dannenberg - Audacity's founders.
I'm also really glad that they did :ugeek:
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply