π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
Prevent misidentification of MP3 streams.
Peter Sobot0a964c6b9ca1
diff --git a/pedalboard/io/ReadableAudioFile.h b/pedalboard/io/ReadableAudioFile.hindex de3a05e..5c7132f 100644--- a/pedalboard/io/ReadableAudioFile.h+++ b/pedalboard/io/ReadableAudioFile.h@@ -53,17 +53,6 @@ public:if (!reader) {// This is slower but more thorough:reader.reset(formatManager.createReaderFor(file.createInputStream()));-- // Known bug: the juce::MP3Reader class will parse formats that are not- // MP3 and pretend like they are, producing garbage output. For now, if we- // 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: file \"" + filename +- "\" does not seem to be of a known or supported format. (If trying "- "to open an MP3 file, ensure the filename ends with '.mp3'.)");- }}if (!reader)@@ -107,26 +96,6 @@ public:"reading audio files.");}}-- // Known bug: the juce::MP3Reader class will parse formats that are not- // MP3 and pretend like they are, producing garbage output. For now, if we- // parse MP3 from an input stream that's not explicitly got ".mp3" on the- // end, ignore it.- if (reader && reader->getFormatName() == "MP3 file") {- bool fileLooksLikeAnMP3 = false;- if (auto filename = getPythonInputStream()->getFilename()) {- fileLooksLikeAnMP3 = juce::File(*filename).hasFileExtension("mp3");- }-- if (!fileLooksLikeAnMP3) {- PythonException::raise();- 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\".)");- }- }}PythonException::raise();diff --git a/pedalboard/juce_overrides/juce_PatchedMP3AudioFormat.cpp b/pedalboard/juce_overrides/juce_PatchedMP3AudioFormat.cppindex d5b1270..c2342ad 100644--- a/pedalboard/juce_overrides/juce_PatchedMP3AudioFormat.cpp+++ b/pedalboard/juce_overrides/juce_PatchedMP3AudioFormat.cpp@@ -2132,6 +2132,14 @@ private:break;}+ bool isParsingFirstFrame = stream.getPosition() == 0;+ if (isParsingFirstFrame) {+ // If we're parsing the first frame of a file/stream, that frame must be+ // at the start of the stream. This prevents accidentally parsing .mp4+ // files (among others) as MP3 files that start with junk.+ break;+ }+++offset;}diff --git a/tests/test_io.py b/tests/test_io.pyindex 11957c9..310c69a 100644--- a/tests/test_io.py+++ b/tests/test_io.py@@ -269,41 +269,40 @@ def test_context_manager_allows_exceptions():assert af.closed[email protected]("audio_filename,samplerate", FILENAMES_AND_SAMPLERATES)+ "cross_platform_formats_only,audio_filename,samplerate",+ [(False, f, sr) for f, sr in FILENAMES_AND_SAMPLERATES]+ + [(True, f, sr) for f, sr in CROSS_PLATFORM_FILENAMES_AND_SAMPLERATES],+)def test_read_okay_without_extension(- tmp_path: pathlib.Path, audio_filename: str, samplerate: float+ tmp_path: pathlib.Path,+ audio_filename: str,+ samplerate: float,+ cross_platform_formats_only: bool,):dest_path = str(tmp_path / "no_extension")shutil.copyfile(audio_filename, dest_path)- try:- with pedalboard.io.AudioFile(dest_path) as af:- assert af.samplerate == samplerate- assert af.num_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+ with pedalboard.io.ReadableAudioFile(+ dest_path, cross_platform_formats_only=cross_platform_formats_only+ ) as af:+ assert af.samplerate == samplerate+ assert af.num_channels == 1[email protected]("audio_filename,samplerate", FILENAMES_AND_SAMPLERATES)-def test_read_from_seekable_stream(audio_filename: str, samplerate: float):+ "cross_platform_formats_only,audio_filename,samplerate",+ [(False, f, sr) for f, sr in FILENAMES_AND_SAMPLERATES]+ + [(True, f, sr) for f, sr in CROSS_PLATFORM_FILENAMES_AND_SAMPLERATES],+)+def test_read_from_seekable_stream(+ audio_filename: str, samplerate: float, cross_platform_formats_only: bool+):with open(audio_filename, "rb") as f:stream = io.BytesIO(f.read())- 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+ af = pedalboard.io.ReadableAudioFile(+ stream, cross_platform_formats_only=cross_platform_formats_only+ )with af:assert af.samplerate == samplerate