Loading...
Searching...
No Matches
juce_BallisticsFilter.cpp
/*
==============================================================================
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 SampleType>
BallisticsFilter<SampleType>::BallisticsFilter()
{
setAttackTime (attackTime);
setReleaseTime (releaseTime);
}
template <typename SampleType>
void BallisticsFilter<SampleType>::setAttackTime (SampleType attackTimeMs)
{
attackTime = attackTimeMs;
cteAT = calculateLimitedCte (static_cast<SampleType> (attackTime));
}
template <typename SampleType>
void BallisticsFilter<SampleType>::setReleaseTime (SampleType releaseTimeMs)
{
releaseTime = releaseTimeMs;
cteRL = calculateLimitedCte (static_cast<SampleType> (releaseTime));
}
template <typename SampleType>
void BallisticsFilter<SampleType>::setLevelCalculationType (LevelCalculationType newLevelType)
{
levelType = newLevelType;
reset();
}
template <typename SampleType>
void BallisticsFilter<SampleType>::prepare (const ProcessSpec& spec)
{
jassert (spec.sampleRate > 0);
jassert (spec.numChannels > 0);
sampleRate = spec.sampleRate;
expFactor = -2.0 * MathConstants<double>::pi * 1000.0 / sampleRate;
setAttackTime (attackTime);
setReleaseTime (releaseTime);
yold.resize (spec.numChannels);
reset();
}
template <typename SampleType>
void BallisticsFilter<SampleType>::reset()
{
reset (0);
}
template <typename SampleType>
void BallisticsFilter<SampleType>::reset (SampleType initialValue)
{
for (auto& old : yold)
old = initialValue;
}
template <typename SampleType>
SampleType BallisticsFilter<SampleType>::processSample (int channel, SampleType inputValue)
{
jassert (isPositiveAndBelow (channel, yold.size()));
if (levelType == LevelCalculationType::RMS)
inputValue *= inputValue;
else
inputValue = std::abs (inputValue);
SampleType cte = (inputValue > yold[(size_t) channel] ? cteAT : cteRL);
SampleType result = inputValue + cte * (yold[(size_t) channel] - inputValue);
yold[(size_t) channel] = result;
if (levelType == LevelCalculationType::RMS)
return std::sqrt (result);
return result;
}
template <typename SampleType>
void BallisticsFilter<SampleType>::snapToZero() noexcept
{
for (auto& old : yold)
util::snapToZero (old);
}
template <typename SampleType>
SampleType BallisticsFilter<SampleType>::calculateLimitedCte (SampleType timeMs) const noexcept
{
return timeMs < static_cast<SampleType> (1.0e-3) ? 0
: static_cast<SampleType> (std::exp (expFactor / timeMs));
}
//==============================================================================
template class BallisticsFilter<float>;
template class BallisticsFilter<double>;
} // namespace juce::dsp
juce::dsp::BallisticsFilter::processSample
SampleType processSample(int channel, SampleType inputValue)
Definition juce_BallisticsFilter.cpp:89
juce::dsp::BallisticsFilter::reset
void reset()
Definition juce_BallisticsFilter.cpp:76
juce::dsp::BallisticsFilter::BallisticsFilter
BallisticsFilter()
Definition juce_BallisticsFilter.cpp:31
juce::dsp::BallisticsFilter::setAttackTime
void setAttackTime(SampleType attackTimeMs)
Definition juce_BallisticsFilter.cpp:38
juce::dsp::BallisticsFilter::prepare
void prepare(const ProcessSpec &spec)
Definition juce_BallisticsFilter.cpp:59
juce::dsp::BallisticsFilter::setReleaseTime
void setReleaseTime(SampleType releaseTimeMs)
Definition juce_BallisticsFilter.cpp:45
juce::dsp::BallisticsFilter::snapToZero
void snapToZero() noexcept
Definition juce_BallisticsFilter.cpp:110
juce::dsp::BallisticsFilter::setLevelCalculationType
void setLevelCalculationType(LevelCalculationType newCalculationType)
Definition juce_BallisticsFilter.cpp:52
juce::MathConstants::pi
static constexpr FloatType pi
Definition juce_MathsFunctions.h:141
juce::dsp::ProcessSpec
Definition juce_ProcessContext.h:36
juce::dsp::ProcessSpec::numChannels
uint32 numChannels
Definition juce_ProcessContext.h:44
juce::dsp::ProcessSpec::sampleRate
double sampleRate
Definition juce_ProcessContext.h:38