KanoMaster22
Posts: 45
Joined: Wed Oct 05, 2016 6:06 pm

Control-C in Python

Fri Aug 11, 2017 10:06 am

I am currently creating a simple (well it was supposed to be) python script that makes a recording with the stereo mirophone on the voice hat. Everything is working apart from I can not end the recording as I don't know how to simulate ctrl-c with os. Here is my script:

Code: Select all

from os import system

system('arecord -c2 -f S32_LE -r 48000 /home/pi/voice-recognizer-raspi/src/music/test_record.wav')
input("Press enter to finish recording")
system('???') #control c needs to go here
Thanks in advance for any help,
KanoMaster22

User avatar
MrYsLab
Posts: 74
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Control-C in Python

Fri Aug 11, 2017 1:30 pm

Do actually need a Control C or do you simply wish to exit. If you just want to exit, then

Code: Select all

import sys
from os import system

system('arecord -c2 -f S32_LE -r 48000 /home/pi/voice-recognizer-raspi/src/music/test_record.wav')
input("Press enter to finish recording")
sys.exit(0)


KanoMaster22
Posts: 45
Joined: Wed Oct 05, 2016 6:06 pm

Re: Control-C in Python

Fri Aug 11, 2017 2:56 pm

I need to use control-c to finish the recording. I've tried exiting and it didn't work.

User avatar
elParaguayo
Posts: 1877
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Control-C in Python

Fri Aug 11, 2017 3:24 pm

Then you might want to try using the subprocess module and use Popen to start the process. You can then send signals to the process. However, I have read that this doesn't always work so you may want to try and report back:

Option 1:

Code: Select all

import subprocess

cmd = ['arecord', '-c2', '-f', 'S32_LE', '-r', '48000', '/home/pi/voice-recognizer-raspi/src/music/test_record.wav']

proc = subprocess.Popen(cmd)
input("Press enter to finish recording")
proc.terminate()
Option 2:

Code: Select all

import subprocess
import signal
import os

cmd = ['arecord', '-c2', '-f', 'S32_LE', '-r', '48000', '/home/pi/voice-recognizer-raspi/src/music/test_record.wav']

proc = subprocess.Popen(cmd)
input("Press enter to finish recording")
os.kill(proc.pid, signal.SIGINT)
Edit: Option 3 (bit of a hack)

Code: Select all

import subprocess

cmd = ['arecord', '-c2', '-f', 'S32_LE', '-r', '48000', '/home/pi/voice-recognizer-raspi/src/music/test_record.wav']

proc = subprocess.Popen(cmd)
input("Press enter to finish recording")
subprocess.call(["killall", "arecord"])
Hopefully one of these will work.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

KanoMaster22
Posts: 45
Joined: Wed Oct 05, 2016 6:06 pm

Re: Control-C in Python

Fri Aug 11, 2017 3:54 pm

I will try these thanks for the help.

User avatar
elParaguayo
Posts: 1877
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Control-C in Python

Fri Aug 11, 2017 3:58 pm

Don't thank me yet. They may not work!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

KanoMaster22
Posts: 45
Joined: Wed Oct 05, 2016 6:06 pm

Re: Control-C in Python

Wed Sep 20, 2017 5:09 pm

Finally got round to trying the code and when I used the first option it worked with no problems. Thanks elParaguayo

KanoMaster22
Posts: 45
Joined: Wed Oct 05, 2016 6:06 pm

Re: Control-C in Python

Wed Sep 20, 2017 5:32 pm

Now, all that is working but I need to move and rename a file before I record something. Here is my code:

Code: Select all

import subprocess

cmd = ['arecord', '-c2', '-f', 'S32_LE', '-r', '48000', '/home/pi/voice-recognizer-raspi/src/music/test_record.wav']
cmd_two = ['mv /home/pi/voice-recognizer-raspi/src/music/test_record.wav /home/pi/voice-recognizer-raspi/src/music/test_record_old.wav']

proc_two = subprocess.Popen(cmd_two)
proc_two.terminate()
proc = subprocess.Popen(cmd)
input("Press enter to finish recording")
proc.terminate()
The idea is that first my old test record file (test_record.wav) needs to be renamed (to test_record_old) so I can record a new test_record. I don't already have a file called test_record_old.wav just in case anyone wants to know. Here is the error message I get when I run the code:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/voice-recognizer-raspi/src/record.py", line 8, in <module>
    proc_two = subprocess.Popen(cmd_two)
  File "/usr/lib/python3.4/subprocess.py", line 859, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.4/subprocess.py", line 1457, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'mv /home/pi/voice-recognizer-raspi/src/music/test_record.wav /home/pi/voice-recognizer-raspi/src/music/test_record_old.wav'

Thanks in advance for any help,
LouisP

User avatar
elParaguayo
Posts: 1877
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Control-C in Python

Wed Sep 20, 2017 7:45 pm

Try changing cmd_two as follows:

Code: Select all

cmd_two = ['mv', '/home/pi/voice-recognizer-raspi/src/music/test_record.wav', '/home/pi/voice-recognizer-raspi/src/music/test_record_old.wav']
You also don't need to terminate cmd_two as it will complete instantly.

However, it's worth noting that you can use the os module to rename files. See the docs for more.

The equivalent would be

Code: Select all

import os
os.rename('/home/pi/voice-recognizer-raspi/src/music/test_record.wav', '/home/pi/voice-recognizer-raspi/src/music/test_record_old.wav')
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

KanoMaster22
Posts: 45
Joined: Wed Oct 05, 2016 6:06 pm

Re: Control-C in Python

Thu Sep 21, 2017 6:16 pm

Thank you elParaguayo. It works perfectly now!

Return to “Python”

Who is online

Users browsing this forum: asandford and 12 guests