LMMS
Loading...
Searching...
No Matches
juce_Compressor.cpp
Go to the documentation of this file.
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
27{
28namespace dsp
29{
30
31//==============================================================================
32template <typename SampleType>
37
38//==============================================================================
39template <typename SampleType>
40void Compressor<SampleType>::setThreshold (SampleType newThreshold)
41{
42 thresholddB = newThreshold;
43 update();
44}
45
46template <typename SampleType>
47void Compressor<SampleType>::setRatio (SampleType newRatio)
48{
49 jassert (newRatio >= static_cast<SampleType> (1.0));
50
51 ratio = newRatio;
52 update();
53}
54
55template <typename SampleType>
56void Compressor<SampleType>::setAttack (SampleType newAttack)
57{
58 attackTime = newAttack;
59 update();
60}
61
62template <typename SampleType>
63void Compressor<SampleType>::setRelease (SampleType newRelease)
64{
65 releaseTime = newRelease;
66 update();
67}
68
69//==============================================================================
70template <typename SampleType>
72{
73 jassert (spec.sampleRate > 0);
74 jassert (spec.numChannels > 0);
75
77
78 envelopeFilter.prepare (spec);
79
80 update();
81 reset();
82}
83
84template <typename SampleType>
89
90//==============================================================================
91template <typename SampleType>
92SampleType Compressor<SampleType>::processSample (int channel, SampleType inputValue)
93{
94 // Ballistics filter with peak rectifier
95 auto env = envelopeFilter.processSample (channel, inputValue);
96
97 // VCA
98 auto gain = (env < threshold) ? static_cast<SampleType> (1.0)
99 : std::pow (env * thresholdInverse, ratioInverse - static_cast<SampleType> (1.0));
100
101 // Output
102 return gain * inputValue;
103}
104
105template <typename SampleType>
107{
108 threshold = Decibels::decibelsToGain (thresholddB, static_cast<SampleType> (-200.0));
109 thresholdInverse = static_cast<SampleType> (1.0) / threshold;
110 ratioInverse = static_cast<SampleType> (1.0) / ratio;
111
112 envelopeFilter.setAttackTime (attackTime);
113 envelopeFilter.setReleaseTime (releaseTime);
114}
115
116//==============================================================================
117template class Compressor<float>;
118template class Compressor<double>;
119
120} // namespace dsp
121} // namespace juce
Compressor(int sample_rate, float clamp)
Definition lofi.cpp:189
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition juce_Decibels.h:42
Definition juce_Compressor.h:39
SampleType processSample(int channel, SampleType inputValue)
Definition juce_Compressor.cpp:92
void prepare(const ProcessSpec &spec)
Definition juce_Compressor.cpp:71
void setRelease(SampleType newRelease)
Definition juce_Compressor.cpp:63
SampleType ratioInverse
Definition juce_Compressor.h:102
void setThreshold(SampleType newThreshold)
Definition juce_Compressor.cpp:40
void setAttack(SampleType newAttack)
Definition juce_Compressor.cpp:56
BallisticsFilter< SampleType > envelopeFilter
Definition juce_Compressor.h:103
void reset()
Definition juce_Compressor.cpp:85
SampleType thresholdInverse
Definition juce_Compressor.h:102
SampleType ratio
Definition juce_Compressor.h:106
double sampleRate
Definition juce_Compressor.h:105
void update()
Definition juce_Compressor.cpp:106
void setRatio(SampleType newRatio)
Definition juce_Compressor.cpp:47
SampleType threshold
Definition juce_Compressor.h:102
SampleType releaseTime
Definition juce_Compressor.h:106
SampleType thresholddB
Definition juce_Compressor.h:106
SampleType attackTime
Definition juce_Compressor.h:106
#define jassert(expression)
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
Definition juce_ProcessContext.h:38
uint32 numChannels
Definition juce_ProcessContext.h:46
double sampleRate
Definition juce_ProcessContext.h:40