62#include "AudioBuffer.h"
63#include "DenormalGuard.h"
64#include "detail/TptSvf.h"
135 if (!(spec.sampleRate > 0.0))
return;
137 updateCoefficients();
153 const int nCh = std::min(buffer.getNumChannels(),
kMaxChannels);
154 const int nS = buffer.getNumSamples();
163 case Mode::LowPass: processBlockMode<Mode::LowPass>(buffer, nCh, nS);
break;
164 case Mode::HighPass: processBlockMode<Mode::HighPass>(buffer, nCh, nS);
break;
165 case Mode::BandPass: processBlockMode<Mode::BandPass>(buffer, nCh, nS);
break;
166 case Mode::Notch: processBlockMode<Mode::Notch>(buffer, nCh, nS);
break;
167 case Mode::AllPass: processBlockMode<Mode::AllPass>(buffer, nCh, nS);
break;
168 case Mode::Bell: processBlockMode<Mode::Bell>(buffer, nCh, nS);
break;
169 case Mode::LowShelf: processBlockMode<Mode::LowShelf>(buffer, nCh, nS);
break;
170 case Mode::HighShelf: processBlockMode<Mode::HighShelf>(buffer, nCh, nS);
break;
192 if (channel < 0 || channel >=
kMaxChannels)
return input;
193 return processOne(input, channel);
210 if (channel < 0 || channel >=
kMaxChannels)
return { input, T(0), T(0) };
211 return processCore(input, channel);
239 if (!std::isfinite(hz))
return;
241 updateCoefficients();
253 if (!std::isfinite(resonance))
return;
254 resonance_ = std::clamp(resonance, T(0), T(1));
257 R_ = T(1) / (T(2) * Q);
259 updateDerivedCoeffs();
273 if (!std::isfinite(q))
return;
274 q = std::max(q, T(0.01));
275 R_ = T(1) / (T(2) * q);
276 resonance_ = std::clamp((q - T(0.5)) / T(49.5), T(0), T(1));
278 updateDerivedCoeffs();
285 updateDerivedCoeffs();
294 if (!std::isfinite(dB))
return;
296 A_ = std::pow(T(10), std::abs(dB) / T(40));
298 updateDerivedCoeffs();
305 [[nodiscard]] T
getQ() const noexcept {
return T(1) / (T(2) *
R_); }
314 std::array<ChannelState, kMaxChannels>
state_ {};
338 void updateCoefficients() noexcept
344 g_ =
static_cast<T
>(std::tan(pi<double> *
static_cast<double>(
cutoff_)
348 updateDerivedCoeffs();
352 void updateBellR() noexcept
375 void updateDerivedCoeffs() noexcept
382 const T sqrtAs = std::sqrt((
gainDb_ >= T(0)) ?
A_ : T(1) /
A_);
387 a1_ = T(1) / (T(1) + T(2) *
effR_ * gEff + gEff * gEff);
396 void processBlockMode(AudioBufferView<T> buffer,
int nCh,
int nS)
noexcept
399 const T twoEffR = T(2) *
effR_;
400 const T twoR = T(2) *
R_;
402 const T shelf0 = As * As, shelf1 = As * twoR;
403 const T k = T(2) *
Rbell_;
404 const T bell = (
gainDb_ >= T(0)) ? (
A_ *
A_ - T(1)) * k : (T(1) / (
A_ *
A_) - T(1)) * k;
405 for (
int ch = 0; ch < nCh; ++ch)
407 T* data = buffer.getChannel(ch);
409 for (
int i = 0; i < nS; ++i)
413 const T lp = o.lowpass, bp = o.bandpass;
414 const T hp = x - twoEffR * bp - lp;
420 else if constexpr (M ==
Mode::AllPass) y = lp + hp - twoR * bp;
421 else if constexpr (M ==
Mode::Bell) y = x + bell * bp;
422 else if constexpr (M ==
Mode::LowShelf) y = shelf0 * lp + shelf1 * bp + hp;
423 else y = lp + shelf1 * bp + shelf0 * hp;
426 if (!std::isfinite(st.ic1eq + st.ic2eq)) st =
ChannelState {};
432 [[nodiscard]] T processOne(T input,
int channel)
noexcept
434 const MultiOutput m = processCore(input, channel);
435 return selectOutput(input, m.lowpass, m.highpass, m.bandpass);
451 [[nodiscard]] MultiOutput processCore(T input,
int channel)
noexcept
454 return { output.lowpass, input - T(2) *
effR_ * output.bandpass - output.lowpass,
458 [[nodiscard]] T selectOutput(T input, T lp, T hp, T bp)
const noexcept
478 const T k = T(2) *
Rbell_;
480 ? input + (
A_ *
A_ - T(1)) * k * bp
481 : input + (T(1) / (
A_ *
A_) - T(1)) * k * bp;
492 const T twoR = T(2) *
R_;
493 return As * As * lp + As * twoR * bp + hp;
500 const T twoR = T(2) *
R_;
501 return lp + As * twoR * bp + As * As * hp;
Non-owning view over audio channel data.
RAII scope guard to disable denormalised (subnormal) floating-point numbers.
TPT State Variable Filter with simultaneous multi-output.
void setResonance(T resonance) noexcept
Sets the resonance amount.
void setCutoff(T hz) noexcept
Sets the cutoff/center frequency.
std::array< ChannelState, kMaxChannels > state_
void setQ(T q) noexcept
Sets the Q factor directly.
Mode getMode() const noexcept
T getGain() const noexcept
void reset() noexcept
Resets internal state.
~StateVariableFilter()=default
void setGain(T dB) noexcept
Sets gain for Bell/Shelf modes (in dB).
static constexpr int kMaxChannels
void setMode(Mode mode) noexcept
Sets the output mode.
MultiOutput processMultiOutput(T input, int channel) noexcept
Processes a single sample returning LP, HP, BP simultaneously.
void processBlock(AudioBufferView< T > buffer) noexcept
Processes an audio buffer in-place using the selected Mode.
detail::TptSvfState< T > ChannelState
Mode
Filter output mode for processBlock/processSample.
@ LowShelf
Low shelf (boost/cut below frequency).
@ BandPass
Bandpass (constant skirt gain).
@ AllPass
Allpass (phase shift, unity gain).
@ LowPass
2nd-order lowpass (12 dB/oct).
@ Bell
Parametric bell (boost/cut at frequency).
@ Notch
Band-reject (notch).
@ HighShelf
High shelf (boost/cut above frequency).
@ HighPass
2nd-order highpass (12 dB/oct).
T processSample(T input, int channel) noexcept
Processes a single sample (selected Mode output).
void prepare(const AudioSpec &spec) noexcept
Prepares the filter.
T getCutoff() const noexcept
T getResonance() const noexcept
TptSvfOutput< T > tptSvfStep(T input, TptSvfState< T > &state, T a1, T a2, T a3) noexcept
Advances the expanded Simper TPT recurrence by one sample.
Main namespace for the DSPark framework.
Describes the audio environment for a DSP processor.
double sampleRate
Sample rate in Hz.
Result struct for simultaneous multi-output processing.
Equivalent-current memories of the two trapezoidal integrators.