@picovoice/eagle-web 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +4 -4
- package/.eslintrc.js +469 -469
- package/.prettierignore +1 -1
- package/.prettierrc +8 -8
- package/README.md +234 -236
- package/dist/esm/index.js +3656 -11290
- package/dist/esm/index.min.js +1 -1
- package/dist/iife/index.js +3655 -11289
- package/dist/iife/index.min.js +1 -1
- package/dist/types/eagle.d.ts +237 -245
- package/dist/types/eagle.d.ts.map +1 -1
- package/dist/types/eagle_errors.d.ts +47 -47
- package/dist/types/eagle_profiler_worker.d.ts +112 -113
- package/dist/types/eagle_profiler_worker.d.ts.map +1 -1
- package/dist/types/eagle_profiler_worker_handler.d.ts +3 -3
- package/dist/types/eagle_worker.d.ts +88 -93
- package/dist/types/eagle_worker.d.ts.map +1 -1
- package/dist/types/eagle_worker_handler.d.ts +3 -3
- package/dist/types/index.d.ts +6 -6
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types.d.ts +142 -140
- package/dist/types/types.d.ts.map +1 -1
- package/module.d.ts +16 -16
- package/package.json +73 -73
- package/rollup.config.js +75 -75
- package/src/eagle.ts +1546 -1508
- package/src/eagle_errors.ts +252 -252
- package/src/eagle_profiler_worker.ts +443 -405
- package/src/eagle_profiler_worker_handler.ts +246 -208
- package/src/eagle_worker.ts +307 -346
- package/src/eagle_worker_handler.ts +138 -170
- package/src/index.ts +94 -98
- package/src/types.ts +224 -218
- package/tsconfig.json +22 -22
package/src/eagle_worker.ts
CHANGED
|
@@ -1,346 +1,307 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2023-
|
|
3
|
-
|
|
4
|
-
You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
|
|
5
|
-
file accompanying this source.
|
|
6
|
-
|
|
7
|
-
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
|
8
|
-
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
9
|
-
specific language governing permissions and limitations under the License.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import PvWorker from 'web-worker:./eagle_worker_handler.ts';
|
|
13
|
-
|
|
14
|
-
import {
|
|
15
|
-
EagleModel,
|
|
16
|
-
EagleOptions,
|
|
17
|
-
EagleWorkerProcessResponse,
|
|
18
|
-
EagleWorkerInitResponse,
|
|
19
|
-
EagleWorkerReleaseResponse,
|
|
20
|
-
EagleWorkerResetResponse,
|
|
21
|
-
EagleProfile,
|
|
22
|
-
PvStatus,
|
|
23
|
-
} from './types';
|
|
24
|
-
import { loadModel } from '@picovoice/web-utils';
|
|
25
|
-
import { pvStatusToException } from "./eagle_errors";
|
|
26
|
-
|
|
27
|
-
export class EagleWorker {
|
|
28
|
-
private readonly _worker: Worker;
|
|
29
|
-
private readonly
|
|
30
|
-
private readonly _sampleRate: number;
|
|
31
|
-
private readonly _version: string;
|
|
32
|
-
|
|
33
|
-
private static _wasmSimd: string;
|
|
34
|
-
private static _wasmSimdLib: string;
|
|
35
|
-
private static _wasmPThread: string;
|
|
36
|
-
private static _wasmPThreadLib: string;
|
|
37
|
-
|
|
38
|
-
private static _sdk: string = 'web';
|
|
39
|
-
|
|
40
|
-
private constructor(
|
|
41
|
-
worker: Worker,
|
|
42
|
-
|
|
43
|
-
sampleRate: number,
|
|
44
|
-
version: string
|
|
45
|
-
) {
|
|
46
|
-
this._worker = worker;
|
|
47
|
-
this.
|
|
48
|
-
this._sampleRate = sampleRate;
|
|
49
|
-
this._version = version;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Number of audio samples per frame expected by Eagle (i.e. length of the array passed into `.process()`)
|
|
54
|
-
*/
|
|
55
|
-
get
|
|
56
|
-
return this.
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Audio sample rate required by Eagle.
|
|
61
|
-
*/
|
|
62
|
-
get sampleRate(): number {
|
|
63
|
-
return this._sampleRate;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Version of Eagle.
|
|
68
|
-
*/
|
|
69
|
-
get version(): string {
|
|
70
|
-
return this._version;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Set base64 wasm file with SIMD feature.
|
|
75
|
-
* @param wasmSimd Base64'd wasm SIMD file to use to initialize wasm.
|
|
76
|
-
*/
|
|
77
|
-
public static setWasmSimd(wasmSimd: string): void {
|
|
78
|
-
if (this._wasmSimd === undefined) {
|
|
79
|
-
this._wasmSimd = wasmSimd;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Set base64 wasm file with SIMD feature in text format.
|
|
85
|
-
* @param wasmSimdLib Base64'd wasm SIMD file in text format.
|
|
86
|
-
*/
|
|
87
|
-
public static setWasmSimdLib(wasmSimdLib: string): void {
|
|
88
|
-
if (this._wasmSimdLib === undefined) {
|
|
89
|
-
this._wasmSimdLib = wasmSimdLib;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Set base64 wasm file with SIMD and pthread feature.
|
|
95
|
-
* @param wasmPThread Base64'd wasm file to use to initialize wasm.
|
|
96
|
-
*/
|
|
97
|
-
public static setWasmPThread(wasmPThread: string): void {
|
|
98
|
-
if (this._wasmPThread === undefined) {
|
|
99
|
-
this._wasmPThread = wasmPThread;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Set base64 SIMD and thread wasm file in text format.
|
|
105
|
-
* @param wasmPThreadLib Base64'd wasm file in text format.
|
|
106
|
-
*/
|
|
107
|
-
public static setWasmPThreadLib(wasmPThreadLib: string): void {
|
|
108
|
-
if (this._wasmPThreadLib === undefined) {
|
|
109
|
-
this._wasmPThreadLib = wasmPThreadLib;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
public static setSdk(sdk: string): void {
|
|
114
|
-
EagleWorker._sdk = sdk;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Creates an instance of the Picovoice Eagle Speaker Recognition Engine.
|
|
119
|
-
*
|
|
120
|
-
* @param accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
|
|
121
|
-
* @param model Eagle model options.
|
|
122
|
-
* @param model.base64 The model in base64 string to initialize Eagle.
|
|
123
|
-
* @param model.publicPath The model path relative to the public directory.
|
|
124
|
-
* @param model.customWritePath Custom path to save the model in storage.
|
|
125
|
-
* Set to a different name to use multiple models across `eagle` instances.
|
|
126
|
-
* @param model.forceWrite Flag to overwrite the model in storage even if it exists.
|
|
127
|
-
* @param model.version Version of the model file. Increment to update the model file in storage.
|
|
128
|
-
* @param
|
|
129
|
-
* @param options
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* `cpu`,
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
* @return An instance of the Eagle engine.
|
|
137
|
-
*/
|
|
138
|
-
public static async create(
|
|
139
|
-
accessKey: string,
|
|
140
|
-
model: EagleModel,
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
event.data.
|
|
163
|
-
event.data.
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
case '
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
event.data.
|
|
173
|
-
event.data.
|
|
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
|
-
*
|
|
214
|
-
*
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const returnPromise: Promise<number[]> = new Promise((resolve, reject) => {
|
|
221
|
-
this._worker.onmessage = (
|
|
222
|
-
event: MessageEvent<EagleWorkerProcessResponse>
|
|
223
|
-
): void => {
|
|
224
|
-
switch (event.data.command) {
|
|
225
|
-
case 'ok':
|
|
226
|
-
resolve(event.data.scores);
|
|
227
|
-
break;
|
|
228
|
-
case 'failed':
|
|
229
|
-
case 'error':
|
|
230
|
-
reject(
|
|
231
|
-
pvStatusToException(
|
|
232
|
-
event.data.status,
|
|
233
|
-
event.data.shortMessage,
|
|
234
|
-
event.data.messageStack
|
|
235
|
-
)
|
|
236
|
-
);
|
|
237
|
-
break;
|
|
238
|
-
default:
|
|
239
|
-
reject(
|
|
240
|
-
pvStatusToException(
|
|
241
|
-
PvStatus.RUNTIME_ERROR,
|
|
242
|
-
// @ts-ignore
|
|
243
|
-
`Unrecognized command: ${event.data.command}`
|
|
244
|
-
)
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
});
|
|
249
|
-
this._worker.postMessage({
|
|
250
|
-
command: 'process',
|
|
251
|
-
inputFrame: pcm,
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
case '
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
event.data.
|
|
277
|
-
event.data.
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
case 'ok':
|
|
309
|
-
resolve();
|
|
310
|
-
break;
|
|
311
|
-
case 'failed':
|
|
312
|
-
case 'error':
|
|
313
|
-
reject(
|
|
314
|
-
pvStatusToException(
|
|
315
|
-
event.data.status,
|
|
316
|
-
event.data.shortMessage,
|
|
317
|
-
event.data.messageStack
|
|
318
|
-
)
|
|
319
|
-
);
|
|
320
|
-
break;
|
|
321
|
-
default:
|
|
322
|
-
reject(
|
|
323
|
-
pvStatusToException(
|
|
324
|
-
PvStatus.RUNTIME_ERROR,
|
|
325
|
-
// @ts-ignore
|
|
326
|
-
`Unrecognized command: ${event.data.command}`
|
|
327
|
-
)
|
|
328
|
-
);
|
|
329
|
-
}
|
|
330
|
-
};
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
this._worker.postMessage({
|
|
334
|
-
command: 'release',
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
return returnPromise;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* Terminates the active worker. Stops all requests being handled by worker.
|
|
342
|
-
*/
|
|
343
|
-
public terminate(): void {
|
|
344
|
-
this._worker.terminate();
|
|
345
|
-
}
|
|
346
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2023-2026 Picovoice Inc.
|
|
3
|
+
|
|
4
|
+
You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
|
|
5
|
+
file accompanying this source.
|
|
6
|
+
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
|
8
|
+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
9
|
+
specific language governing permissions and limitations under the License.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import PvWorker from 'web-worker:./eagle_worker_handler.ts';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
EagleModel,
|
|
16
|
+
EagleOptions,
|
|
17
|
+
EagleWorkerProcessResponse,
|
|
18
|
+
EagleWorkerInitResponse,
|
|
19
|
+
EagleWorkerReleaseResponse,
|
|
20
|
+
EagleWorkerResetResponse,
|
|
21
|
+
EagleProfile,
|
|
22
|
+
PvStatus,
|
|
23
|
+
} from './types';
|
|
24
|
+
import { loadModel } from '@picovoice/web-utils';
|
|
25
|
+
import { pvStatusToException } from "./eagle_errors";
|
|
26
|
+
|
|
27
|
+
export class EagleWorker {
|
|
28
|
+
private readonly _worker: Worker;
|
|
29
|
+
private readonly _minProcessSamples: number;
|
|
30
|
+
private readonly _sampleRate: number;
|
|
31
|
+
private readonly _version: string;
|
|
32
|
+
|
|
33
|
+
private static _wasmSimd: string;
|
|
34
|
+
private static _wasmSimdLib: string;
|
|
35
|
+
private static _wasmPThread: string;
|
|
36
|
+
private static _wasmPThreadLib: string;
|
|
37
|
+
|
|
38
|
+
private static _sdk: string = 'web';
|
|
39
|
+
|
|
40
|
+
private constructor(
|
|
41
|
+
worker: Worker,
|
|
42
|
+
minProcessSamples: number,
|
|
43
|
+
sampleRate: number,
|
|
44
|
+
version: string
|
|
45
|
+
) {
|
|
46
|
+
this._worker = worker;
|
|
47
|
+
this._minProcessSamples = minProcessSamples;
|
|
48
|
+
this._sampleRate = sampleRate;
|
|
49
|
+
this._version = version;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Number of audio samples per frame expected by Eagle (i.e. length of the array passed into `.process()`)
|
|
54
|
+
*/
|
|
55
|
+
get minProcessSamples(): number {
|
|
56
|
+
return this._minProcessSamples;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Audio sample rate required by Eagle.
|
|
61
|
+
*/
|
|
62
|
+
get sampleRate(): number {
|
|
63
|
+
return this._sampleRate;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Version of Eagle.
|
|
68
|
+
*/
|
|
69
|
+
get version(): string {
|
|
70
|
+
return this._version;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Set base64 wasm file with SIMD feature.
|
|
75
|
+
* @param wasmSimd Base64'd wasm SIMD file to use to initialize wasm.
|
|
76
|
+
*/
|
|
77
|
+
public static setWasmSimd(wasmSimd: string): void {
|
|
78
|
+
if (this._wasmSimd === undefined) {
|
|
79
|
+
this._wasmSimd = wasmSimd;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Set base64 wasm file with SIMD feature in text format.
|
|
85
|
+
* @param wasmSimdLib Base64'd wasm SIMD file in text format.
|
|
86
|
+
*/
|
|
87
|
+
public static setWasmSimdLib(wasmSimdLib: string): void {
|
|
88
|
+
if (this._wasmSimdLib === undefined) {
|
|
89
|
+
this._wasmSimdLib = wasmSimdLib;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Set base64 wasm file with SIMD and pthread feature.
|
|
95
|
+
* @param wasmPThread Base64'd wasm file to use to initialize wasm.
|
|
96
|
+
*/
|
|
97
|
+
public static setWasmPThread(wasmPThread: string): void {
|
|
98
|
+
if (this._wasmPThread === undefined) {
|
|
99
|
+
this._wasmPThread = wasmPThread;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Set base64 SIMD and thread wasm file in text format.
|
|
105
|
+
* @param wasmPThreadLib Base64'd wasm file in text format.
|
|
106
|
+
*/
|
|
107
|
+
public static setWasmPThreadLib(wasmPThreadLib: string): void {
|
|
108
|
+
if (this._wasmPThreadLib === undefined) {
|
|
109
|
+
this._wasmPThreadLib = wasmPThreadLib;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
public static setSdk(sdk: string): void {
|
|
114
|
+
EagleWorker._sdk = sdk;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Creates an instance of the Picovoice Eagle Speaker Recognition Engine.
|
|
119
|
+
*
|
|
120
|
+
* @param accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
|
|
121
|
+
* @param model Eagle model options.
|
|
122
|
+
* @param model.base64 The model in base64 string to initialize Eagle.
|
|
123
|
+
* @param model.publicPath The model path relative to the public directory.
|
|
124
|
+
* @param model.customWritePath Custom path to save the model in storage.
|
|
125
|
+
* Set to a different name to use multiple models across `eagle` instances.
|
|
126
|
+
* @param model.forceWrite Flag to overwrite the model in storage even if it exists.
|
|
127
|
+
* @param model.version Version of the model file. Increment to update the model file in storage.
|
|
128
|
+
* @param options Optional configuration arguments.
|
|
129
|
+
* @param options.device String representation of the device (e.g., CPU or GPU) to use. If set to `best`, the most
|
|
130
|
+
* suitable device is selected automatically. If set to `gpu`, the engine uses the first available GPU device. To select a specific
|
|
131
|
+
* GPU device, set this argument to `gpu:${GPU_INDEX}`, where `${GPU_INDEX}` is the index of the target GPU. If set to
|
|
132
|
+
* `cpu`, the engine will run on the CPU with the default number of threads. To specify the number of threads, set this
|
|
133
|
+
* argument to `cpu:${NUM_THREADS}`, where `${NUM_THREADS}` is the desired number of threads.
|
|
134
|
+
* @param options.voiceThreshold Sensitivity threshold for detecting voice.
|
|
135
|
+
*
|
|
136
|
+
* @return An instance of the Eagle engine.
|
|
137
|
+
*/
|
|
138
|
+
public static async create(
|
|
139
|
+
accessKey: string,
|
|
140
|
+
model: EagleModel,
|
|
141
|
+
options: EagleOptions = {}
|
|
142
|
+
): Promise<EagleWorker> {
|
|
143
|
+
const customWritePath = model.customWritePath
|
|
144
|
+
? model.customWritePath
|
|
145
|
+
: 'eagle_model';
|
|
146
|
+
const modelPath = await loadModel({ ...model, customWritePath });
|
|
147
|
+
|
|
148
|
+
const worker = new PvWorker();
|
|
149
|
+
const returnPromise: Promise<EagleWorker> = new Promise(
|
|
150
|
+
(resolve, reject) => {
|
|
151
|
+
// @ts-ignore - block from GC
|
|
152
|
+
this.worker = worker;
|
|
153
|
+
worker.onmessage = (
|
|
154
|
+
event: MessageEvent<EagleWorkerInitResponse>
|
|
155
|
+
): void => {
|
|
156
|
+
switch (event.data.command) {
|
|
157
|
+
case 'ok':
|
|
158
|
+
resolve(
|
|
159
|
+
new EagleWorker(
|
|
160
|
+
worker,
|
|
161
|
+
event.data.minProcessSamples,
|
|
162
|
+
event.data.sampleRate,
|
|
163
|
+
event.data.version
|
|
164
|
+
)
|
|
165
|
+
);
|
|
166
|
+
break;
|
|
167
|
+
case 'failed':
|
|
168
|
+
case 'error':
|
|
169
|
+
reject(
|
|
170
|
+
pvStatusToException(
|
|
171
|
+
event.data.status,
|
|
172
|
+
event.data.shortMessage,
|
|
173
|
+
event.data.messageStack
|
|
174
|
+
)
|
|
175
|
+
);
|
|
176
|
+
break;
|
|
177
|
+
default:
|
|
178
|
+
reject(
|
|
179
|
+
pvStatusToException(
|
|
180
|
+
PvStatus.RUNTIME_ERROR,
|
|
181
|
+
// @ts-ignore
|
|
182
|
+
`Unrecognized command: ${event.data.command}`
|
|
183
|
+
)
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
worker.postMessage({
|
|
191
|
+
command: 'init',
|
|
192
|
+
accessKey: accessKey,
|
|
193
|
+
modelPath: modelPath,
|
|
194
|
+
options: options,
|
|
195
|
+
wasmSimd: this._wasmSimd,
|
|
196
|
+
wasmSimdLib: this._wasmSimdLib,
|
|
197
|
+
wasmPThread: this._wasmPThread,
|
|
198
|
+
wasmPThreadLib: this._wasmPThreadLib,
|
|
199
|
+
sdk: this._sdk,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
return returnPromise;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Processes a frame of audio and returns a list of similarity scores for each speaker profile.
|
|
207
|
+
*
|
|
208
|
+
* @param pcm A frame of audio samples. The number of samples per frame can be attained by calling
|
|
209
|
+
* `.frameLength`. The incoming audio needs to have a sample rate equal to `.sampleRate` and be 16-bit
|
|
210
|
+
* linearly-encoded. Eagle operates on single-channel audio.
|
|
211
|
+
* @param speakerProfiles One or more Eagle speaker profiles. These can be constructed using `EagleProfiler`.
|
|
212
|
+
*
|
|
213
|
+
* @return A list of similarity scores for each speaker profile. A higher score indicates that the voice
|
|
214
|
+
* belongs to the corresponding speaker. The range is [0, 1] with 1.0 representing a perfect match.
|
|
215
|
+
*/
|
|
216
|
+
public process(
|
|
217
|
+
pcm: Int16Array,
|
|
218
|
+
speakerProfiles: EagleProfile[] | EagleProfile,
|
|
219
|
+
): Promise<number[]> {
|
|
220
|
+
const returnPromise: Promise<number[]> = new Promise((resolve, reject) => {
|
|
221
|
+
this._worker.onmessage = (
|
|
222
|
+
event: MessageEvent<EagleWorkerProcessResponse>
|
|
223
|
+
): void => {
|
|
224
|
+
switch (event.data.command) {
|
|
225
|
+
case 'ok':
|
|
226
|
+
resolve(event.data.scores);
|
|
227
|
+
break;
|
|
228
|
+
case 'failed':
|
|
229
|
+
case 'error':
|
|
230
|
+
reject(
|
|
231
|
+
pvStatusToException(
|
|
232
|
+
event.data.status,
|
|
233
|
+
event.data.shortMessage,
|
|
234
|
+
event.data.messageStack
|
|
235
|
+
)
|
|
236
|
+
);
|
|
237
|
+
break;
|
|
238
|
+
default:
|
|
239
|
+
reject(
|
|
240
|
+
pvStatusToException(
|
|
241
|
+
PvStatus.RUNTIME_ERROR,
|
|
242
|
+
// @ts-ignore
|
|
243
|
+
`Unrecognized command: ${event.data.command}`
|
|
244
|
+
)
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
});
|
|
249
|
+
this._worker.postMessage({
|
|
250
|
+
command: 'process',
|
|
251
|
+
inputFrame: pcm,
|
|
252
|
+
speakerProfiles: !Array.isArray(speakerProfiles)
|
|
253
|
+
? [speakerProfiles]
|
|
254
|
+
: speakerProfiles,
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
return returnPromise;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Releases resources acquired by Eagle
|
|
262
|
+
*/
|
|
263
|
+
public release(): Promise<void> {
|
|
264
|
+
const returnPromise: Promise<void> = new Promise((resolve, reject) => {
|
|
265
|
+
this._worker.onmessage = (
|
|
266
|
+
event: MessageEvent<EagleWorkerReleaseResponse>
|
|
267
|
+
): void => {
|
|
268
|
+
switch (event.data.command) {
|
|
269
|
+
case 'ok':
|
|
270
|
+
resolve();
|
|
271
|
+
break;
|
|
272
|
+
case 'failed':
|
|
273
|
+
case 'error':
|
|
274
|
+
reject(
|
|
275
|
+
pvStatusToException(
|
|
276
|
+
event.data.status,
|
|
277
|
+
event.data.shortMessage,
|
|
278
|
+
event.data.messageStack
|
|
279
|
+
)
|
|
280
|
+
);
|
|
281
|
+
break;
|
|
282
|
+
default:
|
|
283
|
+
reject(
|
|
284
|
+
pvStatusToException(
|
|
285
|
+
PvStatus.RUNTIME_ERROR,
|
|
286
|
+
// @ts-ignore
|
|
287
|
+
`Unrecognized command: ${event.data.command}`
|
|
288
|
+
)
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
this._worker.postMessage({
|
|
295
|
+
command: 'release',
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
return returnPromise;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Terminates the active worker. Stops all requests being handled by worker.
|
|
303
|
+
*/
|
|
304
|
+
public terminate(): void {
|
|
305
|
+
this._worker.terminate();
|
|
306
|
+
}
|
|
307
|
+
}
|