From 9c30b5e0356ca51a9cbc17323b60e00a8e01eb90 Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Tue, 20 Oct 2026 10:45:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9B=B4=E6=8E=A5=E5=86=99?= =?UTF-8?q?=E5=85=A5wav=EF=BC=8C=E6=97=A0=E9=9C=80=E7=BB=8F=E8=BF=87?= =?UTF-8?q?=E4=BA=8C=E6=AC=A1=E7=BC=96=E8=A7=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaozhi-server/core/handle/helloHandle.py | 22 +++++++------------ main/xiaozhi-server/core/utils/util.py | 15 +++++++++++++ .../tests/core/handle/test_wakeup_response.py | 22 ++++++++++++++++++- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/main/xiaozhi-server/core/handle/helloHandle.py b/main/xiaozhi-server/core/handle/helloHandle.py index f85301b2a..62d4063a8 100644 --- a/main/xiaozhi-server/core/handle/helloHandle.py +++ b/main/xiaozhi-server/core/handle/helloHandle.py @@ -8,7 +8,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: from core.connection import ConnectionHandler from core.utils.dialogue import Message -from core.utils.util import audio_to_data, audio_to_data_stream +from core.utils.util import audio_to_data, audio_file_to_wav_bytes from core.providers.tts.dto.dto import SentenceType from core.utils.wakeup_word import WakeupWordsConfig from core.handle.sendAudioHandle import sendAudioMessage, send_tts_message @@ -145,22 +145,16 @@ async def wakeupWordsResponse(conn: "ConnectionHandler"): if not tts_result: return - # Retained TTS audio is a file path, not a list of Opus frames. - if isinstance(tts_result, str): - opus_packets = [] - await asyncio.to_thread( - audio_to_data_stream, - tts_result, - callback=opus_packets.append, - sample_rate=conn.sample_rate, - ) - tts_result = opus_packets - # 获取当前音色 voice = getattr(conn.tts, "voice", "default") - # 使用链接的sample_rate - wav_bytes = opus_datas_to_wav_bytes(tts_result, sample_rate=conn.sample_rate) + # 保留的 TTS 音频是文件路径而非 Opus 帧:直接转目标采样率 wav,跳过有损 Opus 往返 + if isinstance(tts_result, str): + wav_bytes = await asyncio.to_thread( + audio_file_to_wav_bytes, tts_result, conn.sample_rate + ) + else: + wav_bytes = opus_datas_to_wav_bytes(tts_result, sample_rate=conn.sample_rate) file_path = wakeup_words_config.generate_file_path(voice) with open(file_path, "wb") as f: f.write(wav_bytes) diff --git a/main/xiaozhi-server/core/utils/util.py b/main/xiaozhi-server/core/utils/util.py index 839e46a85..c5fa3fa9b 100644 --- a/main/xiaozhi-server/core/utils/util.py +++ b/main/xiaozhi-server/core/utils/util.py @@ -475,6 +475,21 @@ def opus_datas_to_wav_bytes(opus_datas, sample_rate=16000, channels=1): pass +def audio_file_to_wav_bytes(audio_file_path, sample_rate=16000): + """音频文件直接转目标采样率的 wav 字节流,不经 Opus 编解码,避免有损""" + file_type = os.path.splitext(audio_file_path)[1] + if file_type: + file_type = file_type.lstrip(".") + with _ffmpeg_path_override(): + audio = AudioSegment.from_file( + audio_file_path, format=file_type, parameters=["-nostdin"] + ) + audio = audio.set_channels(1).set_frame_rate(sample_rate).set_sample_width(2) + buffer = BytesIO() + audio.export(buffer, format="wav") + return buffer.getvalue() + + def check_vad_update(before_config, new_config): if ( new_config.get("selected_module") is None diff --git a/main/xiaozhi-server/tests/core/handle/test_wakeup_response.py b/main/xiaozhi-server/tests/core/handle/test_wakeup_response.py index a11c44fbd..e9059cafe 100644 --- a/main/xiaozhi-server/tests/core/handle/test_wakeup_response.py +++ b/main/xiaozhi-server/tests/core/handle/test_wakeup_response.py @@ -40,7 +40,8 @@ def wakeup_runtime(tmp_path, monkeypatch): struct.pack( "