-
Notifications
You must be signed in to change notification settings - Fork 501
/
file.js
executable file
·187 lines (165 loc) · 5.3 KB
/
file.js
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
#! /usr/bin/env node
//
// Copyright 2020-2022 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 { program } = require("commander");
const fs = require("fs");
const WaveFile = require("wavefile").WaveFile;
const {
Porcupine,
BuiltinKeyword,
getBuiltinKeywordPath,
getInt16Frames,
checkWaveFile,
} = require("@picovoice/porcupine-node");
program
.requiredOption(
"-i, --input_audio_file_path <string>",
"input audio wave file in 16-bit 16KHz linear PCM format (mono)"
)
.requiredOption(
"-a, --access_key <string>",
"AccessKey obtain from the Picovoice Console (https://console.picovoice.ai/)"
)
.option(
"-k, --keyword_file_paths <string>",
"absolute path(s) to porcupine keyword files (.ppn)"
)
.option(
"-b, --keywords <string>",
`built in keyword(s) (${Object.keys(BuiltinKeyword)})`
)
.option(
"-l, --library_file_path <string>",
"absolute path to porcupine dynamic library"
)
.option("-m, --model_file_path <string>", "absolute path to porcupine model")
.option(
"-s, --sensitivity <number>",
"sensitivity value between 0 and 1",
parseFloat,
0.5
);
if (process.argv.length < 3) {
program.help();
}
program.parse(process.argv);
function frameIndexToSeconds(frameIndex, engineInstance) {
return (frameIndex * engineInstance.frameLength) / engineInstance.sampleRate;
}
function fileDemo() {
let audioPath = program["input_audio_file_path"];
let accessKey = program["access_key"]
let keywordPaths = program["keyword_file_paths"];
let keywords = program["keywords"];
let libraryFilePath = program["library_file_path"];
let modelFilePath = program["model_file_path"];
let sensitivity = program["sensitivity"];
let keywordPathsDefined = keywordPaths !== undefined;
let builtinKeywordsDefined = keywords !== undefined;
if (
(keywordPathsDefined && builtinKeywordsDefined) ||
(!keywordPathsDefined && !builtinKeywordsDefined)
) {
console.error(
"One of --keyword_file_paths or --keywords is required: Specify a comma-separated list of built-in --keywords (e.g. 'GRASSHOPPER'), or --keyword_file_paths to .ppn files"
);
return;
}
if (builtinKeywordsDefined) {
keywordPaths = [];
for (let builtinKeyword of keywords.split(",")) {
let keywordString = builtinKeyword.trim().toUpperCase();
if (keywordString in BuiltinKeyword) {
keywordPaths.push(
getBuiltinKeywordPath(
BuiltinKeyword[keywordString]
)
);
} else {
console.error(
`Keyword argument ${builtinKeyword} is not in the list of built-in keywords`
);
return;
}
}
}
if (!Array.isArray(keywordPaths)) {
keywordPaths = keywordPaths.split(",");
}
let keywordNames = [];
// get the 'friendly' name of the keyword instead of showing index '0','1','2', etc.
for (let keywordPath of keywordPaths) {
if (keywordPathsDefined && keywordPath in BuiltinKeyword) {
console.warn(
`--keyword_path argument '${keywordPath}' matches a built-in keyword. Did you mean to use --keywords ?`
);
}
let keywordName = keywordPath
.split(/[\\|\/]/)
.pop()
.split("_")[0];
keywordNames.push(keywordName);
}
if (isNaN(sensitivity) || sensitivity < 0 || sensitivity > 1) {
console.error("--sensitivity must be a number in the range [0,1]");
return;
}
// apply the same sensitivity value to all wake words to make running the demo easier
let sensitivities = [];
for (let i = 0; i < keywordPaths.length; i++) {
sensitivities.push(sensitivity);
}
if (!fs.existsSync(audioPath)) {
console.error(`--input_audio_file_path file not found: ${audioPath}`);
return;
}
let engineInstance;
try {
engineInstance = new Porcupine(
accessKey,
keywordPaths,
sensitivities,
modelFilePath,
libraryFilePath
);
} catch (error) {
console.error(`Error initializing Porcupine engine: ${error}`);
return;
}
let waveBuffer = fs.readFileSync(audioPath);
let inputWaveFile;
try {
inputWaveFile = new WaveFile(waveBuffer);
} catch (error) {
console.error(`Exception trying to read file as wave format: ${audioPath}`);
console.error(error);
return;
}
if (!checkWaveFile(inputWaveFile, engineInstance.sampleRate)) {
console.error(
"Audio file did not meet requirements. Wave file must be 16KHz, 16-bit, linear PCM (mono)."
);
}
let frames = getInt16Frames(inputWaveFile, engineInstance.frameLength);
for (let i = 0; i < frames.length; i++) {
const frame = frames[i];
const keywordIndex = engineInstance.process(frame);
if (keywordIndex !== -1) {
const timestamp = frameIndexToSeconds(i, engineInstance);
console.log(
`Detected keyword '${keywordNames[keywordIndex]}' @ ${timestamp}s`
);
}
}
engineInstance.release();
}
fileDemo();