-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrec_unlimited.py
58 lines (44 loc) · 1.69 KB
/
rec_unlimited.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
"""Create a recording with arbitrary duration.
The soundfile module (https://PySoundFile.readthedocs.io/) has to be installed!
"""
import os
import tempfile
import queue
import sys
import sounddevice as sd
import soundfile as sf
import numpy # Make sure NumPy is loaded before it is used in the callback
assert numpy # avoid "imported but unused" message (W0611)
def int_or_str(text):
"""Helper function for argument parsing."""
try:
return int(text)
except ValueError:
return text
q = queue.Queue()
def callback(indata, frames, time, status):
"""This is called (from a separate thread) for each audio block."""
if status:
print(status, file=sys.stderr)
q.put(indata.copy())
def record(filename=(os.environ['HOME'] + '/station/movements/sound.wav'),
samplerate=48000,
device='snd_rpi_i2s_card',
channels=1,
subtype=None):
print('Starting to record to ' + filename)
try:
if samplerate is None:
device_info = sd.query_devices(device, 'input')
# soundfile expects an int, sounddevice provides a float:
samplerate = int(device_info['default_samplerate'])
# Make sure the file is opened before recording anything:
with sf.SoundFile(filename, mode='x', samplerate=samplerate,
channels=channels, subtype=subtype) as file:
with sd.InputStream(samplerate=samplerate, device=device,
channels=channels, callback=callback):
while True:
file.write(q.get())
except Exception as e:
parser.exit(type(e).__name__ + ': ' + str(e))