From c1bf92d243e786993614a2dccdf14dd4034f252f Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Thu, 8 Jan 2026 11:39:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E6=92=AD=E6=94=BE=E9=9F=B3=E9=A2=91=E6=97=B6=E6=89=93=E6=96=AD?= =?UTF-8?q?=E5=90=8E=E4=BD=BF=E7=94=A8=E5=94=A4=E9=86=92=E8=AF=8D=E6=97=B6?= =?UTF-8?q?=E6=B5=81=E6=8E=A7=E5=99=A8=E6=9C=AA=E9=87=8D=E7=BD=AE=E9=80=A0?= =?UTF-8?q?=E6=88=90=E7=9A=84=E6=AD=BB=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/handle/sendAudioHandle.py | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index 8198dfb0..e60ce412 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -133,12 +133,28 @@ def _get_or_create_rate_controller(conn, frame_duration, is_single_packet): Returns: (rate_controller, flow_control) """ - # 判断是否需要重置:单包模式且 sentence_id 变化,或者控制器不存在 - need_reset = ( - is_single_packet - and getattr(conn, "audio_flow_control", {}).get("sentence_id") - != conn.sentence_id - ) or not hasattr(conn, "audio_rate_controller") + # 检查是否需要重置控制器 + need_reset = False + + if not hasattr(conn, "audio_rate_controller"): + # 控制器不存在,需要创建 + need_reset = True + else: + rate_controller = conn.audio_rate_controller + + # 后台发送任务已停止, 则需要重置 + if ( + not rate_controller.pending_send_task + or rate_controller.pending_send_task.done() + ): + need_reset = True + # 流式单包模式下, sentence_id 变化, 需要重置 + elif ( + is_single_packet + and getattr(conn, "audio_flow_control", {}).get("sentence_id") + != conn.sentence_id + ): + need_reset = True if need_reset: # 创建或获取 rate_controller From 54cac0d2fd0bae402bf2bbdbdb7ad0c62f8fa42e Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Thu, 8 Jan 2026 16:17:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=BD=93=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E8=AE=BE=E5=A4=87=E4=B8=8A=E4=B8=AA=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E6=AD=A3=E5=B8=B8=E7=BB=93=E6=9D=9F=E5=90=8E=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=94=A4=E9=86=92=E8=AF=8D=E5=94=A4=E9=86=92=E9=9F=B3?= =?UTF-8?q?=E9=A2=91=E6=92=AD=E6=94=BE=E4=B8=8D=E5=AE=8C=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/handle/helloHandle.py | 4 ++++ main/xiaozhi-server/core/handle/sendAudioHandle.py | 5 ++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/main/xiaozhi-server/core/handle/helloHandle.py b/main/xiaozhi-server/core/handle/helloHandle.py index a4220d5a..129b0aa7 100644 --- a/main/xiaozhi-server/core/handle/helloHandle.py +++ b/main/xiaozhi-server/core/handle/helloHandle.py @@ -1,5 +1,6 @@ import time import json +import uuid import random import asyncio from core.utils.dialogue import Message @@ -105,6 +106,9 @@ async def checkWakeupWords(conn, text): # 播放唤醒词回复 conn.client_abort = False + # 将唤醒词回复视为新会话,生成新的 sentence_id,确保流控器重置 + conn.sentence_id = str(uuid.uuid4().hex) + conn.logger.bind(tag=TAG).info(f"播放唤醒词回复: {response.get('text')}") await sendAudioMessage(conn, SentenceType.FIRST, opus_packets, response.get("text")) await sendAudioMessage(conn, SentenceType.LAST, [], None) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index e60ce412..323056d1 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -148,10 +148,9 @@ def _get_or_create_rate_controller(conn, frame_duration, is_single_packet): or rate_controller.pending_send_task.done() ): need_reset = True - # 流式单包模式下, sentence_id 变化, 需要重置 + # 当sentence_id 变化,需要重置 elif ( - is_single_packet - and getattr(conn, "audio_flow_control", {}).get("sentence_id") + getattr(conn, "audio_flow_control", {}).get("sentence_id") != conn.sentence_id ): need_reset = True