if websocket connection fails, frontend does not allow recording
Browse files- src/live_transcription.html +62 -46
src/live_transcription.html
CHANGED
|
@@ -101,11 +101,11 @@
|
|
| 101 |
<div id="transcriptions"></div>
|
| 102 |
|
| 103 |
<script>
|
| 104 |
-
let isRecording = false
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
// Tracks whether the user voluntarily closed the WebSocket
|
| 111 |
let userClosing = false;
|
|
@@ -137,46 +137,56 @@
|
|
| 137 |
statusText.textContent = "WebSocket URL updated. Ready to connect.";
|
| 138 |
});
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
function setupWebSocket() {
|
| 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 |
async function startRecording() {
|
|
@@ -200,12 +210,18 @@
|
|
| 200 |
function stopRecording() {
|
| 201 |
userClosing = true;
|
| 202 |
|
| 203 |
-
recorder
|
| 204 |
-
recorder
|
|
|
|
|
|
|
|
|
|
| 205 |
isRecording = false;
|
| 206 |
|
| 207 |
-
websocket
|
| 208 |
-
websocket
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
updateUI();
|
| 211 |
}
|
|
@@ -216,12 +232,12 @@
|
|
| 216 |
transcriptionsDiv.innerHTML = "";
|
| 217 |
|
| 218 |
try {
|
| 219 |
-
setupWebSocket();
|
|
|
|
| 220 |
} catch (err) {
|
| 221 |
-
|
|
|
|
| 222 |
}
|
| 223 |
-
|
| 224 |
-
await startRecording();
|
| 225 |
} else {
|
| 226 |
stopRecording();
|
| 227 |
}
|
|
|
|
| 101 |
<div id="transcriptions"></div>
|
| 102 |
|
| 103 |
<script>
|
| 104 |
+
let isRecording = false;
|
| 105 |
+
let websocket = null;
|
| 106 |
+
let recorder = null;
|
| 107 |
+
let chunkDuration = 1000;
|
| 108 |
+
let websocketUrl = "ws://localhost:8000/asr";
|
| 109 |
|
| 110 |
// Tracks whether the user voluntarily closed the WebSocket
|
| 111 |
let userClosing = false;
|
|
|
|
| 137 |
statusText.textContent = "WebSocket URL updated. Ready to connect.";
|
| 138 |
});
|
| 139 |
|
| 140 |
+
/**
|
| 141 |
+
* Opens webSocket connection.
|
| 142 |
+
* returns a Promise that resolves when the connection is open.
|
| 143 |
+
* rejects if there was an error.
|
| 144 |
+
*/
|
| 145 |
function setupWebSocket() {
|
| 146 |
+
return new Promise((resolve, reject) => {
|
| 147 |
+
try {
|
| 148 |
+
websocket = new WebSocket(websocketUrl);
|
| 149 |
+
} catch (error) {
|
| 150 |
+
statusText.textContent =
|
| 151 |
+
"Invalid WebSocket URL. Please check the URL and try again.";
|
| 152 |
+
reject(error);
|
| 153 |
+
return;
|
| 154 |
+
}
|
| 155 |
|
| 156 |
+
websocket.onopen = () => {
|
| 157 |
+
statusText.textContent = "Connected to server";
|
| 158 |
+
resolve();
|
| 159 |
+
};
|
| 160 |
|
| 161 |
+
websocket.onclose = (event) => {
|
| 162 |
+
// If we manually closed it, we say so
|
| 163 |
+
if (userClosing) {
|
| 164 |
+
statusText.textContent = "WebSocket closed by user.";
|
| 165 |
+
} else {
|
| 166 |
+
statusText.textContent = "Disconnected from the websocket server. If this is the first launch, the model may be downloading in the backend. Check the API logs for more information.";
|
| 167 |
+
}
|
| 168 |
+
userClosing = false;
|
| 169 |
+
};
|
| 170 |
|
| 171 |
+
websocket.onerror = () => {
|
| 172 |
+
statusText.textContent = "Error connecting to WebSocket";
|
| 173 |
+
reject(new Error("Error connecting to WebSocket"));
|
| 174 |
+
};
|
| 175 |
|
| 176 |
+
websocket.onmessage = (event) => {
|
| 177 |
+
const data = JSON.parse(event.data);
|
| 178 |
+
const { transcription, buffer } = data;
|
| 179 |
|
| 180 |
+
// Update confirmed transcription
|
| 181 |
+
fullTranscription += transcription;
|
| 182 |
|
| 183 |
+
// Update the transcription display
|
| 184 |
+
transcriptionsDiv.innerHTML = `
|
| 185 |
+
<span class="transcription">${fullTranscription}</span>
|
| 186 |
+
<span class="buffer">${buffer}</span>
|
| 187 |
+
`;
|
| 188 |
+
};
|
| 189 |
+
});
|
| 190 |
}
|
| 191 |
|
| 192 |
async function startRecording() {
|
|
|
|
| 210 |
function stopRecording() {
|
| 211 |
userClosing = true;
|
| 212 |
|
| 213 |
+
// Stop the recorder if it exists
|
| 214 |
+
if (recorder) {
|
| 215 |
+
recorder.stop();
|
| 216 |
+
recorder = null;
|
| 217 |
+
}
|
| 218 |
isRecording = false;
|
| 219 |
|
| 220 |
+
// Close the websocket if it exists
|
| 221 |
+
if (websocket) {
|
| 222 |
+
websocket.close();
|
| 223 |
+
websocket = null;
|
| 224 |
+
}
|
| 225 |
|
| 226 |
updateUI();
|
| 227 |
}
|
|
|
|
| 232 |
transcriptionsDiv.innerHTML = "";
|
| 233 |
|
| 234 |
try {
|
| 235 |
+
await setupWebSocket();
|
| 236 |
+
await startRecording();
|
| 237 |
} catch (err) {
|
| 238 |
+
statusText.textContent =
|
| 239 |
+
"Could not connect to WebSocket or access mic. Recording aborted.";
|
| 240 |
}
|
|
|
|
|
|
|
| 241 |
} else {
|
| 242 |
stopRecording();
|
| 243 |
}
|