π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
Fix MP3 tests that fail if passing an unnamed stream.
Peter Sobotb42922a35c4a
diff --git a/pedalboard/io/PythonInputStream.h b/pedalboard/io/PythonInputStream.hindex 580e1bf..9070c48 100644--- a/pedalboard/io/PythonInputStream.h+++ b/pedalboard/io/PythonInputStream.h@@ -44,6 +44,17 @@ public:}}+ std::optional<std::string> getFilename() {+ // Some Python file-like objects expose a ".name" property.+ // If this object has that property, return its value;+ // otherwise return an empty optional.+ if (py::hasattr(fileLike, "name")) {+ return fileLike.attr("name").cast<std::string>();+ } else {+ return {};+ }+ }+bool isSeekable() {try {return fileLike.attr("seekable")().cast<bool>();diff --git a/pedalboard/io/ReadableAudioFile.h b/pedalboard/io/ReadableAudioFile.hindex 758663b..12603c3 100644--- a/pedalboard/io/ReadableAudioFile.h+++ b/pedalboard/io/ReadableAudioFile.h@@ -77,8 +77,6 @@ public:"stream must be seekable.");}- // TODO: look at file extension hint-if (!reader) {auto originalStreamPosition = inputStream->getPosition();@@ -105,10 +103,19 @@ public:// parse MP3 from an input stream that's not explicitly got ".mp3" on the// end, ignore it.if (reader && reader->getFormatName() == "MP3 file") {- throw std::domain_error(- "Failed to open audio file-like object: stream does not seem to "- "contain a known or supported format. (If trying "- "to open an MP3 file, pass a format hint of \"mp3\".)");+ bool fileLooksLikeAnMP3 = false;+ if (auto filename = getPythonInputStream()->getFilename()) {+ fileLooksLikeAnMP3 =+ juce::File(filename.value()).hasFileExtension("mp3");+ }++ if (!fileLooksLikeAnMP3) {+ throw std::domain_error(+ "Failed to open audio file-like object: stream does not seem to "+ "contain a known or supported format. (If trying to open an MP3 "+ "file, pass a file-like with a \"name\" attribute ending with "+ "\".mp3\".)");+ }}}diff --git a/tests/test_io.py b/tests/test_io.pyindex c5cbe0f..e4252bf 100644--- a/tests/test_io.py+++ b/tests/test_io.py@@ -252,16 +252,20 @@ def test_context_manager_allows_exceptions():def test_read_okay_without_extension(tmp_path: pathlib.Path, audio_filename: str, samplerate: float):- if ".mp3" in audio_filename:- # Skip this test - due to a bug in LAME's MP3 reader, we require- # any MP3 files to be identified as such.- return-dest_path = str(tmp_path / "no_extension")shutil.copyfile(audio_filename, dest_path)- with pedalboard.io.AudioFile(dest_path) as af:- assert af.samplerate == samplerate- assert af.channels == 1+ try:+ with pedalboard.io.AudioFile(dest_path) as af:+ assert af.samplerate == samplerate+ assert af.channels == 1+ except Exception:+ if ".mp3" in audio_filename:+ # Skip this test - due to a bug in JUCE's MP3 reader on Linux/Windows,+ # we throw an exception when trying to read MP3 files without a known+ # extension.+ pass+ else:+ raise@pytest.mark.parametrize("audio_filename,samplerate", FILENAMES_AND_SAMPLERATES)@@ -269,7 +273,18 @@ def test_read_from_seekable_stream(audio_filename: str, samplerate: float):with open(audio_filename, "rb") as f:stream = io.BytesIO(f.read())- with pedalboard.io.AudioFile(stream) as af:+ try:+ af = pedalboard.io.AudioFile(stream)+ except Exception:+ if ".mp3" in audio_filename:+ # Skip this test - due to a bug in JUCE's MP3 reader on Linux/Windows,+ # we throw an exception when trying to read MP3 files without a known+ # extension.+ return+ else:+ raise++ with af:assert af.samplerate == samplerateassert af.channels == 1if any(ext in audio_filename for ext in EXPECT_LENGTH_TO_BE_EXACT):@@ -305,6 +320,15 @@ def test_read_from_seekable_stream(audio_filename: str, samplerate: float):af.read(1)+ "mp3_filename",+ [f for f in sum(TEST_AUDIO_FILES.values(), []) if f.endswith("mp3")],+)+def test_read_mp3_from_named_stream(mp3_filename: str):+ with pedalboard.io.AudioFile(open(mp3_filename, "rb")) as af:+ assert af is not None++def test_file_like_exceptions_propagate():audio_filename = FILENAMES_AND_SAMPLERATES[0][0]