Loading...
Searching...
No Matches
juce_LadderFilter.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce::dsp
{
enum class LadderFilterMode
{
LPF12, // low-pass 12 dB/octave
HPF12, // high-pass 12 dB/octave
BPF12, // band-pass 12 dB/octave
LPF24, // low-pass 24 dB/octave
HPF24, // high-pass 24 dB/octave
BPF24 // band-pass 24 dB/octave
};
template <typename SampleType>
class LadderFilter
{
public:
//==============================================================================
using Mode = LadderFilterMode;
//==============================================================================
LadderFilter();
void setEnabled (bool isEnabled) noexcept { enabled = isEnabled; }
void setMode (Mode newMode) noexcept;
void prepare (const ProcessSpec& spec);
size_t getNumChannels() const noexcept { return state.size(); }
void reset() noexcept;
void setCutoffFrequencyHz (SampleType newCutoff) noexcept;
void setResonance (SampleType newResonance) noexcept;
void setDrive (SampleType newDrive) noexcept;
//==============================================================================
template <typename ProcessContext>
void process (const ProcessContext& context) noexcept
{
const auto& inputBlock = context.getInputBlock();
auto& outputBlock = context.getOutputBlock();
const auto numChannels = outputBlock.getNumChannels();
const auto numSamples = outputBlock.getNumSamples();
jassert (inputBlock.getNumChannels() <= getNumChannels());
jassert (inputBlock.getNumChannels() == numChannels);
jassert (inputBlock.getNumSamples() == numSamples);
if (! enabled || context.isBypassed)
{
outputBlock.copyFrom (inputBlock);
return;
}
for (size_t n = 0; n < numSamples; ++n)
{
updateSmoothers();
for (size_t ch = 0; ch < numChannels; ++ch)
outputBlock.getChannelPointer (ch)[n] = processSample (inputBlock.getChannelPointer (ch)[n], ch);
}
}
protected:
//==============================================================================
SampleType processSample (SampleType inputValue, size_t channelToUse) noexcept;
void updateSmoothers() noexcept;
private:
//==============================================================================
void setSampleRate (SampleType newValue) noexcept;
void setNumChannels (size_t newValue) { state.resize (newValue); }
void updateCutoffFreq() noexcept { cutoffTransformSmoother.setTargetValue (std::exp (cutoffFreqHz * cutoffFreqScaler)); }
void updateResonance() noexcept { scaledResonanceSmoother.setTargetValue (jmap (resonance, SampleType (0.1), SampleType (1.0))); }
//==============================================================================
SampleType drive, drive2, gain, gain2, comp;
static constexpr size_t numStates = 5;
std::vector<std::array<SampleType, numStates>> state;
std::array<SampleType, numStates> A;
SmoothedValue<SampleType> cutoffTransformSmoother, scaledResonanceSmoother;
SampleType cutoffTransformValue, scaledResonanceValue;
LookupTableTransform<SampleType> saturationLUT { [] (SampleType x) { return std::tanh (x); },
SampleType (-5), SampleType (5), 128 };
SampleType cutoffFreqHz { SampleType (200) };
SampleType resonance;
SampleType cutoffFreqScaler;
Mode mode;
bool enabled = true;
};
} // namespace juce::dsp
juce::dsp::LadderFilter::LadderFilter
LadderFilter()
Definition juce_LadderFilter.cpp:31
juce::dsp::LadderFilter::setCutoffFrequencyHz
void setCutoffFrequencyHz(SampleType newCutoff) noexcept
Definition juce_LadderFilter.cpp:91
juce::dsp::LadderFilter::prepare
void prepare(const ProcessSpec &spec)
Definition juce_LadderFilter.cpp:71
juce::dsp::LadderFilter::setDrive
void setDrive(SampleType newDrive) noexcept
Definition juce_LadderFilter.cpp:109
juce::dsp::LadderFilter::setEnabled
void setEnabled(bool isEnabled) noexcept
Definition juce_LadderFilter.h:56
juce::dsp::LadderFilter::setMode
void setMode(Mode newMode) noexcept
Definition juce_LadderFilter.cpp:44
juce::dsp::LadderFilter::reset
void reset() noexcept
Definition juce_LadderFilter.cpp:80
juce::dsp::LadderFilter::getNumChannels
size_t getNumChannels() const noexcept
Definition juce_LadderFilter.h:65
juce::dsp::LadderFilter::setResonance
void setResonance(SampleType newResonance) noexcept
Definition juce_LadderFilter.cpp:100
juce::dsp::ProcessSpec
Definition juce_ProcessContext.h:36