From 6b73e2287c6e7520b80f853c115ea1b7a1588338 Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Wed, 22 Apr 2026 18:11:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=B9=E7=94=A8soundfile=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E9=9F=B3=E9=A2=91=EF=BC=8C=E8=A7=A3=E5=86=B3torchaudi?= =?UTF-8?q?o=E6=A0=BC=E5=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 3 ++- server.py | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/requirements.txt b/requirements.txt index eaecb69..a645629 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ python-multipart==0.0.9 torch==2.2.0 torchaudio==2.2.0 transformers==4.38.0 -ChatTTS==0.1.1 \ No newline at end of file +ChatTTS +soundfile==0.12.1 \ No newline at end of file diff --git a/server.py b/server.py index 4188910..a6a6925 100644 --- a/server.py +++ b/server.py @@ -124,11 +124,15 @@ def save_audio(audio_tensor: torch.Tensor, filename: str) -> str: filepath = os.path.join(AUDIO_DIR, filename) # 确保 tensor 正确形状 - if audio_tensor.dim() == 1: - audio_tensor = audio_tensor.unsqueeze(0) + if audio_tensor.dim() == 2: + audio_tensor = audio_tensor.squeeze(0) - # 保存为 WAV - torchaudio.save(filepath, audio_tensor, SAMPLE_RATE, format="wav") + # 转换为 numpy + audio_np = audio_tensor.cpu().numpy() if audio_tensor.is_cuda else audio_tensor.numpy() + + # 使用 soundfile 保存 + import soundfile as sf + sf.write(filepath, audio_np, SAMPLE_RATE) return filepath