From b267b3230aa2d8ba4eb1369c82c6185906b1f4a1 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Tue, 11 Feb 2025 13:51:44 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BC=98=E5=8C=96stt=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/handle/audioHandle.py | 45 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/core/handle/audioHandle.py b/core/handle/audioHandle.py index 4e55d115..7217178c 100644 --- a/core/handle/audioHandle.py +++ b/core/handle/audioHandle.py @@ -36,19 +36,10 @@ async def handleAudioMessage(conn, audio): conn.reset_vad_states() async def startToChat(conn, text): - stt_text = get_string_no_punctuation_or_emoji(text) - await conn.websocket.send(json.dumps({ - "type": "stt", - "text": stt_text, - "session_id": conn.session_id} - )) - await conn.websocket.send( - json.dumps({ - "type": "llm", - "text": "😊", - "emotion": "happy", - "session_id": conn.session_id} - )) + # 异步发送 stt 信息 + asyncio.create_task( + schedule_with_interrupt(0, send_stt_message(conn, text)) + ) conn.executor.submit(conn.chat, text) @@ -59,10 +50,6 @@ async def sendAudioMessage(conn, audios, duration, text): if text == conn.tts_first_text: logger.info(f"发送第一段语音: {text}") conn.tts_start_speak_time = time.time() - await send_tts_message(conn, "start", text) - - # 先等待0.5秒,确保客户端处于 speaking 状态 - await asyncio.sleep(0.5) # 发送 sentence_start(每个音频文件之前发送一次) await send_tts_message(conn, "sentence_start", text) @@ -78,15 +65,12 @@ async def sendAudioMessage(conn, audios, duration, text): await send_tts_message(conn, "sentence_end", text) if conn.llm_finish_task and text == conn.tts_last_text: - stop_duration = conn.tts_duration - \ - (time.time() - conn.tts_start_speak_time) + stop_duration = conn.tts_duration - (time.time() - conn.tts_start_speak_time) stop_task = asyncio.create_task( - schedule_with_interrupt( - stop_duration, send_tts_message(conn, 'stop')) + schedule_with_interrupt(stop_duration, send_tts_message(conn, 'stop')) ) conn.scheduled_tasks.append(stop_task) - async def send_tts_message(conn, state, text=None): """发送 TTS 状态消息""" message = { @@ -101,6 +85,23 @@ async def send_tts_message(conn, state, text=None): if state == "stop": conn.clearSpeakStatus() +async def send_stt_message(conn, text): + """发送 STT 状态消息""" + stt_text = get_string_no_punctuation_or_emoji(text) + await conn.websocket.send(json.dumps({ + "type": "stt", + "text": stt_text, + "session_id": conn.session_id} + )) + await conn.websocket.send( + json.dumps({ + "type": "llm", + "text": "😊", + "emotion": "happy", + "session_id": conn.session_id} + )) + await send_tts_message(conn, "start") + async def schedule_with_interrupt(delay, coro): """可中断的延迟调度"""