-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
alexa_picovoice_trigger.py
210 lines (164 loc) · 7.58 KB
/
alexa_picovoice_trigger.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
#
# Copyright 2018-2021 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
import wave
import subprocess
from datetime import datetime
from threading import Thread
import pvporcupine
from pvrecorder import PvRecorder
class PorcupineDemo(Thread):
"""
Microphone Demo for Porcupine wake word engine. It creates an input audio stream from a microphone, monitors it, and
upon detecting the specified wake word(s) prints the detection time and wake word on console. It optionally saves
the recorded audio into a file for further debugging.
"""
def __init__(
self,
access_key,
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_path: Absolute path to the file containing model parameters.
:param keyword_paths: Absolute paths to keyword model files.
:param sensitivities: Sensitivities for detecting keywords. Each value should be a number within [0, 1]. A
higher sensitivity results in fewer misses at the cost of increasing the false alarm rate. If not set 0.5 will
be used.
: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._access_key = access_key
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
def run(self):
"""
Creates an input audio stream, instantiates an instance of Porcupine object, and monitors the audio stream for
occurrences of the wake word(s). It prints the time of detection for each occurrence and the wake word.
"""
keywords = list()
for x in self._keyword_paths:
keyword_phrase_part = os.path.basename(x).replace('.ppn', '').split('_')
if len(keyword_phrase_part) > 6:
keywords.append(' '.join(keyword_phrase_part[0:-6]))
else:
keywords.append(keyword_phrase_part[0])
porcupine = None
recorder = None
wav_file = None
try:
porcupine = pvporcupine.create(
access_key=self._access_key,
library_path=self._library_path,
model_path=self._model_path,
keyword_paths=self._keyword_paths,
sensitivities=self._sensitivities)
recorder = PvRecorder(device_index=self._input_device_index, frame_length=porcupine.frame_length)
recorder.start()
if self._output_path is not None:
wav_file = wave.open(self._output_path, "w")
wav_file.setparams((1, 2, 16000, 512, "NONE", "NONE"))
print(f'Using device: {recorder.selected_device}')
print('Listening {')
for keyword, sensitivity in zip(keywords, self._sensitivities):
print(' %s (%.2f)' % (keyword, sensitivity))
print('}')
while True:
pcm = recorder.read()
if wav_file is not None:
wav_file.writeframes(struct.pack("h" * len(pcm), *pcm))
result = porcupine.process(pcm)
if result >= 0:
print('[%s] Detected %s' % (str(datetime.now()), keywords[result]))
subprocess.call("wmctrl -i -a 0x01a00003 && xdotool keydown t && xdotool keyup t && xdotool keydown Return && xdotool keyup Return",shell=True)
except KeyboardInterrupt:
print('Stopping ...')
finally:
if porcupine is not None:
porcupine.delete()
if recorder is not None:
recorder.delete()
if wav_file is not None:
wav_file.close()
@classmethod
def show_audio_devices(cls):
devices = PvRecorder.get_audio_devices()
for i in range(len(devices)):
print(f'index: {i}, device name: {devices[i]}')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--access_key',
help='AccessKey obtained from Picovoice Console (https://picovoice.ai/console/)')
parser.add_argument(
'--keywords',
nargs='+',
help='List of default keywords for detection. Available keywords: %s' % ', '.join(sorted(pvporcupine.KEYWORDS)),
choices=sorted(pvporcupine.KEYWORDS),
metavar='')
parser.add_argument(
'--keyword_paths',
nargs='+',
help="Absolute paths to keyword model files. If not set it will be populated from `--keywords` argument")
parser.add_argument('--library_path', help='Absolute path to dynamic library.', default=pvporcupine.LIBRARY_PATH)
parser.add_argument(
'--model_path',
help='Absolute path to the file containing model parameters.',
default=pvporcupine.MODEL_PATH)
parser.add_argument(
'--sensitivities',
nargs='+',
help="Sensitivities for detecting keywords. Each value should be a number within [0, 1]. A higher " +
"sensitivity results in fewer misses at the cost of increasing the false alarm rate. If not set 0.5 " +
"will be used.",
type=float,
default=None)
parser.add_argument('--audio_device_index', help='Index of input audio device.', type=int, default=-1)
parser.add_argument('--output_path', help='Absolute path to recorded audio for debugging.', default=None)
parser.add_argument('--show_audio_devices', action='store_true')
args = parser.parse_args()
if args.show_audio_devices:
PorcupineDemo.show_audio_devices()
else:
if args.access_key is None:
raise ValueError("AccessKey (--access_key) is required")
if args.keyword_paths is None:
if args.keywords is None:
raise ValueError("Either `--keywords` or `--keyword_paths` must be set.")
keyword_paths = [pvporcupine.KEYWORD_PATHS[x] for x in args.keywords]
else:
keyword_paths = args.keyword_paths
if args.sensitivities is None:
args.sensitivities = [0.5] * len(keyword_paths)
if len(keyword_paths) != len(args.sensitivities):
raise ValueError('Number of keywords does not match the number of sensitivities.')
PorcupineDemo(
access_key=args.access_key,
library_path=args.library_path,
model_path=args.model_path,
keyword_paths=keyword_paths,
sensitivities=args.sensitivities,
output_path=args.output_path,
input_device_index=args.audio_device_index).run()
if __name__ == '__main__':
main()