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