Export individual label when multiple labels in project

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.
steve
Site Admin
Posts: 81227
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Export individual label when multiple labels in project

Post by steve » Fri Oct 02, 2020 4:37 pm

agua wrote:
Fri Oct 02, 2020 4:26 pm
I wish I were more at ease with Nyquist
You've picked quite a complex task for Nyquist as it requires lots of parsing of scripting data. (Nyquist was designed primarily for manipulating audio rather than text).


This is one part of the puzzle:

Code: Select all

(defun get-selected-label-tracks ()
  ;; Returns a list of selected label tracks as (list (list ID TrackName) ...)
  (let ((tracks (aud-get-info "Tracks"))
         (tracklist ())
         (id 0)
         trackname
         selected
         is-label)
    (dolist (track tracks tracklist)
      ;; Get name if a selected label track
      (dolist (e track)
        (let ((tn (member 'NAME e))
              (sel (member 'SELECTED e))
              (kind (member 'KIND e)))
        (when tn
          (setf trackname (second tn)))
        (when sel
          (setf selected (second sel)))
        (when kind
          (setf is-label (string-equal (second kind) "label")))))
      ;; If selected label track found, push it to the tracklist.
      (when (and trackname selected (= selected 1) is-label)
        (push (list id trackname) tracklist))
      (incf id))))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

agua
Posts: 117
Joined: Wed Mar 14, 2018 3:15 am
Operating System: Windows 7

Re: Export individual label when multiple labels in project

Post by agua » Fri Oct 02, 2020 6:12 pm

Waou! Wonderful and fast, thanks a lot. Works like a charm. I'll have to study the code more closely to understand it all, though! :D
Also, my problem about backslashes in pathname was a stupid one as I just made some keystroke error in the name itself. Once corrected, the problem of backslashes disappeared...

agua
Posts: 117
Joined: Wed Mar 14, 2018 3:15 am
Operating System: Windows 7

Re: Export individual label when multiple labels in project

Post by agua » Fri Oct 02, 2020 10:28 pm

With your help, I am arriving at the following piece of code which displays the first selected label track info :

Code: Select all

(defun get-selected-label-tracks ()
  ;; Returns a list of selected label tracks as (list (list ID TrackName) ...)
  (let ((tracks (aud-get-info "Tracks"))
         (tracklist ())
         (id 0)
         trackname
         selected
         is-label)
    (dolist (track tracks tracklist)
      ;; Get name if a selected label track
      (dolist (e track)
        (let ((tn (member 'NAME e))
              (sel (member 'SELECTED e))
              (kind (member 'KIND e)))
        (when tn
          (setf trackname (second tn)))
        (when sel
          (setf selected (second sel)))
        (when kind
          (setf is-label (string-equal (second kind) "label")))))
      ;; If selected label track found, push it to the tracklist.
      (when (and trackname selected is-label)
		(if (= selected 1)
         (push (list id trackname) tracklist))
        (incf id)); index (0-based) relative to label track (only) list
      ;(incf id); index (0-based) relative to all tracks (audio and labels)
	)))
(setf selected-label-track (first (get-selected-label-tracks))); get first of the possibly several selected label tracks
(format nil "~a" selected-label-track); display this track
(setf selected-label-track-id (first selected-label-track)); get its track id (relative to label track (only) list)
(format nil "~a" selected-label-track-id); display this track id
(setf selected-label-track-name (second selected-label-track)); get its track name
(format nil "~a" selected-label-track-name); display this track name
(setf selected-label-track-content (second (nth selected-label-track-id (aud-get-info "labels")))); get its track content
(format nil "~a" selected-label-track-content); display this content
;(format nil "~a" (strcat selected-label-track-name "\n" (selected-label-track-content))); display this content
(format nil "Track id: = ~a name = ~a content = ~a" selected-label-track-id selected-label-track-name selected-label-track-content)
Quite happy to getting closer to my target, which is to export/save this track content under the filename of it track name, in a fixed chosen directory (as I cannot find what the audacity variable is for the last saving directory used).

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

Re: Export individual label when multiple labels in project

Post by steve » Sat Oct 03, 2020 9:52 am

agua wrote:
Fri Oct 02, 2020 6:12 pm
Also, my problem about backslashes in pathname was a stupid one as I just made some keystroke error in the name itself. Once corrected, the problem of backslashes disappeared...
I would recommend using the "file widget" to enter the target directory.
Unfortunately it will be a bit weird because the file widget is intended for selecting files, not folders, and as you want to use the label track's name as the file name, the actual file name is not known until the plug-in runs.

Nevertheless, I think this is a usable workaround - you just need to be aware that only the directory and not the file name will be used by the plug-in:

Code: Select all

;type tool

;control filename "Select destination folders" file "Select a folder" "*default*/<track name>.txt" "Text file|*.txt;*.TXT|All files|*.*;*" "save"

(defun extract-path (filename)
  ;; Returns the directory part of a fully qualified file name.
  (do ((i (1- (length filename)) (1- i)))
      ((< i 0) "")
    (when (char= (char filename i) *file-separator*)
      (return-from extract-path (subseq filename 0 (1+ i))))))

(setf fname "test")

;; Replace the name of the file with "fname"
(format nil "~a~a.txt" (extract-path filename) fname)

9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

agua
Posts: 117
Joined: Wed Mar 14, 2018 3:15 am
Operating System: Windows 7

Re: Export individual label when multiple labels in project

Post by agua » Sat Oct 03, 2020 4:25 pm

Thank you for this. I'll work on it.

Do you know what is the audacity variable for the last directory used for exporting track ? I mean, when you export a label track, I noticed audacity open the explorer dialog to the last directory you have been saving labels, not the project home directory. So, that must be have been memorized in some variable which I hope might be accessible to Nyquist user. Am I right ?

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

Re: Export individual label when multiple labels in project

Post by steve » Sat Oct 03, 2020 6:32 pm

agua wrote:
Sat Oct 03, 2020 4:25 pm
Do you know what is the audacity variable for the last directory used for exporting track ?
Try:

Code: Select all

(first (aud-do "GetPreference:Name=\"Export/Path\""))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

agua
Posts: 117
Joined: Wed Mar 14, 2018 3:15 am
Operating System: Windows 7

Re: Export individual label when multiple labels in project

Post by agua » Sat Oct 03, 2020 9:38 pm

Hi Steve. Thanks for this cue on the last saving directory. It seems to work well after exporting an audio track. It does not seem to do so though, if the exported track was a label one...

I have an additional question. When I launch my plugin which seems to work quite better now, I get the message "You must first select an audio track...", although I am obviously only addressing label tracks in this plugin. Is there a way to avoid this ?

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

Re: Export individual label when multiple labels in project

Post by steve » Sat Oct 03, 2020 9:56 pm

agua wrote:
Sat Oct 03, 2020 9:38 pm
Is there a way to avoid this ?
Make it a "tool" type plug-in.

Code: Select all

;type tool
This will only work for a real installed plug-in, not the Nyquist Prompt.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

agua
Posts: 117
Joined: Wed Mar 14, 2018 3:15 am
Operating System: Windows 7

Re: Export individual label when multiple labels in project

Post by agua » Sat Oct 03, 2020 10:36 pm

I just did, using the Nyquist Plug-ins Installer in the Tools Menu, but once "Enabled" it keeps showing up in the Effects Menu and still sends this "You must first..." message...
I put the code here :

Code: Select all

;nyquist plug-in
;version 1
;type tool
;name "ExportLabelTrack"

(setq slash (format nil "~a" *file-separator*)) 
(setq ch #\tab)
(setq *float-format* "%#1.6f")

(defun home ()
  (strcat "D:" slash "X" slash "Y" slash "Z")); customized
(setq savepath (string-right-trim slash (home)))

(defun get-selected-label-tracks ()
  ;; Returns a list of selected label tracks as (list (list ID TrackName) ...)
  (let ((tracks (aud-get-info "Tracks"))
         (tracklist ())
         (id 0)
         trackname
         selected
         is-label)
    (dolist (track tracks tracklist)
      ;; Get name if a selected label track
      (dolist (e track)
        (let ((tn (member 'NAME e))
              (sel (member 'SELECTED e))
              (kind (member 'KIND e)))
        (when tn
          (setf trackname (second tn)))
        (when sel
          (setf selected (second sel)))
        (when kind
          (setf is-label (string-equal (second kind) "label")))))
      ;; If selected label track found, push it to the tracklist.
      (when (and trackname selected is-label)
		(if (= selected 1)
         (push (list id trackname) tracklist))
        (incf id)); index (0-based) relative to label track (only) list
      ;(incf id); index (0-based) relative to all tracks (audio and labels)
	)))
(setf selected-label-track (first (get-selected-label-tracks))); get first of the possibly several selected label tracks

(if (first selected-label-track)
 (progn
  ;(format nil "~a" selected-label-track); display this track
  (setf selected-label-track-id (first selected-label-track)); get its track id (relative to label track (only) list)
  ;(format nil "~a" selected-label-track-id); display this track id
  (setf selected-label-track-name (second selected-label-track)); get its track name
  ;(format nil "~a" selected-label-track-name); display this track name
  (setf selected-label-track-content (second (nth selected-label-track-id (aud-get-info "labels")))); get its track content
  ;(format nil "~a" selected-label-track-content); display this content
  ;(format nil "Track id: = ~a name = ~a content = ~a" selected-label-track-id selected-label-track-name selected-label-track-content); display all data
  
  ; Write data to label track name named outputfile in fixed save path location
  (setq outputfile (format nil "~a~a~a (labels).txt" savepath slash selected-label-track-name))
  (setf file-pointer (open outputfile :direction :output));
  (dolist (line selected-label-track-content)
   (format file-pointer (format nil "~a~a~a~a~a~%" (first line) ch (second line) ch (third line)))
  )
  (close file-pointer)
  (format nil "Track~%  id: = ~a~%  name = ~a~%  content = ~a~%~%  has been saved to this output file: ~%~a" selected-label-track-id selected-label-track-name selected-label-track-content outputfile); display all data+filename
 )
 (print "No LABEL TRACK selected.  Please select ONE first.")
)
Do you see any discrepancy which would make it happen that way ?

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

Re: Export individual label when multiple labels in project

Post by steve » Sat Oct 03, 2020 11:55 pm

agua wrote:
Sat Oct 03, 2020 10:36 pm
but once "Enabled" it keeps showing up in the Effects Menu
If you initially installed it as an "Effect" (;type process), then you will need to reset plug-ins and then re-enable the effect.
To reset plug-ins, delete the files:
pluginregistry.cfg
and
pluginsettings.cfg

(both are in the same directory as the "audacity.cfg" Preferences file https://manual.audacityteam.org/man/pre ... tml#stored)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply