-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
480 lines (434 loc) · 16.5 KB
/
index.html
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<!DOCTYPE html>
<html lang="en">
<head>
<script src="node_modules/@picovoice/web-voice-processor/dist/iife/index.js"></script>
<script src="node_modules/@picovoice/eagle-web/dist/iife/index.js"></script>
<script src="eagle_params.js"></script>
<script type="application/javascript">
let profiler = null;
let eagle = null;
let speakerProfiles = []
let timer = null;
let currentTimer = 0.0;
let audioData = [];
let audioContext = null;
const ENABLE_AUDIO_DUMP = false;
let dumpAudio = [];
window.onload = function () {
audioContext = new (window.AudioContext || window.webKitAudioContext)(
{ sampleRate: 16000 }
);
document.getElementById("audioFile").addEventListener("change", async (event) => {
newEnrollmentUI();
try {
await profiler.reset();
} catch (e) {
writeMessage(`Failed to reset Eagle Profiler. Error: ${e}`);
return;
}
writeMessage("Processing audio files...");
const fileList = event.target.files;
let percentage = 0;
for (const f of fileList) {
let audioFrame;
try {
audioFrame = await getAudioFileData(f, audioContext);
} catch (e) {
writeMessage(`Failed to read audio file '${f.name}'. Error: ${e}`);
return;
}
try {
const result = await profiler.enroll(audioFrame);
updateSpeakerProgress(speakerProfiles.length + 1, result.feedback, result.percentage)
percentage = result.percentage;
} catch (e) {
writeMessage(`Failed to enroll using '${f.name}'. Error: ${e}`);
return;
}
}
if (percentage < 100) {
writeMessage(`Speaker is only ${percentage}% done enrollment. ` +
`Please choose a larger data set and try again.`);
enrollFailUI();
return;
}
try {
const profile = await profiler.export();
speakerProfiles.push(profile);
enrollSuccessUI();
writeMessage(`Enrollment for Speaker ${speakerProfiles.length} complete! ` +
`You can begin testing or enroll another speaker.`);
} catch (e) {
writeMessage(`Failed to enroll speaker. Error: ${e}`);
}
});
}
async function getAudioFileData(audioFile, audioContext) {
const dataBuffer = await audioFile.arrayBuffer();
const audioBuffer = await audioContext.decodeAudioData(dataBuffer);
const f32PCM = audioBuffer.getChannelData(0);
const i16PCM = new Int16Array(f32PCM.length);
const INT16_MAX = 32767;
const INT16_MIN = -32768;
i16PCM.set(
f32PCM.map((f) => {
let i = Math.trunc(f * INT16_MAX);
if (f > INT16_MAX) i = INT16_MAX;
if (f < INT16_MIN) i = INT16_MIN;
return i;
})
);
return i16PCM;
}
const micEnrollEngine = {
onmessage: async (event) => {
switch (event.data.command) {
case "process":
audioData.push(event.data.inputFrame);
if (ENABLE_AUDIO_DUMP) {
dumpAudio = dumpAudio.concat(event.data.inputFrame);
}
if (audioData.length * 512 >= profiler.minEnrollSamples) {
let result;
try {
const frames = new Int16Array(audioData.length * 512);
for (let i = 0; i < audioData.length; i++) {
frames.set(audioData[i], i * 512);
}
audioData = [];
result = await profiler.enroll(frames);
} catch (e) {
writeMessage(`Failed to enroll. Error: ${e}`);
return;
}
updateSpeakerProgress(speakerProfiles.length + 1, result.feedback, result.percentage);
if (result.percentage === 100) {
await window.WebVoiceProcessor.WebVoiceProcessor.unsubscribe(micEnrollEngine);
clearInterval(timer);
micEnrollStopUI();
try {
const profile = await profiler.export();
speakerProfiles.push(profile);
enrollSuccessUI();
writeMessage(`Enrollment for Speaker ${speakerProfiles.length} complete! ` +
`You can begin testing or enroll another speaker.`);
} catch (e) {
writeMessage(`Failed to enroll speaker. Error: ${e}`);
}
if (ENABLE_AUDIO_DUMP) {
downloadDumpAudio("enroll.pcm");
}
}
}
break;
}
}
}
async function micEnrollStart() {
currentTimer = 0.0;
audioData = [];
micEnrollStartUI();
newEnrollmentUI();
try {
await profiler.reset();
} catch (e) {
writeMessage(`Failed to reset Eagle Profiler. Error: ${e}`);
return;
}
writeMessage("Keep speaking continuously until enrollment progress reaches 100%.");
try {
await window.WebVoiceProcessor.WebVoiceProcessor.subscribe(micEnrollEngine);
timer = setInterval(() => {
currentTimer += 0.1;
document.getElementById("displayTimer").innerText = `${currentTimer.toFixed(1)}`;
}, 100);
} catch (e) {
writeMessage(e);
}
}
async function micEnrollStop() {
await window.WebVoiceProcessor.WebVoiceProcessor.unsubscribe(micEnrollEngine);
clearInterval(timer);
writeMessage(`Enrollment stopped`);
updateSpeakerTable();
enrollFailUI();
micEnrollStopUI();
}
function writeMessage(message) {
console.log(message);
document.getElementById("status").innerHTML = message;
}
function updateSpeakerProgress(speakerId, feedback, progress) {
const feedbackMsg = getFeedbackMessage(feedback)
console.log(progress, feedbackMsg);
document.getElementById(`speaker${speakerId}Feedback`).innerHTML = ` ${feedbackMsg}`;
document.getElementById(`speaker${speakerId}Progress`).value = progress;
}
function updateSpeakerScore(speakerId, score) {
document.getElementById(`speaker${speakerId}Progress`).value = score * 100;
}
function getFeedbackMessage(feedback) {
switch (feedback) {
case EagleWeb.EagleProfilerEnrollFeedback.AUDIO_TOO_SHORT:
return "Insufficient audio length";
case EagleWeb.EagleProfilerEnrollFeedback.UNKNOWN_SPEAKER:
return "Different speaker detected in audio";
case EagleWeb.EagleProfilerEnrollFeedback.NO_VOICE_FOUND:
return "Unable to detect voice in audio";
case EagleWeb.EagleProfilerEnrollFeedback.QUALITY_ISSUE:
return "Audio quality too low to use for enrollment";
default:
return "Enrolling speaker...";
}
}
function updateSpeakerTable() {
const speakerTable = document.getElementById("speakersTable");
while (speakerTable.lastElementChild) {
speakerTable.removeChild(speakerTable.lastElementChild);
}
for (let i = 0; i < speakerProfiles.length; i++) {
speakerTable.append(createSpeakerRow(i + 1, 100));
speakerTable.append(document.createElement("br"))
}
}
function createSpeakerRow(i, initialProgress) {
const div = document.createElement("div");
div.textContent = `Speaker ${i} `
const speakerProgress = document.createElement("progress");
speakerProgress.max = 100;
speakerProgress.value = initialProgress;
speakerProgress.id = `speaker${i}Progress`
const speakerFeedback = document.createElement("span");
speakerFeedback.id = `speaker${i}Feedback`
div.appendChild(speakerProgress);
div.appendChild(speakerFeedback);
return div;
}
function newEnrollmentUI() {
updateSpeakerTable();
document.getElementById("testContainer").style.display = "block";
document.getElementById("feedbackText").innerHTML = "";
document.getElementById("speakersTable").append(createSpeakerRow(speakerProfiles.length + 1, 0));
document.getElementById("speakersTable").append(document.createElement("br"))
document.getElementById("testStartBtn").disabled = true;
document.getElementById("resetBtn").disabled = true;
}
function enrollFailUI() {
document.getElementById("testStartBtn").disabled = speakerProfiles.length === 0;
document.getElementById("resetBtn").disabled = speakerProfiles.length === 0;
}
function enrollSuccessUI() {
updateSpeakerTable();
document.getElementById("testStartBtn").disabled = false;
document.getElementById("resetBtn").disabled = false;
}
function micEnrollStartUI() {
document.getElementById("displayTimer").style.display = "inline";
document.getElementById("micEnrollStartBtn").style.display = "none";
document.getElementById("audioFile").disabled = true;
document.getElementById("micEnrollStopBtn").style.display = "inline";
}
function micEnrollStopUI() {
document.getElementById("displayTimer").style.display = "none";
document.getElementById("micEnrollStartBtn").style.display = "inline";
document.getElementById("audioFile").disabled = false;
document.getElementById("micEnrollStopBtn").style.display = "none";
document.getElementById("displayTimer").innerText = `0.0`;
}
function micTestStartUI() {
document.getElementById("micEnrollStartBtn").disabled = true;
document.getElementById("audioFile").disabled = true;
document.getElementById("resetBtn").disabled = true;
document.getElementById("testStartBtn").style.display = "none";
document.getElementById("testStopBtn").style.display = "inline";
document.getElementById("testStopBtn").disabled = true;
}
function micTestStopUI() {
document.getElementById("micEnrollStartBtn").disabled = false;
document.getElementById("audioFile").disabled = false;
document.getElementById("resetBtn").disabled = false;
document.getElementById("testStartBtn").style.display = "inline";
document.getElementById("testStopBtn").style.display = "none";
}
async function startEagleProfiler(accessKey) {
writeMessage("Eagle is loading. Please wait...");
try {
profiler = await EagleWeb.EagleProfilerWorker.create(
accessKey,
{
base64: modelParams,
forceWrite: true
});
writeMessage("");
document.getElementById('enrollContainer').style.display = "block";
} catch (e) {
writeMessage(`Failed to initialize Eagle. Error: ${e}`);
}
}
const micTestEngine = {
onmessage: async (event) => {
switch (event.data.command) {
case "process":
if (ENABLE_AUDIO_DUMP) {
dumpAudio = dumpAudio.concat(event.data.inputFrame);
}
let scores
try {
scores = await eagle.process(event.data.inputFrame);
} catch (e) {
writeMessage(`Failed to enroll. Error: ${e}`);
return;
}
for (let i = 0; i < scores.length; i++) {
updateSpeakerScore(i + 1, scores[i]);
}
break;
}
}
}
async function startEagle(accessKey) {
writeMessage("Eagle is loading. Please wait...");
micTestStartUI();
try {
eagle = await EagleWeb.EagleWorker.create(
accessKey,
{
base64: modelParams,
forceWrite: true
},
speakerProfiles);
} catch (e) {
writeMessage(`Failed to initialize Eagle. Error: ${e}`);
return;
}
try {
await profiler.reset();
} catch (e) {
writeMessage(`Failed to reset Eagle Profiler. Error: ${e}`);
return;
}
document.getElementById("testStopBtn").disabled = false;
for (let i = 0; i < speakerProfiles.length; i++) {
updateSpeakerScore(i + 1, 0);
}
writeMessage("Take turns speaking sentences and see if Eagle can recognize which speaker is talking");
try {
await window.WebVoiceProcessor.WebVoiceProcessor.subscribe(micTestEngine);
} catch (e) {
writeMessage(e);
}
}
async function stopEagle() {
try {
await window.WebVoiceProcessor.WebVoiceProcessor.unsubscribe(micTestEngine);
} catch (e) {
writeMessage(e);
}
if (eagle) {
await eagle.terminate();
eagle = null;
}
for (let i = 0; i < speakerProfiles.length; i++) {
updateSpeakerScore(i + 1, 100);
}
micTestStopUI();
writeMessage("");
if (ENABLE_AUDIO_DUMP) {
downloadDumpAudio('test.pcm');
}
}
function resetSpeakers() {
document.getElementById("testContainer").style.display = "none";
speakerProfiles = [];
updateSpeakerTable();
writeMessage("");
}
function downloadDumpAudio(fileName) {
let blob = new Blob(dumpAudio);
dumpAudio = [];
let a = document.createElement('a');
a.download = fileName;
a.href = window.URL.createObjectURL(blob);
a.click();
document.removeChild(a);
}
</script>
</head>
<body>
<h1>Eagle Web Demo</h1>
<p>This demo uses Eagle for Web and the WebVoiceProcessor to:</p>
<ol>
<li>
Create an instance of Eagle with the model file provided.
</li>
<li>
Select an audio file or acquire microphone data stream and convert to voice
processing format (16kHz 16-bit linear PCM). The downsampled audio is
forwarded to the Eagle engine. The audio <i>does not</i> leave the
browser: all processing is occurring via the Eagle WebAssembly code.
</li>
<li>
Enroll multiple speakers with audio files or a microphone.
</li>
<li>
Recognize which speaker is talking in real-time.
</li>
</ol>
After entering the AccessKey, click the "Start Eagle" button.
<hr />
<label for="accessKey"
>AccessKey obtained from
<a href="https://console.picovoice.ai/">Picovoice Console</a>:</label
>
<input type="text" id="accessKey" name="accessKey" />
<input
type="button"
id="submit"
value="Start Eagle"
onclick="startEagleProfiler(document.getElementById('accessKey').value)"
/>
<hr/>
<div id="enrollContainer" style="display: none">
<label for="audioFile">Enroll speaker with one or more audio files:</label>
<input
type="file"
id="audioFile"
name="audioFile"
accept="audio/*,.wav"
multiple />
<p><b>OR</b></p>
<label for="micEnrollStartBtn">Enroll speaker with microphone:</label>
<button id="micEnrollStartBtn" onclick="micEnrollStart()">Record Audio</button>
<button id="micEnrollStopBtn" onclick="micEnrollStop()" style="display: none">Stop Enrollment</button>
<span id="displayTimer" style="display: none;"></span>
<br />
<hr/>
</div>
<div id="testContainer" style="display: none">
<div>
<button
id="testStartBtn"
onclick="startEagle(document.getElementById('accessKey').value)">
Start Test
</button>
<button
id="testStopBtn"
onclick="stopEagle()"
style="display: none">
Stop Test
</button>
<button
id="resetBtn"
onclick="resetSpeakers()">
Reset
</button>
<span id="feedbackText"></span>
</div>
<br />
<div id="speakersTable">
</div>
<hr />
</div>
<div id="status"></div>
</body>
</html>