OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
juce_LadderFilter.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 5 End-User License
11 Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12 27th April 2017).
13
14 End User License Agreement: www.juce.com/juce-5-licence
15 Privacy Policy: www.juce.com/juce-5-privacy-policy
16
17 Or: You may also use this code under the terms of the GPL v3 (see
18 www.gnu.org/licenses).
19
20 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22 DISCLAIMED.
23
24 ==============================================================================
25*/
26
27 namespace juce
28{
29 namespace dsp
30{
31
37 template <typename Type>
38 class LadderFilter
39{
40 public:
41 enum class Mode
42 {
43 LPF12, // low-pass 12 dB/octave
44 HPF12, // high-pass 12 dB/octave
45 LPF24, // low-pass 24 dB/octave
46 HPF24 // high-pass 24 dB/octave
47 };
48
49 //==============================================================================
51 LadderFilter();
52
54 void setEnabled (bool newValue) noexcept { enabled = newValue; }
55
57 void setMode (Mode newValue) noexcept;
58
60 void prepare (const juce::dsp::ProcessSpec& spec);
61
63 size_t getNumChannels() const noexcept { return state.size(); }
64
66 void reset() noexcept;
67
70 void setCutoffFrequencyHz (Type newValue) noexcept;
71
74 void setResonance (Type newValue) noexcept;
75
78 void setDrive (Type newValue) noexcept;
79
80 //==============================================================================
81 template <typename ProcessContext>
82 void process (const ProcessContext& context) noexcept
83 {
84 const auto& inputBlock = context.getInputBlock();
85 auto& outputBlock = context.getOutputBlock();
86 const auto numChannels = outputBlock.getNumChannels();
87 const auto numSamples = outputBlock.getNumSamples();
88
89 jassert (inputBlock.getNumChannels() <= getNumChannels());
90 jassert (inputBlock.getNumChannels() == numChannels);
91 jassert (inputBlock.getNumSamples() == numSamples);
92
93 if (! enabled || context.isBypassed)
94 {
95 outputBlock.copyFrom (inputBlock);
96 return;
97 }
98
99 for (size_t n = 0; n < numSamples; ++n)
100 {
101 updateSmoothers();
102
103 for (size_t ch = 0; ch < numChannels; ++ch)
104 outputBlock.getChannelPointer (ch)[n] = processSample (inputBlock.getChannelPointer (ch)[n], ch);
105 }
106 }
107
108 protected:
109 //==============================================================================
110 Type processSample (Type inputValue, size_t channelToUse) noexcept;
111 void updateSmoothers() noexcept;
112
113private:
114 //==============================================================================
115 Type drive, drive2, gain, gain2, comp;
116
117 static constexpr size_t numStates = 5;
118 std::vector<std::array<Type, numStates>> state;
119 std::array<Type, numStates> A;
120
121 SmoothedValue<Type> cutoffTransformSmoother, scaledResonanceSmoother;
122 Type cutoffTransformValue, scaledResonanceValue;
123
124 LookupTableTransform<Type> saturationLUT { [] (Type x) { return std::tanh (x); }, Type (-5), Type (5), 128 };
125
126 Type cutoffFreqHz { Type (200) };
127 Type resonance;
128
129 Type cutoffFreqScaler;
130
131 Mode mode;
132 bool enabled = true;
133
134 //==============================================================================
135 void setSampleRate (Type newValue) noexcept;
136 void setNumChannels (size_t newValue) { state.resize (newValue); }
137 void updateCutoffFreq() noexcept { cutoffTransformSmoother.setTargetValue (std::exp (cutoffFreqHz * cutoffFreqScaler)); }
138 void updateResonance() noexcept { scaledResonanceSmoother.setTargetValue (jmap (resonance, Type (0.1), Type (1.0))); }
139};
140
141} // namespace dsp
142} // namespace juce
juce::SmoothedValue::setTargetValue
void setTargetValue(FloatType newValue) noexcept
Definition: juce_SmoothedValue.h:273
juce::dsp::LadderFilter
Definition: juce_LadderFilter.h:39
juce::dsp::LadderFilter::LadderFilter
LadderFilter()
Definition: juce_LadderFilter.cpp:34
juce::dsp::LadderFilter::setResonance
void setResonance(Type newValue) noexcept
Definition: juce_LadderFilter.cpp:96
juce::dsp::LadderFilter::setMode
void setMode(Mode newValue) noexcept
Definition: juce_LadderFilter.cpp:45
juce::dsp::LadderFilter::reset
void reset() noexcept
Definition: juce_LadderFilter.cpp:76
juce::dsp::LadderFilter::setDrive
void setDrive(Type newValue) noexcept
Definition: juce_LadderFilter.cpp:105
juce::dsp::LadderFilter::getNumChannels
size_t getNumChannels() const noexcept
Definition: juce_LadderFilter.h:63
juce::dsp::LadderFilter::prepare
void prepare(const juce::dsp::ProcessSpec &spec)
Definition: juce_LadderFilter.cpp:67
juce::dsp::LadderFilter::setCutoffFrequencyHz
void setCutoffFrequencyHz(Type newValue) noexcept
Definition: juce_LadderFilter.cpp:87
juce::dsp::LadderFilter::setEnabled
void setEnabled(bool newValue) noexcept
Definition: juce_LadderFilter.h:54
juce::dsp::ProcessSpec
Definition: juce_ProcessContext.h:39