Spaces:
Running
Running
Update websocket_handler.py
Browse files- websocket_handler.py +6 -3
websocket_handler.py
CHANGED
@@ -608,14 +608,17 @@ async def generate_and_stream_tts(
|
|
608 |
"to": "playing_audio"
|
609 |
})
|
610 |
|
611 |
-
# Convert audio to base64 for transmission
|
|
|
612 |
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
|
613 |
|
614 |
# Stream audio in chunks
|
615 |
-
chunk_size =
|
616 |
total_length = len(audio_base64)
|
617 |
total_chunks = (total_length + chunk_size - 1) // chunk_size
|
618 |
|
|
|
|
|
619 |
for i in range(0, total_length, chunk_size):
|
620 |
# Check for cancellation
|
621 |
if asyncio.current_task().cancelled():
|
@@ -630,7 +633,7 @@ async def generate_and_stream_tts(
|
|
630 |
"chunk_index": chunk_index,
|
631 |
"total_chunks": total_chunks,
|
632 |
"is_last": chunk_index == total_chunks - 1,
|
633 |
-
"mime_type": "audio/mpeg"
|
634 |
})
|
635 |
|
636 |
# Small delay to prevent overwhelming the client
|
|
|
608 |
"to": "playing_audio"
|
609 |
})
|
610 |
|
611 |
+
# Convert entire audio to base64 for transmission
|
612 |
+
import base64
|
613 |
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
|
614 |
|
615 |
# Stream audio in chunks
|
616 |
+
chunk_size = 16384 # Larger chunk size for base64
|
617 |
total_length = len(audio_base64)
|
618 |
total_chunks = (total_length + chunk_size - 1) // chunk_size
|
619 |
|
620 |
+
log_info(f"Streaming TTS audio: {len(audio_data)} bytes as {total_length} base64 chars in {total_chunks} chunks")
|
621 |
+
|
622 |
for i in range(0, total_length, chunk_size):
|
623 |
# Check for cancellation
|
624 |
if asyncio.current_task().cancelled():
|
|
|
633 |
"chunk_index": chunk_index,
|
634 |
"total_chunks": total_chunks,
|
635 |
"is_last": chunk_index == total_chunks - 1,
|
636 |
+
"mime_type": "audio/mpeg"
|
637 |
})
|
638 |
|
639 |
# Small delay to prevent overwhelming the client
|