Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit error message when swapping the samplerate and num_channels arguments on AudioFile. #238

Merged
merged 1 commit into from
Aug 8, 2023
Merged
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
20 changes: 17 additions & 3 deletions pedalboard/io/WriteableAudioFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class WriteableAudioFile
pybind11::gil_scoped_release release;

if (!isInteger(writeSampleRate)) {
throw std::domain_error(
throw py::type_error(
"Opening an audio file for writing requires an integer sample rate.");
}

Expand All @@ -244,8 +244,22 @@ class WriteableAudioFile
}

if (numChannels == 0) {
throw py::type_error("Opening an audio file for writing requires a "
"non-zero num_channels.");
throw std::domain_error("Opening an audio file for writing requires a "
"non-zero num_channels.");
}

// Tiny quality-of-life improvement to try to detect if people have swapped
// the num_channels and samplerate arguments:
if ((numChannels == 48000 || numChannels == 44100 || numChannels == 22050 ||
numChannels == 11025) &&
writeSampleRate < 8000) {
throw std::domain_error(
"Arguments of num_channels=" + std::to_string(numChannels) +
" and samplerate=" + std::to_string(writeSampleRate) +
" were provided when opening a file for writing. These arguments "
"appear to be flipped, and may cause an invalid audio file to be "
"written. Try reversing the order of the samplerate "
"and num_channels arguments.");
}

registerPedalboardAudioFormats(formatManager, true);
Expand Down
13 changes: 12 additions & 1 deletion tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ def test_write_twice_overwrites(
@pytest.mark.parametrize("samplerate", [1234.5, 23.0000000001])
def test_fractional_sample_rates(tmp_path: pathlib.Path, extension: str, samplerate):
filename = str(tmp_path / f"test{extension}")
with pytest.raises(ValueError):
with pytest.raises(TypeError):
pedalboard.io.WriteableAudioFile(filename, samplerate=samplerate, num_channels=1)


Expand Down Expand Up @@ -755,6 +755,17 @@ def test_sample_rate_is_int_by_default(samplerate: int):
assert f.samplerate == samplerate


@pytest.mark.parametrize("extension", [".flac"])
@pytest.mark.parametrize("samplerate", [22050, 44100, 48000])
def test_swapped_parameter_exception(tmp_path: pathlib.Path, extension: str, samplerate):
filename = str(tmp_path / f"test{extension}")
with pytest.raises(ValueError) as e:
pedalboard.io.WriteableAudioFile(filename, samplerate=1, num_channels=samplerate)
assert "reversing" in str(
e
), "Expected exception to include details about reversing parameters."


@pytest.mark.parametrize("dtype", [np.uint8, np.uint16, np.uint32, np.uint64])
def test_fail_to_write_unsigned(tmp_path: pathlib.Path, dtype):
filename = str(tmp_path / "test.wav")
Expand Down
Loading