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.
Post Reply
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 » Fri Mar 27, 2020 11:23 am

steve wrote:
Tue Mar 03, 2020 4:45 pm
I'll write back if I get more information. Sorry I couldn't be more help.
Any update?

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

Re: Convert Label text to LRC file

Post by steve » Fri Mar 27, 2020 6:02 pm

Not as yet.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

chenghuaiyu
Posts: 2
Joined: Wed Dec 23, 2020 1:53 pm
Operating System: Windows 10

plugin script of Converting Label text to SRT subtitle or mp3 LRC format

Post by chenghuaiyu » Wed Dec 23, 2020 2:36 pm

According this forum thread, I've learned a lot and written a plugin which convert label text to SRT subtitle or mp3 LRC format.following is the plugin script code.

Code: Select all

$nyquist plug-in
$version 4
$type tool analyze
$name (_ "Subtitle Generator")
$manpage "Subtitle_Generator"
$debugbutton disabled
$author (_ "Cheng Huaiyu")
$release 1.0.0
$copyright (_ "Released under terms of the GNU General Public License version 2")
;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
;;
;; For information about writing and modifying Nyquist plug-ins:
;; https://wiki.audacityteam.org/wiki/Nyquist_Plug-ins_Reference

$control filename (_ "Export Label Track to File:") file (_ "Select a file") "*default*/subtitles" "SRT file|*.srt;*.SRT|LRC files|*.lrc;*.LRC" "save,overwrite"

;; Reurn number as string with at least 2 digits
(defun pad (num)
  (format nil "~a~a" (if (< num 10) "0" "") num)
)
  
;; Reurn number as string with at least 3 digits
(defun pad3 (num)
  (format nil "~a~a" (if (< num 10) "00" (if (< num 100) "0" "")) num)
)

;; Format time (seconds) as hh:mm:ss,xxx
(defun srt-time-format (sec)
  (let* ((seconds (truncate sec))
        (hh (truncate (/ seconds 3600)))
        (mm (truncate (/ (rem seconds 3600) 60)))
        (ss (rem seconds 60))
        (xxx (round (* (- sec seconds) 1000))))
    (format nil "~a:~a:~a,~a" (pad hh) (pad mm) (pad ss) (pad3 xxx))
  )
)

;; Format time (seconds) as mm:ss.xx
(defun lrc-time-format (sec)
  (let* ((seconds (truncate sec))
         (mm (truncate (/ seconds 60)))
         (ss (rem seconds 60))
         (xx (round (* (- sec seconds) 100))))
    (format nil "~a:~a.~a" (pad mm) (pad ss) (pad xx)))
)

; generate srt format subtitle
;;;;;; srt sample text:
;; 1
;; 00:00:00,260 --> 00:00:00,990
;; subtitle 1
;; 
;; 2
;; 00:00:02,220 --> 00:00:06,410
;; subtitle 2
(defun label-to-srt (labels)
  ;; subtitle index
  (let ((srt "") 
        (ind 0))
    (dolist (label labels)
      (setq ind (1+ ind))
      (setf timeS (srt-time-format (first label)))
      (setf timeE (srt-time-format (second label)))
      (string-append srt (format nil "~a~%~a --> ~a~%~a~%~%" ind timeS timeE (third label)))
    )
    (format nil srt)
  )
)


;; generate mp3 lyric
;;;;;; srt sample text:
;; [00:00.26] subtitle 1
;; [00:02.22] subtitle 2
(defun label-to-lrc (labels)
  (setf lrc "")
  (string-append lrc "[ar:Lyrics artist]\n"
                      "[al:Album where the song is from]\n"
                      "[ti:Lyrics (song) title]\n"
                      "[au:Creator of the Songtext]\n"
                      "[length:How long the song is]\n"
                      "[by:Creator of the LRC file]\n"
                      "[offset:+/- Overall timestamp adjustment in milliseconds, + shifts time up, - shifts down]\n"
                      "[re:The player or editor that created the LRC file]\n"
                      "[ve:version of program]\n\n"
  )

  (dolist (label labels)
    (setf timeS (lrc-time-format (first label)))
    ; (setf timeE (lrc-time-format (second label)))
    (string-append lrc (format nil "[~a] ~s~%" timeS (third label)))
  )
  (format nil lrc)
)

