From 12cee4027a72813029a7edb69fb5021f9597d2ad Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Fri, 20 Jun 2025 16:25:08 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=E5=8F=8D=E5=A4=8D=E6=89=93=E6=96=AD?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=B2=A1=E6=9C=89=E8=A2=AB=E6=B8=85=E9=99=A4?= =?UTF-8?q?=EF=BC=8Cvad=E5=9B=9B=E5=B8=A7=E8=AF=AD=E9=9F=B3=E8=AF=86?= =?UTF-8?q?=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/connection.py | 5 ++-- .../providers/tts/huoshan_double_stream.py | 27 +++++++++++++++++-- .../core/providers/vad/silero.py | 14 +++++++++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index e10ea3d44..3ad945923 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -115,6 +115,7 @@ class ConnectionHandler: self.client_have_voice_last_time = 0.0 self.client_no_voice_last_time = 0.0 self.client_voice_stop = False + self.client_voice_frame_count = 0 # asr相关变量 # 因为实际部署时可能会用到公共的本地ASR,不能把变量暴露给公共ASR @@ -627,8 +628,8 @@ class ConnectionHandler: ) memory_str = future.result() - uuid_str = str(uuid.uuid4()).replace("-", "") - self.sentence_id = uuid_str + self.sentence_id = str(uuid.uuid4().hex) + if self.intent_type == "function_call" and functions is not None: # 使用支持functions的streaming接口 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 7ca2c6539..1ebaf15a0 100644 --- a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py +++ b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py @@ -12,6 +12,8 @@ from core.utils.util import check_model_key from core.providers.tts.base import TTSProviderBase from core.handle.abortHandle import handleAbortMessage from core.providers.tts.dto.dto import SentenceType, ContentType, InterfaceType +from asyncio import Task + TAG = __name__ logger = setup_logging() @@ -141,6 +143,7 @@ class TTSProvider(TTSProviderBase): super().__init__(config, delete_audio_file) self.ws = None self.interface_type = InterfaceType.DUAL_STREAM + self._monitor_task = None # 监听任务引用 self.appId = config.get("appid") self.access_token = config.get("access_token") self.cluster = config.get("cluster") @@ -270,8 +273,7 @@ class TTSProvider(TTSProviderBase): try: # 建立新连接 if self.ws is None: - await handleAbortMessage(self.conn) - logger.bind(tag=TAG).error(f"WebSocket连接不存在,终止发送文本") + logger.bind(tag=TAG).warning(f"WebSocket连接不存在,终止发送文本") return # 过滤Markdown @@ -293,6 +295,25 @@ class TTSProvider(TTSProviderBase): async def start_session(self, session_id): logger.bind(tag=TAG).info(f"开始会话~~{session_id}") try: + task = self._monitor_task + if ( + task is not None + and isinstance(task, Task) + and not task.done() + ): + logger.bind(tag=TAG).info("等待上一个监听任务结束...") + if self.ws is not None: + logger.bind(tag=TAG).info("强制关闭上一个WebSocket连接以唤醒监听任务...") + try: + await self.ws.close() + except Exception as e: + logger.bind(tag=TAG).warning(f"关闭上一个ws异常: {e}") + self.ws = None + try: + await asyncio.wait_for(task, timeout=8) + except Exception as e: + logger.bind(tag=TAG).warning(f"等待监听任务异常: {e}") + self._monitor_task = None # 建立新连接 await self._ensure_connection() @@ -463,6 +484,8 @@ class TTSProvider(TTSProviderBase): except: pass self.ws = None + # 监听任务退出时清理引用 + self._monitor_task = None async def send_event( self, diff --git a/main/xiaozhi-server/core/providers/vad/silero.py b/main/xiaozhi-server/core/providers/vad/silero.py index 29c0c11b9..9df5188c8 100644 --- a/main/xiaozhi-server/core/providers/vad/silero.py +++ b/main/xiaozhi-server/core/providers/vad/silero.py @@ -35,6 +35,10 @@ class VADProvider(VADProviderBase): pcm_frame = self.decoder.decode(opus_packet, 960) conn.client_audio_buffer.extend(pcm_frame) # 将新数据加入缓冲区 + # 初始化帧计数器 + if not hasattr(conn, "client_voice_frame_count"): + conn.client_voice_frame_count = 0 + # 处理缓冲区中的完整帧(每次处理512采样点) client_have_voice = False while len(conn.client_audio_buffer) >= 512 * 2: @@ -50,7 +54,15 @@ class VADProvider(VADProviderBase): # 检测语音活动 with torch.no_grad(): speech_prob = self.model(audio_tensor, 16000).item() - client_have_voice = speech_prob >= self.vad_threshold + is_voice = speech_prob >= self.vad_threshold + + if is_voice: + conn.client_voice_frame_count += 1 + else: + conn.client_voice_frame_count = 0 + + # 只有连续4帧检测到语音才认为有语音 + client_have_voice = conn.client_voice_frame_count >= 4 # 如果之前有声音,但本次没有声音,且与上次有声音的时间差已经超过了静默阈值,则认为已经说完一句话 if conn.client_have_voice and not client_have_voice: From 09f6605cfeecd15952de70900675445f4e3bda99 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Fri, 20 Jun 2025 17:17:09 +0800 Subject: [PATCH 2/4] =?UTF-8?q?update:=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/config/logger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/xiaozhi-server/config/logger.py b/main/xiaozhi-server/config/logger.py index 81566bdea..1cc947461 100644 --- a/main/xiaozhi-server/config/logger.py +++ b/main/xiaozhi-server/config/logger.py @@ -5,7 +5,7 @@ from config.config_loader import load_config from config.settings import check_config_file from datetime import datetime -SERVER_VERSION = "0.5.7" +SERVER_VERSION = "0.5.8" _logger_initialized = False From 879c1267b61cb7d7f68398f3b3d93a84a73e66ec Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Fri, 20 Jun 2025 17:20:08 +0800 Subject: [PATCH 3/4] =?UTF-8?q?update:=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86?= =?UTF-8?q?doubaoasr=E5=87=BA=E7=8E=B0400=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/providers/asr/doubao_stream.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/main/xiaozhi-server/core/providers/asr/doubao_stream.py b/main/xiaozhi-server/core/providers/asr/doubao_stream.py index 5c91eec3d..31704b969 100644 --- a/main/xiaozhi-server/core/providers/asr/doubao_stream.py +++ b/main/xiaozhi-server/core/providers/asr/doubao_stream.py @@ -254,7 +254,6 @@ class ASRProvider(ASRProviderBase): "X-Api-Access-Key": self.access_token, "X-Api-Resource-Id": "volc.bigasr.sauc.duration", "X-Api-Connect-Id": str(uuid.uuid4()), - "Host": "openspeech.bytedance.com", } def generate_header( @@ -310,7 +309,11 @@ class ASRProvider(ASRProviderBase): code = int.from_bytes(res[4:8], "big", signed=False) msg_length = int.from_bytes(res[8:12], "big", signed=False) error_msg = json.loads(res[12:].decode("utf-8")) - return {"code": code, "msg_length": msg_length, "payload_msg": error_msg} + return { + "code": code, + "msg_length": msg_length, + "payload_msg": error_msg, + } # 获取JSON数据(跳过12字节头部) try: From 993d5395b25cd1ae225cc516228ca11ed0ca96a9 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Fri, 20 Jun 2025 17:42:14 +0800 Subject: [PATCH 4/4] =?UTF-8?q?update:=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/xiaozhi/common/constant/Constant.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java index 37fe4c062..b5837eb5c 100644 --- a/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java +++ b/main/manager-api/src/main/java/xiaozhi/common/constant/Constant.java @@ -227,7 +227,7 @@ public interface Constant { /** * 版本号 */ - public static final String VERSION = "0.5.7"; + public static final String VERSION = "0.5.8"; /** * 无效固件URL