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: 42
Joined: Sat Feb 08, 2020 6:28 am
Operating System: Windows 10

Re: Convert Label text to LRC file

Post by bigboss97 » Fri Feb 28, 2020 6:55 am

steve wrote:
Mon Feb 24, 2020 1:12 pm
You can get multi-byte characters from labels using "GetInfo: Type=Labels Format=JSON" via Python.
Thank you for your time.
This is my current results:
A) ASCII only (all good!)

Code: Select all

C:\tmp>python pipe_test.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
Rcvd: <<<
[
  [ 0,
    [
      [ 1.52091, 2.31039, "hello" ],
      [ 3.47138, 3.47138, "world" ] ] ] ]
BatchCommand finished: OK
B) Unicode
Exported labels:

Code: Select all

1.520907	2.310385	hello
3.471383	3.471383	小苹果
Piped:

Code: Select all

GetInfo: Type=Labels Format=JSON
Rcvd: <<<
[
  [ 0,
    [
      [ 1.52091, 2.31039, "hello" ],
    c:\wxwidgets-3.1.1\include\wx\strvarBatchCommand finished: OK
I know that my Windows/DOS shell doesn't display unicode characters. But it should at least show me some unreadable characters :roll: :?:

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

Re: Convert Label text to LRC file

Post by steve » Fri Feb 28, 2020 11:20 am

bigboss97 wrote:
Fri Feb 28, 2020 6:55 am
know that my Windows/DOS shell doesn't display unicode characters. But it should at least show me some unreadable characters
The scripts provided by Audacity assume that data is plain ASCII, and will not work with multi-byte Unicode characters. With Python3 you will probably see errors like:

Code: Select all

"UnicodeDecodeError: 'utf-8' codec can't decode bytes in position x-y: invalid continuation byte
steve wrote:
Mon Feb 24, 2020 1:12 pm
You can get multi-byte characters from labels using "GetInfo: Type=Labels Format=JSON" via Python.
I should have said that "I think it should be possible to get ..."
The "GetInfo: Type=Labels" can definitely handle multi-byte Unicode characters
(try it from Audacity's "Extra menu > Scriptables II > Get Info").
I "assume" that Python3 is capable of accessing that data, but you would need to write your own Unicode compatible functions to read and write Unicode data to/from Audacity.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Convert Label text to LRC file

Post by bigboss97 » Fri Feb 28, 2020 12:40 pm

steve wrote:
Mon Feb 24, 2020 1:12 pm
(try it from Audacity's "Extra menu > Scriptables II > Get Info").
I "assume" that Python3 is capable of accessing that data, but you would need to write your own Unicode compatible functions to read and write Unicode data to/from Audacity.
It took a while until I found where to activate the Extra-menu :-)
Yes, that works. I can write a Python script to read the JSON output. But how do I pipe that output to my script?

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

Re: Convert Label text to LRC file

Post by steve » Fri Feb 28, 2020 1:24 pm

bigboss97 wrote:
Fri Feb 28, 2020 12:40 pm
But how do I pipe that output to my script?
If you can get Python to read the Unicode labels, then you should be able to the whole thing with Python.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Convert Label text to LRC file

Post by bigboss97 » Sat Feb 29, 2020 3:08 am

With my limited Python skill and Google's help :lol: , I tested the UTF-8 read/write:

Code: Select all

import io

encoding = 'utf8'

with io.open('utf-8.txt', 'r', encoding=encoding, newline='\n') as fin:
    text= fin.read()
    print( text)
    
with io.open('utf-8_out.txt', 'w', encoding=encoding, newline='\n') as fout:
    fout.write(text)
then I modified the pipe_test.py:

Code: Select all

FROMFILE = io.open( FROMNAME,'r', encoding='utf8', newline='\n')
*snip*
    while line != '\n':
        result += line
        line = FROMFILE.readline()
        print(" I read line:["+line+"]")
    return result
I guess I can leave TOFILE as it is.
This is my output:

Code: Select all

GetInfo: Type=Labels Format=JSON"
 I read line:[[
]
 I read line:[  [ 0,
]
 I read line:[    [
]
 I read line:[      [ 0, 0, "Hello" ],
]
 I read line:[    c:\wxwidgets-3.1.1\include\wx\strvararg.BatchCommand finished: OK
]
 I read line:[
]
Rcvd: <<<
[
  [ 0,
    [
      [ 0, 0, "Hello" ],
    c:\wxwidgets-3.1.1\include\wx\strvararg.BatchCommand finished: OK
    
Is Audacity writing an UTF-8 pipe?
mod-script-pipe is a DLL. I couldn't look into it.

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

Re: Convert Label text to LRC file

Post by steve » Sat Feb 29, 2020 11:53 am

Does it work for you with just a single label:

Code: Select all

3.471383	3.471383	小苹果
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Convert Label text to LRC file

Post by bigboss97 » Sat Feb 29, 2020 12:31 pm

steve wrote:
Sat Feb 29, 2020 11:53 am
Does it work for you with just a single label:
No, it shows one path. Basically it shows a path whenever it's unicode. For instance:

Code: Select all

0.359909	0.359909	Hello
2.159456	2.159456	回頭我也
4.040272	4.040272	world
6.315828	6.315828	不要你

Code: Select all

 I read line:[      [ 0.359909, 0.359909, "Hello" ],
]
 I read line:[    c:\wxwidgets-3.1.1\include\wx\st      [ 4.04027, 4.04027, "world" ],
]
 I read line:[    c:\wxwidgets-3.1.1\include\wx\strvarBatchCommand finished: OK
]
My UTF-8 routine failed totally when I tried with German:

Code: Select all

1.253878	1.253878	Lüneburg

Code: Select all

GetInfo: Type=Labels Format=JSON"
 I read line:[[
]
Traceback (most recent call last):
  File "pipe_utf-8.py", line 82, in <module>
    quick_test()
  File "pipe_utf-8.py", line 79, in quick_test
    do_command('GetInfo: Type=Labels Format=JSON"')
  File "pipe_utf-8.py", line 71, in do_command
    response = get_response()
  File "pipe_utf-8.py", line 64, in get_response
    line = FROMFILE.readline()
  File "C:\Users\home\AppData\Local\Programs\Python\Python37\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 42: invalid start byte

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

Re: Convert Label text to LRC file

Post by steve » Sat Feb 29, 2020 1:25 pm

Which version of Python are you using?
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

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

Re: Convert Label text to LRC file

Post by steve » Sat Feb 29, 2020 1:54 pm

On Linux, with Python 3, this will read the return message and print as hex values. You may notice that with two labels containing "小苹果", the printed data is a mix of UTF-8 and non-UTF-8 (UTF-8 and "latin 1" characters perhaps?).

Code: Select all

import os
import sys

PIPE_BASE = '/tmp/audacity_script_pipe.'
WRITE_NAME = PIPE_BASE + 'to.' + str(os.getuid())
READ_NAME = PIPE_BASE + 'from.' + str(os.getuid())
EOL = '\n'

write_pipe = open(WRITE_NAME, 'w')
read_pipe = open(READ_NAME, 'rb')

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

def get_response():
    """Return the command response."""
    result = ''
    line = ''
    last = ''
    while (last != b'\n' or line != b'\n'):
        #result += line
        last = line
        line = read_pipe.read(1)
        print(line.hex(), end=' ')


send_command("GetInfo: Type=Labels")
get_response()

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

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

Re: Convert Label text to LRC file

Post by bigboss97 » Sun Mar 01, 2020 11:53 am

steve wrote:
Sat Feb 29, 2020 1:25 pm
Which version of Python are you using?
Python 3.7.5 on Windows 10
Audacity 2.3.3

Post Reply