OpenShot Audio Library | OpenShotAudio  0.6.0
juce_Compressor.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>
32 {
33  update();
34 }
35 
36 //==============================================================================
37 template <typename SampleType>
38 void Compressor<SampleType>::setThreshold (SampleType newThreshold)
39 {
40  thresholddB = newThreshold;
41  update();
42 }
43 
44 template <typename SampleType>
45 void Compressor<SampleType>::setRatio (SampleType newRatio)
46 {
47  jassert (newRatio >= static_cast<SampleType> (1.0));
48 
49  ratio = newRatio;
50  update();
51 }
52 
53 template <typename SampleType>
54 void Compressor<SampleType>::setAttack (SampleType newAttack)
55 {
56  attackTime = newAttack;
57  update();
58 }
59 
60 template <typename SampleType>
61 void Compressor<SampleType>::setRelease (SampleType newRelease)
62 {
63  releaseTime = newRelease;
64  update();
65 }
66 
67 //==============================================================================
68 template <typename SampleType>
70 {
71  jassert (spec.sampleRate > 0);
72  jassert (spec.numChannels > 0);
73 
74  sampleRate = spec.sampleRate;
75 
76  envelopeFilter.prepare (spec);
77 
78  update();
79  reset();
80 }
81 
82 template <typename SampleType>
84 {
85  envelopeFilter.reset();
86 }
87 
88 //==============================================================================
89 template <typename SampleType>
90 SampleType Compressor<SampleType>::processSample (int channel, SampleType inputValue)
91 {
92  // Ballistics filter with peak rectifier
93  auto env = envelopeFilter.processSample (channel, inputValue);
94 
95  // VCA
96  auto gain = (env < threshold) ? static_cast<SampleType> (1.0)
97  : std::pow (env * thresholdInverse, ratioInverse - static_cast<SampleType> (1.0));
98 
99  // Output
100  return gain * inputValue;
101 }
102 
103 template <typename SampleType>
105 {
106  threshold = Decibels::decibelsToGain (thresholddB, static_cast<SampleType> (-200.0));
107  thresholdInverse = static_cast<SampleType> (1.0) / threshold;
108  ratioInverse = static_cast<SampleType> (1.0) / ratio;
109 
110  envelopeFilter.setAttackTime (attackTime);
111  envelopeFilter.setReleaseTime (releaseTime);
112 }
113 
114 //==============================================================================
115 template class Compressor<float>;
116 template class Compressor<double>;
117 
118 } // namespace juce::dsp
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition: juce_Decibels.h:42
void setThreshold(SampleType newThreshold)
SampleType processSample(int channel, SampleType inputValue)
void prepare(const ProcessSpec &spec)
void setAttack(SampleType newAttack)
void setRelease(SampleType newRelease)
void setRatio(SampleType newRatio)