Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tests/test_save_audio_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os
import tempfile
import pytest
import torch
from utils import save_audio


def test_save_audio_rejects_non_1d_input():
with tempfile.TemporaryDirectory() as tmpdir:
path = os.path.join(tmpdir, "audio.wav")
two_d = torch.zeros(2, 100)
with pytest.raises(ValueError, match="expects 1D audio"):
save_audio(two_d, path, sr=22050)
2 changes: 2 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def scan_checkpoint(cp_dir, prefix, renamed_file=None):

def save_audio(audio, path, sr):
# wav: torch with 1d shape
if audio.dim() != 1:
raise ValueError(f"save_audio expects 1D audio, got shape {audio.shape}")
audio = audio * MAX_WAV_VALUE
audio = audio.cpu().numpy().astype("int16")
write(path, sr, audio)