;; Return file extension or empty string
(defun get-file-extension (fname)
  (let ((n (1- (length fname)))
        (ext ""))
    (do ((i n (1- i)))
        ((= i 0) ext)
      (when (char= (char fname i) #\.)
        (setf ext (subseq fname i))
        (return ext)
      )
    )
  )
)

;; Get labels from first label track
(setf labels (second (first (aud-get-info "labels"))))

  
;;(setf txt (case file-ext (0 (string-append filename ".SRT") (label-to-srt labels))
;;                         (1 (string-append filename ".LRC") (label-to-lrc labels))
;;          )
;;)

(setf file-ext (string-upcase (get-file-extension filename)))

;; detect file extension to determine which format to export
(setf txt (if (string= ".LRC" file-ext)
            (label-to-lrc labels)
            (label-to-srt labels)
          )
)

(setf fp (open filename :direction :output))
(format fp "~a" txt)
(close fp)

(format nil "~a" txt)
Given a filename, for example "SubtitleGenerator.ny", you can save the ".ny" file to the "Plug-Ins" subdirectory in your audacity location, or install it by "Tools" menu -> "Nyquist Plug-in Installer..." of the software,
Audacity-Subtitle-Generator.png
Audacity-Subtitle-Generator.png (115.25 KiB) Viewed 783 times

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

Re: Convert Label text to LRC file

Post by steve » Wed Dec 23, 2020 4:09 pm

chenghuaiyu wrote:
Wed Dec 23, 2020 2:36 pm
According this forum thread, I've learned a lot and written a plugin which convert label text to SRT subtitle or mp3 LRC format.following is the plugin script code.
Congratulations. That seems to work as intended.

Perhaps I could make a few suggestions. None of these affect the functioning of your script, but are more a matter of convention.


1. Headers

Code: Select all

$nyquist plug-in
$version 4
$type tool analyze
$name (_ "Subtitle Generator")
$manpage "Subtitle_Generator"
$debugbutton disabled
$author (_ "Cheng Huaiyu")
$release 1.0.0
$copyright (_ "Released under terms of the GNU General Public License version 2")
Normally, headers begin with a semi-colon rather than a dollar sign.
(This may not be in the documentation yet)

Headers are treated by Nyquist as comments - they are ignored, but Audacity reads these special comments to convert the Nyquist code into a plug-in.

Comments normally begin with a semi-colon. However, the plug-ins that are shipped with Audacity use a modified form of these comments, with "$" instead of ";" so that shipped plug-ins can be translated into other languages. This translation mechanism does not work for third party plug-ins because the translations have to be compiled into the Audacity app.

The translatable strings are also enclosed in: (_ "translatable string")

For third party plug-ins, the the "(_ ...)" is ignored.

So the recommended way to write the above headers would be:

Code: Select all

;nyquist plug-in
;version 4
;type tool analyze
;name "Subtitle Generator"
;manpage "Subtitle_Generator"
;debugbutton disabled
;author "Cheng Huaiyu"
;release 1.0.0
;copyright "Released under terms of the GNU General Public License version 2"
In future versions of Audacity it "may" be possible to make third party plug-in headers translatable, but there's no guarantee that the syntax will be the same, so best to use the standard semicolon comment syntax.


2. Debug button

Until a plug-in has been available "In the wild" for a while, it's generally a good idea to leave the Debug button enabled. This is extremely helpful if anyone finds a bug. The debug button is enabled by default if you omit the header ";debugbutton disabled".


3. Manual page

The ";manpage" header creates a "?" help link in the interface IF there is a page in the manual with that name.
Example:

Code: Select all

;manpage ""Low-Pass Filter""
will take you to this page in the manual: https://manual.audacityteam.org/man/low ... ilter.html

For third party plug-ins (which are not documented in the manual), there is an alternative (and optional) way to include a help page. See: http://wiki.audacityteam.org/wiki/Nyqui ... s#helpfile


4. Trailing parentheses

The Nyquist language is a form of LISP. As with other forms of Lisp, it is conventional to avoid trailing parentheses. Thus:

Code: Select all

;; Good
(setf txt (if (string= ".LRC" file-ext)
            (label-to-lrc labels)
            (label-to-srt labels)))

;; Bad
(setf txt (if (string= ".LRC" file-ext)
            (label-to-lrc labels)
            (label-to-srt labels)
          )
)
I hope you will find this useful for writing many more beautiful Nyquist plug-ins :-)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

chenghuaiyu
Posts: 2
Joined: Wed Dec 23, 2020 1:53 pm
Operating System: Windows 10

Re: Convert Label text to LRC file

Post by chenghuaiyu » Thu Dec 24, 2020 1:36 am

steve wrote:
Wed Dec 23, 2020 4:09 pm
chenghuaiyu wrote:
Wed Dec 23, 2020 2:36 pm
According this forum thread, I've learned a lot and written a plugin which convert label text to SRT subtitle or mp3 LRC format.following is the plugin script code.
Congratulations. That seems to work as intended.

Perhaps I could make a few suggestions. None of these affect the functioning of your script, but are more a matter of convention.


1. Headers

Code: Select all

$nyquist plug-in
$version 4
$type tool analyze
$name (_ "Subtitle Generator")
$manpage "Subtitle_Generator"
$debugbutton disabled
$author (_ "Cheng Huaiyu")
$release 1.0.0
$copyright (_ "Released under terms of the GNU General Public License version 2")
Normally, headers begin with a semi-colon rather than a dollar sign.
(This may not be in the documentation yet)

Headers are treated by Nyquist as comments - they are ignored, but Audacity reads these special comments to convert the Nyquist code into a plug-in.

