Loading...
Searching...
No Matches
juce_Bias.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 Bias
{
public:
Bias() noexcept = default;
//==============================================================================
void setBias (FloatType newBias) noexcept
{
jassert (newBias >= static_cast<FloatType> (-1) && newBias <= static_cast<FloatType> (1));
bias.setTargetValue (newBias);
}
//==============================================================================
FloatType getBias() const noexcept { return bias.getTargetValue(); }
void setRampDurationSeconds (double newDurationSeconds) noexcept
{
if (! approximatelyEqual (rampDurationSeconds, newDurationSeconds))
{
rampDurationSeconds = newDurationSeconds;
updateRamp();
}
}
double getRampDurationSeconds() const noexcept { return rampDurationSeconds; }
//==============================================================================
void prepare (const ProcessSpec& spec) noexcept
{
sampleRate = spec.sampleRate;
updateRamp();
}
void reset() noexcept
{
bias.reset (sampleRate, rampDurationSeconds);
}
//==============================================================================
template <typename SampleType>
SampleType processSample (SampleType inputSample) noexcept
{
return inputSample + bias.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)
{
bias.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] + bias.getNextValue();
}
else
{
JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6255 6386)
auto* biases = static_cast<FloatType*> (alloca (sizeof (FloatType) * len));
for (size_t i = 0; i < len; ++i)
biases[i] = bias.getNextValue();
for (size_t chan = 0; chan < numChannels; ++chan)
FloatVectorOperations::add (outBlock.getChannelPointer (chan),
inBlock.getChannelPointer (chan),
biases, static_cast<int> (len));
JUCE_END_IGNORE_WARNINGS_MSVC
}
}
private:
//==============================================================================
SmoothedValue<FloatType> bias;
double sampleRate = 0, rampDurationSeconds = 0;
void updateRamp() noexcept
{
if (sampleRate > 0)
bias.reset (sampleRate, rampDurationSeconds);
}
};
} // namespace juce::dsp
juce::SmoothedValue
Definition juce_SmoothedValue.h:227
juce::SmoothedValue::reset
void reset(double sampleRate, double rampLengthInSeconds) noexcept
Definition juce_SmoothedValue.h:253
juce::dsp::Bias::processSample
SampleType processSample(SampleType inputSample) noexcept
Definition juce_Bias.h:90
juce::dsp::Bias::getBias
FloatType getBias() const noexcept
Definition juce_Bias.h:60
juce::dsp::Bias::prepare
void prepare(const ProcessSpec &spec) noexcept
Definition juce_Bias.h:76
juce::dsp::Bias::setRampDurationSeconds
void setRampDurationSeconds(double newDurationSeconds) noexcept
Definition juce_Bias.h:63
juce::dsp::Bias::setBias
void setBias(FloatType newBias) noexcept
Definition juce_Bias.h:50
juce::dsp::Bias::process
void process(const ProcessContext &context) noexcept
Definition juce_Bias.h:98
juce::dsp::ProcessSpec
Definition juce_ProcessContext.h:36