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 template <typename SampleType>
setThreshold(SampleType newThreshold)33 void Limiter<SampleType>::setThreshold (SampleType newThreshold)
34 {
35     thresholddB = newThreshold;
36     update();
37 }
38 
39 template <typename SampleType>
setRelease(SampleType newRelease)40 void Limiter<SampleType>::setRelease (SampleType newRelease)
41 {
42     releaseTime = newRelease;
43     update();
44 }
45 
46 //==============================================================================
47 template <typename SampleType>
prepare(const ProcessSpec & spec)48 void Limiter<SampleType>::prepare (const ProcessSpec& spec)
49 {
50     jassert (spec.sampleRate > 0);
51     jassert (spec.numChannels > 0);
52 
53     sampleRate = spec.sampleRate;
54 
55     firstStageCompressor.prepare (spec);
56     secondStageCompressor.prepare (spec);
57 
58     update();
59     reset();
60 }
61 
62 template <typename SampleType>
reset()63 void Limiter<SampleType>::reset()
64 {
65     firstStageCompressor.reset();
66     secondStageCompressor.reset();
67 
68     outputVolume.reset (sampleRate, 0.001);
69 }
70 
71 //==============================================================================
72 template <typename SampleType>
update()73 void Limiter<SampleType>::update()
74 {
75     firstStageCompressor.setThreshold ((SampleType) -10.0);
76     firstStageCompressor.setRatio     ((SampleType) 4.0);
77     firstStageCompressor.setAttack    ((SampleType) 2.0);
78     firstStageCompressor.setRelease   ((SampleType) 200.0);
79 
80     secondStageCompressor.setThreshold (thresholddB);
81     secondStageCompressor.setRatio     ((SampleType) 1000.0);
82     secondStageCompressor.setAttack    ((SampleType) 0.001);
83     secondStageCompressor.setRelease   (releaseTime);
84 
85     auto ratioInverse = (SampleType) (1.0 / 4.0);
86 
87     auto gain = (SampleType) std::pow (10.0, 10.0 * (1.0 - ratioInverse) / 40.0);
88     gain *= Decibels::decibelsToGain (-thresholddB, (SampleType) -100.0);
89 
90     outputVolume.setTargetValue (gain);
91 }
92 
93 //==============================================================================
94 template class Limiter<float>;
95 template class Limiter<double>;
96 
97 } // namespace dsp
98 } // namespace juce
99