Shortcut to Label with track name + code

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 (https://forum.audacityteam.org/t/labeling-a-label-track-from-its-name/54261/1) 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-programming-variables-and-data-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.

Something like this?

;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)))

Hi steve,

Exactly! :astonished: 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).
Screenshot 2022-05-05 at 15.47.55.png

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).

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!

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):

;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/keyboard_preferences.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.