forked from Picovoice/porcupine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathporcupine_demo_mic.py
158 lines (128 loc) · 5.33 KB
/
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
#
# Copyright 2018-2023 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
from datetime import datetime
import pvporcupine
from pvrecorder import PvRecorder
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--access_key',
help='AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)')
parser.add_argument(
'--keywords',
nargs='+',
help='List of default keywords for detection. Available keywords: %s' % ', '.join(
'%s' % w for w in 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: using the library provided by `pvporcupine`')
parser.add_argument(
'--model_path',
help='Absolute path to the file containing model parameters. '
'Default: using the library provided by `pvporcupine`')
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:
for i, device in enumerate(PvRecorder.get_available_devices()):
print('Device %d: %s' % (i, device))
return
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.')
try:
porcupine = pvporcupine.create(
access_key=args.access_key,
library_path=args.library_path,
model_path=args.model_path,
keyword_paths=keyword_paths,
sensitivities=args.sensitivities)
except pvporcupine.PorcupineInvalidArgumentError as e:
print("One or more arguments provided to Porcupine is invalid: ", args)
print(e)
raise e
except pvporcupine.PorcupineActivationError as e:
print("AccessKey activation error")
raise e
except pvporcupine.PorcupineActivationLimitError as e:
print("AccessKey '%s' has reached it's temporary device limit" % args.access_key)
raise e
except pvporcupine.PorcupineActivationRefusedError as e:
print("AccessKey '%s' refused" % args.access_key)
raise e
except pvporcupine.PorcupineActivationThrottledError as e:
print("AccessKey '%s' has been throttled" % args.access_key)
raise e
except pvporcupine.PorcupineError as e:
print("Failed to initialize Porcupine")
raise e
keywords = list()
for x in 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])
print('Porcupine version: %s' % porcupine.version)
recorder = PvRecorder(
frame_length=porcupine.frame_length,
device_index=args.audio_device_index)
recorder.start()
wav_file = None
if args.output_path is not None:
wav_file = wave.open(args.output_path, "w")
wav_file.setnchannels(1)
wav_file.setsampwidth(2)
wav_file.setframerate(16000)
print('Listening ... (press Ctrl+C to exit)')
try:
while True:
pcm = recorder.read()
result = porcupine.process(pcm)
if wav_file is not None:
wav_file.writeframes(struct.pack("h" * len(pcm), *pcm))
if result >= 0:
print('[%s] Detected %s' % (str(datetime.now()), keywords[result]))
except KeyboardInterrupt:
print('Stopping ...')
finally:
recorder.delete()
porcupine.delete()
if wav_file is not None:
wav_file.close()
if __name__ == '__main__':
main()