Running macros/commands during playback

I just found out that Audacity apparently doesn’t allow running a macro during playback, via a keyboard shortcut.

Is there any way I can run multiple commands at a single keypress during playback? Doesn’t matter if it’s scripting, plugins or what.

(What I want to do is to run the equivalent of “Right at Playback Position”, “Add label to selection”, and unfocus the cursor away from the label text input box, moving it back to the track. In standard Audacity keybindings that would be “]”, “Ctrl+B” and “Enter”, except I have to do that literally tens of thousands of times, and typing mistakes will easily disrupt the whole workflow.)

Most commands require playback to be stopped, but for those that don’t you can send commands from Python.
See: Scripting - Audacity Manual

An alternative would be to use a 3rd party automation tool such as Autokey.

I tried a few (xmacro, xdotool, atbswp) but sadly they seem to have timing issues; somehow the keypresses aren’t registered by Audacity correctly, and fiddling around with artificial delays induce unacceptable lag.

For now I modified the audacity source to remove the focus put on newly created labels, which reduces the number of keypresses to 2. I’ll check if the Python route can solve it, thank you.


What I want to do is to run the equivalent of “Right at Playback Position”, “Add label to selection”, and unfocus the cursor away from the label text input box, moving it back to the track. In standard Audacity keybindings that would be “]”, “Ctrl+B” and “Enter”, except I have to do that literally tens of thousands of times,

I am sure you are light years ahead of this, but Ctrl-M works well for me to create point labels. I can enter these labels rapidly without text, or at a more leisurely rate with text. :smiley: YMMV

Yeah I had been doing that for a while, point labels then “export multiple”. There are a couple problems with it tho:

  • I’m marking a very large number of sections (these are sentences from linguistic fieldwork). Errors are inevitable; sometimes I’ll put a point mark at the beginning of a word, then miss the end and put the next one at the start of the next word. Since all odd marks are interpreted as “beginning” and all even “ends”, this throws off everything at that point. And when you’re exporting 1000+ wavs from a single interview, it can be hard to track down where exactly the error was – especially if a second error masks the first misalignment. Ultimately what I’m actually doing, conceptually, isn’t marking points in time but regions; and everything is neater and easier to proof if Audacity knows about that. Then the regions are visually highlighted, and one can move between them, easily play individual regions to ensure nothing was cut off etc.

  • I just upgraded to 2.4, which ok, is not essential to me but has some niceties (multiview is great for linguistics, and the button sizes are fixed on HiDPI Linux). But it seems like, in this version, Ctrl+M point labels will now put the focus into the label text input too.

I made a small patch where focus on label doesn’t happen when audio is playing, since I figure it’s unlikely that anybody would want to type label titles while the audio is running and the label scrolling out of view. I wonder if this is something upstream would want? (I’m not at all familiar with Audacity code tho.)

After some test runs I’m coming to think I actually like having “end selection” separated from “add label”. It’s one extra keystroke, but there are many cases where the speaker hesitates or makes mistakes, and the region I was marking turns out to be a dud. In these cases it’s easier to amend by moving around and retyping the “[” “]” than to undo a label (ctrl+z is messy during playback). So I think the patch to bypass label input focus makes it good enough for what I want.

The labels don’t quickly scroll out of view when zoomed out. At the default zoom level I have about 10 seconds to enter the label text before it has scrolled off-screen. If I zoom out a bit I can give myself more time so that I can easily record an album from tape and add song names while recording.

Oh I see. A zoom level like that would be unworkable when marking the beginning and end of individual words so I guess I never tried it at this level.

I tried this, and it works well for me. Here it is in action:

This is the AutoKey script:

# Set selection end and add label.

While this is getting off-topic, I thought I’d report how’s it going with the task of mass marking thousands of small labels, in case web searches bring here anybody doing similar work.

  • As noted before, there’s actually advantages in separating ‘]’ (end of selection) from ‘ctrl+b’ (selection to label), because it’s easier to skip around and adjust selections with [] than adjusting labels. Unlike labels, there are keybindings specifically to increase and decrease selections on both sides, you can redo a ] directly without redoing the [, and so on. This helps dealing with hesitations, speaking errors, repetitions and other false positives.

  • With the patch to avoid the spurious focus-inside-label, this reduces the process to 3 presses, 2 to tentatively mark a selection and the third to confirm it sounded right. The only snag is that the focused track (highlighted yellow) becomes the label track. I thought my patch had broken something, but I tried vanilla and it has the same behaviour: you enter the track name, press esc/enter, and the focus doesn’t go back to the audio track. Considering that there’s code in there specifically to return the focus, I wonder if this is a bug (or I misunderstood something). Proceeding with focus on label track is usually harmless, but sometimes I have to press “up” to focus back, e.g. after “select none” to continue playing after testing a label (and often I also need an “enter” to toggle the audio track selections back on).

  • I mapped “selection to label” to ‘b’ rather than ‘ctrl+b’ (I don’t need the command that’s normally on “b”). If I remove my right hand from the normal touch-typing position and rest index and middle fingers on [ and ], I can do the workflow “[, ], b” quite comfortably.

  • Previously I was exporting the unnamed labels to multiple wav files, then renamed the wavs with a script drawing from my data. When I missed a word or something, the files would get misnamed from that point, requiring a manual binary search in Audacity to find where was the problem. A smarter approach is to export the labels to a label file, rename the labels with my data, then re-import them as a label track (the old track can be safely deleted). Now the words are labelled inside Audacity, and one can use the standard label movement keybindings to help find mislabellings.