From 0b03e9a3f04b617b4cd4294d267fd575bef00e2a Mon Sep 17 00:00:00 2001 From: Harold Date: Mon, 10 Nov 2025 16:05:19 +0800 Subject: [PATCH] feat: tts fix --- .../xiaozhi-server/core/providers/tts/cartesia.py | 6 +++--- .../xiaozhi-server/core/providers/tts/deepgram.py | 5 +---- .../core/providers/tts/elevenlabs.py | 15 ++++++--------- .../core/providers/tts/fishspeech.py | 1 + main/xiaozhi-server/core/utils/util.py | 14 +++++++++----- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/main/xiaozhi-server/core/providers/tts/cartesia.py b/main/xiaozhi-server/core/providers/tts/cartesia.py index b4b69bca2..5b980faea 100644 --- a/main/xiaozhi-server/core/providers/tts/cartesia.py +++ b/main/xiaozhi-server/core/providers/tts/cartesia.py @@ -44,7 +44,7 @@ class TTSProvider(TTSProviderBase): # 标记为单流式接口 self.interface_type = InterfaceType.SINGLE_STREAM - # 设置音频文件类型为 WAV + # 设置音频文件类型为 PCM self.audio_file_type = "pcm" # 获取 API Key @@ -119,14 +119,14 @@ class TTSProvider(TTSProviderBase): async def text_to_speak(self, text: str, output_file: Optional[str] = None) -> Optional[bytes]: """ - 将文本转换为 WAV 格式音频(完整音频,非流式) + 将文本转换为 PCM 格式音频(完整音频,非流式) Args: text: 要合成的文本 output_file: 输出文件路径(未使用,保留兼容性) Returns: - WAV 格式的完整音频字节数据 + PCM 格式的完整音频字节数据 """ try: ws = self._get_ws_client() diff --git a/main/xiaozhi-server/core/providers/tts/deepgram.py b/main/xiaozhi-server/core/providers/tts/deepgram.py index 2f0c77769..449a7db49 100644 --- a/main/xiaozhi-server/core/providers/tts/deepgram.py +++ b/main/xiaozhi-server/core/providers/tts/deepgram.py @@ -51,10 +51,7 @@ class TTSProvider(TTSProviderBase): for chunk in audio_stream: f.write(chunk) else: - audio_bytes = b"" - for chunk in audio_stream: - audio_bytes += chunk["data"] - return audio_bytes + return b''.join(audio_stream) except Exception as e: logger.bind(tag=TAG).error(f"Deepgram TTS failed: {e}", exc_info=True) diff --git a/main/xiaozhi-server/core/providers/tts/elevenlabs.py b/main/xiaozhi-server/core/providers/tts/elevenlabs.py index 913f3ae98..936ac74c8 100644 --- a/main/xiaozhi-server/core/providers/tts/elevenlabs.py +++ b/main/xiaozhi-server/core/providers/tts/elevenlabs.py @@ -52,7 +52,7 @@ class TTSProvider(TTSProviderBase): # 标记为单流式接口 self.interface_type = InterfaceType.SINGLE_STREAM - # 设置音频文件类型为 MP3 + # 设置音频文件类型为 PCM self.audio_file_type = "pcm" # 获取 API Key @@ -95,24 +95,23 @@ class TTSProvider(TTSProviderBase): logger.bind(tag=TAG).info( f"ElevenLabs SDK initialized: model={self.model}, " f"voice_id={self.voice_id[:8]}..., " - f"format=mp3_44100" + f"format={self.output_format}" ) async def text_to_speak(self, text: str, output_file: Optional[str] = None) -> Optional[bytes]: """ - 将文本转换为 MP3 格式音频(完整音频,非流式) + 将文本转换为 PCM 格式音频(完整音频,非流式) Args: text: 要合成的文本 output_file: 输出文件路径(未使用,保留兼容性) Returns: - MP3 格式的完整音频字节数据 + PCM 格式的完整音频字节数据 """ try: # 使用 SDK 的 convert 方法生成完整音频(返回 generator) - # 强制使用 mp3_44100 格式(忽略配置中的 output_format) - # 因为 base.py 需要完整的音频文件格式(MP3/WAV),而不是原始 PCM + # 使用配置的 output_format(如 pcm_16000) audio_stream = self.client.text_to_speech.convert( voice_id=self.voice_id, text=text, @@ -127,9 +126,7 @@ class TTSProvider(TTSProviderBase): for chunk in audio_stream: f.write(chunk) else: - audio_bytes = b"" - for chunk in audio_stream: - audio_bytes += chunk["data"] + audio_bytes = b''.join(chunk for chunk in audio_stream if chunk) return audio_bytes except Exception as e: diff --git a/main/xiaozhi-server/core/providers/tts/fishspeech.py b/main/xiaozhi-server/core/providers/tts/fishspeech.py index 14ff7bd98..9994ba21a 100644 --- a/main/xiaozhi-server/core/providers/tts/fishspeech.py +++ b/main/xiaozhi-server/core/providers/tts/fishspeech.py @@ -92,6 +92,7 @@ class TTSProvider(TTSProviderBase): config.get('ref_text')if config.get('ref_text') else config.get("reference_text") ) self.format = config.get("response_format", "pcm") + self.sample_rate = config.get("sample_rate", 16000) self.audio_file_type = config.get("response_format", "pcm") self.api_key = config.get("api_key", "YOUR_API_KEY") if self.api_key is None: diff --git a/main/xiaozhi-server/core/utils/util.py b/main/xiaozhi-server/core/utils/util.py index ea34d3e8a..f67abc0c2 100644 --- a/main/xiaozhi-server/core/utils/util.py +++ b/main/xiaozhi-server/core/utils/util.py @@ -315,19 +315,23 @@ def audio_to_data(audio_file_path: str, is_opus: bool = True) -> list[bytes]: def audio_bytes_to_data_stream(audio_bytes, file_type, is_opus, callback: Callable[[Any], Any]) -> None: """ - 直接用音频二进制数据转为opus/pcm数据,支持wav、mp3、p3 + transfer audio bytes to opus/pcm data, support wav、mp3、p3、pcm """ if file_type == "p3": # 直接用p3解码 return p3.decode_opus_from_bytes_stream(audio_bytes, callback) + elif file_type == "pcm": + audio = AudioSegment.from_raw( + BytesIO(audio_bytes), + ) else: - # 其他格式用pydub + # other formats use pydub audio = AudioSegment.from_file( BytesIO(audio_bytes), format=file_type, parameters=["-nostdin"] ) - audio = audio.set_channels(1).set_frame_rate(16000).set_sample_width(2) - raw_data = audio.raw_data - pcm_to_data_stream(raw_data, is_opus, callback) + audio = audio.set_channels(1).set_frame_rate(16000).set_sample_width(2) + raw_data = audio.raw_data + pcm_to_data_stream(raw_data, is_opus, callback) def pcm_to_data_stream(raw_data, is_opus=True, callback: Callable[[Any], Any] = None):