15 from threading
import Thread
18 from pvrecorder
import PvRecorder
23 Microphone Demo for Rhino Speech-to-Intent engine. It creates an input audio stream from a microphone, monitors
24 it, and extracts the intent from the speech command. It optionally saves the recorded audio into a file for further
28 def __init__(self, access_key, library_path, model_path, context_path, require_endpoint, audio_device_index=None,
33 :param access_key: AccessKey obtained from Picovoice Console (https://console.picovoice.ai/).
34 :param library_path: Absolute path to Rhino's dynamic library.
35 :param model_path: Absolute path to file containing model parameters.
36 :param context_path: Absolute path to file containing context model (file with `.rhn` extension). A context
37 represents the set of expressions (spoken commands), intents, and intent arguments (slots) within a domain of
39 :param require_endpoint If set to `False`, Rhino does not require an endpoint (chunk of silence) before
41 :param audio_device_index: Optional argument. If provided, audio is recorded from this input device. Otherwise,
42 the default audio input device is used.
43 :param output_path: If provided recorded audio will be stored in this location at the end of the run.
59 Creates an input audio stream, instantiates an instance of Rhino object, and infers the intent from spoken
68 rhino = pvrhino.create(
75 recorder = PvRecorder(device_index=self.
_audio_device_index, frame_length=rhino.frame_length)
80 wav_file.setparams((1, 2, 16000, 512,
"NONE",
"NONE"))
82 print(rhino.context_info)
85 print(f
"Using device: {recorder.selected_device}")
92 if wav_file
is not None:
93 wav_file.writeframes(struct.pack(
"h" * len(pcm), *pcm))
95 is_finalized = rhino.process(pcm)
97 inference = rhino.get_inference()
98 if inference.is_understood:
100 print(
" intent : '%s'" % inference.intent)
102 for slot, value
in inference.slots.items():
103 print(
" %s : '%s'" % (slot, value))
107 print(
"Didn't understand the command.\n")
108 except pvrhino.RhinoInvalidArgumentError
as e:
109 print(
"One or more arguments provided to Rhino is invalid: {\n" +
110 f
"\t{self._access_key=}\n" +
111 f
"\t{self._library_path=}\n" +
112 f
"\t{self._model_path=}\n" +
113 f
"\t{self._context_path=}\n" +
114 f
"\t{self._require_endpoint=}\n" +
116 print(f
"If all other arguments seem valid, ensure that '{self._access_key}' is a valid AccessKey")
118 except pvrhino.RhinoActivationError
as e:
119 print(
"AccessKey activation error")
121 except pvrhino.RhinoActivationLimitError
as e:
122 print(f
"AccessKey '{self._access_key}' has reached it's temporary device limit")
124 except pvrhino.RhinoActivationRefusedError
as e:
125 print(f
"AccessKey '{self._access_key}' refused")
127 except pvrhino.RhinoActivationThrottledError
as e:
128 print(f
"AccessKey '{self._access_key}' has been throttled")
130 except pvrhino.RhinoError
as e:
131 print(f
"Failed to initialize Rhino")
133 except KeyboardInterrupt:
134 print(
'Stopping ...')
137 if recorder
is not None:
140 if rhino
is not None:
143 if wav_file
is not None:
148 devices = PvRecorder.get_audio_devices()
150 for i
in range(len(devices)):
151 print(f
'index: {i}, device name: {devices[i]}')
155 parser = argparse.ArgumentParser()
157 parser.add_argument(
'--access_key',
158 help=
'AccessKey obtained from Picovoice Console (https://picovoice.ai/console/)',
161 parser.add_argument(
'--context_path', help=
"Absolute path to context file.", required=
True)
163 parser.add_argument(
'--library_path', help=
"Absolute path to dynamic library.", default=pvrhino.LIBRARY_PATH)
167 help=
"Absolute path to the file containing model parameters.",
168 default=pvrhino.MODEL_PATH)
172 help=
"Inference sensitivity. It should be a number within [0, 1]. A higher sensitivity value results in " +
173 "fewer misses at the cost of (potentially) increasing the erroneous inference rate.",
178 '--require_endpoint',
179 help=
"If set to `False`, Rhino does not require an endpoint (chunk of silence) before finishing inference.",
181 choices=[
'True',
'False'])
183 parser.add_argument(
'--audio_device_index', help=
'Index of input audio device.', type=int, default=-1)
185 parser.add_argument(
'--output_path', help=
'Absolute path to recorded audio for debugging.', default=
None)
187 parser.add_argument(
'--show_audio_devices', action=
'store_true')
189 args = parser.parse_args()
191 if args.require_endpoint.lower() ==
'false':
192 require_endpoint =
False
194 require_endpoint =
True
196 if args.show_audio_devices:
197 RhinoDemo.show_audio_devices()
199 if not args.context_path:
200 raise ValueError(
'Missing path to context file')
203 access_key=args.access_key,
204 library_path=args.library_path,
205 model_path=args.model_path,
206 context_path=args.context_path,
207 require_endpoint=require_endpoint,
208 audio_device_index=args.audio_device_index,
209 output_path=args.output_path).run()
212 if __name__ ==
'__main__':