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: 154
Joined: Wed Sep 04, 2019 1:17 pm
Operating System: macOS 10.15 Catalina or later

Spectral Bandpass Filter Plugin

Post by Paul2 » Tue May 11, 2021 2:16 pm

Hi all,

Here is a plugin which I hope will be useful to some.
My first foray into Lisp territory, with gracious help from Steve.

It applies a bandpass filter effect to a selection in spectral view/edit.
Essentially, it's the opposite of SpectralEditMulti.ny (by Paul Licameli and updated by Steve Daulton) which notches out the selection.

The passband (Q) is computed and applied automatically.
The rolloff is not that steep, so if you need more isolating/rejection of unwanted frequencies, run it several times.

An example after a single run:
Screen Shot 2021-05-09 at 8.24.27 PM.png
Screen Shot 2021-05-09 at 8.24.27 PM.png (201.94 KiB) Viewed 255 times

The plugin is below, install it like any other Nyquist Plugin.
Don't forget to enable it before trying to use it in Audacity.
Please note that I have only tried it in Audacity Ver 2.3.1
Attachments
SpectralEditBPF.ny
(739 Bytes) Downloaded 7 times
Last edited by Paul2 on Thu May 13, 2021 3:53 pm, edited 4 times in total.

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

Re: Spectral Bandpass Filter Plugin

Post by steve » Tue May 11, 2021 2:37 pm

Looks good here :)

One comment:
Normally the plug-in "headers" should be written as normal comments with a single semicolon as the first character.

Code: Select all

$nyquist plug-in
$version 4
$type process spectral
$name (_ "Spectral edit BPF")
$action (_ "Filtering...")
$author (_ "Paul")
$release 0.1.0
$copyright (_ "Released under terms of the GNU General Public License version 2")

would normally be written as:

Code: Select all

;nyquist plug-in
;version 4
;type process spectral
;name "Spectral edit BPF"
;action "Filtering..."
;author "Paul"
;release 0.1.0
;copyright "Released under terms of the GNU General Public License version 2"

The reason that shipped plug-ins use the "$" symbol is so that Audacity can intercept the "Underscore" macro and replace the enclosed string with a translated string from gettext. This is a complication that does not normally concern 3rd party plug-ins because Audacity only has the translated strings for plug-ins that are shipped as part of the Audacity release.

By using the syntax:

Code: Select all

$keyword (_ "quoted string")
you are telling Audacity to look up "quoted string" in it's translations, which will always silently fail, except for shipped plug-ins that have been translated into other languages.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Spectral Bandpass Filter Plugin

Post by Paul2 » Tue May 11, 2021 3:25 pm

Thanks Steve.

Have updated the original download link with the newer version.

Also bumped up the ver from 0.1.0 to 0.1.1 to avoid any confusion due to the incorrect header.

Hope this very simple plugin will motivate others to add or improve on it and also write their own.
Nyquist plugins add a lot of power and versatility to Audacity.
It's definitely worth investing some time and effort into learning it.

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

Re: Spectral Bandpass Filter Plugin

Post by steve » Tue May 11, 2021 5:47 pm

Paul2 wrote:
Tue May 11, 2021 2:16 pm
Please note that I have only tried it in Audacity Ver 2.3.1
It works fine for me with Audacity 3.0.2.

There is the possibility of the plug-in failing (user error). For example, if there is no spectral selection, or if bandwidth is nil.
You could include a check to ensure there is a valid selection using an IF statement.
In pseudo code:

Code: Select all

IF octaves == NIL
THEN
"Error"
ELSE
Apply-filter
In Nyquist code:

Code: Select all

(if (not octaves)
    "Error.\nA spectral selection is required."
    (progn
      (setf q (/ (sqrt (power 2.0 octaves))(- (power 2.0 octaves) 1)))
      (bandpass2 *track* center q)))
PROGN creates a block of code and returns the result of the final expression (the filtered audio).

IF: https://www.audacity-forum.de/download/ ... ef-137.htm
PROGN: https://www.audacity-forum.de/download/ ... ef-206.htm
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Spectral Bandpass Filter Plugin

