fix: 改用soundfile保存音频,解决torchaudio格式问题

This commit is contained in:
2026-04-22 18:11:13 +08:00
parent 3ca2921820
commit 6b73e2287c
2 changed files with 10 additions and 5 deletions

View File

@@ -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
ChatTTS
soundfile==0.12.1

View File

@@ -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