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
26namespace juce::dsp
27{
28
29//==============================================================================
30template <typename SampleType>
35
36//==============================================================================
37template <typename SampleType>
39{
40 thresholddB = newThreshold;
41 update();
42}
43
44template <typename SampleType>
46{
47 jassert (newRatio >= static_cast<SampleType> (1.0));
48
49 ratio = newRatio;
50 update();
51}
52
53template <typename SampleType>
55{
56 attackTime = newAttack;
57 update();
58}
59
60template <typename SampleType>
62{
63 releaseTime = newRelease;
64 update();
65}
66
67//==============================================================================
68template <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
82template <typename SampleType>
84{
85 envelopeFilter.reset();
86}
87
88//==============================================================================
89template <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
97 : std::pow (env * thresholdInverse, ratioInverse - static_cast<SampleType> (1.0));
98
99 // Output
100 return gain * inputValue;
101}
102
103template <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//==============================================================================
115template class Compressor<float>;
116template class Compressor<double>;
117
118} // namespace juce::dsp
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
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)