From 1dfa619243e4fbf73ff8968ce5d1daddf0d0ce0b Mon Sep 17 00:00:00 2001 From: lizhongxiang Date: Thu, 13 Mar 2025 15:30:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B5=81=E5=BC=8F=E6=B5=81=E7=95=85?= =?UTF-8?q?=E5=BA=A6=E9=97=AE=E9=A2=98=EF=BC=8C=E8=8B=B1=E6=96=87=E7=AC=A6?= =?UTF-8?q?=E5=8F=B7=E5=88=87=E5=88=86=EF=BC=8C=E7=A9=BA=E6=A0=BC=E5=88=87?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/connection.py | 2 +- .../core/handle/sendAudioHandle.py | 36 +++++++++++++------ .../core/providers/tts/fishspeech.py | 4 +-- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index eea0536e..fd7fe0d6 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -257,7 +257,7 @@ class ConnectionHandler: current_text = full_text[processed_chars:] # 从未处理的位置开始 # 查找最后一个有效标点 - punctuations = ("。", "?", "!", ";", ":") + punctuations = ("。", "?", "!", ";", ":", ".", "?", "!", ";", ":"," ") last_punct_pos = -1 for punct in punctuations: pos = current_text.rfind(punct) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index c19f1277..0aa6a49d 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -9,7 +9,8 @@ from core.utils.util import remove_punctuation_and_length, get_string_no_punctua TAG = __name__ logger = setup_logging() -async def sendAudioMessageStream(conn, audios_queue, text, text_index=0): + +async def sendAudioMessageStream(conn, audios_queue, text, text_index=0, llm_finish_task=False): # 发送句子开始消息 if text_index == conn.tts_first_text_index: logger.bind(tag=TAG).info(f"发送第一段语音: {text}") @@ -18,8 +19,10 @@ async def sendAudioMessageStream(conn, audios_queue, text, text_index=0): # 初始化流控参数 frame_duration = 60 # 毫秒 start_time = time.time() # 使用高精度计时器 + # 初始化流控参数 + frame_duration = 60 # 毫秒 + start_time_chunk = time.perf_counter() # 使用高精度计时器 play_position = 0 # 已播放的时长(毫秒) - time_out_stop = False while True: try: start_get_queue = time.time() @@ -45,13 +48,21 @@ async def sendAudioMessageStream(conn, audios_queue, text, text_index=0): break if audio_opus_datas: - queue_duration = time.time() - start_get_queue - last_duration = conn.tts_duration - queue_duration - if last_duration <= 0: - last_duration = 0 - conn.tts_duration = duration + last_duration for opus_packet in audio_opus_datas: + if conn.client_abort: + return + # 计算当前包的预期发送时间 + # 计算当前包的预期发送时间 + expected_time = start_time_chunk + (play_position / 1000) + current_time = time.perf_counter() + + # 等待直到预期时间 + delay = expected_time - current_time + if delay > 0: + await asyncio.sleep(delay) + logger.bind(tag=TAG).info(f'发送数据长度:{len(opus_packet)}') await conn.websocket.send(opus_packet) + play_position += frame_duration # 更新播放位置 start_time = time.time() # 更新获取数据的时间 except Exception as e: logger.bind(tag=TAG).error(f"发生错误: {e}") @@ -60,11 +71,16 @@ async def sendAudioMessageStream(conn, audios_queue, text, text_index=0): print(f'{text_index}-{conn.tts_last_text_index}') # 发送结束消息(如果是最后一个文本) + logger.bind(tag=TAG).info(f"{conn.llm_finish_task},{text_index},{conn.tts_last_text_index}") if conn.llm_finish_task and text_index == conn.tts_last_text_index: - if conn.tts_duration and conn.tts_duration > 0: - await asyncio.sleep(conn.tts_duration) + expected_time = start_time_chunk + (play_position / 1000) + current_time = time.perf_counter() + # 等待直到预期时间 + delay = expected_time - current_time + if delay > 0: + await asyncio.sleep(delay) await send_tts_message(conn, 'stop', None) - if await isLLMWantToFinish(text): + if conn.close_after_chat or "拜拜" in text or "再见" in text: await conn.close() diff --git a/main/xiaozhi-server/core/providers/tts/fishspeech.py b/main/xiaozhi-server/core/providers/tts/fishspeech.py index d9fd0442..632af436 100644 --- a/main/xiaozhi-server/core/providers/tts/fishspeech.py +++ b/main/xiaozhi-server/core/providers/tts/fishspeech.py @@ -230,7 +230,7 @@ class TTSProvider(TTSProviderBase): if (len(audio_raw) % 1920) > 0: duration += 60 duration = duration / 1000.0 - logger.bind(tag=TAG).info(f'发送数据长度:{len(audio_raw)}') + # logger.bind(tag=TAG).info(f'发送数据长度:{len(audio_raw)}') opus_datas = self.wav_to_opus_data_audio_raw(audio_raw) queue.put({ "data": opus_datas, @@ -248,7 +248,7 @@ class TTSProvider(TTSProviderBase): duration += 60 duration = duration / 1000.0 # 把 audio 转成 opus - logger.bind(tag=TAG).info(f'发送数据长度:{len(audio_raw)}') + # logger.bind(tag=TAG).info(f'发送数据长度:{len(audio_raw)}') opus_datas = self.wav_to_opus_data_audio_raw(audio_raw) queue.put({ "data": opus_datas,