Loading...
Searching...
No Matches
juce_FIRFilter.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::FIR
{
template <typename NumericType>
struct Coefficients;
//==============================================================================
template <typename SampleType>
class Filter
{
public:
using NumericType = typename SampleTypeHelpers::ElementType<SampleType>::Type;
using CoefficientsPtr = typename Coefficients<NumericType>::Ptr;
//==============================================================================
Filter() : coefficients (new Coefficients<NumericType>) { reset(); }
Filter (CoefficientsPtr coefficientsToUse) : coefficients (std::move (coefficientsToUse)) { reset(); }
Filter (const Filter&) = default;
Filter (Filter&&) = default;
Filter& operator= (const Filter&) = default;
Filter& operator= (Filter&&) = default;
//==============================================================================
inline void prepare ([[maybe_unused]] const ProcessSpec& spec) noexcept
{
// This class can only process mono signals. Use the ProcessorDuplicator class
// to apply this filter on a multi-channel audio stream.
jassert (spec.numChannels == 1);
reset();
}
void reset()
{
if (coefficients != nullptr)
{
auto newSize = coefficients->getFilterOrder() + 1;
if (newSize != size)
{
memory.malloc (1 + jmax (newSize, size, static_cast<size_t> (128)));
fifo = snapPointerToAlignment (memory.getData(), sizeof (SampleType));
size = newSize;
}
for (size_t i = 0; i < size; ++i)
fifo[i] = SampleType {0};
}
}
//==============================================================================
typename Coefficients<NumericType>::Ptr coefficients;
//==============================================================================
template <typename ProcessContext>
void process (const ProcessContext& context) noexcept
{
static_assert (std::is_same_v<typename ProcessContext::SampleType, SampleType>,
"The sample-type of the FIR filter must match the sample-type supplied to this process callback");
check();
auto&& inputBlock = context.getInputBlock();
auto&& outputBlock = context.getOutputBlock();
// This class can only process mono signals. Use the ProcessorDuplicator class
// to apply this filter on a multi-channel audio stream.
jassert (inputBlock.getNumChannels() == 1);
jassert (outputBlock.getNumChannels() == 1);
auto numSamples = inputBlock.getNumSamples();
auto* src = inputBlock .getChannelPointer (0);
auto* dst = outputBlock.getChannelPointer (0);
auto* fir = coefficients->getRawCoefficients();
size_t p = pos;
if (context.isBypassed)
{
for (size_t i = 0; i < numSamples; ++i)
{
fifo[p] = dst[i] = src[i];
p = (p == 0 ? size - 1 : p - 1);
}
}
else
{
for (size_t i = 0; i < numSamples; ++i)
dst[i] = processSingleSample (src[i], fifo, fir, size, p);
}
pos = p;
}
SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType sample) noexcept
{
check();
return processSingleSample (sample, fifo, coefficients->getRawCoefficients(), size, pos);
}
private:
//==============================================================================
HeapBlock<SampleType> memory;
SampleType* fifo = nullptr;
size_t pos = 0, size = 0;
//==============================================================================
void check()
{
jassert (coefficients != nullptr);
if (size != (coefficients->getFilterOrder() + 1))
reset();
}
static SampleType JUCE_VECTOR_CALLTYPE processSingleSample (SampleType sample, SampleType* buf,
const NumericType* fir, size_t m, size_t& p) noexcept
{
SampleType out (0);
buf[p] = sample;
size_t k;
for (k = 0; k < m - p; ++k)
out += buf[(p + k)] * fir[k];
for (size_t j = 0; j < p; ++j)
out += buf[j] * fir[j + k];
p = (p == 0 ? m - 1 : p - 1);
return out;
}
JUCE_LEAK_DETECTOR (Filter)
};
//==============================================================================
template <typename NumericType>
struct Coefficients : public ProcessorState
{
//==============================================================================
Coefficients() : coefficients ({ NumericType() }) {}
Coefficients (size_t size) { coefficients.resize ((int) size); }
Coefficients (const NumericType* samples, size_t numSamples) : coefficients (samples, (int) numSamples) {}
Coefficients (const Coefficients&) = default;
Coefficients (Coefficients&&) = default;
Coefficients& operator= (const Coefficients&) = default;
Coefficients& operator= (Coefficients&&) = default;
using Ptr = ReferenceCountedObjectPtr<Coefficients>;
//==============================================================================
size_t getFilterOrder() const noexcept { return static_cast<size_t> (coefficients.size()) - 1; }
double getMagnitudeForFrequency (double frequency, double sampleRate) const noexcept;
void getMagnitudeForFrequencyArray (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(); }
//==============================================================================
void normalise() noexcept;
//==============================================================================
Array<NumericType> coefficients;
};
} // namespace juce::dsp::FIR
juce::Array
Definition juce_Array.h:56
juce::HeapBlock
Definition juce_HeapBlock.h:87
juce::ReferenceCountedObjectPtr
Definition juce_ReferenceCountedObject.h:247
juce::dsp::FIR::Filter
Definition juce_FIRFilter.h:50
juce::dsp::FIR::Filter::coefficients
Coefficients< NumericType >::Ptr coefficients
Definition juce_FIRFilter.h:113
juce::dsp::FIR::Filter::CoefficientsPtr
typename Coefficients< NumericType >::Ptr CoefficientsPtr
Definition juce_FIRFilter.h:58
juce::dsp::FIR::Filter::process
void process(const ProcessContext &context) noexcept
Definition juce_FIRFilter.h:118
juce::dsp::FIR::Filter::NumericType
typename SampleTypeHelpers::ElementType< SampleType >::Type NumericType
Definition juce_FIRFilter.h:55
juce::dsp::FIR::Filter::processSample
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType sample) noexcept
Definition juce_FIRFilter.h:160
juce::dsp::FIR::Filter::Filter
Filter(CoefficientsPtr coefficientsToUse)
Definition juce_FIRFilter.h:65
juce::dsp::FIR::Filter::prepare
void prepare(const ProcessSpec &spec) noexcept
Definition juce_FIRFilter.h:74
juce::dsp::FIR::Filter::Filter
Filter()
Definition juce_FIRFilter.h:62
juce::dsp::FIR::Filter::reset
void reset()
Definition juce_FIRFilter.h:87
juce::dsp::FIR
Definition juce_FIRFilter.h:30
juce::dsp::FIR::Coefficients
Definition juce_FIRFilter.h:214
juce::dsp::FIR::Coefficients::normalise
void normalise() noexcept
juce::dsp::FIR::Coefficients::getMagnitudeForFrequencyArray
void getMagnitudeForFrequencyArray(double *frequencies, double *magnitudes, size_t numSamples, double sampleRate) const noexcept
juce::dsp::FIR::Coefficients::getFilterOrder
size_t getFilterOrder() const noexcept
Definition juce_FIRFilter.h:237
juce::dsp::FIR::Coefficients::Coefficients
Coefficients()
Definition juce_FIRFilter.h:217
juce::dsp::FIR::Coefficients::Coefficients
Coefficients(const NumericType *samples, size_t numSamples)
Definition juce_FIRFilter.h:223
juce::dsp::FIR::Coefficients::Coefficients
Coefficients(size_t size)
Definition juce_FIRFilter.h:220
juce::dsp::FIR::Coefficients::getMagnitudeForFrequency
double getMagnitudeForFrequency(double frequency, double sampleRate) const noexcept
juce::dsp::FIR::Coefficients::Ptr
ReferenceCountedObjectPtr< Coefficients > Ptr
Definition juce_FIRFilter.h:233
juce::dsp::FIR::Coefficients::coefficients
Array< NumericType > coefficients
Definition juce_FIRFilter.h:275
juce::dsp::FIR::Coefficients::getPhaseForFrequency
double getPhaseForFrequency(double frequency, double sampleRate) const noexcept
juce::dsp::FIR::Coefficients::getRawCoefficients
NumericType * getRawCoefficients() noexcept
Definition juce_FIRFilter.h:262
juce::dsp::FIR::Coefficients::getRawCoefficients
const NumericType * getRawCoefficients() const noexcept
Definition juce_FIRFilter.h:265
juce::dsp::FIR::Coefficients::getPhaseForFrequencyArray
void getPhaseForFrequencyArray(double *frequencies, double *phases, size_t numSamples, double sampleRate) const noexcept
juce::dsp::ProcessSpec
Definition juce_ProcessContext.h:36
juce::dsp::ProcessorState
Definition juce_ProcessContext.h:68