-
Notifications
You must be signed in to change notification settings - Fork 0
/
recording.py
84 lines (71 loc) · 2.3 KB
/
recording.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Standard libraries
import time
import wave
import audioop
# PyPi packages
import alsaaudio as alsa
# Local packages
import transcription
filename = '/tmp/homebrain-stt-recording-temp.wav'
volume_threshold = 13000
# Records audio to a file until two seconds of 'silence' have passed
# Silence is defined as any audio below volume_threshold
def record_audio(pcm_in, data, silencetime=2):
print('Recording. {} seconds of silence will commit the command.'.format(silencetime))
buf = open(filename, 'w+b')
time_start = time.time()
vol = volume_threshold + 1
while ((time.time() - time_start) < silencetime) or (vol > volume_threshold):
if vol > volume_threshold:
time_start = time.time()
buf.write(data)
l, data = pcm_in.read()
vol = audioop.max(data, 2)
time.sleep(.001)
buf.close()
def get_pcm():
# Prepare the CAPTURE device. It must use 16k Hz,
# little endian, 16 bit signed integer
pcm_in = alsa.PCM(alsa.PCM_CAPTURE, alsa.PCM_NONBLOCK, 'default')
pcm_in.setchannels(1)
pcm_in.setrate(16000)
pcm_in.setformat(alsa.PCM_FORMAT_S16_LE)
# Size of block of each read
pcm_in.setperiodsize(512)
return pcm_in
def passive_listen():
result = listen("passive")
if result == "home brain":
return True
else:
return False
def active_listen():
return listen("active")
def listen(mode):
pcm_in = get_pcm()
silencetime = 0
transcriber = None
if mode == "passive":
silencetime = 1
transcriber=transcription.passive_transcribe
timeout = float("inf")
elif mode == "active":
silencetime = 2
transcriber=transcription.active_transcribe
timeout = 10
else:
print("Invalid listen mode, must be either passive or active")
return
print("Waiting for {} input...".format(mode))
start_time = time.time()
while start_time+timeout > time.time():
l, data = pcm_in.read()
vol = audioop.max(data, 2)
# enter recording state
if vol > volume_threshold:
record_audio(pcm_in, data, silencetime)
transcribed = transcriber(filename)
if transcribed and any(transcribed.hypstr):
return transcribed.hypstr
else:
return None