ciyidogan commited on
Commit
f0b5234
·
verified ·
1 Parent(s): 4216c57

Update websocket_handler.py

Browse files
Files changed (1) hide show
  1. websocket_handler.py +15 -2
websocket_handler.py CHANGED
@@ -600,6 +600,10 @@ async def generate_and_stream_tts(
600
  # Generate audio
601
  audio_data = await tts_provider.synthesize(text)
602
 
 
 
 
 
603
  # Change state to playing
604
  await session.change_state(ConversationState.PLAYING_AUDIO)
605
  await websocket.send_json({
@@ -620,13 +624,21 @@ async def generate_and_stream_tts(
620
  chunk = audio_data[i:i + chunk_size]
621
  chunk_index = i // chunk_size
622
 
 
 
 
 
 
 
 
 
623
  await websocket.send_json({
624
  "type": "tts_audio",
625
- "data": base64.b64encode(chunk).decode('utf-8'),
626
  "chunk_index": chunk_index,
627
  "total_chunks": total_chunks,
628
  "is_last": chunk_index == total_chunks - 1,
629
- "mime_type": "audio/mpeg" # MP3 format for ElevenLabs
630
  })
631
 
632
  # Small delay to prevent overwhelming the client
@@ -654,6 +666,7 @@ async def generate_and_stream_tts(
654
  log_error(
655
  f"TTS generation error",
656
  error=str(e),
 
657
  session_id=session.session.session_id
658
  )
659
  await websocket.send_json({
 
600
  # Generate audio
601
  audio_data = await tts_provider.synthesize(text)
602
 
603
+ # Debug log to check audio data
604
+ log_debug(f"Audio data type: {type(audio_data)}, length: {len(audio_data)}")
605
+ log_debug(f"First 10 bytes: {audio_data[:10]}")
606
+
607
  # Change state to playing
608
  await session.change_state(ConversationState.PLAYING_AUDIO)
609
  await websocket.send_json({
 
624
  chunk = audio_data[i:i + chunk_size]
625
  chunk_index = i // chunk_size
626
 
627
+ # IMPORTANT: Ensure chunk is bytes before encoding
628
+ if isinstance(chunk, str):
629
+ # If chunk is already a string, it might be base64 already
630
+ chunk_base64 = chunk
631
+ else:
632
+ # Convert bytes to base64
633
+ chunk_base64 = base64.b64encode(chunk).decode('utf-8')
634
+
635
  await websocket.send_json({
636
  "type": "tts_audio",
637
+ "data": chunk_base64,
638
  "chunk_index": chunk_index,
639
  "total_chunks": total_chunks,
640
  "is_last": chunk_index == total_chunks - 1,
641
+ "mime_type": "audio/mpeg"
642
  })
643
 
644
  # Small delay to prevent overwhelming the client
 
666
  log_error(
667
  f"TTS generation error",
668
  error=str(e),
669
+ traceback=traceback.format_exc(),
670
  session_id=session.session.session_id
671
  )
672
  await websocket.send_json({