-
Notifications
You must be signed in to change notification settings - Fork 2
/
echo.py
executable file
·156 lines (112 loc) · 3.85 KB
/
echo.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import threading
import sys
if sys.version_info[0] < 3:
import Queue as queue
else:
import queue
import numpy as np
import audioop
import pyaudio
from voice_engine.element import Element
from voice_engine.file_sink import FileSink
from kws import KWS
from player import Player
class Source(Element):
def __init__(self, rate=16000, frames_size=None):
super(Source, self).__init__()
self.rate = rate
self.frames_size = frames_size if frames_size else rate / 100
self.channels = 6
self.pyaudio_instance = pyaudio.PyAudio()
device_index = None
for i in range(self.pyaudio_instance.get_device_count()):
dev = self.pyaudio_instance.get_device_info_by_index(i)
name = dev['name'].encode('utf-8')
print('{}:{} with {} input channels'.format(i, name, dev['maxInputChannels']))
if name.find('ReSpeaker 4 Mic Array') >= 0 and dev['maxInputChannels'] == self.channels:
device_index = i
break
if device_index is None:
raise ValueError('Can not find an input device with {} channel(s)'.format(self.channels))
self.stream = self.pyaudio_instance.open(
start=False,
format=pyaudio.paInt16,
input_device_index=device_index,
channels=self.channels,
rate=int(self.rate),
frames_per_buffer=int(self.frames_size),
stream_callback=self._callback,
input=True
)
def _callback(self, in_data, frame_count, time_info, status):
super(Source, self).put(in_data)
return None, pyaudio.paContinue
def start(self):
self.stream.start_stream()
def stop(self):
self.stream.stop_stream()
class Route(Element):
def __init__(self):
super(Route, self).__init__()
self.channels = 6
self.detect_mask = 0
self.kws_list = []
for ch in range(self.channels):
def callback_gen(channel):
def on_detected(keyword):
self.detect_mask |= 1 << channel
print('channel {} detected'.format(channel))
return on_detected
kws = KWS()
self.kws_list.append(kws)
kws.on_detected = callback_gen(ch)
kws.start()
self.queue = queue.Queue()
self.done = True
def put(self, data):
self.queue.put(data)
def start(self):
self.done = False
thread = threading.Thread(target=self.run)
thread.daemon = True
thread.start()
def stop(self):
self.done = True
for ch in range(self.channels):
self.kws_list[ch].stop()
def on_data(self, data):
pass
def run(self):
while not self.done:
data = self.queue.get()
data = np.fromstring(data, dtype='int16')
rms_data = []
rms_data_db = []
for ch in range(self.channels):
mono = data[ch::self.channels]
self.kws_list[ch].put(mono.tostring())
def main():
import time
import datetime
src = Source(frames_size=1600)
route = Route()
player = Player(pyaudio_instance=src.pyaudio_instance)
# filename = '1.quiet.' + datetime.datetime.now().strftime("%Y%m%d.%H:%M:%S") + '.wav'
# sink = FileSink(filename, channels=src.channels, rate=src.rate)
src.pipeline(route)
# src.link(sink)
src.pipeline_start()
time.sleep(1)
player.play('respeaker.wav')
time.sleep(1)
player.play('respeaker.wav')
for _ in range(10):
time.sleep(1)
if route.detect_mask == 0b111111:
print('all channels detected')
break
src.pipeline_stop()
if route.detect_mask != 0b111111:
print('Not all channels detected')
if __name__ == '__main__':
main()