Loading...
Searching...
No Matches
juce_dsp/processors/juce_IIRFilter.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::IIR
{
template <typename NumericType>
struct ArrayCoefficients
{
static std::array<NumericType, 4> makeFirstOrderLowPass (double sampleRate, NumericType frequency);
static std::array<NumericType, 4> makeFirstOrderHighPass (double sampleRate, NumericType frequency);
static std::array<NumericType, 4> makeFirstOrderAllPass (double sampleRate, NumericType frequency);
static std::array<NumericType, 6> makeLowPass (double sampleRate, NumericType frequency);
static std::array<NumericType, 6> makeLowPass (double sampleRate, NumericType frequency, NumericType Q);
static std::array<NumericType, 6> makeHighPass (double sampleRate, NumericType frequency);
static std::array<NumericType, 6> makeHighPass (double sampleRate, NumericType frequency, NumericType Q);
static std::array<NumericType, 6> makeBandPass (double sampleRate, NumericType frequency);
static std::array<NumericType, 6> makeBandPass (double sampleRate, NumericType frequency, NumericType Q);
static std::array<NumericType, 6> makeNotch (double sampleRate, NumericType frequency);
static std::array<NumericType, 6> makeNotch (double sampleRate, NumericType frequency, NumericType Q);
static std::array<NumericType, 6> makeAllPass (double sampleRate, NumericType frequency);
static std::array<NumericType, 6> makeAllPass (double sampleRate, NumericType frequency, NumericType Q);
static std::array<NumericType, 6> makeLowShelf (double sampleRate,
NumericType cutOffFrequency,
NumericType Q,
NumericType gainFactor);
static std::array<NumericType, 6> makeHighShelf (double sampleRate,
NumericType cutOffFrequency,
NumericType Q,
NumericType gainFactor);
static std::array<NumericType, 6> makePeakFilter (double sampleRate,
NumericType centreFrequency,
NumericType Q,
NumericType gainFactor);
private:
// Unfortunately, std::sqrt is not marked as constexpr just yet in all compilers
static constexpr NumericType inverseRootTwo = static_cast<NumericType> (0.70710678118654752440L);
};
//==============================================================================
template <typename NumericType>
struct Coefficients : public ProcessorState
{
Coefficients();
Coefficients (NumericType b0, NumericType b1,
NumericType a0, NumericType a1);
Coefficients (NumericType b0, NumericType b1, NumericType b2,
NumericType a0, NumericType a1, NumericType a2);
Coefficients (NumericType b0, NumericType b1, NumericType b2, NumericType b3,
NumericType a0, NumericType a1, NumericType a2, NumericType a3);
Coefficients (const Coefficients&) = default;
Coefficients (Coefficients&&) = default;
Coefficients& operator= (const Coefficients&) = default;
Coefficients& operator= (Coefficients&&) = default;
template <size_t Num>
explicit Coefficients (const std::array<NumericType, Num>& values) { assignImpl<Num> (values.data()); }
template <size_t Num>
Coefficients& operator= (const std::array<NumericType, Num>& values) { return assignImpl<Num> (values.data()); }
using Ptr = ReferenceCountedObjectPtr<Coefficients>;
//==============================================================================
static Ptr makeFirstOrderLowPass (double sampleRate, NumericType frequency);
static Ptr makeFirstOrderHighPass (double sampleRate, NumericType frequency);
static Ptr makeFirstOrderAllPass (double sampleRate, NumericType frequency);
//==============================================================================
static Ptr makeLowPass (double sampleRate, NumericType frequency);
static Ptr makeLowPass (double sampleRate, NumericType frequency, NumericType Q);
//==============================================================================
static Ptr makeHighPass (double sampleRate, NumericType frequency);
static Ptr makeHighPass (double sampleRate, NumericType frequency, NumericType Q);
//==============================================================================
static Ptr makeBandPass (double sampleRate, NumericType frequency);
static Ptr makeBandPass (double sampleRate, NumericType frequency, NumericType Q);
//==============================================================================
static Ptr makeNotch (double sampleRate, NumericType frequency);
static Ptr makeNotch (double sampleRate, NumericType frequency, NumericType Q);
//==============================================================================
static Ptr makeAllPass (double sampleRate, NumericType frequency);
static Ptr makeAllPass (double sampleRate, NumericType frequency, NumericType Q);
//==============================================================================
static Ptr makeLowShelf (double sampleRate, NumericType cutOffFrequency,
NumericType Q, NumericType gainFactor);
static Ptr makeHighShelf (double sampleRate, NumericType cutOffFrequency,
NumericType Q, NumericType gainFactor);
static Ptr makePeakFilter (double sampleRate, NumericType centreFrequency,
NumericType Q, NumericType gainFactor);
//==============================================================================
size_t getFilterOrder() const noexcept;
double getMagnitudeForFrequency (double frequency, double sampleRate) const noexcept;
void getMagnitudeForFrequencyArray (const double* frequencies, double* magnitudes,
size_t numSamples, double sampleRate) const noexcept;
double getPhaseForFrequency (double frequency, double sampleRate) const noexcept;
void getPhaseForFrequencyArray (double* frequencies, double* phases,
size_t numSamples, double sampleRate) const noexcept;
NumericType* getRawCoefficients() noexcept { return coefficients.getRawDataPointer(); }
const NumericType* getRawCoefficients() const noexcept { return coefficients.begin(); }
//==============================================================================
Array<NumericType> coefficients;
private:
using ArrayCoeffs = ArrayCoefficients<NumericType>;
template <size_t Num>
Coefficients& assignImpl (const NumericType* values);
template <size_t Num>
Coefficients& assign (const NumericType (& values)[Num]) { return assignImpl<Num> (values); }
};
//==============================================================================
template <typename SampleType>
class Filter
{
public:
using NumericType = typename SampleTypeHelpers::ElementType<SampleType>::Type;
using CoefficientsPtr = typename Coefficients<NumericType>::Ptr;
//==============================================================================
Filter();
Filter (CoefficientsPtr coefficientsToUse);
Filter (const Filter&) = default;
Filter (Filter&&) = default;
Filter& operator= (const Filter&) = default;
Filter& operator= (Filter&&) = default;
//==============================================================================
CoefficientsPtr coefficients;
//==============================================================================
void reset() { reset (SampleType {0}); }
void reset (SampleType resetToValue);
//==============================================================================
void prepare (const ProcessSpec&) noexcept;
template <typename ProcessContext>
void process (const ProcessContext& context) noexcept
{
if (context.isBypassed)
processInternal<ProcessContext, true> (context);
else
processInternal<ProcessContext, false> (context);
#if JUCE_DSP_ENABLE_SNAP_TO_ZERO
snapToZero();
#endif
}
SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType sample) noexcept;
void snapToZero() noexcept;
private:
//==============================================================================
void check();
template <typename ProcessContext, bool isBypassed>
void processInternal (const ProcessContext& context) noexcept;
//==============================================================================
HeapBlock<SampleType> memory;
SampleType* state = nullptr;
size_t order = 0;
JUCE_LEAK_DETECTOR (Filter)
};
} // namespace juce::dsp::IIR
juce::Array
Definition juce_Array.h:56
juce::HeapBlock
Definition juce_HeapBlock.h:87
juce::ReferenceCountedObjectPtr
Definition juce_ReferenceCountedObject.h:247
juce::dsp::IIR::Filter
Definition juce_dsp/processors/juce_IIRFilter.h:297
juce::dsp::IIR::Filter::prepare
void prepare(const ProcessSpec &) noexcept
Definition juce_IIRFilter_Impl.h:83
juce::dsp::IIR::Filter::Filter
Filter()
Definition juce_IIRFilter_Impl.h:54
juce::dsp::IIR::Filter::coefficients
CoefficientsPtr coefficients
Definition juce_dsp/processors/juce_IIRFilter.h:331
juce::dsp::IIR::Filter::snapToZero
void snapToZero() noexcept
Definition juce_IIRFilter_Impl.h:223
juce::dsp::IIR::Filter::reset
void reset()
Definition juce_dsp/processors/juce_IIRFilter.h:339
juce::dsp::IIR::Filter::processSample
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType sample) noexcept
Definition juce_IIRFilter_Impl.h:207
juce::dsp::IIR::Filter::CoefficientsPtr
typename Coefficients< NumericType >::Ptr CoefficientsPtr
Definition juce_dsp/processors/juce_IIRFilter.h:305
juce::dsp::IIR::Filter::NumericType
typename SampleTypeHelpers::ElementType< SampleType >::Type NumericType
Definition juce_dsp/processors/juce_IIRFilter.h:302
juce::dsp::IIR::Filter::process
void process(const ProcessContext &context) noexcept
Definition juce_dsp/processors/juce_IIRFilter.h:352
juce::dsp::IIR
Definition juce_dsp/processors/juce_IIRFilter.cpp:27
juce::dsp::IIR::ArrayCoefficients
Definition juce_dsp/processors/juce_IIRFilter.h:37
juce::dsp::IIR::ArrayCoefficients::makeHighPass
static std::array< NumericType, 6 > makeHighPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:94
juce::dsp::IIR::ArrayCoefficients::makePeakFilter
static std::array< NumericType, 6 > makePeakFilter(double sampleRate, NumericType centreFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:251
juce::dsp::IIR::ArrayCoefficients::makeBandPass
static std::array< NumericType, 6 > makeBandPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:120
juce::dsp::IIR::ArrayCoefficients::makeFirstOrderHighPass
static std::array< NumericType, 4 > makeFirstOrderHighPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:44
juce::dsp::IIR::ArrayCoefficients::makeLowPass
static std::array< NumericType, 6 > makeLowPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:68
juce::dsp::IIR::ArrayCoefficients::makeLowShelf
static std::array< NumericType, 6 > makeLowShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:199
juce::dsp::IIR::ArrayCoefficients::makeFirstOrderLowPass
static std::array< NumericType, 4 > makeFirstOrderLowPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:32
juce::dsp::IIR::ArrayCoefficients::makeFirstOrderAllPass
static std::array< NumericType, 4 > makeFirstOrderAllPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:56
juce::dsp::IIR::ArrayCoefficients::makeAllPass
static std::array< NumericType, 6 > makeAllPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:173
juce::dsp::IIR::ArrayCoefficients::makeHighShelf
static std::array< NumericType, 6 > makeHighShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:225
juce::dsp::IIR::ArrayCoefficients::makeNotch
static std::array< NumericType, 6 > makeNotch(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:147
juce::dsp::IIR::Coefficients::makeLowShelf
static Ptr makeLowShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:403
juce::dsp::IIR::Coefficients::getRawCoefficients
const NumericType * getRawCoefficients() const noexcept
Definition juce_dsp/processors/juce_IIRFilter.h:263
juce::dsp::IIR::Coefficients::Coefficients
Coefficients(const std::array< NumericType, Num > &values)
Definition juce_dsp/processors/juce_IIRFilter.h:148
juce::dsp::IIR::Coefficients::makeFirstOrderHighPass
static Ptr makeFirstOrderHighPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:314
juce::dsp::IIR::Coefficients::makeHighPass
static Ptr makeHighPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:343
juce::dsp::IIR::Coefficients::Ptr
ReferenceCountedObjectPtr< Coefficients > Ptr
Definition juce_dsp/processors/juce_IIRFilter.h:157
juce::dsp::IIR::Coefficients::makeHighShelf
static Ptr makeHighShelf(double sampleRate, NumericType cutOffFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:412
juce::dsp::IIR::Coefficients::getMagnitudeForFrequencyArray
void getMagnitudeForFrequencyArray(const double *frequencies, double *magnitudes, size_t numSamples, double sampleRate) const noexcept
Definition juce_dsp/processors/juce_IIRFilter.cpp:466
juce::dsp::IIR::Coefficients::makeLowPass
static Ptr makeLowPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:328
juce::dsp::IIR::Coefficients::getMagnitudeForFrequency
double getMagnitudeForFrequency(double frequency, double sampleRate) const noexcept
Definition juce_dsp/processors/juce_IIRFilter.cpp:436
juce::dsp::IIR::Coefficients::makePeakFilter
static Ptr makePeakFilter(double sampleRate, NumericType centreFrequency, NumericType Q, NumericType gainFactor)
Definition juce_dsp/processors/juce_IIRFilter.cpp:421
juce::dsp::IIR::Coefficients::getPhaseForFrequencyArray
void getPhaseForFrequencyArray(double *frequencies, double *phases, size_t numSamples, double sampleRate) const noexcept
Definition juce_dsp/processors/juce_IIRFilter.cpp:532
juce::dsp::IIR::Coefficients::makeAllPass
static Ptr makeAllPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:388
juce::dsp::IIR::Coefficients::getRawCoefficients
NumericType * getRawCoefficients() noexcept
Definition juce_dsp/processors/juce_IIRFilter.h:260
juce::dsp::IIR::Coefficients::makeBandPass
static Ptr makeBandPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:358
juce::dsp::IIR::Coefficients::Coefficients
Coefficients()
Definition juce_dsp/processors/juce_IIRFilter.cpp:276
juce::dsp::IIR::Coefficients::getPhaseForFrequency
double getPhaseForFrequency(double frequency, double sampleRate) const noexcept
Definition juce_dsp/processors/juce_IIRFilter.cpp:502
juce::dsp::IIR::Coefficients::makeNotch
static Ptr makeNotch(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:373
juce::dsp::IIR::Coefficients::getFilterOrder
size_t getFilterOrder() const noexcept
Definition juce_dsp/processors/juce_IIRFilter.cpp:430
juce::dsp::IIR::Coefficients::makeFirstOrderLowPass
static Ptr makeFirstOrderLowPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:307
juce::dsp::IIR::Coefficients::makeFirstOrderAllPass
static Ptr makeFirstOrderAllPass(double sampleRate, NumericType frequency)
Definition juce_dsp/processors/juce_IIRFilter.cpp:321
juce::dsp::IIR::Coefficients::coefficients
Array< NumericType > coefficients
Definition juce_dsp/processors/juce_IIRFilter.h:269
juce::dsp::ProcessSpec
Definition juce_ProcessContext.h:36
juce::dsp::ProcessorState
Definition juce_ProcessContext.h:68