Skip to content

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

Add tests for ForceMono.

Peter Sobote00dd04702b0
diff --git a/pedalboard/plugin_templates/ForceMono.h b/pedalboard/plugin_templates/ForceMono.h
index 7af20aa..87b2655 100644
--- a/pedalboard/plugin_templates/ForceMono.h
+++ b/pedalboard/plugin_templates/ForceMono.h
@@ -81,4 +81,44 @@ private:
T plugin;
};
+/**
+ * A test plugin used to verify the behaviour of the ForceMono wrapper.
+ */
+class ExpectsMono : public AddLatency {
+public:
+ virtual ~ExpectsMono(){};
+
+ virtual void prepare(const juce::dsp::ProcessSpec &spec) {
+ if (spec.numChannels != 1) {
+ throw std::runtime_error("Expected mono input!");
+ }
+ AddLatency::prepare(spec);
+ }
+
+ virtual int
+ process(const juce::dsp::ProcessContextReplacing<float> &context) {
+ if (context.getInputBlock().getNumChannels() != 1) {
+ throw std::runtime_error("Expected mono input!");
+ }
+ return AddLatency::process(context);
+ }
+};
+
+using ForceMonoTestPlugin = ForceMono<ExpectsMono>;
+
+inline void init_force_mono_test_plugin(py::module &m) {
+ py::class_<ForceMonoTestPlugin, Plugin>(m,
+ "ForceMonoTestPlugin")
+ .def(py::init([]() {
+ return std::make_unique<ForceMonoTestPlugin>();
+ }))
+ .def("__repr__", [](const ForceMonoTestPlugin &plugin) {
+ std::ostringstream ss;
+ ss << "<pedalboard.ForceMonoTestPlugin";
+ ss << " at " << &plugin;
+ ss << ">";
+ return ss.str();
+ });
+}
+
} // namespace Pedalboard
\ No newline at end of file
diff --git a/pedalboard/python_bindings.cpp b/pedalboard/python_bindings.cpp
index 170809e..cceb502 100644
--- a/pedalboard/python_bindings.cpp
+++ b/pedalboard/python_bindings.cpp
@@ -32,6 +32,9 @@ namespace py = pybind11;
#include "Plugin.h"
#include "process.h"
+#include "plugin_templates/PrimeWithSilence.h"
+#include "plugin_templates/ForceMono.h"
+
#include "plugins/AddLatency.h"
#include "plugins/Chorus.h"
#include "plugins/Compressor.h"
@@ -50,8 +53,6 @@ namespace py = pybind11;
#include "plugins/PitchShift.h"
#include "plugins/Reverb.h"
-#include "plugin_templates/PrimeWithSilence.h"
-
using namespace Pedalboard;
static constexpr int DEFAULT_BUFFER_SIZE = 8192;
@@ -161,4 +162,5 @@ PYBIND11_MODULE(pedalboard_native, m) {
py::module internal = m.def_submodule("_internal");
init_add_latency(internal);
init_prime_with_silence_test_plugin(internal);
+ init_force_mono_test_plugin(internal);
};
diff --git a/tests/test_force_mono.py b/tests/test_force_mono.py
new file mode 100644
index 0000000..518707e
--- /dev/null
+++ b/tests/test_force_mono.py
@@ -0,0 +1,47 @@
+#! /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 ForceMonoTestPlugin
+
+
+NUM_SECONDS = 1.0
+
+
[email protected]("sample_rate", [22050, 44100])
[email protected]("buffer_size", [1, 16, 128, 8192])
+def test_force_mono(sample_rate, buffer_size):
+ stereo_noise = np.stack(
+ [
+ np.random.rand(int(NUM_SECONDS * sample_rate)),
+ np.random.rand(int(NUM_SECONDS * sample_rate)),
+ ]
+ )
+ output = ForceMonoTestPlugin().process(stereo_noise, sample_rate, buffer_size=buffer_size)
+ np.testing.assert_allclose(output[0], output[1])
+
+ expected_mono = (stereo_noise[0] + stereo_noise[1]) / 2
+ np.testing.assert_allclose(output, np.stack([expected_mono, expected_mono]), atol=1e-7)
+
+
[email protected]("sample_rate", [22050, 44100])
[email protected]("buffer_size", [1, 16, 128, 8192])
+def test_force_mono_on_already_mono(sample_rate, buffer_size):
+ mono_noise = np.random.rand(int(NUM_SECONDS * sample_rate))
+ output = ForceMonoTestPlugin().process(mono_noise, sample_rate, buffer_size=buffer_size)
+ np.testing.assert_allclose(output, mono_noise)