Post by Paul2 » Tue May 11, 2021 6:45 pm

Thanks Steve, very helpful info there.
Yep, I didn't put in any error checking code, bad !!
Will modify it shortly, test and repost.

By the way, to get more familiar with the syntax and all the brackets, leaving aside what the pgm is supposed to do for now,
is the following correct?

Code: Select all


; EXAMPLE 1
; ----------

(if (not octaves)
    (setf q 0.001)  ; condition 1 if true
    (setf q 20))    ; condition 2 if false and has 2 closing brackets to indicate end of setf and if
    (bandpass2 *track* center q)   ; carries on
    

; EXAMPLE 2
; ---------
    
(if (not octaves)
    (progn (setf SomeVar 0.001)   
               (setf AnotherVar 20))   ; has 2 closing brackets to indicate end of setf and end of progn
    (progn (setf SomeVar 0.275)
               (setf AnotherVar 40)))  ; has 3 closing brackets to indicate end of setf, progn and if
    (bandpass2 *track* center q)   ; carries on  
    
 

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

Re: Spectral Bandpass Filter Plugin

Post by steve » Tue May 11, 2021 7:13 pm

Code: Select all

(if (not octaves)
    (setf q 0.001)  ; condition 1 if true
    (setf q 20))    ; condition 2 if false and has 2 closing brackets to indicate end of setf and if
    (bandpass2 *track* center q)   ; carries on
This is easier to read like this:

Code: Select all

(if (not octaves)
    (setf q 0.001)  ; condition 1 if true
    (setf q 20))    ; condition 2 if false and has 2 closing brackets to indicate end of setf and if
(bandpass2 *track* center q)   ; carries on
Yes, that will work as described, provided that "center" also has a valid value, BUT watch out for:

Code: Select all

(setf q (/ (sqrt (power 2.0 octaves))(- (power 2.0 octaves) 1)))
If "octaves" is NIL or zero then the code will fail.


Code: Select all

(if (not octaves)
    (progn (setf SomeVar 0.001)   
               (setf AnotherVar 20))   ; has 2 closing brackets to indicate end of setf and end of progn
    (progn (setf SomeVar 0.275)
               (setf AnotherVar 40)))  ; has 3 closing brackets to indicate end of setf, progn and if
    (bandpass2 *track* center q)   ; carries on  
Yes, that should work, but better to write that as:

Code: Select all

(if (not octaves)
    (progn (setf SomeVar 0.001)   
           (setf AnotherVar 20))   ; has 2 closing brackets to indicate end of setf and end of progn
    (progn (setf SomeVar 0.275)
           (setf AnotherVar 40)))  ; has 3 closing brackets to indicate end of setf, progn and if
(bandpass2 *track* center q)   ; carries on  
so that we can then see at a glance that:
1. the first "progn" block is done if the condition is true,
2. else the second "progn" block runs,
3. and the final line is outside of the conditional statement.


Another useful way to handle blocks of code conditionally is with COND (See: https://www.audacity-forum.de/download/ ... ef-079.htm). This has the additional advantage that it can define multiple test conditions (if something, then something, else if something else, ...)


By the way, Top Tip: If your text editor does not have "parentheses matching", get one that does. (Sorry, I don't have any recommendations for text editors on macOS. For Windows I'd suggest NotePad++.)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Spectral Bandpass Filter Plugin

Post by Paul2 » Tue May 11, 2021 8:27 pm

Thanks for the confirmation that my syntax is correct, starting to get the hang of it.
Yes, the indentation came out a bit weird even when using the CODE tags with [ and ].

In my text editor it looks likes the screenshot below, as you say, much easier to read.
BTW, for Mac there are two choices (that I know of) for good text editors:

- Sublime Text (not free but has a non expiring demo period and they rely on users to do the right thing).
- Editra.

The second is what I use, but unfortunately it's no longer being developed.
Not quite sure where to download an old version (I use 0.7.20), there seems to be plenty of dodgy sites that claim you can download it from them.
I have the dmg, but not sure how legal it is to share it, even though it was freeware.

What I like about Editra is, matching brackets turn blue when you click on one of them and red when there is no other matching one.
Example shown below where I purposely inserted an extra bracket on the last line.
E1.png
E1.png (29.4 KiB) Viewed 167 times

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

Re: Spectral Bandpass Filter Plugin

Post by steve » Tue May 11, 2021 9:35 pm

Paul2 wrote:
Tue May 11, 2021 8:27 pm
Yes, the indentation came out a bit weird even when using the CODE tags with [ and ].
Perhaps you used tabs instead of spaces. Always use spaces.

Paul2 wrote:
Tue May 11, 2021 8:27 pm
- Sublime Text (not free but has a non expiring demo period and they rely on users to do the right thing).
- Editra
There's also Atom, which I believe is free and widely used (I don't use it myself as we are spoilt for choice on Linux). Apparently Atom can be a bit slow with big projects, but that's unlikely to be a problem with Nyquist plug-ins that are usually less than 200 lines.

