OpenShot Audio Library | OpenShotAudio: juce_StateVariableTPTFilter.cpp Source File

OpenShot Audio Library | OpenShotAudio 1.0.0

Loading...
Searching...
No Matches
juce_StateVariableTPTFilter.cpp
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>
31StateVariableTPTFilter<SampleType>::StateVariableTPTFilter()
32{
33 update();
34}
35
36template <typename SampleType>
37 void StateVariableTPTFilter<SampleType>::setType (Type newValue)
38{
39 filterType = newValue;
40}
41
42template <typename SampleType>
43 void StateVariableTPTFilter<SampleType>::setCutoffFrequency (SampleType newCutoffFrequencyHz)
44{
45 jassert (isPositiveAndBelow (newCutoffFrequencyHz, static_cast<SampleType> (sampleRate * 0.5)));
46
47 cutoffFrequency = newCutoffFrequencyHz;
48 update();
49}
50
51template <typename SampleType>
52 void StateVariableTPTFilter<SampleType>::setResonance (SampleType newResonance)
53{
54 jassert (newResonance > static_cast<SampleType> (0));
55
56 resonance = newResonance;
57 update();
58}
59
60//==============================================================================
61template <typename SampleType>
62 void StateVariableTPTFilter<SampleType>::prepare (const ProcessSpec& spec)
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>
77 void StateVariableTPTFilter<SampleType>::reset()
78{
79 reset (static_cast<SampleType> (0));
80}
81
82template <typename SampleType>
83 void StateVariableTPTFilter<SampleType>::reset (SampleType newValue)
84{
85 for (auto v : { &s1, &s2 })
86 std::fill (v->begin(), v->end(), newValue);
87}
88
89template <typename SampleType>
90 void StateVariableTPTFilter<SampleType>::snapToZero() noexcept
91{
92 for (auto v : { &s1, &s2 })
93 for (auto& element : *v)
94 util::snapToZero (element);
95}
96
97//==============================================================================
98template <typename SampleType>
99SampleType StateVariableTPTFilter<SampleType>::processSample (int channel, SampleType inputValue)
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>
123void StateVariableTPTFilter<SampleType>::update()
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
juce::dsp::StateVariableTPTFilter::StateVariableTPTFilter
StateVariableTPTFilter()
Definition juce_StateVariableTPTFilter.cpp:31
juce::dsp::StateVariableTPTFilter::snapToZero
void snapToZero() noexcept
Definition juce_StateVariableTPTFilter.cpp:90
juce::dsp::StateVariableTPTFilter::setType
void setType(Type newType)
Definition juce_StateVariableTPTFilter.cpp:37
juce::dsp::StateVariableTPTFilter::prepare
void prepare(const ProcessSpec &spec)
Definition juce_StateVariableTPTFilter.cpp:62
juce::dsp::StateVariableTPTFilter::setResonance
void setResonance(SampleType newResonance)
Definition juce_StateVariableTPTFilter.cpp:52
juce::dsp::StateVariableTPTFilter::reset
void reset()
Definition juce_StateVariableTPTFilter.cpp:77
juce::dsp::StateVariableTPTFilter::processSample
SampleType processSample(int channel, SampleType inputValue)
Definition juce_StateVariableTPTFilter.cpp:99
juce::dsp::StateVariableTPTFilter::setCutoffFrequency
void setCutoffFrequency(SampleType newFrequencyHz)
Definition juce_StateVariableTPTFilter.cpp:43
juce::MathConstants::pi
static constexpr FloatType pi
Definition juce_MathsFunctions.h:141
juce::dsp::ProcessSpec
Definition juce_ProcessContext.h:36
juce::dsp::ProcessSpec::numChannels
uint32 numChannels
Definition juce_ProcessContext.h:44
juce::dsp::ProcessSpec::sampleRate
double sampleRate
Definition juce_ProcessContext.h:38