Skip to content

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

Ensure that IIR filters are applied to all channels.

Peter Sobot536bbbb4ecd2
diff --git a/pedalboard/plugins/HighpassFilter.h b/pedalboard/plugins/HighpassFilter.h
index 98f964b..b278232 100644
--- a/pedalboard/plugins/HighpassFilter.h
+++ b/pedalboard/plugins/HighpassFilter.h
@@ -24,16 +24,20 @@ namespace py = pybind11;
namespace Pedalboard {
template <typename SampleType>
-class HighpassFilter : public JucePlugin<juce::dsp::IIR::Filter<SampleType>> {
+class HighpassFilter : public JucePlugin<juce::dsp::ProcessorDuplicator<
+ juce::dsp::IIR::Filter<SampleType>,
+ juce::dsp::IIR::Coefficients<SampleType>>> {
public:
void setCutoffFrequencyHz(float f) noexcept { cutoffFrequencyHz = f; }
float getCutoffFrequencyHz() const noexcept { return cutoffFrequencyHz; }
virtual void prepare(const juce::dsp::ProcessSpec &spec) override {
- JucePlugin<juce::dsp::IIR::Filter<SampleType>>::prepare(spec);
- this->getDSP().coefficients =
- juce::dsp::IIR::Coefficients<SampleType>::makeFirstOrderHighPass(
+ *this->getDSP().state =
+ *juce::dsp::IIR::Coefficients<SampleType>::makeFirstOrderHighPass(
spec.sampleRate, cutoffFrequencyHz);
+ JucePlugin<juce::dsp::ProcessorDuplicator<
+ juce::dsp::IIR::Filter<SampleType>,
+ juce::dsp::IIR::Coefficients<SampleType>>>::prepare(spec);
}
private:
diff --git a/pedalboard/plugins/IIRFilters.h b/pedalboard/plugins/IIRFilters.h
index 0a8d7cb..da227d8 100644
--- a/pedalboard/plugins/IIRFilters.h
+++ b/pedalboard/plugins/IIRFilters.h
@@ -37,7 +37,9 @@ inline float clampCutoffFrequency(float cutoffFrequencyHz, float sampleRate) {
* A base class for all IIR filter classes.
*/
template <typename SampleType>
-class IIRFilter : public JucePlugin<juce::dsp::IIR::Filter<SampleType>> {
+class IIRFilter : public JucePlugin<juce::dsp::ProcessorDuplicator<
+ juce::dsp::IIR::Filter<SampleType>,
+ juce::dsp::IIR::Coefficients<SampleType>>> {
public:
void setCutoffFrequencyHz(float f) {
if (f <= 0)
@@ -64,7 +66,9 @@ public:
if (this->lastSpec.sampleRate != spec.sampleRate ||
this->lastSpec.maximumBlockSize < spec.maximumBlockSize ||
spec.numChannels != this->lastSpec.numChannels) {
- JucePlugin<juce::dsp::IIR::Filter<SampleType>>::prepare(spec);
+ JucePlugin<juce::dsp::ProcessorDuplicator<
+ juce::dsp::IIR::Filter<SampleType>,
+ juce::dsp::IIR::Coefficients<SampleType>>>::prepare(spec);
this->lastSpec = spec;
}
}
@@ -79,13 +83,13 @@ template <typename SampleType>
class HighShelfFilter : public IIRFilter<SampleType> {
public:
virtual void prepare(const juce::dsp::ProcessSpec &spec) override {
- IIRFilter<SampleType>::prepare(spec);
-
- this->getDSP().coefficients =
- juce::dsp::IIR::Coefficients<SampleType>::makeHighShelf(
+ *this->getDSP().state =
+ *juce::dsp::IIR::Coefficients<SampleType>::makeHighShelf(
spec.sampleRate,
clampCutoffFrequency(this->cutoffFrequencyHz, spec.sampleRate),
this->Q, this->gainFactor);
+
+ IIRFilter<SampleType>::prepare(spec);
}
};
@@ -93,26 +97,25 @@ template <typename SampleType>
class LowShelfFilter : public IIRFilter<SampleType> {
public:
virtual void prepare(const juce::dsp::ProcessSpec &spec) override {
- IIRFilter<SampleType>::prepare(spec);
-
- this->getDSP().coefficients =
- juce::dsp::IIR::Coefficients<SampleType>::makeLowShelf(
+ *this->getDSP().state =
+ *juce::dsp::IIR::Coefficients<SampleType>::makeLowShelf(
spec.sampleRate,
clampCutoffFrequency(this->cutoffFrequencyHz, spec.sampleRate),
this->Q, this->gainFactor);
+
+ IIRFilter<SampleType>::prepare(spec);
}
};
template <typename SampleType> class PeakFilter : public IIRFilter<SampleType> {
public:
virtual void prepare(const juce::dsp::ProcessSpec &spec) override {
- IIRFilter<SampleType>::prepare(spec);
-
- this->getDSP().coefficients =
- juce::dsp::IIR::Coefficients<SampleType>::makePeakFilter(
+ *this->getDSP().state =
+ *juce::dsp::IIR::Coefficients<SampleType>::makePeakFilter(
spec.sampleRate,
clampCutoffFrequency(this->cutoffFrequencyHz, spec.sampleRate),
this->Q, this->gainFactor);
+ IIRFilter<SampleType>::prepare(spec);
}
};
diff --git a/pedalboard/plugins/LowpassFilter.h b/pedalboard/plugins/LowpassFilter.h
index 71863fe..33d01c9 100644
--- a/pedalboard/plugins/LowpassFilter.h
+++ b/pedalboard/plugins/LowpassFilter.h
@@ -24,16 +24,20 @@ namespace py = pybind11;
namespace Pedalboard {
template <typename SampleType>
-class LowpassFilter : public JucePlugin<juce::dsp::IIR::Filter<SampleType>> {
+class LowpassFilter : public JucePlugin<juce::dsp::ProcessorDuplicator<
+ juce::dsp::IIR::Filter<SampleType>,
+ juce::dsp::IIR::Coefficients<SampleType>>> {
public:
void setCutoffFrequencyHz(float f) noexcept { cutoffFrequencyHz = f; }
float getCutoffFrequencyHz() const noexcept { return cutoffFrequencyHz; }
virtual void prepare(const juce::dsp::ProcessSpec &spec) override {
- JucePlugin<juce::dsp::IIR::Filter<SampleType>>::prepare(spec);
- this->getDSP().coefficients =
- juce::dsp::IIR::Coefficients<SampleType>::makeFirstOrderLowPass(
+ *this->getDSP().state =
+ *juce::dsp::IIR::Coefficients<SampleType>::makeFirstOrderLowPass(
spec.sampleRate, cutoffFrequencyHz);
+ JucePlugin<juce::dsp::ProcessorDuplicator<
+ juce::dsp::IIR::Filter<SampleType>,
+ juce::dsp::IIR::Coefficients<SampleType>>>::prepare(spec);
}
private:
diff --git a/tests/test_filters.py b/tests/test_filters.py
index 5f42952..5abbc28 100644
--- a/tests/test_filters.py
+++ b/tests/test_filters.py
@@ -22,11 +22,10 @@ from .utils import generate_sine_at
def rms(x: np.ndarray) -> float:
- return np.sqrt(np.mean(x ** 2))
-
-
-def normalized(x: np.ndarray) -> np.ndarray:
- return x / np.amax(np.abs(x))
+ if len(x.shape) == 1:
+ return np.sqrt(np.mean(x ** 2))
+ else:
+ return np.array([rms(channel) for channel in x])
def db_to_gain(db: float) -> float:
@@ -44,10 +43,11 @@ def octaves_between(a_hz: float, b_hz: float) -> float:
@pytest.mark.parametrize("filter_type", [HighpassFilter, LowpassFilter])
@pytest.mark.parametrize("fundamental_hz", [440, 880])
@pytest.mark.parametrize("sample_rate", [22050, 44100, 48000])
-def test_filter_attenutation(filter_type, fundamental_hz, sample_rate):
- num_seconds = 10.0
- samples = np.arange(num_seconds * sample_rate)
- sine_wave = np.sin(2 * np.pi * fundamental_hz * samples / sample_rate)
[email protected]("num_channels", [1, 2])
+def test_filter_attenutation(filter_type, fundamental_hz, sample_rate, num_channels):
+ sine_wave = generate_sine_at(
+ sample_rate, fundamental_hz, num_seconds=10, num_channels=num_channels
+ )
filtered = filter_type(cutoff_frequency_hz=fundamental_hz)(sine_wave, sample_rate)
# The volume at the cutoff frequency should be -3dB of the input volume
@@ -57,10 +57,11 @@ def test_filter_attenutation(filter_type, fundamental_hz, sample_rate):
@pytest.mark.parametrize("cutoff_frequency_hz", [440, 880])
@pytest.mark.parametrize("fundamental_hz", [880, 1760])
@pytest.mark.parametrize("sample_rate", [22050, 44100, 48000])
-def test_lowpass_slope(cutoff_frequency_hz, fundamental_hz, sample_rate):
- num_seconds = 1.0
- samples = np.arange(num_seconds * sample_rate)
- sine_wave = np.sin(2 * np.pi * fundamental_hz * samples / sample_rate)
[email protected]("num_channels", [1, 2])
+def test_lowpass_slope(cutoff_frequency_hz, fundamental_hz, sample_rate, num_channels):
+ sine_wave = generate_sine_at(
+ sample_rate, fundamental_hz, num_seconds=1, num_channels=num_channels
+ )
filtered = LowpassFilter(cutoff_frequency_hz=cutoff_frequency_hz)(sine_wave, sample_rate)
@@ -75,9 +76,12 @@ def test_lowpass_slope(cutoff_frequency_hz, fundamental_hz, sample_rate):
@pytest.mark.parametrize("filter_type", [HighShelfFilter, LowShelfFilter])
@pytest.mark.parametrize("fundamental_hz", [440, 880])
@pytest.mark.parametrize("sample_rate", [22050, 44100, 48000])
[email protected]("num_channels", [1, 2])
@pytest.mark.parametrize("gain_db", [-12, -6, 0, 6, 12])
-def test_shelf_filters(filter_type, fundamental_hz, sample_rate, gain_db):
- sine_wave = generate_sine_at(sample_rate, fundamental_hz, num_seconds=2)
+def test_shelf_filters(filter_type, fundamental_hz, sample_rate, num_channels, gain_db):
+ sine_wave = generate_sine_at(
+ sample_rate, fundamental_hz, num_seconds=2, num_channels=num_channels
+ )
filtered = filter_type(cutoff_frequency_hz=fundamental_hz, gain_db=gain_db)(
sine_wave, sample_rate
)
@@ -88,8 +92,9 @@ def test_shelf_filters(filter_type, fundamental_hz, sample_rate, gain_db):
@pytest.mark.parametrize("fundamental_hz", [440, 880])
@pytest.mark.parametrize("sample_rate", [22050, 44100, 48000])
[email protected]("num_channels", [1, 2])
@pytest.mark.parametrize("gain_db", [-12, -6, 0, 6, 12])
-def test_peak_filter(fundamental_hz, sample_rate, gain_db):
+def test_peak_filter(fundamental_hz, sample_rate, num_channels, gain_db):
sine_wave = generate_sine_at(sample_rate, fundamental_hz, num_seconds=2)
filtered = PeakFilter(cutoff_frequency_hz=fundamental_hz, gain_db=gain_db)(
sine_wave, sample_rate
@@ -102,10 +107,13 @@ def test_peak_filter(fundamental_hz, sample_rate, gain_db):
@pytest.mark.parametrize("filter_type", [HighShelfFilter, LowShelfFilter])
@pytest.mark.parametrize("fundamental_hz", [440, 880])
@pytest.mark.parametrize("sample_rate", [22050, 44100, 48000])
[email protected]("num_channels", [1, 2])
@pytest.mark.parametrize("gain_db", [-12, -6, 0, 6, 12])
@pytest.mark.parametrize("q", [1 / np.sqrt(2), 1, 100])
-def test_q_factor(filter_type, fundamental_hz, sample_rate, gain_db, q):
- sine_wave = generate_sine_at(sample_rate, fundamental_hz, num_seconds=2)
+def test_q_factor(filter_type, fundamental_hz, sample_rate, num_channels, gain_db, q):
+ sine_wave = generate_sine_at(
+ sample_rate, fundamental_hz, num_seconds=2, num_channels=num_channels
+ )
cutoff_frequency_hz = (
(fundamental_hz / 4) if filter_type == HighShelfFilter else (fundamental_hz * 4)