NAP
filternode.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Std includes
8 #include <atomic>
9 
10 // Audio includes
11 #include <audio/core/audionode.h>
12 #include <audio/utility/delay.h>
13 #include <audio/utility/dirtyflag.h>
14 #include <audio/utility/linearsmoothedvalue.h>
15 
16 namespace nap
17 {
18  namespace audio
19  {
20 
25  class NAPAPI FilterNode : public Node
26  {
27  public:
28  enum class EMode
29  {
30  LowPass, HighPass, BandPass, LowRes, HighRes
31  };
32 
33  public:
34  FilterNode(NodeManager& nodeManager) : Node(nodeManager), mOutput(8), mInput(8)
35  {
36  update();
37  }
38 
39  // Inherited from Node
40  void process() override;
41 
45  InputPin audioInput = {this};
46 
50  OutputPin audioOutput = {this};
51 
59  void prepare(ControllerValue frequency, ControllerValue resonanceBand, ControllerValue gain);
60 
65  void setMode(EMode mode);
66 
72  void setFrequency(ControllerValue cutoffFrequency);
73 
77  void setResonance(ControllerValue resonance);
78 
82  void setBand(ControllerValue band);
83 
87  void setGain(ControllerValue gain);
88 
92  EMode getMode() const { return mMode; }
93 
97  ControllerValue getFrequency() const { return mFrequency; }
98 
102  ControllerValue getResonance() const { return mResonance; }
103 
107  ControllerValue getBand() const { return mBand; }
108 
112  ControllerValue getGain() const { return mGain; }
113 
114  private:
115  void update();
116  void calcCoeffs();
117 
118  std::atomic<EMode> mMode = {EMode::LowPass};
119  std::atomic<ControllerValue> mFrequency = {440.f};
120  std::atomic<ControllerValue> mResonance = {0.f};
121  std::atomic<ControllerValue> mBand = {100.f};
122  std::atomic<ControllerValue> mGain = {1.f};
123  DirtyFlag mIsDirty;
124 
125  // Filter coefficients
126  LinearSmoothedValue<ControllerValue> a0 = { 0, 64 };
127  LinearSmoothedValue<ControllerValue> a1 = { 0, 64 };
128  LinearSmoothedValue<ControllerValue> a2 = { 0, 64 };
129  LinearSmoothedValue<ControllerValue> b1 = { 0, 64 };
130  LinearSmoothedValue<ControllerValue> b2 = { 0, 64 };
131 
132  // Coeffecient destinations before update.
133  ControllerValue a0Dest, a1Dest, a2Dest, b1Dest, b2Dest = 0;
134 
135  Delay mOutput; // Delay line storing the input signal.
136  Delay mInput; // Delay line storing the output signal.
137  };
138 
139  }
140 }
nap::audio::FilterNode::getResonance
ControllerValue getResonance() const
Definition: filternode.h:102
nap::audio::FilterNode::getGain
ControllerValue getGain() const
Definition: filternode.h:112
nap::audio::FilterNode::getBand
ControllerValue getBand() const
Definition: filternode.h:107
nap::audio::FilterNode
Definition: filternode.h:25
nap::audio::NodeManager
Definition: audionodemanager.h:33
nap::audio::ControllerValue
float ControllerValue
Definition: audiotypes.h:113
nap::audio::FilterNode::getFrequency
ControllerValue getFrequency() const
Definition: filternode.h:97
nap::audio::OutputPin
Definition: audiopin.h:204
nap::audio::Node
Definition: audionode.h:33
nap::audio::FilterNode::FilterNode
FilterNode(NodeManager &nodeManager)
Definition: filternode.h:34
nap::audio::FilterNode::EMode
EMode
Definition: filternode.h:28
nap::audio::InputPin
Definition: audiopin.h:87
nap::audio::FilterNode::getMode
EMode getMode() const
Definition: filternode.h:92
nap
Definition: templateapp.h:17