OpenShot Library | OpenShotAudio 0.2.2
juce_LadderFilter.h
1
2/** @weakgroup juce_dsp-processors
3 * @{
4 */
5/*
6 ==============================================================================
7
8 This file is part of the JUCE library.
9 Copyright (c) 2017 - ROLI Ltd.
10
11 JUCE is an open source library subject to commercial or open-source
12 licensing.
13
14 By using JUCE, you agree to the terms of both the JUCE 5 End-User License
15 Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
16 27th April 2017).
17
18 End User License Agreement: www.juce.com/juce-5-licence
19 Privacy Policy: www.juce.com/juce-5-privacy-policy
20
21 Or: You may also use this code under the terms of the GPL v3 (see
22 www.gnu.org/licenses).
23
24 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
25 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
26 DISCLAIMED.
27
28 ==============================================================================
29*/
30
31namespace juce
32{
33namespace dsp
34{
35
36/**
37 Multi-mode filter based on the Moog ladder filter.
38
39 @tags{DSP}
40*/
41template <typename Type>
43{
44public:
45 enum class Mode
46 {
47 LPF12, // low-pass 12 dB/octave
48 HPF12, // high-pass 12 dB/octave
49 LPF24, // low-pass 24 dB/octave
50 HPF24 // high-pass 24 dB/octave
51 };
52
53 //==============================================================================
54 /** Creates an uninitialised filter. Call prepare() before first use. */
56
57 /** Enables or disables the filter. If disabled it will simply pass through the input signal. */
58 void setEnabled (bool newValue) noexcept { enabled = newValue; }
59
60 /** Sets filter mode. */
61 void setMode (Mode newValue) noexcept;
62
63 /** Initialises the filter. */
64 void prepare (const juce::dsp::ProcessSpec& spec);
65
66 /** Returns the current number of channels. */
67 size_t getNumChannels() const noexcept { return state.size(); }
68
69 /** Resets the internal state variables of the filter. */
70 void reset() noexcept;
71
72 /** Sets the cutoff frequency of the filter.
73 @param newValue cutoff frequency in Hz */
74 void setCutoffFrequencyHz (Type newValue) noexcept;
75
76 /** Sets the resonance of the filter.
77 @param newValue a value between 0 and 1; higher values increase the resonance and can result in self oscillation! */
78 void setResonance (Type newValue) noexcept;
79
80 /** Sets the amount of saturation in the filter.
81 @param newValue saturation amount; it can be any number greater than or equal to one. Higher values result in more distortion.*/
82 void setDrive (Type newValue) noexcept;
83
84 //==============================================================================
85 template <typename ProcessContext>
86 void process (const ProcessContext& context) noexcept
87 {
88 const auto& inputBlock = context.getInputBlock();
89 auto& outputBlock = context.getOutputBlock();
90 const auto numChannels = outputBlock.getNumChannels();
91 const auto numSamples = outputBlock.getNumSamples();
92
93 jassert (inputBlock.getNumChannels() <= getNumChannels());
94 jassert (inputBlock.getNumChannels() == numChannels);
95 jassert (inputBlock.getNumSamples() == numSamples);
96
97 if (! enabled || context.isBypassed)
98 {
99 outputBlock.copyFrom (inputBlock);
100 return;
101 }
102
103 for (size_t n = 0; n < numSamples; ++n)
104 {
105 updateSmoothers();
106
107 for (size_t ch = 0; ch < numChannels; ++ch)
108 outputBlock.getChannelPointer (ch)[n] = processSample (inputBlock.getChannelPointer (ch)[n], ch);
109 }
110 }
111
112protected:
113 //==============================================================================
114 Type processSample (Type inputValue, size_t channelToUse) noexcept;
115 void updateSmoothers() noexcept;
116
117private:
118 //==============================================================================
119 Type drive, drive2, gain, gain2, comp;
120
121 static constexpr size_t numStates = 5;
122 std::vector<std::array<Type, numStates>> state;
123 std::array<Type, numStates> A;
124
125 SmoothedValue<Type> cutoffTransformSmoother, scaledResonanceSmoother;
126 Type cutoffTransformValue, scaledResonanceValue;
127
128 LookupTableTransform<Type> saturationLUT { [] (Type x) { return std::tanh (x); }, Type (-5), Type (5), 128 };
129
130 Type cutoffFreqHz { Type (200) };
131 Type resonance;
132
133 Type cutoffFreqScaler;
134
135 Mode mode;
136 bool enabled = true;
137
138 //==============================================================================
139 void setSampleRate (Type newValue) noexcept;
140 void setNumChannels (size_t newValue) { state.resize (newValue); }
141 void updateCutoffFreq() noexcept { cutoffTransformSmoother.setTargetValue (std::exp (cutoffFreqHz * cutoffFreqScaler)); }
142 void updateResonance() noexcept { scaledResonanceSmoother.setTargetValue (jmap (resonance, Type (0.1), Type (1.0))); }
143};
144
145} // namespace dsp
146} // namespace juce
147
148/** @}*/
void setTargetValue(FloatType newValue) noexcept
Set the next value to ramp towards.
Multi-mode filter based on the Moog ladder filter.
LadderFilter()
Creates an uninitialised filter.
void setResonance(Type newValue) noexcept
Sets the resonance of the filter.
void setMode(Mode newValue) noexcept
Sets filter mode.
void reset() noexcept
Resets the internal state variables of the filter.
void setDrive(Type newValue) noexcept
Sets the amount of saturation in the filter.
size_t getNumChannels() const noexcept
Returns the current number of channels.
void prepare(const juce::dsp::ProcessSpec &spec)
Initialises the filter.
void setCutoffFrequencyHz(Type newValue) noexcept
Sets the cutoff frequency of the filter.
void setEnabled(bool newValue) noexcept
Enables or disables the filter.
This structure is passed into a DSP algorithm's prepare() method, and contains information about vari...