Skip to content

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

Add reset option to process() to allow multi-stage renders.

Peter Sobot7929317bbf65
diff --git a/pedalboard/ExternalPlugin.h b/pedalboard/ExternalPlugin.h
index 9ede37b..b79c34f 100644
--- a/pedalboard/ExternalPlugin.h
+++ b/pedalboard/ExternalPlugin.h
@@ -291,11 +291,16 @@ public:
void prepare(const juce::dsp::ProcessSpec &spec) override {
if (pluginInstance) {
- setNumChannels(spec.numChannels);
- pluginInstance->setRateAndBufferSizeDetails(spec.sampleRate,
- spec.maximumBlockSize);
- pluginInstance->prepareToPlay(spec.sampleRate, spec.maximumBlockSize);
- pluginInstance->setNonRealtime(true);
+ if (lastSpec.sampleRate != spec.sampleRate ||
+ lastSpec.maximumBlockSize != spec.maximumBlockSize ||
+ spec.numChannels != lastSpec.numChannels) {
+ setNumChannels(spec.numChannels);
+ pluginInstance->setRateAndBufferSizeDetails(spec.sampleRate,
+ spec.maximumBlockSize);
+ pluginInstance->prepareToPlay(spec.sampleRate, spec.maximumBlockSize);
+ pluginInstance->setNonRealtime(true);
+ lastSpec = spec;
+ }
}
}
diff --git a/pedalboard/JucePlugin.h b/pedalboard/JucePlugin.h
index cb83144..dae52aa 100644
--- a/pedalboard/JucePlugin.h
+++ b/pedalboard/JucePlugin.h
@@ -50,7 +50,12 @@ public:
virtual ~JucePlugin(){};
void prepare(const juce::dsp::ProcessSpec &spec) override {
- dspBlock.prepare(spec);
+ if (lastSpec.sampleRate != spec.sampleRate ||
+ lastSpec.maximumBlockSize != spec.maximumBlockSize ||
+ spec.numChannels != lastSpec.numChannels) {
+ dspBlock.prepare(spec);
+ lastSpec = spec;
+ }
}
void process(
diff --git a/pedalboard/Plugin.h b/pedalboard/Plugin.h
index ac1789e..6392300 100644
--- a/pedalboard/Plugin.h
+++ b/pedalboard/Plugin.h
@@ -39,5 +39,8 @@ public:
// thread-safe. Note: use std::lock or std::scoped_lock when locking multiple
// plugins to avoid deadlocking.
std::mutex mutex;
+
+protected:
+ juce::dsp::ProcessSpec lastSpec;
};
} // namespace Pedalboard
\ No newline at end of file
diff --git a/pedalboard/pedalboard.py b/pedalboard/pedalboard.py
index bd080ae..786037b 100644
--- a/pedalboard/pedalboard.py
+++ b/pedalboard/pedalboard.py
@@ -103,6 +103,7 @@ class Pedalboard(collections.MutableSequence):
audio: np.ndarray,
sample_rate: Optional[float] = None,
buffer_size: Optional[int] = None,
+ reset: bool = True,
) -> np.ndarray:
if sample_rate is not None and not isinstance(sample_rate, (int, float)):
raise TypeError("sample_rate must be None, an integer, or a floating-point number.")
@@ -121,7 +122,7 @@ class Pedalboard(collections.MutableSequence):
)
# pyBind11 makes a copy of self.plugins when passing it into process.
- kwargs = {"sample_rate": effective_sample_rate, "plugins": self.plugins}
+ kwargs = {"sample_rate": effective_sample_rate, "plugins": self.plugins, "reset": reset}
if buffer_size:
kwargs["buffer_size"] = buffer_size
return process(audio, **kwargs)
diff --git a/pedalboard/process.h b/pedalboard/process.h
index c804522..8cc5472 100644
--- a/pedalboard/process.h
+++ b/pedalboard/process.h
@@ -38,10 +38,10 @@ template <typename SampleType>
py::array_t<float>
process(const py::array_t<SampleType, py::array::c_style> inputArray,
double sampleRate, const std::vector<Plugin *> &plugins,
- unsigned int bufferSize) {
+ unsigned int bufferSize, bool reset) {
const py::array_t<float, py::array::c_style> float32InputArray =
inputArray.attr("astype")("float32");
- return process(float32InputArray, sampleRate, plugins, bufferSize);
+ return process(float32InputArray, sampleRate, plugins, bufferSize, reset);
}
/**
@@ -50,9 +50,9 @@ process(const py::array_t<SampleType, py::array::c_style> inputArray,
template <typename SampleType>
py::array_t<float>
processSingle(const py::array_t<SampleType, py::array::c_style> inputArray,
- double sampleRate, Plugin &plugin, unsigned int bufferSize) {
+ double sampleRate, Plugin &plugin, unsigned int bufferSize, bool reset) {
std::vector<Plugin *> plugins{&plugin};
- return process<SampleType>(inputArray, sampleRate, plugins, bufferSize);
+ return process<SampleType>(inputArray, sampleRate, plugins, bufferSize, reset);
}
/**
@@ -64,7 +64,7 @@ template <>
py::array_t<float>
process<float>(const py::array_t<float, py::array::c_style> inputArray,
double sampleRate, const std::vector<Plugin *> &plugins,
- unsigned int bufferSize) {
+ unsigned int bufferSize, bool reset) {
// Numpy/Librosa convention is (num_samples, num_channels)
py::buffer_info inputInfo = inputArray.request();
@@ -152,10 +152,12 @@ process<float>(const py::array_t<float, py::array::c_style> inputArray,
std::make_unique<std::scoped_lock<std::mutex>>(plugin->mutex));
}
- for (auto *plugin : plugins) {
- if (plugin == nullptr)
- continue;
- plugin->reset();
+ if (reset) {
+ for (auto *plugin : plugins) {
+ if (plugin == nullptr)
+ continue;
+ plugin->reset();
+ }
}
juce::dsp::ProcessSpec spec;
diff --git a/pedalboard/python_bindings.cpp b/pedalboard/python_bindings.cpp
index 3e87d10..4f41929 100644
--- a/pedalboard/python_bindings.cpp
+++ b/pedalboard/python_bindings.cpp
@@ -54,20 +54,20 @@ PYBIND11_MODULE(pedalboard_native, m) {
"Run a 32-bit floating point audio buffer through a list of Pedalboard "
"plugins.",
py::arg("input_array"), py::arg("sample_rate"), py::arg("plugins"),
- py::arg("buffer_size") = DEFAULT_BUFFER_SIZE);
+ py::arg("buffer_size") = DEFAULT_BUFFER_SIZE, py::arg("reset") = true);
m.def("process", process<double>,
"Run a 64-bit floating point audio buffer through a list of Pedalboard "
"plugins. The buffer will be converted to 32-bit for processing.",
py::arg("input_array"), py::arg("sample_rate"), py::arg("plugins"),
- py::arg("buffer_size") = DEFAULT_BUFFER_SIZE);
+ py::arg("buffer_size") = DEFAULT_BUFFER_SIZE, py::arg("reset") = true);
m.def("process", processSingle<float>,
"Run a 32-bit floating point audio buffer through a single Pedalboard "
"plugin. (Note: if calling this multiple times with multiple plugins, "
"consider passing a list of plugins instead.)",
py::arg("input_array"), py::arg("sample_rate"), py::arg("plugin"),
- py::arg("buffer_size") = DEFAULT_BUFFER_SIZE);
+ py::arg("buffer_size") = DEFAULT_BUFFER_SIZE, py::arg("reset") = true);
m.def("process", processSingle<double>,
"Run a 64-bit floating point audio buffer through a single Pedalboard "
@@ -75,7 +75,7 @@ PYBIND11_MODULE(pedalboard_native, m) {
"consider passing a list of plugins instead.) The buffer will be "
"converted to 32-bit for processing.",
py::arg("input_array"), py::arg("sample_rate"), py::arg("plugin"),
- py::arg("buffer_size") = DEFAULT_BUFFER_SIZE);
+ py::arg("buffer_size") = DEFAULT_BUFFER_SIZE, py::arg("reset") = true);
auto plugin =
py::class_<Plugin>(m, "Plugin",
@@ -94,24 +94,26 @@ PYBIND11_MODULE(pedalboard_native, m) {
"process",
[](Plugin *self,
const py::array_t<float, py::array::c_style> inputArray,
- double sampleRate, unsigned int bufferSize) {
- return process(inputArray, sampleRate, {self}, bufferSize);
+ double sampleRate, unsigned int bufferSize, bool reset) {
+ return process(inputArray, sampleRate, {self}, bufferSize,
+ reset);
},
"Run a 32-bit floating point audio buffer through this plugin."
"(Note: if calling this multiple times with multiple plugins, "
"consider using pedalboard.process(...) instead.)",
py::arg("input_array"), py::arg("sample_rate"),
- py::arg("buffer_size") = DEFAULT_BUFFER_SIZE)
+ py::arg("buffer_size") = DEFAULT_BUFFER_SIZE,
+ py::arg("reset") = true)
.def(
"process",
[](Plugin *self,
const py::array_t<double, py::array::c_style> inputArray,
- double sampleRate, unsigned int bufferSize) {
+ double sampleRate, unsigned int bufferSize, bool reset) {
const py::array_t<float, py::array::c_style> float32InputArray =
inputArray.attr("astype")("float32");
return process(float32InputArray, sampleRate, {self},
- bufferSize);
+ bufferSize, reset);
},
"Run a 64-bit floating point audio buffer through this plugin."
"(Note: if calling this multiple times with multiple plugins, "
@@ -119,7 +121,8 @@ PYBIND11_MODULE(pedalboard_native, m) {
"will be "
"converted to 32-bit for processing.",
py::arg("input_array"), py::arg("sample_rate"),
- py::arg("buffer_size") = DEFAULT_BUFFER_SIZE);
+ py::arg("buffer_size") = DEFAULT_BUFFER_SIZE,
+ py::arg("reset") = true);
plugin.attr("__call__") = plugin.attr("process");
init_chorus(m);