From 5d9e63f89b36aa7b70c5c5b0aec2d87ae1326552 Mon Sep 17 00:00:00 2001 From: pany-05 Date: Thu, 6 Nov 2025 11:04:58 +0800 Subject: [PATCH] [bugfix] fix elevenlabs&&cartesia using in docker --- .../core/providers/tts/cartesia.py | 159 ++++-------------- .../core/providers/tts/elevenlabs.py | 114 ++++--------- main/xiaozhi-server/custom_config.yaml | 2 +- 3 files changed, 68 insertions(+), 207 deletions(-) diff --git a/main/xiaozhi-server/core/providers/tts/cartesia.py b/main/xiaozhi-server/core/providers/tts/cartesia.py index 8017c5876..38b1f1682 100644 --- a/main/xiaozhi-server/core/providers/tts/cartesia.py +++ b/main/xiaozhi-server/core/providers/tts/cartesia.py @@ -38,7 +38,7 @@ class TTSProvider(TTSProviderBase): type: cartesia_sdk api_key: your_api_key voice_id: your_voice_id # 或 voice_embedding - model: sonic-english + model: sonic-3 language: en encoding: pcm_s16le sample_rate: 24000 @@ -50,6 +50,8 @@ class TTSProvider(TTSProviderBase): # 标记为单流式接口 self.interface_type = InterfaceType.SINGLE_STREAM + # 设置音频文件类型为 WAV + self.audio_file_type = "wav" # 获取 API Key self.api_key = config.get("api_key") or os.environ.get("CARTESIA_API_KEY") @@ -73,12 +75,14 @@ class TTSProvider(TTSProviderBase): self.voice_id = voice_config # 模型和语言配置 - self.model = config.get("model", "sonic-english") + self.model = config.get("model", "sonic-3") self.language = config.get("language", "en") # 音频配置 self.encoding = config.get("encoding", "pcm_s16le") - self.sample_rate = config.get("sample_rate", 24000) + # 确保 sample_rate 是整数类型(配置文件可能返回字符串) + sample_rate_value = config.get("sample_rate", 24000) + self.sample_rate = int(sample_rate_value) if isinstance(sample_rate_value, str) else sample_rate_value # 验证至少有一个 voice 配置 if not self.voice_id and not self.voice_embedding: @@ -121,18 +125,25 @@ class TTSProvider(TTSProviderBase): async def text_to_speak(self, text: str, output_file: Optional[str] = None) -> Optional[bytes]: """ - 非流式方法:将完整文本转换为音频 + 将文本转换为 WAV 格式音频(完整音频,非流式) - 注意:Cartesia 主要设计为流式使用,此方法会等待完整音频生成 - 建议使用 to_tts_stream() 方法以获得更低的延迟 + Args: + text: 要合成的文本 + output_file: 输出文件路径(未使用,保留兼容性) + + Returns: + WAV 格式的完整音频字节数据 """ + import io + import wave + try: ws = await self._get_ws_client() - audio_chunks = [] + pcm_chunks = [] voice = self._prepare_voice_param() - # 发送流式请求 + # 发送流式请求,获取 PCM 数据 for output in ws.send( model_id=self.model, transcript=text, @@ -140,131 +151,35 @@ class TTSProvider(TTSProviderBase): stream=True, output_format={ "container": "raw", - "encoding": self.encoding, - "sample_rate": self.sample_rate + "encoding": self.encoding, # pcm_s16le + "sample_rate": self.sample_rate # 16000 or 24000 } ): # Cartesia SDK 返回 WebSocketTtsOutput 对象 - # 从 output.audio 获取音频数据 + # 从 output.audio 获取 PCM 音频数据 if output and hasattr(output, 'audio') and output.audio: - audio_chunks.append(output.audio) + pcm_chunks.append(output.audio) - # 合并所有音频块 - audio_data = b''.join(audio_chunks) + # 合并所有 PCM 音频块 + pcm_data = b''.join(pcm_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 + # 将 PCM 转换为 WAV 格式 + wav_buffer = io.BytesIO() + with wave.open(wav_buffer, 'wb') as wav_file: + wav_file.setnchannels(1) # 单声道 + wav_file.setsampwidth(2) # 16-bit = 2 bytes + wav_file.setframerate(self.sample_rate) # 使用配置的采样率 + wav_file.writeframes(pcm_data) + + wav_bytes = wav_buffer.getvalue() + + # 返回完整的 WAV 字节数据 + return wav_bytes 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.py b/main/xiaozhi-server/core/providers/tts/elevenlabs.py index c99efa622..43d7c6ddc 100644 --- a/main/xiaozhi-server/core/providers/tts/elevenlabs.py +++ b/main/xiaozhi-server/core/providers/tts/elevenlabs.py @@ -11,7 +11,6 @@ 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 @@ -53,6 +52,8 @@ class TTSProvider(TTSProviderBase): # 标记为单流式接口 self.interface_type = InterfaceType.SINGLE_STREAM + # 设置音频文件类型为 MP3 + self.audio_file_type = "mp3" # 获取 API Key self.api_key = config.get("api_key") or os.environ.get("ELEVEN_API_KEY") @@ -91,107 +92,52 @@ class TTSProvider(TTSProviderBase): 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}" + f"format=mp3_44100" ) 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,需要编码 + 将文本转换为 MP3 格式音频(完整音频,非流式) Args: text: 要合成的文本 - opus_handler: 音频数据回调函数 (接收 bytes) + output_file: 输出文件路径(未使用,保留兼容性) + + Returns: + MP3 格式的完整音频字节数据 """ - 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( + # 使用 SDK 的 convert 方法生成完整音频(返回 generator) + # 强制使用 mp3_44100 格式(忽略配置中的 output_format) + # 因为 base.py 需要完整的音频文件格式(MP3/WAV),而不是原始 PCM + audio_result = 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, - 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" + output_format="mp3_44100", # 强制 MP3 格式,44.1kHz + voice_settings=self.voice_settings ) + # 检查返回类型 + if isinstance(audio_result, bytes): + # 直接返回 bytes + return audio_result + else: + # 是 generator,需要收集所有音频块 + audio_chunks = [] + for chunk in audio_result: + if chunk: + audio_chunks.append(chunk) + + # 合并为完整的 MP3 数据 + mp3_data = b''.join(audio_chunks) + return mp3_data + except Exception as e: - error_msg = str(e).replace("{", "{{").replace("}", "}}") - logger.bind(tag=TAG).error(f"TTS 合成失败: {error_msg}", exc_info=True) + logger.bind(tag=TAG).error(f"ElevenLabs TTS failed: {e}", exc_info=True) raise async def close(self): diff --git a/main/xiaozhi-server/custom_config.yaml b/main/xiaozhi-server/custom_config.yaml index 416f6e209..ad2b51d4e 100644 --- a/main/xiaozhi-server/custom_config.yaml +++ b/main/xiaozhi-server/custom_config.yaml @@ -38,7 +38,7 @@ server: manager-api: url: http://xiaozhi-esp32-server-web:8002/xiaozhi # This secret is used for local development, it will be replaced by the actual secret when the server is deployed - secret: 613be043-dc7e-4f5e-b775-e318e5bc87a3 + secret: e8f02a3c-0d93-4824-9346-a813e56d6895 log: # 设置控制台输出的日志格式,时间、日志级别、标签、消息