Asynchronous coroutine: Read text from the text buffer queue and send it at 200ms intervals after audio transmission begins.
()
| 995 | log("info", f"Send coroutine stopped, total frames sent: {frames_sent}") |
| 996 | |
| 997 | async def send_text_loop(): |
| 998 | """Asynchronous coroutine: Read text from the text buffer queue and send it at 200ms intervals after audio transmission begins.""" |
| 999 | nonlocal audio_send_started |
| 1000 | text_interval = 0.2 # 200ms per text |
| 1001 | texts_sent = 0 |
| 1002 | current_turn_started = False |
| 1003 | |
| 1004 | log("info", "Text send coroutine started") |
| 1005 | |
| 1006 | while not close: |
| 1007 | try: |
| 1008 | try: |
| 1009 | text = text_buffer_queue.get_nowait() |
| 1010 | except queue.Empty: |
| 1011 | if current_turn_started and not audio_send_started['flag']: |
| 1012 | current_turn_started = False |
| 1013 | log("info", "New turn detected, resetting text send state") |
| 1014 | await asyncio.sleep(0.05) |
| 1015 | continue |
| 1016 | |
| 1017 | if not current_turn_started: |
| 1018 | log("info", f"Waiting for audio to start before sending text: '{text[:20]}...'") |
| 1019 | while not close and not audio_send_started['flag']: |
| 1020 | await asyncio.sleep(0.05) |
| 1021 | |
| 1022 | if close: |
| 1023 | log("info", "Text send coroutine stopped while waiting for audio") |
| 1024 | break |
| 1025 | |
| 1026 | current_turn_started = True |
| 1027 | log("info", "Audio started, beginning text transmission at 200ms intervals") |
| 1028 | |
| 1029 | try: |
| 1030 | msg = b"\x02" + bytes(text, encoding="utf8") |
| 1031 | await ws.send_bytes(msg) |
| 1032 | texts_sent += 1 |
| 1033 | log("info", f"Sent buffered text #{texts_sent}: {text}") |
| 1034 | except Exception as send_err: |
| 1035 | log("error", f"Failed to send text '{text}': {send_err}") |
| 1036 | break |
| 1037 | |
| 1038 | await asyncio.sleep(text_interval) |
| 1039 | |
| 1040 | except Exception as e: |
| 1041 | log("error", f"Text send coroutine error: {e}") |
| 1042 | import traceback |
| 1043 | traceback.print_exc() |
| 1044 | |
| 1045 | log("info", f"Text send coroutine stopped, total texts sent: {texts_sent}") |
| 1046 | |
| 1047 | log("info", "accepted connection") |
| 1048 | close = False |