Skip to content

Commit

Permalink
feat: add dynamic IIR filter
Browse files Browse the repository at this point in the history
  • Loading branch information
zsliu98 committed Dec 25, 2023
1 parent dbe95f5 commit 168e73c
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 107 deletions.
2 changes: 1 addition & 1 deletion source/PluginProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PluginProcessor : public juce::AudioProcessor, public juce::AudioProcessor
void parameterChanged(const juce::String &parameterID, float newValue) override;

private:
zlIIR::SingleFilter<float> filter;
zlIIR::Filter<float> filter;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginProcessor)
};
12 changes: 3 additions & 9 deletions source/dsp/compressor/tracker/virtual_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,15 @@ namespace zlCompressor {

virtual ~VirtualTracker() = default;

virtual void reset() = 0;

virtual void prepare(const juce::dsp::ProcessSpec &spec) = 0;

virtual void reset() = 0;
virtual void process(const juce::AudioBuffer<FloatType> &buffer) = 0;

virtual void setMomentarySize(size_t mSize) = 0;

virtual inline FloatType getBufferPeak() = 0;

virtual inline FloatType getMomentaryLoudness() = 0;

virtual inline FloatType getIntegratedLoudness() = 0;

virtual inline FloatType getIntegratedTotalLoudness() = 0;

virtual void process(const juce::AudioBuffer<FloatType> &buffer) = 0;
};
}

Expand Down
15 changes: 15 additions & 0 deletions source/dsp/dynamic_filter/dynamic_filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (C) 2023 - zsliu98
// This file is part of ZLEqualizer
//
// ZLEqualizer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// ZLEqualizer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with ZLEqualizer. If not, see <https://www.gnu.org/licenses/>.

#ifndef ZLEQUALIZER_DYNAMIC_FILTER_H
#define ZLEQUALIZER_DYNAMIC_FILTER_H

#include "dynamic_iir_filter.h"

#endif //ZLEQUALIZER_DYNAMIC_FILTER_H
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,39 @@
//
// You should have received a copy of the GNU General Public License along with ZLEqualizer. If not, see <https://www.gnu.org/licenses/>.

#include "dynamic_filter.h"
#include "dynamic_iir_filter.h"

