Repository navigation
Expand file tree
/
Copy pathdsp.cpp
More file actions
653 lines (586 loc) · 27.7 KB
/
Copy pathdsp.cpp
File metadata and controls
653 lines (586 loc) · 27.7 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
#include "dsp.hpp"
#include "audio_math.hpp"
#include <algorithm>
#include <cstring>
namespace crossfeed {
constexpr double PI = 3.14159265358979323846;
CrossfeedDSP::CrossfeedDSP() {
recompute_coeffs(control_params_);
requested_params_ = control_params_;
control_snapshot_.current = control_params_;
for (auto& p : param_buffers_) p = control_snapshot_;
pending_snapshot_ = control_snapshot_;
pending_params_ = control_params_;
}
bool CrossfeedDSP::valid_sample_rate(float rate) noexcept {
return std::isfinite(rate) && rate >= MIN_SAMPLE_RATE && rate <= MAX_SAMPLE_RATE;
}
bool CrossfeedDSP::normalize_params(FilterParams& p) {
if (!valid_sample_rate(p.sample_rate) || !std::isfinite(p.level_db) ||
!std::isfinite(p.freq_hz) || !std::isfinite(p.delay_us) ||
!std::isfinite(p.phase_apf_hz) || !std::isfinite(p.center_trim_db) ||
!std::isfinite(p.shadow_hz) || !std::isfinite(p.preamp_db)) return false;
// Keep distance from the degenerate Nyquist boundary, including at 8 kHz.
const float ceiling = p.sample_rate * 0.45f;
p.preamp_db = std::clamp(p.preamp_db, -24.0f, 12.0f);
p.level_db = std::clamp(p.level_db, MIN_LEVEL_DB, MAX_LEVEL_DB);
p.freq_hz = std::clamp(p.freq_hz, MIN_FREQ_HZ, std::min(MAX_FREQ_HZ, ceiling));
p.delay_us = std::clamp(p.delay_us, MIN_DELAY_US, MAX_DELAY_US);
p.phase_apf_hz = std::clamp(p.phase_apf_hz, MIN_PHASE_APF_HZ, std::min(MAX_PHASE_APF_HZ, ceiling));
p.center_trim_db = std::clamp(p.center_trim_db, MIN_CENTER_TRIM_DB, MAX_CENTER_TRIM_DB);
p.shadow_hz = std::clamp(p.shadow_hz, MIN_SHADOW_HZ, std::min(MAX_SHADOW_HZ, ceiling));
return true;
}
bool CrossfeedDSP::publish_locked(FilterParams p) {
const uint32_t requested = requested_rate_.load(std::memory_order_acquire);
if (requested != 0) p.sample_rate = static_cast<float>(requested);
if (!valid_sample_rate(p.sample_rate) || !std::isfinite(p.level_db) ||
!std::isfinite(p.freq_hz) || !std::isfinite(p.delay_us) ||
!std::isfinite(p.phase_apf_hz) || !std::isfinite(p.center_trim_db) ||
!std::isfinite(p.shadow_hz) || !std::isfinite(p.preamp_db)) return false;
// Preserve pristine target parameters requested by caller
requested_params_ = p;
requested_params_.preamp_db = std::clamp(p.preamp_db, -24.0f, 12.0f);
requested_params_.level_db = std::clamp(p.level_db, MIN_LEVEL_DB, MAX_LEVEL_DB);
requested_params_.freq_hz = std::clamp(p.freq_hz, MIN_FREQ_HZ, MAX_FREQ_HZ);
requested_params_.delay_us = std::clamp(p.delay_us, MIN_DELAY_US, MAX_DELAY_US);
requested_params_.phase_apf_hz = std::clamp(p.phase_apf_hz, MIN_PHASE_APF_HZ, MAX_PHASE_APF_HZ);
requested_params_.center_trim_db = std::clamp(p.center_trim_db, MIN_CENTER_TRIM_DB, MAX_CENTER_TRIM_DB);
requested_params_.shadow_hz = std::clamp(p.shadow_hz, MIN_SHADOW_HZ, MAX_SHADOW_HZ);
if (!normalize_params(p)) return false;
p.generation = control_params_.generation + 1;
recompute_coeffs(p);
for (unsigned slot = 0; slot < 2; ++slot) {
auto& saved = control_snapshot_.slots[slot];
saved.sample_rate = p.sample_rate;
normalize_params(saved);
recompute_coeffs(saved);
}
control_params_ = p;
control_snapshot_.current = p;
param_buffers_[write_idx_] = control_snapshot_;
// Acquire recovers a slot released by the reader; release publishes ours.
write_idx_ = exchange_idx_.exchange(write_idx_ | DIRTY, std::memory_order_acq_rel) & ~DIRTY;
return true;
}
bool CrossfeedDSP::set_params(float sample_rate, float level_db, float freq_hz, bool enabled) {
if (!valid_sample_rate(sample_rate)) return false;
std::lock_guard<std::mutex> lock(write_mutex_);
auto p = requested_params_;
p.sample_rate = sample_rate;
p.level_db = level_db;
p.freq_hz = freq_hz;
p.enabled = enabled;
return publish_locked(p);
}
bool CrossfeedDSP::set_all_params(float sample_rate, float level_db, float freq_hz,
float delay_us, float phase_apf_hz, float center_trim_db,
float shadow_hz, bool advanced_effects, bool enabled,
bool limiter, bool legacy_voicing) {
if (!valid_sample_rate(sample_rate)) return false;
std::lock_guard<std::mutex> lock(write_mutex_);
auto p = requested_params_;
p.sample_rate = sample_rate;
p.level_db = level_db;
p.freq_hz = freq_hz;
p.delay_us = delay_us;
p.phase_apf_hz = phase_apf_hz;
p.center_trim_db = center_trim_db;
p.shadow_hz = shadow_hz;
p.advanced_effects = advanced_effects;
p.enabled = enabled;
p.limiter = limiter;
p.legacy_voicing = legacy_voicing;
return publish_locked(p);
}
bool CrossfeedDSP::set_numeric(float FilterParams::*field, float value) {
std::lock_guard<std::mutex> lock(write_mutex_);
auto p = requested_params_;
p.*field = value;
return publish_locked(p);
}
bool CrossfeedDSP::set_level_db(float value) {
return set_numeric(&FilterParams::level_db, value);
}
bool CrossfeedDSP::set_freq_hz(float value) {
return set_numeric(&FilterParams::freq_hz, value);
}
bool CrossfeedDSP::set_delay_us(float value) {
return set_numeric(&FilterParams::delay_us, value);
}
bool CrossfeedDSP::set_phase_apf_hz(float value) {
return set_numeric(&FilterParams::phase_apf_hz, value);
}
bool CrossfeedDSP::set_center_trim_db(float value) {
return set_numeric(&FilterParams::center_trim_db, value);
}
bool CrossfeedDSP::set_shadow_hz(float value) {
return set_numeric(&FilterParams::shadow_hz, value);
}
void CrossfeedDSP::set_enabled(bool value) {
std::lock_guard<std::mutex> lock(write_mutex_);
auto p = requested_params_;
p.enabled = value;
publish_locked(p);
}
void CrossfeedDSP::set_advanced_effects(bool value) {
std::lock_guard<std::mutex> lock(write_mutex_);
auto p = requested_params_;
p.advanced_effects = value;
publish_locked(p);
}
void CrossfeedDSP::set_limiter(bool value) {
std::lock_guard<std::mutex> lock(write_mutex_);
auto p = requested_params_;
p.limiter = value;
publish_locked(p);
}
FilterParams CrossfeedDSP::get_params() const {
std::lock_guard<std::mutex> lock(write_mutex_);
return control_params_;
}
bool CrossfeedDSP::get_limiter() const { return get_params().limiter; }
FilterParams CrossfeedDSP::get_requested_params() const {
std::lock_guard<std::mutex> lock(write_mutex_);
return requested_params_;
}
float CrossfeedDSP::get_level_db() const { return get_params().level_db; }
float CrossfeedDSP::get_freq_hz() const { return get_params().freq_hz; }
float CrossfeedDSP::get_delay_us() const { return get_params().delay_us; }
float CrossfeedDSP::get_phase_apf_hz() const { return get_params().phase_apf_hz; }
float CrossfeedDSP::get_center_trim_db() const { return get_params().center_trim_db; }
float CrossfeedDSP::get_shadow_hz() const { return get_params().shadow_hz; }
float CrossfeedDSP::get_sample_rate() const { return get_params().sample_rate; }
bool CrossfeedDSP::get_advanced_effects() const { return get_params().advanced_effects; }
bool CrossfeedDSP::is_enabled() const { return get_params().enabled; }
void CrossfeedDSP::request_sample_rate(uint32_t rate) noexcept {
requested_rate_.store(rate, std::memory_order_release);
}
bool CrossfeedDSP::service_sample_rate() {
std::lock_guard<std::mutex> lock(write_mutex_);
const auto rate = requested_rate_.load(std::memory_order_acquire);
if (rate == 0 || static_cast<float>(rate) == control_params_.sample_rate) return true;
if (!valid_sample_rate(static_cast<float>(rate))) return false;
auto p = requested_params_;
p.sample_rate = static_cast<float>(rate);
return publish_locked(p);
}
void CrossfeedDSP::reset() {
reset_requested_.store(true, std::memory_order_release);
}
void CrossfeedDSP::recompute_coeffs(FilterParams& params) {
params.preamp_linear = static_cast<float>(std::pow(10.0, params.preamp_db / 20.0));
params.gain2 = std::pow(10.0f, params.level_db / 20.0f);
params.trim_linear = std::pow(10.0f, params.center_trim_db / 20.0f);
params.delay_samples = (params.delay_us / 1000000.0f) * params.sample_rate;
// Legacy keeps the exact historical computation (bit-identical output).
// Flat-mono: direct path is a flat tap, and the cross gain is capped so
// the mono sum stays within +-1.5 dB of unity for ANY cross-path phase
// (delay, APF, shadow) by the triangle inequality: |1 + X| in
// [1-x, 1+x] with x <= FLAT_MONO_MAX_CROSS.
params.cross_gain = params.legacy_voicing
? params.gain2
: std::min(params.gain2, FLAT_MONO_MAX_CROSS);
if (params.legacy_voicing) {
double dir_gain_db = FULL_DIR_GAIN * (params.gain2 / FULL_GAIN2);
compute_lowshelf(params.dir_coeffs, params.sample_rate, params.freq_hz, 0.7071f, dir_gain_db);
} else {
params.dir_coeffs = BiquadCoeffs{}; // Flat direct tap, decoupled from blend.
}
compute_lowpass(params.cross_coeffs, params.sample_rate, params.freq_hz, 0.5f);
compute_allpass(params.apf_coeffs, params.sample_rate, params.phase_apf_hz, 0.7071f);
compute_lowpass(params.shadow_coeffs, params.sample_rate, params.shadow_hz, 0.7071f);
}
void CrossfeedDSP::compute_lowshelf(BiquadCoeffs& out, float sample_rate, float freq, float q, float gain_db) {
if (!valid_sample_rate(sample_rate) || !std::isfinite(freq) || freq <= 0.0f ||
freq >= sample_rate * 0.5f || !std::isfinite(q) || q <= 0.0f || !std::isfinite(gain_db)) {
out = BiquadCoeffs{};
return;
}
double A = std::pow(10.0, static_cast<double>(gain_db) / 40.0);
double w0 = 2.0f * PI * freq / sample_rate;
double cos_w = std::cos(w0);
double sin_w = std::sin(w0);
double alpha = sin_w / (2.0f * q);
double sqrtA = std::sqrt(A);
double a0 = (A + 1.0f) + (A - 1.0f) * cos_w + 2.0f * sqrtA * alpha;
if (std::abs(a0) < 1e-9f) {
out = BiquadCoeffs{};
return;
}
double inv_a0 = 1.0f / a0;
out.b0 = (A * ((A + 1.0f) - (A - 1.0f) * cos_w + 2.0f * sqrtA * alpha)) * inv_a0;
out.b1 = (2.0f * A * ((A - 1.0f) - (A + 1.0f) * cos_w)) * inv_a0;
out.b2 = (A * ((A + 1.0f) - (A - 1.0f) * cos_w - 2.0f * sqrtA * alpha)) * inv_a0;
out.a1 = (-2.0f * ((A - 1.0f) + (A + 1.0f) * cos_w)) * inv_a0;
out.a2 = ((A + 1.0f) + (A - 1.0f) * cos_w - 2.0f * sqrtA * alpha) * inv_a0;
}
void CrossfeedDSP::compute_lowpass(BiquadCoeffs& out, float sample_rate, float freq, float q) {
if (!valid_sample_rate(sample_rate) || !std::isfinite(freq) || freq <= 0.0f ||
freq >= sample_rate * 0.5f || !std::isfinite(q) || q <= 0.0f) {
out = BiquadCoeffs{};
return;
}
double w0 = 2.0f * PI * freq / sample_rate;
double cos_w = std::cos(w0);
double sin_w = std::sin(w0);
double alpha = sin_w / (2.0f * q);
double a0 = 1.0f + alpha;
if (std::abs(a0) < 1e-9f) {
out = BiquadCoeffs{};
return;
}
double inv_a0 = 1.0f / a0;
out.b0 = ((1.0f - cos_w) * 0.5f) * inv_a0;
out.b1 = (1.0f - cos_w) * inv_a0;
out.b2 = ((1.0f - cos_w) * 0.5f) * inv_a0;
out.a1 = (-2.0f * cos_w) * inv_a0;
out.a2 = (1.0f - alpha) * inv_a0;
}
void CrossfeedDSP::compute_allpass(BiquadCoeffs& out, float sample_rate, float freq, float q) {
if (!valid_sample_rate(sample_rate) || !std::isfinite(freq) || freq <= 0.0f ||
freq >= sample_rate * 0.5f || !std::isfinite(q) || q <= 0.0f) {
out = BiquadCoeffs{};
return;
}
double w0 = 2.0f * PI * freq / sample_rate;
double cos_w = std::cos(w0);
double sin_w = std::sin(w0);
double alpha = sin_w / (2.0f * q);
double a0 = 1.0f + alpha;
if (std::abs(a0) < 1e-9f) {
out = BiquadCoeffs{};
return;
}
double inv_a0 = 1.0f / a0;
out.b0 = (1.0f - alpha) * inv_a0;
out.b1 = (-2.0f * cos_w) * inv_a0;
out.b2 = 1.0f;
out.a1 = (-2.0f * cos_w) * inv_a0;
out.a2 = (1.0f - alpha) * inv_a0;
}
void CrossfeedDSP::Engine::reset(const FilterParams& p) noexcept {
dir_l.reset(); dir_r.reset(); cross_l.reset(); cross_r.reset();
apf_l.reset(); apf_r.reset(); shadow_l.reset(); shadow_r.reset();
delay_l.reset(); delay_r.reset();
params = p;
delay_samples = p.delay_samples;
delay_step = 0.0f;
delay_remaining = 0;
}
void CrossfeedDSP::Engine::retarget(const FilterParams& p) noexcept {
params = p;
delay_remaining = std::max(1u, static_cast<unsigned>(p.sample_rate * TRANSITION_SECONDS));
delay_step = (p.delay_samples - delay_samples) / static_cast<float>(delay_remaining);
}
void CrossfeedDSP::Engine::process(float l, float r, float& out_l, float& out_r) noexcept {
if (delay_remaining > 0) {
delay_samples += delay_step;
if (--delay_remaining == 0) delay_samples = params.delay_samples;
}
const float dl = dir_l.process(l, params.dir_coeffs);
const float dr = dir_r.process(r, params.dir_coeffs);
const float cl = cross_l.process(l, params.cross_coeffs);
const float cr = cross_r.process(r, params.cross_coeffs);
// Keep histories warm even in pure/bypass mode, so enabling a stage never
// replays stale samples from the last time it was used.
delay_l.write(apf_l.process(shadow_l.process(cl, params.shadow_coeffs), params.apf_coeffs));
delay_r.write(apf_r.process(shadow_r.process(cr, params.shadow_coeffs), params.apf_coeffs));
if (!params.enabled) {
// Bypass follows the processed path's active output trim. Pure mode
// has no trim on either path. Shared preamp is applied by the caller.
const float trim = params.advanced_effects ? params.trim_linear : 1.0f;
out_l = l * trim; out_r = r * trim;
} else if (params.advanced_effects) {
out_l = (dl + params.cross_gain * delay_r.read(delay_samples)) * params.trim_linear;
out_r = (dr + params.cross_gain * delay_l.read(delay_samples)) * params.trim_linear;
} else {
out_l = dl + params.cross_gain * cr;
out_r = dr + params.cross_gain * cl;
}
}
bool CrossfeedDSP::begin_block() noexcept {
if (exchange_idx_.load(std::memory_order_acquire) & DIRTY) {
read_idx_ = exchange_idx_.exchange(read_idx_, std::memory_order_acq_rel) & ~DIRTY;
pending_snapshot_ = param_buffers_[read_idx_];
pending_params_ = pending_snapshot_.current;
}
if (meter_revision_ != pending_snapshot_.meter_reset) {
meters_ = ListeningStatus{};
meter_revision_ = pending_snapshot_.meter_reset;
}
const bool reset = reset_requested_.exchange(false, std::memory_order_acquire);
const uint32_t rate = requested_rate_.load(std::memory_order_acquire);
if (rate != 0 && pending_params_.sample_rate != static_cast<float>(rate)) {
// Do not use coefficients or delay histories belonging to another rate.
awaiting_rate_ = true;
started_ = false;
fade_remaining_ = 0;
return false;
}
if (input_reset_pending_) {
// Sanitization keeps new poison out of the filters; any state the bad
// data already reached is cleared once, at a block boundary, without
// disturbing an ongoing fade's parameters.
input_reset_pending_ = false;
engines_[active_engine_].reset(engines_[active_engine_].params);
engines_[1 - active_engine_].reset(engines_[1 - active_engine_].params);
}
if (limiter_config_rate_ != pending_params_.sample_rate || reset)
configure_limiter(pending_params_.sample_rate);
if (pending_params_.limiter != limiter_on_) {
// A fresh enable starts from unity gain with an empty hold; rings stay
// warm, so a peak that entered just before enabling may pass through.
limiter_on_ = pending_params_.limiter;
if (limiter_on_) {
lim_env_ = 1.0f;
lim_hold_.reset();
}
}
auto& current = engines_[active_engine_];
if (!started_ || reset || current.params.sample_rate != pending_params_.sample_rate) {
current.reset(pending_params_);
started_ = true;
fade_remaining_ = 0;
if (awaiting_rate_ && !reset) {
engines_[1 - active_engine_] = current;
current.params.enabled = false;
// Recovery fades from the actual unscaled rate-fallback output.
current.params.advanced_effects = false;
current.params.preamp_linear = 1.0f;
fade_length_ = std::max(1u, static_cast<unsigned>(pending_params_.sample_rate * TRANSITION_SECONDS));
fade_remaining_ = fade_length_;
}
awaiting_rate_ = false;
} else if (fade_remaining_ == 0 && current.params.generation != pending_params_.generation) {
auto& next = engines_[1 - active_engine_];
next = current;
next.retarget(pending_params_);
fade_length_ = std::max(1u, static_cast<unsigned>(pending_params_.sample_rate * TRANSITION_SECONDS));
fade_remaining_ = fade_length_;
}
return true;
}
void CrossfeedDSP::configure_limiter(float sample_rate) noexcept {
unsigned lookahead = static_cast<unsigned>(sample_rate * LIMITER_LOOKAHEAD_SECONDS);
if (lookahead < 1) lookahead = 1;
lim_ring_l_.configure(lookahead);
lim_ring_r_.configure(lookahead);
lim_hold_.configure(lookahead);
lim_ring_l_.reset();
lim_ring_r_.reset();
// The hold turns a crest into a step L samples early; one one-pole with
// k/L per sample converges as e^-k, so k = 18 leaves ~1e-8 of the step
// by the time the crest emerges — no exp/pow on the audio thread.
const float attack = 18.0f / static_cast<float>(lookahead);
lim_attack_coef_ = attack < 1.0f ? attack : 1.0f;
const float release = sample_rate * LIMITER_RELEASE_SECONDS;
lim_release_coef_ = release > 1.0f ? 1.0f / release : 1.0f;
limiter_config_rate_ = sample_rate;
}
void CrossfeedDSP::process_sample(float l, float r, float& out_l, float& out_r) noexcept {
engines_[active_engine_].process(l, r, out_l, out_r);
const float gain = engines_[active_engine_].params.preamp_linear;
out_l *= gain; out_r *= gain;
if (fade_remaining_ > 0) {
float next_l, next_r;
engines_[1 - active_engine_].process(l, r, next_l, next_r);
const unsigned next_engine = 1 - active_engine_;
const float next_gain = engines_[next_engine].params.preamp_linear;
next_l *= next_gain; next_r *= next_gain;
const float mix = 1.0f - static_cast<float>(fade_remaining_ - 1) / static_cast<float>(fade_length_);
out_l += mix * (next_l - out_l);
out_r += mix * (next_r - out_r);
if (--fade_remaining_ == 0) active_engine_ = 1 - active_engine_;
}
// Stereo-linked lookahead limiter on the post-mix signal (after fades and
// preamp, so crossfading engines sum before limiting). The delayed samples
// meet the current, fully-converged envelope: the attack window matches
// the lookahead, so a peak's required gain is reached before it emerges.
// Rings stay warm even while the stage is off; only envelope math is gated.
const float lim_delayed_l = lim_ring_l_.process(out_l);
const float lim_delayed_r = lim_ring_r_.process(out_r);
if (limiter_on_) {
const float link = std::max(std::abs(out_l), std::abs(out_r));
const float hold = lim_hold_.process(link);
const float required = hold > LIMITER_CEILING_LINEAR ? LIMITER_CEILING_LINEAR / hold : 1.0f;
const float coef = required < lim_env_ ? lim_attack_coef_ : lim_release_coef_;
lim_env_ += (required - lim_env_) * coef;
if (lim_env_ < block_limiter_gain_) block_limiter_gain_ = lim_env_;
out_l = lim_delayed_l * lim_env_;
out_r = lim_delayed_r * lim_env_;
}
}
void CrossfeedDSP::process_interleaved(const float* in, float* out, size_t frames) noexcept {
if (frames == 0) return;
ScopedDenormals denormals;
block_input_peak_ = block_output_peak_ = 0;
block_in_l_ = block_in_r_ = block_out_l_ = block_out_r_ = 0;
block_tp_in_ = block_tp_out_ = 0;
block_rms_sum_ = 0;
block_frames_ = 0;
block_limiter_gain_ = 1.0f;
block_input_invalid_ = false;
const bool ready = begin_block();
for (size_t i = 0; i < frames; ++i) {
float l = in[2 * i], r = in[2 * i + 1];
if (!std::isfinite(l)) { l = 0.0f; block_input_invalid_ = true; }
if (!std::isfinite(r)) { r = 0.0f; block_input_invalid_ = true; }
if (ready) process_sample(l, r, out[2 * i], out[2 * i + 1]);
else { out[2 * i] = l; out[2 * i + 1] = r; }
meter_sample(l, r, out[2 * i], out[2 * i + 1]);
}
publish_meters();
}
void CrossfeedDSP::process_planar(const float* in_l, const float* in_r, float* out_l, float* out_r, size_t frames) noexcept {
if (frames == 0) return;
ScopedDenormals denormals;
block_input_peak_ = block_output_peak_ = 0;
block_in_l_ = block_in_r_ = block_out_l_ = block_out_r_ = 0;
block_tp_in_ = block_tp_out_ = 0;
block_rms_sum_ = 0;
block_frames_ = 0;
block_limiter_gain_ = 1.0f;
block_input_invalid_ = false;
const bool ready = begin_block();
for (size_t i = 0; i < frames; ++i) {
float l = in_l[i], r = in_r[i];
if (!std::isfinite(l)) { l = 0.0f; block_input_invalid_ = true; }
if (!std::isfinite(r)) { r = 0.0f; block_input_invalid_ = true; }
if (ready) process_sample(l, r, out_l[i], out_r[i]);
else { out_l[i] = l; out_r[i] = r; }
meter_sample(l, r, out_l[i], out_r[i]);
}
publish_meters();
}
bool CrossfeedDSP::same_sound(const FilterParams& a, const FilterParams& b) noexcept {
return a.level_db == b.level_db && a.freq_hz == b.freq_hz && a.delay_us == b.delay_us &&
a.phase_apf_hz == b.phase_apf_hz && a.center_trim_db == b.center_trim_db &&
a.shadow_hz == b.shadow_hz && a.advanced_effects == b.advanced_effects &&
a.limiter == b.limiter && a.legacy_voicing == b.legacy_voicing;
}
bool CrossfeedDSP::set_snapshot(FilterParams params) {
std::lock_guard<std::mutex> lock(write_mutex_);
return publish_locked(params);
}
bool CrossfeedDSP::set_preamp_db(float value) { return set_numeric(&FilterParams::preamp_db, value); }
void CrossfeedDSP::reset_meters() {
std::lock_guard<std::mutex> lock(write_mutex_);
++control_snapshot_.meter_reset;
publish_locked(control_params_);
}
bool CrossfeedDSP::store_slot(unsigned slot) {
if (slot > 1) return false;
std::lock_guard<std::mutex> lock(write_mutex_);
control_snapshot_.slots[slot] = control_params_;
control_snapshot_.valid[slot] = true;
return publish_locked(control_params_);
}
bool CrossfeedDSP::recall_slot(unsigned slot) {
if (slot > 1) return false;
std::lock_guard<std::mutex> lock(write_mutex_);
if (!control_snapshot_.valid[slot]) return false;
auto p = control_snapshot_.slots[slot];
p.enabled = true;
p.preamp_db = control_params_.preamp_db; // Shared listening level, not part of an A/B candidate.
p.sample_rate = control_params_.sample_rate;
return publish_locked(p);
}
bool CrossfeedDSP::get_slot(unsigned slot, FilterParams& params) const {
if (slot > 1) return false;
std::lock_guard<std::mutex> lock(write_mutex_);
if (!control_snapshot_.valid[slot]) return false;
params = control_snapshot_.slots[slot];
return true;
}
ListeningStatus CrossfeedDSP::get_listening_status() const {
std::lock_guard<std::mutex> lock(meter_read_mutex_);
if (meter_exchange_.load(std::memory_order_acquire) & DIRTY)
meter_read_ = meter_exchange_.exchange(meter_read_, std::memory_order_acq_rel) & ~DIRTY;
return meter_buffers_[meter_read_];
}
// Catmull-Rom interpolation between p1..p2 with neighbors p0/p3; can overshoot
// the endpoints, which is exactly what a cheap true-peak estimate needs.
static inline float catmull_rom(float p0, float p1, float p2, float p3, float t) noexcept {
const float t2 = t * t, t3 = t2 * t;
return 0.5f * ((2.0f * p1) + (-p0 + p2) * t +
(2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * t2 +
(-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
}
void CrossfeedDSP::meter_sample(float l, float r, float out_l, float out_r) noexcept {
const float input = std::max(std::abs(l), std::abs(r));
const float output = std::max(std::abs(out_l), std::abs(out_r));
if (std::abs(l) > block_in_l_) block_in_l_ = std::abs(l);
if (std::abs(r) > block_in_r_) block_in_r_ = std::abs(r);
if (std::abs(out_l) > block_out_l_) block_out_l_ = std::abs(out_l);
if (std::abs(out_r) > block_out_r_) block_out_r_ = std::abs(out_r);
block_rms_sum_ += static_cast<double>(out_l) * out_l + static_cast<double>(out_r) * out_r;
++block_frames_;
// True-peak: Catmull-Rom midpoints between the last two samples of each
// channel pair (3 of 4 phases; phase 0 is the sample itself). The kernel
// can overshoot its endpoints, which linear interpolation never does.
if (tp_hist_fill_ < 4) {
tp_hist_in_[tp_hist_fill_] = std::abs(l) > std::abs(r) ? l : r;
tp_hist_out_[tp_hist_fill_] = std::abs(out_l) > std::abs(out_r) ? out_l : out_r;
++tp_hist_fill_;
if (tp_hist_fill_ == 4) {
block_tp_in_ = std::max(block_tp_in_, std::max(std::abs(tp_hist_in_[1]), std::abs(tp_hist_in_[2])));
block_tp_out_ = std::max(block_tp_out_, std::max(std::abs(tp_hist_out_[1]), std::abs(tp_hist_out_[2])));
}
return;
}
tp_hist_in_[0] = tp_hist_in_[1];
tp_hist_in_[1] = tp_hist_in_[2];
tp_hist_in_[2] = tp_hist_in_[3];
tp_hist_in_[3] = std::abs(l) > std::abs(r) ? l : r;
tp_hist_out_[0] = tp_hist_out_[1];
tp_hist_out_[1] = tp_hist_out_[2];
tp_hist_out_[2] = tp_hist_out_[3];
tp_hist_out_[3] = std::abs(out_l) > std::abs(out_r) ? out_l : out_r;
block_tp_in_ = std::max(block_tp_in_, std::abs(tp_hist_in_[2]));
block_tp_out_ = std::max(block_tp_out_, std::abs(tp_hist_out_[2]));
for (int phase = 1; phase <= 3; ++phase) {
const float t = phase * 0.25f;
block_tp_in_ = std::max(block_tp_in_,
std::abs(catmull_rom(tp_hist_in_[0], tp_hist_in_[1], tp_hist_in_[2], tp_hist_in_[3], t)));
block_tp_out_ = std::max(block_tp_out_,
std::abs(catmull_rom(tp_hist_out_[0], tp_hist_out_[1], tp_hist_out_[2], tp_hist_out_[3], t)));
}
// Clip flagging uses the true-peak estimate: inter-sample overs count.
meters_.input_clip |= block_tp_in_ >= 1.0f;
meters_.output_clip |= block_tp_out_ >= 1.0f;
if (std::isfinite(input)) block_input_peak_ = std::max(block_input_peak_, input);
if (std::isfinite(output)) block_output_peak_ = std::max(block_output_peak_, output);
meters_.input_clip |= input >= 1 || !std::isfinite(l) || !std::isfinite(r);
meters_.output_clip |= output >= 1 || !std::isfinite(out_l) || !std::isfinite(out_r);
}
void CrossfeedDSP::publish_meters() noexcept {
meters_.input_peak = block_input_peak_;
meters_.output_peak = block_output_peak_;
meters_.limiter_gain = block_limiter_gain_;
meters_.in_l = block_in_l_;
meters_.in_r = block_in_r_;
meters_.out_l = block_out_l_;
meters_.out_r = block_out_r_;
meters_.tp_in = block_tp_in_;
meters_.tp_out = block_tp_out_;
if (block_frames_ > 0) {
const float mean = static_cast<float>(block_rms_sum_ / block_frames_);
meters_.rms_out = std::sqrt(mean);
} else {
meters_.rms_out = 0.0f;
}
meters_.input_hold = std::max(meters_.input_hold, block_input_peak_);
meters_.output_hold = std::max(meters_.output_hold, block_output_peak_);
meters_.hold_in_l = std::max(meters_.hold_in_l, block_in_l_);
meters_.hold_in_r = std::max(meters_.hold_in_r, block_in_r_);
meters_.hold_out_l = std::max(meters_.hold_out_l, block_out_l_);
meters_.hold_out_r = std::max(meters_.hold_out_r, block_out_r_);
if (block_input_invalid_) {
meters_.input_invalid = true;
input_reset_pending_ = true;
}
meter_buffers_[meter_write_] = meters_;
meter_write_ = meter_exchange_.exchange(meter_write_ | DIRTY, std::memory_order_acq_rel) & ~DIRTY;
}
} // namespace crossfeed