π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
Add support for reading seekable file-like objects.
Peter Sobot11b2e2784468
diff --git a/pedalboard/io/AudioFileInit.h b/pedalboard/io/AudioFileInit.hindex ce2a584..3f18900 100644--- a/pedalboard/io/AudioFileInit.h+++ b/pedalboard/io/AudioFileInit.h@@ -53,10 +53,33 @@ inline void init_audio_file(py::module &m) {"samplerate and num_channels arguments.");} else {throw py::type_error("AudioFile instances can only be opened in "- "read mode (\"r\") and write mode (\"w\").");+ "read mode (\"r\") or write mode (\"w\").");}},py::arg("cls"), py::arg("filename"), py::arg("mode") = "r")+ .def_static(+ "__new__",+ [](const py::object *, py::object filelike, std::string mode) {+ if (mode == "r") {+ if (!isReadableFileLike(filelike)) {+ throw py::type_error(+ "Expected either a filename or a file-like object (with "+ "read, seek, seekable, and tell methods), but received: " ++ filelike.attr("__repr__")().cast<std::string>());+ }++ return std::make_shared<ReadableAudioFile>(+ std::make_unique<PythonInputStream>(filelike));+ } else if (mode == "w") {+ throw py::type_error(+ "Opening an audio file-like object for writing requires "+ "samplerate and num_channels arguments.");+ } else {+ throw py::type_error("AudioFile instances can only be opened in "+ "read mode (\"r\") or write mode (\"w\").");+ }+ },+ py::arg("cls"), py::arg("file_like"), py::arg("mode") = "r").def_static("__new__",[](const py::object *, std::string filename, std::string mode,@@ -79,7 +102,7 @@ inline void init_audio_file(py::module &m) {filename, sampleRate.value(), numChannels, bitDepth, quality);} else {throw py::type_error("AudioFile instances can only be opened in "- "read mode (\"r\") and write mode (\"w\").");+ "read mode (\"r\") or write mode (\"w\").");}},py::arg("cls"), py::arg("filename"), py::arg("mode") = "w",diff --git a/pedalboard/io/PythonInputStream.h b/pedalboard/io/PythonInputStream.hnew file mode 100644index 0000000..580e1bf--- /dev/null+++ b/pedalboard/io/PythonInputStream.h@@ -0,0 +1,179 @@+/*+ * pedalboard+ * Copyright 2021 Spotify AB+ *+ * Licensed under the GNU Public License, Version 3.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * https://www.gnu.org/licenses/gpl-3.0.html+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++#pragma once++#include <mutex>+#include <optional>++namespace py = pybind11;++#include "../JuceHeader.h"++namespace Pedalboard {++bool isReadableFileLike(py::object fileLike) {+ return py::hasattr(fileLike, "read") && py::hasattr(fileLike, "seek") &&+ py::hasattr(fileLike, "tell") && py::hasattr(fileLike, "seekable");+}++/**+ * A juce::InputStream subclass that fetches its+ * data from a provided Python file-like object.+ */+class PythonInputStream : public juce::InputStream {+public:+ PythonInputStream(py::object fileLike) : fileLike(fileLike) {+ if (!isReadableFileLike(fileLike)) {+ throw py::type_error("Expected a file-like object (with read, seek, "+ "seekable, and tell methods).");+ }+ }++ bool isSeekable() {+ try {+ return fileLike.attr("seekable")().cast<bool>();+ } catch (py::error_already_set e) {+ pythonErrorRaised = true;+ e.restore();+ return false;+ }+ }++ juce::int64 getTotalLength() {+ py::gil_scoped_acquire acquire;++ // TODO: Try reading a couple of Python properties that may contain the+ // total length: urllib3.response.HTTPResponse provides `length_remaining`,+ // for instance++ try {+ if (!fileLike.attr("seekable")().cast<bool>()) {+ return -1;+ }+ } catch (py::error_already_set e) {+ pythonErrorRaised = true;+ e.restore();+ return 0;+ }++ if (totalLength == -1) {+ try {+ juce::int64 pos = fileLike.attr("tell")().cast<juce::int64>();+ fileLike.attr("seek")(0, 2);+ totalLength = fileLike.attr("tell")().cast<juce::int64>();+ fileLike.attr("seek")(pos, 0);+ } catch (py::error_already_set e) {+ pythonErrorRaised = true;+ e.restore();+ return -1;+ }+ }++ return totalLength;+ }++ int read(void *buffer, int bytesToRead) {+ // The buffer should never be null, and a negative size is probably a+ // sign that something is broken!+ jassert(buffer != nullptr && bytesToRead >= 0);++ py::bytes result;+ {+ py::gil_scoped_acquire acquire;+ try {+ result = fileLike.attr("read")(bytesToRead).cast<py::bytes>();+ } catch (py::error_already_set e) {+ pythonErrorRaised = true;+ e.restore();+ return 0;+ }+ }++ std::string resultAsString = result;+ std::memcpy((char *)buffer, (char *)resultAsString.data(),+ resultAsString.size());++ int bytesCopied = resultAsString.size();++ lastReadWasSmallerThanExpected = bytesToRead > bytesCopied;+ return bytesCopied;+ }++ bool isExhausted() {+ if (lastReadWasSmallerThanExpected) {+ return true;+ }++ try {+ py::gil_scoped_acquire acquire;+ return fileLike.attr("tell")().cast<juce::int64>() == getTotalLength();+ } catch (py::error_already_set e) {+ pythonErrorRaised = true;+ e.restore();+ return true;+ }+ }++ juce::int64 getPosition() {+ try {+ py::gil_scoped_acquire acquire;+ return fileLike.attr("tell")().cast<juce::int64>();+ } catch (py::error_already_set e) {+ pythonErrorRaised = true;+ e.restore();+ return -1;+ }+ }++ bool setPosition(juce::int64 pos) {+ try {+ py::gil_scoped_acquire acquire;++ if (fileLike.attr("seekable")().cast<bool>()) {+ fileLike.attr("seek")(pos);+ }++ return fileLike.attr("tell")().cast<juce::int64>() == pos;+ } catch (py::error_already_set e) {+ pythonErrorRaised = true;+ e.restore();+ return false;+ }+ }++ void raisePendingPythonExceptions() {+ if (pythonErrorRaised) {+ pythonErrorRaised = false;+ py::gil_scoped_acquire acquire;+ throw py::error_already_set();+ }+ }++ py::object getFileLikeObject() { return fileLike; }++private:+ py::object fileLike;+ juce::int64 totalLength = -1;+ bool lastReadWasSmallerThanExpected = false;++ // the juce::InputStream interface is not exception-safe, so we have to+ // change the behaviour of this subclass if an exception gets thrown,+ // then throw the exception ourselves when we have the opportunity.+ bool pythonErrorRaised = false;+};+}; // namespace Pedalboard\ No newline at end of filediff --git a/pedalboard/io/ReadableAudioFile.h b/pedalboard/io/ReadableAudioFile.hindex 208f718..758663b 100644--- a/pedalboard/io/ReadableAudioFile.h+++ b/pedalboard/io/ReadableAudioFile.h@@ -26,6 +26,7 @@#include "../BufferUtils.h"#include "../JuceHeader.h"#include "AudioFile.h"+#include "PythonInputStream.h"namespace py = pybind11;@@ -68,6 +69,57 @@ public:"\" does not seem to be of a known or supported format.");}+ ReadableAudioFile(std::unique_ptr<PythonInputStream> inputStream) {+ formatManager.registerBasicFormats();++ if (!inputStream->isSeekable()) {+ throw std::domain_error("Failed to open audio file-like object: input "+ "stream must be seekable.");+ }++ // TODO: look at file extension hint++ if (!reader) {+ auto originalStreamPosition = inputStream->getPosition();++ for (int i = 0; i < formatManager.getNumKnownFormats(); i++) {+ auto *af = formatManager.getKnownFormat(i);++ if (auto *r = af->createReaderFor(inputStream.get(), false)) {+ inputStream.release();+ reader.reset(r);+ break;+ }++ inputStream->setPosition(originalStreamPosition);+ if (inputStream->getPosition() != originalStreamPosition) {+ throw std::runtime_error(+ "Input file-like object did not seek to expected position. The "+ "provided file-like object must be fully seekable to allow "+ "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") {+ 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\".)");+ }+ }++ if (!reader)+ throw std::domain_error(+ "Failed to open audio file-like object: file \"" + filename ++ "\" does not seem to contain a known or supported format.");++ raisePendingPythonExceptions();+ }+double getSampleRate() const {if (!reader)throw std::runtime_error("I/O operation on a closed file.");@@ -171,6 +223,7 @@ public:numSamples)) {throw std::runtime_error("Failed to read from file.");}+ raisePendingPythonExceptions();} else {// If the audio is stored in an integral format, read it as integers// and do the floating-point conversion ourselves to work around@@ -181,6 +234,7 @@ public:currentPosition, numSamples)) {throw std::runtime_error("Failed to read from file.");}+ raisePendingPythonExceptions();// When converting 24-bit, 16-bit, or 8-bit data from int to float,// the values provided by the above read() call are shifted left@@ -285,6 +339,7 @@ public:currentPosition, numSamples)) {throw std::runtime_error("Failed to read from file.");}+ raisePendingPythonExceptions();} else {// Read the file in smaller chunks, converting from int32 to the// appropriate output format as we go:@@ -308,6 +363,7 @@ public:samplesToRead)) {throw std::runtime_error("Failed to read from file.");}+ raisePendingPythonExceptions();// Convert the data in intBuffers to the output format:char shift = 32 - reader->bitsPerSample;@@ -366,6 +422,19 @@ public:std::string getFilename() const { return filename; }+ PythonInputStream *getPythonInputStream() const {+ if (!filename.empty()) {+ return nullptr;+ }+ if (!reader) {+ return nullptr;+ }++ // the AudioFormatReader retains exclusive ownership over the input stream,+ // so we have to cast here instead of holding a shared_ptr:+ return (PythonInputStream *)reader->input;+ }+std::shared_ptr<ReadableAudioFile> enter() { return shared_from_this(); }void exit(const py::object &type, const py::object &value,@@ -373,6 +442,11 @@ public:close();}+ void raisePendingPythonExceptions() {+ if (auto *inputStream = getPythonInputStream())+ inputStream->raisePendingPythonExceptions();+ }+private:juce::AudioFormatManager formatManager;std::string filename;@@ -394,6 +468,18 @@ inline void init_readable_audio_file(py::module &m) {return std::make_shared<ReadableAudioFile>(filename);}),py::arg("filename"))+ .def(py::init([](py::object filelike) {+ if (!isReadableFileLike(filelike)) {+ throw py::type_error(+ "Expected either a filename or a file-like object (with "+ "read, seek, seekable, and tell methods), but received: " ++ filelike.attr("__repr__")().cast<std::string>());+ }++ return std::make_shared<ReadableAudioFile>(+ std::make_unique<PythonInputStream>(filelike));+ }),+ py::arg("file_like")).def_static("__new__",[](const py::object *, std::string filename) {@@ -428,9 +514,17 @@ inline void init_readable_audio_file(py::module &m) {[](const ReadableAudioFile &file) {std::ostringstream ss;ss << "<pedalboard.io.ReadableAudioFile";+if (!file.getFilename().empty()) {ss << " filename=\"" << file.getFilename() << "\"";+ } else if (PythonInputStream *stream =+ file.getPythonInputStream()) {+ ss << " file_like="+ << stream->getFileLikeObject()+ .attr("__repr__")()+ .cast<std::string>();}+if (file.isClosed()) {ss << " closed";} else {diff --git a/tests/test_io.py b/tests/test_io.pyindex 0c7c42b..da2c715 100644--- a/tests/test_io.py+++ b/tests/test_io.py@@ -15,6 +15,7 @@# limitations under the License.+import ioimport osimport globimport wave@@ -263,6 +264,87 @@ def test_read_okay_without_extension(assert af.channels == 1[email protected]("audio_filename,samplerate", FILENAMES_AND_SAMPLERATES)+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:+ assert af.samplerate == samplerate+ assert af.channels == 1+ if any(ext in audio_filename for ext in EXPECT_LENGTH_TO_BE_EXACT):+ assert af.frames == int(samplerate * EXPECTED_DURATION_SECONDS)+ else:+ assert af.frames >= int(samplerate * EXPECTED_DURATION_SECONDS)++ samples = af.read(int(samplerate * EXPECTED_DURATION_SECONDS))+ assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS))++ # File should no longer be useful:+ af.read(1).nbytes == 0++ # Seeking back to the start of the file should work:+ assert af.seekable()+ af.seek(0)+ samples = af.read(int(samplerate * EXPECTED_DURATION_SECONDS))+ assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS))++ # Seeking to an arbitrary point should also work+ af.seek(int(samplerate * EXPECTED_DURATION_SECONDS / 2))+ samples = af.read(int(samplerate * EXPECTED_DURATION_SECONDS / 2))+ assert samples.shape == (1, int(samplerate * EXPECTED_DURATION_SECONDS / 2))++ assert f"samplerate={int(af.samplerate)}" in repr(af)+ assert f"num_channels={af.channels}" in repr(af)+ assert repr(stream) in repr(af)++ with pytest.raises(RuntimeError):+ af.channels++ with pytest.raises(RuntimeError):+ af.read(1)+++def test_file_like_exceptions_propagate():+ audio_filename = FILENAMES_AND_SAMPLERATES[0][0]++ with open(audio_filename, 'rb') as f:+ stream = io.BytesIO(f.read())++ stream_read = stream.read++ call_count = [0]++ def throw_exception_on_99th_call(*args, **kwargs):+ call_count[0] += 1+ if call_count[0] == 99:+ raise ValueError("Called 99 times!")+ return stream_read(*args, **kwargs)++ stream.read = throw_exception_on_99th_call++ with pedalboard.io.AudioFile(stream) as af:+ assert af.read(1).nbytes > 0+ with pytest.raises(ValueError) as e:+ for _ in range(100):+ af.read(100)+ assert "Called 99 times!" in str(e)+++def test_file_like_must_be_seekable():+ audio_filename = FILENAMES_AND_SAMPLERATES[0][0]++ with open(audio_filename, 'rb') as f:+ stream = io.BytesIO(f.read())+ stream.seekable = lambda: False++ with pytest.raises(ValueError) as e:+ with pedalboard.io.AudioFile(stream):+ pass++ assert "seekable" in str(e)++def test_write_fails_without_extension(tmp_path: pathlib.Path):dest_path = str(tmp_path / "no_extension")with pytest.raises(ValueError):