-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivefilterresponse.cpp
More file actions
707 lines (627 loc) · 27.6 KB
/
Copy pathactivefilterresponse.cpp
File metadata and controls
707 lines (627 loc) · 27.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
* KFilter6
* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright (C) 2002-2026 Martin Erdtmann
*/
#include "activefilterresponse.h"
#include <cmath>
#include <complex>
#include <limits>
namespace
{
constexpr double Pi = 3.141592653589793238462643383279502884;
ActiveFilterResponse neutralResponse()
{
ActiveFilterResponse response;
response.values.fill(std::complex<double>{1.0, 0.0});
return response;
}
ActiveFilterResponse failedResponse(ActiveFilterResponseStatus status,
std::size_t sectionIndex)
{
ActiveFilterResponse response;
const double nan = std::numeric_limits<double>::quiet_NaN();
response.values.fill(std::complex<double>{nan, nan});
response.status = status;
response.hasActiveSections = true;
response.problemSectionIndex = sectionIndex;
return response;
}
bool validButterworthParameters(int order, double cutoffHz)
{
return order >= 1 && order <= 8 && std::isfinite(cutoffHz) && cutoffHz > 0.0;
}
bool validBesselParameters(int order, double cutoffHz)
{
return order >= 1 && order <= 8 && std::isfinite(cutoffHz) && cutoffHz > 0.0;
}
bool validGenericQParameters(double cutoffHz, double q)
{
return std::isfinite(cutoffHz) && cutoffHz > 0.0 &&
std::isfinite(q) && q > 0.0;
}
bool validLinkwitzRileyParameters(int order, double cutoffHz)
{
return order >= 2 && order <= 8 && (order % 2) == 0 &&
std::isfinite(cutoffHz) && cutoffHz > 0.0;
}
bool validBandPassParameters(int order, double lowerFrequencyHz, double upperFrequencyHz)
{
return order >= 1 && order <= 8 &&
std::isfinite(lowerFrequencyHz) && lowerFrequencyHz > 0.0 &&
std::isfinite(upperFrequencyHz) && upperFrequencyHz > lowerFrequencyHz;
}
bool validNotchParameters(double centerFrequencyHz, double q)
{
return std::isfinite(centerFrequencyHz) && centerFrequencyHz > 0.0 &&
std::isfinite(q) && q > 0.0;
}
bool validPeakingEqParameters(double centerFrequencyHz, double q, double gainDb)
{
if (!std::isfinite(centerFrequencyHz) || centerFrequencyHz <= 0.0 ||
!std::isfinite(q) || q <= 0.0 || !std::isfinite(gainDb)) {
return false;
}
const double amplitude = std::pow(10.0, gainDb / 40.0);
return std::isfinite(amplitude) && amplitude > 0.0 &&
std::isfinite(1.0 / amplitude);
}
bool validShelfParameters(double transitionFrequencyHz, double q, double gainDb)
{
if (!std::isfinite(transitionFrequencyHz) || transitionFrequencyHz <= 0.0 ||
!std::isfinite(q) || q <= 0.0 || !std::isfinite(gainDb)) {
return false;
}
const double amplitude = std::pow(10.0, gainDb / 40.0);
return std::isfinite(amplitude) && amplitude > 0.0 &&
std::isfinite(std::sqrt(amplitude)) &&
std::isfinite(1.0 / amplitude) &&
std::isfinite(1.0 / std::sqrt(amplitude));
}
std::complex<double> peakingEqTransfer(double frequencyHz,
double centerFrequencyHz,
double q,
double gainDb)
{
// Analog peaking-EQ prototype (the sample-rate-independent counterpart of
// the standard peaking biquad). With A = 10^(gainDb/40):
// H(s) = (s^2 + (A/Q)s + 1) / (s^2 + s/(A Q) + 1).
// Therefore H(j) = A^2 = 10^(gainDb/20) exactly at f = f0, while
// gainDb == 0 makes numerator and denominator identical at every frequency.
const double amplitude = std::pow(10.0, gainDb / 40.0);
const double ratio = frequencyHz / centerFrequencyHz;
const std::complex<double> normalizedS{0.0, ratio};
const std::complex<double> sSquared = normalizedS * normalizedS;
const std::complex<double> numerator =
sSquared + (amplitude / q) * normalizedS + 1.0;
const std::complex<double> denominator =
sSquared + normalizedS / (amplitude * q) + 1.0;
return numerator / denominator;
}
std::complex<double> shelfTransfer(double frequencyHz,
double transitionFrequencyHz,
double q,
double gainDb,
bool highShelf)
{
// Symmetric normalized second-order analog shelving prototype.
// A = 10^(gainDb/40), s = j*f/f0. For Low Shelf:
// H_LS(s) = (s^2 + sqrt(A)/Q*s + A) /
// (s^2 + 1/(sqrt(A)Q)*s + 1/A)
// High Shelf is the frequency-inverted counterpart H_LS(1/s).
// Consequently the affected plateau is exactly 10^(gainDb/20), the
// opposite plateau is unity, and |H(j*f0)| = A (half the gain in dB).
// gainDb == 0 makes numerator and denominator identical for every f.
const double amplitude = std::pow(10.0, gainDb / 40.0);
const double sqrtAmplitude = std::sqrt(amplitude);
const double ratio = frequencyHz / transitionFrequencyHz;
const std::complex<double> normalizedS{0.0, ratio};
const std::complex<double> sSquared = normalizedS * normalizedS;
if (highShelf) {
const std::complex<double> numerator =
amplitude * sSquared + (sqrtAmplitude / q) * normalizedS + 1.0;
const std::complex<double> denominator =
sSquared / amplitude + normalizedS / (sqrtAmplitude * q) + 1.0;
return numerator / denominator;
}
const std::complex<double> numerator =
sSquared + (sqrtAmplitude / q) * normalizedS + amplitude;
const std::complex<double> denominator =
sSquared + normalizedS / (sqrtAmplitude * q) + 1.0 / amplitude;
return numerator / denominator;
}
bool validAllPassParameters(int order, double frequencyHz, double q)
{
if ((order != 1 && order != 2) ||
!std::isfinite(frequencyHz) || frequencyHz <= 0.0) {
return false;
}
// Q is part of the second-order denominator only. Keep it deliberately
// irrelevant for AP1, just as unused crossover Q metadata is ignored.
return order == 1 || (std::isfinite(q) && q > 0.0);
}
bool validGainParameters(double gainDb)
{
if (!std::isfinite(gainDb)) {
return false;
}
const double linearGain = std::pow(10.0, gainDb / 20.0);
return std::isfinite(linearGain) && linearGain > 0.0;
}
bool validDelayParameters(double delayMs)
{
return std::isfinite(delayMs) && delayMs >= 0.0;
}
std::complex<double> allPassTransfer(int order,
double frequencyHz,
double centerFrequencyHz,
double q)
{
// Patch 188 All-pass convention, using normalized s = j*f/f0:
// AP1: H(s) = (s - 1) / (s + 1)
// AP2: H(s) = (s^2 - s/Q + 1) / (s^2 + s/Q + 1)
// Both are exact unity-magnitude transfer functions for real f.
const double ratio = frequencyHz / centerFrequencyHz;
const std::complex<double> s{0.0, ratio};
if (order == 1) {
return (s - 1.0) / (s + 1.0);
}
const std::complex<double> sSquared = s * s;
return (sSquared - s / q + 1.0) / (sSquared + s / q + 1.0);
}
std::complex<double> gainTransfer(double gainDb)
{
return {std::pow(10.0, gainDb / 20.0), 0.0};
}
std::complex<double> delayTransfer(double frequencyHz, double delayMs)
{
const double delaySeconds = delayMs / 1000.0;
const double phaseRadians = -2.0 * Pi * frequencyHz * delaySeconds;
return std::polar(1.0, phaseRadians);
}
std::complex<double> notchTransfer(double frequencyHz,
double centerFrequencyHz,
double q)
{
// Normalized form of
// H(s) = (s^2 + w0^2) / (s^2 + (w0/Q)s + w0^2), with s = j*w.
// Using r = f/f0 keeps the computation well-scaled and yields exactly 0+0j
// when a frequency-grid point is exactly equal to the notch center.
const double ratio = frequencyHz / centerFrequencyHz;
const double numerator = 1.0 - ratio * ratio;
const std::complex<double> denominator{numerator, ratio / q};
return numerator / denominator;
}
std::complex<double> butterworthTransfer(int order,
double frequencyHz,
double cutoffHz,
bool highPass)
{
const double normalizedFrequency = frequencyHz / cutoffHz;
const std::complex<double> s{0.0, normalizedFrequency};
std::complex<double> response{1.0, 0.0};
for (int poleIndex = 0; poleIndex < order; ++poleIndex) {
const double angle = Pi * static_cast<double>(2 * poleIndex + order + 1) /
static_cast<double>(2 * order);
const std::complex<double> pole = std::polar(1.0, angle);
if (highPass) {
response *= s / (s - pole);
} else {
response *= (-pole) / (s - pole);
}
}
return response;
}
struct BesselPoleSet
{
const std::complex<double>* poles = nullptr;
int count = 0;
};
BesselPoleSet besselPoleSet(int order)
{
// Analog Bessel poles for orders 1...8, magnitude-normalized so that
// |H(j)| = 1/sqrt(2). This makes the user-facing cutoff frequency the
// conventional -3.0103 dB point for every supported order.
static const std::complex<double> order1[] = {
{-0.99999999999999933, 0.0}
};
static const std::complex<double> order2[] = {
{-1.1016013305921608, 0.63600982475703405},
{-1.1016013305921608, -0.63600982475703405}
};
static const std::complex<double> order3[] = {
{-1.0474091610089349, 0.99926443628063688},
{-1.3226757999104441, 0.0},
{-1.0474091610089349, -0.99926443628063688}
};
static const std::complex<double> order4[] = {
{-0.99520876435027195, 1.2571057394546641},
{-1.3700678305514422, 0.41024971749375155},
{-1.3700678305514422, -0.41024971749375155},
{-0.99520876435027195, -1.2571057394546641}
};
static const std::complex<double> order5[] = {
{-0.95767654856268147, 1.4711243207303943},
{-1.3808773258604392, 0.71790958762676804},
{-1.5023162714474785, 0.0},
{-1.3808773258604392, -0.71790958762676804},
{-0.95767654856268147, -1.4711243207303943}
};
static const std::complex<double> order6[] = {
{-0.93065652294685908, 1.6618632689425918},
{-1.3818580975965642, 0.97147189071157158},
{-1.5714904036160318, 0.32089637422262396},
{-1.5714904036160318, -0.32089637422262396},
{-1.3818580975965642, -0.97147189071157158},
{-0.93065652294685908, -1.6618632689425918}
};
static const std::complex<double> order7[] = {
{-0.90986778062347051, 1.8364513530363944},
{-1.3789032167954749, 1.1915667778006531},
{-1.6120387662261257, 0.58924450693147201},
{-1.6843681792731817, 0.0},
{-1.6120387662261257, -0.58924450693147201},
{-1.3789032167954749, -1.1915667778006531},
{-0.90986778062347051, -1.8364513530363944}
};
static const std::complex<double> order8[] = {
{-0.89286971884713751, 1.9983258436413065},
{-1.3738412176373769, 1.3883565758775629},
{-1.6369394181268888, 0.82279562513969984},
{-1.757408400401653, 0.2728675751022327},
{-1.757408400401653, -0.2728675751022327},
{-1.6369394181268888, -0.82279562513969984},
{-1.3738412176373769, -1.3883565758775629},
{-0.89286971884713751, -1.9983258436413065}
};
switch (order) {
case 1: return {order1, 1};
case 2: return {order2, 2};
case 3: return {order3, 3};
case 4: return {order4, 4};
case 5: return {order5, 5};
case 6: return {order6, 6};
case 7: return {order7, 7};
case 8: return {order8, 8};
default: return {};
}
}
std::complex<double> besselTransfer(int order,
double frequencyHz,
double cutoffHz,
bool highPass)
{
const BesselPoleSet poleSet = besselPoleSet(order);
const std::complex<double> s{0.0, frequencyHz / cutoffHz};
// The low-pass prototype is H(s) = product((-p)/(s-p)), which has unity
// DC gain. The high-pass is obtained by the exact analog LP->HP
// transformation s -> 1/s.
const std::complex<double> prototypeS = highPass ? 1.0 / s : s;
std::complex<double> response{1.0, 0.0};
for (int poleIndex = 0; poleIndex < poleSet.count; ++poleIndex) {
const std::complex<double> pole = poleSet.poles[poleIndex];
response *= (-pole) / (prototypeS - pole);
}
return response;
}
std::complex<double> genericQTransfer(double frequencyHz,
double cutoffHz,
double q,
bool highPass)
{
// Canonical second-order Q-based crossover form with normalized
// s = j*f/f0. The user-facing Frequency value is therefore the
// natural frequency f0; at f0 the magnitude is exactly Q.
const std::complex<double> s{0.0, frequencyHz / cutoffHz};
const std::complex<double> sSquared = s * s;
const std::complex<double> denominator = sSquared + s / q + 1.0;
return highPass ? sSquared / denominator
: 1.0 / denominator;
}
std::complex<double> linkwitzRileyTransfer(int order,
double frequencyHz,
double cutoffHz,
bool highPass)
{
// A Linkwitz-Riley filter of order N is the cascade of two identical
// Butterworth filters of order N/2. Squaring the complete complex
// Butterworth response preserves both magnitude and phase.
const std::complex<double> butterworth =
butterworthTransfer(order / 2, frequencyHz, cutoffHz, highPass);
return butterworth * butterworth;
}
enum class SectionSupport
{
Supported,
Unsupported,
Invalid
};
SectionSupport evaluateSection(const ActiveFilterSection& section,
const KFilterFrequencyGrid& frequencies,
std::array<std::complex<double>, KFilterFrequencyCount>& response)
{
if (!section.enabled()) {
response.fill(std::complex<double>{1.0, 0.0});
return SectionSupport::Supported;
}
if (section.type() == ActiveFilterType::BandPass) {
const auto& parameters = std::get<ActiveFilterBandPassParameters>(section.parameters());
if (parameters.characteristic != ActiveFilterCharacteristic::Butterworth) {
return SectionSupport::Unsupported;
}
if (!validBandPassParameters(parameters.order,
parameters.lowerFrequencyHz,
parameters.upperFrequencyHz)) {
return SectionSupport::Invalid;
}
// KFilter defines a crossover-style band-pass as one Butterworth high-pass
// at the lower cutoff multiplied by one Butterworth low-pass at the upper
// cutoff. `order` therefore applies independently to both flanks.
for (std::size_t sampleIndex = 0; sampleIndex < KFilterFrequencyCount; ++sampleIndex) {
const double frequencyHz = frequencies[sampleIndex];
if (!std::isfinite(frequencyHz) || frequencyHz <= 0.0) {
return SectionSupport::Invalid;
}
response[sampleIndex] =
butterworthTransfer(parameters.order,
frequencyHz,
parameters.lowerFrequencyHz,
true) *
butterworthTransfer(parameters.order,
frequencyHz,
parameters.upperFrequencyHz,
false);
}
return SectionSupport::Supported;
}
if (section.type() == ActiveFilterType::Notch) {
const auto& parameters = std::get<ActiveFilterNotchParameters>(section.parameters());
if (!validNotchParameters(parameters.centerFrequencyHz, parameters.q)) {
return SectionSupport::Invalid;
}
for (std::size_t sampleIndex = 0; sampleIndex < KFilterFrequencyCount; ++sampleIndex) {
const double frequencyHz = frequencies[sampleIndex];
if (!std::isfinite(frequencyHz) || frequencyHz <= 0.0) {
return SectionSupport::Invalid;
}
response[sampleIndex] = notchTransfer(frequencyHz,
parameters.centerFrequencyHz,
parameters.q);
}
return SectionSupport::Supported;
}
if (section.type() == ActiveFilterType::PeakingEq) {
const auto& parameters =
std::get<ActiveFilterPeakingEqParameters>(section.parameters());
if (!validPeakingEqParameters(parameters.centerFrequencyHz,
parameters.q,
parameters.gainDb)) {
return SectionSupport::Invalid;
}
for (std::size_t sampleIndex = 0; sampleIndex < KFilterFrequencyCount; ++sampleIndex) {
const double frequencyHz = frequencies[sampleIndex];
if (!std::isfinite(frequencyHz) || frequencyHz <= 0.0) {
return SectionSupport::Invalid;
}
response[sampleIndex] = peakingEqTransfer(frequencyHz,
parameters.centerFrequencyHz,
parameters.q,
parameters.gainDb);
}
return SectionSupport::Supported;
}
if (section.type() == ActiveFilterType::LowShelf ||
section.type() == ActiveFilterType::HighShelf) {
const bool highShelf = section.type() == ActiveFilterType::HighShelf;
double transitionFrequencyHz = 0.0;
double q = 0.0;
double gainDb = 0.0;
if (highShelf) {
const auto& parameters =
std::get<ActiveFilterHighShelfParameters>(section.parameters());
transitionFrequencyHz = parameters.transitionFrequencyHz;
q = parameters.q;
gainDb = parameters.gainDb;
} else {
const auto& parameters =
std::get<ActiveFilterLowShelfParameters>(section.parameters());
transitionFrequencyHz = parameters.transitionFrequencyHz;
q = parameters.q;
gainDb = parameters.gainDb;
}
if (!validShelfParameters(transitionFrequencyHz, q, gainDb)) {
return SectionSupport::Invalid;
}
for (std::size_t sampleIndex = 0; sampleIndex < KFilterFrequencyCount; ++sampleIndex) {
const double frequencyHz = frequencies[sampleIndex];
if (!std::isfinite(frequencyHz) || frequencyHz <= 0.0) {
return SectionSupport::Invalid;
}
response[sampleIndex] = shelfTransfer(frequencyHz,
transitionFrequencyHz,
q,
gainDb,
highShelf);
}
return SectionSupport::Supported;
}
if (section.type() == ActiveFilterType::AllPass) {
const auto& parameters = std::get<ActiveFilterAllPassParameters>(section.parameters());
if (!validAllPassParameters(parameters.order, parameters.frequencyHz, parameters.q)) {
return SectionSupport::Invalid;
}
for (std::size_t sampleIndex = 0; sampleIndex < KFilterFrequencyCount; ++sampleIndex) {
const double frequencyHz = frequencies[sampleIndex];
if (!std::isfinite(frequencyHz) || frequencyHz <= 0.0) {
return SectionSupport::Invalid;
}
response[sampleIndex] = allPassTransfer(parameters.order,
frequencyHz,
parameters.frequencyHz,
parameters.q);
}
return SectionSupport::Supported;
}
if (section.type() == ActiveFilterType::Gain) {
const auto& parameters = std::get<ActiveFilterGainParameters>(section.parameters());
if (!validGainParameters(parameters.gainDb)) {
return SectionSupport::Invalid;
}
response.fill(gainTransfer(parameters.gainDb));
return SectionSupport::Supported;
}
if (section.type() == ActiveFilterType::Delay) {
const auto& parameters = std::get<ActiveFilterDelayParameters>(section.parameters());
if (!validDelayParameters(parameters.delayMs)) {
return SectionSupport::Invalid;
}
for (std::size_t sampleIndex = 0; sampleIndex < KFilterFrequencyCount; ++sampleIndex) {
const double frequencyHz = frequencies[sampleIndex];
if (!std::isfinite(frequencyHz) || frequencyHz <= 0.0) {
return SectionSupport::Invalid;
}
const double phaseRadians = -2.0 * Pi * frequencyHz * (parameters.delayMs / 1000.0);
if (!std::isfinite(phaseRadians)) {
return SectionSupport::Invalid;
}
response[sampleIndex] = delayTransfer(frequencyHz, parameters.delayMs);
}
return SectionSupport::Supported;
}
if (section.type() == ActiveFilterType::Polarity) {
const auto& parameters = std::get<ActiveFilterPolarityParameters>(section.parameters());
response.fill(parameters.inverted ? std::complex<double>{-1.0, 0.0}
: std::complex<double>{1.0, 0.0});
return SectionSupport::Supported;
}
int order = 0;
double cutoffHz = 0.0;
double q = 0.707;
bool highPass = false;
ActiveFilterCharacteristic characteristic = ActiveFilterCharacteristic::Butterworth;
switch (section.type()) {
case ActiveFilterType::LowPass: {
const auto& parameters = std::get<ActiveFilterLowPassParameters>(section.parameters());
characteristic = parameters.characteristic;
order = parameters.order;
cutoffHz = parameters.frequencyHz;
q = parameters.q;
break;
}
case ActiveFilterType::HighPass: {
const auto& parameters = std::get<ActiveFilterHighPassParameters>(section.parameters());
characteristic = parameters.characteristic;
order = parameters.order;
cutoffHz = parameters.frequencyHz;
q = parameters.q;
highPass = true;
break;
}
case ActiveFilterType::BandPass: // handled above; retained for exhaustive enum handling
case ActiveFilterType::Notch: // handled above; retained for exhaustive enum handling
case ActiveFilterType::PeakingEq: // handled above; retained for exhaustive enum handling
case ActiveFilterType::LowShelf: // handled above; retained for exhaustive enum handling
case ActiveFilterType::HighShelf: // handled above; retained for exhaustive enum handling
case ActiveFilterType::AllPass: // handled above; retained for exhaustive enum handling
case ActiveFilterType::Gain: // handled above; retained for exhaustive enum handling
case ActiveFilterType::Delay: // handled above; retained for exhaustive enum handling
case ActiveFilterType::Polarity: // handled above; retained for exhaustive enum handling
return SectionSupport::Unsupported;
}
if (characteristic != ActiveFilterCharacteristic::Butterworth &&
characteristic != ActiveFilterCharacteristic::Bessel &&
characteristic != ActiveFilterCharacteristic::LinkwitzRiley &&
characteristic != ActiveFilterCharacteristic::GenericQ) {
return SectionSupport::Unsupported;
}
const bool bessel = characteristic == ActiveFilterCharacteristic::Bessel;
const bool genericQ = characteristic == ActiveFilterCharacteristic::GenericQ;
const bool linkwitzRiley = characteristic == ActiveFilterCharacteristic::LinkwitzRiley;
// Patch 206 deliberately defines Generic/Q-based only as one canonical
// second-order section. Higher-order section decomposition is not
// implied by a single Q value, so those orders remain unsupported.
if (genericQ && order != 2) {
return SectionSupport::Unsupported;
}
const bool invalidParameters =
bessel ? !validBesselParameters(order, cutoffHz)
: (genericQ ? !validGenericQParameters(cutoffHz, q)
: (linkwitzRiley ? !validLinkwitzRileyParameters(order, cutoffHz)
: !validButterworthParameters(order, cutoffHz)));
if (invalidParameters) {
return SectionSupport::Invalid;
}
for (std::size_t sampleIndex = 0; sampleIndex < KFilterFrequencyCount; ++sampleIndex) {
const double frequencyHz = frequencies[sampleIndex];
if (!std::isfinite(frequencyHz) || frequencyHz <= 0.0) {
return SectionSupport::Invalid;
}
if (bessel) {
response[sampleIndex] = besselTransfer(order, frequencyHz, cutoffHz, highPass);
} else if (genericQ) {
response[sampleIndex] = genericQTransfer(frequencyHz, cutoffHz, q, highPass);
} else if (linkwitzRiley) {
response[sampleIndex] = linkwitzRileyTransfer(order, frequencyHz, cutoffHz, highPass);
} else {
response[sampleIndex] = butterworthTransfer(order, frequencyHz, cutoffHz, highPass);
}
}
return SectionSupport::Supported;
}
}
ActiveFilterResponse calculateActiveFilterResponse(const ActiveFilterChain& chain)
{
ActiveFilterResponse response = neutralResponse();
if (!chain.enabled()) {
return response;
}
const KFilterFrequencyGrid& frequencies = kfilterFrequencyGridHz();
bool haveActiveSection = false;
for (std::size_t sectionIndex = 0; sectionIndex < chain.sectionCount(); ++sectionIndex) {
const ActiveFilterSection& section = chain.section(sectionIndex);
if (!section.enabled()) {
continue;
}
haveActiveSection = true;
std::array<std::complex<double>, KFilterFrequencyCount> sectionResponse{};
const SectionSupport support = evaluateSection(section, frequencies, sectionResponse);
if (support != SectionSupport::Supported) {
return failedResponse(support == SectionSupport::Unsupported
? ActiveFilterResponseStatus::Unsupported
: ActiveFilterResponseStatus::InvalidParameters,
sectionIndex);
}
for (std::size_t sampleIndex = 0; sampleIndex < KFilterFrequencyCount; ++sampleIndex) {
response.values[sampleIndex] *= sectionResponse[sampleIndex];
}
}
response.hasActiveSections = haveActiveSection;
response.status = haveActiveSection ? ActiveFilterResponseStatus::Valid
: ActiveFilterResponseStatus::Neutral;
return response;
}
std::complex<double> applyActiveFilterResponseSample(
const ActiveFilterResponse& response,
std::size_t sampleIndex,
const std::complex<double>& signal)
{
if (response.status != ActiveFilterResponseStatus::Valid ||
sampleIndex >= KFilterFrequencyCount) {
return signal;
}
return signal * response.values[sampleIndex];
}
const ActiveFilterResponse& ActiveFilterResponseCache::responseFor(const ActiveFilterChain& chain)
{
if (!m_valid || !m_cachedChain.transferEquivalent(chain)) {
m_response = calculateActiveFilterResponse(chain);
m_cachedChain = chain;
m_valid = true;
++m_generation;
}
return m_response;
}
std::uint64_t ActiveFilterResponseCache::generation() const
{
return m_generation;
}