✓ 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

Re: ✓ Converting point labels to regions

Post by guraknugen » Sun May 09, 2021 8:40 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))))

I tried this thing now, and there are two things that comes to mind:
  • Swedish characters (åäöÅÄÖ) doesn't seem to be supported, they are all replaced with underscores.
  • Unnecessary regions are created: It seems like last End tag and next Start tag is converted to a region as well, which is not wanted. What I want is this:
    Let's say I have 6 labels, L1–L6. All odd labels are start labels and all even labels are end labels. The regions I then want are the following:
    [L1]————[L2] [L3]————[L4] [L5]————[L6]
    But I also get:
    [L2]—[L3] [L4]—[L5] in between the wanted regions.
I am very unfamiliar with Nyquist to say the least, so I can't say that I fully understand the code.
To begin with, is it possible to overcome the lack of UNICODE support?
I will study the code further and look some things up to see if I can understand the code better and maybe modify it myself to remove (or never create) those unnecessary regions, but all help are appreciated.

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

Re: ✓ Converting point labels to regions

Post by guraknugen » Sun May 09, 2021 8:52 pm

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

This works the way I want, except that UNICODE doesn't seem to be supported. Many of my labels are Swedish song titles, and we have the letters åäöÅÄÖ in our alphabet (those are real letters, so they are not just a and o with some funny dots and stuff). They are all replaced with underscores. Is this possible to work around, somehow? Also I might have some Japanese titles in the future (such as 夜空の星 and 京都慕情), and I suspect those will be all underscores, which would be a little confusing, so full UNICODE support would be nice.

But otherwise, this code works great. Thanks!

steve
Site Admin
Posts: 81404
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 9:19 pm

guraknugen wrote:
Sun May 09, 2021 8:52 pm
This works the way I want, except that UNICODE doesn't seem to be supported. Many of my labels are Swedish song titles, and we have the letters åäöÅÄÖ i
Unfortunately that's going to be a deal breaker for you. Nyquist does not have Unicode support.
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 » Fri May 21, 2021 7:45 am

steve wrote:
Sun May 09, 2021 9:19 pm
guraknugen wrote:
Sun May 09, 2021 8:52 pm
This works the way I want, except that UNICODE doesn't seem to be supported. Many of my labels are Swedish song titles, and we have the letters åäöÅÄÖ i
Unfortunately that's going to be a deal breaker for you. Nyquist does not have Unicode support.
Is there a way in Nyquist to export the labels, run a shell script on the exported label file and then import it back? That would solve this issue, I think. It would allow me to use my script without leaving Audacity.

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

Re: ✓ Converting point labels to regions

Post by steve » Fri May 21, 2021 10:42 am

guraknugen wrote:
Fri May 21, 2021 7:45 am
Is there a way in Nyquist to export the labels, run a shell script on the exported label file and then import it back?
The Audacity version of Nyquist cannot run a shell script. (It was considered too much of a security risk to allow Nyquist to run arbitrary system commands).

You can export and import labels, and you don't need to use Nyquist for that.
See: https://manual.audacityteam.org/man/lab ... tml#export
and: https://manual.audacityteam.org/man/imp ... abels.html

You could perhaps automate the process (export labels > shell script > import labels) with "Autokey" (available for most Linux distributions).
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 » Sun May 23, 2021 1:22 am

steve wrote:
Fri May 21, 2021 10:42 am

You could perhaps automate the process (export labels > shell script > import labels) with "Autokey" (available for most Linux distributions).
I actually use AutoKey and have done so for years, but I didn't think of that in this case.

Yes, that seems to work. It's not fast, but it's OK. I had to insert some delays to make sure things weren't executed before last command was finished. Here's my AutoKey script, in case anyone is interested:

Code: Select all

import os
import glob
import subprocess

def SendKey(Key):
    keyboard.send_keys(Key)
    time.sleep(0.3)

# Define needed paths.
LabelFile="/tmp/Etikettspår"
if os.path.exists(LabelFile):
    os.remove(LabelFile)

Script=os.path.join(os.environ['HOME'], 
                    "bin",
                    "ConvertLabelsPointsToRegions.sh")

# Export labels.
SendKey("<alt>+a")
SendKey("e")
SendKey("e")
SendKey("<ctrl>+a")
SendKey(LabelFile)
SendKey("<enter>")
time.sleep(2)

# Run the script.
subprocess.check_call([Script, LabelFile])

# Import the new labels.
SendKey("<alt>+a")
SendKey("i")
SendKey("e")
SendKey("<ctrl>+l")
SendKey(LabelFile)
SendKey("<enter>")

# Tidy up.
time.sleep(2)
FileList = glob.glob(LabelFile+"*")
for File in FileList:
    if os.path.exists(File):
        os.remove(File)
ConvertLabelsPointsToRegions.sh is the bash script that's described in the original post, and it's located in $HOME/bin/ in this case. Maybe I should have added a test to see if that script is present before running it, but there are probably quite a few more things to improve as well.

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

Re: ✓ Converting point labels to regions

Post by steve » Sun May 23, 2021 11:09 am

guraknugen wrote:
Sun May 23, 2021 1:22 am
Yes, that seems to work. It's not fast, but it's OK. I had to insert some delays to make sure things weren't executed before last command was finished. Here's my AutoKey script, in case anyone is interested:
You may be able to make it quicker (and shorter) by creating a keyboard shortcut for "Export Labels" (https://manual.audacityteam.org/man/key ... ences.html)
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 » Thu May 27, 2021 6:40 pm

Yes, I know about the shortcut thing. I always try to use shortcuts as much as possible, so that is usually one of the first things I look for in most applications. IN this case I already made a few shortcuts, like exporting audio, but I actually don't have one for labels, yet…
Thanks for the suggestion. :)

If anyone wonders why I take so long to look for any replies to my own posts, it's because I thought I'd get an email every time someone replies, but apparently that doesn't happen, and is maybe not supposed to happen, but I'm used to that in other forums, so I just assumed it's the same here.

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

Re: ✓ Converting point labels to regions

Post by steve » Thu May 27, 2021 9:29 pm

guraknugen wrote:
Thu May 27, 2021 6:40 pm
If anyone wonders why I take so long to look for any replies to my own posts, it's because I thought I'd get an email every time someone replies, but apparently that doesn't happen, and is maybe not supposed to happen, but I'm used to that in other forums, so I just assumed it's the same here.
Way down at the bottom of the page, click on the wrench icon and click on "subscribe topic" so that it does NOT have a tick mark. You should then receive email notifications of replies.
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 » Fri May 28, 2021 8:42 pm

steve wrote:
Thu May 27, 2021 9:29 pm
guraknugen wrote:
Thu May 27, 2021 6:40 pm
If anyone wonders why I take so long to look for any replies to my own posts, it's because I thought I'd get an email every time someone replies, but apparently that doesn't happen, and is maybe not supposed to happen, but I'm used to that in other forums, so I just assumed it's the same here.
Way down at the bottom of the page, click on the wrench icon and click on "subscribe topic" so that it does NOT have a tick mark. You should then receive email notifications of replies.
It's not ticked, but this time I actually got an email, so I don't know what was going on there. Maybe I just didn't recognize the email and deleted it without realizing what I was deleting. Ok, so at least that's not an issue any more, as it seems. :mrgreen:

Post Reply