49#include "../Core/DspMath.h"
50#include "../Core/Biquad.h"
51#include "../Core/SimdOps.h"
52#include "../Core/AudioSpec.h"
53#include "../Core/AudioBuffer.h"
54#include "../Core/StateBlob.h"
55#include "../Core/detail/DcBlock.h"
113 void prepare(
double sampleRate,
int numChannels = 2,
double cutoffHz = -1.0)
115 if (!(sampleRate > 0.0))
return;
150 order_.store(std::clamp(order, 1, 10), std::memory_order_relaxed);
156 return order_.load(std::memory_order_relaxed);
172 if (!std::isfinite(hz))
return;
173 cutoffHz_.store(std::max(hz, T(1)), std::memory_order_relaxed);
179 return cutoffHz_.load(std::memory_order_relaxed);
198 const int nCh = std::min(buffer.getNumChannels(),
numChannels_);
199 const int nS = buffer.getNumSamples();
207 for (; ch + 1 < nCh; ch += 2)
208 runPair<simd::kVecDoubleNarrow>(buffer.getChannel(ch), buffer.getChannel(ch + 1), nS, ch, ch + 1, currentOrder);
210 for (; ch < nCh; ++ch)
212 if (currentOrder <= 1)
231 if (data ==
nullptr || numSamples <= 0)
return;
235 if (currentOrder <= 1)
264 if (channel < 0 || channel >=
kMaxChannels)
return input;
266 const auto ch =
static_cast<size_t>(channel);
267 const double x = std::isfinite(input) ?
static_cast<double>(input) : 0.0;
274 for (
int s = 0; s < numStages; ++s)
276 auto& sec =
sections_[
static_cast<size_t>(s)];
277 v =
stepSection(sec.b0, sec.a1, sec.a2, sec.state[ch], v);
279 return static_cast<T
>(v);
294 [[nodiscard]] std::vector<uint8_t>
getState()
const
297 w.
write(
"order", order_.load(std::memory_order_relaxed));
298 w.
write(
"cutoff",
static_cast<float>(cutoffHz_.load(std::memory_order_relaxed)));
307 setOrder(r.
read(
"order", 1));
308 setCutoff(
static_cast<T
>(r.
read(
"cutoff", 5.0f)));
320 const T cutoff = cutoffHz_.load(std::memory_order_relaxed);
321 const int order = order_.load(std::memory_order_relaxed);
322 if (cutoff != lastCutoff_ || order != lastOrder_)
324 forceUpdateCoefficients(cutoff);
330 const T cutoff = explicitCutoff > T(0) ? explicitCutoff : cutoffHz_.load(std::memory_order_relaxed);
331 const double fc =
static_cast<double>(cutoff);
335 const int order = order_.load(std::memory_order_relaxed);
338 onePoleR_ = std::exp(-std::numbers::pi * 2.0 * fc / sampleRate_);
341 static constexpr float qTable[6][kMaxBiquadStages] = {
344 { 0.5412f, 1.3066f },
345 { 0.5177f, 0.7071f, 1.9319f },
346 { 0.5098f, 0.6013f, 0.8999f, 2.5628f },
347 { 0.5062f, 0.5612f, 0.7071f, 1.1013f, 3.1962f }
350 const int tableIdx = std::clamp(order / 2, 1, 5);
351 for (
int s = 0; s < tableIdx; ++s)
359 sampleRate_, fc,
static_cast<double>(qTable[tableIdx][s]));
360 assert(c.b1 == -2.0 * c.b0 && c.b2 == c.b0
361 &&
"RBJ high-pass numerator is no longer b0 * (1, -2, 1)");
363 auto& sec = sections_[
static_cast<size_t>(s)];
369 lastCutoff_ = cutoff;
376 struct SectionState {
double x1 = 0.0, x2 = 0.0, y1 = 0.0, y2 = 0.0; };
381 double b0 = 0.0, a1 = 0.0, a2 = 0.0;
382 std::array<SectionState, kMaxChannels> state {};
396 const double d = (x - z.x1) - (z.x1 - z.x2);
397 const double y = b0 * d - a1 * z.y1 - a2 * z.y2;
398 z.x2 = z.x1; z.x1 = x;
399 z.y2 = z.y1; z.y1 = y;
414 static constexpr double kDenormalFloor = 1e-30;
418 if (std::abs(z.y1) + std::abs(z.x1) < kDenormalFloor)
424 if (std::abs(z.y1) + std::abs(z.y2) + std::abs(z.x1) + std::abs(z.x2) < kDenormalFloor)
428 void runOnePole(T* data,
int numSamples,
int channel)
noexcept
430 const double r = onePoleR_;
431 OnePoleState z = onePole_[
static_cast<size_t>(channel)];
434 for (
int i = 0; i < numSamples; ++i)
435 data[i] =
static_cast<T
>(stepOnePole(r, z, std::isfinite(data[i]) ?
static_cast<double>(data[i]) : 0.0));
437 onePole_[
static_cast<size_t>(channel)] = z;
440 void runCascade(T* data,
int numSamples,
int channel,
int numStages)
noexcept
446 double b0[kMaxBiquadStages] {}, a1[kMaxBiquadStages] {}, a2[kMaxBiquadStages] {};
448 const auto ch =
static_cast<size_t>(channel);
449 for (
int s = 0; s < numStages; ++s)
451 const auto& sec = sections_[
static_cast<size_t>(s)];
452 b0[s] = sec.b0; a1[s] = sec.a1; a2[s] = sec.a2;
453 z[s] = sec.state[ch];
456 for (
int i = 0; i < numSamples; ++i)
458 double v = std::isfinite(data[i]) ?
static_cast<double>(data[i]) : 0.0;
459 for (
int s = 0; s < numStages; ++s)
460 v = stepSection(b0[s], a1[s], a2[s], z[s], v);
461 data[i] =
static_cast<T
>(v);
464 for (
int s = 0; s < numStages; ++s)
466 flushDenormals(z[s]);
467 sections_[
static_cast<size_t>(s)].state[ch] = z[s];
473 static constexpr bool kPairChannels = simd::kVecDoubleNarrow == 2 &&
sizeof(
void*) == 8;
477 void runPair(T* left, T* right,
int numSamples,
int chLeft,
int chRight,
int order)
noexcept
481 runOnePolePair<W>(left, right, numSamples, chLeft, chRight);
484 switch (std::clamp(order / 2, 1, kMaxBiquadStages))
486 case 1: runCascadePair<W, 1>(left, right, numSamples, chLeft, chRight);
break;
487 case 2: runCascadePair<W, 2>(left, right, numSamples, chLeft, chRight);
break;
488 case 3: runCascadePair<W, 3>(left, right, numSamples, chLeft, chRight);
break;
489 case 4: runCascadePair<W, 4>(left, right, numSamples, chLeft, chRight);
break;
490 default: runCascadePair<W, 5>(left, right, numSamples, chLeft, chRight);
break;
494 static double finiteOrZero(T x)
noexcept {
return std::isfinite(x) ?
static_cast<double>(x) : 0.0; }
497 void runOnePolePair(T* left, T* right,
int numSamples,
int chLeft,
int chRight)
noexcept
499 using O = simd::Vec<double, W>;
500 auto& zl = onePole_[
static_cast<size_t>(chLeft)];
501 auto& zr = onePole_[
static_cast<size_t>(chRight)];
502 const auto r = O::set1(onePoleR_);
503 auto x1 = O::set2(zl.x1, zr.x1);
504 auto y1 = O::set2(zl.y1, zr.y1);
505 for (
int i = 0; i < numSamples; ++i)
507 const auto x = O::set2(finiteOrZero(left[i]), finiteOrZero(right[i]));
508 y1 = O::add(O::sub(x, x1), O::mul(r, y1));
510 left[i] =
static_cast<T
>(O::lane0(y1));
511 right[i] =
static_cast<T
>(O::lane1(y1));
513 zl = { O::lane0(x1), O::lane0(y1) };
514 zr = { O::lane1(x1), O::lane1(y1) };
519 template <
int W,
int S>
520 void runCascadePair(T* left, T* right,
int numSamples,
int chLeft,
int chRight)
noexcept
522 using O = simd::Vec<double, W>;
523 using V =
typename O::V;
524 const auto cl =
static_cast<size_t>(chLeft), cr =
static_cast<size_t>(chRight);
525 V b0[S], a1[S], a2[S], x1[S], x2[S], y1[S], y2[S];
526 for (
int s = 0; s < S; ++s)
528 const auto& sec = sections_[
static_cast<size_t>(s)];
529 b0[s] = O::set1(sec.b0); a1[s] = O::set1(sec.a1); a2[s] = O::set1(sec.a2);
530 x1[s] = O::set2(sec.state[cl].x1, sec.state[cr].x1);
531 x2[s] = O::set2(sec.state[cl].x2, sec.state[cr].x2);
532 y1[s] = O::set2(sec.state[cl].y1, sec.state[cr].y1);
533 y2[s] = O::set2(sec.state[cl].y2, sec.state[cr].y2);
535 for (
int i = 0; i < numSamples; ++i)
537 V v = O::set2(finiteOrZero(left[i]), finiteOrZero(right[i]));
538 for (
int s = 0; s < S; ++s)
541 const V d = O::sub(O::sub(v, x1[s]), O::sub(x1[s], x2[s]));
542 const V y = O::sub(O::sub(O::mul(b0[s], d), O::mul(a1[s], y1[s])), O::mul(a2[s], y2[s]));
543 x2[s] = x1[s]; x1[s] = v;
544 y2[s] = y1[s]; y1[s] = y;
547 left[i] =
static_cast<T
>(O::lane0(v));
548 right[i] =
static_cast<T
>(O::lane1(v));
550 for (
int s = 0; s < S; ++s)
552 auto& sec = sections_[
static_cast<size_t>(s)];
553 sec.state[cl] = { O::lane0(x1[s]), O::lane0(x2[s]), O::lane0(y1[s]), O::lane0(y2[s]) };
554 sec.state[cr] = { O::lane1(x1[s]), O::lane1(x2[s]), O::lane1(y1[s]), O::lane1(y2[s]) };
555 flushDenormals(sec.state[cl]);
556 flushDenormals(sec.state[cr]);
560 double sampleRate_ = 48000.0;
561 int numChannels_ = 2;
562 double onePoleR_ = 0.0;
564 std::atomic<int> order_ { 1 };
565 std::atomic<T> cutoffHz_ { T(5) };
566 T lastCutoff_ = T(-1);
570 std::array<OnePoleState, kMaxChannels> onePole_ {};
571 std::array<Section, kMaxBiquadStages> sections_ {};
Non-owning view over audio channel data.
DC blocking filter with configurable Butterworth order (1-10).
void prepare(double sampleRate, int numChannels=2, double cutoffHz=-1.0)
Prepares the DC blocker, resetting internal states and precalculating coefficients.
void setOrder(int order) noexcept
Sets the filter order (1-10). Thread-safe.
std::vector< uint8_t > getState() const
Serializes the parameter state (setup/UI threads; allocates).
std::array< OnePoleState, kMaxChannels > onePole_
std::atomic< T > cutoffHz_
void runOnePole(T *data, int numSamples, int channel) noexcept
static constexpr int kMaxBiquadStages
DCBlocker() noexcept
Builds a filter coherent with the documented defaults (5 Hz @ 48 kHz).
void runCascadePair(T *left, T *right, int numSamples, int chLeft, int chRight) noexcept
T processSample(int channel, T input) noexcept
Processes a single sample for a given channel.
void forceUpdateCoefficients(T explicitCutoff=T(0)) noexcept
void setCutoff(T hz) noexcept
Requests a cutoff frequency update.
static constexpr bool kPairChannels
void reset() noexcept
Clears the internal history states to zero.
std::atomic< int > order_
static void flushDenormals(OnePoleState &z) noexcept
void runOnePolePair(T *left, T *right, int numSamples, int chLeft, int chRight) noexcept
int getOrder() const noexcept
Returns the current filter order.
double onePoleR_
Derived from the documented defaults in the constructor.
void runCascade(T *data, int numSamples, int channel, int numStages) noexcept
void updateCoefficientsIfNeeded() noexcept
static void flushDenormals(SectionState &z) noexcept
void processBlock(AudioBufferView< T > buffer) noexcept
Processes an AudioBufferView in-place.
void processBlock(int channel, T *data, int numSamples) noexcept
Processes a block of samples for one channel in-place.
bool setState(const uint8_t *data, size_t size)
Restores parameters from a blob (tolerant; rejects foreign ids).
static double stepSection(double b0, double a1, double a2, SectionState &z, double x) noexcept
Direct Form I with the numerator applied as a second difference.
static double finiteOrZero(T x) noexcept
std::array< Section, kMaxBiquadStages > sections_
void runPair(T *left, T *right, int numSamples, int chLeft, int chRight, int order) noexcept
Two channels through the filter in the lanes of one register (W = 2).
void prepare(const AudioSpec &spec)
Prepares from AudioSpec (unified API); keeps the configured cutoff.
static double stepOnePole(double r, OnePoleState &z, double x) noexcept
y[n] = (x[n] - x[n-1]) + R*y[n-1]; the difference zeroes DC exactly.
static constexpr int kMaxChannels
T getCutoff() const noexcept
Returns the requested cutoff frequency in Hz.
Tolerant reader: missing keys yield defaults, unknown keys are skipped.
float read(const char *key, float defaultValue) const
Reads a float, or defaultValue when the key is absent.
bool isValid() const noexcept
uint32_t processorId() const noexcept
Serializes key/value parameters into a versioned blob.
std::vector< uint8_t > blob() const
Finalizes and returns the blob.
void write(const char *key, float value)
Writes a float parameter.
double dcBlockStep(double pole, DcBlockState &state, double input) noexcept
Applies (1 - z^-1) / (1 - pole * z^-1), with a pole in [0, 1).
Main namespace for the DSPark framework.
constexpr uint32_t stateId(const char(&tag)[5]) noexcept
Builds a FOURCC processor id, e.g. dspark::stateId("COMP").
Describes the audio environment for a DSP processor.
int numChannels
Number of audio channels (e.g., 1 = mono, 2 = stereo).
double sampleRate
Sample rate in Hz.
static BiquadCoeffs makeHighPass(double sampleRate, double freq, double Q=0.7071067811865476) noexcept
High-pass filter.
Second-order section: b0 * (1 - z^-1)^2 / (1 + a1 z^-1 + a2 z^-2).
Previous input and output of the one-pole DC blocker.