39namespace 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);
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);
173 case Parameters<NumericType>::Type::lowPass: processBlock<isBypassed, Parameters<NumericType>::Type::lowPass> (src, dst, n);
break;
174 case Parameters<NumericType>::Type::bandPass: processBlock<isBypassed, Parameters<NumericType>::Type::bandPass> (src, dst, n);
break;
175 case Parameters<NumericType>::Type::highPass: processBlock<isBypassed, Parameters<NumericType>::Type::highPass> (src, dst, n);
break;
176 default: jassertfalse;
181 std::array<SampleType, 3> y;
185 JUCE_LEAK_DETECTOR (
Filter)
194 template <
typename NumericType>
218 jassert (sampleRate > 0);
219 jassert (resonance > NumericType (0));
220 jassert (frequency > NumericType (0) && frequency <= NumericType (sampleRate * 0.5));
223 R2 =
static_cast<NumericType
> (1.0 / resonance);
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; }
241 NumericType h =
static_cast<NumericType
> (1.0 / (1.0 + R2 * g + g * g));
A smart-pointer class which points to a reference-counted object.
An IIR filter that can perform low, band and high-pass filtering on an audio signal,...
Filter()
Creates a filter with default parameters.
Parameters< NumericType >::Ptr parameters
The parameters of the state variable filter.
typename Parameters< NumericType >::Ptr ParametersPtr
A typedef for a ref-counted pointer to the coefficients object.
void prepare(const ProcessSpec &) noexcept
Initialization of the filter.
void reset() noexcept
Resets the filter's processing pipeline.
SampleType JUCE_VECTOR_CALLTYPE processSample(SampleType sample) noexcept
Processes a single sample, without any locking or checking.
Filter(const Filter &)=default
Creates a copy of another filter.
void snapToZero() noexcept
Ensure that the state variables are rounded to zero if the state variables are denormals.
typename SampleTypeHelpers::ElementType< SampleType >::Type NumericType
The NumericType is the underlying primitive type used by the SampleType (which could be either a prim...
Filter(Filter &&)=default
Move constructor.
This structure is passed into a DSP algorithm's prepare() method, and contains information about vari...
Commonly used mathematical constants.
This is a handy base class for the state of a processor (such as parameter values) which is typically...
Structure used for the state variable filter parameters.
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.
Type type
The type of the IIR filter.