OpenShot Audio Library | OpenShotAudio  0.6.0
juce_FirstOrderTPTFilter.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 //==============================================================================
37 template <typename SampleType>
39 {
40  filterType = newValue;
41 }
42 
43 template <typename SampleType>
45 {
46  jassert (isPositiveAndBelow (newValue, static_cast<SampleType> (sampleRate * 0.5)));
47 
48  cutoffFrequency = newValue;
49  update();
50 }
51 
52 //==============================================================================
53 template <typename SampleType>
55 {
56  jassert (spec.sampleRate > 0);
57  jassert (spec.numChannels > 0);
58 
59  sampleRate = spec.sampleRate;
60  s1.resize (spec.numChannels);
61 
62  update();
63  reset();
64 }
65 
66 template <typename SampleType>
68 {
69  reset (static_cast<SampleType> (0));
70 }
71 
72 template <typename SampleType>
73 void FirstOrderTPTFilter<SampleType>::reset (SampleType newValue)
74 {
75  std::fill (s1.begin(), s1.end(), newValue);
76 }
77 
78 //==============================================================================
79 template <typename SampleType>
80 SampleType FirstOrderTPTFilter<SampleType>::processSample (int channel, SampleType inputValue)
81 {
82  auto& s = s1[(size_t) channel];
83 
84  auto v = G * (inputValue - s);
85  auto y = v + s;
86  s = y + v;
87 
88  switch (filterType)
89  {
90  case Type::lowpass: return y;
91  case Type::highpass: return inputValue - y;
92  case Type::allpass: return 2 * y - inputValue;
93  default: break;
94  }
95 
96  jassertfalse;
97  return y;
98 }
99 
100 template <typename SampleType>
102 {
103  for (auto& s : s1)
104  util::snapToZero (s);
105 }
106 
107 //==============================================================================
108 template <typename SampleType>
110 {
111  auto g = SampleType (std::tan (juce::MathConstants<double>::pi * cutoffFrequency / sampleRate));
112  G = g / (1 + g);
113 }
114 
115 //==============================================================================
116 template class FirstOrderTPTFilter<float>;
117 template class FirstOrderTPTFilter<double>;
118 
119 } // namespace juce::dsp
void prepare(const ProcessSpec &spec)
SampleType processSample(int channel, SampleType inputValue)
void setCutoffFrequency(SampleType newFrequencyHz)