-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmic_streaming.py
272 lines (220 loc) · 10.2 KB
/
mic_streaming.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#
# Copyright 2018 Picovoice Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from halo import Halo
import time, logging
import argparse
import os
import glob
import struct
import sys
from datetime import datetime
from threading import Thread
import deepspeech
from audio_tools import VADAudio
import numpy as np
import pyaudio
import soundfile
#sys.path.append(os.path.join(os.path.dirname(__file__), 'binding/python'))
#sys.path.append(os.path.join(os.path.dirname(__file__), 'resources/util/python'))
from pvporcupine import * #Porcupine
#from util import *
logging.basicConfig(level=logging.INFO)
class PorcupineDemo(Thread):
"""
Demo class for wake word detection (aka Porcupine) library. It creates an input audio stream from a microphone,
monitors it, and upon detecting the specified wake word(s) prints the detection time and index of wake word on
console. It optionally saves the recorded audio into a file for further review.
"""
def __init__(
self,
library_path,
model_path,
keyword_paths,
sensitivities,
input_device_index=None,
output_path=None):
"""
Constructor.
:param library_path: Absolute path to Porcupine's dynamic library.
:param model_file_path: Absolute path to the model parameter file.
:param keyword_file_paths: List of absolute paths to keyword files.
:param sensitivities: Sensitivity parameter for each wake word. For more information refer to
'include/pv_porcupine.h'. It uses the
same sensitivity value for all keywords.
:param input_device_index: Optional argument. If provided, audio is recorded from this input device. Otherwise,
the default audio input device is used.
:param output_path: If provided recorded audio will be stored in this location at the end of the run.
"""
super(PorcupineDemo, self).__init__()
self._library_path = library_path
self._model_path = model_path
self._keyword_paths = keyword_paths
self._sensitivities = sensitivities
self._input_device_index = input_device_index
self._output_path = output_path
if self._output_path is not None:
self._recorded_frames = []
#Load DeepSpeech model
print('Initializing model...')
dirname = os.path.dirname(os.path.abspath(__file__))
model_name = glob.glob(os.path.join(dirname,'*.tflite'))[0]
logging.info("Model: %s", model_name)
self.model = deepspeech.Model(model_name)
try:
scorer_name = glob.glob(os.path.join(dirname,'*.scorer'))[0]
logging.info("Language model: %s", scorer_name)
self.model.enableExternalScorer(scorer_name)
except Exception as e:
pass
def transcribe(self):
# Start audio with VAD
vad_audio = VADAudio(aggressiveness=1,
device=None,
input_rate=16000,
file=None)
print("Listening (ctrl-C to exit)...")
frames = vad_audio.vad_collector()
# Stream from microphone to DeepSpeech using VAD
spinner = Halo(spinner='line')
stream_context = self.model.createStream()
#wav_data = bytearray()
for frame in frames:
if frame is not None:
if spinner: spinner.start()
logging.debug("streaming frame")
stream_context.feedAudioContent(np.frombuffer(frame, np.int16))
#if ARGS.savewav: wav_data.extend(frame)
else:
if spinner: spinner.stop()
logging.debug("end utterence")
#if ARGS.savewav:
# vad_audio.write_wav(os.path.join(ARGS.savewav, datetime.now().strftime("savewav_%Y-%m-%d_%H-%M-%S_%f.wav")), wav_data)
# wav_data = bytearray()
text = stream_context.finishStream()
print("Recognized: %s" % text)
if 'stop recording' in text:
vad_audio.destroy()
#break
return 1
stream_context = self.model.createStream()
def run(self):
"""
Creates an input audio stream, initializes wake word detection (Porcupine) object, and monitors the audio
stream for occurrences of the wake word(s). It prints the time of detection for each occurrence and index of
wake word.
"""
num_keywords = len(self._keyword_paths)
keywords = list()
for x in self._keyword_paths:
keywords.append(os.path.basename(x).replace('.ppn', '').replace('_compressed', '').split('_')[0])
print('listening for:')
for keyword, sensitivity in zip(keywords, self._sensitivities):
print('- %s (sensitivity: %f)' % (keyword, sensitivity))
porcupine = None
pa = None
audio_stream = None
try:
porcupine = Porcupine(
library_path=self._library_path,
model_path=self._model_path,
keyword_paths=self._keyword_paths,
sensitivities=self._sensitivities)
pa = pyaudio.PyAudio()
audio_stream = pa.open(
rate=porcupine.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=porcupine.frame_length,
input_device_index=self._input_device_index)
while True:
pcm = audio_stream.read(porcupine.frame_length)
pcm = struct.unpack_from("h" * porcupine.frame_length, pcm)
if self._output_path is not None:
self._recorded_frames.append(pcm)
result = porcupine.process(pcm)
if result >= 0:
print('[%s] Detected %s' % (str(datetime.now()), keywords[result]))
audio_stream.close()
if self.transcribe():
audio_stream = pa.open(
rate=porcupine.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=porcupine.frame_length,
input_device_index=self._input_device_index)
except KeyboardInterrupt:
print('stopping ...')
finally:
if porcupine is not None:
porcupine.delete()
if audio_stream is not None:
audio_stream.close()
if pa is not None:
pa.terminate()
if self._output_path is not None and len(self._recorded_frames) > 0:
recorded_audio = np.concatenate(self._recorded_frames, axis=0).astype(np.int16)
soundfile.write(self._output_path, recorded_audio, samplerate=porcupine.sample_rate, subtype='PCM_16')
_AUDIO_DEVICE_INFO_KEYS = ['index', 'name', 'defaultSampleRate', 'maxInputChannels']
@classmethod
def show_audio_devices_info(cls):
""" Provides information regarding different audio devices available. """
pa = pyaudio.PyAudio()
for i in range(pa.get_device_count()):
info = pa.get_device_info_by_index(i)
print(', '.join("'%s': '%s'" % (k, str(info[k])) for k in cls._AUDIO_DEVICE_INFO_KEYS))
pa.terminate()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--keywords', help='comma-separated list of default keywords (%s)' % ', '.join(sorted(KEYWORDS)))
parser.add_argument('--keyword_paths', help='comma-separated absolute paths to keyword files')
parser.add_argument('--library_path', help="absolute path to Porcupine's dynamic library", default=LIBRARY_PATH)
parser.add_argument('--model_path', help='absolute path to model parameter file', default=MODEL_PATH)
parser.add_argument('--sensitivities', help='detection sensitivity [0, 1]', default=0.5)
parser.add_argument('--input_audio_device_index', help='index of input audio device', type=int, default=None)
parser.add_argument(
'--output_path',
help='absolute path to where recorded audio will be stored. If not set, it will be bypassed.')
parser.add_argument('--show_audio_devices_info', action='store_true')
args = parser.parse_args()
if args.show_audio_devices_info:
PorcupineDemo.show_audio_devices_info()
else:
if args.keyword_paths is None:
if args.keywords is None:
raise ValueError('either --keywords or --keyword_file_paths must be set')
keywords = [x.strip() for x in args.keywords.split(',')]
if all(x in KEYWORDS for x in keywords):
keyword_paths = [KEYWORD_PATHS[x] for x in keywords]
else:
raise ValueError(
'selected keywords are not available by default. available keywords are: %s' % ', '.join(KEYWORDS))
else:
keyword_paths = [x.strip() for x in args.keyword_paths.split(',')]
if isinstance(args.sensitivities, float):
sensitivities = [args.sensitivities] * len(keyword_paths)
else:
sensitivities = [float(x) for x in args.sensitivities.split(',')]
PorcupineDemo(
library_path=args.library_path,
model_path=args.model_path,
keyword_paths=keyword_paths,
sensitivities=sensitivities,
output_path=args.output_path,
input_device_index=args.input_audio_device_index).run()
if __name__ == '__main__':
main()