OpenShot Audio Library | OpenShotAudio  0.6.0
juce_FirstOrderTPTFilter.h
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 enum class FirstOrderTPTFilterType
30 {
31  lowpass,
32  highpass,
33  allpass
34 };
35 
36 //==============================================================================
52 template <typename SampleType>
54 {
55 public:
56  //==============================================================================
57  using Type = FirstOrderTPTFilterType;
58 
59  //==============================================================================
62 
63  //==============================================================================
65  void setType (Type newType);
66 
71  void setCutoffFrequency (SampleType newFrequencyHz);
72 
73  //==============================================================================
75  Type getType() const noexcept { return filterType; }
76 
78  SampleType getCutoffFrequency() const noexcept { return cutoffFrequency; }
79 
80  //==============================================================================
82  void prepare (const ProcessSpec& spec);
83 
85  void reset();
86 
88  void reset (SampleType newValue);
89 
90  //==============================================================================
92  template <typename ProcessContext>
93  void process (const ProcessContext& context) noexcept
94  {
95  const auto& inputBlock = context.getInputBlock();
96  auto& outputBlock = context.getOutputBlock();
97  const auto numChannels = outputBlock.getNumChannels();
98  const auto numSamples = outputBlock.getNumSamples();
99 
100  jassert (inputBlock.getNumChannels() <= s1.size());
101  jassert (inputBlock.getNumChannels() == numChannels);
102  jassert (inputBlock.getNumSamples() == numSamples);
103 
104  if (context.isBypassed)
105  {
106  outputBlock.copyFrom (inputBlock);
107  return;
108  }
109 
110  for (size_t channel = 0; channel < numChannels; ++channel)
111  {
112  auto* inputSamples = inputBlock .getChannelPointer (channel);
113  auto* outputSamples = outputBlock.getChannelPointer (channel);
114 
115  for (size_t i = 0; i < numSamples; ++i)
116  outputSamples[i] = processSample ((int) channel, inputSamples[i]);
117  }
118 
119  #if JUCE_DSP_ENABLE_SNAP_TO_ZERO
120  snapToZero();
121  #endif
122  }
123 
124  //==============================================================================
126  SampleType processSample (int channel, SampleType inputValue);
127 
132  void snapToZero() noexcept;
133 
134 private:
135  //==============================================================================
136  void update();
137 
138  //==============================================================================
139  SampleType G = 0;
140  std::vector<SampleType> s1 { 2 };
141  double sampleRate = 44100.0;
142 
143  //==============================================================================
144  Type filterType = Type::lowpass;
145  SampleType cutoffFrequency = 1000.0;
146 };
147 
148 } // namespace juce::dsp
SampleType getCutoffFrequency() const noexcept
void prepare(const ProcessSpec &spec)
SampleType processSample(int channel, SampleType inputValue)
void process(const ProcessContext &context) noexcept
void setCutoffFrequency(SampleType newFrequencyHz)