From ee5a53d67d27d9362f6fe5abe1005767a2b42e47 Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Mon, 9 Mar 2026 17:55:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=80=89=E7=94=A8seed?= =?UTF-8?q?-tts-2.0=E6=96=87=E6=9C=AC=E7=BC=BA=E5=A4=B1=20fix:=20=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E7=AB=AF=E8=AF=B4=E8=AF=9D=E7=8A=B6=E6=80=81=E4=B8=8E?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/handle/sendAudioHandle.py | 8 +++----- .../core/providers/tts/huoshan_double_stream.py | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index 531d8958..fc2c6f6c 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -21,7 +21,6 @@ async def sendAudioMessage(conn: "ConnectionHandler", sentenceType, audios, text if conn.tts.tts_audio_first_sentence: conn.logger.bind(tag=TAG).info(f"发送第一段语音: {text}") conn.tts.tts_audio_first_sentence = False - await send_tts_message(conn, "start", None) if sentenceType == SentenceType.FIRST: # 同一句子的后续消息加入流控队列,其他情况立即发送 @@ -204,7 +203,6 @@ def _start_background_sender(conn: "ConnectionHandler", rate_controller, flow_co conn.last_activity_time = time.time() * 1000 await _do_send_audio(conn, packet, flow_control) - conn.client_is_speaking = True # 使用 start_sending 启动后台循环 rate_controller.start_sending(send_callback) @@ -232,12 +230,10 @@ async def _send_audio_with_rate_control( # 预缓冲:前N个包直接发送 if flow_control["packet_count"] < PRE_BUFFER_COUNT: await _do_send_audio(conn, packet, flow_control) - conn.client_is_speaking = True elif send_delay > 0: # 固定延迟模式 await asyncio.sleep(send_delay) await _do_send_audio(conn, packet, flow_control) - conn.client_is_speaking = True else: # 动态流控模式:仅添加到队列,由后台循环负责发送 rate_controller.add_audio(packet) @@ -267,7 +263,7 @@ async def _do_send_audio(conn: "ConnectionHandler", opus_packet, flow_control): async def send_tts_message(conn: "ConnectionHandler", state, text=None): """发送 TTS 状态消息""" if text is None and state == "sentence_start": - return + return message = {"type": "tts", "state": state, "session_id": conn.session_id} if text is not None: message["text"] = textUtils.check_emoji(text) @@ -318,3 +314,5 @@ async def send_stt_message(conn: "ConnectionHandler", text): json.dumps({"type": "stt", "text": stt_text, "session_id": conn.session_id}) ) await send_tts_message(conn, "start") + # 发送start消息后客户端状态会处于说话中状态,同步服务端状态 + conn.client_is_speaking = True \ No newline at end of file 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 152aa33f..a2680063 100644 --- a/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py +++ b/main/xiaozhi-server/core/providers/tts/huoshan_double_stream.py @@ -148,6 +148,7 @@ class TTSProvider(TTSProviderBase): self.access_token = config.get("access_token") self.cluster = config.get("cluster") self.resource_id = config.get("resource_id") + self.resource_type = True if self.resource_id == "seed-tts-2.0" else False self.activate_session = False if config.get("private_voice"): self.voice = config.get("private_voice") @@ -511,7 +512,7 @@ class TTSProvider(TTSProviderBase): if res.optional.event == EVENT_SessionCanceled: logger.bind(tag=TAG).debug(f"释放服务端资源成功~~") self.activate_session = False - elif res.optional.event == EVENT_TTSSentenceStart: + elif not self.resource_type and res.optional.event == EVENT_TTSSentenceStart: json_data = json.loads(res.payload.decode("utf-8")) self.tts_text = json_data.get("text", "") logger.bind(tag=TAG).debug(f"句子语音生成开始: {self.tts_text}") @@ -522,8 +523,17 @@ class TTSProvider(TTSProviderBase): res.optional.event == EVENT_TTSResponse and res.header.message_type == AUDIO_ONLY_RESPONSE ): + # 处理seed-tts-2.0文本字幕 + if self.resource_type and self.conn.tts_MessageText: + logger.bind(tag=TAG).info( + f"句子语音生成成功: {self.conn.tts_MessageText}" + ) + self.tts_audio_queue.put( + (SentenceType.FIRST, [], self.conn.tts_MessageText) + ) + self.conn.tts_MessageText = None self.wav_to_opus_data_audio_raw_stream(res.payload, callback=self.handle_opus) - elif res.optional.event == EVENT_TTSSentenceEnd: + elif not self.resource_type and res.optional.event == EVENT_TTSSentenceEnd: logger.bind(tag=TAG).info(f"句子语音生成成功:{self.tts_text}") elif res.optional.event == EVENT_SessionFinished: logger.bind(tag=TAG).debug(f"会话结束~~")