From 75fd5d8cc3d6cd81af8457da48106a62e1b610ab Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Sat, 17 Oct 2026 07:22:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=A5=E5=B0=BE=E6=9B=BF=E6=8D=A2=E8=AF=8D?= =?UTF-8?q?=E6=AE=8B=E7=95=99=E5=85=88=E5=8F=91=20+=20=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E7=BB=93=E6=9D=9F=E5=86=B2=E5=88=B7=E7=BC=96=E7=A0=81=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 旧 finish_session 不发 _pending_prefix 残留,句尾替换词被扣时整段丢;monitor / to_tts 收 SessionFinished 不调 is_end=True 冲刷 opus 编码器,每句固定丢尾。改成 finish_session 开头先发残留,monitor / to_tts 调 is_end=True 冲刷,FIRST 同步重置。 --- .../providers/tts/huoshan_double_stream.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py index 08a3f1c63..8d8223c87 100644 --- a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py +++ b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py @@ -307,6 +307,9 @@ class TTSProvider(TTSProviderBase): if message.sentence_type == SentenceType.FIRST: # 重置流式处理状态 self.reset_stream_state() + # 重置 opus 流式编码器跨轮残留 + if hasattr(self, "opus_encoder") and self.opus_encoder: + self.opus_encoder.reset_state() # 初始化参数 try: if not getattr(self.conn, "sentence_id", None): @@ -449,6 +452,9 @@ class TTSProvider(TTSProviderBase): logger.bind(tag=TAG).debug(f"关闭会话~~{session_id}") try: if self.ws: + # 句尾替换词滑动窗口残留先发出去 + if getattr(self, "_pending_prefix", ""): + await self.text_to_speak(self._pending_prefix, None) header = Header( message_type=FULL_CLIENT_REQUEST, message_type_specific_flags=MsgTypeFlagWithEvent, @@ -564,6 +570,11 @@ class TTSProvider(TTSProviderBase): logger.bind(tag=TAG).info(f"句子语音生成成功:{self.tts_text}") elif res.optional.event == EVENT_SessionFinished: logger.bind(tag=TAG).debug(f"会话结束~~") + # 句尾冲刷流式 opus 编码器不足一帧的 PCM 残量 + if hasattr(self, "opus_encoder") and self.opus_encoder: + self.wav_to_opus_data_audio_raw_stream( + b"", is_end=True, callback=self.handle_opus + ) self.activate_session = False self._process_before_stop_play_files() # 非复用模式下,会话结束后发送 FinishConnection @@ -846,6 +857,13 @@ class TTSProvider(TTSProviderBase): ): self.wav_to_opus_data_audio_raw_stream(res.payload, callback=lambda opus_frame: audio_data.append(opus_frame)) elif res.optional.event == EVENT_SessionFinished: + # 句尾冲刷流式 opus 编码器不足一帧的 PCM 残量 + if hasattr(self, "opus_encoder") and self.opus_encoder: + self.wav_to_opus_data_audio_raw_stream( + b"", + is_end=True, + callback=lambda opus_frame: audio_data.append(opus_frame), + ) break finally: