-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
319 lines (275 loc) · 10.8 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
<!DOCTYPE html>
<html lang="en">
<head>
<script src="node_modules/@picovoice/web-voice-processor/dist/iife/index.js"></script>
<script src="node_modules/@picovoice/koala-web/dist/iife/index.js"></script>
<script src="koala_params.js"></script>
<script type="application/javascript">
let koala = null;
let originalBuffer;
let enhancedBuffer;
let outputFrames;
window.onload = function () {
const audioContext = new (window.AudioContext || window.webKitAudioContext)(
{sampleRate: 16000}
);
const originalAudioGain = audioContext.createGain();
originalAudioGain.gain.value = 0;
const enhancedAudioGain = audioContext.createGain();
enhancedAudioGain.gain.value = 1;
originalAudioGain.connect(audioContext.destination);
enhancedAudioGain.connect(audioContext.destination);
let originalAudioSource;
let enhancedAudioSource;
let isPlaying = false;
function readAudioFile(selectedFile, callback) {
let reader = new FileReader();
reader.onload = function (ev) {
let wavBytes = reader.result;
audioContext.decodeAudioData(wavBytes, callback);
};
reader.readAsArrayBuffer(selectedFile);
}
const fileSelector = document.getElementById("audioFile");
fileSelector.addEventListener("change", async (event) => {
outputFrames = [];
resultBox.style.display = "none";
originalAudioSource?.stop();
enhancedAudioSource?.stop();
writeMessage("Loading audio file...");
const fileList = event.target.files;
readAudioFile(fileList[0], async (audioBuffer) => {
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;
})
);
writeMessage("Processing audio file...");
const splitPcm = [];
await koala.reset();
for (let i = 0; i < (i16PCM.length - koala.frameLength + 1); i += koala.frameLength) {
const split = i16PCM.slice(i, i + koala.frameLength);
splitPcm.push(split);
await koala.process(split);
}
writeMessage("Waiting for Koala engine to finish processing audio file...");
await waitForProcess(splitPcm, outputFrames);
originalBuffer = createBuffer(i16PCM);
enhancedBuffer = createBuffer(mergeFrames(outputFrames, koala.delaySample));
writeMessage("Press 'Play' to listen to recording. Move the slider to play around with noise.");
resultBox.style.display = "block";
});
});
const displayTimer = document.getElementById("displayTimer");
const recordButton = document.getElementById("recordAudio");
const stopRecord = document.getElementById("stopRecord");
const resultBox = document.getElementById("result");
const volumeControl = document.getElementById("volumeControl");
const playAudio = document.getElementById("playAudio");
let timer = null;
let currentTimer = 0.0;
let audioData = [];
const recorderEngine = {
onmessage: (event) => {
switch (event.data.command) {
case "process":
audioData.push(event.data.inputFrame);
break;
}
}
}
recordButton.addEventListener("click", async () => {
displayTimer.style.display = "inline";
stopRecord.style.display = "inline";
recordButton.style.display = "none";
resultBox.style.display = "none";
originalAudioSource?.stop();
enhancedAudioSource?.stop();
currentTimer = 0.0;
audioData = [];
outputFrames = [];
try {
writeMessage("Recording audio...");
window.WebVoiceProcessor.WebVoiceProcessor.setOptions({
frameLength: koala.frameLength
});
await window.WebVoiceProcessor.WebVoiceProcessor.subscribe([recorderEngine, koala]);
timer = setInterval(() => {
currentTimer += 0.1;
displayTimer.innerText = `${currentTimer.toFixed(1)} / 120`;
if (currentTimer === 120) {
stopRecord.click();
}
}, 100);
} catch (e) {
writeMessage(e);
}
});
stopRecord.addEventListener("click", async () => {
displayTimer.style.display = "none";
stopRecord.style.display = "none";
recordButton.style.display = "inline";
await window.WebVoiceProcessor.WebVoiceProcessor.unsubscribe([recorderEngine, koala]);
clearInterval(timer);
writeMessage("Waiting for Koala engine to finish processing...")
await waitForProcess(audioData, outputFrames);
originalBuffer = createBuffer(mergeFrames(audioData));
enhancedBuffer = createBuffer(mergeFrames(outputFrames, koala.delaySample));
writeMessage("Press 'Play' to listen to recording. Move the slider to play around with noise.");
resultBox.style.display = "block";
});
volumeControl.addEventListener("input", (e) => {
originalAudioGain.gain.value = 1 - e.target.value;
enhancedAudioGain.gain.value = e.target.value;
});
playAudio.addEventListener("click", () => {
if (!isPlaying) {
isPlaying = true;
const current_time = audioContext.currentTime;
originalAudioSource = audioContext.createBufferSource();
enhancedAudioSource = audioContext.createBufferSource();
originalAudioSource.buffer = originalBuffer;
originalAudioSource.loop = true;
originalAudioSource.connect(originalAudioGain);
originalAudioSource.start(current_time + 0.2);
enhancedAudioSource.buffer = enhancedBuffer;
enhancedAudioSource.loop = true;
enhancedAudioSource.connect(enhancedAudioGain);
enhancedAudioSource.start(current_time + 0.2);
playAudio.innerHTML = "Stop"
} else {
isPlaying = false;
originalAudioSource.stop();
enhancedAudioSource.stop();
playAudio.innerHTML = "Play"
}
});
function mergeFrames(data, delaySample = 0) {
let delay = 0;
const pcm = new Int16Array(data.length * koala.frameLength);
for (let i = 0; i < data.length; i++) {
if (i * koala.frameLength < delaySample) {
delay += 1;
} else {
pcm.set(data[i], (i - delay) * koala.frameLength);
}
}
return pcm;
}
function createBuffer(data) {
const buffer = audioContext.createBuffer(1, data.length, koala.sampleRate);
const source = new Float32Array(data.length);
for (let i = 0; i < data.length; i++){
source[i] = data[i] < 0 ? data[i] / 32768 : data[i] / 32767;
}
buffer.copyToChannel(source, 0);
return buffer;
}
async function waitForProcess(input, output) {
return new Promise(resolve => {
setInterval(() => {
if (input.length === output.length) {
resolve();
}
}, 100)
});
}
}
function writeMessage(message) {
console.log(message);
document.getElementById("status").innerHTML = message;
}
function processErrorCallback(error) {
writeMessage(error);
}
function processCallback(enhancedPcm) {
outputFrames.push(enhancedPcm);
}
async function startKoala(accessKey) {
writeMessage("Koala is loading. Please wait...");
try {
koala = await KoalaWeb.KoalaWorker.create(
accessKey,
processCallback,
{ base64: modelParams },
{ processErrorCallback: processErrorCallback }
);
writeMessage("Koala worker ready!");
writeMessage(
"WebVoiceProcessor initializing. Microphone permissions requested ..."
);
window.WebVoiceProcessor.WebVoiceProcessor.setOptions({
frameLength: koala.frameLength
});
document.getElementById("control").style.display = "block";
writeMessage("Koala worker is ready!");
} catch (err) {
processErrorCallback(err);
}
}
</script>
</head>
<body>
<h1>Koala Web Demo</h1>
<p>This demo uses Koala for Web and the WebVoiceProcessor to:</p>
<ol>
<li>
Create an instance of Koala with the model file provided.
</li>
<li>
Select an audio file or acquire microphone (& ask permission) data stream and convert to voice
processing format (16kHz 16-bit linear PCM). The downsampled audio is
forwarded to the Koala engine. The audio <i>does not</i> leave the
browser: all processing is occurring via the Koala WebAssembly code.
</li>
<li>
Enhance audio real time using Koala engine. Output both original and enhanced
audio.
</li>
</ol>
After entering the AccessKey, click the "Start Koala" 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 Koala"
onclick="startKoala(document.getElementById('accessKey').value)"
/>
<hr/>
<div id="control" style="display: none">
<label for="audioFile">Choose audio file to enhance:</label>
<input type="file" id="audioFile" name="audioFile"/>
<p><b>OR</b></p>
<label for="recordAudio">Record audio to enhance (up to 2 minutes):</label>
<button id="recordAudio">Record Audio</button>
<span id="displayTimer" style="display: none;"></span>
<button id="stopRecord" style="display: none;">Stop Recording</button>
<hr/>
</div>
<div id="status"></div>
<br>
<div id="result" style="display: none">
<label>
Original
<input type="range" id="volumeControl" min="0" max="1" value="1" step="0.01" />
Koalafied
<br>
<br>
<button id="playAudio">Play</button>
</label>
</div>
<br>
</body>
</html>