iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
NoiseGate.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 The class in this file is derived from the FAUST ef.gate_mono code
9
10 https://faustlibraries.grame.fr/libs/misceffects/#efgate_mono
11
12 analyzers.lib/amp_follower_ar:author Jonatan Liljedahl, revised by Romain Michon
13 signals.lib/onePoleSwitching:author Jonatan Liljedahl, revised by Dario Sanfilippo
14 licence STK-4.3
15
16 https://github.com/grame-cncm/faustlibraries/blob/master/licenses/stk-4.3.0.md
17
18 ==============================================================================
19*/
20
21#pragma once
22
28#pragma once
29
30#include "IPlugUtilities.h"
31
32BEGIN_IPLUG_NAMESPACE
33
34template<typename T, int NC = 1>
36{
37public:
38 void SetThreshold(double thresholdDB)
39 {
40 mThreshold = DBToAmp(thresholdDB);
41 }
42
43 void SetSampleRate(double sampleRate)
44 {
45 mSampleRate = sampleRate;
46 }
47
48 void SetAttackTime(double attackTime)
49 {
50 mAttackTime = std::max(attackTime, (1.0 / mSampleRate));
51 mMinRate = std::min(mAttackTime, mReleaseTime);
52 }
53
54 void SetHoldTime(double holdTime)
55 {
56 mHoldTime = std::max(holdTime, (1.0 / mSampleRate));
57 }
58
59 void SetReleaseTime(double releaseTime)
60 {
61 mReleaseTime = std::max(releaseTime, (1.0 / mSampleRate));
62 mMinRate = std::min(mAttackTime, mReleaseTime);
63 }
64
65 void ProcessBlock(T** inputs, T** outputs, T* sidechain, int nChans, int nFrames)
66 {
67 assert(nChans <= NC);
68
69 for (auto s=0; s <nFrames; s++)
70 {
71 auto trigger = sidechain[s];
72 auto gain = processSample(trigger);
73 for (auto c=0; c<nChans; c++)
74 {
75 outputs[c][s] = inputs[c][s] * gain;
76 }
77 }
78 }
79
80private:
81 inline T processSample(T sample)
82 {
83 auto ampFollower = [](T x, T& y, double att, double rel, double sr) {
84 auto tau2pole = [](double tau, double sr) {
85 return std::exp(-1.0 / (tau * sr));
86 };
87 const auto absX = std::abs(x);
88 const auto coeff = (absX > y) ? T(tau2pole(att, sr)) : T(tau2pole(rel, sr));
89 y = (T(1) - coeff) * absX + coeff * y;
90 return y;
91 };
92
93 auto rawGate = ampFollower(sample, mHistory2, mMinRate, mMinRate, mSampleRate) > mThreshold;
94
95 if (rawGate < mPrevGate)
96 {
97 mHoldCounter = int(mHoldTime * mSampleRate);
98 }
99 else
100 {
101 if (mHoldCounter > 0)
102 {
103 mHoldCounter--;
104 }
105 }
106
107 const auto heldGate = std::max(rawGate, mHoldCounter > 0);
108 const auto smoothed = ampFollower(T(heldGate), mHistory1, mAttackTime, mReleaseTime, mSampleRate);
109
110 mPrevGate = rawGate;
111
112 return smoothed;
113 }
114
115 double mThreshold = 0.5;
116 double mSampleRate = 48000.0;
117 double mAttackTime = 0.01f;
118 double mHoldTime = 0.01f;
119 double mReleaseTime = 0.01f;
120 double mMinRate = mAttackTime;
121 T mHistory1, mHistory2 = 0.0f;
122 bool mPrevGate = false;
123 int mHoldCounter = 0;
124};
125
126END_IPLUG_NAMESPACE
Utility functions and macros.
static double DBToAmp(double dB)
Calculates gain from a given dB value.