OpenShot Audio Library | OpenShotAudio  0.6.0
juce_NoiseGate.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  RMSFilter.setLevelCalculationType (BallisticsFilterLevelCalculationType::RMS);
36  RMSFilter.setAttackTime (static_cast<SampleType> (0.0));
37  RMSFilter.setReleaseTime (static_cast<SampleType> (50.0));
38 }
39 
40 template <typename SampleType>
41 void NoiseGate<SampleType>::setThreshold (SampleType newValue)
42 {
43  thresholddB = newValue;
44  update();
45 }
46 
47 template <typename SampleType>
48 void NoiseGate<SampleType>::setRatio (SampleType newRatio)
49 {
50  jassert (newRatio >= static_cast<SampleType> (1.0));
51 
52  ratio = newRatio;
53  update();
54 }
55 
56 template <typename SampleType>
57 void NoiseGate<SampleType>::setAttack (SampleType newAttack)
58 {
59  attackTime = newAttack;
60  update();
61 }
62 
63 template <typename SampleType>
64 void NoiseGate<SampleType>::setRelease (SampleType newRelease)
65 {
66  releaseTime = newRelease;
67  update();
68 }
69 
70 //==============================================================================
71 template <typename SampleType>
73 {
74  jassert (spec.sampleRate > 0);
75  jassert (spec.numChannels > 0);
76 
77  sampleRate = spec.sampleRate;
78 
79  RMSFilter.prepare (spec);
80  envelopeFilter.prepare (spec);
81 
82  update();
83  reset();
84 }
85 
86 template <typename SampleType>
88 {
89  RMSFilter.reset();
90  envelopeFilter.reset();
91 }
92 
93 //==============================================================================
94 template <typename SampleType>
95 SampleType NoiseGate<SampleType>::processSample (int channel, SampleType sample)
96 {
97  // RMS ballistics filter
98  auto env = RMSFilter.processSample (channel, sample);
99 
100  // Ballistics filter
101  env = envelopeFilter.processSample (channel, env);
102 
103  // VCA
104  auto gain = (env > threshold) ? static_cast<SampleType> (1.0)
105  : std::pow (env * thresholdInverse, currentRatio - static_cast<SampleType> (1.0));
106 
107  // Output
108  return gain * sample;
109 }
110 
111 template <typename SampleType>
113 {
114  threshold = Decibels::decibelsToGain (thresholddB, static_cast<SampleType> (-200.0));
115  thresholdInverse = static_cast<SampleType> (1.0) / threshold;
116  currentRatio = ratio;
117 
118  envelopeFilter.setAttackTime (attackTime);
119  envelopeFilter.setReleaseTime (releaseTime);
120 }
121 
122 //==============================================================================
123 template class NoiseGate<float>;
124 template class NoiseGate<double>;
125 
126 } // namespace juce::dsp
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition: juce_Decibels.h:42
void prepare(const ProcessSpec &spec)
SampleType processSample(int channel, SampleType inputValue)
void setRelease(SampleType newRelease)
void setRatio(SampleType newRatio)
void setAttack(SampleType newAttack)
void setThreshold(SampleType newThreshold)