From d410f1fffe11a85072415b4a3548d82e9d6534be Mon Sep 17 00:00:00 2001 From: LuZhong Date: Thu, 26 Mar 2026 22:49:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20tts=5Frequest=20?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E7=B1=BB=E5=9E=8B=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 TTS_REQUEST 枚举值和 TtsRequestMessageHandler 处理器, 支持通过 WebSocket 处理 tts_request 请求并返回 opus 音频数据。 Co-Authored-By: Claude Opus 4.6 --- .../textHandler/ttsRequestMessageHandler.py | 65 +++++++++++++++++++ .../core/handle/textMessageHandlerRegistry.py | 2 + .../core/handle/textMessageType.py | 1 + 3 files changed, 68 insertions(+) create mode 100644 main/xiaozhi-server/core/handle/textHandler/ttsRequestMessageHandler.py diff --git a/main/xiaozhi-server/core/handle/textHandler/ttsRequestMessageHandler.py b/main/xiaozhi-server/core/handle/textHandler/ttsRequestMessageHandler.py new file mode 100644 index 000000000..bbc3d7f98 --- /dev/null +++ b/main/xiaozhi-server/core/handle/textHandler/ttsRequestMessageHandler.py @@ -0,0 +1,65 @@ +import json +import base64 +from typing import Dict, Any, TYPE_CHECKING + +if TYPE_CHECKING: + from core.connection import ConnectionHandler + +from core.handle.textMessageHandler import TextMessageHandler +from core.handle.textMessageType import TextMessageType + +TAG = __name__ + + +class TtsRequestMessageHandler(TextMessageHandler): + """TTS请求消息处理器""" + + @property + def message_type(self) -> TextMessageType: + return TextMessageType.TTS_REQUEST + + async def handle(self, conn: "ConnectionHandler", msg_json: Dict[str, Any]) -> None: + text = msg_json.get("text", "") + if not text: + conn.logger.bind(tag=TAG).warning("收到空的TTS请求") + return + + conn.logger.bind(tag=TAG).info(f"收到TTS请求: {text[:50]}...") + + try: + # 调用TTS provider生成音频 + audio_datas = conn.tts.to_tts(text) + if not audio_datas: + conn.logger.bind(tag=TAG).error("TTS生成音频失败") + return + + # 发送开始状态 + start_response = { + "type": "tts_response", + "state": "start", + "text": text + } + await conn.websocket.send(json.dumps(start_response)) + + # 分片发送音频数据 + for i, audio_chunk in enumerate(audio_datas): + audio_base64 = base64.b64encode(audio_chunk).decode("utf-8") + chunk_response = { + "type": "tts_response", + "state": "sentence_start" if i == 0 else "middle", + "audio": audio_base64, + "text": text + } + await conn.websocket.send(json.dumps(chunk_response)) + + # 发送结束状态 + stop_response = { + "type": "tts_response", + "state": "stop", + "text": text + } + await conn.websocket.send(json.dumps(stop_response)) + conn.logger.bind(tag=TAG).info(f"发送TTS响应完成: {len(audio_datas)} 包") + + except Exception as e: + conn.logger.bind(tag=TAG).error(f"处理TTS请求失败: {e}") \ No newline at end of file diff --git a/main/xiaozhi-server/core/handle/textMessageHandlerRegistry.py b/main/xiaozhi-server/core/handle/textMessageHandlerRegistry.py index 65e9474f1..798e3339c 100644 --- a/main/xiaozhi-server/core/handle/textMessageHandlerRegistry.py +++ b/main/xiaozhi-server/core/handle/textMessageHandlerRegistry.py @@ -8,6 +8,7 @@ from core.handle.textHandler.mcpMessageHandler import McpTextMessageHandler from core.handle.textMessageHandler import TextMessageHandler from core.handle.textHandler.serverMessageHandler import ServerTextMessageHandler from core.handle.textHandler.pingMessageHandler import PingMessageHandler +from core.handle.textHandler.ttsRequestMessageHandler import TtsRequestMessageHandler TAG = __name__ @@ -29,6 +30,7 @@ class TextMessageHandlerRegistry: McpTextMessageHandler(), ServerTextMessageHandler(), PingMessageHandler(), + TtsRequestMessageHandler(), ] for handler in handlers: diff --git a/main/xiaozhi-server/core/handle/textMessageType.py b/main/xiaozhi-server/core/handle/textMessageType.py index bd04d2897..853f9e852 100644 --- a/main/xiaozhi-server/core/handle/textMessageType.py +++ b/main/xiaozhi-server/core/handle/textMessageType.py @@ -10,3 +10,4 @@ class TextMessageType(Enum): MCP = "mcp" SERVER = "server" PING = "ping" + TTS_REQUEST = "tts_request"