-
Notifications
You must be signed in to change notification settings - Fork 86
/
vad_doa.py
executable file
·52 lines (38 loc) · 1.34 KB
/
vad_doa.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
import sys
import webrtcvad
import numpy as np
from mic_array import MicArray
from pixel_ring import pixel_ring
RATE = 16000
CHANNELS = 4
VAD_FRAMES = 10 # ms
DOA_FRAMES = 200 # ms
def main():
vad = webrtcvad.Vad(3)
speech_count = 0
chunks = []
doa_chunks = int(DOA_FRAMES / VAD_FRAMES)
try:
with MicArray(RATE, CHANNELS, RATE * VAD_FRAMES / 1000) as mic:
for chunk in mic.read_chunks():
# Use single channel audio to detect voice activity
if vad.is_speech(chunk[0::CHANNELS].tobytes(), RATE):
speech_count += 1
sys.stdout.write('1')
else:
sys.stdout.write('0')
sys.stdout.flush()
chunks.append(chunk)
if len(chunks) == doa_chunks:
if speech_count > (doa_chunks / 2):
frames = np.concatenate(chunks)
direction = mic.get_direction(frames)
pixel_ring.set_direction(direction)
print('\n{}'.format(int(direction)))
speech_count = 0
chunks = []
except KeyboardInterrupt:
pass
pixel_ring.off()
if __name__ == '__main__':
main()