1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - 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 6 End-User License
11    Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
12 
13    End User License Agreement: www.juce.com/juce-6-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
27 {
28 namespace dsp
29 {
30 
31 /**
32     A simple noise gate with standard threshold, ratio, attack time and
33     release time controls. Can be used as an expander if the ratio is low.
34 
35     @tags{DSP}
36 */
37 template <typename SampleType>
38 class NoiseGate
39 {
40 public:
41     //==============================================================================
42     /** Constructor. */
43     NoiseGate();
44 
45     //==============================================================================
46     /** Sets the threshold in dB of the noise-gate.*/
47     void setThreshold (SampleType newThreshold);
48 
49     /** Sets the ratio of the noise-gate (must be higher or equal to 1).*/
50     void setRatio (SampleType newRatio);
51 
52     /** Sets the attack time in milliseconds of the noise-gate.*/
53     void setAttack (SampleType newAttack);
54 
55     /** Sets the release time in milliseconds of the noise-gate.*/
56     void setRelease (SampleType newRelease);
57 
58     //==============================================================================
59     /** Initialises the processor. */
60     void prepare (const ProcessSpec& spec);
61 
62     /** Resets the internal state variables of the processor. */
63     void reset();
64 
65     //==============================================================================
66     /** Processes the input and output samples supplied in the processing context. */
67     template <typename ProcessContext>
process(const ProcessContext & context)68     void process (const ProcessContext& context) noexcept
69     {
70         const auto& inputBlock = context.getInputBlock();
71         auto& outputBlock      = context.getOutputBlock();
72         const auto numChannels = outputBlock.getNumChannels();
73         const auto numSamples  = outputBlock.getNumSamples();
74 
75         jassert (inputBlock.getNumChannels() == numChannels);
76         jassert (inputBlock.getNumSamples() == numSamples);
77 
78         if (context.isBypassed)
79         {
80             outputBlock.copyFrom (inputBlock);
81             return;
82         }
83 
84         for (size_t channel = 0; channel < numChannels; ++channel)
85         {
86             auto* inputSamples  = inputBlock .getChannelPointer (channel);
87             auto* outputSamples = outputBlock.getChannelPointer (channel);
88 
89             for (size_t i = 0; i < numSamples; ++i)
90                 outputSamples[i] = processSample ((int) channel, inputSamples[i]);
91         }
92     }
93 
94     /** Performs the processing operation on a single sample at a time. */
95     SampleType processSample (int channel, SampleType inputValue);
96 
97 private:
98     //==============================================================================
99     void update();
100 
101     //==============================================================================
102     SampleType threshold, thresholdInverse, currentRatio;
103     BallisticsFilter<SampleType> envelopeFilter, RMSFilter;
104 
105     double sampleRate = 44100.0;
106     SampleType thresholddB = -100, ratio = 10.0, attackTime = 1.0, releaseTime = 100.0;
107 };
108 
109 } // namespace dsp
110 } // namespace juce
111