Spaces:
Running
Running
Update index.html
Browse files- index.html +18 -70
index.html
CHANGED
@@ -188,9 +188,6 @@
|
|
188 |
let bars;
|
189 |
let animationId;
|
190 |
let isListening = false;
|
191 |
-
let mediaStream;
|
192 |
-
let audioSource;
|
193 |
-
let playbackContext;
|
194 |
|
195 |
function createVisualizer() {
|
196 |
const barCount = 64;
|
@@ -236,7 +233,7 @@
|
|
236 |
const transcription = await sttPipeline(audio);
|
237 |
addLog(`User: ${transcription.text}`);
|
238 |
|
239 |
-
const botResponse = `I heard you say: "${transcription.text}"
|
240 |
addLog(`Bot: ${botResponse}`);
|
241 |
|
242 |
const speechOutput = await ttsPipeline(botResponse);
|
@@ -258,16 +255,18 @@
|
|
258 |
}
|
259 |
|
260 |
function playAudio(audioArray) {
|
261 |
-
const audioBuffer =
|
262 |
const channelData = audioBuffer.getChannelData(0);
|
263 |
channelData.set(audioArray);
|
264 |
|
265 |
-
const source =
|
266 |
source.buffer = audioBuffer;
|
267 |
-
source.connect(
|
|
|
268 |
source.start();
|
269 |
}
|
270 |
|
|
|
271 |
async function toggleListening() {
|
272 |
if (isListening) {
|
273 |
await stopListening();
|
@@ -275,6 +274,7 @@
|
|
275 |
await startListening();
|
276 |
}
|
277 |
}
|
|
|
278 |
|
279 |
async function startListening() {
|
280 |
try {
|
@@ -283,21 +283,6 @@
|
|
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');
|
@@ -307,16 +292,17 @@
|
|
307 |
addLog('--- vad: speech end');
|
308 |
cancelAnimationFrame(animationId);
|
309 |
processSpeech(audio);
|
310 |
-
},
|
311 |
-
onVADMisfire: () => {
|
312 |
-
addLog('--- vad: misfire (false positive)');
|
313 |
}
|
314 |
});
|
315 |
|
|
|
|
|
|
|
|
|
316 |
await myvad.start();
|
317 |
startButton.textContent = 'End Call';
|
318 |
isListening = true;
|
319 |
-
addLog('System: Listening...
|
320 |
} catch (error) {
|
321 |
console.error('Error starting VAD:', error);
|
322 |
addLog('System: Error starting voice detection. Please check your microphone and try again.');
|
@@ -325,51 +311,13 @@
|
|
325 |
|
326 |
async function stopListening() {
|
327 |
if (myvad) {
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
}
|
335 |
-
}
|
336 |
-
|
337 |
-
if (mediaStream) {
|
338 |
-
mediaStream.getTracks().forEach(track => {
|
339 |
-
track.stop();
|
340 |
-
addLog(`System: Audio track ${track.kind} stopped.`);
|
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();
|
352 |
-
addLog('System: Audio context closed.');
|
353 |
-
} catch (error) {
|
354 |
-
console.error('Error closing audio context:', error);
|
355 |
-
addLog('System: Error closing audio context.');
|
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;
|
372 |
-
addLog('System: Call ended. All audio resources released.');
|
373 |
}
|
374 |
|
375 |
startButton.addEventListener('click', toggleListening);
|
|
|
188 |
let bars;
|
189 |
let animationId;
|
190 |
let isListening = false;
|
|
|
|
|
|
|
191 |
|
192 |
function createVisualizer() {
|
193 |
const barCount = 64;
|
|
|
233 |
const transcription = await sttPipeline(audio);
|
234 |
addLog(`User: ${transcription.text}`);
|
235 |
|
236 |
+
const botResponse = `I heard you say: "${transcription.text}".`;
|
237 |
addLog(`Bot: ${botResponse}`);
|
238 |
|
239 |
const speechOutput = await ttsPipeline(botResponse);
|
|
|
255 |
}
|
256 |
|
257 |
function playAudio(audioArray) {
|
258 |
+
const audioBuffer = audioContext.createBuffer(1, audioArray.length, 16000);
|
259 |
const channelData = audioBuffer.getChannelData(0);
|
260 |
channelData.set(audioArray);
|
261 |
|
262 |
+
const source = audioContext.createBufferSource();
|
263 |
source.buffer = audioBuffer;
|
264 |
+
source.connect(analyser);
|
265 |
+
analyser.connect(audioContext.destination);
|
266 |
source.start();
|
267 |
}
|
268 |
|
269 |
+
|
270 |
async function toggleListening() {
|
271 |
if (isListening) {
|
272 |
await stopListening();
|
|
|
274 |
await startListening();
|
275 |
}
|
276 |
}
|
277 |
+
|
278 |
|
279 |
async function startListening() {
|
280 |
try {
|
|
|
283 |
analyser.fftSize = 128;
|
284 |
dataArray = new Uint8Array(analyser.frequencyBinCount);
|
285 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
myvad = await vad.MicVAD.new({
|
287 |
onSpeechStart: () => {
|
288 |
addLog('--- vad: speech start');
|
|
|
292 |
addLog('--- vad: speech end');
|
293 |
cancelAnimationFrame(animationId);
|
294 |
processSpeech(audio);
|
|
|
|
|
|
|
295 |
}
|
296 |
});
|
297 |
|
298 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
299 |
+
const source = audioContext.createMediaStreamSource(stream);
|
300 |
+
source.connect(analyser);
|
301 |
+
|
302 |
await myvad.start();
|
303 |
startButton.textContent = 'End Call';
|
304 |
isListening = true;
|
305 |
+
addLog('System: Listening...');
|
306 |
} catch (error) {
|
307 |
console.error('Error starting VAD:', error);
|
308 |
addLog('System: Error starting voice detection. Please check your microphone and try again.');
|
|
|
311 |
|
312 |
async function stopListening() {
|
313 |
if (myvad) {
|
314 |
+
await myvad.pause();
|
315 |
+
startButton.textContent = 'Begin Call';
|
316 |
+
isListening = false;
|
317 |
+
addLog('System: Stopped listening.');
|
318 |
+
cancelAnimationFrame(animationId);
|
319 |
+
addLog('websocket closed');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
320 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
321 |
}
|
322 |
|
323 |
startButton.addEventListener('click', toggleListening);
|