π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
Add hard clipping plugin. (#150)
Peter Sobotfc4ebe248ea9
diff --git a/pedalboard/plugins/Clipping.h b/pedalboard/plugins/Clipping.hnew file mode 100644index 0000000..d73f828--- /dev/null+++ b/pedalboard/plugins/Clipping.h@@ -0,0 +1,86 @@+/*+ * 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.+ */++#include <pybind11/pybind11.h>+#include <pybind11/stl.h>++namespace py = pybind11;++#include "../JucePlugin.h"++namespace Pedalboard {+template <typename SampleType> class Clipping : public Plugin {+public:+ void setThresholdDecibels(const SampleType f) noexcept {+ thresholdDecibels = f;++ negativeThresholdGain = -juce::Decibels::decibelsToGain<SampleType>(f);+ positiveThresholdGain = juce::Decibels::decibelsToGain<SampleType>(f);+ }++ float getThresholdDecibels() const noexcept { return thresholdDecibels; }++ virtual void prepare(const juce::dsp::ProcessSpec &spec) {}++ virtual int process(+ const juce::dsp::ProcessContextReplacing<SampleType> &context) override {+ auto ioBlock = context.getOutputBlock();++ for (int c = 0; c < ioBlock.getNumChannels(); c++) {+ SampleType *channelPointer = ioBlock.getChannelPointer(c);++ juce::FloatVectorOperations::clip(+ channelPointer, channelPointer, negativeThresholdGain,+ positiveThresholdGain, ioBlock.getNumSamples());+ }++ return context.getOutputBlock().getNumSamples();+ }++ virtual void reset() {}++private:+ SampleType thresholdDecibels;++ SampleType negativeThresholdGain;+ SampleType positiveThresholdGain;+};++inline void init_clipping(py::module &m) {+ py::class_<Clipping<float>, Plugin, std::shared_ptr<Clipping<float>>>(+ m, "Clipping",+ "A distortion plugin that adds hard distortion to the signal "+ "by clipping the signal at the provided threshold (in decibels).")+ .def(py::init([](float thresholdDb) {+ auto plugin = std::make_unique<Clipping<float>>();+ plugin->setThresholdDecibels(thresholdDb);+ return plugin;+ }),+ py::arg("threshold_db") = -6.0)+ .def("__repr__",+ [](const Clipping<float> &plugin) {+ std::ostringstream ss;+ ss << "<pedalboard.Clipping";+ ss << " threshold_db=" << plugin.getThresholdDecibels();+ ss << " at " << &plugin;+ ss << ">";+ return ss.str();+ })+ .def_property("threshold_db", &Clipping<float>::getThresholdDecibels,+ &Clipping<float>::setThresholdDecibels);+}+}; // namespace Pedalboard\ No newline at end of filediff --git a/pedalboard/python_bindings.cpp b/pedalboard/python_bindings.cppindex cef5ebe..0a90c05 100644--- a/pedalboard/python_bindings.cpp+++ b/pedalboard/python_bindings.cpp@@ -42,6 +42,7 @@ namespace py = pybind11;#include "plugins/Bitcrush.h"#include "plugins/Chain.h"#include "plugins/Chorus.h"+#include "plugins/Clipping.h"#include "plugins/Compressor.h"#include "plugins/Convolution.h"#include "plugins/Delay.h"@@ -161,6 +162,7 @@ or buffer, set ``reset`` to ``False``.// Publicly accessible plugins:init_bitcrush(m);init_chorus(m);+ init_clipping(m);init_compressor(m);init_convolution(m);init_delay(m);diff --git a/tests/test_clipping.py b/tests/test_clipping.pynew file mode 100644index 0000000..0e0d5ad--- /dev/null+++ b/tests/test_clipping.py@@ -0,0 +1,42 @@+#! /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 import Clipping+from .utils import generate_sine_at, db_to_gain++[email protected]("threshold_db", list(np.arange(0.0, -40, -0.5)))[email protected]("fundamental_hz", [440, 880])[email protected]("sample_rate", [22050, 44100, 48000])[email protected]("num_channels", [1, 2])+def test_bitcrush(+ threshold_db: float, fundamental_hz: float, sample_rate: float, num_channels: int+):+ sine_wave = generate_sine_at(+ sample_rate, fundamental_hz, num_seconds=0.1, num_channels=num_channels+ )++ plugin = Clipping(threshold_db)+ output = plugin.process(sine_wave, sample_rate)++ assert np.all(np.isfinite(output))+ _min, _max = -db_to_gain(threshold_db), db_to_gain(threshold_db)++ expected_output = np.clip(sine_wave, _min, _max)+ np.testing.assert_allclose(output, expected_output, atol=0.01)diff --git a/tests/test_filters.py b/tests/test_filters.pyindex f6870cc..47c301d 100644--- a/tests/test_filters.py+++ b/tests/test_filters.py@@ -18,7 +18,7 @@import pytestimport numpy as npfrom pedalboard import HighpassFilter, LowpassFilter, HighShelfFilter, LowShelfFilter, PeakFilter-from .utils import generate_sine_at+from .utils import generate_sine_at, db_to_gaindef rms(x: np.ndarray) -> float:@@ -28,14 +28,6 @@ def rms(x: np.ndarray) -> float:return np.array([rms(channel) for channel in x])-def db_to_gain(db: float) -> float:- return 10.0 ** (db / 20.0)---def gain_to_db(gain: float) -> float:- return 20 * np.log10(gain)--def octaves_between(a_hz: float, b_hz: float) -> float:return np.log2(a_hz / b_hz)diff --git a/tests/utils.py b/tests/utils.pyindex 61cc6e6..acedb4b 100644--- a/tests/utils.py+++ b/tests/utils.py@@ -23,3 +23,11 @@ def generate_sine_at(else:TEST_SINE_WAVE_CACHE[cache_key] = sine_wavereturn TEST_SINE_WAVE_CACHE[cache_key]+++def db_to_gain(db: float) -> float:+ return 10.0 ** (db / 20.0)+++def gain_to_db(gain: float) -> float:+ return 20 * np.log10(gain)