OpenShot Audio Library | OpenShotAudio  0.6.0
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 
26 namespace juce::dsp
27 {
28 
29 //==============================================================================
30 template <typename SampleType>
32 {
33  update();
34 }
35 
36 template <typename SampleType>
38 {
39  filterType = newValue;
40 }
41 
42 template <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 
51 template <typename SampleType>
53 {
54  jassert (newResonance > static_cast<SampleType> (0));
55 
56  resonance = newResonance;
57  update();
58 }
59 
60 //==============================================================================
61 template <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 
76 template <typename SampleType>
78 {
79  reset (static_cast<SampleType> (0));
80 }
81 
82 template <typename SampleType>
84 {
85  for (auto v : { &s1, &s2 })
86  std::fill (v->begin(), v->end(), newValue);
87 }
88 
89 template <typename SampleType>
91 {
92  for (auto v : { &s1, &s2 })
93  for (auto& element : *v)
94  util::snapToZero (element);
95 }
96 
97 //==============================================================================
98 template <typename SampleType>
99 SampleType 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 //==============================================================================
122 template <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 //==============================================================================
131 template class StateVariableTPTFilter<float>;
132 template class StateVariableTPTFilter<double>;
133 
134 } // namespace juce::dsp
void setResonance(SampleType newResonance)
SampleType processSample(int channel, SampleType inputValue)
void setCutoffFrequency(SampleType newFrequencyHz)