satish.gunjal
Posts: 39
Joined: Sat May 23, 2015 11:50 am

'arecord' command using 'subprocess' in python

Thu Dec 06, 2018 5:41 am

I am using Adafruit I2S MEMS microphone with Raspberry Pi Zero W.

Followed the Adafruit guide to configure the microphone. I am able to record the audio using below commands.

Code: Select all

arecord -D plughw:1 -c1 -r 48000 -f S32_LE -t wav -V mono -v file.wav

Code: Select all

arecord -D dmic_sv -c2 -r 44100 -f S32_LE -t wav -V mono -v file.wav
I want to run this command from my python script so that i can start and stop the recording on Rest web method call.
Below is the sample script I am using for testing...

Code: Select all

import subprocess
import os
import time
import signal

#proc_args = ['arecord', '-D' , 'dmic_sv' , '-c2' , '-r' , '44100' , '-f' , 'S32_LE' , '-t' , 'wav' , '-V' , 'mono' , '-v' , 'subprocess1.wav']
proc_args = ['arecord', '-D' , 'plughw:1' , '-c1' , '-r' , '44100' , '-f' , 'S32_LE' , '-t' , 'wav' , '-V' , 'mono' , '-v' , 'subprocess1.wav']
rec_proc = subprocess.Popen('proc_args', shell=True, preexec_fn=os.setsid)
print("startRecordingArecord()> rec_proc pid= " + str(rec_proc.pid))
print("startRecordingArecord()> recording started")

time.sleep(20)
os.killpg(rec_proc.pid, signal.SIGTERM)
rec_proc.terminate()
rec_proc = None
print("stopRecordingArecord()> Recording stopped")
Audio is not getting recorded, output is as below...

Code: Select all

[email protected]:~/test $ sudo python subprocess1.py
startRecordingArecord()> rec_proc pid= 2161
startRecordingArecord()> recording started
Usage: arecord [OPTION]... [FILE]...

-h, --help              help
    --version           print current version
-l, --list-devices      list all soundcards and digital audio devices
-L, --list-pcms         list device names
-D, --device=NAME       select PCM by name
-q, --quiet             quiet mode
-t, --file-type TYPE    file type (voc, wav, raw or au)
-c, --channels=#        channels
-f, --format=FORMAT     sample format (case insensitive)
-r, --rate=#            sample rate
-d, --duration=#        interrupt after # seconds
-M, --mmap              mmap stream
-N, --nonblock          nonblocking mode
-F, --period-time=#     distance between interrupts is # microseconds
-B, --buffer-time=#     buffer duration is # microseconds
    --period-size=#     distance between interrupts is # frames
    --buffer-size=#     buffer duration is # frames
-A, --avail-min=#       min available space for wakeup is # microseconds
-R, --start-delay=#     delay for automatic PCM start is # microseconds
                        (relative to buffer size if <= 0)
-T, --stop-delay=#      delay for automatic PCM stop is # microseconds from xrun
-v, --verbose           show PCM structure and setup (accumulative)
-V, --vumeter=TYPE      enable VU meter (TYPE: mono or stereo)
-I, --separate-channels one file for each channel
-i, --interactive       allow interactive operation from stdin
-m, --chmap=ch1,ch2,..  Give the channel map to override or follow
    --disable-resample  disable automatic rate resample
    --disable-channels  disable automatic channel conversions
    --disable-format    disable automatic format conversions
    --disable-softvol   disable software volume control (softvol)
    --test-position     test ring buffer position
    --test-coef=#       test coefficient for ring buffer position (default 8)
                        expression for validation is: coef * (buffer_size / 2)
    --test-nowait       do not wait for ring buffer - eats whole CPU
    --max-file-time=#   start another output file when the old file has recorded
                        for this many seconds
    --process-id-file   write the process ID here
    --use-strftime      apply the strftime facility to the output file name
    --dump-hw-params    dump hw_params of the device
    --fatal-errors      treat all errors as fatal
Recognized sample formats are: S8 U8 S16_LE S16_BE U16_LE U16_BE S24_LE S24_BE U24_LE U24_BE S32_LE S32_BE U32_LE U32_BE FLOAT_LE FLOAT_BE FLOAT64_LE FLOAT64_BE IEC958_SUBFRAME_LE IEC958_SUBFRAME_BE MU_LAW A_LAW IMA_ADPCM MPEG GSM SPECIAL S24_3LE S24_3BE U24_3LE U24_3BE S20_3LE S20_3BE U20_3LE U20_3BE S18_3LE S18_3BE U18_3LE U18_3BE G723_24 G723_24_1B G723_40 G723_40_1B DSD_U8 DSD_U16_LE DSD_U32_LE DSD_U16_BE DSD_U32_BE
Some of these may not be available on selected hardware
The available format shortcuts are:
-f cd (16 bit little endian, 44100, stereo)
-f cdr (16 bit big endian, 44100, stereo)
-f dat (16 bit little endian, 48000, stereo)
stopRecordingArecord()> Recording stopped
Note: I am able to record audio using pyaudio library but quality(multiple clicking sound) not good thats why i am exploring this option.

User avatar
neilgl
Posts: 253
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: 'arecord' command using 'subprocess' in python

Thu Dec 06, 2018 10:10 am

Probably it will work OK as user pi if you change the line to

Code: Select all

rec_proc = subprocess.Popen(proc_args, shell=False, preexec_fn=os.setsid)
i.e. remove the quotes from proc_args and set shell to False not True
and no need to run as sudo

Code: Select all

python mic.py

satish.gunjal
Posts: 39
Joined: Sat May 23, 2015 11:50 am

Re: 'arecord' command using 'subprocess' in python

Thu Dec 06, 2018 11:44 am

thanks it worked!!

working code...

Code: Select all

import subprocess
import os
import time
import signal

#proc_args = ['arecord', '-D' , 'dmic_sv' , '-c2' , '-r' , '44100' , '-f' , 'S32_LE' , '-t' , 'wav' , '-V' , 'mono' , '-v' , 'subprocess1.wav']
proc_args = ['arecord', '-D' , 'plughw:1' , '-c1' , '-r' , '44100' , '-f' , 'S32_LE' , '-t' , 'wav' , '-V' , 'mono' , '-v' , 'subprocess1.wav']
rec_proc = subprocess.Popen(proc_args, shell=False, preexec_fn=os.setsid)
print("startRecordingArecord()> rec_proc pid= " + str(rec_proc.pid))
print("startRecordingArecord()> recording started")

time.sleep(20)
os.killpg(rec_proc.pid, signal.SIGTERM)
rec_proc.terminate()
rec_proc = None
print("stopRecordingArecord()> Recording stopped")
any idea why its not working with below 'arecord' command?

Code: Select all

proc_args = ['arecord', '-D' , 'dmic_sv' , '-c2' , '-r' , '44100' , '-f' , 'S32_LE' , '-t' , 'wav' , '-V' , 'mono' , '-v' , 'subprocess1.wav']

User avatar
neilgl
Posts: 253
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: 'arecord' command using 'subprocess' in python

Thu Dec 06, 2018 11:58 pm

Sorry no idea why dmic_sv does not work.

Return to “Python”