OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
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>
31 Compressor<SampleType>::Compressor()
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>
69 void Compressor<SampleType>::prepare (const ProcessSpec& spec)
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>
83 void Compressor<SampleType>::reset()
84{
85 envelopeFilter.reset();
86}
87
88//==============================================================================
89 template <typename SampleType>
90SampleType 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>
104 void Compressor<SampleType>::update()
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
juce::Decibels::decibelsToGain
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition juce_Decibels.h:42
juce::dsp::Compressor
Definition juce_Compressor.h:37
juce::dsp::Compressor::reset
void reset()
Definition juce_Compressor.cpp:83
juce::dsp::Compressor::setThreshold
void setThreshold(SampleType newThreshold)
Definition juce_Compressor.cpp:38
juce::dsp::Compressor::processSample
SampleType processSample(int channel, SampleType inputValue)
Definition juce_Compressor.cpp:90
juce::dsp::Compressor::prepare
void prepare(const ProcessSpec &spec)
Definition juce_Compressor.cpp:69
juce::dsp::Compressor::setAttack
void setAttack(SampleType newAttack)
Definition juce_Compressor.cpp:54
juce::dsp::Compressor::setRelease
void setRelease(SampleType newRelease)
Definition juce_Compressor.cpp:61
juce::dsp::Compressor::setRatio
void setRatio(SampleType newRatio)
Definition juce_Compressor.cpp:45
juce::dsp::Compressor::Compressor
Compressor()
Definition juce_Compressor.cpp:31
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