diff --git a/tests/test_save_audio_validation.py b/tests/test_save_audio_validation.py new file mode 100644 index 0000000..8b8fbe7 --- /dev/null +++ b/tests/test_save_audio_validation.py @@ -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) diff --git a/utils.py b/utils.py index 888ea89..82a3857 100644 --- a/utils.py +++ b/utils.py @@ -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)