DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
MultibandSaturation.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
54#include "Clipper.h"
55#include "CrossoverFilter.h"
56#include "Saturation.h"
57#include "../Core/AudioBuffer.h"
58#include "../Core/AudioSpec.h"
59#include "../Core/DspMath.h"
60#include "../Core/StateBlob.h"
61
62#include <algorithm>
63#include <array>
64#include <atomic>
65#include <cmath>
66#include <cstddef>
67#include <cstdint>
68#include <cstdio>
69#include <memory>
70#include <numbers>
71#include <vector>
72
73namespace dspark {
74
82template <FloatType T, int MaxBands = 6>
84{
85 static_assert(MaxBands >= 2, "MultibandSaturation needs at least 2 bands (one split)");
86
87public:
89
91 {
92 for (auto& band : saturation_) band = std::make_unique<Saturation<T>>();
93 }
94
97
98 // -- Lifecycle -----------------------------------------------------------
99
107 void prepare(const AudioSpec& spec)
108 {
109 if (!spec.isValid()) return;
110 prepared_.store(false, std::memory_order_relaxed);
111 spec_ = spec;
112 crossover_.prepare(spec);
113 for (int b = 0; b < MaxBands; ++b)
114 {
115 bandBuffers_[static_cast<size_t>(b)].resize(spec.numChannels, spec.maxBlockSize);
116 saturation_[static_cast<size_t>(b)]->prepare(spec);
117 }
118 // Two lines a band: its dry side waits for the slowest band too.
119 padLines_.assign(static_cast<size_t>(2 * MaxBands) * static_cast<size_t>(std::max(spec.numChannels, 1))
120 * static_cast<size_t>(kPadSize), T(0));
121 dryBuffer_.resize(spec.numChannels, spec.maxBlockSize);
122 bandDry_.resize(spec.numChannels, spec.maxBlockSize);
123 clip_.setMode(Clipper<T>::Mode::Hard);
124 clip_.setOversampling(4);
125 clip_.setCeiling(outputCeilingDb_.load(std::memory_order_relaxed));
126 clip_.prepare(spec);
127 fadeStep_ = 1.0 / std::max(1.0, kFadeSeconds * spec.sampleRate);
128 reset();
129 prepared_.store(true, std::memory_order_relaxed);
130 }
131
133 void reset() noexcept
134 {
135 crossover_.reset();
136 for (auto& band : saturation_) band->reset();
137 std::fill(padLines_.begin(), padLines_.end(), T(0));
138 padPos_ = 0;
139 clip_.reset();
140 clipping_ = outputClip_.load(std::memory_order_relaxed);
141 mixNow_ = static_cast<double>(mix_.load(std::memory_order_relaxed));
142 dryRunning_ = mixNow_ < 1.0;
143 for (int b = 0; b < MaxBands; ++b)
144 {
145 BandRamp& ramp = ramps_[static_cast<size_t>(b)];
146 const Targets t = targets(b, anySolo());
147 ramp.heard = t.heard;
148 ramp.left = t.left;
149 ramp.right = t.right;
150 ramp.computed = t.heard > 0.0;
151 }
152 }
153
161 void processBlock(AudioBufferView<T> buffer) noexcept
162 {
163 if (!prepared_.load(std::memory_order_relaxed)) return;
164 const int total = buffer.getNumSamples();
165 const int chunk = bandBuffers_[0].getNumSamples();
166 if (chunk <= 0) return;
167 for (int offset = 0; offset < total; offset += chunk)
168 processChunk(buffer.getSubView(offset, std::min(chunk, total - offset)));
169 }
170
171 // -- Crossover -------------------------------------------------------------
172
174 void setNumBands(int n) noexcept { crossover_.setNumBands(std::clamp(n, 2, MaxBands)); }
176 void setNumBandsKeepingSplits(int n) noexcept { crossover_.setNumBandsKeepingSplits(std::clamp(n, 2, MaxBands)); }
178 void setCrossoverFrequency(int index, T hz) noexcept { crossover_.setCrossoverFrequency(index, hz); }
180 void setOrder(int order) noexcept { crossover_.setOrder(order); }
182 void setCrossoverMode(typename Crossover::FilterMode mode) noexcept { crossover_.setFilterMode(mode); }
184 void setCrossoverStructure(typename Crossover::Structure structure) noexcept { crossover_.setStructure(structure); }
186 void setCrossoverTopology(typename Crossover::Topology topology) noexcept { crossover_.setTopology(topology); }
187
188 [[nodiscard]] int getNumBands() const noexcept { return crossover_.getNumBands(); }
189 [[nodiscard]] T getCrossoverFrequency(int index) const noexcept { return crossover_.getCrossoverFrequency(index); }
190 [[nodiscard]] int getOrder() const noexcept { return crossover_.getOrder(); }
191 [[nodiscard]] typename Crossover::FilterMode getCrossoverMode() const noexcept { return crossover_.getFilterMode(); }
192 [[nodiscard]] typename Crossover::Structure getCrossoverStructure() const noexcept { return crossover_.getStructure(); }
193 [[nodiscard]] typename Crossover::Topology getCrossoverTopology() const noexcept { return crossover_.getTopology(); }
194
195 // -- Bands ---------------------------------------------------------------
196
198 [[nodiscard]] Saturation<T>& getBandSaturation(int band) noexcept
199 {
200 return *saturation_[static_cast<size_t>(std::clamp(band, 0, MaxBands - 1))];
201 }
203 [[nodiscard]] const Saturation<T>& getBandSaturation(int band) const noexcept
204 {
205 return *saturation_[static_cast<size_t>(std::clamp(band, 0, MaxBands - 1))];
206 }
207
209 void setBandAlgorithm(int band, typename Saturation<T>::Algorithm algo) { getBandSaturation(band).setAlgorithm(algo); }
211 void setBandDrive(int band, T dB) { getBandSaturation(band).setDrive(dB); }
212
214 void setBandLevel(int band, T dB) noexcept
215 {
216 if (band < 0 || band >= MaxBands || !std::isfinite(dB)) return;
217 params_[static_cast<size_t>(band)].levelDb.store(std::clamp(dB, T(-60), T(24)), std::memory_order_relaxed);
218 }
220 void setBandPan(int band, T pan) noexcept
221 {
222 if (band < 0 || band >= MaxBands || !std::isfinite(pan)) return;
223 params_[static_cast<size_t>(band)].pan.store(std::clamp(pan, T(-1), T(1)), std::memory_order_relaxed);
224 }
226 void setBandMute(int band, bool on) noexcept
227 {
228 if (band < 0 || band >= MaxBands) return;
229 params_[static_cast<size_t>(band)].mute.store(on, std::memory_order_relaxed);
230 }
232 void setBandSolo(int band, bool on) noexcept
233 {
234 if (band < 0 || band >= MaxBands) return;
235 params_[static_cast<size_t>(band)].solo.store(on, std::memory_order_relaxed);
236 }
237
238 [[nodiscard]] T getBandLevel(int band) const noexcept { return param(band).levelDb.load(std::memory_order_relaxed); }
239 [[nodiscard]] T getBandPan(int band) const noexcept { return param(band).pan.load(std::memory_order_relaxed); }
240 [[nodiscard]] bool isBandMuted(int band) const noexcept { return param(band).mute.load(std::memory_order_relaxed); }
241 [[nodiscard]] bool isBandSoloed(int band) const noexcept { return param(band).solo.load(std::memory_order_relaxed); }
242
244 [[nodiscard]] bool isBandComputed(int band) const noexcept
245 {
246 return band >= 0 && band < MaxBands && ramps_[static_cast<size_t>(band)].computed;
247 }
248
249 // -- The sum ---------------------------------------------------------------
250
265 void setMix(T amount) noexcept
266 {
267 if (std::isfinite(amount)) mix_.store(std::clamp(amount, T(0), T(1)), std::memory_order_relaxed);
268 }
269 [[nodiscard]] T getMix() const noexcept { return mix_.load(std::memory_order_relaxed); }
270
280 void setOutputClip(bool on) noexcept { outputClip_.store(on, std::memory_order_relaxed); }
281 [[nodiscard]] bool getOutputClip() const noexcept { return outputClip_.load(std::memory_order_relaxed); }
282
284 void setOutputCeiling(T dB) noexcept
285 {
286 if (!std::isfinite(dB)) return;
287 outputCeilingDb_.store(std::clamp(dB, T(-24), T(0)), std::memory_order_relaxed);
288 clip_.setCeiling(outputCeilingDb_.load(std::memory_order_relaxed));
289 }
290 [[nodiscard]] T getOutputCeiling() const noexcept { return outputCeilingDb_.load(std::memory_order_relaxed); }
291
292 // -- Settings shared by every band (setup thread) ----------------------------
293
295 void setOversampling(int factor)
296 {
297 for (auto& band : saturation_) band->setOversampling(factor);
298 }
301 {
302 for (auto& band : saturation_) band->setOversamplingMode(mode);
303 }
307 {
308 for (auto& each : saturation_) each->setOversamplingBand(band);
309 }
311 void setAntialiasing(typename Saturation<T>::Antialiasing mode) noexcept
312 {
313 for (auto& band : saturation_) band->setAntialiasing(mode);
314 }
315
323 [[nodiscard]] int getLatency() const noexcept
324 {
325 return crossover_.getLatency() + slowestBand(crossover_.getNumBands())
326 + (outputClip_.load(std::memory_order_relaxed) ? clip_.getLatency() : 0);
327 }
328
329 // -- State -----------------------------------------------------------------
330
332 [[nodiscard]] std::vector<uint8_t> getState() const
333 {
334 StateWriter w(stateId("MBST"), 1);
335 const int n = crossover_.getNumBands();
336 w.write("numBands", n);
337 w.write("order", crossover_.getOrder());
338 w.write("xoverMode", static_cast<int32_t>(crossover_.getFilterMode()));
339 w.write("xoverStructure", static_cast<int32_t>(crossover_.getStructure()));
340 w.write("xoverTopology", static_cast<int32_t>(crossover_.getTopology()));
341 w.write("mix", static_cast<float>(getMix()));
342 w.write("outputClip", getOutputClip());
343 w.write("outputCeiling", static_cast<float>(getOutputCeiling()));
344 char key[32];
345 for (int i = 0; i < n - 1; ++i)
346 {
347 std::snprintf(key, sizeof(key), "x%d", i);
348 w.write(key, static_cast<float>(crossover_.getCrossoverFrequency(i)));
349 }
350 for (int i = 0; i < n; ++i)
351 {
352 std::snprintf(key, sizeof(key), "band%d", i);
353 w.write(key, getBandSaturation(i).getState());
354 std::snprintf(key, sizeof(key), "b%d.level", i);
355 w.write(key, static_cast<float>(getBandLevel(i)));
356 std::snprintf(key, sizeof(key), "b%d.pan", i);
357 w.write(key, static_cast<float>(getBandPan(i)));
358 std::snprintf(key, sizeof(key), "b%d.mute", i);
359 w.write(key, isBandMuted(i));
360 std::snprintf(key, sizeof(key), "b%d.solo", i);
361 w.write(key, isBandSoloed(i));
362 }
363 return w.blob();
364 }
365
367 bool setState(const uint8_t* data, size_t size)
368 {
369 StateReader r(data, size);
370 if (!r.isValid() || r.processorId() != stateId("MBST")) return false;
371 const int n = std::clamp(static_cast<int>(r.read("numBands", int32_t(3))), 2, MaxBands);
372 setNumBands(n);
373 setOrder(r.read("order", int32_t(24)));
374 setCrossoverMode(static_cast<typename Crossover::FilterMode>(std::clamp(static_cast<int>(r.read("xoverMode", int32_t(0))), 0, 1)));
375 setCrossoverStructure(static_cast<typename Crossover::Structure>(std::clamp(static_cast<int>(r.read("xoverStructure", int32_t(0))), 0, 1)));
376 setCrossoverTopology(static_cast<typename Crossover::Topology>(std::clamp(static_cast<int>(r.read("xoverTopology", int32_t(0))), 0, 1)));
377 setMix(static_cast<T>(r.read("mix", 1.0f)));
378 setOutputClip(r.read("outputClip", false));
379 setOutputCeiling(static_cast<T>(r.read("outputCeiling", 0.0f)));
380 char key[32];
381 for (int i = 0; i < n - 1; ++i)
382 {
383 std::snprintf(key, sizeof(key), "x%d", i);
384 const float f = r.read(key, -1.0f);
385 if (f > 0.0f) setCrossoverFrequency(i, static_cast<T>(f));
386 }
387 for (int i = 0; i < n; ++i)
388 {
389 std::snprintf(key, sizeof(key), "band%d", i);
390 const auto nested = r.readBlob(key);
391 if (!nested.empty()) getBandSaturation(i).setState(nested.data(), nested.size());
392 std::snprintf(key, sizeof(key), "b%d.level", i);
393 setBandLevel(i, static_cast<T>(r.read(key, 0.0f)));
394 std::snprintf(key, sizeof(key), "b%d.pan", i);
395 setBandPan(i, static_cast<T>(r.read(key, 0.0f)));
396 std::snprintf(key, sizeof(key), "b%d.mute", i);
397 setBandMute(i, r.read(key, false));
398 std::snprintf(key, sizeof(key), "b%d.solo", i);
399 setBandSolo(i, r.read(key, false));
400 }
401 return true;
402 }
403
404private:
405 static constexpr double kFadeSeconds = 0.02;
406 static constexpr int kPadSize = 512;
407
408 struct BandParams
409 {
410 std::atomic<T> levelDb { T(0) };
411 std::atomic<T> pan { T(0) };
412 std::atomic<bool> mute { false };
413 std::atomic<bool> solo { false };
414 };
415
417 struct BandRamp
418 {
419 double heard = 1.0;
420 double left = 1.0, right = 1.0;
421 bool computed = true;
422 };
423
424 struct Targets
425 {
426 double heard, left, right;
427 };
428
429 [[nodiscard]] const BandParams& param(int band) const noexcept
430 {
431 return params_[static_cast<size_t>(std::clamp(band, 0, MaxBands - 1))];
432 }
433
434 [[nodiscard]] bool anySolo() const noexcept
435 {
436 const int n = crossover_.getNumBands();
437 for (int b = 0; b < n; ++b)
438 if (params_[static_cast<size_t>(b)].solo.load(std::memory_order_relaxed)) return true;
439 return false;
440 }
441
443 [[nodiscard]] Targets targets(int band, bool soloing) const noexcept
444 {
445 const BandParams& p = params_[static_cast<size_t>(band)];
446 const bool heard = !p.mute.load(std::memory_order_relaxed)
447 && (!soloing || p.solo.load(std::memory_order_relaxed));
448 const double level = std::pow(10.0, static_cast<double>(p.levelDb.load(std::memory_order_relaxed)) / 20.0);
449 const double pan = static_cast<double>(p.pan.load(std::memory_order_relaxed));
450 const double left = (pan > 0.0) ? std::cos(0.5 * std::numbers::pi * pan) : 1.0;
451 const double right = (pan < 0.0) ? std::cos(-0.5 * std::numbers::pi * pan) : 1.0;
452 return { heard ? 1.0 : 0.0, level * left, level * right };
453 }
454
456 [[nodiscard]] int slowestBand(int n) const noexcept
457 {
458 int slowest = 0;
459 for (int b = 0; b < std::clamp(n, 0, MaxBands); ++b)
460 slowest = std::max(slowest, saturation_[static_cast<size_t>(b)]->getLatency());
461 return std::min(slowest, kPadSize - 1);
462 }
463
464 void processChunk(AudioBufferView<T> buffer) noexcept
465 {
466 const int nCh = std::min(buffer.getNumChannels(), bandBuffers_[0].getNumChannels());
467 const int nS = std::min(buffer.getNumSamples(), bandBuffers_[0].getNumSamples());
468 if (nCh <= 0 || nS <= 0) return;
469
470 for (int b = 0; b < MaxBands; ++b)
471 views_[static_cast<size_t>(b)] = bandBuffers_[static_cast<size_t>(b)].toView().getSubView(0, nS);
472 const int nb = crossover_.processBlock(buffer, views_.data(), MaxBands);
473 if (nb < 2) return;
474
475 const bool soloing = anySolo();
476 const int slowest = slowestBand(nb);
477
478 // The dry side of the mix: the dry side of each band, waiting for the
479 // slowest band as the band itself does, summed without level, pan or
480 // mute. It runs while the mix is, or is on its way, under one.
481 const double mixTarget = static_cast<double>(mix_.load(std::memory_order_relaxed));
482 const bool dry = mixTarget < 1.0 || mixNow_ < 1.0;
483 if (dry && !dryRunning_)
484 for (int b = 0; b < MaxBands; ++b) clearPad(MaxBands + b);
485 dryRunning_ = dry;
486 auto dryBand = bandDry_.toView().getSubView(0, nS);
487 if (dry)
488 for (int ch = 0; ch < nCh; ++ch) std::fill_n(dryBuffer_.getChannel(ch), nS, T(0));
489 for (int ch = 0; ch < nCh; ++ch) std::fill_n(buffer.getChannel(ch), nS, T(0));
490
491 for (int b = 0; b < nb; ++b)
492 {
493 BandRamp& ramp = ramps_[static_cast<size_t>(b)];
494 const Targets t = targets(b, soloing);
495
496 // A band that is not heard, and has faded out, is not computed,
497 // unless the mix wants its dry side.
498 const bool silent = t.heard == 0.0 && ramp.heard == 0.0;
499 if (silent && !dry)
500 {
501 ramp.computed = false;
502 ramp.left = t.left;
503 ramp.right = t.right;
504 continue;
505 }
506 Saturation<T>& sat = *saturation_[static_cast<size_t>(b)];
507 if (!ramp.computed)
508 {
509 // Back from silence: its history is of another time.
510 sat.reset();
511 clearPad(b);
512 ramp.computed = true;
513 }
514 auto view = views_[static_cast<size_t>(b)];
515 if (dry) sat.processPieces(view, &dryBand, mixTarget > 0.0);
516 else sat.processBlock(view);
517 const int pad = slowest - sat.getLatency();
518 if (dry) addDryBand(b, nCh, nS, pad);
519
520 const double heardStep = std::clamp(t.heard - ramp.heard, -fadeStep_, fadeStep_);
521 const bool fading = ramp.heard != t.heard;
522 const double leftStep = (t.left - ramp.left) * fadeStep_;
523 const double rightStep = (t.right - ramp.right) * fadeStep_;
524 for (int ch = 0; ch < nCh; ++ch)
525 {
526 const T* in = view.getChannel(ch);
527 T* out = buffer.getChannel(ch);
528 T* line = padLines_.data() + (static_cast<size_t>(b) * static_cast<size_t>(bandBuffers_[0].getNumChannels())
529 + static_cast<size_t>(ch)) * static_cast<size_t>(kPadSize);
530 double heard = ramp.heard, left = ramp.left, right = ramp.right;
531 const bool isRight = (ch == 1);
532 int pos = padPos_;
533 for (int i = 0; i < nS; ++i)
534 {
535 // Every band is delayed to the slowest one's latency.
536 line[pos] = in[i];
537 const T aligned = line[(pos - pad) & (kPadSize - 1)];
538 pos = (pos + 1) & (kPadSize - 1);
539 if (fading)
540 {
541 heard += heardStep;
542 if ((heardStep > 0.0 && heard > t.heard) || (heardStep < 0.0 && heard < t.heard)) heard = t.heard;
543 }
544 left += leftStep;
545 right += rightStep;
546 const double side = (nCh == 2) ? (isRight ? right : left) : 0.5 * (left + right);
547 out[i] += static_cast<T>(static_cast<double>(aligned) * heard * side);
548 }
549 if (ch == nCh - 1)
550 {
551 ramp.heard = heard;
552 ramp.left = left;
553 ramp.right = right;
554 }
555 }
556 }
557 // Bands past the count (after a change of it) are left as they were.
558 padPos_ = (padPos_ + nS) & (kPadSize - 1);
559 for (int b = nb; b < MaxBands; ++b) ramps_[static_cast<size_t>(b)].computed = false;
560
561 // The mix, gliding like the levels do.
562 if (dry)
563 {
564 const double step = std::clamp(mixTarget - mixNow_, -fadeStep_, fadeStep_);
565 double mix = mixNow_;
566 for (int ch = 0; ch < nCh; ++ch)
567 {
568 const T* d = dryBuffer_.getChannel(ch);
569 T* out = buffer.getChannel(ch);
570 mix = mixNow_;
571 for (int i = 0; i < nS; ++i)
572 {
573 if (mix != mixTarget)
574 {
575 mix += step;
576 if ((step > 0.0 && mix > mixTarget) || (step < 0.0 && mix < mixTarget)) mix = mixTarget;
577 }
578 out[i] = static_cast<T>(static_cast<double>(d[i]) + mix * (static_cast<double>(out[i]) - static_cast<double>(d[i])));
579 }
580 }
581 mixNow_ = mix;
582 }
583
584 // The clipper after the sum, and no sample over its ceiling (its
585 // filters ring a little past it).
586 const bool clip = outputClip_.load(std::memory_order_relaxed);
587 if (clip && !clipping_) clip_.reset();
588 clipping_ = clip;
589 if (clip)
590 {
591 clip_.processBlock(buffer.getSubView(0, nS));
592 const T top = decibelsToGain(outputCeilingDb_.load(std::memory_order_relaxed));
593 for (int ch = 0; ch < nCh; ++ch)
594 {
595 T* out = buffer.getChannel(ch);
596 for (int i = 0; i < nS; ++i) out[i] = std::clamp(out[i], -top, top);
597 }
598 }
599 }
600
602 void addDryBand(int band, int channels, int samples, int pad) noexcept
603 {
604 for (int ch = 0; ch < channels; ++ch)
605 {
606 const T* in = bandDry_.getChannel(ch);
607 T* sum = dryBuffer_.getChannel(ch);
608 T* line = padLines_.data() + (static_cast<size_t>(MaxBands + band) * static_cast<size_t>(bandBuffers_[0].getNumChannels())
609 + static_cast<size_t>(ch)) * static_cast<size_t>(kPadSize);
610 int pos = padPos_;
611 for (int i = 0; i < samples; ++i)
612 {
613 line[pos] = in[i];
614 sum[i] += line[(pos - pad) & (kPadSize - 1)];
615 pos = (pos + 1) & (kPadSize - 1);
616 }
617 }
618 }
619
620 void clearPad(int band) noexcept
621 {
622 const size_t per = static_cast<size_t>(bandBuffers_[0].getNumChannels()) * static_cast<size_t>(kPadSize);
623 std::fill_n(padLines_.data() + static_cast<size_t>(band) * per, per, T(0));
624 }
625
626 std::atomic<bool> prepared_ { false };
627 AudioSpec spec_ {};
628 Crossover crossover_;
629 std::array<std::unique_ptr<Saturation<T>>, MaxBands> saturation_ {};
630 std::array<AudioBuffer<T>, MaxBands> bandBuffers_ {};
631 std::array<AudioBufferView<T>, MaxBands> views_ {};
632 std::array<BandParams, MaxBands> params_ {};
633 std::array<BandRamp, MaxBands> ramps_ {};
634 std::vector<T> padLines_;
635 int padPos_ = 0;
636 AudioBuffer<T> dryBuffer_, bandDry_;
637 Clipper<T> clip_;
638 std::atomic<T> mix_ { T(1) }, outputCeilingDb_ { T(0) };
639 std::atomic<bool> outputClip_ { false };
640 double mixNow_ = 1.0;
641 bool dryRunning_ = false, clipping_ = false;
642 double fadeStep_ = 1.0 / 960.0;
643};
644
645} // namespace dspark
Non-owning view over audio channel data.
Definition AudioBuffer.h:53
Real-time audio clipper with analog modeling and anti-aliasing features.
Definition Clipper.h:92
Linkwitz-Riley crossover with 2-12 bands: 6, 12, 24, 36 or 48 dB per octave.
int getLatency() const noexcept
Returns latency in samples: 0 for IIR; for linear-phase, three quarters of the FIR (3072 at 48 kHz),...
void setStructure(Structure structure) noexcept
Chooses how the sections of the minimum-phase engine are realised.
void setNumBandsKeepingSplits(int n) noexcept
Sets the number of output bands (2..MaxBands) and keeps the splits.
void setOrder(int order) noexcept
Sets the crossover slope: 6, 12, 24, 36 or 48 (dB/oct). Other values are ignored.
int getNumBands() const noexcept
void setNumBands(int n) noexcept
Sets the number of output bands (2..MaxBands).
void setTopology(Topology topology) noexcept
Arranges the splits of the minimum-phase engine.
Structure
How the minimum-phase sections are realised (see setStructure()).
void setCrossoverFrequency(int index, T freqHz) noexcept
Sets a crossover frequency. Automatically maintains sorting.
T getCrossoverFrequency(int index) const noexcept
Returns the target frequency of split point index.
int processBlock(AudioBufferView< T > input, AudioBufferView< T > *bandOutputs, int numOutputBands) noexcept
Splits input into separate band outputs.
FilterMode
Filter processing mode.
FilterMode getFilterMode() const noexcept
Topology
How the splits of the minimum-phase engine are arranged (see setTopology()).
int getOrder() const noexcept
Topology getTopology() const noexcept
The arrangement set by setTopology().
void setFilterMode(FilterMode mode) noexcept
Sets the processing mode. A live switch resets filter state (transient).
Structure getStructure() const noexcept
The structure set by setStructure().
void prepare(const AudioSpec &spec)
Prepares the crossover for processing.
Multi-band saturation: crossover split, a Saturation per band, sum.
Crossover::Topology getCrossoverTopology() const noexcept
T getBandPan(int band) const noexcept
void setOversamplingMode(typename Saturation< T >::OversamplingMode mode)
The oversampler filters of every band (NOT real-time safe).
T getCrossoverFrequency(int index) const noexcept
void setOversampling(int factor)
The oversampling factor of every band (NOT real-time safe; see Saturation::setOversampling()).
MultibandSaturation(const MultibandSaturation &)=delete
void setAntialiasing(typename Saturation< T >::Antialiasing mode) noexcept
The antialiasing order of every band.
int getLatency() const noexcept
Latency in samples: the crossover's and the slowest band's.
void processBlock(AudioBufferView< T > buffer) noexcept
Processes audio through the bands, in place.
const Saturation< T > & getBandSaturation(int band) const noexcept
The Saturation of a band (the index is clamped).
void prepare(const AudioSpec &spec)
Prepares the crossover, every band and the buffers.
bool getOutputClip() const noexcept
void setBandLevel(int band, T dB) noexcept
Level of a band in dB, -60 to +24 (any thread; glides over 20 ms).
void setBandSolo(int band, bool on) noexcept
Solos a band: while any band is soloed only the soloed ones are heard (any thread; fades over 20 ms).
CrossoverFilter< T, MaxBands > Crossover
void setCrossoverTopology(typename Crossover::Topology topology) noexcept
Cascade or balanced arrangement of the minimum-phase splits (any thread; see CrossoverFilter::setTopo...
bool setState(const uint8_t *data, size_t size)
Restores what getState() wrote (setup thread: the bands may reallocate).
void setNumBandsKeepingSplits(int n) noexcept
Number of bands keeping the split frequencies (any thread; see CrossoverFilter::setNumBandsKeepingSpl...
T getBandLevel(int band) const noexcept
void setBandPan(int band, T pan) noexcept
Pan of a band, -1 (left) to +1 (right); a balance that never lifts a side (any thread).
void setOrder(int order) noexcept
Slope of the crossover in dB per octave (any thread; see CrossoverFilter::setOrder()).
void reset() noexcept
Clears the crossover, the bands and the alignment delays.
MultibandSaturation & operator=(const MultibandSaturation &)=delete
void setBandDrive(int band, T dB)
The drive of a band in dB (any thread).
Saturation< T > & getBandSaturation(int band) noexcept
The Saturation of a band (the index is clamped).
void setCrossoverMode(typename Crossover::FilterMode mode) noexcept
Minimum-phase (IIR) or linear-phase crossover (any thread; see CrossoverFilter).
void setNumBands(int n) noexcept
Number of bands (2..MaxBands; any thread; re-spaces the split frequencies; a live change fades,...
void setOversamplingBand(typename Saturation< T >::OversamplingBand band)
The band the first stage of every band's oversampler keeps (NOT real-time safe; see Saturation::setOv...
void setBandAlgorithm(int band, typename Saturation< T >::Algorithm algo)
The algorithm of a band (any thread).
bool isBandMuted(int band) const noexcept
void setOutputClip(bool on) noexcept
An oversampled clipper after the sum (off by default; any thread).
bool isBandComputed(int band) const noexcept
Whether a band is computed now (false while it is not heard; audio-thread state).
void setMix(T amount) noexcept
Blends the sum of the bands with the programme, 0 (dry) to 1 (the default: the bands alone; any threa...
void setBandMute(int band, bool on) noexcept
Mutes a band (any thread; fades over 20 ms).
Crossover::Structure getCrossoverStructure() const noexcept
void setOutputCeiling(T dB) noexcept
Ceiling of the output clipper, -24 to 0 dBFS (default 0; any thread).
void setCrossoverFrequency(int index, T hz) noexcept
Sets a split frequency (any thread).
void setCrossoverStructure(typename Crossover::Structure structure) noexcept
Direct-form or ladder sections in the minimum-phase crossover (any thread; see CrossoverFilter::setSt...
bool isBandSoloed(int band) const noexcept
Crossover::FilterMode getCrossoverMode() const noexcept
std::vector< uint8_t > getState() const
Serializes the crossover, each band's Saturation and its level, pan, mute and solo.
Professional multi-algorithm saturation processor with analog simulation.
typename Oversampling< SampleType >::Band OversamplingBand
The band the oversampler's first stage keeps (see setOversamplingBand()).
Algorithm
Defines the harmonic generation topology.
Antialiasing
Antiderivative antialiasing order (see setAntialiasing()).
Tolerant reader: missing keys yield defaults, unknown keys are skipped.
Definition StateBlob.h:171
std::vector< uint8_t > readBlob(const char *key) const
Reads a nested blob; empty when the key is absent.
Definition StateBlob.h:242
float read(const char *key, float defaultValue) const
Reads a float, or defaultValue when the key is absent.
Definition StateBlob.h:214
bool isValid() const noexcept
Definition StateBlob.h:209
uint32_t processorId() const noexcept
Definition StateBlob.h:210
Serializes key/value parameters into a versioned blob.
Definition StateBlob.h:53
std::vector< uint8_t > blob() const
Finalizes and returns the blob.
Definition StateBlob.h:112
void write(const char *key, float value)
Writes a float parameter.
Definition StateBlob.h:71
Main namespace for the DSPark framework.
T decibelsToGain(T dB, T minusInfinityDb=T(-100)) noexcept
Converts a value in decibels to linear gain.
Definition DspMath.h:77
constexpr uint32_t stateId(const char(&tag)[5]) noexcept
Builds a FOURCC processor id, e.g. dspark::stateId("COMP").
Definition StateBlob.h:672
Describes the audio environment for a DSP processor.
Definition AudioSpec.h:37
constexpr bool isValid() const noexcept
Checks if the specification contains valid, processable parameters.
Definition AudioSpec.h:71
int numChannels
Number of audio channels (e.g., 1 = mono, 2 = stereo).
Definition AudioSpec.h:58
int maxBlockSize
Maximum number of samples per processing block.
Definition AudioSpec.h:53
double sampleRate
Sample rate in Hz.
Definition AudioSpec.h:45