Skip to content

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

Move most GSMCompressor buffering logic to utility classes.

Peter Sobot3def00e76a77
diff --git a/pedalboard/plugin_templates/FixedBlockSizePlugin.h b/pedalboard/plugin_templates/FixedBlockSizePlugin.h
new file mode 100644
index 0000000..a9a6e88
--- /dev/null
+++ b/pedalboard/plugin_templates/FixedBlockSizePlugin.h
@@ -0,0 +1,350 @@
+/*
+ * pedalboard
+ * Copyright 2022 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 "../JuceHeader.h"
+#include "../Plugin.h"
+#include "../plugins/AddLatency.h"
+#include <mutex>
+
+namespace Pedalboard {
+
+/**
+ * A template class that wraps a Pedalboard plugin,
+ * but ensures that its process() function is only ever passed a fixed
+ * block size. This block size can be set in the prepare() method, or as a
+ * template argument.
+ */
+template <typename T, unsigned int DefaultBlockSize = 0,
+ typename SampleType = float>
+class FixedBlockSize : public Plugin {
+public:
+ virtual ~FixedBlockSize(){};
+
+ virtual void prepare(const juce::dsp::ProcessSpec &spec) {
+ if (lastSpec.sampleRate != spec.sampleRate ||
+ lastSpec.maximumBlockSize != spec.maximumBlockSize ||
+ lastSpec.numChannels != spec.numChannels) {
+ if (spec.maximumBlockSize % blockSize == 0) {
+ // We need much less intermediate memory in this case:
+ inputBuffer.setSize(spec.numChannels, blockSize);
+ outputBuffer.clear();
+ inStreamLatency = 0;
+ } else {
+ inputBuffer.setSize(spec.numChannels,
+ blockSize * 2 + spec.maximumBlockSize * 2);
+ outputBuffer.setSize(spec.numChannels,
+ blockSize * 2 + spec.maximumBlockSize * 2);
+ // Add enough latency to the stream to allow us to process an entire
+ // block:
+ inStreamLatency = blockSize;
+ }
+ lastSpec = spec;
+ }
+
+ // Tell the delegate plugin that its maximum block
+ // size is the fixed size we'll be sending in:
+ juce::dsp::ProcessSpec newSpec = spec;
+ newSpec.maximumBlockSize = blockSize;
+ plugin.prepare(newSpec);
+ }
+
+ virtual int
+ process(const juce::dsp::ProcessContextReplacing<SampleType> &context) {
+ auto ioBlock = context.getOutputBlock();
+
+ if (lastSpec.maximumBlockSize % blockSize == 0) {
+ // The best case scenario: the input is evenly divisible
+ // by the fixed block size, so we need no buffers!
+ int samplesOutput = 0;
+ for (int i = 0; i < ioBlock.getNumSamples(); i += blockSize) {
+ unsigned long samplesAvailable =
+ std::min((unsigned long)blockSize, ioBlock.getNumSamples() - i);
+ if (samplesAvailable < blockSize)
+ break;
+
+ juce::dsp::AudioBlock<SampleType> subBlock =
+ ioBlock.getSubBlock(i, blockSize);
+ juce::dsp::ProcessContextReplacing<SampleType> subContext(subBlock);
+ int samplesOutputThisBlock = plugin.process(subContext);
+
+ if (samplesOutputThisBlock != blockSize) {
+ throw std::runtime_error(
+ "FixedBlockSize currently requires wrapped plugins to impart no "
+ "delay. This is an internal Pedalboard error and should be "
+ "reported.");
+ }
+
+ if (samplesOutput > 0 && samplesOutputThisBlock < blockSize) {
+ throw std::runtime_error("Plugin that returns fixed-size blocks "
+ "returned too few samples!");
+ }
+ samplesOutput += samplesOutputThisBlock;
+ }
+
+ int remainderInSamples = ioBlock.getNumSamples() % blockSize;
+ if (remainderInSamples > 0) {
+ if (samplesProcessed > 0) {
+ // We're at the end of our buffer, so pad with zeros.
+
+ int offset = ioBlock.getNumSamples() - remainderInSamples;
+
+ // Copy the remainder into inputBuffer:
+ juce::dsp::AudioBlock<SampleType> inputBlock(inputBuffer);
+ juce::dsp::AudioBlock<SampleType> subBlock =
+ inputBlock.getSubBlock(0, blockSize);
+
+ subBlock.clear();
+ subBlock.copyFrom(ioBlock.getSubBlock(offset, remainderInSamples));
+
+ juce::dsp::ProcessContextReplacing<SampleType> subContext(subBlock);
+ int samplesOutputThisBlock = plugin.process(subContext);
+ if (samplesOutputThisBlock != blockSize) {
+ throw std::runtime_error(
+ "FixedBlockSize currently requires wrapped plugins to impart "
+ "no delay. This is an internal Pedalboard error and should be "
+ "reported.");
+ }
+
+ if (samplesOutput > 0 && samplesOutputThisBlock < blockSize) {
+ throw std::runtime_error(
+ "Plugin that returns fixed-size blocks returned too few "
+ "samples! This is an internal Pedalboard error and should be "
+ "reported.");
+ }
+
+ // Copy the output back into ioBlock, right-aligned:
+ ioBlock
+ .getSubBlock(ioBlock.getNumSamples() - remainderInSamples,
+ remainderInSamples)
+ .copyFrom(subBlock);
+
+ samplesOutput += remainderInSamples;
+ } else {
+ jassertfalse;
+ }
+ }
+
+ samplesProcessed += samplesOutput;
+ return samplesOutput;
+ } else {
+ // We have to render three parts:
+ // 1) Push as many samples as possible into inputBuffer
+ int samplesToAddToInputBuffer =
+ std::min((int)inputBuffer.getNumSamples() - (int)inputBufferSamples,
+ (int)ioBlock.getNumSamples());
+
+ ioBlock.copyTo(inputBuffer, 0, inputBufferSamples,
+ samplesToAddToInputBuffer);
+ inputBufferSamples += samplesToAddToInputBuffer;
+
+ // 2) Copy the output from the previous render call into the ioBlock
+ int samplesOutput = 0;
+
+ int minimumSamplesToOutput = ioBlock.getNumSamples();
+ if (ioBlock.getNumSamples() == lastSpec.maximumBlockSize) {
+ minimumSamplesToOutput += inStreamLatency;
+ }
+ if (outputBufferSamples >= minimumSamplesToOutput) {
+ ioBlock.copyFrom(outputBuffer, 0, 0, ioBlock.getNumSamples());
+ outputBufferSamples -= ioBlock.getNumSamples();
+
+ // Move the remainder of the output buffer to the left:
+ if (outputBufferSamples > 0) {
+ for (int i = 0; i < outputBuffer.getNumChannels(); i++) {
+ std::memmove(outputBuffer.getWritePointer(i),
+ outputBuffer.getWritePointer(i) +
+ ioBlock.getNumSamples(),
+ sizeof(SampleType) * outputBufferSamples);
+ }
+ }
+
+ samplesOutput = ioBlock.getNumSamples();
+ }
+
+ // 3) If the input buffer is large enough, process!
+ int samplesProcessed = 0;
+ juce::dsp::AudioBlock<SampleType> inputBlock(inputBuffer);
+ for (int i = 0; i < inputBufferSamples; i += blockSize) {
+ int samplesAvailable = std::min(blockSize, inputBufferSamples - i);
+ if (samplesAvailable < blockSize)
+ break;
+
+ juce::dsp::AudioBlock<SampleType> subBlock =
+ inputBlock.getSubBlock(i, blockSize);
+ juce::dsp::ProcessContextReplacing<SampleType> subContext(subBlock);
+ int samplesProcessedThisBlock = plugin.process(subContext);
+ if (samplesProcessedThisBlock != blockSize) {
+ throw std::runtime_error(
+ "FixedBlockSize currently requires wrapped plugins to impart no "
+ "delay. This is an internal Pedalboard error and should be "
+ "reported.");
+ }
+
+ if (samplesProcessed > 0 && samplesProcessedThisBlock < blockSize) {
+ throw std::runtime_error(
+ "Plugin that returns fixed-size blocks returned too few samples! "
+ "This is an internal Pedalboard error and should be reported.");
+ }
+ samplesProcessed += samplesProcessedThisBlock;
+ }
+
+ // Copy the newly-processed data into the output buffer:
+ inputBlock.copyTo(outputBuffer, 0, outputBufferSamples, samplesProcessed);
+ outputBufferSamples += samplesProcessed;
+
+ if (!(outputBufferSamples <= outputBuffer.getNumSamples())) {
+ throw std::runtime_error("Output buffer overrun! This is an internal "
+ "Pedalboard error and should be reported.");
+ }
+
+ // ... and move the remaining input data to the left of the input buffer:
+ inputBlock.move(samplesProcessed, 0,
+ inputBufferSamples - samplesProcessed);
+ inputBufferSamples -= samplesProcessed;
+
+ // ... and copy the remaining output buffer if we can:
+ if (samplesOutput == 0 && outputBufferSamples >= minimumSamplesToOutput) {
+ ioBlock.copyFrom(outputBuffer, 0, 0, ioBlock.getNumSamples());
+ outputBufferSamples -= ioBlock.getNumSamples();
+
+ // Move the remainder of the output buffer to the left:
+ if (outputBufferSamples > 0) {
+ for (int i = 0; i < outputBuffer.getNumChannels(); i++) {
+ std::memmove(outputBuffer.getWritePointer(i),
+ outputBuffer.getWritePointer(i) +
+ ioBlock.getNumSamples(),
+ sizeof(SampleType) * outputBufferSamples);
+ }
+ }
+
+ samplesOutput = ioBlock.getNumSamples();
+ }
+
+ samplesProcessed += samplesOutput;
+ return samplesOutput;
+ }
+ }
+
+ virtual void reset() {
+ inputBufferSamples = 0;
+ outputBufferSamples = 0;
+
+ inStreamLatency = 0;
+ samplesProcessed = 0;
+ lastSpec = {0};
+ plugin.reset();
+
+ inputBuffer.clear();
+ outputBuffer.clear();
+ }
+
+ T &getNestedPlugin() { return plugin; }
+
+ void setFixedBlockSize(int newBlockSize) {
+ blockSize = newBlockSize;
+ reset();
+ }
+
+ int getFixedBlockSize() const { return blockSize; }
+
+private:
+ T plugin;
+ unsigned int blockSize = DefaultBlockSize;
+ int inStreamLatency = 0;
+
+ juce::AudioBuffer<SampleType> inputBuffer;
+ unsigned int inputBufferSamples = 0;
+
+ juce::AudioBuffer<SampleType> outputBuffer;
+ unsigned int outputBufferSamples = 0;
+
+ unsigned int samplesProcessed = 0;
+};
+
+// TODO: Add plugin wrappers to make mono plugins stereo (and/or multichannel),
+// or to mixdown to mono.
+
+/**
+ * A test plugin used to verify the behaviour of the FixedBlockSize wrapper.
+ */
+class ExpectsFixedBlockSize : public AddLatency {
+public:
+ virtual ~ExpectsFixedBlockSize(){};
+
+ virtual void prepare(const juce::dsp::ProcessSpec &spec) {
+ if (spec.maximumBlockSize != expectedBlockSize) {
+ throw std::runtime_error("Expected maximum block size of exactly " +
+ std::to_string(expectedBlockSize) + "!");
+ }
+ AddLatency::prepare(spec);
+ this->getDSP().setMaximumDelayInSamples(1024);
+ this->getDSP().setDelay(1024);
+ }
+
+ virtual int
+ process(const juce::dsp::ProcessContextReplacing<float> &context) {
+ if (context.getInputBlock().getNumSamples() != expectedBlockSize) {
+ throw std::runtime_error("Expected maximum block size of exactly " +
+ std::to_string(expectedBlockSize) + "!");
+ }
+ return AddLatency::process(context);
+ }
+
+ virtual void reset() { AddLatency::reset(); }
+
+ void setExpectedBlockSize(int newExpectedBlockSize) {
+ expectedBlockSize = newExpectedBlockSize;
+ }
+
+private:
+ int expectedBlockSize = 0;
+};
+
+class FixedSizeBlockTestPlugin : public FixedBlockSize<ExpectsFixedBlockSize> {
+public:
+ void setExpectedBlockSize(int newExpectedBlockSize) {
+ setFixedBlockSize(newExpectedBlockSize);
+ getNestedPlugin().setExpectedBlockSize(newExpectedBlockSize);
+ }
+
+ int getExpectedBlockSize() const { return getFixedBlockSize(); }
+
+private:
+ int expectedBlockSize = 0;
+};
+
+inline void init_fixed_size_block_test_plugin(py::module &m) {
+ py::class_<FixedSizeBlockTestPlugin, Plugin>(m, "FixedSizeBlockTestPlugin")
+ .def(py::init([](int expectedBlockSize) {
+ auto plugin = new FixedSizeBlockTestPlugin();
+ plugin->setExpectedBlockSize(expectedBlockSize);
+ return plugin;
+ }),
+ py::arg("expected_block_size") = 160)
+ .def("__repr__", [](const FixedSizeBlockTestPlugin &plugin) {
+ std::ostringstream ss;
+ ss << "<pedalboard.FixedSizeBlockTestPlugin";
+ ss << " expected_block_size=" << plugin.getExpectedBlockSize();
+ ss << " at " << &plugin;
+ ss << ">";
+ return ss.str();
+ });
+}
+
+} // namespace Pedalboard
\ No newline at end of file
diff --git a/pedalboard/plugin_templates/ForceMono.h b/pedalboard/plugin_templates/ForceMono.h
new file mode 100644
index 0000000..7af20aa
--- /dev/null
+++ b/pedalboard/plugin_templates/ForceMono.h
@@ -0,0 +1,84 @@
+/*
+ * pedalboard
+ * Copyright 2022 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 "../JuceHeader.h"
+#include "../Plugin.h"
+#include <mutex>
+
+namespace Pedalboard {
+
+/**
+ * A template class that wraps a Pedalboard plugin,
+ * but ensures that its process() function is only ever passed a mono signal.
+ */
+template <typename T, typename SampleType = float>
+class ForceMono : public Plugin {
+public:
+ virtual ~ForceMono(){};
+
+ virtual void prepare(const juce::dsp::ProcessSpec &spec) {
+ juce::dsp::ProcessSpec newSpec = spec;
+ newSpec.numChannels = 1;
+ plugin.prepare(newSpec);
+ }
+
+ virtual int
+ process(const juce::dsp::ProcessContextReplacing<SampleType> &context) {
+ auto ioBlock = context.getOutputBlock();
+
+ // Mix all channels to mono first, if necessary.
+ if (ioBlock.getNumChannels() > 1) {
+ float channelVolume = 1.0f / ioBlock.getNumChannels();
+ for (int i = 0; i < ioBlock.getNumChannels(); i++) {
+ ioBlock.getSingleChannelBlock(i) *= channelVolume;
+ }
+
+ // Copy all of the latter channels into the first channel,
+ // which will be used for processing:
+ auto firstChannel = ioBlock.getSingleChannelBlock(0);
+ for (int i = 1; i < ioBlock.getNumChannels(); i++) {
+ firstChannel += ioBlock.getSingleChannelBlock(i);
+ }
+ }
+
+ juce::dsp::AudioBlock<SampleType> monoBlock =
+ ioBlock.getSingleChannelBlock(0);
+ juce::dsp::ProcessContextReplacing<SampleType> subContext(monoBlock);
+ int samplesProcessed = plugin.process(monoBlock);
+
+ // Copy the mono signal back out to all other channels:
+ if (ioBlock.getNumChannels() > 1) {
+ auto firstChannel = ioBlock.getSingleChannelBlock(0);
+ for (int i = 1; i < ioBlock.getNumChannels(); i++) {
+ ioBlock.getSingleChannelBlock(i).copyFrom(firstChannel);
+ }
+ }
+
+ return samplesProcessed;
+ }
+
+ virtual void reset() { plugin.reset(); }
+
+ T &getNestedPlugin() { return plugin; }
+
+private:
+ T plugin;
+};
+
+} // namespace Pedalboard
\ No newline at end of file
diff --git a/pedalboard/plugin_templates/PrimeWithSilence.h b/pedalboard/plugin_templates/PrimeWithSilence.h
index 043ba6a..ac02b31 100644
--- a/pedalboard/plugin_templates/PrimeWithSilence.h
+++ b/pedalboard/plugin_templates/PrimeWithSilence.h
@@ -28,7 +28,8 @@ namespace Pedalboard {
* A dummy plugin that buffers audio data internally, used to test Pedalboard's
* automatic latency compensation.
*/
-template <typename T, typename SampleType = float>
+template <typename T, typename SampleType = float,
+ int DefaultSilenceLengthSamples = 0>
class PrimeWithSilence
: public JucePlugin<juce::dsp::DelayLine<
SampleType, juce::dsp::DelayLineInterpolationTypes::None>> {
@@ -39,6 +40,8 @@ public:
JucePlugin<juce::dsp::DelayLine<
SampleType,
juce::dsp::DelayLineInterpolationTypes::None>>::prepare(spec);
+ this->getDSP().setMaximumDelayInSamples(silenceLengthSamples);
+ this->getDSP().setDelay(silenceLengthSamples);
plugin.prepare(spec);
}
@@ -82,7 +85,7 @@ public:
private:
T plugin;
int samplesOutput = 0;
- int silenceLengthSamples = 0;
+ int silenceLengthSamples = DefaultSilenceLengthSamples;
};
/**
diff --git a/pedalboard/plugin_templates/Resample.h b/pedalboard/plugin_templates/Resample.h
index a95019b..db3bf91 100644
--- a/pedalboard/plugin_templates/Resample.h
+++ b/pedalboard/plugin_templates/Resample.h
@@ -120,7 +120,8 @@ public:
* resampled audio and its sampleRate and maximumBlockSize parameters
* are adjusted accordingly.
*/
-template <typename T = Passthrough<float>, typename SampleType = float>
+template <typename T = Passthrough<float>, typename SampleType = float,
+ int DefaultSampleRate = 8000>
class Resample : public Plugin {
public:
virtual ~Resample(){};
@@ -129,7 +130,7 @@ public:
bool specChanged = lastSpec.sampleRate != spec.sampleRate ||
lastSpec.maximumBlockSize < spec.maximumBlockSize ||
lastSpec.numChannels != spec.numChannels;
- if (specChanged) {
+ if (specChanged || nativeToTargetResamplers.empty()) {
reset();
nativeToTargetResamplers.resize(spec.numChannels);
@@ -146,12 +147,6 @@ public:
maximumBlockSizeInTargetSampleRate =
std::ceil(spec.maximumBlockSize / resamplerRatio);
- const juce::dsp::ProcessSpec subSpec = {
- .numChannels = spec.numChannels,
- .sampleRate = targetSampleRate,
- .maximumBlockSize = maximumBlockSizeInTargetSampleRate};
- plugin.prepare(subSpec);
-
// Store the remainder of the input: any samples that weren't consumed in
// one pushSamples() call but would be consumable in the next one.
inputReservoir.setSize(spec.numChannels,
@@ -176,6 +171,12 @@ public:
lastSpec = spec;
}
+
+ const juce::dsp::ProcessSpec subSpec = {
+ .numChannels = spec.numChannels,
+ .sampleRate = targetSampleRate,
+ .maximumBlockSize = maximumBlockSizeInTargetSampleRate};
+ plugin.prepare(subSpec);
}
int process(const juce::dsp::ProcessContextReplacing<SampleType> &context)
@@ -376,6 +377,8 @@ public:
T &getNestedPlugin() { return plugin; }
virtual void reset() override final {
+ plugin.reset();
+
nativeToTargetResamplers.clear();
targetToNativeResamplers.clear();
@@ -399,7 +402,7 @@ public:
private:
T plugin;
- float targetSampleRate = 8000.0f;
+ float targetSampleRate = (float)DefaultSampleRate;
ResamplingQuality quality = ResamplingQuality::WindowedSinc;
double resamplerRatio = 1.0;
@@ -523,37 +526,43 @@ inline void init_resample_with_latency(py::module &m) {
py::arg("target_sample_rate") = 8000.0,
py::arg("internal_latency") = 1024,
py::arg("quality") = ResamplingQuality::WindowedSinc)
- .def("__repr__", [](Resample<AddLatency, float> &plugin) {
- std::ostringstream ss;
- ss << "<pedalboard.ResampleWithLatency";
- ss << " target_sample_rate=" << plugin.getTargetSampleRate();
- ss << " internal_latency="
- << plugin.getNestedPlugin().getDSP().getDelay();
- ss << " quality=";
- switch (plugin.getQuality()) {
- case ResamplingQuality::ZeroOrderHold:
- ss << "ZeroOrderHold";
- break;
- case ResamplingQuality::Linear:
- ss << "Linear";
- break;
- case ResamplingQuality::CatmullRom:
- ss << "CatmullRom";
- break;
- case ResamplingQuality::Lagrange:
- ss << "Lagrange";
- break;
- case ResamplingQuality::WindowedSinc:
- ss << "WindowedSinc";
- break;
- default:
- ss << "unknown";
- break;
- }
- ss << " at " << &plugin;
- ss << ">";
- return ss.str();
- });
+ .def("__repr__",
+ [](Resample<AddLatency, float> &plugin) {
+ std::ostringstream ss;
+ ss << "<pedalboard.ResampleWithLatency";
+ ss << " target_sample_rate=" << plugin.getTargetSampleRate();
+ ss << " internal_latency="
+ << plugin.getNestedPlugin().getDSP().getDelay();
+ ss << " quality=";
+ switch (plugin.getQuality()) {
+ case ResamplingQuality::ZeroOrderHold:
+ ss << "ZeroOrderHold";
+ break;
+ case ResamplingQuality::Linear:
+ ss << "Linear";
+ break;
+ case ResamplingQuality::CatmullRom:
+ ss << "CatmullRom";
+ break;
+ case ResamplingQuality::Lagrange:
+ ss << "Lagrange";
+ break;
+ case ResamplingQuality::WindowedSinc:
+ ss << "WindowedSinc";
+ break;
+ default:
+ ss << "unknown";
+ break;
+ }
+ ss << " at " << &plugin;
+ ss << ">";
+ return ss.str();
+ })
+ .def_property("target_sample_rate",
+ &Resample<AddLatency, float>::getTargetSampleRate,
+ &Resample<AddLatency, float>::setTargetSampleRate)
+ .def_property("quality", &Resample<AddLatency, float>::getQuality,
+ &Resample<AddLatency, float>::setQuality);
}
} // namespace Pedalboard
\ No newline at end of file
diff --git a/pedalboard/plugins/GSMCompressor.h b/pedalboard/plugins/GSMCompressor.h
index 0ad2506..2f403c9 100644
--- a/pedalboard/plugins/GSMCompressor.h
+++ b/pedalboard/plugins/GSMCompressor.h
@@ -16,6 +16,11 @@
*/
#include "../Plugin.h"
+#include "../plugin_templates/FixedBlockSizePlugin.h"
+#include "../plugin_templates/ForceMono.h"
+#include "../plugin_templates/PrimeWithSilence.h"
+#include "../plugin_templates/Resample.h"
+
extern "C" {
#include <gsm.h>
}
@@ -48,9 +53,9 @@ private:
gsm _gsm = nullptr;
};
-class GSMCompressor : public Plugin {
+class GSMCompressorInternal : public Plugin {
public:
- virtual ~GSMCompressor(){};
+ virtual ~GSMCompressorInternal(){};
virtual void prepare(const juce::dsp::ProcessSpec &spec) override {
bool specChanged = lastSpec.sampleRate != spec.sampleRate ||
@@ -59,17 +64,10 @@ public:
if (!encoder || specChanged) {
reset();
- resamplerRatio = spec.sampleRate / GSM_SAMPLE_RATE;
- inverseResamplerRatio = GSM_SAMPLE_RATE / spec.sampleRate;
-
- gsmFrameSizeInNativeSampleRate = GSM_FRAME_SIZE_SAMPLES * resamplerRatio;
- int maximumBlockSizeInGSMSampleRate =
- spec.maximumBlockSize / resamplerRatio;
-
- // Store the remainder of the input: any samples that weren't consumed in
- // one pushSamples() call but would be consumable in the next one.
- inputReservoir.setSize(1, (int)std::ceil(resamplerRatio) +
- spec.maximumBlockSize);
+ if (spec.sampleRate != GSM_SAMPLE_RATE) {
+ throw std::runtime_error("GSMCompressor plugin must be run at " +
+ std::to_string(GSM_SAMPLE_RATE) + "Hz!");
+ }
if (!encoder.getContext()) {
throw std::runtime_error("Failed to initialize GSM encoder.");
@@ -78,25 +76,6 @@ public:
throw std::runtime_error("Failed to initialize GSM decoder.");
}
- inStreamLatency = 0;
-
- // Add the resamplers' latencies so the output is properly aligned:
- inStreamLatency += nativeToGSMResampler.getBaseLatency() * resamplerRatio;
- inStreamLatency += gsmToNativeResampler.getBaseLatency() * resamplerRatio;
-
- resampledBuffer.setSize(1, maximumBlockSizeInGSMSampleRate +
- GSM_FRAME_SIZE_SAMPLES +
- (inStreamLatency / resamplerRatio));
- outputBuffer.setSize(1, spec.maximumBlockSize +
- gsmFrameSizeInNativeSampleRate +
- inStreamLatency);
-
- // Feed one GSM frame's worth of silence at the start so that we
- // can tolerate different buffer sizes without underrunning any buffers.
- std::vector<float> silence(gsmFrameSizeInNativeSampleRate);
- inStreamLatency += silence.size();
- pushSamples(silence.data(), silence.size());
-
lastSpec = spec;
}
}
@@ -105,269 +84,87 @@ public:
const juce::dsp::ProcessContextReplacing<float> &context) override final {
auto ioBlock = context.getOutputBlock();
- // Mix all channels to mono first, if necessary; GSM (in reality) is
- // mono-only.
- if (ioBlock.getNumChannels() > 1) {
- float channelVolume = 1.0f / ioBlock.getNumChannels();
- for (int i = 0; i < ioBlock.getNumChannels(); i++) {
- ioBlock.getSingleChannelBlock(i) *= channelVolume;
- }
-
- // Copy all of the latter channels into the first channel,
- // which will be used for processing:
- auto firstChannel = ioBlock.getSingleChannelBlock(0);
- for (int i = 1; i < ioBlock.getNumChannels(); i++) {
- firstChannel += ioBlock.getSingleChannelBlock(i);
- }
- }
-
- // Actually do the GSM processing!
- pushSamples(ioBlock.getChannelPointer(0), ioBlock.getNumSamples());
- int samplesOutput =
- pullSamples(ioBlock.getChannelPointer(0), ioBlock.getNumSamples());
-
- // Copy the mono signal back out to all other channels:
- if (ioBlock.getNumChannels() > 1) {
- auto firstChannel = ioBlock.getSingleChannelBlock(0);
- for (int i = 1; i < ioBlock.getNumChannels(); i++) {
- ioBlock.getSingleChannelBlock(i).copyFrom(firstChannel);
- }
+ if (ioBlock.getNumSamples() != GSM_FRAME_SIZE_SAMPLES) {
+ throw std::runtime_error("GSMCompressor plugin must be passed exactly " +
+ std::to_string(GSM_FRAME_SIZE_SAMPLES) +
+ " at a time.");
}
- samplesProduced += samplesOutput;
- int samplesToReturn = std::min((long)(samplesProduced - inStreamLatency),
- (long)ioBlock.getNumSamples());
- if (samplesToReturn < 0)
- samplesToReturn = 0;
-
- return samplesToReturn;
- }
-
- /*
- * Return the number of samples needed for this
- * plugin to return a single GSM frame's worth of audio.
- */
- int spaceAvailableInResampledBuffer() const {
- return resampledBuffer.getNumSamples() - samplesInResampledBuffer;
- }
-
- int spaceAvailableInOutputBuffer() const {
- return outputBuffer.getNumSamples() - samplesInOutputBuffer;
- }
-
- /*
- * Push a certain number of input samples into the internal buffer(s)
- * of this plugin, as GSM coding processes audio 160 samples at a time.
- */
- void pushSamples(float *inputSamples, int numInputSamples) {
- float expectedOutputSamples = numInputSamples / resamplerRatio;
-
- if (spaceAvailableInResampledBuffer() < expectedOutputSamples) {
+ if (ioBlock.getNumChannels() != 1) {
throw std::runtime_error(
- "More samples were provided than can be buffered! This is an "
- "internal Pedalboard error and should be reported. Buffer had " +
- std::to_string(samplesInResampledBuffer) + "/" +
- std::to_string(resampledBuffer.getNumSamples()) +
- " samples at 8kHz, but was provided " +
- std::to_string(expectedOutputSamples) + ".");
+ "GSMCompressor plugin must be passed mono input!");
}
- float *resampledBufferPointer =
- resampledBuffer.getWritePointer(0) + samplesInResampledBuffer;
+ // Convert samples to signed 16-bit integer first,
+ // then pass to the GSM Encoder, then immediately back
+ // around to the GSM decoder.
+ short frame[GSM_FRAME_SIZE_SAMPLES];
- int samplesUsed = 0;
- if (samplesInInputReservoir) {
- // Copy the input samples into the input reservoir and use that as the
- // resampler's input:
- expectedOutputSamples += (float)samplesInInputReservoir / resamplerRatio;
- inputReservoir.copyFrom(0, samplesInInputReservoir, inputSamples,
- numInputSamples);
- samplesUsed = nativeToGSMResampler.process(
- resamplerRatio, inputReservoir.getReadPointer(0),
- resampledBufferPointer, expectedOutputSamples);
+ juce::AudioDataConverters::convertFloatToInt16LE(
+ ioBlock.getChannelPointer(0), frame, GSM_FRAME_SIZE_SAMPLES);
- if (samplesUsed < numInputSamples + samplesInInputReservoir) {
- // Take the missing samples and put them at the start of the input
- // reservoir for next time:
- int unusedInputSampleCount =
- (numInputSamples + samplesInInputReservoir) - samplesUsed;
- inputReservoir.copyFrom(0, 0,
- inputReservoir.getReadPointer(0) + samplesUsed,
- unusedInputSampleCount);
- samplesInInputReservoir = unusedInputSampleCount;
- } else {
- samplesInInputReservoir = 0;
- }
- } else {
- samplesUsed = nativeToGSMResampler.process(resamplerRatio, inputSamples,
- resampledBufferPointer,
- expectedOutputSamples);
+ // Actually do the GSM processing!
+ gsm_frame encodedFrame;
- if (samplesUsed < numInputSamples) {
- // Take the missing samples and put them at the start of the input
- // reservoir for next time:
- int unusedInputSampleCount = numInputSamples - samplesUsed;
- inputReservoir.copyFrom(0, 0, inputSamples + samplesUsed,
- unusedInputSampleCount);
- samplesInInputReservoir = unusedInputSampleCount;
- }
+ gsm_encode(encoder.getContext(), frame, encodedFrame);
+ if (gsm_decode(decoder.getContext(), encodedFrame, frame) < 0) {
+ throw std::runtime_error("GSM decoder could not decode frame!");
}
- samplesInResampledBuffer += expectedOutputSamples;
-
- performEncodeAndDecode();
- }
-
- int pullSamples(float *outputSamples, int maxOutputSamples) {
- performEncodeAndDecode();
+ juce::AudioDataConverters::convertInt16LEToFloat(
+ frame, ioBlock.getChannelPointer(0), GSM_FRAME_SIZE_SAMPLES);
- // Copy the data out of outputBuffer and into the provided pointer, at
- // the right side of the buffer:
- int samplesToCopy = std::min(samplesInOutputBuffer, maxOutputSamples);
- int offsetInOutput = maxOutputSamples - samplesToCopy;
- juce::FloatVectorOperations::copy(outputSamples + offsetInOutput,
- outputBuffer.getWritePointer(0),
- samplesToCopy);
- samplesInOutputBuffer -= samplesToCopy;
-
- // Move remaining samples to the left side of the output buffer:
- std::memmove((char *)outputBuffer.getWritePointer(0),
- (char *)(outputBuffer.getWritePointer(0) + samplesToCopy),
- samplesInOutputBuffer * sizeof(float));
-
- performEncodeAndDecode();
-
- return samplesToCopy;
- }
-
- void performEncodeAndDecode() {
- while (samplesInResampledBuffer >= GSM_FRAME_SIZE_SAMPLES) {
- float *encodeBuffer = resampledBuffer.getWritePointer(0);
-
- // Convert samples to signed 16-bit integer first,
- // then pass to the GSM Encoder, then immediately back
- // around to the GSM decoder.
- short frame[GSM_FRAME_SIZE_SAMPLES];
-
- juce::AudioDataConverters::convertFloatToInt16LE(encodeBuffer, frame,
- GSM_FRAME_SIZE_SAMPLES);
-
- // Actually do the GSM encoding/decoding:
- gsm_frame encodedFrame;
-
- gsm_encode(encoder.getContext(), frame, encodedFrame);
- if (gsm_decode(decoder.getContext(), encodedFrame, frame) < 0) {
- throw std::runtime_error("GSM decoder could not decode frame!");
- }
-
- if (spaceAvailableInOutputBuffer() < gsmFrameSizeInNativeSampleRate) {
- throw std::runtime_error(
- "Not enough space in output buffer to store a GSM frame! Needed " +
- std::to_string(gsmFrameSizeInNativeSampleRate) +
- " samples but only had " +
- std::to_string(spaceAvailableInOutputBuffer()) +
- " samples available. This is "
- "an internal Pedalboard error and should be reported.");
- }
- float *outputBufferPointer =
- outputBuffer.getWritePointer(0) + samplesInOutputBuffer;
-
- juce::AudioDataConverters::convertInt16LEToFloat(
- frame, gsmOutputFrame + samplesInGsmOutputFrame,
- GSM_FRAME_SIZE_SAMPLES);
- samplesInGsmOutputFrame += GSM_FRAME_SIZE_SAMPLES;
-
- // Resample back up to the native sample rate and store in outputBuffer,
- // using gsmOutputFrame as a temporary buffer to store up to 1 extra
- // sample to compensate for rounding errors:
- int expectedOutputSamples = samplesInGsmOutputFrame * resamplerRatio;
- int samplesConsumed = gsmToNativeResampler.process(
- inverseResamplerRatio, gsmOutputFrame, outputBufferPointer,
- expectedOutputSamples);
- std::memmove((char *)gsmOutputFrame,
- (char *)(gsmOutputFrame + samplesConsumed),
- (samplesInGsmOutputFrame - samplesConsumed) * sizeof(float));
-
- samplesInGsmOutputFrame -= samplesConsumed;
- samplesInOutputBuffer += expectedOutputSamples;
-
- // Now that we're done with this chunk of resampledBuffer, move its
- // contents to the left:
- int samplesRemainingInResampledBuffer =
- samplesInResampledBuffer - GSM_FRAME_SIZE_SAMPLES;
- std::memmove(
- (char *)resampledBuffer.getWritePointer(0),
- (char *)(resampledBuffer.getWritePointer(0) + GSM_FRAME_SIZE_SAMPLES),
- samplesRemainingInResampledBuffer * sizeof(float));
- samplesInResampledBuffer -= GSM_FRAME_SIZE_SAMPLES;
- }
+ return GSM_FRAME_SIZE_SAMPLES;
}
void reset() override final {
encoder.reset();
decoder.reset();
- nativeToGSMResampler.reset();
- gsmToNativeResampler.reset();
-
- resampledBuffer.clear();
- outputBuffer.clear();
- inputReservoir.clear();
-
- samplesInResampledBuffer = 0;
- samplesInOutputBuffer = 0;
- samplesInInputReservoir = 0;
- samplesInGsmOutputFrame = 0;
-
- samplesProduced = 0;
- inStreamLatency = 0;
}
-protected:
- virtual int getLatencyHint() override { return inStreamLatency; }
-
-private:
static constexpr size_t GSM_FRAME_SIZE_SAMPLES = 160;
- static constexpr float GSM_SAMPLE_RATE = 8000;
-
- double resamplerRatio = 1.0;
- double inverseResamplerRatio = 1.0;
- float gsmFrameSizeInNativeSampleRate;
-
- juce::AudioBuffer<float> inputReservoir;
- int samplesInInputReservoir = 0;
-
- juce::Interpolators::Lagrange nativeToGSMResampler;
- juce::AudioBuffer<float> resampledBuffer;
- int samplesInResampledBuffer = 0;
+ static constexpr int GSM_SAMPLE_RATE = 8000;
+private:
GSMWrapper encoder;
GSMWrapper decoder;
-
- juce::Interpolators::Lagrange gsmToNativeResampler;
- float gsmOutputFrame[GSM_FRAME_SIZE_SAMPLES + 1];
- int samplesInGsmOutputFrame = 0;
-
- juce::AudioBuffer<float> outputBuffer;
- int samplesInOutputBuffer = 0;
-
- int samplesProduced = 0;
- int inStreamLatency = 0;
};
+using GSMCompressor = ForceMono<
+ Resample<PrimeWithSilence<
+ FixedBlockSize<GSMCompressorInternal,
+ GSMCompressorInternal::GSM_FRAME_SIZE_SAMPLES>,
+ float, 1600>,
+ float, GSMCompressorInternal::GSM_SAMPLE_RATE>>;
+
inline void init_gsm_compressor(py::module &m) {
py::class_<GSMCompressor, Plugin>(
m, "GSMCompressor",
"Apply an GSM compressor to emulate the sound of a GSM (\"2G\") cellular "
"phone connection. This plugin internally resamples the input audio to "
"8kHz.")
- .def(py::init([]() { return new GSMCompressor(); }))
- .def("__repr__", [](const GSMCompressor &plugin) {
- std::ostringstream ss;
- ss << "<pedalboard.GSMCompressor";
- ss << " at " << &plugin;
- ss << ">";
- return ss.str();
- });
+ .def(py::init([](ResamplingQuality quality) {
+ auto plugin = std::make_unique<GSMCompressor>();
+ plugin->getNestedPlugin().setQuality(quality);
+ return plugin;
+ }),
+ py::arg("quality") = ResamplingQuality::WindowedSinc)
+ .def("__repr__",
+ [](const GSMCompressor &plugin) {
+ std::ostringstream ss;
+ ss << "<pedalboard.GSMCompressor";
+ ss << " at " << &plugin;
+ ss << ">";
+ return ss.str();
+ })
+ .def_property(
+ "quality",
+ [](GSMCompressor &plugin) {
+ return plugin.getNestedPlugin().getQuality();
+ },
+ [](GSMCompressor &plugin, ResamplingQuality quality) {
+ return plugin.getNestedPlugin().setQuality(quality);
+ });
}
}; // namespace Pedalboard
\ No newline at end of file
diff --git a/pedalboard/python_bindings.cpp b/pedalboard/python_bindings.cpp
index eff491f..b69ebe9 100644
--- a/pedalboard/python_bindings.cpp
+++ b/pedalboard/python_bindings.cpp
@@ -33,6 +33,7 @@ namespace py = pybind11;
#include "process.h"
#include "plugin_templates/Resample.h"
+#include "plugin_templates/PrimeWithSilence.h"
#include "plugins/AddLatency.h"
#include "plugins/Chorus.h"
@@ -141,13 +142,18 @@ PYBIND11_MODULE(pedalboard_native, m) {
py::arg("reset") = true);
plugin.attr("__call__") = plugin.attr("process");
+ // Publicly accessible plugins:
init_chorus(m);
init_compressor(m);
init_convolution(m);
init_delay(m);
init_distortion(m);
init_gain(m);
+
+ // Init Resample before GSMCompressor, which uses Resample::Quality:
+ init_resample(m);
init_gsm_compressor(m);
+
init_highpass(m);
init_invert(m);
init_ladderfilter(m);
@@ -157,7 +163,6 @@ PYBIND11_MODULE(pedalboard_native, m) {
init_noisegate(m);
init_phaser(m);
init_pitch_shift(m);
- init_resample(m);
init_reverb(m);
init_external_plugins(m);
@@ -167,4 +172,6 @@ PYBIND11_MODULE(pedalboard_native, m) {
init_add_latency(internal);
init_prime_with_silence_test_plugin(internal);
init_resample_with_latency(internal);
+ init_prime_with_silence_test_plugin(internal);
+ init_fixed_size_block_test_plugin(internal);
};
diff --git a/tests/test_fixed_size_blocks.py b/tests/test_fixed_size_blocks.py
new file mode 100644
index 0000000..a7c7eca
--- /dev/null
+++ b/tests/test_fixed_size_blocks.py
@@ -0,0 +1,55 @@
+#! /usr/bin/env python
+#
+# Copyright 2022 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.
+
+
+import pytest
+import numpy as np
+from pedalboard_native._internal import FixedSizeBlockTestPlugin
+
+
[email protected]("sample_rate", [22050, 44100, 48000])
[email protected]("buffer_size", [1, 16, 40, 128, 160, 8192, 8193])
[email protected]("fixed_buffer_size", [1, 16, 40, 128, 160, 8192, 8193])
[email protected]("num_channels", [1, 2])
+def test_fixed_size_blocks_plugin(sample_rate, buffer_size, fixed_buffer_size, num_channels):
+ num_seconds = 5.0
+ noise = np.random.rand(int(num_seconds * sample_rate))
+ if num_channels == 2:
+ noise = np.stack([noise, noise])
+ plugin = FixedSizeBlockTestPlugin(fixed_buffer_size)
+ output = plugin.process(noise, sample_rate, buffer_size=buffer_size)
+ try:
+ np.testing.assert_allclose(noise, output)
+ except AssertionError:
+ import matplotlib.pyplot as plt
+
+ if num_channels == 2:
+ noise = noise[0]
+ output = output[0]
+
+ for cut in (buffer_size * 2, len(noise) // 200, len(noise)):
+ fig, ax = plt.subplots(3)
+ ax[0].plot(noise[:cut])
+ ax[0].set_title("Input")
+ ax[1].plot(output[:cut])
+ ax[1].set_title("Output")
+ ax[2].plot(np.abs(noise - output)[:cut])
+ ax[2].set_title("Diff")
+ ax[2].set_ylim(0, 1)
+ plt.savefig(f"{sample_rate}-{buffer_size}-{cut}.png", dpi=300)
+ plt.clf()
+
+ raise
diff --git a/tests/test_gsm_compressor.py b/tests/test_gsm_compressor.py
index 9408cc2..c03cfe3 100644
--- a/tests/test_gsm_compressor.py
+++ b/tests/test_gsm_compressor.py
@@ -17,7 +17,7 @@
import pytest
import numpy as np
-from pedalboard import GSMCompressor
+from pedalboard import GSMCompressor, Resample
# GSM is a _very_ lossy codec:
GSM_ABSOLUTE_TOLERANCE = 0.75
@@ -34,6 +34,10 @@ def generate_sine_at(
) -> np.ndarray:
samples = np.arange(num_seconds * sample_rate)
sine_wave = np.sin(2 * np.pi * fundamental_hz * samples / sample_rate)
+ # Fade the sine wave in at the start and out at the end to remove any transients:
+ fade_duration = int(sample_rate * 0.1)
+ sine_wave[:fade_duration] *= np.linspace(0, 1, fade_duration)
+ sine_wave[-fade_duration:] *= np.linspace(1, 0, fade_duration)
if num_channels == 2:
return np.stack([sine_wave, sine_wave])
return sine_wave
@@ -42,27 +46,56 @@ def generate_sine_at(
@pytest.mark.parametrize("fundamental_hz", [440.0])
@pytest.mark.parametrize("sample_rate", [8000, 11025, 22050, 32000, 32001, 44100, 48000])
@pytest.mark.parametrize("buffer_size", [1, 32, 160, 8192])
[email protected]("duration", [1.0, 3.0, 30.0])
[email protected]("duration", [1.0])
+ "quality",
+ [
+ Resample.Quality.ZeroOrderHold,
+ Resample.Quality.Linear,
+ Resample.Quality.Lagrange,
+ Resample.Quality.CatmullRom,
+ Resample.Quality.WindowedSinc,
+ ],
+)
@pytest.mark.parametrize("num_channels", [1, 2])
def test_gsm_compressor(
- fundamental_hz: float, sample_rate: float, buffer_size: int, duration: float, num_channels: int
+ fundamental_hz: float,
+ sample_rate: float,
+ buffer_size: int,
+ duration: float,
+ quality: Resample.Quality,
+ num_channels: int,
):
signal = (
generate_sine_at(sample_rate, fundamental_hz, duration, num_channels) * SINE_WAVE_VOLUME
)
- compressed = GSMCompressor()(signal, sample_rate, buffer_size=buffer_size)
- np.testing.assert_allclose(signal, compressed, atol=GSM_ABSOLUTE_TOLERANCE)
+ output = GSMCompressor(quality=quality)(signal, sample_rate, buffer_size=buffer_size)
+ np.testing.assert_allclose(signal, output, atol=GSM_ABSOLUTE_TOLERANCE)
@pytest.mark.parametrize("sample_rate", [8000, 11025, 22050, 32000, 32001, 44100, 48000])
[email protected]("num_channels", [1]) # , 2])
-def test_gsm_compressor_invariant_to_buffer_size(sample_rate: float, num_channels: int):
+ "quality",
+ [
+ Resample.Quality.ZeroOrderHold,
+ Resample.Quality.Linear,
+ Resample.Quality.Lagrange,
+ Resample.Quality.CatmullRom,
+ Resample.Quality.WindowedSinc,
+ ],
+)
[email protected]("num_channels", [1, 2])
+def test_gsm_compressor_invariant_to_buffer_size(
+ sample_rate: float,
+ quality: Resample.Quality,
+ num_channels: int,
+):
fundamental_hz = 400.0
duration = 3.0
signal = generate_sine_at(sample_rate, fundamental_hz, duration, num_channels)
compressed = [
- GSMCompressor()(signal, sample_rate, buffer_size=buffer_size)
+ GSMCompressor(quality=quality)(signal, sample_rate, buffer_size=buffer_size)
for buffer_size in (1, 32, 8192)
]
for a, b in zip(compressed, compressed[1:]):
diff --git a/tests/test_resample.py b/tests/test_resample.py
index 4de8024..c729396 100644
--- a/tests/test_resample.py
+++ b/tests/test_resample.py
@@ -31,8 +31,8 @@ TOLERANCE_PER_QUALITY = {
@pytest.mark.parametrize("fundamental_hz", [440])
[email protected]("sample_rate", [8000, 22050, 44100, 48000])
[email protected]("target_sample_rate", [8000, 22050, 44100, 48000, 1234.56])
[email protected]("sample_rate", [8000, 11025, 22050, 44100, 48000])
[email protected]("target_sample_rate", [8000, 11025, 22050, 44100, 48000, 1234.56])
@pytest.mark.parametrize("buffer_size", [1, 32, 128, 8192, 96000])
@pytest.mark.parametrize("duration", [0.5, 1.23456])
@pytest.mark.parametrize("num_channels", [1, 2])
@@ -58,7 +58,41 @@ def test_resample(
plugin = plugin_class(target_sample_rate)
output = plugin.process(sine_wave, sample_rate, buffer_size=buffer_size)
- np.testing.assert_allclose(sine_wave, output, atol=0.12)
+ np.testing.assert_allclose(sine_wave, output, atol=0.16)
+
+
[email protected]("fundamental_hz", [440])
[email protected]("sample_rate", [8000, 11025, 22050, 44100, 48000])
[email protected]("target_sample_rate", [8000, 11025, 22050, 44100, 48000, 1234.56])
[email protected]("duration", [0.5, 1.23456])
[email protected]("num_channels", [1, 2])
[email protected]("plugin_class", [Resample, ResampleWithLatency])
+def test_resample_invariant_to_buffer_size(
+ fundamental_hz: float,
+ sample_rate: float,
+ target_sample_rate: float,
+ duration: float,
+ num_channels: int,
+ plugin_class,
+):
+ samples = np.arange(duration * sample_rate)
+ sine_wave = np.sin(2 * np.pi * fundamental_hz * samples / sample_rate)
+ # Fade the sine wave in at the start and out at the end to remove any transients:
+ fade_duration = int(sample_rate * 0.1)
+ sine_wave[:fade_duration] *= np.linspace(0, 1, fade_duration)
+ sine_wave[-fade_duration:] *= np.linspace(1, 0, fade_duration)
+ if num_channels == 2:
+ sine_wave = np.stack([sine_wave, sine_wave])
+
+ plugin = plugin_class(target_sample_rate)
+
+ buffer_sizes = [1, 32, 128, 8192, 96000]
+ outputs = [
+ plugin.process(sine_wave, sample_rate, buffer_size=buffer_size)
+ for buffer_size in buffer_sizes
+ ]
+
+ np.testing.assert_allclose(a, b, atol=1e-3)
@pytest.mark.parametrize("fundamental_hz", [440])
@@ -165,3 +199,25 @@ def test_extreme_resampling(
output = plugin.process(sine_wave, sample_rate, buffer_size=buffer_size)
np.testing.assert_allclose(sine_wave, output, atol=TOLERANCE_PER_QUALITY[quality])
+
+
[email protected]("num_channels", [1, 2])
+def test_quality_can_change(
+ num_channels: int,
+ fundamental_hz: float = 440,
+ sample_rate: float = 8000,
+ buffer_size: int = 96000,
+ duration: float = 0.5,
+):
+ samples = np.arange(duration * sample_rate)
+ sine_wave = np.sin(2 * np.pi * fundamental_hz * samples / sample_rate)
+ if num_channels == 2:
+ sine_wave = np.stack([sine_wave, sine_wave])
+
+ plugin = Resample(sample_rate)
+ output1 = plugin.process(sine_wave, sample_rate, buffer_size=buffer_size)
+ np.testing.assert_allclose(sine_wave, output1, atol=0.35)
+
+ plugin.quality = Resample.Quality.ZeroOrderHold
+ output2 = plugin.process(sine_wave, sample_rate, buffer_size=buffer_size)
+ np.testing.assert_allclose(sine_wave, output2, atol=0.75)