Spaces:
Running
Running
Update index.html
Browse files- index.html +44 -9
index.html
CHANGED
@@ -203,9 +203,8 @@
|
|
203 |
const source = this.audioContext.createMediaStreamSource(this.stream);
|
204 |
this.processor = this.audioContext.createScriptProcessor(1024, 1, 1);
|
205 |
this.processor.onaudioprocess = (event) => {
|
206 |
-
// Always process audio, but at reduced gain when not speaking
|
207 |
const inputData = event.inputBuffer.getChannelData(0);
|
208 |
-
const gain = this.userIsSpeaking ? 1 : 0.2;
|
209 |
const processedData = inputData.map(sample => sample * gain);
|
210 |
this.onAudioProcess(processedData);
|
211 |
};
|
@@ -236,15 +235,30 @@
|
|
236 |
this.isPlaying = false;
|
237 |
this.onPlaybackStart = onPlaybackStart;
|
238 |
this.onPlaybackEnd = onPlaybackEnd;
|
|
|
|
|
239 |
|
240 |
this.scriptNode.onaudioprocess = (event) => {
|
241 |
const outputBuffer = event.outputBuffer.getChannelData(0);
|
242 |
-
|
|
|
243 |
if (!this.isPlaying) {
|
244 |
this.isPlaying = true;
|
245 |
this.onPlaybackStart();
|
246 |
}
|
247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
} else {
|
249 |
if (this.isPlaying) {
|
250 |
this.isPlaying = false;
|
@@ -265,15 +279,28 @@
|
|
265 |
stop() {
|
266 |
this.audioContext.suspend();
|
267 |
this.samples = [];
|
|
|
|
|
268 |
this.isPlaying = false;
|
269 |
}
|
270 |
|
271 |
addSamples(samples) {
|
272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
273 |
}
|
274 |
|
275 |
clear() {
|
276 |
this.samples = [];
|
|
|
|
|
277 |
this.isPlaying = false;
|
278 |
}
|
279 |
}
|
@@ -291,8 +318,6 @@
|
|
291 |
);
|
292 |
|
293 |
function processMicrophoneInput(audioData) {
|
294 |
-
// Here you would typically send this data to your server for processing
|
295 |
-
// For now, we'll just visualize it
|
296 |
updateVisualizer(audioData);
|
297 |
}
|
298 |
|
@@ -399,7 +424,7 @@
|
|
399 |
if (myvad) {
|
400 |
await myvad.destroy();
|
401 |
myvad = null;
|
402 |
-
|
403 |
streamer.stop();
|
404 |
playback.stop();
|
405 |
startButton.textContent = 'Begin Call';
|
@@ -413,7 +438,17 @@
|
|
413 |
logsDiv.innerHTML = '';
|
414 |
});
|
415 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
416 |
initializePipelines();
|
417 |
</script>
|
418 |
</body>
|
419 |
-
</html>
|
|
|
203 |
const source = this.audioContext.createMediaStreamSource(this.stream);
|
204 |
this.processor = this.audioContext.createScriptProcessor(1024, 1, 1);
|
205 |
this.processor.onaudioprocess = (event) => {
|
|
|
206 |
const inputData = event.inputBuffer.getChannelData(0);
|
207 |
+
const gain = this.userIsSpeaking ? 1 : 0.2;
|
208 |
const processedData = inputData.map(sample => sample * gain);
|
209 |
this.onAudioProcess(processedData);
|
210 |
};
|
|
|
235 |
this.isPlaying = false;
|
236 |
this.onPlaybackStart = onPlaybackStart;
|
237 |
this.onPlaybackEnd = onPlaybackEnd;
|
238 |
+
this.currentBuffer = new Float32Array(1024);
|
239 |
+
this.currentBufferIndex = 0;
|
240 |
|
241 |
this.scriptNode.onaudioprocess = (event) => {
|
242 |
const outputBuffer = event.outputBuffer.getChannelData(0);
|
243 |
+
|
244 |
+
if (this.samples.length > 0 || this.currentBufferIndex < this.currentBuffer.length) {
|
245 |
if (!this.isPlaying) {
|
246 |
this.isPlaying = true;
|
247 |
this.onPlaybackStart();
|
248 |
}
|
249 |
+
|
250 |
+
for (let i = 0; i < outputBuffer.length; i++) {
|
251 |
+
if (this.currentBufferIndex >= this.currentBuffer.length) {
|
252 |
+
if (this.samples.length > 0) {
|
253 |
+
this.currentBuffer = this.samples.shift();
|
254 |
+
this.currentBufferIndex = 0;
|
255 |
+
} else {
|
256 |
+
outputBuffer[i] = 0;
|
257 |
+
continue;
|
258 |
+
}
|
259 |
+
}
|
260 |
+
outputBuffer[i] = this.currentBuffer[this.currentBufferIndex++];
|
261 |
+
}
|
262 |
} else {
|
263 |
if (this.isPlaying) {
|
264 |
this.isPlaying = false;
|
|
|
279 |
stop() {
|
280 |
this.audioContext.suspend();
|
281 |
this.samples = [];
|
282 |
+
this.currentBuffer = new Float32Array(1024);
|
283 |
+
this.currentBufferIndex = 0;
|
284 |
this.isPlaying = false;
|
285 |
}
|
286 |
|
287 |
addSamples(samples) {
|
288 |
+
for (let i = 0; i < samples.length; i += 1024) {
|
289 |
+
const chunk = samples.slice(i, i + 1024);
|
290 |
+
if (chunk.length < 1024) {
|
291 |
+
const paddedChunk = new Float32Array(1024);
|
292 |
+
paddedChunk.set(chunk);
|
293 |
+
this.samples.push(paddedChunk);
|
294 |
+
} else {
|
295 |
+
this.samples.push(chunk);
|
296 |
+
}
|
297 |
+
}
|
298 |
}
|
299 |
|
300 |
clear() {
|
301 |
this.samples = [];
|
302 |
+
this.currentBuffer = new Float32Array(1024);
|
303 |
+
this.currentBufferIndex = 0;
|
304 |
this.isPlaying = false;
|
305 |
}
|
306 |
}
|
|
|
318 |
);
|
319 |
|
320 |
function processMicrophoneInput(audioData) {
|
|
|
|
|
321 |
updateVisualizer(audioData);
|
322 |
}
|
323 |
|
|
|
424 |
if (myvad) {
|
425 |
await myvad.destroy();
|
426 |
myvad = null;
|
427 |
+
}
|
428 |
streamer.stop();
|
429 |
playback.stop();
|
430 |
startButton.textContent = 'Begin Call';
|
|
|
438 |
logsDiv.innerHTML = '';
|
439 |
});
|
440 |
|
441 |
+
function createVisualizer() {
|
442 |
+
const barCount = 64;
|
443 |
+
for (let i = 0; i < barCount; i++) {
|
444 |
+
const bar = document.createElement('div');
|
445 |
+
bar.className = 'bar';
|
446 |
+
visualizer.appendChild(bar);
|
447 |
+
}
|
448 |
+
}
|
449 |
+
|
450 |
+
createVisualizer();
|
451 |
initializePipelines();
|
452 |
</script>
|
453 |
</body>
|
454 |
+
</html>
|