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.
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 Mar 20, 2021 2:07 am

steve wrote:
Thu Mar 04, 2021 12:42 am
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
I got RC6 portable. I haven't done that for such long that I'm not sure whether I've missed any step :lol:
I enabled mod-script-pipe, restarted and double checked enabled. When I ran my script I got nothing returned:

Code: Select all

PS E:\work\python\Audacity> python .\pipe_utf-8.py
pipe-test.py, running on windows
Write to  "\\.\pipe\ToSrvPipe"
Read from "\\.\pipe\FromSrvPipe"
-- Both pipes exist.  Good.
-- File to write to has been opened
-- File to read from has now been opened too

Send: >>>
GetInfo: Type=Labels Format=JSON"
 I read line:[
]
Rcvd: <<<
When I ran the same script on v2.3.3 I got:

Code: Select all

PS E:\work\python\Audacity> python .\pipe_utf-8.py
pipe-test.py, running on windows
Write to  "\\.\pipe\ToSrvPipe"
Read from "\\.\pipe\FromSrvPipe"
-- Both pipes exist.  Good.
-- File to write to has been opened
-- File to read from has now been opened too

Send: >>>
GetInfo: Type=Labels Format=JSON"
 I read line:[[
]
 I read line:[  [ 0,
]
 I read line:[    [
]
 I read line:[      [ 0, 0, "Hello" ] ] ] ]
]
 I read line:[BatchCommand finished: OK
]
 I read line:[
]
Rcvd: <<<
[
  [ 0,
    [
      [ 0, 0, "Hello" ] ] ] ]
BatchCommand finished: OK
Another strange thing also happened. When I restart v3 mod-script-pipe is reset to New (after v3 has been launched).

The script:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import io
import os
import sys


if sys.platform == 'win32':
    print("pipe-test.py, running on windows")
    TONAME = '\\\\.\\pipe\\ToSrvPipe'
    FROMNAME = '\\\\.\\pipe\\FromSrvPipe'
    EOL = '\r\n\0'
else:
    print("pipe-test.py, running on linux or mac")
    TONAME = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
    FROMNAME = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
    EOL = '\n'

print("Write to  \"" + TONAME +"\"")
if not os.path.exists(TONAME):
    print(" ..does not exist.  Ensure Audacity is running with mod-script-pipe.")
    sys.exit()

print("Read from \"" + FROMNAME +"\"")
if not os.path.exists(FROMNAME):
    print(" ..does not exist.  Ensure Audacity is running with mod-script-pipe.")
    sys.exit()

print("-- Both pipes exist.  Good.")

TOFILE = open(TONAME, 'w')
print("-- File to write to has been opened")
#FROMFILE = open(FROMNAME, 'rt')
FROMFILE = io.open( FROMNAME,'r', encoding='utf8', newline='\n')
print("-- File to read from has now been opened too\r\n")


def send_command(command):
    """Send a single command."""
    print("Send: >>> \n"+command)
    TOFILE.write(command + EOL)
    TOFILE.flush()

def get_response():
    """Return the command response."""
    result = ''
    line = ''
    while line != '\n':
        result += line
        line = FROMFILE.readline()
        print(" I read line:["+line+"]")
    return result

def do_command(command):
    """Send one command, and return the response."""
    send_command(command)
    response = get_response()
    print("Rcvd: <<< \n" + response)
    return response

def quick_test():
    """Example list of commands."""
#    do_command('Help: Command=Help')
#    do_command('Help: Command="GetInfo"')
    do_command('GetInfo: Type=Labels Format=JSON"')
    #do_command('SetPreference: Name=GUI/Theme Value=classic Reload=1')

quick_test()

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

Re: Convert Label text to LRC file

Post by steve » Sat Mar 20, 2021 11:33 am

Testing with Audacity 3.0.0 (release version) on Linux, when I send the command (from Python "pipeclient.py"):

Code: Select all

GetInfo: Type=Labels Format=JSON"
I get:

Code: Select all

Sending command: GetInfo: Type=Labels Format=JSON"
[ 
  [ 1,
    [ 
      [ 0.435929, 0.435929, "hello" ],
      [ 2.13066, 3.33559, "world" ] ] ] ]
BatchCommand finished: OK

bigboss97 wrote:
Sat Mar 20, 2021 2:07 am
I got RC6 portable. I haven't done that for such long that I'm not sure whether I've missed any step
Now that Audacity 3.0.0 has been released, it's probably best to resume your Python work with the release version. See: https://www.audacityteam.org/download/windows/
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 Mar 20, 2021 12:36 pm

steve wrote:
Sat Mar 20, 2021 11:33 am
Now that Audacity 3.0.0 has been released, it's probably best to resume your Python work with the release version. See: https://www.audacityteam.org/download/windows/
Got the same result on v3 release. Do I have to do something under Extra menu which I've done on my v2?

Code: Select all

PS E:\work\python\Audacity> python.exe .\pipe_utf-8.py
pipe-test.py, running on windows
Write to  "\\.\pipe\ToSrvPipe"
Read from "\\.\pipe\FromSrvPipe"
-- Both pipes exist.  Good.
-- File to write to has been opened
-- File to read from has now been opened too

Send: >>>
GetInfo: Type=Labels Format=JSON"
 I read line:[
]
Rcvd: <<<

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

Re: Convert Label text to LRC file

Post by steve » Sat Mar 20, 2021 1:53 pm

bigboss97 wrote:
Sat Mar 20, 2021 12:36 pm
Do I have to do something under Extra menu which I've done on my v2?
Just ensure that "mod-script-pipe" is enabled in "Preferences > Modules".
(Note that Audacity has to be restarted after changing that setting)

Can you get any commands to work from Python? (for example, try sending "Play:")
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 Mar 20, 2021 11:49 pm

steve wrote:
Sat Mar 20, 2021 1:53 pm
Can you get any commands to work from Python? (for example, try sending "Play:")
"Play:" works, confirmed.

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 Mar 21, 2021 12:08 am

Just tried:
do_command('SetLabel: Text=Hello LabelIndex=1 Start=2 End=3')
that works.

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

Re: Convert Label text to LRC file

Post by steve » Sun Mar 21, 2021 12:19 am

Does

Code: Select all

GetInfo: Type=Labels Format=JSON"
work for you with "pipeclient.py" (command line interface)?
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 » Sun Mar 21, 2021 12:46 am

Where can I find pipeclient.py?

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

Re: Convert Label text to LRC file

Post by steve » Sun Mar 21, 2021 10:02 am

bigboss97 wrote:
Sun Mar 21, 2021 12:46 am
Where can I find pipeclient.py?
The same place as pipe_test.py. Here: https://github.com/audacity/audacity/tr ... piped-work
Documentation is in the file (See the Docstrings).
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 » Sun Mar 21, 2021 10:32 am

Great! pipeclient works and the unicode issue has been resolved.
Now I have to update my old script for v3. Thank you so much for your very patient help.

Code: Select all

Enter command or 'Q' to quit: GetInfo: Type=Labels Format=JSON
Sending command: GetInfo: Type=Labels Format=JSON
[
  [ 1,
    [
      [ 0, 0, "Hello" ],
      [ 1.31483, 1.31483, "小苹果" ] ] ] ]
      

Post Reply