-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrec.py
53 lines (44 loc) · 1.53 KB
/
rec.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
# From https://github.com/spatialaudio/python-sounddevice/blob/0.4.6/examples/rec_unlimited.py
import queue
from os import path, listdir
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 nname(fname='sample.wav', mydir='wav'):
'''Gets a new (unused) filename, numerically incremented'''
p,_,s = fname.partition('.')
a = listdir(mydir)
n = 0
for x in a:
b = x.partition(p)[2]
c = b.partition('.')[0]
if c.isnumeric():
n = max(n,int(c))
fname = path.join(mydir, f'{p}{n+1}.{s}')
return fname
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())
try:
samplerate = 44100
channels = 1
# Make sure the file is opened before recording anything:
f = nname()
with sf.SoundFile(f, mode='x', samplerate=samplerate,
channels=channels) as file:
with sd.InputStream(samplerate=samplerate,
channels=channels, callback=callback):
print('#' * 80)
print('press Ctrl+C to stop the recording')
print('#' * 80)
while True:
file.write(q.get())
except KeyboardInterrupt:
print('\nRecording finished: ' + f)
except Exception as e:
print(e, file=sys.stederr)