✓ Converting point labels to regions

Help for Audacity on GNU/Linux.
Forum rules
ImageThis forum is for Audacity on GNU/Linux.
Please state:
  • which version of Linux you are using,
  • the exact three-section version number of Audacity from Help menu > About Audacity,
  • whether you installed your distribution's release, PPA version, or compiled Audacity from source code.

Audacity 1.2.x and 1.3.x are obsolete and no longer supported. If you still have those versions, please upgrade (see https://www.audacityteam.org/download/).
The old forums for those versions are now closed, but you can still read the archives of the 1.2.x and 1.3.x forums.
guraknugen
Posts: 12
Joined: Thu Apr 30, 2009 9:05 pm
Operating System: Please select

✓ Converting point labels to regions

Post by guraknugen » Sun May 02, 2021 9:26 pm

Hi!

There doesn't seem to be a feature to join labels together in Audacity. For instance if I have two point labels, L1 and L2, I want only L1, but I want it to be a region starting at L1 and ending at L2.
Then I found that if I export my labels, they will end up as a text file that really is a CSV with TAB separators, so I wrote this very simple Bash script to convert point labels to regions:

Code: Select all

#!/bin/bash

# Constants ————————————————————————————————————————————————————————————————————
readonly UserError=3
readonly ScriptName=${0##*/}

# Usage ————————————————————————————————————————————————————————————————————————
if (($#!=1)); then
	printf "%s\n%s\n\n" "Usage:" "${ScriptName} <path>"
	exit $((UserError))
fi

# Initial values ———————————————————————————————————————————————————————————————
OutFile=$(realpath "${1}")
InFile="${OutFile}.old"
mv "${OutFile}" "${InFile}"

# Main —————————————————————————————————————————————————————————————————————————
while IFS=$'\t' read -r Start1 End1 Title; do
	IFS=$'\t' read -r Start2 End2 Dummy
	printf "%s\t%s\t%s\n" "${Start1}" "${Start2}" "${Title}" >> "${OutFile}"
done < "${InFile}"
How to use it:
  • Export your labels in Audacity.
  • Run your script in a terminal: NameOfTheScript.sh /Path/to/the/exported/file
  • You'll now have two files: The modified file (Labeltrack.txt) and the original file (Labeltrack.txt.old), or something like that.
  • Remove your current label track in Audacity (I'm not sure if this is necessary, though).
  • Import the ”Labeltrack.txt” file in Audacity.
  • Done.
This doesn't work if regions are supposed to overlap each other.
The end label’s title is ignored. Title of the region is the start label’s title.

Disclaimer
As always, backup before testing. This script is not thoroughly tested, so there might be bugs, even though the script is very simple. You might want to add some error handling, I guess. The script currently needs the text file to be properly made to begin with. If you, for instance, add a label in there somewhere before exporting, things will not work properly since the script will not detect that at all. It will just stupidly assume that every odd label is a start label and every even label is an end label. It's basically just a loop with four lines reading two lines from the text file every time.

So this is not really a question, I just thought that maybe someone would find it useful. If not, feel free to remove it from this forum.

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

Re: ✓ Converting point labels to regions

Post by steve » Sun May 02, 2021 11:27 pm

Doesn't seem to work for me.

This was my input Labeltext.txt file:

Code: Select all

1.003868	1.003868	1
2.733589	2.733589	2
4.146706	4.146706	3
5.388387	5.388387	4

and this was the output:

Code: Select all

1.003868	1.003868	1	2.733589	2.733589	2	
4.146706	4.146706	3	5.388387	5.388387	4	

Am I right in thinking that the intended output should be:

Code: Select all

1.003868	2.733589	1
2.733589	4.146706	2
4.146706	5.388387	3

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

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

Re: ✓ Converting point labels to regions

Post by steve » Sun May 02, 2021 11:41 pm

How about running this script in the Nyquist Prompt (https://manual.audacityteam.org/man/nyquist_prompt.html). This acts directly on point labels that are in the first label track:

Code: Select all

;type tool
(setf labels (cadar (aud-get-info "Labels")))

(let (start end txt
      (indx 0))
  (dolist (label labels "")
    (setf start end)
    (setf txt (third label))
    (setf end (first label))
    (when (and start end)
      (aud-do (format nil "SetLabel: Label=~s Text=~s Start=~s End=~s~%"
                      indx txt start end))
      (incf indx))))

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

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

Re: ✓ Converting point labels to regions

Post by steve » Mon May 03, 2021 12:19 am

guraknugen wrote:
Sun May 02, 2021 9:26 pm
  • Export your labels in Audacity.
  • Run your script in a terminal: NameOfTheScript.sh /Path/to/the/exported/file
This may be why it didn't appear to work.
To run the script on Xubuntu 20.04, or I assume other Debian based distros, either the script has to be made executable and then run with:

Code: Select all

bash NameOfTheScript.sh /Path/to/the/exported/file
or

Code: Select all

<path.to/script>/NameOfTheScript.sh /Path/to/the/exported/file
and not

Code: Select all

sh NameOfTheScript.sh /Path/to/the/exported/file
When running the script with bash, I'm getting:

Code: Select all

1.003868	2.733589	1
4.146706	5.388387	3

I assume that is as intended - Each odd numbered label treated as start, and each even numbered label treated as an end time, and the label text taken from the odd numbered label.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: ✓ Converting point labels to regions

Post by steve » Mon May 03, 2021 1:46 am

steve wrote:
Mon May 03, 2021 12:19 am
Each odd numbered label treated as start, and each even numbered label treated as an end time, and the label text taken from the odd numbered label.
That can be done with a Nyquist script like this:

Code: Select all

;type tool

(defun get-new-labels (labels)
  ;;; Returns Nyquist formatted labels in reverse order.
  (let ((isodd t)
        (newlabels ())
        start txt)
    (dolist (label labels newlabels)
      (if isodd
          (progn
            (setf start (first label))
            (setf txt (third label)))
          (push (list start (first label) txt) newlabels))
      (setf isodd (not isodd)))))

(defun delete-labeltrack (trknum)
  (aud-do (format nil "SelectTracks: Track=~s TrackCount=1 Mode=\"Set\"" trknum))
  (aud-do "SetTrackStatus: Focused=1")
  (aud-do "TrackClose:"))

(defun make-new-labels (labels)
  (dolist (label labels "")
    (aud-do (format nil "SelectTime: Start=~s End=~s~%"
                    (first label) (second label)))
    (aud-do "AddLabel:")
    ; Labels are in reverse order, so each new label is Label=0
    (aud-do (format nil "SetLabel: Label=0 Text=~s" (third label)))))

(let* ((oldlabels (aud-get-info "Labels"))
       (trknum (caar oldlabels))      ; id of first label track
       (oldlabels (cadar oldlabels))  ;first label track labels
       (newlabels (get-new-labels oldlabels)))
  (delete-labeltrack trknum)
  (make-new-labels newlabels))

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

guraknugen
Posts: 12
Joined: Thu Apr 30, 2009 9:05 pm
Operating System: Please select

Re: ✓ Converting point labels to regions

Post by guraknugen » Sat May 08, 2021 7:21 pm

steve wrote:
Sun May 02, 2021 11:41 pm
How about running this script in the Nyquist Prompt (https://manual.audacityteam.org/man/nyquist_prompt.html). This acts directly on point labels that are in the first label track:

Code: Select all

;type tool
(setf labels (cadar (aud-get-info "Labels")))

(let (start end txt
      (indx 0))
  (dolist (label labels "")
    (setf start end)
    (setf txt (third label))
    (setf end (first label))
    (when (and start end)
      (aud-do (format nil "SetLabel: Label=~s Text=~s Start=~s End=~s~%"
                      indx txt start end))
      (incf indx))))

Probably a very good idea. I had that idea in mind too, but I suck at Nyquist, so I tried something that I think I know slightly better.
I'll try this next time, thanks for the suggestion!

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

Re: ✓ Converting point labels to regions

Post by steve » Sat May 08, 2021 7:47 pm

guraknugen wrote:
Sat May 08, 2021 7:21 pm
I had that idea in mind too, but I suck at Nyquist, so I tried something that I think I know slightly better.
Sure, no problem.

A nice aspect of Nyquist is that bits of code like this can easily be converted into plug-ins (like the effects below the dividing line in the Effect menu).
Some information about that here: https://wiki.audacityteam.org/wiki/Nyqu ... _Reference
guraknugen wrote:
Sat May 08, 2021 7:21 pm
I'll try this next time
If you do, and if you need any help, feel free to ask. I'm sure that as you can write bash, you'd have little difficulty using Nyquist ;)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

guraknugen
Posts: 12
Joined: Thu Apr 30, 2009 9:05 pm
Operating System: Please select

Re: ✓ Converting point labels to regions

Post by guraknugen » Sat May 08, 2021 10:53 pm

steve wrote:
Mon May 03, 2021 12:19 am
guraknugen wrote:
Sun May 02, 2021 9:26 pm
  • Export your labels in Audacity.
  • Run your script in a terminal: NameOfTheScript.sh /Path/to/the/exported/file
This may be why it didn't appear to work.
To run the script on Xubuntu 20.04, or I assume other Debian based distros, either the script has to be made executable and then run with:

Code: Select all

bash NameOfTheScript.sh /Path/to/the/exported/file
or

Code: Select all

<path.to/script>/NameOfTheScript.sh /Path/to/the/exported/file
and not

Code: Select all

sh NameOfTheScript.sh /Path/to/the/exported/file
When running the script with bash, I'm getting:

Code: Select all

1.003868	2.733589	1
4.146706	5.388387	3

I assume that is as intended - Each odd numbered label treated as start, and each even numbered label treated as an end time, and the label text taken from the odd numbered label.
Yes, I forgot to mention that the scripts needs to be executable, sorry for that.
Also, it has to be run in Bash, since a few lines are ”Bashisms”. It might work in Ksh and Zsh to, but I didn't try that. It shouldn't be too hard to rewrite the script to be POSIX compliant, though. I just like Bash better, therefore Bash for me… 😃
And you assume it right. Odd labels are start and even labels are end, and titles are expected at start labels, while end label names are ignored.

guraknugen
Posts: 12
Joined: Thu Apr 30, 2009 9:05 pm
Operating System: Please select

Re: ✓ Converting point labels to regions

Post by guraknugen » Sat May 08, 2021 11:03 pm

steve wrote:
Sat May 08, 2021 7:47 pm
guraknugen wrote:
Sat May 08, 2021 7:21 pm
I had that idea in mind too, but I suck at Nyquist, so I tried something that I think I know slightly better.
Sure, no problem.

A nice aspect of Nyquist is that bits of code like this can easily be converted into plug-ins (like the effects below the dividing line in the Effect menu).
Some information about that here: https://wiki.audacityteam.org/wiki/Nyqu ... _Reference
guraknugen wrote:
Sat May 08, 2021 7:21 pm
I'll try this next time
If you do, and if you need any help, feel free to ask. I'm sure that as you can write bash, you'd have little difficulty using Nyquist ;)
I guess I could do it, but as I haven't yet (except a couple of years ago when I tried it a little), I think the syntax looks confusing with the parentheses around a function instead of around its parameters, or as in Bash, no parentheses needed at all in most cases:

Code: Select all

(FunctionName Parameter1 Parameter2) ; Nyquist
FunctionName(Parameter1, Parameter2) // C, Basic etc.
FunctionName $Parameter1 $Parameter2 # Bash etc.
Of course it's possible to get used to that too. :)

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

Re: ✓ Converting point labels to regions

Post by steve » Sun May 09, 2021 1:31 pm

guraknugen wrote:
Sat May 08, 2021 11:03 pm
Of course it's possible to get used to that too.
:D Yes it is.
I guess that I had an advantage when I started to use Nyquist in that I had no previous programming experience (other than a little bit of BASIC when I was a kid).

On the other hand, you don't need to bother with semicolons at the end of lines :D

There is an alternative syntax for Nyquist called SAL. I've rarely used it myself, but apparently it is often preferred by people that are familiar with C-like syntax. There's some information about SAL here:
http://www.cs.cmu.edu/~rbd/doc/nyquist/part7.html
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply