diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 90e9edb38..2070c3ee0 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -647,6 +647,49 @@ TTS: type: edge voice: zh-CN-XiaoxiaoNeural output_dir: tmp/ + # ============== SDK 版本TTS(推荐)============== + # 使用官方SDK,代码更简洁、性能更好、维护成本更低 + CartesiaSDK: + # Cartesia TTS - SDK版本(推荐) + # 官方SDK实现,低延迟,支持流式合成 + # API Key获取地址:https://play.cartesia.ai/console + # 或设置环境变量:export CARTESIA_API_KEY=your-api-key + type: cartesia_sdk + api_key: 你的Cartesia API密钥 + # Voice ID或voice embedding + voice_id: 你的语音ID + # 模型选择:sonic-english, sonic-multilingual等 + model: sonic-english + # 语言代码:en, zh, es等 + language: en + # 音频编码:pcm_s16le, pcm_f32le, pcm_mulaw等 + encoding: pcm_s16le + # 采样率:8000, 16000, 22050, 24000, 44100等 + sample_rate: 24000 + output_dir: tmp/ + ElevenLabsSDK: + # ElevenLabs TTS - SDK版本(推荐) + # 官方SDK实现,代码更简洁,维护成本更低,性能更好 + # API Key获取地址:https://elevenlabs.io/app/settings/api-keys + # 或设置环境变量:export ELEVEN_API_KEY=your-api-key + type: elevenlabs_sdk + api_key: 你的ElevenLabs API密钥 + # Voice ID获取:https://elevenlabs.io/app/voice-library + # 默认:21m00Tcm4TlvDq8ikWAM (Rachel) + voice_id: 21m00Tcm4TlvDq8ikWAM + # 模型选择:eleven_multilingual_v2, eleven_turbo_v2_5等 + model: eleven_multilingual_v2 + # 音频输出格式:pcm_16000, pcm_22050, pcm_24000, mp3_44100等 + output_format: pcm_16000 + # 语音稳定性:0-1,越高越稳定 + stability: 0.5 + # 相似度增强:0-1,越高越接近原音色 + similarity_boost: 0.75 + # 风格夸张:0-1 + style: 0.0 + # 启用说话人增强 + use_speaker_boost: true + output_dir: tmp/ DoubaoTTS: # 定义TTS API类型 type: doubao diff --git a/main/xiaozhi-server/core/providers/llm/openai/openai.py b/main/xiaozhi-server/core/providers/llm/openai/openai.py index a116a80db..f2f87c511 100644 --- a/main/xiaozhi-server/core/providers/llm/openai/openai.py +++ b/main/xiaozhi-server/core/providers/llm/openai/openai.py @@ -101,13 +101,13 @@ class LLMProvider(LLMProviderBase): for chunk in stream: # 检查是否存在有效的choice且content不为空 if getattr(chunk, "choices", None): - yield chunk.choices[0].delta.content, chunk.choices[ - 0 - ].delta.tool_calls + content = chunk.choices[0].delta.content + tool_calls = chunk.choices[0].delta.tool_calls + yield content, tool_calls # 存在 CompletionUsage 消息时,生成 Token 消耗 log elif isinstance(getattr(chunk, "usage", None), CompletionUsage): usage_info = getattr(chunk, "usage", None) - logger.bind(tag=TAG).info( + logger.bind(tag=TAG).debug( f"Token 消耗:输入 {getattr(usage_info, 'prompt_tokens', '未知')}," f"输出 {getattr(usage_info, 'completion_tokens', '未知')}," f"共计 {getattr(usage_info, 'total_tokens', '未知')}" diff --git a/main/xiaozhi-server/core/providers/tts/cartesia_sdk.py b/main/xiaozhi-server/core/providers/tts/cartesia_sdk.py new file mode 100644 index 000000000..8017c5876 --- /dev/null +++ b/main/xiaozhi-server/core/providers/tts/cartesia_sdk.py @@ -0,0 +1,271 @@ +""" +Cartesia TTS SDK 实现 + +使用官方 Cartesia Python SDK 实现流式 TTS +文档: https://docs.cartesia.ai/ +""" + +import asyncio +import os +import time +from typing import Optional, Callable, Dict, Any +from config.logger import setup_logging +from core.providers.tts.base import TTSProviderBase +from core.providers.tts.dto.dto import InterfaceType + +try: + import cartesia +except ImportError: + raise ImportError( + "Cartesia SDK not installed. Install with: pip install cartesia" + ) + +TAG = __name__ +logger = setup_logging() + + +class TTSProvider(TTSProviderBase): + """ + Cartesia TTS SDK 提供商 + + 特点: + - 低延迟流式合成 + - 支持多种语言和声音 + - 使用官方 SDK,代码简洁 + + 配置示例: + CartesiaSDK: + type: cartesia_sdk + api_key: your_api_key + voice_id: your_voice_id # 或 voice_embedding + model: sonic-english + language: en + encoding: pcm_s16le + sample_rate: 24000 + output_dir: tmp/ + """ + + def __init__(self, config: dict, delete_audio_file: bool = True): + super().__init__(config, delete_audio_file) + + # 标记为单流式接口 + self.interface_type = InterfaceType.SINGLE_STREAM + + # 获取 API Key + self.api_key = config.get("api_key") or os.environ.get("CARTESIA_API_KEY") + if not self.api_key: + raise ValueError( + "Cartesia API key is required. " + "Provide via api_key in config or set CARTESIA_API_KEY environment variable" + ) + + # Voice 配置(支持 voice_id 或 voice_embedding) + # 优先读取顶层 voice_id,然后检查嵌套的 voice.id 和 voice.embedding + self.voice_id = config.get("voice_id", "") + self.voice_embedding = None + + if not self.voice_id: + voice_config = config.get("voice", {}) + if isinstance(voice_config, dict): + self.voice_id = voice_config.get("id", "") + self.voice_embedding = voice_config.get("embedding") + elif voice_config: # 如果 voice 是字符串,直接作为 voice_id + self.voice_id = voice_config + + # 模型和语言配置 + self.model = config.get("model", "sonic-english") + self.language = config.get("language", "en") + + # 音频配置 + self.encoding = config.get("encoding", "pcm_s16le") + self.sample_rate = config.get("sample_rate", 24000) + + # 验证至少有一个 voice 配置 + if not self.voice_id and not self.voice_embedding: + raise ValueError( + "Voice configuration is required. " + "Provide either voice_id or voice.embedding in config" + ) + + # 初始化 SDK 客户端 + self.client = cartesia.Cartesia(api_key=self.api_key) + self._ws_client = None + + logger.bind(tag=TAG).info( + f"Cartesia SDK initialized: model={self.model}, " + f"voice_id={self.voice_id[:8]+'...' if self.voice_id else 'embedding'}, " + f"language={self.language}, " + f"encoding={self.encoding}, sample_rate={self.sample_rate}" + ) + + async def _get_ws_client(self): + """获取或创建 WebSocket 客户端(复用连接)""" + if self._ws_client is None: + self._ws_client = self.client.tts.websocket() + return self._ws_client + + def _prepare_voice_param(self) -> Dict[str, Any]: + """准备 voice 参数(避免重复代码)""" + if self.voice_id: + return { + "mode": "id", + "id": self.voice_id + } + elif self.voice_embedding: + return { + "mode": "embedding", + "embedding": self.voice_embedding + } + else: + raise ValueError("Neither voice_id nor voice_embedding is configured") + + async def text_to_speak(self, text: str, output_file: Optional[str] = None) -> Optional[bytes]: + """ + 非流式方法:将完整文本转换为音频 + + 注意:Cartesia 主要设计为流式使用,此方法会等待完整音频生成 + 建议使用 to_tts_stream() 方法以获得更低的延迟 + """ + try: + ws = await self._get_ws_client() + + audio_chunks = [] + voice = self._prepare_voice_param() + + # 发送流式请求 + for output in ws.send( + model_id=self.model, + transcript=text, + voice=voice, + stream=True, + output_format={ + "container": "raw", + "encoding": self.encoding, + "sample_rate": self.sample_rate + } + ): + # Cartesia SDK 返回 WebSocketTtsOutput 对象 + # 从 output.audio 获取音频数据 + if output and hasattr(output, 'audio') and output.audio: + audio_chunks.append(output.audio) + + # 合并所有音频块 + audio_data = b''.join(audio_chunks) + + # 保存到文件或返回 + if output_file: + with open(output_file, 'wb') as f: + f.write(audio_data) + logger.bind(tag=TAG).info(f"Audio saved to {output_file}, size: {len(audio_data)} bytes") + return None + else: + return audio_data + + except Exception as e: + logger.bind(tag=TAG).error(f"Cartesia TTS failed: {e}", exc_info=True) + raise + + def to_tts_stream(self, text: str, opus_handler: Optional[Callable] = None): + """ + 流式生成音频(核心方法) + + 注意:浏览器端使用 Web Audio API 播放,直接发送 PCM 数据 + ESP32 等嵌入式设备使用 Opus,需要编码 + + Args: + text: 要合成的文本 + opus_handler: 音频数据回调函数 (接收 bytes) + """ + try: + from core.providers.tts.dto.dto import SentenceType + + start_time = time.time() + text_preview = text[:30] + "..." if len(text) > 30 else text + logger.bind(tag=TAG).info(f"🎙️ TTS开始: [{text_preview}]") + + # 发送句子开始标记 + self.tts_audio_queue.put((SentenceType.FIRST, None, text)) + + # 在异步事件循环中运行 + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + + try: + # 运行异步合成 + chunk_count = loop.run_until_complete( + self._stream_synthesis(text, opus_handler, start_time) + ) + finally: + loop.close() + + # 发送句子结束标记 + self.tts_audio_queue.put((SentenceType.LAST, None, text)) + + end_time = time.time() + total_time = (end_time - start_time) * 1000 + logger.bind(tag=TAG).info( + f"✅ TTS完成: {chunk_count}块, 耗时 {total_time:.0f}ms" + ) + + except Exception as e: + logger.bind(tag=TAG).error(f"TTS 合成失败: {e}", exc_info=True) + raise + + async def _stream_synthesis(self, text: str, opus_handler: Optional[Callable], start_time: float) -> int: + """ + 异步流式合成实现 + + Args: + text: 要合成的文本 + opus_handler: 音频数据回调函数 + start_time: 开始时间(用于计算首包延迟) + + Returns: + 生成的音频块数量 + """ + try: + ws = await self._get_ws_client() + voice = self._prepare_voice_param() + + first_chunk_time = None + chunk_count = 0 + + # 发送流式请求并处理响应 + for output in ws.send( + model_id=self.model, + transcript=text, + voice=voice, + stream=True, + output_format={ + "container": "raw", + "encoding": self.encoding, + "sample_rate": self.sample_rate + }, + language=self.language + ): + # Cartesia SDK 返回 WebSocketTtsOutput 对象 + # 需要从 output.audio 获取实际的音频数据 + if output and hasattr(output, 'audio'): + audio_data = output.audio + + if audio_data: + if first_chunk_time is None: + first_chunk_time = time.time() + ttfb = (first_chunk_time - start_time) * 1000 + logger.bind(tag=TAG).info(f"⚡ 首包延迟: {ttfb:.0f}ms") + + # 发送音频数据(bytes) + if opus_handler: + opus_handler(audio_data) + chunk_count += 1 + + return chunk_count + + except Exception as e: + logger.bind(tag=TAG).error(f"Stream synthesis error: {e}", exc_info=True) + raise + + async def close(self): + """关闭 WebSocket 连接(Cartesia SDK 会自动管理)""" + self._ws_client = None + diff --git a/main/xiaozhi-server/core/providers/tts/elevenlabs_sdk.py b/main/xiaozhi-server/core/providers/tts/elevenlabs_sdk.py new file mode 100644 index 000000000..c4935f3af --- /dev/null +++ b/main/xiaozhi-server/core/providers/tts/elevenlabs_sdk.py @@ -0,0 +1,199 @@ +""" +ElevenLabs TTS SDK 实现 + +使用官方 ElevenLabs Python SDK 实现流式 TTS +文档: https://elevenlabs.io/docs/ +""" + +import asyncio +import os +from typing import Optional, Callable +from config.logger import setup_logging +from core.providers.tts.base import TTSProviderBase +from core.providers.tts.dto.dto import InterfaceType +from core.utils import opus_encoder_utils + +try: + from elevenlabs.client import ElevenLabs + from elevenlabs import VoiceSettings +except ImportError: + raise ImportError( + "ElevenLabs SDK not installed. Install with: pip install elevenlabs" + ) + +TAG = __name__ +logger = setup_logging() + + +class TTSProvider(TTSProviderBase): + """ + ElevenLabs TTS SDK 提供商 + + 特点: + - 高质量、情感丰富的语音 + - 支持声音克隆 + - 使用官方 SDK,代码简洁 + + 配置示例: + ElevenLabsSDK: + type: elevenlabs_sdk + api_key: your_api_key + voice_id: 21m00Tcm4TlvDq8ikWAM # Rachel voice + model: eleven_multilingual_v2 + output_format: pcm_16000 + stability: 0.5 + similarity_boost: 0.75 + style: 0.0 + use_speaker_boost: true + output_dir: tmp/ + """ + + def __init__(self, config: dict, delete_audio_file: bool = True): + super().__init__(config, delete_audio_file) + + # 标记为单流式接口 + self.interface_type = InterfaceType.SINGLE_STREAM + + # 获取 API Key + self.api_key = config.get("api_key") or os.environ.get("ELEVEN_API_KEY") + if not self.api_key: + raise ValueError( + "ElevenLabs API key is required. " + "Provide via api_key in config or set ELEVEN_API_KEY environment variable" + ) + + # Voice 配置 + self.voice_id = config.get("voice_id", "21m00Tcm4TlvDq8ikWAM") # Rachel (default) + + # 模型配置 + self.model = config.get("model", "eleven_multilingual_v2") + + # 音频输出格式 + self.output_format = config.get("output_format", "pcm_16000") + + # Voice Settings + self.stability = config.get("stability", 0.5) + self.similarity_boost = config.get("similarity_boost", 0.75) + self.style = config.get("style", 0.0) + self.use_speaker_boost = config.get("use_speaker_boost", True) + + # 流式延迟优化 (0-4,越大延迟越低但质量可能下降,推荐 2-3) + self.optimize_streaming_latency = config.get("optimize_streaming_latency", 3) + + # 初始化 SDK 客户端 + self.client = ElevenLabs(api_key=self.api_key) + + # 创建 VoiceSettings + self.voice_settings = VoiceSettings( + stability=self.stability, + similarity_boost=self.similarity_boost, + style=self.style, + use_speaker_boost=self.use_speaker_boost + ) + + # 初始化 Opus 编码器(ElevenLabs pcm_16000 是 16kHz PCM) + self.opus_encoder = opus_encoder_utils.OpusEncoderUtils( + sample_rate=16000, channels=1, frame_size_ms=60 + ) + + # PCM 缓冲区(用于累积不完整的帧) + self.pcm_buffer = bytearray() + + logger.bind(tag=TAG).info( + f"ElevenLabs SDK initialized: model={self.model}, " + f"voice_id={self.voice_id[:8]}..., " + f"format={self.output_format}" + ) + + async def text_to_speak(self, text: str, output_file: Optional[str] = None) -> Optional[bytes]: + """ + 非流式方法:将完整文本转换为音频 + + 注意:此方法会等待完整音频生成 + 建议使用 to_tts_stream() 方法以获得更低的延迟 + """ + try: + # 使用 SDK 的 convert 方法生成完整音频 + audio_data = self.client.text_to_speech.convert( + voice_id=self.voice_id, + text=text, + model_id=self.model, + output_format=self.output_format, + voice_settings=self.voice_settings + ) + + # 保存到文件或返回 + if output_file: + with open(output_file, 'wb') as f: + f.write(audio_data) + logger.bind(tag=TAG).info(f"Audio saved to {output_file}, size: {len(audio_data)} bytes") + return None + else: + return audio_data + + except Exception as e: + logger.bind(tag=TAG).error(f"ElevenLabs TTS failed: {e}", exc_info=True) + raise + + def to_tts_stream(self, text: str, opus_handler: Optional[Callable] = None): + """ + 流式生成音频(核心方法) + + 注意:浏览器端使用 Web Audio API 播放,直接发送 PCM 数据 + ESP32 等嵌入式设备使用 Opus,需要编码 + + Args: + text: 要合成的文本 + opus_handler: 音频数据回调函数 (接收 bytes) + """ + import time + try: + from core.providers.tts.dto.dto import SentenceType + + start_time = time.time() + text_preview = text[:30] + "..." if len(text) > 30 else text + logger.bind(tag=TAG).info(f"🎙️ TTS开始: [{text_preview}]") + + # 发送句子开始标记 + self.tts_audio_queue.put((SentenceType.FIRST, None, text)) + + # 使用 SDK 的 stream 方法(返回 Iterator[bytes] PCM 数据) + audio_stream = self.client.text_to_speech.stream( + voice_id=self.voice_id, + text=text, + model_id=self.model, + output_format=self.output_format, + voice_settings=self.voice_settings, + optimize_streaming_latency=self.optimize_streaming_latency + ) + + first_chunk_time = None + chunk_count = 0 + + # 直接发送 PCM 数据(浏览器端可以直接播放) + for pcm_chunk in audio_stream: + if pcm_chunk and opus_handler: + if first_chunk_time is None: + first_chunk_time = time.time() + ttfb = (first_chunk_time - start_time) * 1000 + logger.bind(tag=TAG).info(f"⚡ 首包延迟: {ttfb:.0f}ms") + chunk_count += 1 + opus_handler(pcm_chunk) + + # 发送句子结束标记 + self.tts_audio_queue.put((SentenceType.LAST, None, text)) + + end_time = time.time() + total_time = (end_time - start_time) * 1000 + logger.bind(tag=TAG).info( + f"✅ TTS完成: {chunk_count}块, 耗时 {total_time:.0f}ms" + ) + + except Exception as e: + logger.bind(tag=TAG).error(f"TTS 合成失败: {e}", exc_info=True) + raise + + async def close(self): + """清理资源(ElevenLabs SDK 自动管理连接)""" + pass + diff --git a/main/xiaozhi-server/requirements.txt b/main/xiaozhi-server/requirements.txt index bc9d6956c..7bd45b888 100644 --- a/main/xiaozhi-server/requirements.txt +++ b/main/xiaozhi-server/requirements.txt @@ -36,3 +36,7 @@ psutil==7.0.0 portalocker==3.2.0 Jinja2==3.1.6 vosk==0.3.44 + +# TTS SDKs +elevenlabs>=1.11.0 +cartesia>=1.0.0