diff --git a/core/connection.py b/core/connection.py index 23c62817d..3d7a98132 100644 --- a/core/connection.py +++ b/core/connection.py @@ -71,7 +71,9 @@ class ConnectionHandler: # 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 @@ -328,22 +330,22 @@ class ConnectionHandler: while not self.stop_event.is_set(): text = None try: - opus_datas, text, text_id = self.audio_play_queue.get() - future = asyncio.run_coroutine_threadsafe(sendAudioMessage(self, opus_datas, text, text_id), self.loop) + 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.result() except Exception as e: self.logger.bind(tag=TAG).error(f"audio_play_priority priority_thread: {text} {e}") - def speak_and_play(self, text, text_id=0): + def speak_and_play(self, text, text_index=0): if text is None or len(text) <= 0: self.logger.bind(tag=TAG).info(f"无需tts转换,query为空,{text}") - return None, text, text_id + return None, text, text_index tts_file = self.tts.to_tts(text) if tts_file is None: self.logger.bind(tag=TAG).error(f"tts转换失败,{text}") - return None, text, text_id + return None, text, text_index self.logger.bind(tag=TAG).debug(f"TTS 文件生成完毕: {tts_file}") - return tts_file, text, text_id + return tts_file, text, text_index def clearSpeakStatus(self): self.logger.bind(tag=TAG).debug(f"清除服务端讲话状态") @@ -351,13 +353,15 @@ class ConnectionHandler: 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 not self.tts_first_text: + 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 diff --git a/core/handle/musicHandler.py b/core/handle/musicHandler.py index 6e260f205..59a113dde 100644 --- a/core/handle/musicHandler.py +++ b/core/handle/musicHandler.py @@ -140,7 +140,9 @@ class MusicHandler: 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"): opus_packets, duration = p3.decode_opus_from_file(music_path) diff --git a/core/handle/sendAudioHandle.py b/core/handle/sendAudioHandle.py index 4ed489c60..11628d439 100644 --- a/core/handle/sendAudioHandle.py +++ b/core/handle/sendAudioHandle.py @@ -15,9 +15,9 @@ async def isLLMWantToFinish(last_text): return False -async def sendAudioMessage(conn, audios, text, text_id=0): +async def sendAudioMessage(conn, audios, text, text_index=0): # 发送句子开始消息 - if text == conn.tts_first_text: + 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) @@ -45,7 +45,7 @@ async def sendAudioMessage(conn, audios, text, text_id=0): play_position += frame_duration # 更新播放位置 await send_tts_message(conn, "sentence_end", text) # 发送结束消息(如果是最后一个文本) - if conn.llm_finish_task and text_id == conn.tts_last_text_index: + if conn.llm_finish_task and text_index == conn.tts_last_text_index: await send_tts_message(conn, 'stop', None) if await isLLMWantToFinish(text): await conn.close()