From 197874b0698bb38ce876ea84bc2c8bdaf7a3c35b Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Tue, 4 Mar 2025 00:23:16 +0800 Subject: [PATCH] =?UTF-8?q?update:=E5=8E=BB=E6=8E=89=E4=BA=86=E6=97=A0?= =?UTF-8?q?=E7=94=A8=E5=8F=98=E9=87=8F=EF=BC=8C=E4=BF=AE=E5=A4=8Dcontinueb?= =?UTF-8?q?ug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + core/connection.py | 51 ++++++++++++---------------------- core/handle/musicHandler.py | 2 -- core/handle/sendAudioHandle.py | 1 - 4 files changed, 18 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index c09248c59..cff679fb4 100644 --- a/.gitignore +++ b/.gitignore @@ -146,3 +146,4 @@ tmp .private_config.yaml .env.development docker-compose.yml +web/vue/node_modules diff --git a/core/connection.py b/core/connection.py index 3d7a98132..8b6d12854 100644 --- a/core/connection.py +++ b/core/connection.py @@ -21,9 +21,11 @@ from core.utils.auth_code_gen import AuthCodeGenerator TAG = __name__ + class TTSException(RuntimeError): pass + class ConnectionHandler: def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts, _music): self.config = config @@ -70,12 +72,8 @@ class ConnectionHandler: self.dialogue = Dialogue() # tts相关变量 - self.tts_first_text = None self.tts_first_text_index = -1 - self.tts_last_text = None self.tts_last_text_index = -1 - self.tts_start_speak_time = None - self.tts_duration = 0 # iot相关变量 self.iot_descriptors = {} @@ -239,7 +237,7 @@ class ConnectionHandler: current_text = full_text[processed_chars:] # 从未处理的位置开始 # 查找最后一个有效标点 - punctuations = ("。", "?", "!", "?", "!", ";", ";", ":", ":", ",") + punctuations = ("。", "?", "!", "?", "!", ";", ";", ":", ":") last_punct_pos = -1 for punct in punctuations: pos = current_text.rfind(punct) @@ -284,38 +282,28 @@ class ConnectionHandler: if future is None: continue text = None + opus_datas, text_index, tts_file = [], 0, None try: self.logger.bind(tag=TAG).debug("正在处理TTS任务...") tts_file, text, text_index = future.result(timeout=10) if text is None or len(text) <= 0: - raise TTSException(f"{text_index}: tts text is empty") - continue - if tts_file is None: - self.logger.bind(tag=TAG).error(f"TTS文件生成失败: {text_index}: {text}") - raise TTSException(f"{text_index}: tts file is empty") - continue - self.logger.bind(tag=TAG).debug(f"TTS文件生成完毕,文件路径: {tts_file}") - if os.path.exists(tts_file): - opus_datas, duration = self.tts.wav_to_opus_data(tts_file) + self.logger.bind(tag=TAG).error(f"TTS出错:{text_index}: tts text is empty") + elif tts_file is None: + self.logger.bind(tag=TAG).error(f"TTS出错: file is empty: {text_index}: {text}") else: - self.logger.bind(tag=TAG).error(f"TTS文件不存在: {tts_file}") - opus_datas = [] + self.logger.bind(tag=TAG).debug(f"TTS生成:文件路径: {tts_file}") + if os.path.exists(tts_file): + opus_datas, duration = self.tts.wav_to_opus_data(tts_file) + else: + self.logger.bind(tag=TAG).error(f"TTS出错:文件不存在{tts_file}") except TimeoutError: - self.logger.bind(tag=TAG).error("TTS 任务超时") - raise TTSException("tts timeout") - continue + self.logger.bind(tag=TAG).error("TTS超时") except Exception as e: - self.logger.bind(tag=TAG).error(f"TTS 任务出错: {e}") - if self.llm_finish_task and self.tts_last_text == text and self.tts_last_text_index == text_index: - # raise TTSException("tts error, but need stop") - self.logger.bind(tag=TAG).debug(f"TTS 任务出错: 但需要正常停止") - opus_datas, duration = [], 0 - else: - continue + self.logger.bind(tag=TAG).error(f"TTS出错: {e}") if not self.client_abort: # 如果没有中途打断就发送语音 self.audio_play_queue.put((opus_datas, text, text_index)) - if self.tts.delete_audio_file and os.path.exists(tts_file): + if self.tts.delete_audio_file and tts_file is not None and os.path.exists(tts_file): os.remove(tts_file) except Exception as e: self.logger.bind(tag=TAG).error(f"TTS任务处理错误: {e}") @@ -331,7 +319,8 @@ class ConnectionHandler: text = None try: opus_datas, text, text_index = self.audio_play_queue.get() - future = asyncio.run_coroutine_threadsafe(sendAudioMessage(self, opus_datas, text, text_index), self.loop) + future = asyncio.run_coroutine_threadsafe(sendAudioMessage(self, opus_datas, text, text_index), + self.loop) future.result() except Exception as e: self.logger.bind(tag=TAG).error(f"audio_play_priority priority_thread: {text} {e}") @@ -350,19 +339,13 @@ class ConnectionHandler: def clearSpeakStatus(self): self.logger.bind(tag=TAG).debug(f"清除服务端讲话状态") self.asr_server_receive = True - self.tts_last_text = None self.tts_last_text_index = -1 - self.tts_first_text = None self.tts_first_text_index = -1 - self.tts_duration = 0 - self.tts_start_speak_time = None def recode_first_last_text(self, text, text_index=0): if self.tts_first_text_index == -1: self.logger.bind(tag=TAG).info(f"大模型说出第一句话: {text}") - self.tts_first_text = text self.tts_first_text_index = text_index - self.tts_last_text = text self.tts_last_text_index = text_index async def close(self): diff --git a/core/handle/musicHandler.py b/core/handle/musicHandler.py index 59a113dde..53ec49c4e 100644 --- a/core/handle/musicHandler.py +++ b/core/handle/musicHandler.py @@ -139,9 +139,7 @@ class MusicHandler: return text = f"正在播放{selected_music}" await send_stt_message(conn, text) - conn.tts_first_text = selected_music conn.tts_first_text_index = 0 - conn.tts_last_text = selected_music conn.tts_last_text_index = 0 conn.llm_finish_task = True if music_path.endswith(".p3"): diff --git a/core/handle/sendAudioHandle.py b/core/handle/sendAudioHandle.py index 11628d439..d0dcb82f4 100644 --- a/core/handle/sendAudioHandle.py +++ b/core/handle/sendAudioHandle.py @@ -19,7 +19,6 @@ async def sendAudioMessage(conn, audios, text, text_index=0): # 发送句子开始消息 if text_index == conn.tts_first_text_index: logger.bind(tag=TAG).info(f"发送第一段语音: {text}") - conn.tts_start_speak_time = time.perf_counter() await send_tts_message(conn, "sentence_start", text) # 初始化流控参数