-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathmic.js
More file actions
executable file
·181 lines (160 loc) · 5.44 KB
/
mic.js
File metadata and controls
executable file
·181 lines (160 loc) · 5.44 KB
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
#! /usr/bin/env node
//
// Copyright 2020-2025 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.
//
"use strict";
const fs = require("fs");
const { program } = require("commander");
const { Rhino } = require("@picovoice/rhino-node");
const { RhinoInvalidArgumentError } = require("@picovoice/rhino-node/dist/errors");
const { PvRecorder } = require("@picovoice/pvrecorder-node");
program
.option(
"-a, --access_key <string>",
"AccessKey obtain from the Picovoice Console (https://console.picovoice.ai/)"
)
.option(
"-c, --context_path <string>",
`absolute path to rhino context (.rhn extension)`
)
.option(
"-l, --library_file_path <string>",
"absolute path to rhino dynamic library"
)
.option("-m, --model_file_path <string>", "absolute path to rhino model")
.option(
"-y, --device <string>",
"Device to run inference on (`best`, `cpu:{num_threads}`, `gpu:{gpu_index}`). Default: selects best device for `pvrhino`")
.option(
"-s, --sensitivity <number>",
"sensitivity value between 0 and 1",
parseFloat,
0.5
).option(
"-i, --audio_device_index <number>",
"index of audio device to use to record audio",
Number,
-1
).option(
"-d, --endpoint_duration_sec <bool>",
"Endpoint duration in seconds. " +
"An endpoint is a chunk of silence at the end of an utterance that marks the end of spoken command. " +
"It should be a positive number within [0.5, 5]. " +
"A lower endpoint duration reduces delay and improves responsiveness. " +
"A higher endpoint duration assures Rhino doesn't return inference preemptively " +
"in case the user pauses before finishing the request." ,
parseFloat,
1.0
).option(
"-e, --requires_endpoint <bool>",
"If set to `false`, Rhino does not require an endpoint (chunk of silence) before finishing inference.",
"true"
).option(
"-sd, --show_audio_devices",
"show the list of available devices"
).option(
"-sy, --show_inference_devices",
"Print the list of devices available to run Rhino inference.",
false);
if (process.argv.length < 3) {
program.help();
}
program.parse(process.argv);
let isInterrupted = false;
async function micDemo() {
let accessKey = program["access_key"]
let contextPath = program["context_path"];
let libraryFilePath = program["library_file_path"];
let modelFilePath = program["model_file_path"];
let device = program["device"];
let sensitivity = program["sensitivity"];
let audioDeviceIndex = program["audio_device_index"];
let endpointDurationSec = program["endpoint_duration_sec"];
let requiresEndpoint = program["requires_endpoint"].toLowerCase() !== 'false';
let showAudioDevices = program["show_audio_devices"];
let showInferenceDevices = program["show_inference_devices"];
if (showInferenceDevices) {
console.log(Rhino.listAvailableDevices().join('\n'));
process.exit();
}
if (showAudioDevices) {
const devices = PvRecorder.getAvailableDevices();
for (let i = 0; i < devices.length; i++) {
console.log(`index: ${i}, device name: ${devices[i]}`);
}
process.exit();
}
if (accessKey === undefined) {
console.error(
"`--access_key` is a required argument"
);
return;
}
if (isNaN(sensitivity) || sensitivity < 0 || sensitivity > 1) {
console.error("--sensitivity must be a number in the range [0,1]");
return;
}
if (isNaN(endpointDurationSec) || endpointDurationSec < 0.5 || endpointDurationSec > 5.0) {
console.error("--endpointDurationSec must be a number in the range [0.5, 5.0]");
return;
}
if (!fs.existsSync(contextPath)) {
throw new RhinoInvalidArgumentError(
`File not found at 'contextPath': ${contextPath}`
);
}
let contextName = contextPath
.split(/[\\|\/]/)
.pop()
.split("_")[0];
let handle = new Rhino(
accessKey,
contextPath,
{
modelPath: modelFilePath,
device: device,
sensitivity: sensitivity,
endpointDurationSec: endpointDurationSec,
requiresEndpoint: requiresEndpoint,
libraryPath: libraryFilePath
}
);
const frameLength = handle.frameLength;
const recorder = new PvRecorder(frameLength, audioDeviceIndex);
recorder.start();
console.log(`Using device: ${recorder.getSelectedDevice()}`);
console.log("Context info:");
console.log("-------------");
console.log(handle.getContextInfo());
console.log(
`Listening for speech within the context of '${contextName}'. Please speak your phrase into the microphone. `
);
console.log("Press ctrl+c to exit.")
while (!isInterrupted) {
const pcm = await recorder.read();
const isFinalized = handle.process(pcm);
if (isFinalized === true) {
let inference = handle.getInference();
console.log("Inference result:");
console.log(JSON.stringify(inference, null, 4));
console.log();
}
}
console.log("Stopping...");
recorder.stop();
recorder.release();
}
(async function () {
try {
await micDemo();
} catch (e) {
console.error(e.toString());
}
})();