20 wav_file = wave.open(file_name, mode=
"rb")
21 channels = wav_file.getnchannels()
22 num_frames = wav_file.getnframes()
24 if wav_file.getframerate() != sample_rate:
25 raise ValueError(
"Audio file should have a sample rate of %d. got %d" % (sample_rate, wav_file.getframerate()))
27 samples = wav_file.readframes(num_frames)
30 frames = struct.unpack(
'h' * num_frames * channels, samples)
33 print(
"Picovoice processes single-channel audio but stereo file is provided. Processing left channel only.")
35 return frames[::channels]
39 parser = argparse.ArgumentParser()
41 parser.add_argument(
'--input_audio_path', help=
'Absolute path to input audio file.',
44 parser.add_argument(
'--access_key',
45 help=
'AccessKey obtained from Picovoice Console (https://picovoice.ai/console/)',
48 parser.add_argument(
'--context_path', help=
"Absolute path to context file.", required=
True)
50 parser.add_argument(
'--library_path', help=
'Absolute path to dynamic library.', default=pvrhino.LIBRARY_PATH)
54 help=
'Absolute path to the file containing model parameters.',
55 default=pvrhino.MODEL_PATH)
59 help=
"Inference sensitivity. It should be a number within [0, 1]. A higher sensitivity value results in " +
60 "fewer misses at the cost of (potentially) increasing the erroneous inference rate.",
66 help=
"If set to `False`, Rhino does not require an endpoint (chunk of silence) before finishing inference.",
68 choices=[
'True',
'False'])
70 args = parser.parse_args()
72 if args.require_endpoint.lower() ==
'false':
73 require_endpoint =
False
75 require_endpoint =
True
78 rhino = pvrhino.create(
79 access_key=args.access_key,
80 library_path=args.library_path,
81 model_path=args.model_path,
82 context_path=args.context_path,
83 sensitivity=args.sensitivity,
84 require_endpoint=require_endpoint)
85 except pvrhino.RhinoInvalidArgumentError
as e:
86 print(f
"One or more arguments provided to Rhino is invalid: {args}")
87 print(f
"If all other arguments seem valid, ensure that '{args.access_key}' is a valid AccessKey")
89 except pvrhino.RhinoActivationError
as e:
90 print(
"AccessKey activation error")
92 except pvrhino.RhinoActivationLimitError
as e:
93 print(f
"AccessKey '{args.access_key}' has reached it's temporary device limit")
95 except pvrhino.RhinoActivationRefusedError
as e:
96 print(f
"AccessKey '{args.access_key}' refused")
98 except pvrhino.RhinoActivationThrottledError
as e:
99 print(f
"AccessKey '{args.access_key}' has been throttled")
101 except pvrhino.RhinoError
as e:
102 print(f
"Failed to initialize Rhino")
105 audio =
read_file(args.input_audio_path, rhino.sample_rate)
107 num_frames = len(audio) // rhino.frame_length
108 for i
in range(num_frames):
109 frame = audio[i * rhino.frame_length:(i + 1) * rhino.frame_length]
110 is_finalized = rhino.process(frame)
112 inference = rhino.get_inference()
113 if inference.is_understood:
115 print(
" intent : '%s'" % inference.intent)
117 for slot, value
in inference.slots.items():
118 print(
" %s : '%s'" % (slot, value))
122 print(
"Didn't understand the command.")
128 if __name__ ==
'__main__':