Comments normally begin with a semi-colon. However, the plug-ins that are shipped with Audacity use a modified form of these comments, with "$" instead of ";" so that shipped plug-ins can be translated into other languages. This translation mechanism does not work for third party plug-ins because the translations have to be compiled into the Audacity app.

The translatable strings are also enclosed in: (_ "translatable string")

For third party plug-ins, the the "(_ ...)" is ignored.

So the recommended way to write the above headers would be:

Code: Select all

;nyquist plug-in
;version 4
;type tool analyze
;name "Subtitle Generator"
;manpage "Subtitle_Generator"
;debugbutton disabled
;author "Cheng Huaiyu"
;release 1.0.0
;copyright "Released under terms of the GNU General Public License version 2"
In future versions of Audacity it "may" be possible to make third party plug-in headers translatable, but there's no guarantee that the syntax will be the same, so best to use the standard semicolon comment syntax.


2. Debug button

Until a plug-in has been available "In the wild" for a while, it's generally a good idea to leave the Debug button enabled. This is extremely helpful if anyone finds a bug. The debug button is enabled by default if you omit the header ";debugbutton disabled".


3. Manual page

The ";manpage" header creates a "?" help link in the interface IF there is a page in the manual with that name.
Example:

Code: Select all

;manpage ""Low-Pass Filter""
will take you to this page in the manual: https://manual.audacityteam.org/man/low ... ilter.html

For third party plug-ins (which are not documented in the manual), there is an alternative (and optional) way to include a help page. See: http://wiki.audacityteam.org/wiki/Nyqui ... s#helpfile


4. Trailing parentheses

The Nyquist language is a form of LISP. As with other forms of Lisp, it is conventional to avoid trailing parentheses. Thus:

Code: Select all

;; Good
(setf txt (if (string= ".LRC" file-ext)
            (label-to-lrc labels)
            (label-to-srt labels)))

;; Bad
(setf txt (if (string= ".LRC" file-ext)
            (label-to-lrc labels)
            (label-to-srt labels)
          )
)
I hope you will find this useful for writing many more beautiful Nyquist plug-ins :-)
Thanks a lot for your detail explanation.

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

Re: Convert Label text to LRC file

Post by steve » Thu Dec 24, 2020 10:49 am

@chenghuaiyu
I'd like to suggest that you create a new topic for your Label to LRC plug-in so that it doesn't get lost in this long topic topic.

I'm a little concerned about the final line:

Code: Select all

(format nil "~a" txt)
If there's a lot of labels, this may create a window that is too tall, and Nyquist message text windows do not scroll.
How important do you think it is for the plug-in to display the contents of the file, rather than just a confirmation message that the file has been written? (There's several options of what could be done, but as I don't use LRC files I'm uncertain of what is important).

I'd like to see feedback from people that do use LRC files so that the plug-in may be tweaked if necessary prior to "publication". It isn't my decision for which plug-ins are shipped with Audacity, but if users of LRC files are happy with this plug-in, then I can upload it to Audacity's "official" plug-ins on the Audacity wiki (http://wiki.audacityteam.org/wiki/Downl ... t_Plug-ins).
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 03, 2021 5:35 am

steve wrote:
Tue Mar 03, 2020 4:45 pm
You're getting less on Windows than I do on Linux. Your version appears to be completely choking on the Unicode characters.

For the current version of Audacity, it appears that the script pipe module does not support Unicode at all on Windows. I've written to the developers about this, but I'm not hopeful of Unicode support being added any time soon, unless the problem was just an oversight that can be easily fixed.

I'll write back if I get more information. Sorry I couldn't be more help.
Steve, have you got any update?

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

Re: Convert Label text to LRC file

Post by steve » Wed Mar 03, 2021 9:30 am

bigboss97 wrote:
Wed Mar 03, 2021 5:35 am
Steve, have you got any update?
I'm glad you reminded me :) In the release candidate for Audacity 3.0.0 (See: viewtopic.php?f=49&t=116221)

Nyquist still does not support Unicode (and probably never will).

Python scripting now correctly handles multi-byte characters returned from Audacity. (fixed)
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 03, 2021 10:36 pm

steve wrote:
Wed Mar 03, 2021 9:30 am
[Python scripting now correctly handles multi-byte characters returned from Audacity. (fixed)
Super! Just can't wait to test that (continue my work at where I stopped). Oh that was even pre-pendamic.
The other day, I was thinking... I had started something with audacity and given up, but what was the reason?
Luckily, I bookmarked the thread. So I could read through my own thread again :lol:

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

Re: Convert Label text to LRC file

Post by steve » Thu Mar 04, 2021 12:42 am

bigboss97 wrote:
Wed Mar 03, 2021 10:36 pm
Just can't wait to test that
If you feel comfortable doing so, there's a "release candidate" for Audacity 3.0.0 available for testing. See: viewtopic.php?f=49&t=116221
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply