Henrik Gill
Posts: 108
Joined: Sat Feb 24, 2024 3:23 pm

How do I fix this Python code?

Sun Feb 25, 2024 1:34 pm

I am making a audio recorder with a Raspberry Pi 0 W 2 and I have some python code. The problem is that they are 2 separate projects. (1 for playing and 1 for pausing.) How can I make it 1 project? When I run the code, it starts recording, but when I stop the code, it still runs. My goal is when I run the code, it records, and when I stop running the code it stops recording. I am using the Audacity script-mod-pipe. What should I do?
Here is my code:

Code: Select all

import sys

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.  This is a good thing.")

TOFILE = open(TONAME, 'w')
print("-- File to which to write has been opened")
FROMFILE = open(FROMNAME, 'rt')
print("-- File from which to read has now been opened also\r\n")
def send_command(command):
    """Send a single command."""
    print("Send: >>> \n"+command)
    TOFILE.write(command + EOL)
    TOFILE.flush()

#record command  \/
send_command("Record1stChoice")

import sys

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.  This is a good thing.")

TOFILE = open(TONAME, 'w')
print("-- File to which to write has been opened")
FROMFILE = open(FROMNAME, 'rt')
print("-- File from which to read has now been opened also\r\n")
def send_command(command):
    """Send a single command."""
    print("Send: >>> \n"+command)
    TOFILE.write(command + EOL)
    TOFILE.flush()

#stop command  \/
send_command("Stop")
As you can see, there are 2 scripts, how can I make it only 1? Can anyone help?

User avatar
scruss
Posts: 5816
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON

Re: How do I fix this Python code?

Sun Feb 25, 2024 9:53 pm

This is untried, but maybe replace everything from #record command \/ onwards with:

Code: Select all

try:
    send_command("Record1stChoice")
except KeyboardInterrupt:
    send_command("Stop")
    sys.exit()
I don't know if the Record1stChoice command waits or not. You might have to put an infinite loop after it if it doesn't wait.

The two scripts are basically the same, they only send different commands. So they can be combined.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

Henrik Gill
Posts: 108
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Sun Feb 25, 2024 10:08 pm

I see thank you

Henrik Gill
Posts: 108
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Sun Feb 25, 2024 10:49 pm

When I ran the script it said 'Invalid Syntax'

User avatar
scruss
Posts: 5816
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON

Re: How do I fix this Python code?

Sun Feb 25, 2024 11:56 pm

please paste whole script into a code section here
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

Henrik Gill
Posts: 108
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Mon Feb 26, 2024 12:41 am

Here's the code:

Code: Select all

import sys

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.  This is a good thing.")

TOFILE = open(TONAME, 'w')
print("-- File to which to write has been opened")
FROMFILE = open(FROMNAME, 'rt')
print("-- File from which to read has now been opened also\r\n")
def send_command(command):
    """Send a single command."""
    print("Send: >>> \n"+command)
    TOFILE.write(command + EOL)
    TOFILE.flush()

send_command("Record1stChoice")
except KeyboardInterrupt:
    send_command("Stop")
    sys.exit()
    

User avatar
B.Goode
Posts: 16299
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How do I fix this Python code?

Mon Feb 26, 2024 12:37 pm

The suggestion from @scruss included a

Code: Select all

try:
statement.

Might its omission be the cause of the syntax error?

Including the whole error message and related traceback as part of your question would remove any uncertainty.
Beware of the Leopard


Henrik Gill
Posts: 108
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Mon Feb 26, 2024 1:04 pm

I added the

Code: Select all

try:
and it didn't say "syntax error" but when I ran the code it recorded fine but when I stopped it, it still was recording. It was supposed to stop recording when I stopped the code but it didn't. Any ideas how to fix?

User avatar
B.Goode
Posts: 16299
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How do I fix this Python code?

Mon Feb 26, 2024 1:57 pm

Perhaps the reason for the unexpected behaviour has something in common with your difficulty using the Export feature?

The suggestion in your parallel thread about understanding the Audacity API maybe appropriate here also.
Beware of the Leopard

Henrik Gill
Posts: 108
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Mon Feb 26, 2024 8:42 pm

No, the export is a whole different project. It just has to stop recording when the code is stopped.

User avatar
B.Goode
Posts: 16299
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How do I fix this Python code?

Mon Feb 26, 2024 10:17 pm

Henrik Gill wrote:
Mon Feb 26, 2024 8:42 pm
No, the export is a whole different project. It just has to stop recording when the code is stopped.
It seems to be using the same program interface to Audacity?


In which case this might be the common feature you need to consider?

Ref: https://manual.audacityteam.org/man/scr ... l#Commands
After checking that the "pipe_test.py" script works, you could try adding other commands to the end of the pipe_test.py script. Each command name ends with a colon, and may be followed by parameters. For example:

do_command("Help:")

For practical scripting, the script "pipeclient.py" provides a useful starting point:


In both examples of your modified code the Stop and Export keywords lack colons. Might that be significant?
Beware of the Leopard

Henrik Gill
Posts: 108
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Mon Feb 26, 2024 11:13 pm

No when I added the colon it said "invalid syntax"

User avatar
B.Goode
Posts: 16299
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How do I fix this Python code?

Tue Feb 27, 2024 8:19 am

Henrik Gill wrote:
Mon Feb 26, 2024 11:13 pm
No when I added the colon it said "invalid syntax"

If you hope for assistance to fix syntax errors in your Python code you will need to post the failing code and the full error message and traceback that results from trying to run it.
Beware of the Leopard

Henrik Gill
Posts: 108
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Tue Feb 27, 2024 1:06 pm

No, I don't need the colon. I would only need the colon if I ran pipeclient.py. I just want audacity to stop recording when the code is stopped. The record works, just not the stop recording.

User avatar
B.Goode
Posts: 16299
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How do I fix this Python code?

Tue Feb 27, 2024 2:20 pm

Maybe your (two) difficulties using the Audacity Scripting Module are independent of the fact that you happen to be using Python on Raspberry Pi hardware and OS as a platform.

If the Audacity documentation at https://manual.audacityteam.org/man/scripting.html is not clear, or possibly incorrect, perhaps you would find knowledgeable advice directly from an Audacity support forum?

https://forum.audacityteam.org/



I have nothing further to contribute.
Beware of the Leopard

hippy
Posts: 16090
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How do I fix this Python code?

Tue Feb 27, 2024 3:36 pm

Henrik Gill wrote:
Mon Feb 26, 2024 1:04 pm
I added the

Code: Select all

try:
and it didn't say "syntax error" but when I ran the code it recorded fine but when I stopped it, it still was recording. It was supposed to stop recording when I stopped the code but it didn't. Any ideas how to fix?
How are you stopping the code ?

Assuming the code as you have it is as below, and things work as hoped -

Code: Select all

try:
    send_command("Record1stChoice")
except KeyboardInterrupt:
    send_command("Stop")
    sys.exit()
It will send the "Record1stChoice", won't error, will continue at the line after 'sys.exit()', won't ever send a "Stop" command.

If that is your code, there's nothing after it, it should automatically exit to the command line after sending the "Record1stChoice", no need to explicitly stop the code.

It seems to be behaving as expected. You would need a 'while True: pass' after sending "Record1stChoice" so you then have to use Ctrl-C to have the exception handle that and send the "Stop" command. Something like this though not correct use of error trapping IMO -

Code: Select all

try:
    send_command("Record1stChoice")
    while True:
        pass
except KeyboardInterrupt:
    send_command("Stop")
    sys.exit()
I would suggest -

Code: Select all

send_command("Record1stChoice")
try:
    while True:
        pass
except KeyboardInterrupt:
    pass
send_command("Stop")

Henrik Gill
Posts: 108
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Tue Feb 27, 2024 11:04 pm

Again, when I press "Run" it records. Then I press "Stop" and it still records.What do I do?

ame
Posts: 8924
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: How do I fix this Python code?

Tue Feb 27, 2024 11:39 pm

Have you tried something simple, to make sure the commands actually work?

Code: Select all

import time
send_command("Record1stChoice")
time.sleep(5)
send_command("Stop")
This should start recording, wait five seconds, then stop recording.

If it works, it means the commands work, so you just have a scheduling problem. If it doesn't work it means it's broken and will never work, or you've misinterpreted something.
Oh no, not again.

hippy
Posts: 16090
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How do I fix this Python code?

Wed Feb 28, 2024 12:11 pm

Henrik Gill wrote:
Tue Feb 27, 2024 11:04 pm
Again, when I press "Run" it records. Then I press "Stop" and it still records.What do I do?
What do you mean by "press Run" and "press Stop" ?

You haven't shown any code which has anything to do with something being pushed. It's difficult to tell what you may be doing wrong if you don't post the code you are actually using.

Henrik Gill
Posts: 108
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Wed Feb 28, 2024 1:19 pm

The run is in the python editor, Mu. I pressed Run at the top of the app to run the code and stop to stop running it. The buttons aren't external they are built in the app.

memjr
Posts: 3815
Joined: Fri Aug 21, 2020 5:59 pm

Re: How do I fix this Python code?

Wed Feb 28, 2024 1:52 pm

Henrik Gill wrote:
Wed Feb 28, 2024 1:19 pm
The run is in the python editor, Mu. I pressed Run at the top of the app to run the code and stop to stop running it. The buttons aren't external they are built in the app.
That's because you're not sending the "stop" command before your program exits.

The stop button interrupts your program. It does not send the stop command to stop the recording.

Henrik Gill
Posts: 108
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Wed Feb 28, 2024 2:51 pm

ame wrote:
Tue Feb 27, 2024 11:39 pm
Have you tried something simple, to make sure the commands actually work?

Code: Select all

import time
send_command("Record1stChoice")
time.sleep(5)
send_command("Stop")
This should start recording, wait five seconds, then stop recording.

If it works, it means the commands work, so you just have a scheduling problem. If it doesn't work it means it's broken and will never work, or you've misinterpreted something.

ame
Posts: 8924
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: How do I fix this Python code?

Wed Feb 28, 2024 3:47 pm

Henrik Gill wrote:
Wed Feb 28, 2024 2:51 pm
ame wrote:
Tue Feb 27, 2024 11:39 pm
Have you tried something simple, to make sure the commands actually work?

Code: Select all

import time
send_command("Record1stChoice")
time.sleep(5)
send_command("Stop")
This should start recording, wait five seconds, then stop recording.

If it works, it means the commands work, so you just have a scheduling problem. If it doesn't work it means it's broken and will never work, or you've misinterpreted something.
Did you have something to add here?
Oh no, not again.

hippy
Posts: 16090
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How do I fix this Python code?

Wed Feb 28, 2024 5:14 pm

Code: Select all

def send_command(command):
    """Send a single command."""
    print("Send: >>> \n"+command)
That should be reporting whenever a command is sent. If it's not being reported it is unlikely being sent.

Return to “Python”