Update static/index.html
Browse files- static/index.html +53 -63
static/index.html
CHANGED
@@ -43,75 +43,65 @@
|
|
43 |
let socket;
|
44 |
let audioChunks = [];
|
45 |
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
try {
|
48 |
-
const
|
49 |
-
audio: {
|
50 |
-
sampleRate: 16000,
|
51 |
-
channelCount: 1,
|
52 |
-
echoCancellation: true,
|
53 |
-
noiseSuppression: true
|
54 |
-
}
|
55 |
-
});
|
56 |
-
|
57 |
-
// Initialize WebSocket connection
|
58 |
-
socket = new WebSocket(`wss://${window.location.host}/ws`);
|
59 |
-
|
60 |
-
|
61 |
-
socket.onclose = () => {
|
62 |
-
document.getElementById('status').textContent = 'Reconnecting...';
|
63 |
-
setTimeout(initializeAudio, 1000); // Attempt reconnection
|
64 |
-
};
|
65 |
-
|
66 |
-
socket.onmessage = (event) => {
|
67 |
-
const data = JSON.parse(event.data);
|
68 |
-
|
69 |
-
if (data.user_text) {
|
70 |
-
addMessage('user', data.user_text);
|
71 |
-
addMessage('assistant', `German: ${data.response_de}\nEnglish: ${data.response_en}`);
|
72 |
-
}
|
73 |
-
|
74 |
-
if (data.audio) {
|
75 |
-
// Convert base64 audio to ArrayBuffer and play it
|
76 |
-
const audio = new Audio(URL.createObjectURL(
|
77 |
-
new Blob([new Uint8Array(atob(data.audio).split('').map(c => c.charCodeAt(0)))],
|
78 |
-
{ type: 'audio/wav' })
|
79 |
-
));
|
80 |
-
audio.play();
|
81 |
-
}
|
82 |
-
};
|
83 |
-
|
84 |
-
// Initialize MediaRecorder
|
85 |
-
mediaRecorder = new MediaRecorder(stream);
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
// Send chunks to server
|
93 |
-
if (audioChunks.length > 0) {
|
94 |
-
const audioBlob = new Blob(audioChunks);
|
95 |
-
audioBlob.arrayBuffer().then(buffer => {
|
96 |
-
if (socket.readyState === WebSocket.OPEN) {
|
97 |
-
socket.send(buffer);
|
98 |
-
}
|
99 |
-
});
|
100 |
-
audioChunks = [];
|
101 |
-
}
|
102 |
-
};
|
103 |
-
|
104 |
-
// Start recording in chunks
|
105 |
-
mediaRecorder.start(2000); // Send 2 seconds of audio at a time
|
106 |
-
|
107 |
-
document.getElementById('status').textContent = 'Listening... (Say "Computer" to activate)';
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
} catch (error) {
|
110 |
-
console.error('Error
|
111 |
-
document.getElementById('status').textContent = 'Error initializing audio';
|
112 |
}
|
113 |
-
}
|
114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
function addMessage(sender, text) {
|
116 |
const conversation = document.getElementById('conversation');
|
117 |
const message = document.createElement('div');
|
|
|
43 |
let socket;
|
44 |
let audioChunks = [];
|
45 |
|
46 |
+
function initializeAudio() {
|
47 |
+
function connect() {
|
48 |
+
socket = new WebSocket(`wss://${window.location.host}/ws`);
|
49 |
+
|
50 |
+
socket.onopen = () => {
|
51 |
+
document.getElementById('status').textContent = 'Connected. Listening... (Say "Computer" to activate)';
|
52 |
+
};
|
53 |
+
|
54 |
+
socket.onmessage = (event) => {
|
55 |
try {
|
56 |
+
const data = JSON.parse(event.data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
if (data.user_text) {
|
59 |
+
addMessage('user', data.user_text);
|
60 |
+
addMessage('assistant', `German: ${data.response_de}\nEnglish: ${data.response_en}`);
|
61 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
if (data.audio) {
|
64 |
+
// Convert base64 audio to ArrayBuffer and play it
|
65 |
+
const audio = new Audio(URL.createObjectURL(
|
66 |
+
new Blob([new Uint8Array(atob(data.audio).split('').map(c => c.charCodeAt(0)))],
|
67 |
+
{ type: 'audio/wav' })
|
68 |
+
));
|
69 |
+
audio.play();
|
70 |
+
}
|
71 |
} catch (error) {
|
72 |
+
console.error('Error parsing WebSocket message:', error);
|
|
|
73 |
}
|
74 |
+
};
|
75 |
|
76 |
+
socket.onclose = (event) => {
|
77 |
+
document.getElementById('status').textContent = 'Disconnected. Reconnecting...';
|
78 |
+
setTimeout(connect, 1000); // Reconnect after 1 second
|
79 |
+
};
|
80 |
+
|
81 |
+
socket.onerror = (error) => {
|
82 |
+
console.error('WebSocket error:', error);
|
83 |
+
document.getElementById('status').textContent = 'Connection error';
|
84 |
+
};
|
85 |
+
}
|
86 |
+
|
87 |
+
connect();
|
88 |
+
|
89 |
+
// Your existing audio initialization code
|
90 |
+
mediaRecorder.ondataavailable = (event) => {
|
91 |
+
if (event.data.size > 0) {
|
92 |
+
const reader = new FileReader();
|
93 |
+
reader.onloadend = () => {
|
94 |
+
// Ensure we're sending 16-bit PCM audio
|
95 |
+
if (socket.readyState === WebSocket.OPEN) {
|
96 |
+
socket.send(reader.result);
|
97 |
+
}
|
98 |
+
};
|
99 |
+
reader.readAsArrayBuffer(event.data);
|
100 |
+
}
|
101 |
+
};
|
102 |
+
|
103 |
+
mediaRecorder.start(480); // 30ms chunks for VAD
|
104 |
+
}
|
105 |
function addMessage(sender, text) {
|
106 |
const conversation = document.getElementById('conversation');
|
107 |
const message = document.createElement('div');
|