π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
Add bitcrusher.
Peter Sobotc0224cbaaf6d
diff --git a/pedalboard/plugins/Bitcrusher.h b/pedalboard/plugins/Bitcrusher.hnew file mode 100644index 0000000..909c5d1--- /dev/null+++ b/pedalboard/plugins/Bitcrusher.h@@ -0,0 +1,99 @@+/*+ * 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.+ */++#include "../JucePlugin.h"+#include <cmath>++namespace Pedalboard {+template <typename SampleType> class Bitcrusher : public Plugin {+public:+ SampleType getBitDepth() const { return bitDepth; }+ void setBitDepth(const SampleType value) {+ if (value < 0.0 || value > 32) {+ throw std::range_error("Bit depth must be between 0.0 and 32.0 bits.");+ }+ bitDepth = value;+ };++ virtual void prepare(const juce::dsp::ProcessSpec &spec) override {+ scaleFactor = pow(2, bitDepth);+ inverseScaleFactor = 1.0 / scaleFactor;+ }+ virtual void reset() override {}++ virtual int process(+ const juce::dsp::ProcessContextReplacing<SampleType> &context) override {+ auto block = context.getOutputBlock();++ block.multiplyBy(scaleFactor);++ for (int c = 0; c < block.getNumChannels(); c++) {+ SampleType *channelPointer = block.getChannelPointer(c);++ // To allow for some SIMD optimization:+ int numIterations = block.getNumSamples() / INNER_LOOP_DIMENSION;++ for (int i = 0; i < numIterations; i++) {+ for (int j = 0; j < INNER_LOOP_DIMENSION; j++) {+ channelPointer[(i * INNER_LOOP_DIMENSION) + j] =+ nearbyintf(channelPointer[(i * INNER_LOOP_DIMENSION) + j]);+ }+ }++ for (int i = numIterations * INNER_LOOP_DIMENSION;+ i < block.getNumSamples(); i++) {+ channelPointer[i] = nearbyintf(channelPointer[i]);+ }+ }++ block.multiplyBy(inverseScaleFactor);+ return block.getNumSamples();+ }++private:+ SampleType bitDepth = 8.0f;++ SampleType scaleFactor = 1.0f;+ SampleType inverseScaleFactor = 1.0f;++ static constexpr int INNER_LOOP_DIMENSION = 16;+};++inline void init_bitcrusher(py::module &m) {+ py::class_<Bitcrusher<float>, Plugin, std::shared_ptr<Bitcrusher<float>>>(+ m, "Bitcrusher",+ "A plugin that reduces the signal to a given bit depth, giving the audio "+ "a lo-fi, digitized sound. Floating-point bit depths are supported.")+ .def(py::init([](float bitDepth) {+ auto bitcrusher = std::make_unique<Bitcrusher<float>>();+ bitcrusher->setBitDepth(bitDepth);+ return bitcrusher;+ }),+ py::arg("bit_depth") = 8)+ .def("__repr__",+ [](const Bitcrusher<float> &plugin) {+ std::ostringstream ss;+ ss << "<pedalboard.Bitcrusher";+ ss << " bit_depth=" << plugin.getBitDepth();+ ss << " at " << &plugin;+ ss << ">";+ return ss.str();+ })+ .def_property("bit_depth", &Bitcrusher<float>::getBitDepth,+ &Bitcrusher<float>::setBitDepth);+}+}; // namespace Pedalboarddiff --git a/pedalboard/python_bindings.cpp b/pedalboard/python_bindings.cppindex f51e007..8bf2f67 100644--- a/pedalboard/python_bindings.cpp+++ b/pedalboard/python_bindings.cpp@@ -39,6 +39,7 @@ namespace py = pybind11;#include "plugin_templates/Resample.h"#include "plugins/AddLatency.h"+#include "plugins/Bitcrusher.h"#include "plugins/Chain.h"#include "plugins/Chorus.h"#include "plugins/Compressor.h"@@ -146,6 +147,7 @@ PYBIND11_MODULE(pedalboard_native, m) {init_plugin_container(m);// Publicly accessible plugins:+ init_bitcrusher(m);init_chorus(m);init_compressor(m);init_convolution(m);diff --git a/tests/test_bitcrusher.py b/tests/test_bitcrusher.pynew file mode 100644index 0000000..18cd7dd--- /dev/null+++ b/tests/test_bitcrusher.py@@ -0,0 +1,38 @@+#! /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 Pedalboard, Bitcrusher+from .utils import generate_sine_at++[email protected]("bit_depth", list(np.arange(1, 32, 0.5)))[email protected]("fundamental_hz", [440, 880])[email protected]("sample_rate", [22050, 44100, 48000])[email protected]("num_channels", [1, 2])+def test_bitcrusher(bit_depth: 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 = Bitcrusher(bit_depth)+ output = plugin.process(sine_wave, sample_rate)++ assert np.all(np.isfinite(output))++ expected_output = np.around(sine_wave.astype(np.float64) * (2 ** bit_depth)) / (2 ** bit_depth)+ np.testing.assert_allclose(output, expected_output, atol=0.01)+