Paul2 wrote:
Tue May 11, 2021 8:27 pm
matching brackets turn blue when you click on one of them and red when there is no other matching one.
Yes, that's incredibly helpful for LISP languages.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Spectral Bandpass Filter Plugin

Post by Paul2 » Tue May 11, 2021 10:24 pm

Perhaps you used tabs instead of spaces. Always use spaces.
Aha, mystery solved, I did use tabs.

It's getting pretty late here (going for 1am), so will add error checking to the plugin after a good night's sleep and upload later in the morning.
There's also Atom
I downloaded it and had a look inside the app structure before installing or running it.

On closer inspection found out why it runs slow on big projects.
The amount of frameworks it uses plus json, javascript, cocoa, node and other resources and libs is just mind boggling.
No wonder the download is 210MB, whilst the actual main binary executable is only 300KB.
I'm sure for other tasks it's fine but like you say, for small Lisp programs it's a bit of an overkill.
Thanks anyways for mentioning it.

BTW, sorry if I'm getting side tracked but maybe this extra info will benefit other readers who are thinking of starting with Lisp as well.

With regards to that site you always refer to, there is a zipped archive of the Nyquist ref manual.
https://www.audacity-forum.de/download/ ... 7-html.zip
Nice to have it available locally.
I'm assuming that it's not 100% compatible with Nyquist Lisp as I see commands like SUM are not listed and others that are listed
like direct file operations would not work in Audacity.

There is also a PEEK and POKE command, do those work and I'm assuming that the Lisp built into Audacity is sandboxed so as to prevent accidental (or otherwise) poking at random RAM and I/O locations.

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

Re: Spectral Bandpass Filter Plugin

Post by steve » Tue May 11, 2021 10:43 pm

Paul2 wrote:
Tue May 11, 2021 10:24 pm
I'm assuming that it's not 100% compatible with Nyquist Lisp as I see commands like SUM are not listed and others that are listed
like direct file operations would not work in Audacity.
The latest Nyquist manual is also available on-line here: http://www.cs.cmu.edu/~rbd/doc/nyquist/
I frequently refer to the Nyquist language reference: http://www.cs.cmu.edu/~rbd/doc/nyquist/indx.html
and the XLISP reference (Nyquist is an extension of XLISP): https://www.audacity-forum.de/download/ ... -index.htm
The XLISP provides examples for nearly all of the built-in XLISP commands.

Audacity 3.0.2 is pretty up to date with stand alone Nyquist at the moment as Audacity had a recent update of the Nyquist library.

Yes, some functions and features are missing from the Audacity version, such as PEEK and POKE, and also some XLISP commands such as SYSTEM.

Nyquist is not actually sandboxed (you can still crash Audacity if you try), but most of the dangerous functions have been removed.


Nyquist can however read and write files. For example, take a look at Sample Data Import, and Sample Data Export. (The code for these plug-ins can be found here: https://github.com/audacity/audacity/tr ... r/plug-ins). Whatever file permissions are normal for your user account apply when reading / writing with Nyquist.

Other useful documentation can be found in the Audacity wiki: https://wiki.audacityteam.org/wiki/Nyqu ... _Reference
Here you will find Nyquist features that are unique to Audacity.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply