-
Notifications
You must be signed in to change notification settings - Fork 501
/
porcupine.tsx
213 lines (197 loc) · 7.57 KB
/
porcupine.tsx
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// Copyright 2020-2021 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 { NativeModules } from 'react-native';
import * as PorcupineErrors from './porcupine_errors';
import type BuiltInKeywords from './builtin_keywords';
const RCTPorcupine = NativeModules.PvPorcupine;
type NativeError = {
code: string;
message: string;
};
class Porcupine {
private _handle: string;
private _frameLength: number;
private _sampleRate: number;
private _version: string;
/**
* Static creator for initializing Porcupine from one of the built-in keywords
* @param accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/.
* @param keywords List of keywords (phrases) for detection. The list of available (default) keywords can be retrieved
* using `Porcupine.KEYWORDS`. If `keyword_paths` is set then this argument will be ignored.
* @param modelPath Path to the file containing model parameters. If not set it will be set to the default location.
* @param sensitivities sensitivities for each keywords model. A higher sensitivity reduces miss rate
* at the cost of potentially higher false alarm rate. Sensitivity should be a floating-point number within
* [0, 1].
* @returns An instance of the engine.
*/
public static async fromBuiltInKeywords(
accessKey: string,
keywords: BuiltInKeywords[],
modelPath: string = '',
sensitivities: number[] = []
) {
try {
let { handle, frameLength, sampleRate, version } =
await RCTPorcupine.fromBuiltInKeywords(
accessKey,
modelPath,
Object.values(keywords),
sensitivities
);
return new Porcupine(handle, frameLength, sampleRate, version);
} catch (err) {
if (err instanceof PorcupineErrors.PorcupineError) {
throw err;
} else {
const nativeError = err as NativeError;
throw this.codeToError(nativeError.code, nativeError.message);
}
}
}
/**
* Static creator for initializing Porcupine from a list of paths to custom keyword files
* @param accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/).
* @param keywordPaths Absolute paths to keyword model files.
* @param modelPath Path to the file containing model parameters. If not set it will be set to the default location.
* @param sensitivities sensitivities for each keywords model. A higher sensitivity reduces miss rate
* at the cost of potentially higher false alarm rate. Sensitivity should be a floating-point number within
* [0, 1].
* @returns An instance of the engine.
*/
public static async fromKeywordPaths(
accessKey: string,
keywordsPaths: string[],
modelPath: string = '',
sensitivities: number[] = []
) {
try {
let { handle, frameLength, sampleRate, version } =
await RCTPorcupine.fromKeywordPaths(
accessKey,
modelPath,
keywordsPaths,
sensitivities
);
return new Porcupine(handle, frameLength, sampleRate, version);
} catch (err) {
if (err instanceof PorcupineErrors.PorcupineError) {
throw err;
} else {
const nativeError = err as NativeError;
throw this.codeToError(nativeError.code, nativeError.message);
}
}
}
private constructor(
handle: string,
frameLength: number,
sampleRate: number,
version: string
) {
this._handle = handle;
this._frameLength = frameLength;
this._sampleRate = sampleRate;
this._version = version;
}
/**
* Process a frame of audio with the wake word engine.
* @param frame A frame of audio samples to be assessed by Porcupine. The required audio format is found by
* calling `.sampleRate` to get the required sample rate and `.frameLength` to get the required frame size.
* Audio must be single-channel and 16-bit linearly-encoded.
* @returns {Promise} Index of the detected keyword, or -1 if no detection occurred
*/
async process(frame: number[]) {
if (frame === undefined) {
throw new PorcupineErrors.PorcupineInvalidArgumentError(
'Frame array provided to process() is undefined or null'
);
} else if (frame.length !== this._frameLength) {
throw new PorcupineErrors.PorcupineInvalidArgumentError(
`Size of frame array provided to 'process' (${frame.length}) does not match the engine 'frameLength' (${this._frameLength})`
);
}
// sample the first frame to check for non-integer values
if (!Number.isInteger(frame[0])) {
throw new PorcupineErrors.PorcupineInvalidArgumentError(
`Non-integer frame values provided to process(): ${frame[0]}. Porcupine requires 16-bit integers`
);
}
try {
return await RCTPorcupine.process(this._handle, frame);
} catch (err) {
const nativeError = err as NativeError;
throw Porcupine.codeToError(nativeError.code, nativeError.message);
}
}
/**
* Frees memory that was allocated for Porcupine
*/
async delete() {
return RCTPorcupine.delete(this._handle);
}
/**
* Gets the required number of audio samples per frame.
* @returns Required frame length.
*/
get frameLength() {
return this._frameLength;
}
/**
* Get the audio sample rate required by Porcupine.
* @returns Required sample rate.
*/
get sampleRate() {
return this._sampleRate;
}
/**
* Gets the version number of the Porcupine library.
* @returns Version of Porcupine
*/
get version() {
return this._version;
}
/**
* Gets the Error type given a code.
* @param code Code name of native Error.
* @param message Detailed message of the error.
*/
private static codeToError(code: string, message: string) {
switch (code) {
case 'PorcupineException':
return new PorcupineErrors.PorcupineError(message);
case 'PorcupineMemoryException':
return new PorcupineErrors.PorcupineMemoryError(message);
case 'PorcupineIOException':
return new PorcupineErrors.PorcupineIOError(message);
case 'PorcupineInvalidArgumentException':
return new PorcupineErrors.PorcupineInvalidArgumentError(message);
case 'PorcupineStopIterationException':
return new PorcupineErrors.PorcupineStopIterationError(message);
case 'PorcupineKeyException':
return new PorcupineErrors.PorcupineKeyError(message);
case 'PorcupineInvalidStateException':
return new PorcupineErrors.PorcupineInvalidStateError(message);
case 'PorcupineRuntimeException':
return new PorcupineErrors.PorcupineRuntimeError(message);
case 'PorcupineActivationException':
return new PorcupineErrors.PorcupineActivationError(message);
case 'PorcupineActivationLimitException':
return new PorcupineErrors.PorcupineActivationLimitError(message);
case 'PorcupineActivationThrottledException':
return new PorcupineErrors.PorcupineActivationThrottledError(message);
case 'PorcupineActivationRefusedException':
return new PorcupineErrors.PorcupineActivationRefusedError(message);
default:
throw new Error(`unexpected code: ${code}, message: ${message}`);
}
}
}
export default Porcupine;