forked from Picovoice/porcupine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
porcupine_demo.py
212 lines (164 loc) · 7.89 KB
/
porcupine_demo.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
#
# 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.
#
import argparse
import os
import struct
import sys
from datetime import datetime
from threading import Thread
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 porcupine import Porcupine
from util import *
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_file_path,
keyword_file_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_file_path = model_file_path
self._keyword_file_paths = keyword_file_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 = []
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_file_paths)
keyword_names = list()
for x in self._keyword_file_paths:
keyword_names.append(os.path.basename(x).replace('.ppn', '').replace('_compressed', '').split('_')[0])
print('listening for:')
for keyword_name, sensitivity in zip(keyword_names, self._sensitivities):
print('- %s (sensitivity: %f)' % (keyword_name, sensitivity))
porcupine = None
pa = None
audio_stream = None
try:
porcupine = Porcupine(
library_path=self._library_path,
model_file_path=self._model_file_path,
keyword_file_paths=self._keyword_file_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 num_keywords == 1 and result:
print('[%s] detected keyword' % str(datetime.now()))
elif num_keywords > 1 and result >= 0:
print('[%s] detected %s' % (str(datetime.now()), keyword_names[result]))
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(KEYWORDS))
parser.add_argument('--keyword_file_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_file_path', help='absolute path to model parameter file', default=MODEL_FILE_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_file_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_file_paths = [KEYWORD_FILE_PATHS[x] for x in keywords]
else:
raise ValueError(
'selected keywords are not available by default. available keywords are: %s' % ', '.join(KEYWORDS))
else:
keyword_file_paths = [x.strip() for x in args.keyword_file_paths.split(',')]
if isinstance(args.sensitivities, float):
sensitivities = [args.sensitivities] * len(keyword_file_paths)
else:
sensitivities = [float(x) for x in args.sensitivities.split(',')]
PorcupineDemo(
library_path=args.library_path,
model_file_path=args.model_file_path,
keyword_file_paths=keyword_file_paths,
sensitivities=sensitivities,
output_path=args.output_path,
input_device_index=args.input_audio_device_index).run()
if __name__ == '__main__':
main()