Loading...
Searching...
No Matches
juce_Gain.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce::dsp
{
template <typename FloatType>
class Gain
{
public:
Gain() noexcept = default;
//==============================================================================
void setGainLinear (FloatType newGain) noexcept { gain.setTargetValue (newGain); }
void setGainDecibels (FloatType newGainDecibels) noexcept { setGainLinear (Decibels::decibelsToGain<FloatType> (newGainDecibels)); }
FloatType getGainLinear() const noexcept { return gain.getTargetValue(); }
FloatType getGainDecibels() const noexcept { return Decibels::gainToDecibels<FloatType> (getGainLinear()); }
void setRampDurationSeconds (double newDurationSeconds) noexcept
{
if (! approximatelyEqual (rampDurationSeconds, newDurationSeconds))
{
rampDurationSeconds = newDurationSeconds;
reset();
}
}
double getRampDurationSeconds() const noexcept { return rampDurationSeconds; }
bool isSmoothing() const noexcept { return gain.isSmoothing(); }
//==============================================================================
void prepare (const ProcessSpec& spec) noexcept
{
sampleRate = spec.sampleRate;
reset();
}
void reset() noexcept
{
if (sampleRate > 0)
gain.reset (sampleRate, rampDurationSeconds);
}
//==============================================================================
template <typename SampleType>
SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType s) noexcept
{
return s * gain.getNextValue();
}
template <typename ProcessContext>
void process (const ProcessContext& context) noexcept
{
auto&& inBlock = context.getInputBlock();
auto&& outBlock = context.getOutputBlock();
jassert (inBlock.getNumChannels() == outBlock.getNumChannels());
jassert (inBlock.getNumSamples() == outBlock.getNumSamples());
auto len = inBlock.getNumSamples();
auto numChannels = inBlock.getNumChannels();
if (context.isBypassed)
{
gain.skip (static_cast<int> (len));
if (context.usesSeparateInputAndOutputBlocks())
outBlock.copyFrom (inBlock);
return;
}
if (numChannels == 1)
{
auto* src = inBlock.getChannelPointer (0);
auto* dst = outBlock.getChannelPointer (0);
for (size_t i = 0; i < len; ++i)
dst[i] = src[i] * gain.getNextValue();
}
else
{
JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6255 6386)
auto* gains = static_cast<FloatType*> (alloca (sizeof (FloatType) * len));
for (size_t i = 0; i < len; ++i)
gains[i] = gain.getNextValue();
JUCE_END_IGNORE_WARNINGS_MSVC
for (size_t chan = 0; chan < numChannels; ++chan)
FloatVectorOperations::multiply (outBlock.getChannelPointer (chan),
inBlock.getChannelPointer (chan),
gains, static_cast<int> (len));
}
}
private:
//==============================================================================
SmoothedValue<FloatType> gain;
double sampleRate = 0, rampDurationSeconds = 0;
};
} // namespace juce::dsp
juce::Decibels::decibelsToGain
static Type decibelsToGain(Type decibels, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition juce_Decibels.h:42
juce::Decibels::gainToDecibels
static Type gainToDecibels(Type gain, Type minusInfinityDb=Type(defaultMinusInfinitydB))
Definition juce_Decibels.h:56
juce::SmoothedValue
Definition juce_SmoothedValue.h:227
juce::dsp::Gain::getGainDecibels
FloatType getGainDecibels() const noexcept
Definition juce_Gain.h:51
juce::dsp::Gain::getGainLinear
FloatType getGainLinear() const noexcept
Definition juce_Gain.h:48
juce::dsp::Gain::prepare
void prepare(const ProcessSpec &spec) noexcept
Definition juce_Gain.h:71
juce::dsp::Gain::getRampDurationSeconds
double getRampDurationSeconds() const noexcept
Definition juce_Gain.h:64
juce::dsp::Gain::isSmoothing
bool isSmoothing() const noexcept
Definition juce_Gain.h:67
juce::dsp::Gain::setGainLinear
void setGainLinear(FloatType newGain) noexcept
Definition juce_Gain.h:42
juce::dsp::Gain::processSample
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType s) noexcept
Definition juce_Gain.h:87
juce::dsp::Gain::reset
void reset() noexcept
Definition juce_Gain.h:78
juce::dsp::Gain::process
void process(const ProcessContext &context) noexcept
Definition juce_Gain.h:94
juce::dsp::Gain::setRampDurationSeconds
void setRampDurationSeconds(double newDurationSeconds) noexcept
Definition juce_Gain.h:54
juce::dsp::Gain::setGainDecibels
void setGainDecibels(FloatType newGainDecibels) noexcept
Definition juce_Gain.h:45
juce::dsp::ProcessSpec
Definition juce_ProcessContext.h:36