Shortcut to Label with track name + code

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
RafaelCampos
Posts: 3
Joined: Wed May 04, 2022 2:15 pm
Operating System: macOS 10.15 Catalina or later

Shortcut to Label with track name + code

Post by RafaelCampos » Thu May 05, 2022 11:41 am

Hi,

I am using Audacity to annotate a large amount of data. I need to go trough each track and add labels on intervals of interest. The labels are generally composed by the track's name and a code: e.g., "track1_C00".

I found this thread (viewtopic.php?f=69&t=107137) where a user is trying to add a label at the start of a track, and I learned that it is possible to add widgets (https://audionyq.com/nyquist-programmin ... ata-types/) from which to select a series of option.

Can anyone help me write a NYQUIST macro that takes the track name, opens a widget with all my codes (n = 9), and then concatenates both into a label at selection?

I know this is something I should figure out by myself but I would deeply appreciate some guidance.

Thank you.

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

Re: Shortcut to Label with track name + code

Post by steve » Thu May 05, 2022 2:35 pm

RafaelCampos wrote:
Thu May 05, 2022 11:41 am
I know this is something I should figure out by myself but I would deeply appreciate some guidance.
Something like this?

Code: Select all

;nyquist plug-in
;version 4
;type tool analyze
;name "Add Custom Label"

;control code "Select code" choice "C01,C02,C03,C04,C05,C06,C07,C08,C09" 0

(defun getcode (id)
  (format nil "_C0~a" (+ id 1)))

(let ((txt (get '*track* 'name))
      (start (get '*selection* 'start))
      (end (get '*selection* 'end)))
  (setf txt (strcat txt (getcode code)))
  (list (list 0 (- end start) txt)))

Learn more about Nyquist programming at audionyq.com

RafaelCampos
Posts: 3
Joined: Wed May 04, 2022 2:15 pm
Operating System: macOS 10.15 Catalina or later

Re: Shortcut to Label with track name + code

Post by RafaelCampos » Thu May 05, 2022 2:51 pm

steve wrote:
Thu May 05, 2022 2:35 pm
Something like this?
Hi steve,

Exactly! :o Thank you so much.

Just one detail: Is there a way to have it add the label to the same label track? Every-time I create a new label it also creates a new track (screenshot attached).
Attachments
Screenshot 2022-05-05 at 15.47.55.png
Screenshot 2022-05-05 at 15.47.55.png (231.88 KiB) Viewed 132 times

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

Re: Shortcut to Label with track name + code

Post by steve » Thu May 05, 2022 2:59 pm

RafaelCampos wrote:
Thu May 05, 2022 2:51 pm
Is there a way to have it add the label to the same label track?
To do that you need to make the selection on the audio track and the label track.
Make the selection on the audio track first, then "Shift + Click" the "Select" button on the left end of the label track.

(Alternatively, if the audio track and the label track are next to each other, you can click + drag the selection across both tracks).
Learn more about Nyquist programming at audionyq.com

RafaelCampos
Posts: 3
Joined: Wed May 04, 2022 2:15 pm
Operating System: macOS 10.15 Catalina or later

Re: Shortcut to Label with track name + code

Post by RafaelCampos » Thu May 05, 2022 3:19 pm

steve wrote:
Thu May 05, 2022 2:59 pm
Make the selection on the audio track first, then "Shift + Click" the "Select" button on the left end of the label track.
That would take a lot of time because I would have to scroll down. However, when I export the labels (even when there are multiple tracks), Audacity creates a single .txt with all of them, which is exactly what I wanted. So it still works exactly as I needed even with multiple label tracks.

Thank you for your time. You just saved me many of hours of work.
Best!

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

Re: Shortcut to Label with track name + code

Post by steve » Thu May 05, 2022 5:08 pm

RafaelCampos wrote:
Thu May 05, 2022 3:19 pm
That would take a lot of time because I would have to scroll down.
If you have lots to do, then there's a workaround, but probably not worth the effort if you can manage with the plug-in as is.

The workaround is to create another plug-in, using this code (additional headers are required to make it a full plug-in):

Code: Select all

;type tool

(defun first-label-track-id ()
  (let ((tracks (aud-get-info "tracks"))
        (tracknum 0))
    (dolist (track tracks nil)
      (when (member '(KIND "label") track :test 'equal)
        (return tracknum))
      (incf tracknum))))

(aud-do-command "selecttracks"
                :track (first-label-track-id)
                :trackcount 1
                :mode "Add")
""

The above code looks for the first label track and adds it to the current selection.

Then create a (normal) Macro with the two plug-ins. (add the label track to the selection, then create the label).

Finally, make a keyboard shortcut to run the macro (See: https://manual.audacityteam.org/man/key ... ences.html)

(Unfortunately it is not possible to combine both bits of code in a single Nyquist plug-in because changing the selection can only be done as a "Tools" type plug-in, and returning labels requires an "analyze" (or "tools analyze") type plug-in.
Learn more about Nyquist programming at audionyq.com

Post Reply