OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
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>
31 FirstOrderTPTFilter<SampleType>::FirstOrderTPTFilter()
32{
33 update();
34}
35
36//==============================================================================
37 template <typename SampleType>
38 void FirstOrderTPTFilter<SampleType>::setType (Type newValue)
39{
40 filterType = newValue;
41}
42
43 template <typename SampleType>
44 void FirstOrderTPTFilter<SampleType>::setCutoffFrequency (SampleType newValue)
45{
46 jassert (isPositiveAndBelow (newValue, static_cast<SampleType> (sampleRate * 0.5)));
47
48 cutoffFrequency = newValue;
49 update();
50}
51
52//==============================================================================
53 template <typename SampleType>
54 void FirstOrderTPTFilter<SampleType>::prepare (const ProcessSpec& spec)
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>
67 void FirstOrderTPTFilter<SampleType>::reset()
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>
80SampleType 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>
101 void FirstOrderTPTFilter<SampleType>::snapToZero() noexcept
102{
103 for (auto& s : s1)
104 util::snapToZero (s);
105}
106
107//==============================================================================
108 template <typename SampleType>
109 void FirstOrderTPTFilter<SampleType>::update()
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
juce::dsp::FirstOrderTPTFilter
Definition juce_FirstOrderTPTFilter.h:54
juce::dsp::FirstOrderTPTFilter::prepare
void prepare(const ProcessSpec &spec)
Definition juce_FirstOrderTPTFilter.cpp:54
juce::dsp::FirstOrderTPTFilter::snapToZero
void snapToZero() noexcept
Definition juce_FirstOrderTPTFilter.cpp:101
juce::dsp::FirstOrderTPTFilter::processSample
SampleType processSample(int channel, SampleType inputValue)
Definition juce_FirstOrderTPTFilter.cpp:80
juce::dsp::FirstOrderTPTFilter::FirstOrderTPTFilter
FirstOrderTPTFilter()
Definition juce_FirstOrderTPTFilter.cpp:31
juce::dsp::FirstOrderTPTFilter::setType
void setType(Type newType)
Definition juce_FirstOrderTPTFilter.cpp:38
juce::dsp::FirstOrderTPTFilter::setCutoffFrequency
void setCutoffFrequency(SampleType newFrequencyHz)
Definition juce_FirstOrderTPTFilter.cpp:44
juce::dsp::FirstOrderTPTFilter::reset
void reset()
Definition juce_FirstOrderTPTFilter.cpp:67
juce::MathConstants
Definition juce_MathsFunctions.h:139
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