39 namespace StateVariableFilter
41 template <
typename NumericType>
57 template <
typename SampleType>
65 using NumericType =
typename SampleTypeHelpers::ElementType<SampleType>::Type;
87 void reset() noexcept { s1 = s2 = SampleType {0}; }
93 void snapToZero() noexcept { util::snapToZero (s1); util::snapToZero (s2); }
101 template <
typename ProcessContext>
102 void process (
const ProcessContext& context) noexcept
104 static_assert (std::is_same<typename ProcessContext::SampleType, SampleType>::value,
105 "The sample-type of the filter must match the sample-type supplied to this process callback");
107 if (context.isBypassed)
108 processInternal<true, ProcessContext> (context);
110 processInternal<false, ProcessContext> (context);
117 switch (parameters->type)
122 default: jassertfalse;
125 return SampleType{0};
130 template <bool isBypassed, typename Parameters<NumericType>::Type type>
133 y[2] = (sample - s1 * state.R2 - s1 * state.g - s2) * state.h;
135 y[1] = y[2] * state.g + s1;
136 s1 = y[2] * state.g + y[1];
138 y[0] = y[1] * state.g + s2;
139 s2 = y[1] * state.g + y[0];
141 return isBypassed ? sample : y[static_cast<size_t> (type)];
144 template <bool isBypassed, typename Parameters<NumericType>::Type type>
145 void processBlock (
const SampleType* input, SampleType* output,
size_t n) noexcept
149 for (
size_t i = 0 ; i < n; ++i)
150 output[i] = processLoop<isBypassed, type> (input[i], state);
156 template <
bool isBypassed,
typename ProcessContext>
157 void processInternal (
const ProcessContext& context) noexcept
159 auto&& inputBlock = context.getInputBlock();
160 auto&& outputBlock = context.getOutputBlock();
164 jassert (inputBlock.getNumChannels() == 1);
165 jassert (outputBlock.getNumChannels() == 1);
167 auto n = inputBlock.getNumSamples();
168 auto* src = inputBlock .getChannelPointer (0);
169 auto* dst = outputBlock.getChannelPointer (0);
171 switch (parameters->type)
176 default: jassertfalse;
181 std::array<SampleType, 3> y;
185 JUCE_LEAK_DETECTOR (
Filter)
194 template <
typename NumericType>
207 Type type = Type::lowPass;
218 jassert (sampleRate > 0);
224 h =
static_cast<NumericType> (1.0 / (1.0 + R2 * g + g * g));
236 Parameters& operator= (
const Parameters& o) noexcept { g = o.g; R2 = o.R2; h = o.h;
return *
this; }
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType sample) noexcept
Processes a single sample, without any locking or checking.
An IIR filter that can perform low, band and high-pass filtering on an audio signal, with 12 dB of attenuation / octave, using a TPT structure, designed for fast modulation (see Vadim Zavalishin's documentation about TPT structures for more information).
void reset() noexcept
Resets the filter's processing pipeline.
Structure used for the state variable filter parameters.
Filter()
Creates a filter with default parameters.
typename Parameters< NumericType >::Ptr ParametersPtr
A typedef for a ref-counted pointer to the coefficients object.
void snapToZero() noexcept
Ensure that the state variables are rounded to zero if the state variables are denormals.
void prepare(const ProcessSpec &) noexcept
Initialization of the filter.
This is a handy base class for the state of a processor (such as parameter values) which is typically...
This structure is passed into a DSP algorithm's prepare() method, and contains information about vari...
A smart-pointer class which points to a reference-counted object.
typename SampleTypeHelpers::ElementType< SampleType >::Type NumericType
The NumericType is the underlying primitive type used by the SampleType (which could be either a prim...
Parameters< NumericType >::Ptr parameters
The parameters of the state variable filter.
Commonly used mathematical constants.
void setCutOffFrequency(double sampleRate, NumericType frequency, NumericType resonance=static_cast< NumericType >(1.0/MathConstants< double >::sqrt2)) noexcept
Sets the cutoff frequency and resonance of the IIR filter.