forked from Picovoice/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhino_porcupine_demo_mic.py
250 lines (199 loc) · 9.12 KB
/
rhino_porcupine_demo_mic.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
#
# Copyright 2018-2020 Picovoice Inc.
#
# You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
# file accompanying this source.
#
# 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.
#
import argparse
import os
import struct
from threading import Thread
import numpy as np
import pvporcupine
import pvrhino
import pyaudio
import soundfile
class RhinoDemo(Thread):
"""
Demo class for Speech-to-Intent (aka Rhino) library. It creates an input audio stream from a microphone, monitors
it, and upon detecting the specified wake phrase extracts the intent from the speech command that follows. Wake word
detection is done using Porcupine's wake word detection engine (https://github.com/Picovoice/Porcupine).
"""
def __init__(
self,
rhino_library_path,
rhino_model_file_path,
rhino_context_file_path,
porcupine_library_path,
porcupine_model_file_path,
porcupine_keyword_file_path,
input_device_index=None,
output_path=None):
"""
Constructor.
:param rhino_library_path: Absolute path to Rhino's dynamic library.
:param rhino_model_file_path: Absolute path to Rhino's model parameter file.
:param rhino_context_file_path: Absolute path to Rhino's context file that defines the context of commands.
:param porcupine_library_path: Absolute path to Porcupine's dynamic library.
:param porcupine_model_file_path: Absolute path to Porcupine's model parameter file.
:param porcupine_keyword_file_path: Absolute path to Porcupine's keyword file for wake phrase.
: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(RhinoDemo, self).__init__()
self._rhino_library_path = rhino_library_path
self._rhino_model_file_path = rhino_model_file_path
self._rhino_context_file_path = rhino_context_file_path
self._porcupine_library_path = porcupine_library_path
self._porcupine_model_file_path = porcupine_model_file_path
self._porcupine_keyword_file_path = porcupine_keyword_file_path
self._input_device_index = input_device_index
self._output_path = output_path
if self._output_path is not None:
self._recorded_frames = list()
def run(self):
"""
Creates an input audio stream, initializes wake word detection (Porcupine) and speech to intent (Rhino)
engines, and monitors the audio stream for occurrences of the wake word and then infers the intent from speech
command that follows.
"""
porcupine = None
rhino = None
pa = None
audio_stream = None
wake_phrase_detected = False
intent_extraction_is_finalized = False
try:
porcupine = pvporcupine.create(
library_path=self._porcupine_library_path,
model_path=self._porcupine_model_file_path,
keyword_paths=[self._porcupine_keyword_file_path],
sensitivities=[0.5])
rhino = pvrhino.create(
library_path=self._rhino_library_path,
model_path=self._rhino_model_file_path,
context_path=self._rhino_context_file_path)
print()
print('****************************** context ******************************')
print(rhino.context_info)
print('*********************************************************************')
print()
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)
# NOTE: This is true now and will be correct possibly forever. If it changes the logic below need to change.
assert porcupine.frame_length == rhino.frame_length
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)
if not wake_phrase_detected:
keyword_index = porcupine.process(pcm)
wake_phrase_detected = keyword_index == 0
if wake_phrase_detected:
print('detected wake phrase')
elif not intent_extraction_is_finalized:
intent_extraction_is_finalized = rhino.process(pcm)
else:
inference = rhino.get_inference()
if inference.is_understood:
print()
print('intent: %s' % inference.intent)
print('---')
for slot, value in inference.slots.items():
print('%s: %s' % (slot, value))
print()
else:
print("didn't understand the command")
wake_phrase_detected = False
intent_extraction_is_finalized = False
except KeyboardInterrupt:
print('stopping ...')
finally:
if porcupine is not None:
porcupine.delete()
if rhino is not None:
rhino.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(
os.path.expanduser(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(
'--rhino_library_path',
default=pvrhino.LIBRARY_PATH,
help="absolute path to Rhino's dynamic library")
parser.add_argument(
'--rhino_model_file_path',
default=pvrhino.MODEL_PATH,
help="absolute path to Rhino's model file path")
parser.add_argument(
'--rhino_context_file_path',
help="absolute path to Rhino's context file")
parser.add_argument(
'--porcupine_library_path',
default=pvporcupine.LIBRARY_PATH,
help="absolute path to Porcupine's dynamic library")
parser.add_argument(
'--porcupine_model_file_path',
default=pvporcupine.MODEL_PATH,
help="absolute path to Porcupine's model parameter file")
parser.add_argument(
'--porcupine_keyword_file_path',
default=pvporcupine.KEYWORD_PATHS['picovoice'],
help='absolute path to porcupine keyword file')
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.',
default=None)
parser.add_argument('--show_audio_devices_info', action='store_true')
args = parser.parse_args()
if args.show_audio_devices_info:
RhinoDemo.show_audio_devices_info()
else:
if not args.rhino_context_file_path:
raise ValueError('missing rhino_context_file_path')
RhinoDemo(
rhino_library_path=args.rhino_library_path,
rhino_model_file_path=args.rhino_model_file_path,
rhino_context_file_path=args.rhino_context_file_path,
porcupine_library_path=args.porcupine_library_path,
porcupine_model_file_path=args.porcupine_model_file_path,
porcupine_keyword_file_path=args.porcupine_keyword_file_path,
input_device_index=args.input_audio_device_index,
output_path=args.output_path).run()
if __name__ == '__main__':
main()