JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_StateVariableTPTFilter.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce::dsp
27{
28
29//==============================================================================
30template <typename SampleType>
35
36template <typename SampleType>
38{
39 filterType = newValue;
40}
41
42template <typename SampleType>
44{
45 jassert (isPositiveAndBelow (newCutoffFrequencyHz, static_cast<SampleType> (sampleRate * 0.5)));
46
47 cutoffFrequency = newCutoffFrequencyHz;
48 update();
49}
50
51template <typename SampleType>
53{
54 jassert (newResonance > static_cast<SampleType> (0));
55
56 resonance = newResonance;
57 update();
58}
59
60//==============================================================================
61template <typename SampleType>
63{
64 jassert (spec.sampleRate > 0);
65 jassert (spec.numChannels > 0);
66
67 sampleRate = spec.sampleRate;
68
69 s1.resize (spec.numChannels);
70 s2.resize (spec.numChannels);
71
72 reset();
73 update();
74}
75
76template <typename SampleType>
78{
79 reset (static_cast<SampleType> (0));
80}
81
82template <typename SampleType>
84{
85 for (auto v : { &s1, &s2 })
86 std::fill (v->begin(), v->end(), newValue);
87}
88
89template <typename SampleType>
91{
92 for (auto v : { &s1, &s2 })
93 for (auto& element : *v)
94 util::snapToZero (element);
95}
96
97//==============================================================================
98template <typename SampleType>
100{
101 auto& ls1 = s1[(size_t) channel];
102 auto& ls2 = s2[(size_t) channel];
103
104 auto yHP = h * (inputValue - ls1 * (g + R2) - ls2);
105
106 auto yBP = yHP * g + ls1;
107 ls1 = yHP * g + yBP;
108
109 auto yLP = yBP * g + ls2;
110 ls2 = yBP * g + yLP;
111
112 switch (filterType)
113 {
114 case Type::lowpass: return yLP;
115 case Type::bandpass: return yBP;
116 case Type::highpass: return yHP;
117 default: return yLP;
118 }
119}
120
121//==============================================================================
122template <typename SampleType>
124{
125 g = static_cast<SampleType> (std::tan (juce::MathConstants<double>::pi * cutoffFrequency / sampleRate));
126 R2 = static_cast<SampleType> (1.0 / resonance);
127 h = static_cast<SampleType> (1.0 / (1.0 + R2 * g + g * g));
128}
129
130//==============================================================================
131template class StateVariableTPTFilter<float>;
132template class StateVariableTPTFilter<double>;
133
134} // namespace juce::dsp
An IIR filter that can perform low, band and high-pass filtering on an audio signal,...
void snapToZero() noexcept
Ensure that the state variables are rounded to zero if the state variables are denormals.
void setType(Type newType)
Sets the filter type.
void prepare(const ProcessSpec &spec)
Initialises the filter.
void setResonance(SampleType newResonance)
Sets the resonance of the filter.
void reset()
Resets the internal state variables of the filter.
SampleType processSample(int channel, SampleType inputValue)
Processes one sample at a time on a given channel.
void setCutoffFrequency(SampleType newFrequencyHz)
Sets the cutoff frequency of the filter.
T fill(T... args)
#define jassert(expression)
Platform-independent assertion macro.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Returns true if a value is at least zero, and also below a specified upper limit.
Commonly used mathematical constants.
This structure is passed into a DSP algorithm's prepare() method, and contains information about vari...
typedef size_t
T tan(T... args)