-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mic_rec.py
64 lines (54 loc) · 1.35 KB
/
test_mic_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
54
55
56
57
58
59
60
61
62
63
64
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser = argparse.ArgumentParser()
parser.add_argument(
'-o',
'--output',
default='test.wav',
help='Recorded sound file')
parser.add_argument(
'-d',
'--device',
type=int,
help='Choised record device ID')
args = parser.parse_args()
import sounddevice as sd
from scipy.io.wavfile import write
import contextlib
import os
import sys
@contextlib.contextmanager
def ignore_stderr():
devnull = os.open(os.devnull, os.O_WRONLY)
old_stderr = os.dup(2)
sys.stderr.flush()
os.dup2(devnull, 2)
os.close(devnull)
try:
yield
finally:
os.dup2(old_stderr, 2)
os.close(old_stderr)
if args.device:
sd.default.device = args.device
devices = sd.query_devices()
devices1 = sd.query_devices(device=None,kind='input')
print("Audiodevices:")
print(devices)
print()
print("Selected microphone:")
print(" ", devices1["index"], devices1["name"])
print()
print("File name:")
print(" ", args.output)
print()
fs = 44100 # Sample rate
seconds = 10 # Duration of recording
sd.default.samplerate = fs
sd.default.channels = 1
#if args.device:
# sd.default.device = args.device
with ignore_stderr():
myrecording = sd.rec(int(seconds * fs))
sd.wait() # Wait until recording is finished
write(args.output, fs, myrecording) # Save as WAV file