Spaces:
Running
Running
Update index.html
Browse files- index.html +27 -9
index.html
CHANGED
|
@@ -190,6 +190,7 @@
|
|
| 190 |
let isListening = false;
|
| 191 |
let mediaStream;
|
| 192 |
let audioSource;
|
|
|
|
| 193 |
|
| 194 |
function createVisualizer() {
|
| 195 |
const barCount = 64;
|
|
@@ -257,14 +258,13 @@
|
|
| 257 |
}
|
| 258 |
|
| 259 |
function playAudio(audioArray) {
|
| 260 |
-
const audioBuffer =
|
| 261 |
const channelData = audioBuffer.getChannelData(0);
|
| 262 |
channelData.set(audioArray);
|
| 263 |
|
| 264 |
-
const source =
|
| 265 |
source.buffer = audioBuffer;
|
| 266 |
-
source.connect(
|
| 267 |
-
analyser.connect(audioContext.destination);
|
| 268 |
source.start();
|
| 269 |
}
|
| 270 |
|
|
@@ -283,10 +283,21 @@
|
|
| 283 |
analyser.fftSize = 128;
|
| 284 |
dataArray = new Uint8Array(analyser.frequencyBinCount);
|
| 285 |
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
audioSource = audioContext.createMediaStreamSource(mediaStream);
|
| 288 |
audioSource.connect(analyser);
|
| 289 |
|
|
|
|
|
|
|
|
|
|
| 290 |
myvad = await vad.MicVAD.new({
|
| 291 |
onSpeechStart: () => {
|
| 292 |
addLog('--- vad: speech start');
|
|
@@ -315,7 +326,7 @@
|
|
| 315 |
async function stopListening() {
|
| 316 |
if (myvad) {
|
| 317 |
try {
|
| 318 |
-
await myvad.stop();
|
| 319 |
addLog('System: VAD stopped.');
|
| 320 |
} catch (error) {
|
| 321 |
console.error('Error stopping VAD:', error);
|
|
@@ -323,7 +334,6 @@
|
|
| 323 |
}
|
| 324 |
}
|
| 325 |
|
| 326 |
-
// Stop all tracks in the media stream
|
| 327 |
if (mediaStream) {
|
| 328 |
mediaStream.getTracks().forEach(track => {
|
| 329 |
track.stop();
|
|
@@ -331,13 +341,11 @@
|
|
| 331 |
});
|
| 332 |
}
|
| 333 |
|
| 334 |
-
// Disconnect the audio source from the analyser
|
| 335 |
if (audioSource) {
|
| 336 |
audioSource.disconnect();
|
| 337 |
addLog('System: Audio source disconnected.');
|
| 338 |
}
|
| 339 |
|
| 340 |
-
// Close the audio context
|
| 341 |
if (audioContext) {
|
| 342 |
try {
|
| 343 |
await audioContext.close();
|
|
@@ -348,6 +356,16 @@
|
|
| 348 |
}
|
| 349 |
}
|
| 350 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
cancelAnimationFrame(animationId);
|
| 352 |
startButton.textContent = 'Begin Call';
|
| 353 |
isListening = false;
|
|
|
|
| 190 |
let isListening = false;
|
| 191 |
let mediaStream;
|
| 192 |
let audioSource;
|
| 193 |
+
let playbackContext;
|
| 194 |
|
| 195 |
function createVisualizer() {
|
| 196 |
const barCount = 64;
|
|
|
|
| 258 |
}
|
| 259 |
|
| 260 |
function playAudio(audioArray) {
|
| 261 |
+
const audioBuffer = playbackContext.createBuffer(1, audioArray.length, 16000);
|
| 262 |
const channelData = audioBuffer.getChannelData(0);
|
| 263 |
channelData.set(audioArray);
|
| 264 |
|
| 265 |
+
const source = playbackContext.createBufferSource();
|
| 266 |
source.buffer = audioBuffer;
|
| 267 |
+
source.connect(playbackContext.destination);
|
|
|
|
| 268 |
source.start();
|
| 269 |
}
|
| 270 |
|
|
|
|
| 283 |
analyser.fftSize = 128;
|
| 284 |
dataArray = new Uint8Array(analyser.frequencyBinCount);
|
| 285 |
|
| 286 |
+
const constraints = {
|
| 287 |
+
audio: {
|
| 288 |
+
echoCancellation: true,
|
| 289 |
+
noiseSuppression: true,
|
| 290 |
+
autoGainControl: true
|
| 291 |
+
}
|
| 292 |
+
};
|
| 293 |
+
|
| 294 |
+
mediaStream = await navigator.mediaDevices.getUserMedia(constraints);
|
| 295 |
audioSource = audioContext.createMediaStreamSource(mediaStream);
|
| 296 |
audioSource.connect(analyser);
|
| 297 |
|
| 298 |
+
// Create a separate context for playback
|
| 299 |
+
playbackContext = new (window.AudioContext || window.webkitAudioContext)();
|
| 300 |
+
|
| 301 |
myvad = await vad.MicVAD.new({
|
| 302 |
onSpeechStart: () => {
|
| 303 |
addLog('--- vad: speech start');
|
|
|
|
| 326 |
async function stopListening() {
|
| 327 |
if (myvad) {
|
| 328 |
try {
|
| 329 |
+
await myvad.stop();
|
| 330 |
addLog('System: VAD stopped.');
|
| 331 |
} catch (error) {
|
| 332 |
console.error('Error stopping VAD:', error);
|
|
|
|
| 334 |
}
|
| 335 |
}
|
| 336 |
|
|
|
|
| 337 |
if (mediaStream) {
|
| 338 |
mediaStream.getTracks().forEach(track => {
|
| 339 |
track.stop();
|
|
|
|
| 341 |
});
|
| 342 |
}
|
| 343 |
|
|
|
|
| 344 |
if (audioSource) {
|
| 345 |
audioSource.disconnect();
|
| 346 |
addLog('System: Audio source disconnected.');
|
| 347 |
}
|
| 348 |
|
|
|
|
| 349 |
if (audioContext) {
|
| 350 |
try {
|
| 351 |
await audioContext.close();
|
|
|
|
| 356 |
}
|
| 357 |
}
|
| 358 |
|
| 359 |
+
if (playbackContext) {
|
| 360 |
+
try {
|
| 361 |
+
await playbackContext.close();
|
| 362 |
+
addLog('System: Playback context closed.');
|
| 363 |
+
} catch (error) {
|
| 364 |
+
console.error('Error closing playback context:', error);
|
| 365 |
+
addLog('System: Error closing playback context.');
|
| 366 |
+
}
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
cancelAnimationFrame(animationId);
|
| 370 |
startButton.textContent = 'Begin Call';
|
| 371 |
isListening = false;
|