32#include "CrossoverFilter.h"
33#include "Compressor.h"
34#include "../Core/AudioBuffer.h"
35#include "../Core/AudioSpec.h"
36#include "../Core/DspMath.h"
37#include "../Core/SimdOps.h"
38#include "../Core/StateBlob.h"
58template <FloatType T,
int MaxBands = 12>
61 static_assert(MaxBands >= 2,
"MultibandCompressor needs at least 2 bands (one split)");
79 prepared_.store(
false, std::memory_order_relaxed);
81 crossover_.prepare(spec);
82 for (
int b = 0; b < MaxBands; ++b)
85 compressors_[b].prepare(spec);
87 lastNumBands_ = MaxBands;
89 prepared_.store(
true, std::memory_order_relaxed);
104 if (!prepared_.load(std::memory_order_relaxed))
return;
106 const int total = buffer.getNumSamples();
107 const int chunk = bandBuffers_[0].getNumSamples();
108 if (chunk <= 0)
return;
109 for (
int offset = 0; offset < total; offset += chunk)
110 processChunk(buffer.getSubView(offset, std::min(chunk, total - offset)));
120 const int nCh = std::min(buffer.getNumChannels(), bandBuffers_[0].getNumChannels());
121 const int nS = std::min(buffer.getNumSamples(), bandBuffers_[0].getNumSamples());
122 if (nCh <= 0 || nS <= 0)
return;
129 for (
int b = 0; b < MaxBands; ++b)
130 views_[b] = bandBuffers_[b].toView().getSubView(0, nS);
133 const int nb = crossover_.processBlock(buffer, views_.data(), MaxBands);
139 if (nb > lastNumBands_)
140 for (
int b = lastNumBands_; b < nb; ++b)
141 compressors_[b].
reset();
152 for (
int b = 0; b < nb; ++b)
156 for (
int ch = 0; ch < nCh; ++ch)
158 T*
const __restrict out = buffer.getChannel(ch);
159 const T*
const __restrict src0 = bandBuffers_[0].getChannel(ch);
162 std::copy(src0, src0 + nS, out);
165 for (
int b = 1; b < nb; ++b)
166 simd::add(out, bandBuffers_[b].getChannel(ch), nS);
187 crossover_.setNumBands(std::clamp(n, 2, MaxBands));
197 crossover_.setNumBandsKeepingSplits(std::clamp(n, 2, MaxBands));
207 crossover_.setCrossoverFrequency(index, freqHz);
216 crossover_.setOrder(order);
230 crossover_.setFilterMode(mode);
240 crossover_.setStructure(structure);
250 crossover_.setTopology(topology);
262 assert(band >= 0 && band < MaxBands);
263 int safeBand = std::clamp(band, 0, MaxBands - 1);
264 return compressors_[safeBand];
274 assert(band >= 0 && band < MaxBands);
275 int safeBand = std::clamp(band, 0, MaxBands - 1);
276 return compressors_[safeBand];
289 for (
auto& compressor : compressors_) compressor.setMix(amount);
301 if (band >= 0 && band < MaxBands)
302 compressors_[band].setThreshold(dB);
312 if (band >= 0 && band < MaxBands)
313 compressors_[band].setRatio(ratio);
323 if (band >= 0 && band < MaxBands)
324 compressors_[band].setAttack(ms);
334 if (band >= 0 && band < MaxBands)
335 compressors_[band].setRelease(ms);
351 if (band < 0 || band >= MaxBands)
return T(0);
352 return compressors_[band].getGainReductionDb();
356 [[nodiscard]]
int getNumBands() const noexcept {
return crossover_.getNumBands(); }
361 return crossover_.getCrossoverFrequency(index);
365 [[nodiscard]]
int getOrder() const noexcept {
return crossover_.getOrder(); }
370 return crossover_.getFilterMode();
376 return crossover_.getStructure();
382 return crossover_.getTopology();
391 const int nb = crossover_.getNumBands();
392 for (
int b = 0; b < nb && b < MaxBands; ++b)
393 maxBand = std::max(maxBand, compressors_[b].
getLatency());
394 return crossover_.getLatency() + maxBand;
401 for (
auto& c : compressors_) c.reset();
402 lastNumBands_ = MaxBands;
406 [[nodiscard]] std::vector<uint8_t>
getState()
const
409 const int n = crossover_.getNumBands();
410 w.
write(
"numBands", n);
411 w.
write(
"order", crossover_.getOrder());
412 w.
write(
"xoverMode",
static_cast<int32_t
>(crossover_.getFilterMode()));
413 w.
write(
"xoverStructure",
static_cast<int32_t
>(crossover_.getStructure()));
414 w.
write(
"xoverTopology",
static_cast<int32_t
>(crossover_.getTopology()));
416 for (
int i = 0; i < n - 1; ++i)
418 std::snprintf(key,
sizeof(key),
"x%d", i);
419 w.
write(key,
static_cast<float>(crossover_.getCrossoverFrequency(i)));
421 for (
int i = 0; i < n; ++i)
423 std::snprintf(key,
sizeof(key),
"band%d", i);
434 const int n = std::clamp(r.
read(
"numBands", 3), 2, MaxBands);
438 r.
read(
"xoverMode", 0)));
440 std::clamp(r.
read(
"xoverStructure", 0), 0, 1)));
442 std::clamp(r.
read(
"xoverTopology", 0), 0, 1)));
444 for (
int i = 0; i < n - 1; ++i)
446 std::snprintf(key,
sizeof(key),
"x%d", i);
447 const float f = r.
read(key, -1.0f);
450 for (
int i = 0; i < n; ++i)
452 std::snprintf(key,
sizeof(key),
"band%d", i);
453 const auto nested = r.
readBlob(key);
461 std::atomic<bool> prepared_ {
false };
462 int lastNumBands_ = MaxBands;
463 CrossoverFilter<T, MaxBands> crossover_;
464 std::array<Compressor<T>, MaxBands> compressors_ {};
465 std::array<AudioBuffer<T>, MaxBands> bandBuffers_ {};
466 std::array<AudioBufferView<T>, MaxBands> views_ {};
Non-owning view over audio channel data.
High-fidelity modular compressor designed for real-time applications.
Structure
How the minimum-phase sections are realised (see setStructure()).
FilterMode
Filter processing mode.
Topology
How the splits of the minimum-phase engine are arranged (see setTopology()).
Multi-band compressor: crossover split, per-band compression, sum.
void setNumBands(int n) noexcept
Sets the number of active frequency bands.
void setBandRatio(int band, T ratio) noexcept
Sets the ratio for a specific band.
const Compressor< T > & getBandCompressor(int band) const noexcept
Direct constant access to a band's compressor for state queries.
void setOrder(int order) noexcept
Sets the crossover slope in dB/oct: 12, 24 or 48.
void setCrossoverTopology(typename CrossoverFilter< T, MaxBands >::Topology topology) noexcept
Cascade or balanced arrangement of the minimum-phase splits (see CrossoverFilter::setTopology(); any ...
void processBlock(AudioBufferView< T > buffer) noexcept
Processes audio through the multi-band compressor.
bool setState(const uint8_t *data, size_t size)
Restores topology and band compressors from a blob.
void setCrossoverFrequency(int index, T freqHz) noexcept
Sets the crossover frequency for a specific split point.
std::vector< uint8_t > getState() const
Serializes crossover topology and per-band compressor states.
void setCrossoverMode(typename CrossoverFilter< T, MaxBands >::FilterMode mode) noexcept
Sets the phase/processing mode of the crossover (IIR or linear-phase).
void setNumBandsKeepingSplits(int n) noexcept
Sets the number of active bands keeping the split frequencies (see CrossoverFilter::setNumBandsKeepin...
void setBandThreshold(int band, T dB) noexcept
Sets the threshold for a specific band.
T getCrossoverFrequency(int index) const noexcept
Returns the target frequency of split point index in Hz.
int getLatency() const noexcept
Returns the total latency of the multi-band system.
int getNumBands() const noexcept
Returns the current number of active bands.
void setBandAttack(int band, T ms) noexcept
Sets the attack time for a specific band.
void prepare(const AudioSpec &spec)
Prepares the multiband compressor and internal buffers for processing.
T getBandGainReductionDb(int band) const noexcept
Gets the current gain reduction applied to a specific band.
Compressor< T > & getBandCompressor(int band) noexcept
Direct access to a band's compressor for full configuration.
void reset() noexcept
Resets all internal states (envelopes, delay lines, etc.).
CrossoverFilter< T, MaxBands >::FilterMode getCrossoverMode() const noexcept
Returns the crossover processing mode (IIR or linear-phase).
void setCrossoverStructure(typename CrossoverFilter< T, MaxBands >::Structure structure) noexcept
How the sections of the minimum-phase crossover are realised (see CrossoverFilter::setStructure(); an...
CrossoverFilter< T, MaxBands >::Topology getCrossoverTopology() const noexcept
Returns the arrangement of the minimum-phase splits.
int getOrder() const noexcept
Returns the crossover slope in dB/oct (12, 24 or 48).
CrossoverFilter< T, MaxBands >::Structure getCrossoverStructure() const noexcept
Returns the structure of the minimum-phase crossover.
void setMix(T amount) noexcept
The mix of every band at once, 0 (dry) to 1 (any thread).
void setBandRelease(int band, T ms) noexcept
Sets the release time for a specific band.
Tolerant reader: missing keys yield defaults, unknown keys are skipped.
std::vector< uint8_t > readBlob(const char *key) const
Reads a nested blob; empty when the key is absent.
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.
void add(float *DSPARK_RESTRICT dst, const float *DSPARK_RESTRICT src, int count) noexcept
Adds source samples into a destination buffer (no scaling).
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.
constexpr bool isValid() const noexcept
Checks if the specification contains valid, processable parameters.
int numChannels
Number of audio channels (e.g., 1 = mono, 2 = stereo).
int maxBlockSize
Maximum number of samples per processing block.