KanoMaster22
Posts: 34
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: 39
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: 34
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: 1836
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: 34
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: 1836
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.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 20 guests