namespace zlIIR {
namespace zlDynamicFilter {
template<typename FloatType>
void DynamicFilter<FloatType>::prepare(const juce::dsp::ProcessSpec &spec) {
void DynamicIIRFilter<FloatType>::prepare(const juce::dsp::ProcessSpec &spec) {
mFilter.prepare(spec);
tFilter.prepare(spec);
sFilter.prepare(spec);
compressor.prepare(spec);
mixer.prepare(spec);
tBuffer.setSize(static_cast<int>(spec.numChannels),
static_cast<int>(spec.maximumBlockSize));
}

template<typename FloatType>
void DynamicFilter<FloatType>::process(juce::AudioBuffer<FloatType> &buffer) {
mFilter.process(buffer);
void DynamicIIRFilter<FloatType>::process(juce::AudioBuffer<FloatType> &mBuffer, juce::AudioBuffer<FloatType> &sBuffer) {
mFilter.process(mBuffer);
if (dynamicON.load()) {
tBuffer.makeCopyOf(buffer, true);
tFilter.process(tBuffer);
mixer.pushDrySamples(juce::dsp::AudioBlock<FloatType>(buffer));
mixer.mixWetSamples(juce::dsp::AudioBlock<FloatType>(tBuffer));
sFilter.process(sBuffer);
auto dryMixPortion = compressor.process(sBuffer);
if (dryMixPortion < 1.0) {
mixer.setWetMixProportion(1 - dryMixPortion);
mixer.pushDrySamples(juce::dsp::AudioBlock<FloatType>(mBuffer));
tBuffer.makeCopyOf(mBuffer, true);
tFilter.process(tBuffer);
mixer.mixWetSamples(juce::dsp::AudioBlock<FloatType>(tBuffer));
}
}
}

template
class DynamicFilter<float>;
class DynamicIIRFilter<float>;

template
class DynamicFilter<double>;
class DynamicIIRFilter<double>;
}
49 changes: 49 additions & 0 deletions source/dsp/dynamic_filter/dynamic_iir_filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2023 - zsliu98
// This file is part of ZLEqualizer
//
// ZLEqualizer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// ZLEqualizer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with ZLEqualizer. If not, see <https://www.gnu.org/licenses/>.

#ifndef ZLEQUALIZER_DYNAMIC_IIR_FILTER_H
#define ZLEQUALIZER_DYNAMIC_IIR_FILTER_H

#include <juce_audio_processors/juce_audio_processors.h>
#include <juce_dsp/juce_dsp.h>

#include "../iir_filter/iir_filter.h"
#include "../compressor/compressor.h"

namespace zlDynamicFilter {
template<typename FloatType>
class DynamicIIRFilter {
public:
DynamicIIRFilter() = default;

void prepare(const juce::dsp::ProcessSpec &spec);

void process(juce::AudioBuffer<FloatType> &mBuffer, juce::AudioBuffer<FloatType> &sBuffer);

inline zlIIR::Filter<FloatType> &getMainFilter() { return mFilter; }

inline zlIIR::Filter<FloatType> &getTargetFilter() { return tFilter; }

inline zlIIR::Filter<FloatType> &getSideFilter() { return sFilter; }

inline void setDynamicON(bool x) {
dynamicON.store(x);
}

private:
zlIIR::Filter<FloatType> mFilter, tFilter, sFilter;
zlCompressor::ForwardCompressor<FloatType> compressor;
juce::dsp::DryWetMixer<FloatType> mixer;
juce::AudioBuffer<FloatType> tBuffer;
std::atomic<bool> dynamicON = false;
};
}


#endif //ZLEQUALIZER_DYNAMIC_IIR_FILTER_H
73 changes: 0 additions & 73 deletions source/dsp/iir_filter/dynamic_filter.h

This file was deleted.

1 change: 0 additions & 1 deletion source/dsp/iir_filter/iir_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@
#define ZLEQUALIZER_IIR_FILTER_H

#include "single_filter.h"
#include "dynamic_filter.h"

#endif //ZLEQUALIZER_IIR_FILTER_H
20 changes: 10 additions & 10 deletions source/dsp/iir_filter/single_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

namespace zlIIR {
template<typename FloatType>
void SingleFilter<FloatType>::prepare(const juce::dsp::ProcessSpec &spec) {
void Filter<FloatType>::prepare(const juce::dsp::ProcessSpec &spec) {
processSpec = spec;
setOrder(order.load());
updateParas();
}

template<typename FloatType>
void SingleFilter<FloatType>::process(juce::AudioBuffer<FloatType> &buffer) {
void Filter<FloatType>::process(juce::AudioBuffer<FloatType> &buffer) {
auto block = juce::dsp::AudioBlock<FloatType>(buffer);
auto context = juce::dsp::ProcessContextReplacing<FloatType>(block);
for (auto &f: filters) {
Expand All @@ -31,37 +31,37 @@ namespace zlIIR {
}

template<typename FloatType>
void SingleFilter<FloatType>::setFreq(FloatType x) {
void Filter<FloatType>::setFreq(FloatType x) {
freq.store(static_cast<double>(x));
updateParas();
}

template<typename FloatType>
void SingleFilter<FloatType>::setGain(FloatType x) {
void Filter<FloatType>::setGain(FloatType x) {
gain.store(static_cast<double>(x));
updateParas();
}

template<typename FloatType>
void SingleFilter<FloatType>::setQ(FloatType x) {
void Filter<FloatType>::setQ(FloatType x) {
q.store(static_cast<double>(x));
updateParas();
}

template<typename FloatType>
void SingleFilter<FloatType>::setFilterType(zlIIR::FilterType x) {
void Filter<FloatType>::setFilterType(zlIIR::FilterType x) {
filterType.store(x);
updateParas();
}

template<typename FloatType>
void SingleFilter<FloatType>::setOrder(size_t x) {
void Filter<FloatType>::setOrder(size_t x) {
order.store(x);
updateParas();
}

template<typename FloatType>
void SingleFilter<FloatType>::updateParas() {
void Filter<FloatType>::updateParas() {
auto coeff = DesignFilter::getCoeff(filterType.load(),
freq.load(), processSpec.sampleRate,
gain.load(), q.load(), order.load());
Expand All @@ -83,8 +83,8 @@ namespace zlIIR {
}

template
class SingleFilter<float>;
class Filter<float>;

template
class SingleFilter<double>;
class Filter<double>;
}
4 changes: 2 additions & 2 deletions source/dsp/iir_filter/single_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

namespace zlIIR {
template<typename FloatType>
class SingleFilter {
class Filter {
public:
SingleFilter() = default;
Filter() = default;

void prepare(const juce::dsp::ProcessSpec &spec);

Expand Down

0 comments on commit 168e73c

Please sign in to comment.