Spaces:
Runtime error
Runtime error
Commit
·
4ae7ed6
1
Parent(s):
b809819
WS: support binary finish marker; client sends 0xFF.. marker (client only sends binary chunks)
Browse files
app.py
CHANGED
|
@@ -112,8 +112,14 @@ async def websocket_audio(websocket: WebSocket):
|
|
| 112 |
# Unknown text message - ignore or log
|
| 113 |
logger.debug(f"Unknown WS text message: {data}")
|
| 114 |
else:
|
| 115 |
-
# Binary audio chunk
|
| 116 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
result = await asyncio.get_event_loop().run_in_executor(None, server_wrapper.process_chunk_from_bytes, data)
|
| 118 |
if result and result.get("text"):
|
| 119 |
await websocket.send_json({"type": "update", "text": result["text"]})
|
|
@@ -338,9 +344,9 @@ function stopRecording() {
|
|
| 338 |
if (processor) processor.disconnect();
|
| 339 |
if (source) source.disconnect();
|
| 340 |
|
| 341 |
-
// Send finish signal (
|
| 342 |
if (ws && ws.readyState === WebSocket.OPEN) {
|
| 343 |
-
ws.send(
|
| 344 |
setTimeout(() => {
|
| 345 |
if (ws) ws.close();
|
| 346 |
}, 500);
|
|
|
|
| 112 |
# Unknown text message - ignore or log
|
| 113 |
logger.debug(f"Unknown WS text message: {data}")
|
| 114 |
else:
|
| 115 |
+
# Binary audio chunk (or binary control marker)
|
| 116 |
try:
|
| 117 |
+
# If client sent the 4-byte control marker 0xFF 0xFF 0xFF 0xFF, treat as FINISH
|
| 118 |
+
if isinstance(data, (bytes, bytearray)) and data == b"\xFF\xFF\xFF\xFF":
|
| 119 |
+
result = await asyncio.get_event_loop().run_in_executor(None, server_wrapper.finish)
|
| 120 |
+
await websocket.send_json({"type": "finish", **(result or {})})
|
| 121 |
+
break
|
| 122 |
+
|
| 123 |
result = await asyncio.get_event_loop().run_in_executor(None, server_wrapper.process_chunk_from_bytes, data)
|
| 124 |
if result and result.get("text"):
|
| 125 |
await websocket.send_json({"type": "update", "text": result["text"]})
|
|
|
|
| 344 |
if (processor) processor.disconnect();
|
| 345 |
if (source) source.disconnect();
|
| 346 |
|
| 347 |
+
// Send finish signal (binary marker) so server recognizes it
|
| 348 |
if (ws && ws.readyState === WebSocket.OPEN) {
|
| 349 |
+
ws.send(new Uint8Array([0xFF, 0xFF, 0xFF, 0xFF]));
|
| 350 |
setTimeout(() => {
|
| 351 |
if (ws) ws.close();
|
| 352 |
}, 500);
|