OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
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>
46 void Limiter<SampleType>::prepare (const ProcessSpec& spec)
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>
61 void Limiter<SampleType>::reset()
62{
63 firstStageCompressor.reset();
64 secondStageCompressor.reset();
65
66 outputVolume.reset (sampleRate, 0.001);
67}
68
69//==============================================================================
70 template <typename SampleType>
71 void Limiter<SampleType>::update()
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
juce::Decibels::decibelsToGain
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition juce_Decibels.h:42
juce::dsp::Limiter
Definition juce_Limiter.h:37
juce::dsp::Limiter::setThreshold
void setThreshold(SampleType newThreshold)
Definition juce_Limiter.cpp:31
juce::dsp::Limiter::prepare
void prepare(const ProcessSpec &spec)
Definition juce_Limiter.cpp:46
juce::dsp::Limiter::setRelease
void setRelease(SampleType newRelease)
Definition juce_Limiter.cpp:38
juce::dsp::Limiter::reset
void reset()
Definition juce_Limiter.cpp:61
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