OpenShot Library | OpenShotAudio 0.2.2
juce_Gain.h
1
2/** @weakgroup juce_dsp-processors
3 * @{
4 */
5/*
6 ==============================================================================
7
8 This file is part of the JUCE library.
9 Copyright (c) 2017 - ROLI Ltd.
10
11 JUCE is an open source library subject to commercial or open-source
12 licensing.
13
14 By using JUCE, you agree to the terms of both the JUCE 5 End-User License
15 Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
16 27th April 2017).
17
18 End User License Agreement: www.juce.com/juce-5-licence
19 Privacy Policy: www.juce.com/juce-5-privacy-policy
20
21 Or: You may also use this code under the terms of the GPL v3 (see
22 www.gnu.org/licenses).
23
24 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
25 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
26 DISCLAIMED.
27
28 ==============================================================================
29*/
30
31namespace juce
32{
33namespace dsp
34{
35
36/**
37 Applies a gain to audio samples as single samples or AudioBlocks.
38
39 @tags{DSP}
40*/
41template <typename FloatType>
42class Gain
43{
44public:
45 Gain() noexcept = default;
46
47 //==============================================================================
48 /** Applies a new gain as a linear value. */
49 void setGainLinear (FloatType newGain) noexcept { gain.setTargetValue (newGain); }
50
51 /** Applies a new gain as a decibel value. */
52 void setGainDecibels (FloatType newGainDecibels) noexcept { setGainLinear (Decibels::decibelsToGain<FloatType> (newGainDecibels)); }
53
54 /** Returns the current gain as a linear value. */
55 FloatType getGainLinear() const noexcept { return gain.getTargetValue(); }
56
57 /** Returns the current gain in decibels. */
58 FloatType getGainDecibels() const noexcept { return Decibels::gainToDecibels<FloatType> (getGainLinear()); }
59
60 /** Sets the length of the ramp used for smoothing gain changes. */
61 void setRampDurationSeconds (double newDurationSeconds) noexcept
62 {
63 if (rampDurationSeconds != newDurationSeconds)
64 {
65 rampDurationSeconds = newDurationSeconds;
66 reset();
67 }
68 }
69
70 /** Returns the ramp duration in seconds. */
71 double getRampDurationSeconds() const noexcept { return rampDurationSeconds; }
72
73 /** Returns true if the current value is currently being interpolated. */
74 bool isSmoothing() const noexcept { return gain.isSmoothing(); }
75
76 //==============================================================================
77 /** Called before processing starts. */
78 void prepare (const ProcessSpec& spec) noexcept
79 {
80 sampleRate = spec.sampleRate;
81 reset();
82 }
83
84 /** Resets the internal state of the gain */
85 void reset() noexcept
86 {
87 if (sampleRate > 0)
88 gain.reset (sampleRate, rampDurationSeconds);
89 }
90
91 //==============================================================================
92 /** Returns the result of processing a single sample. */
93 template <typename SampleType>
94 SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType s) noexcept
95 {
96 return s * gain.getNextValue();
97 }
98
99 /** Processes the input and output buffers supplied in the processing context. */
100 template <typename ProcessContext>
101 void process (const ProcessContext& context) noexcept
102 {
103 auto&& inBlock = context.getInputBlock();
104 auto&& outBlock = context.getOutputBlock();
105
106 jassert (inBlock.getNumChannels() == outBlock.getNumChannels());
107 jassert (inBlock.getNumSamples() == outBlock.getNumSamples());
108
109 auto len = inBlock.getNumSamples();
110 auto numChannels = inBlock.getNumChannels();
111
112 if (context.isBypassed)
113 {
114 gain.skip (static_cast<int> (len));
115
116 if (context.usesSeparateInputAndOutputBlocks())
117 outBlock.copyFrom (inBlock);
118
119 return;
120 }
121
122 if (numChannels == 1)
123 {
124 auto* src = inBlock.getChannelPointer (0);
125 auto* dst = outBlock.getChannelPointer (0);
126
127 for (size_t i = 0; i < len; ++i)
128 dst[i] = src[i] * gain.getNextValue();
129 }
130 else
131 {
132 auto* gains = static_cast<FloatType*> (alloca (sizeof (FloatType) * len));
133
134 for (size_t i = 0; i < len; ++i)
135 gains[i] = gain.getNextValue();
136
137 for (size_t chan = 0; chan < numChannels; ++chan)
138 FloatVectorOperations::multiply (outBlock.getChannelPointer (chan),
139 inBlock.getChannelPointer (chan),
140 gains, static_cast<int> (len));
141 }
142 }
143
144private:
145 //==============================================================================
147 double sampleRate = 0, rampDurationSeconds = 0;
148};
149
150} // namespace dsp
151} // namespace juce
152
153/** @}*/
static void JUCE_CALLTYPE multiply(float *dest, const float *src, int numValues) noexcept
Multiplies the destination values by the source values.
A utility class for values that need smoothing to avoid audio glitches.
Applies a gain to audio samples as single samples or AudioBlocks.
Definition: juce_Gain.h:43
FloatType getGainDecibels() const noexcept
Returns the current gain in decibels.
Definition: juce_Gain.h:58
FloatType getGainLinear() const noexcept
Returns the current gain as a linear value.
Definition: juce_Gain.h:55
void prepare(const ProcessSpec &spec) noexcept
Called before processing starts.
Definition: juce_Gain.h:78
double getRampDurationSeconds() const noexcept
Returns the ramp duration in seconds.
Definition: juce_Gain.h:71
bool isSmoothing() const noexcept
Returns true if the current value is currently being interpolated.
Definition: juce_Gain.h:74
void setGainLinear(FloatType newGain) noexcept
Applies a new gain as a linear value.
Definition: juce_Gain.h:49
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType s) noexcept
Returns the result of processing a single sample.
Definition: juce_Gain.h:94
void reset() noexcept
Resets the internal state of the gain.
Definition: juce_Gain.h:85
void process(const ProcessContext &context) noexcept
Processes the input and output buffers supplied in the processing context.
Definition: juce_Gain.h:101
void setRampDurationSeconds(double newDurationSeconds) noexcept
Sets the length of the ramp used for smoothing gain changes.
Definition: juce_Gain.h:61
void setGainDecibels(FloatType newGainDecibels) noexcept
Applies a new gain as a decibel value.
Definition: juce_Gain.h:52
This structure is passed into a DSP algorithm's prepare() method, and contains information about vari...