Creating a panel with buttons that trigger a command or macro?

Questions, discussion and recipes for scripting and batch processing in Audacity.
See also the "Scripting" section of the Audacity manual.
Forum rules
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
Post Reply
KoenT
Posts: 6
Joined: Fri Mar 18, 2022 11:02 am
Operating System: Windows 10 / 11

Creating a panel with buttons that trigger a command or macro?

Post by KoenT » Thu Jun 02, 2022 12:59 pm

Hi,

is there any way in Audacity to create a GUI panel with several (around 20) text buttons, where clicking one of these buttons performs a command (or macro) on the currently selected audio fragment in a track?

The goal here would be to make labeling of audio fragments in audio tracks more easy: you would select a fragment of an audio track and then click the button with the wanted label, and a new label would be inserted for the currently selected audio fragment, with the text of that new label set to some text string setup for the clicked button (where that text string could be different from what's shown on the button widget).

I was able to add a simple keyboard shortcut for the "Labels - Paste Text to New Label" command, which works nicely.
I also looked into a way to use macro's to also set the text for the label for the selected audio fragment, but couldn't find a way to do that. And in any case: doing it that way would require 20 different keyboard shortcuts (one for each label), and especially also remembering the different keyboard shortcuts... which is not very user-friendly...

So, having a simple button panel that can perform a command or macro that inserts a label with a specific text string for the currently selected audio fragment would be great.

Any idea if this is possible? And if so, how?

(If that doesn't work, another option would be to create a simple static web page or standalone application that shows the different label buttons and copies a label string to the clipboard + then switch back to Audacity and press the keyboard shortcut for "Labels - Paste Text to New Label" mentioned above. You would need to constantly switch app focus then of course.)

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

Re: Creating a panel with buttons that trigger a command or macro?

Post by steve » Thu Jun 02, 2022 3:15 pm

Here's a Nyquist plug-in that will label the current selection with a choice of text options.
Installation instructions for Nyquist plug-in on Windows are here: https://manual.audacityteam.org/man/ins ... st_install
LabelSelected.ny
(374 Bytes) Downloaded 3 times


After customising, installing and enabling this plug-in, it will appear in the "Tools" menu, and you can create a keyboard shortcut to launch it.



The current text options are:
  • "Hello"
  • "World"
  • "ABC"
  • "Audacity"
To customise the plug-in for your own text options, you will need to edit the plug-in in a plain text editor. If you're on Windows, I'd recommend using NotePad++ https://notepad-plus-plus.org/

This is what's in the plug-in:

Code: Select all

;nyquist plug-in
;version 4
;type tool analyze
;name "Label Selection"
;author "Steve Daulton"
;release 3.1.3
;copyright "GPL v2 or GPL v3"


;control text "Label Text" choice "Hello,World,ABC,Audacity" 0

(setf text (case text
              (0 "Hello")
              (1 "World")
              (2 "ABC")
              (3 "Audacity")))

(list (list 0 (get-duration 1) text))

Notice that the text options appear in two places.
This line:
;control text "Label Text" choice "Hello,World,ABC,Audacity" 0
and these lines:

Code: Select all

(setf text (case text
              (0 "Hello")
              (1 "World")
              (2 "ABC")
              (3 "Audacity")))
Both lists must match.

By default it will create a new label in a new label track.
You will probably prefer to make all the labels in one track, so to do that, make the selection in both the audio track and the label track like this:

Screenshot_2022-06-02_16-14-20.png
Screenshot_2022-06-02_16-14-20.png (33.8 KiB) Viewed 234 times
Learn more about Nyquist programming at audionyq.com

KoenT
Posts: 6
Joined: Fri Mar 18, 2022 11:02 am
Operating System: Windows 10 / 11

Re: Creating a panel with buttons that trigger a command or macro?

Post by KoenT » Fri Jun 03, 2022 9:12 am

Hi Steve,

thanks so much for taking the time to reply with so much detail. I really appreciate it!
It also easily managed to install the plugin and get it to work in Audacity on Windows.
I also tried changing the text strings in the "setf" block, so that they are different from the ones in the ";control" line, and that does still appear to work (the number and order of the strings must still correspond of course).

This is already a good step, but I'm afraid that in practice, the way this works would still be too cumbersome for someone to annotate hundreds of files and create thousands of individual labels.
Allow me to explain (this is by no means criticism to your highly appreciated reply of course, just things I bring up that might have a solution perhaps, to make this work better):
  • You have to click twice each time to bring up the panel + it disappears after applying a label: the panel should stay open on top of the Audacity window (like a floating panel), or otherwise as a dockable panel. Can that be done?
  • Selecting and applying the right label to the current selection also takes 3 clicks: selecting the list box, choosing the label and then clicking OK. I would really like to have a panel of buttons (one for each possible label) that you can simply click. Are button controls possible too? And can you arrange them in specific positions on the panel?
  • The fact that you have to select both the audio track and the label track is going to be annoying for annotators: is it possible in some way to only make the selection in the audio track? When I use a keyboard shortcut for the "Paste text to new label" that seems to work fine (without having to select also in the label track).
The way it would work best is like this:
  1. open button panel (which remains open all the time)
  2. open sound file to annotate (via File menu)
  3. select sound fragment in audio track (1x click and drag + maybe some fine-tuning of the selection)
  4. click label button in panel to insert a label for the selection (1x click)
  5. repeat from 3. for all interesting sound fragments in the audio file
  6. export labels (via File menu, or via keyboard shortcut)
  7. close project (via File menu, or via keyboard shortcut)
  8. repeat from 2. for all sound files to annotate
It would be so great if this workflow would be possible with Audacity.

A bit unrelated, but maybe you have an answer nonetheless: I noticed that when I use the "Paste text to new label" command, it works nicely, but the label text box remains active / in edit mode. This also happens when you (single) click the text of another label.
If you then press the "space bar" to listen to the labeled fragment, you insert spaces in that label's text...
So, you always need to press "escape" right after you select a label before you can press "space bar" to play the sound.
Is there a command exit the edit mode of the label text?

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

Re: Creating a panel with buttons that trigger a command or macro?

Post by steve » Fri Jun 03, 2022 9:37 am

KoenT wrote:
Fri Jun 03, 2022 9:12 am
You have to click twice each time to bring up the panel + it disappears after applying a label: the panel should stay open on top of the Audacity window (like a floating panel), or otherwise as a dockable panel. Can that be done?
No that' not possible with a Nyquist plug-in. Nyquist plug-ins always close after applying (or cancelling).

KoenT wrote:
Fri Jun 03, 2022 9:12 am
Selecting and applying the right label to the current selection also takes 3 clicks: selecting the list box, choosing the label and then clicking OK. I would really like to have a panel of buttons (one for each possible label) that you can simply click. Are button controls possible too? And can you arrange them in specific positions on the panel?
Nyquist plug-ins do not have "radio buttons". The available controls ("widgets") are shown here: https://wiki.audacityteam.org/wiki/Nyqu ... ns_Widgets

KoenT wrote:
Fri Jun 03, 2022 9:12 am
The fact that you have to select both the audio track and the label track is going to be annoying for annotators
Perhaps annotators are too easily annoyed :D
With a single track of audio in a project, it is very easy to make a selection in the audio track and drag the selection down to include the label track, all in one movement. After the first few hundred labels the gesture becomes habitual.

I agree that it is not so convenient in the case of more complex projects with multiple tracks, but in this case there may also be multiple label tracks, and the plug-in needs to know which label track to use. Does the user want to use the first label track, or the last, or the first one below the selected audio track, or the label track nearest to the selected audio track, or a new label track, or some other label track?

It would be possible to use a (rather complicated) workaround so as the force all labels created by the plug-in to appear in one specific label track, but doing so is not going to please everyone (because different people are likely to want different things), and it would be a lot more work for the plug-in programmer.
Learn more about Nyquist programming at audionyq.com

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

Re: Creating a panel with buttons that trigger a command or macro?

Post by steve » Fri Jun 03, 2022 9:42 am

KoenT wrote:
Fri Jun 03, 2022 9:12 am
The way it would work best is like this:

open button panel (which remains open all the time)
open sound file to annotate (via File menu)
select sound fragment in audio track (1x click and drag + maybe some fine-tuning of the selection)
click label button in panel to insert a label for the selection (1x click)
repeat from 3. for all interesting sound fragments in the audio file
export labels (via File menu, or via keyboard shortcut)
close project (via File menu, or via keyboard shortcut)
repeat from 2. for all sound files to annotate
Given that that is not possible with a Nyquist plug-in, an alternative that comes to mind:
  1. Have a text window open (in NotePad or any other) containing a list of the words / phrases that you want to use.
  2. Select and copy ("Ctrl + V") the word / phrase
  3. Select the required region in Audacity
  4. "Alt + Ctrl + V" (paste text to new label)
Learn more about Nyquist programming at audionyq.com

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

Re: Creating a panel with buttons that trigger a command or macro?

Post by steve » Fri Jun 03, 2022 9:49 am

KoenT wrote:
Fri Jun 03, 2022 9:12 am
If you then press the "space bar" to listen to the labeled fragment, you insert spaces in that label's text...
So, you always need to press "escape" right after you select a label before you can press "space bar" to play the sound.
Is there a command exit the edit mode of the label text?
The "normal" way to "close" a label's text is to press "Enter".
"Esc" also works because it "escapes" (jumps out of) the label's text edit (whereas "Enter" closes the edit).

The fact that the text box remains open is very useful in some cases. For example, say that you wanted to label sections as:
Part 1
Part 2
Part 3
...

Then all that you need to do is to paste "Part ", then type "1" and "Enter".
If the label text automatically closed, then it is unnecessarily to reopen the label, ensure that the cursor is at the end of the pasted text, then type "1" and "Enter".

KoenT wrote:
Fri Jun 03, 2022 9:12 am
Is there a command exit the edit mode of the label text?
"Enter"
Learn more about Nyquist programming at audionyq.com

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

Re: Creating a panel with buttons that trigger a command or macro?

Post by steve » Fri Jun 03, 2022 9:54 am

KoenT wrote:
Fri Jun 03, 2022 9:12 am
The way it would work best is like this:

open button panel (which remains open all the time)
open sound file to annotate (via File menu)
select sound fragment in audio track (1x click and drag + maybe some fine-tuning of the selection)
click label button in panel to insert a label for the selection (1x click)
repeat from 3. for all interesting sound fragments in the audio file
export labels (via File menu, or via keyboard shortcut)
close project (via File menu, or via keyboard shortcut)
repeat from 2. for all sound files to annotate

It would be so great if this workflow would be possible with Audacity.
Maybe you could make something similar using AutoHotKey.
Learn more about Nyquist programming at audionyq.com

KoenT
Posts: 6
Joined: Fri Mar 18, 2022 11:02 am
Operating System: Windows 10 / 11

Re: Creating a panel with buttons that trigger a command or macro?

Post by KoenT » Fri Jun 03, 2022 1:58 pm

Once again: thanks for you feedback and insights!
And I understand why some things are handier the way they are now in Audacity for some purposes.

About annotators: well, I've done that hard work myself (but in a DAW using MIDI tracks using note numbers and note lengths)

I think my conclusion is that it's indeed not possible at the moment with what's available in Audacity.
I considered the text window and copying pasting, but that is also very annoying...
So, I'm going for a simple static web page with all my buttons, and each button copies the label ID to the clipboard. Then I can simply press my keyboard shortcut in Audacity to "Paste text as label". Already did a quick test, and it works nicely! Only thing is you need to switch between 2 apps all the time, but arranging them next to each other should work fine.

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

Re: Creating a panel with buttons that trigger a command or macro?

Post by steve » Fri Jun 03, 2022 3:43 pm

Depending on your skill set, another possibility would be to write a little Python app to issue the appropriate scripting commands.
Learn more about Nyquist programming at audionyq.com

KoenT
Posts: 6
Joined: Fri Mar 18, 2022 11:02 am
Operating System: Windows 10 / 11

Re: Creating a panel with buttons that trigger a command or macro?

Post by KoenT » Wed Jun 08, 2022 8:26 am

Thanks Steve!
I didn't know you could script Audacity via Python too (I had seen Nyquist mentioned here and there, plus the macro's of course).
This may indeed also provide a good solution: creating a small standalone Python GUI with the buttons, connected to Audacity via that pipe.
Maybe we'll see how far we get with the simple static web page + keyboard shortcut to paste text as label (no extra installation necessary, no need for us to build an executable from the Python code) and if we notice it's not "fluent" enough yet that way, we will look into that option.

Post Reply