Skip to content

πŸŽ› πŸ”Š A Python library for audio.

Add support for writing to file-like objects.

Peter Sobotd80d0dd3d5f8
diff --git a/pedalboard/io/AudioFileInit.h b/pedalboard/io/AudioFileInit.h
index 3f18900..ef6333e 100644
--- a/pedalboard/io/AudioFileInit.h
+++ b/pedalboard/io/AudioFileInit.h
@@ -42,7 +42,7 @@ inline void init_audio_file(py::module &m) {
py::class_<AudioFile, std::shared_ptr<AudioFile>>(
m, "AudioFile", "A base class for readable and writeable audio files.")
.def(py::init<>()) // Make this class effectively abstract; we can only
- // instantiate subclasses.
+ // instantiate subclasses via __new__.
.def_static(
"__new__",
[](const py::object *, std::string filename, std::string mode) {
@@ -107,6 +107,55 @@ inline void init_audio_file(py::module &m) {
},
py::arg("cls"), py::arg("filename"), py::arg("mode") = "w",
py::arg("samplerate") = py::none(), py::arg("num_channels") = 1,
- py::arg("bit_depth") = 16, py::arg("quality") = py::none());
+ py::arg("bit_depth") = 16, py::arg("quality") = py::none())
+ .def_static(
+ "__new__",
+ [](const py::object *, py::object filelike, std::string mode,
+ std::optional<double> sampleRate, int numChannels, int bitDepth,
+ std::optional<std::variant<std::string, float>> quality,
+ std::optional<std::string> format) {
+ if (mode == "r") {
+ throw py::type_error(
+ "Opening a file-like object for reading does not require "
+ "samplerate, num_channels, bit_depth, or quality arguments - "
+ "these parameters "
+ "will be read from the file-like object.");
+ } else if (mode == "w") {
+ if (!sampleRate) {
+ throw py::type_error("Opening a file-like object for writing "
+ "requires a samplerate "
+ "argument to be provided.");
+ }
+
+ if (!isWriteableFileLike(filelike)) {
+ throw py::type_error(
+ "Expected either a filename or a file-like object (with "
+ "write, seek, seekable, and tell methods), but received: " +
+ filelike.attr("__repr__")().cast<std::string>());
+ }
+
+ auto stream = std::make_unique<PythonOutputStream>(filelike);
+ if (!format && !stream->getFilename()) {
+ throw py::type_error(
+ "Unable to infer audio file format for writing. Expected "
+ "either a \".name\" property on the provided file-like "
+ "object (" +
+ filelike.attr("__repr__")().cast<std::string>() +
+ ") or an explicit file format passed as the \"format=\" "
+ "argument.");
+ }
+
+ return std::make_shared<WriteableAudioFile>(
+ format.value_or(""), std::move(stream), sampleRate.value(),
+ numChannels, bitDepth, quality);
+ } 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") = "w",
+ py::arg("samplerate") = py::none(), py::arg("num_channels") = 1,
+ py::arg("bit_depth") = 16, py::arg("quality") = py::none(),
+ py::arg("format") = py::none());
}
} // namespace Pedalboard
\ No newline at end of file
diff --git a/pedalboard/io/PythonFileLike.h b/pedalboard/io/PythonFileLike.h
new file mode 100644
index 0000000..1689c0c
--- /dev/null
+++ b/pedalboard/io/PythonFileLike.h
@@ -0,0 +1,132 @@
+/*
+ * 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 {
+
+namespace PythonException {
+// Check if there's a Python exception pending in the interpreter.
+inline bool isPending() {
+ py::gil_scoped_acquire acquire;
+ return PyErr_Occurred() != nullptr;
+}
+
+// If an exception is pending, raise it as a C++ exception to break the current
+// control flow and result in an error being thrown in Python later.
+inline void raise() {
+ py::gil_scoped_acquire acquire;
+
+ if (PyErr_Occurred()) {
+ py::error_already_set existingError;
+ throw existingError;
+ }
+}
+}; // namespace PythonException
+
+/**
+ * A base class for file-like Python object wrappers.
+ */
+class PythonFileLike {
+public:
+ PythonFileLike(py::object fileLike) : fileLike(fileLike) {}
+
+ std::string getRepresentation() {
+ py::gil_scoped_acquire acquire;
+ if (PythonException::isPending())
+ return "<__repr__ failed>";
+ return py::repr(fileLike).cast<std::string>();
+ }
+
+ std::optional<std::string> getFilename() noexcept {
+ // Some Python file-like objects expose a ".name" property.
+ // If this object has that property, return its value;
+ // otherwise return an empty optional.
+ py::gil_scoped_acquire acquire;
+
+ if (!PythonException::isPending() && py::hasattr(fileLike, "name")) {
+ return py::str(fileLike.attr("name")).cast<std::string>();
+ } else {
+ return {};
+ }
+ }
+
+ bool isSeekable() noexcept {
+ py::gil_scoped_acquire acquire;
+
+ if (!PythonException::isPending()) {
+ try {
+ return fileLike.attr("seekable")().cast<bool>();
+ } catch (py::error_already_set e) {
+ e.restore();
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ }
+ }
+
+ return false;
+ }
+
+ juce::int64 getPosition() noexcept {
+ py::gil_scoped_acquire acquire;
+
+ if (!PythonException::isPending()) {
+ try {
+ return fileLike.attr("tell")().cast<juce::int64>();
+ } catch (py::error_already_set e) {
+ e.restore();
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ }
+ }
+
+ return -1;
+ }
+
+ bool setPosition(juce::int64 pos) noexcept {
+ py::gil_scoped_acquire acquire;
+
+ if (!PythonException::isPending()) {
+ try {
+ if (fileLike.attr("seekable")().cast<bool>()) {
+ fileLike.attr("seek")(pos);
+ }
+
+ return fileLike.attr("tell")().cast<juce::int64>() == pos;
+ } catch (py::error_already_set e) {
+ e.restore();
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ }
+ }
+
+ return false;
+ }
+
+ py::object getFileLikeObject() { return fileLike; }
+
+protected:
+ py::object fileLike;
+};
+}; // namespace Pedalboard
\ No newline at end of file
diff --git a/pedalboard/io/PythonInputStream.h b/pedalboard/io/PythonInputStream.h
index 9070c48..9bfc541 100644
--- a/pedalboard/io/PythonInputStream.h
+++ b/pedalboard/io/PythonInputStream.h
@@ -23,6 +23,7 @@
namespace py = pybind11;
#include "../JuceHeader.h"
+#include "PythonFileLike.h"
namespace Pedalboard {
@@ -35,37 +36,35 @@ bool isReadableFileLike(py::object fileLike) {
* A juce::InputStream subclass that fetches its
* data from a provided Python file-like object.
*/
-class PythonInputStream : public juce::InputStream {
+class PythonInputStream : public juce::InputStream, public PythonFileLike {
public:
- PythonInputStream(py::object fileLike) : fileLike(fileLike) {
+ PythonInputStream(py::object fileLike) : PythonFileLike(fileLike) {
if (!isReadableFileLike(fileLike)) {
throw py::type_error("Expected a file-like object (with read, seek, "
"seekable, and tell methods).");
}
}
- 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() noexcept {
+ if (PythonException::isPending())
+ return false;
- bool isSeekable() {
try {
+ py::gil_scoped_acquire acquire;
return fileLike.attr("seekable")().cast<bool>();
} catch (py::error_already_set e) {
- pythonErrorRaised = true;
e.restore();
return false;
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ return false;
}
}
- juce::int64 getTotalLength() {
+ juce::int64 getTotalLength() noexcept {
+ if (PythonException::isPending())
+ return -1;
+
py::gil_scoped_acquire acquire;
// TODO: Try reading a couple of Python properties that may contain the
@@ -76,46 +75,68 @@ public:
if (!fileLike.attr("seekable")().cast<bool>()) {
return -1;
}
- } catch (py::error_already_set e) {
- pythonErrorRaised = true;
- e.restore();
- return 0;
- }
- if (totalLength == -1) {
- try {
+ if (totalLength == -1) {
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;
}
+ } catch (py::error_already_set e) {
+ e.restore();
+ return -1;
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ return -1;
}
return totalLength;
}
- int read(void *buffer, int bytesToRead) {
+ int read(void *buffer, int bytesToRead) noexcept {
// 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;
+ if (PythonException::isPending())
+ return 0;
+
+ py::bytes resultBytes;
{
py::gil_scoped_acquire acquire;
try {
- result = fileLike.attr("read")(bytesToRead).cast<py::bytes>();
+ auto readResult = fileLike.attr("read")(bytesToRead);
+
+ if (!py::isinstance<py::bytes>(readResult)) {
+ std::string message =
+ "File-like object passed to AudioFile was expected to return "
+ "bytes from its read(...) method, but "
+ "returned " +
+ py::str(readResult.get_type().attr("__name__"))
+ .cast<std::string>() +
+ ".";
+
+ if (py::hasattr(fileLike, "mode") &&
+ fileLike.attr("mode").cast<std::string>() == "r") {
+ message += " (Try opening the stream in \"rb\" mode instead of "
+ "\"r\" mode if possible.)";
+ }
+
+ throw py::type_error(message);
+ return 0;
+ }
+
+ resultBytes = readResult.cast<py::bytes>();
} catch (py::error_already_set e) {
- pythonErrorRaised = true;
e.restore();
return 0;
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ return 0;
}
}
- std::string resultAsString = result;
+ std::string resultAsString = resultBytes;
std::memcpy((char *)buffer, (char *)resultAsString.data(),
resultAsString.size());
@@ -125,7 +146,10 @@ public:
return bytesCopied;
}
- bool isExhausted() {
+ bool isExhausted() noexcept {
+ if (PythonException::isPending())
+ return true;
+
if (lastReadWasSmallerThanExpected) {
return true;
}
@@ -134,24 +158,34 @@ public:
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;
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ return true;
}
}
- juce::int64 getPosition() {
+ juce::int64 getPosition() noexcept {
+ if (PythonException::isPending())
+ return -1;
+
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;
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ return -1;
}
}
- bool setPosition(juce::int64 pos) {
+ bool setPosition(juce::int64 pos) noexcept {
+ if (PythonException::isPending())
+ return false;
+
try {
py::gil_scoped_acquire acquire;
@@ -161,30 +195,16 @@ public:
return fileLike.attr("tell")().cast<juce::int64>() == pos;
} catch (py::error_already_set e) {
- pythonErrorRaised = true;
e.restore();
return false;
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ 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 file
diff --git a/pedalboard/io/PythonOutputStream.h b/pedalboard/io/PythonOutputStream.h
new file mode 100644
index 0000000..a4eb150
--- /dev/null
+++ b/pedalboard/io/PythonOutputStream.h
@@ -0,0 +1,104 @@
+/*
+ * 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"
+#include "PythonFileLike.h"
+
+namespace Pedalboard {
+
+bool isWriteableFileLike(py::object fileLike) {
+ return py::hasattr(fileLike, "write") && py::hasattr(fileLike, "seek") &&
+ py::hasattr(fileLike, "tell") && py::hasattr(fileLike, "seekable");
+}
+
+/**
+ * A juce::OutputStream subclass that writes its
+ * data to a provided Python file-like object.
+ */
+class PythonOutputStream : public juce::OutputStream, public PythonFileLike {
+public:
+ PythonOutputStream(py::object fileLike) : PythonFileLike(fileLike) {
+ if (!isWriteableFileLike(fileLike)) {
+ throw py::type_error("Expected a file-like object (with write, seek, "
+ "seekable, and tell methods).");
+ }
+ }
+
+ virtual void flush() noexcept override {
+ py::gil_scoped_acquire acquire;
+ try {
+ if (py::hasattr(fileLike, "flush")) {
+ fileLike.attr("flush")();
+ }
+ } catch (py::error_already_set e) {
+ e.restore();
+ return;
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ return;
+ }
+ }
+
+ virtual juce::int64 getPosition() noexcept override {
+ return PythonFileLike::getPosition();
+ }
+
+ virtual bool setPosition(juce::int64 pos) noexcept override {
+ return PythonFileLike::setPosition(pos);
+ }
+
+ virtual bool write(const void *ptr, size_t numBytes) noexcept override {
+ py::gil_scoped_acquire acquire;
+
+ try {
+ int bytesWritten =
+ fileLike.attr("write")(py::bytes((const char *)ptr, numBytes))
+ .cast<int>();
+
+ if (bytesWritten < numBytes) {
+ return false;
+ }
+ } catch (py::error_already_set e) {
+ e.restore();
+ return false;
+ } catch (const py::builtin_exception &e) {
+ e.set_error();
+ return false;
+ }
+ return true;
+ }
+
+ virtual bool writeRepeatedByte(juce::uint8 byte,
+ size_t numTimesToRepeat) noexcept override {
+ // TODO: This could be made faster, but is it ever actually called?
+ for (int i = 0; i < numTimesToRepeat; i++) {
+ if (!write(&byte, 1)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+};
+}; // namespace Pedalboard
\ No newline at end of file
diff --git a/pedalboard/io/ReadableAudioFile.h b/pedalboard/io/ReadableAudioFile.h
index 12603c3..622a824 100644
--- a/pedalboard/io/ReadableAudioFile.h
+++ b/pedalboard/io/ReadableAudioFile.h
@@ -73,6 +73,7 @@ public:
formatManager.registerBasicFormats();
if (!inputStream->isSeekable()) {
+ PythonException::raise();
throw std::domain_error("Failed to open audio file-like object: input "
"stream must be seekable.");
}
@@ -89,11 +90,15 @@ public:
break;
}
+ // createReaderFor may have thrown a Python exception, under the hood
+ // which we need to check for before blindly continuing:
+ PythonException::raise();
+
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 "
+ "Input file-like object did not seek to the expected position. "
+ "The provided file-like object must be fully seekable to allow "
"reading audio files.");
}
}
@@ -110,6 +115,7 @@ public:
}
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 "
@@ -119,12 +125,15 @@ public:
}
}
+ PythonException::raise();
+
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.");
+ "Failed to open audio file-like object: " +
+ inputStream->getRepresentation() +
+ " does not seem to contain a known or supported format.");
- raisePendingPythonExceptions();
+ PythonException::raise();
}
double getSampleRate() const {
@@ -226,22 +235,25 @@ public:
}
if (reader->usesFloatingPointData || reader->bitsPerSample == 32) {
- if (!reader->read(channelPointers, numChannels, currentPosition,
- numSamples)) {
+ auto readResult = reader->read(channelPointers, numChannels,
+ currentPosition, numSamples);
+ PythonException::raise();
+
+ if (!readResult) {
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
// floating-point imprecision in JUCE when reading formats smaller than
// 32-bit (i.e.: 16-bit audio is off by about 0.003%)
-
- if (!reader->readSamples((int **)channelPointers, numChannels, 0,
- currentPosition, numSamples)) {
+ auto readResult =
+ reader->readSamples((int **)channelPointers, numChannels, 0,
+ currentPosition, numSamples);
+ PythonException::raise();
+ if (!readResult) {
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
@@ -342,11 +354,12 @@ public:
channelPointers[c] = ((int *)outputInfo.ptr) + (numSamples * c);
}
- if (!reader->readSamples(channelPointers, numChannels, 0,
- currentPosition, numSamples)) {
+ auto readResult = reader->readSamples(channelPointers, numChannels, 0,
+ currentPosition, numSamples);
+ PythonException::raise();
+ if (!readResult) {
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:
@@ -365,12 +378,15 @@ public:
channelPointers[c] = intBuffers[c].data();
}
- if (!reader->readSamples(channelPointers, numChannels, 0,
- currentPosition + startSample,
- samplesToRead)) {
+ auto readResult =
+ reader->readSamples(channelPointers, numChannels, 0,
+ currentPosition + startSample, samplesToRead);
+
+ PythonException::raise();
+
+ if (!readResult) {
throw std::runtime_error("Failed to read from file.");
}
- raisePendingPythonExceptions();
// Convert the data in intBuffers to the output format:
char shift = 32 - reader->bitsPerSample;
@@ -449,11 +465,6 @@ public:
close();
}
- void raisePendingPythonExceptions() {
- if (auto *inputStream = getPythonInputStream())
- inputStream->raisePendingPythonExceptions();
- }
-
private:
juce::AudioFormatManager formatManager;
std::string filename;
@@ -472,19 +483,17 @@ inline void init_readable_audio_file(py::module &m) {
"pedalboard.io.get_supported_read_formats() to see which formats are "
"supported on the current platform.)")
.def(py::init([](std::string filename) {
- return std::make_shared<ReadableAudioFile>(filename);
+ // This definition is only here to provide nice docstrings.
+ throw std::runtime_error(
+ "Internal error: __init__ should never be called, as this "
+ "class implements __new__.");
}),
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));
+ // This definition is only here to provide nice docstrings.
+ throw std::runtime_error(
+ "Internal error: __init__ should never be called, as this "
+ "class implements __new__.");
}),
py::arg("file_like"))
.def_static(
@@ -493,6 +502,20 @@ inline void init_readable_audio_file(py::module &m) {
return std::make_shared<ReadableAudioFile>(filename);
},
py::arg("cls"), py::arg("filename"))
+ .def_static(
+ "__new__",
+ [](const py::object *, 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: " +
+ py::repr(filelike).cast<std::string>());
+ }
+
+ return std::make_shared<ReadableAudioFile>(
+ std::make_unique<PythonInputStream>(filelike));
+ },
+ py::arg("cls"), py::arg("file_like"))
.def(
"read", &ReadableAudioFile::read, py::arg("num_frames") = 0,
"Read the given number of frames (samples in each channel) from this "
@@ -526,10 +549,7 @@ inline void init_readable_audio_file(py::module &m) {
ss << " filename=\"" << file.getFilename() << "\"";
} else if (PythonInputStream *stream =
file.getPythonInputStream()) {
- ss << " file_like="
- << stream->getFileLikeObject()
- .attr("__repr__")()
- .cast<std::string>();
+ ss << " file_like=" << stream->getRepresentation();
}
if (file.isClosed()) {
diff --git a/pedalboard/io/WriteableAudioFile.h b/pedalboard/io/WriteableAudioFile.h
index 8f3c214..9155a29 100644
--- a/pedalboard/io/WriteableAudioFile.h
+++ b/pedalboard/io/WriteableAudioFile.h
@@ -26,6 +26,7 @@
#include "../BufferUtils.h"
#include "../JuceHeader.h"
#include "AudioFile.h"
+#include "PythonOutputStream.h"
namespace py = pybind11;
@@ -190,7 +191,14 @@ public:
std::string filename, double writeSampleRate, int numChannels = 1,
int bitDepth = 16,
std::optional<std::variant<std::string, float>> qualityInput = {})
- : filename(filename) {
+ : WriteableAudioFile(filename, nullptr, writeSampleRate, numChannels,
+ bitDepth, qualityInput) {}
+
+ WriteableAudioFile(
+ std::string filename,
+ std::unique_ptr<PythonOutputStream> pythonOutputStream,
+ double writeSampleRate, int numChannels = 1, int bitDepth = 16,
+ std::optional<std::variant<std::string, float>> qualityInput = {}) {
pybind11::gil_scoped_release release;
if (!isInteger(writeSampleRate)) {
@@ -209,28 +217,63 @@ public:
}
formatManager.registerBasicFormats();
- juce::File file(filename);
+ std::unique_ptr<juce::OutputStream> outputStream;
+ juce::AudioFormat *format = nullptr;
+ std::string extension;
+
+ if (pythonOutputStream) {
+ // Use the pythonOutputStream's filename if possible, falling back to the
+ // provided filename string (which should contain an extension) if
+ // necessary.
+ std::string filenameOrFormat;
+ if (!filename.empty()) {
+ filenameOrFormat = ("." + filename);
+ } else if (auto streamName = pythonOutputStream->getFilename()) {
+ filenameOrFormat = streamName.value();
+ }
- std::unique_ptr<AutoDeleteFileOutputStream> outputStream =
- AutoDeleteFileOutputStream::createOutputStream(file);
- if (!outputStream->openedOk()) {
- throw std::domain_error("Unable to open audio file for writing: " +
- filename);
- }
+ juce::File file(filenameOrFormat);
+ extension = file.getFileExtension().toStdString();
- juce::AudioFormat *format =
- formatManager.findFormatForFileExtension(file.getFileExtension());
+ format = formatManager.findFormatForFileExtension(extension);
- if (!format) {
- if (file.getFileExtension().isEmpty()) {
- throw std::domain_error("No file extension provided - cannot detect "
- "audio format to write with for file path: " +
+ if (!format) {
+ if (pythonOutputStream->getFilename()) {
+ throw std::domain_error("Unable to detect audio format to use for "
+ "file-like object with filename: " +
+ pythonOutputStream->getFilename().value());
+ } else {
+ throw std::domain_error(
+ "Provided format argument (\"" + filename +
+ "\") does not correspond to a supported file type.");
+ }
+ }
+
+ unsafeOutputStream = pythonOutputStream.get();
+ outputStream = std::move(pythonOutputStream);
+ } else {
+ juce::File file(filename);
+ extension = file.getFileExtension().toStdString();
+
+ outputStream = AutoDeleteFileOutputStream::createOutputStream(file);
+ if (!static_cast<juce::FileOutputStream *>(outputStream.get())
+ ->openedOk()) {
+ throw std::domain_error("Unable to open audio file for writing: " +
filename);
}
- throw std::domain_error(
- "Unable to detect audio format for file extension: " +
- file.getFileExtension().toStdString());
+ format = formatManager.findFormatForFileExtension(extension);
+
+ if (!format) {
+ if (extension.empty()) {
+ throw std::domain_error("No file extension provided - cannot detect "
+ "audio format to write with for filename: " +
+ filename);
+ }
+
+ throw std::domain_error(
+ "Unable to detect audio format for file extension: " + extension);
+ }
}
// Normalize the input to a string here, as we need to do parsing anyways:
@@ -259,13 +302,14 @@ public:
numChannels, bitDepth, emptyMetadata,
qualityOptionIndex));
if (!writer) {
+ PythonException::raise();
+
// Check common errors first:
juce::Array<int> possibleSampleRates = format->getPossibleSampleRates();
if (possibleSampleRates.isEmpty()) {
throw std::domain_error(
- file.getFileExtension().toStdString() +
- " audio files are not writable with Pedalboard.");
+ extension + " audio files are not writable with Pedalboard.");
}
if (!possibleSampleRates.contains((int)writeSampleRate)) {
@@ -286,8 +330,7 @@ public:
if (possibleBitDepths.isEmpty()) {
throw std::domain_error(
- file.getFileExtension().toStdString() +
- " audio files are not writable with Pedalboard.");
+ extension + " audio files are not writable with Pedalboard.");
}
if (!possibleBitDepths.contains((int)bitDepth)) {
@@ -312,12 +355,16 @@ public:
}
throw std::domain_error(
- "Unable to create audio file writer with samplerate=" +
- std::to_string(writeSampleRate) +
+ "Unable to create " + format->getFormatName().toStdString() +
+ " writer with samplerate=" + std::to_string(writeSampleRate) +
", num_channels=" + std::to_string(numChannels) + ", bit_depth=" +
std::to_string(bitDepth) + ", and quality=" + humanReadableQuality);
} else {
+ // If we have a writer object, it now owns the OutputStream we passed in
+ // - so we need to release it before possibly throwing an exception, or
+ // the stream will leak.
outputStream.release();
+ PythonException::raise();
}
}
@@ -372,7 +419,7 @@ public:
return;
} else if (numChannels != getNumChannels()) {
throw std::runtime_error(
- "WritableAudioFile was opened with num_channels=" +
+ "WriteableAudioFile was opened with num_channels=" +
std::to_string(getNumChannels()) +
", but was passed an array containing " +
std::to_string(numChannels) + "-channel audio!");
@@ -413,6 +460,7 @@ public:
if (!write(channelPointers, numChannels, samplesToWrite)) {
throw std::runtime_error("Unable to write data to audio file.");
}
+ PythonException::raise();
}
break;
@@ -427,6 +475,7 @@ public:
if (!write(channelPointers, numChannels, numSamples)) {
throw std::runtime_error("Unable to write data to audio file.");
}
+ PythonException::raise();
break;
}
default:
@@ -610,11 +659,26 @@ public:
close();
}
+ PythonOutputStream *getPythonOutputStream() const {
+ if (!filename.empty()) {
+ return nullptr;
+ }
+ if (!writer) {
+ return nullptr;
+ }
+
+ // the AudioFormatWriter retains exclusive ownership over the output stream,
+ // and doesn't expose it - so we keep our own reference (which may be
+ // deallocated out from under us!)
+ return unsafeOutputStream;
+ }
+
private:
juce::AudioFormatManager formatManager;
std::string filename;
std::optional<std::string> quality;
std::unique_ptr<juce::AudioFormatWriter> writer;
+ PythonOutputStream *unsafeOutputStream = nullptr;
juce::CriticalSection objectLock;
int framesWritten = 0;
};
@@ -631,12 +695,26 @@ inline void init_writeable_audio_file(py::module &m) {
py::init([](std::string filename, double sampleRate, int numChannels,
int bitDepth,
std::optional<std::variant<std::string, float>> quality) {
- return std::make_shared<WriteableAudioFile>(
- filename, sampleRate, numChannels, bitDepth, quality);
+ // This definition is only here to provide nice docstrings.
+ throw std::runtime_error(
+ "Internal error: __init__ should never be called, as this "
+ "class implements __new__.");
}),
py::arg("filename"), py::arg("samplerate"),
py::arg("num_channels") = 1, py::arg("bit_depth") = 16,
py::arg("quality") = py::none())
+ .def(py::init([](py::object filelike, double sampleRate, int numChannels,
+ int bitDepth,
+ std::optional<std::variant<std::string, float>> quality,
+ std::optional<std::string> format) {
+ // This definition is only here to provide nice docstrings.
+ throw std::runtime_error(
+ "Internal error: __init__ should never be called, as this "
+ "class implements __new__.");
+ }),
+ py::arg("file_like"), py::arg("samplerate"),
+ py::arg("num_channels") = 1, py::arg("bit_depth") = 16,
+ py::arg("quality") = py::none(), py::arg("format") = py::none())
.def_static(
"__new__",
[](const py::object *, std::string filename,
@@ -653,6 +731,43 @@ inline void init_writeable_audio_file(py::module &m) {
py::arg("cls"), py::arg("filename"),
py::arg("samplerate") = py::none(), py::arg("num_channels") = 1,
py::arg("bit_depth") = 16, py::arg("quality") = py::none())
+ .def_static(
+ "__new__",
+ [](const py::object *, py::object filelike,
+ std::optional<double> sampleRate, int numChannels, int bitDepth,
+ std::optional<std::variant<std::string, float>> quality,
+ std::optional<std::string> format) {
+ if (!sampleRate) {
+ throw py::type_error(
+ "Opening an audio file for writing requires a samplerate "
+ "argument to be provided.");
+ }
+ if (!isWriteableFileLike(filelike)) {
+ throw py::type_error(
+ "Expected either a filename or a file-like object (with "
+ "write, seek, seekable, and tell methods), but received: " +
+ py::repr(filelike).cast<std::string>());
+ }
+
+ auto stream = std::make_unique<PythonOutputStream>(filelike);
+ if (!format && !stream->getFilename()) {
+ throw py::type_error(
+ "Unable to infer audio file format for writing. Expected "
+ "either a \".name\" property on the provided file-like "
+ "object (" +
+ py::repr(filelike).cast<std::string>() +
+ ") or an explicit file format passed as the \"format=\" "
+ "argument.");
+ }
+
+ return std::make_shared<WriteableAudioFile>(
+ format.value_or(""), std::move(stream), sampleRate.value(),
+ numChannels, bitDepth, quality);
+ },
+ py::arg("cls"), py::arg("file_like"),
+ py::arg("samplerate") = py::none(), py::arg("num_channels") = 1,
+ py::arg("bit_depth") = 16, py::arg("quality") = py::none(),
+ py::arg("format") = py::none())
.def(
"write",
[](WriteableAudioFile &file, py::array_t<char> samples) {
@@ -725,9 +840,14 @@ inline void init_writeable_audio_file(py::module &m) {
[](const WriteableAudioFile &file) {
std::ostringstream ss;
ss << "<pedalboard.io.WriteableAudioFile";
+
if (!file.getFilename().empty()) {
ss << " filename=\"" << file.getFilename() << "\"";
+ } else if (PythonOutputStream *stream =
+ file.getPythonOutputStream()) {
+ ss << " file_like=" << stream->getRepresentation();
}
+
if (file.isClosed()) {
ss << " closed";
} else {
diff --git a/tests/test_io.py b/tests/test_io.py
index e4252bf..1cc24e5 100644
--- a/tests/test_io.py
+++ b/tests/test_io.py
@@ -331,10 +331,7 @@ def test_read_mp3_from_named_stream(mp3_filename: str):
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 = open(audio_filename, "rb")
stream_read = stream.read
call_count = [0]
@@ -369,12 +366,92 @@ def test_file_like_must_be_seekable():
assert "seekable" in str(e)
+def test_no_crash_if_type_error_on_file_like():
+ audio_filename = FILENAMES_AND_SAMPLERATES[0][0]
+
+ with open(audio_filename, "rb") as f:
+ stream = io.BytesIO(f.read())
+
+ # Seekable should be a method, not a property:
+ stream.seekable = False
+
+ with pytest.raises(TypeError) as e:
+ with pedalboard.io.AudioFile(stream):
+ pass
+
+ assert "bool" in str(e)
+
+
def test_write_fails_without_extension(tmp_path: pathlib.Path):
dest_path = str(tmp_path / "no_extension")
with pytest.raises(ValueError):
pedalboard.io.AudioFile(dest_path, "w", 44100, 1)
[email protected]("extension", pedalboard.io.get_supported_write_formats())
+def test_write_to_stream_supports_format(extension: str):
+ assert pedalboard.io.AudioFile(io.BytesIO(), "w", 44100, 2, format=extension) is not None
+ assert pedalboard.io.AudioFile(io.BytesIO(), "w", 44100, 2, format=extension[1:]) is not None
+ assert pedalboard.io.WriteableAudioFile(io.BytesIO(), 44100, 2, format=extension) is not None
+ assert (
+ pedalboard.io.WriteableAudioFile(io.BytesIO(), 44100, 2, format=extension[1:]) is not None
+ )
+
+ with pytest.raises(ValueError):
+ pedalboard.io.AudioFile(io.BytesIO(), "w", 44100, 2, format="txt")
+
+
[email protected]("extension", pedalboard.io.get_supported_write_formats())
+def test_write_to_stream_prefers_format_over_stream_name(extension: str):
+ stream = io.BytesIO()
+ stream.name = "foo.txt"
+ assert pedalboard.io.AudioFile(stream, "w", 44100, 2, format=extension) is not None
+ assert pedalboard.io.AudioFile(stream, "w", 44100, 2, format=extension[1:]) is not None
+ assert pedalboard.io.WriteableAudioFile(stream, 44100, 2, format=extension) is not None
+ assert pedalboard.io.WriteableAudioFile(stream, 44100, 2, format=extension[1:]) is not None
+
+ with pytest.raises(ValueError):
+ pedalboard.io.AudioFile(stream, "w", 44100, 2)
+
+
[email protected]("extension", pedalboard.io.get_supported_write_formats())
+def test_read_from_non_bytes_stream(extension: str):
+ stream = io.StringIO()
+ stream.name = f"foo{extension}"
+
+ with pytest.raises(TypeError) as e:
+ pedalboard.io.AudioFile(stream, "r")
+
+ assert "expected to return bytes" in str(e)
+ assert "returned str" in str(e)
+
+ with pytest.raises(TypeError) as e:
+ pedalboard.io.ReadableAudioFile(stream)
+
+ assert "expected to return bytes" in str(e)
+ assert "returned str" in str(e)
+
+
[email protected]("extension", pedalboard.io.get_supported_write_formats())
+def test_write_to_non_bytes_stream(extension: str):
+ stream = io.StringIO()
+
+ try:
+ stream.write(b"")
+ except TypeError as e:
+ expected_message = e.args[0]
+
+ with pytest.raises(TypeError) as e:
+ pedalboard.io.AudioFile(stream, "w", 44100, 2, format=extension)
+
+ assert expected_message in str(e)
+
+ with pytest.raises(TypeError) as e:
+ pedalboard.io.WriteableAudioFile(stream, 44100, 2, format=extension)
+
+ assert expected_message in str(e)
+
+
def test_fails_gracefully():
with pytest.raises(ValueError):
pedalboard.io.AudioFile(__file__)
@@ -511,7 +588,64 @@ def test_basic_write_int32_to_16_bit_wav(tmp_path: pathlib.Path):
with pedalboard.io.ReadableAudioFile(filename) as af:
as_written = af.read(len(signal))[0]
- np.testing.assert_allclose(original, as_written, atol=2 / (2 ** 15))
+ np.testing.assert_allclose(original, as_written, atol=2 / (2**15))
+
+
[email protected]("extension", pedalboard.io.get_supported_write_formats())
+ "samplerate", [8000, 11025, 12000, 16000, 22050, 32000, 44100, 48000, 88200, 96000]
+)
[email protected]("num_channels", [1, 2, 3])
[email protected]("transposed", [False, True])
[email protected]("input_format", [np.float32, np.float64, np.int8, np.int16, np.int32])
+def test_write_to_seekable_stream(
+ extension: str, samplerate: float, num_channels: int, transposed: bool, input_format
+):
+ original_audio = generate_sine_at(samplerate, num_channels=num_channels)
+
+ write_bit_depth = 16
+
+ # Not all formats support full 32-bit depth:
+ if extension in {".wav"} and np.issubdtype(input_format, np.signedinteger):
+ write_bit_depth = np.dtype(input_format).itemsize * 8
+
+ # Handle integer audio types by scaling the floating-point data to the full integer range:
+ if np.issubdtype(input_format, np.signedinteger):
+ _max = np.iinfo(input_format).max
+ audio = (original_audio * _max).astype(input_format)
+ else:
+ _max = 1.0
+ audio = original_audio.astype(input_format)
+
+ # Before writing, assert that the data we're about to write is what we expect:
+ tolerance = get_tolerance_for_format_and_bit_depth(".wav", input_format, "int16")
+ np.testing.assert_allclose(original_audio, audio.astype(np.float32) / _max, atol=tolerance)
+
+ num_samples = audio.shape[-1]
+
+ stream = io.BytesIO()
+ stream.name = f"my_file{extension}"
+
+ with pedalboard.io.WriteableAudioFile(
+ stream,
+ samplerate=samplerate,
+ num_channels=num_channels,
+ bit_depth=write_bit_depth,
+ ) as af:
+ if transposed:
+ af.write(audio.T)
+ else:
+ af.write(audio)
+
+ assert stream.tell() > 0
+ stream.seek(0)
+
+ with pedalboard.io.AudioFile(stream) as af:
+ assert af.samplerate == samplerate
+ assert af.channels == num_channels
+ tolerance = get_tolerance_for_format_and_bit_depth(extension, input_format, af.file_dtype)
+ as_written = af.read(num_samples)
+ np.testing.assert_allclose(original_audio, np.squeeze(as_written), atol=tolerance)
@pytest.mark.parametrize("extension", pedalboard.io.get_supported_write_formats())