Convert Label text to LRC file

Help for Audacity on Windows.
Forum rules
ImageThis forum is for Audacity on Windows.
Please state which version of Windows you are using,
and the exact three-section version number of Audacity from "Help menu > About Audacity".


Audacity 1.2.x and 1.3.x are obsolete and no longer supported. If you still have those versions, please upgrade at 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.
steve
Site Admin
Posts: 81402
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Convert Label text to LRC file

Post by steve » Wed Mar 24, 2021 12:19 pm

bigboss97 wrote:
Wed Mar 24, 2021 11:13 am
I want to populate labels in a selected region. But I couldn't find any parameter in GetInfo: to retrieve the start and end of the current selection.
This is tricky in the current version of Audacity. "GetInfo:" is the only way to directly get data from Audacity back to Python, and currently there isn't a GetInfo: option to get the selection time (or even the cursor position).

You can do this with a Nyquist plug-in, but it's a bit messy.
Try this in the Nyquist Prompt:

Code: Select all

;type analyze

; get start and end times
(setf sel-start (get '*selection* 'start))
(setf sel-end (get '*selection* 'end))

;print the results
(format nil "Start = ~a   End = ~a" sel-start sel-end)

Nyquist can't delete labels, but it can create labels by returning a specially formed list.
The format for returning labels is "a list of lists", where each inner list is in the format:
(start-seconds end-seconds label-text)
The start and end times are relative to the selection start.

Example - label the current selection:

Code: Select all

;type analyze

(setf start (get '*selection* 'start))
(setf end (get '*selection* 'end))

;calculate the length
(setf duration (- end start))

; make the label. 
; Relative start time = 0
; Relative end time = duration
(list (list 0 duration "Label  Text"))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

bigboss97
Posts: 45
Joined: Sat Feb 08, 2020 6:28 am
Operating System: Windows 10

Re: Convert Label text to LRC file

Post by bigboss97 » Wed Mar 24, 2021 9:38 pm

steve wrote:
Wed Mar 24, 2021 12:19 pm
currently there isn't a GetInfo: option to get the selection time (or even the cursor position).
Thanks for your response. Back to the drawing board... :-)

My initial idea was... user inserts labels via GUI and the script modifies the labels (by reading existing label track and generate a new one). Then I thought that script can insert labels, too. But user has to tell the position.
Looks like I have to combine the two ideas. User insert labels as markers. Script reads the markers and populate lyrics accordingly.
Any input is welcome :)

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

Re: Convert Label text to LRC file

Post by steve » Wed Mar 24, 2021 10:33 pm

bigboss97 wrote:
Wed Mar 24, 2021 9:38 pm
My initial idea was... user inserts labels via GUI and the script modifies the labels (by reading existing label track and generate a new one). Then I thought that script can insert labels, too. But user has to tell the position.
Perhaps this post will give you some ideas: viewtopic.php?p=420490#p420490
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

bigboss97
Posts: 45
Joined: Sat Feb 08, 2020 6:28 am
Operating System: Windows 10

Re: Convert Label text to LRC file

Post by bigboss97 » Thu Mar 25, 2021 11:44 am

I'm now trying to use AddLabelPlaying:. Unfortunately, playing stops as soon as I apply SetLabel:.
I guess this is a nature of audacity, not a bug. Looks like I have to add all empty labels and then apply SetLabel:.

bigboss97
Posts: 45
Joined: Sat Feb 08, 2020 6:28 am
Operating System: Windows 10

Re: Convert Label text to LRC file

Post by bigboss97 » Sun May 09, 2021 2:36 am

I've almost everything done. But there's one issue...
I can't send SetLabel with leading or trailing spaces. They will be trimmed automatically. But this is accepted if it's done via GUI. Is this a bug?
It doesn't work in my script. So I tested it with pipeclient.py, same result:

Code: Select all

Enter command or 'Q' to quit: SetLabel: Text="   x   z   " Label=1
Sending command: SetLabel: Text="   x   z   " Label=1

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

Re: Convert Label text to LRC file

Post by steve » Sun May 09, 2021 2:21 pm

bigboss97 wrote:
Sun May 09, 2021 2:36 am
I can't send SetLabel with leading or trailing spaces. They will be trimmed automatically. But this is accepted if it's done via GUI. Is this a bug?
Probably not a bug. My guess is that mod-script-pipe strips leading / trailing spaces before evaluating the command arguments.
In most cases this is useful as anything other than a string literal should not have trailing or leading white space. For example, a numeric value passed as " 42 " should be interpreted as integer "42".

bigboss97 wrote:
Thu Mar 25, 2021 11:44 am
Looks like I have to add all empty labels and then apply SetLabel:.
Yes.
Note that the label id is set dynamically according to where it is on the time line, not the order in which the labels were created.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

bigboss97
Posts: 45
Joined: Sat Feb 08, 2020 6:28 am
Operating System: Windows 10

Re: Convert Label text to LRC file

Post by bigboss97 » Sat Jun 12, 2021 11:44 am

Here is the first usable version of my Python script. Not very user friendly :D
Here is the tutorial
Image

Paul2
Posts: 164
Joined: Wed Sep 04, 2019 1:17 pm
Operating System: macOS 10.15 Catalina or later

Re: Convert Label text to LRC file

Post by Paul2 » Sat Jun 12, 2021 11:58 am

Very nice idea @bigboss97.
Clever use of "mod-pipe".
I don't have any use for karaoke but, your script has given me other ideas, thank you.

BTW
You mention in your video that $ marks end of line, what happens if the lyrics contain..."Gimme my $100, bro"... : by rap artist 49 cents :D

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

Re: Convert Label text to LRC file

Post by steve » Sat Jun 12, 2021 12:57 pm

bigboss97 wrote:
Sat Jun 12, 2021 11:44 am
Not very user friendly
:D That's true, but it works!
I had to pause your video several times to understand how to use the script correctly.

Perhaps worth saying in your video description to use the latest version of Audacity (currently 3.0.2, though 3.0.3 is due to be released soon).
3.0.2 has some important bug fixes, and 3.0.3 has some more bug fixes.

In reply to a question that you raised in the video:
by default, the mod-script-pipe module is set as "New" (and yes you are correct that it has to be changed to "Enabled", and then restart Audacity).
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Convert Label text to LRC file

Post by steve » Sat Jun 12, 2021 2:44 pm

Rather late in the day, but a possible workaround for Nyquist, if you don't mind pre-processing the text.
(tested with Audacity 3.0.2)
Example (probably very bad Chinese writing from Google Translate):

Code: Select all

你好我的朋友你今天好吗
Pre-process the text so there is one character per line:

Code: Select all

你
好
我
的
朋
友
你
今
天
好
吗

and save it as a text file.

We can then read it with Nyquist a line at a time.
It doesn't matter that Nyquist does not know where the characters start and end, because it knows where the lines end. As long as we are dealing with whole line strings, there's no problem:

Code: Select all

(setf fname "<path-to-file>/test.txt")
(let ((fp (open fname))
      (labels ()))
  (prog1
    (do ((line (read-line fp) (read-line fp))
         (start 0 (+ 2 start))
         (end 1 (+ 2 end)))
        ((not line) labels)
      (push (list start end line) labels))
    (close fp)))

You can also split Unicode strings on known single byte characters, such as spaces:

Code: Select all

(setf str "你好 我 的")
(dotimes (i (length str))
  (when (char= (char str i) #\Space)
    (return (list (list 0 1 (subseq str 0 i))))))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply