OpenShot Audio Library | OpenShotAudio: juce_NoiseGate.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
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
26namespace juce::dsp
27{
28
29//==============================================================================
30template <typename SampleType>
31NoiseGate<SampleType>::NoiseGate()
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
40template <typename SampleType>
41 void NoiseGate<SampleType>::setThreshold (SampleType newValue)
42{
43 thresholddB = newValue;
44 update();
45}
46
47template <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
56template <typename SampleType>
57 void NoiseGate<SampleType>::setAttack (SampleType newAttack)
58{
59 attackTime = newAttack;
60 update();
61}
62
63template <typename SampleType>
64 void NoiseGate<SampleType>::setRelease (SampleType newRelease)
65{
66 releaseTime = newRelease;
67 update();
68}
69
70//==============================================================================
71template <typename SampleType>
72 void NoiseGate<SampleType>::prepare (const ProcessSpec& spec)
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
86template <typename SampleType>
87 void NoiseGate<SampleType>::reset()
88{
89 RMSFilter.reset();
90 envelopeFilter.reset();
91}
92
93//==============================================================================
94template <typename SampleType>
95SampleType 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
111template <typename SampleType>
112void NoiseGate<SampleType>::update()
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//==============================================================================
123template class NoiseGate<float>;
124template class NoiseGate<double>;
125
126} // namespace juce::dsp
juce::Decibels::decibelsToGain
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition juce_Decibels.h:42
juce::dsp::NoiseGate
Definition juce_NoiseGate.h:37
juce::dsp::NoiseGate::prepare
void prepare(const ProcessSpec &spec)
Definition juce_NoiseGate.cpp:72
juce::dsp::NoiseGate::processSample
SampleType processSample(int channel, SampleType inputValue)
Definition juce_NoiseGate.cpp:95
juce::dsp::NoiseGate::setRelease
void setRelease(SampleType newRelease)
Definition juce_NoiseGate.cpp:64
juce::dsp::NoiseGate::setRatio
void setRatio(SampleType newRatio)
Definition juce_NoiseGate.cpp:48
juce::dsp::NoiseGate::setAttack
void setAttack(SampleType newAttack)
Definition juce_NoiseGate.cpp:57
juce::dsp::NoiseGate::setThreshold
void setThreshold(SampleType newThreshold)
Definition juce_NoiseGate.cpp:41
juce::dsp::NoiseGate::NoiseGate
NoiseGate()
Definition juce_NoiseGate.cpp:31
juce::dsp::NoiseGate::reset
void reset()
Definition juce_NoiseGate.cpp:87
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