DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::CrossoverFilter< T, MaxBands > Class Template Reference

Linkwitz-Riley crossover with 2-12 bands: 6, 12, 24, 36 or 48 dB per octave. More...

#include <CrossoverFilter.h>

Public Types

enum class  FilterMode { MinimumPhase , LinearPhase }
 Filter processing mode. More...
 
enum class  Structure { DirectForm , Ladder }
 How the minimum-phase sections are realised (see setStructure()). More...
 
enum class  Topology { Cascade , Balanced }
 How the splits of the minimum-phase engine are arranged (see setTopology()). More...
 

Public Member Functions

 CrossoverFilter ()
 
void prepare (const AudioSpec &spec)
 Prepares the crossover for processing.
 
int processBlock (AudioBufferView< T > input, AudioBufferView< T > *bandOutputs, int numOutputBands) noexcept
 Splits input into separate band outputs.
 
void setNumBands (int n) noexcept
 Sets the number of output bands (2..MaxBands).
 
void setNumBandsKeepingSplits (int n) noexcept
 Sets the number of output bands (2..MaxBands) and keeps the splits.
 
void setCrossoverFrequency (int index, T freqHz) noexcept
 Sets a crossover frequency. Automatically maintains sorting.
 
void setOrder (int order) noexcept
 Sets the crossover slope: 6, 12, 24, 36 or 48 (dB/oct). Other values are ignored.
 
void setFilterMode (FilterMode mode) noexcept
 Sets the processing mode. A live switch resets filter state (transient).
 
void setStructure (Structure structure) noexcept
 Chooses how the sections of the minimum-phase engine are realised.
 
Structure getStructure () const noexcept
 The structure set by setStructure().
 
void setTopology (Topology topology) noexcept
 Arranges the splits of the minimum-phase engine.
 
Topology getTopology () const noexcept
 The arrangement set by setTopology().
 
int getNumBands () const noexcept
 
int getOrder () const noexcept
 
FilterMode getFilterMode () const noexcept
 
T getCrossoverFrequency (int index) const noexcept
 Returns the target frequency of split point index.
 
int getLatency () const noexcept
 Returns latency in samples: 0 for IIR; for linear-phase, three quarters of the FIR (3072 at 48 kHz), whatever the block size.
 
void reset () noexcept
 
std::vector< uint8_t > getState () const
 Serializes the split topology (setup/UI threads; allocates).
 
bool setState (const uint8_t *data, size_t size)
 Restores the split topology from a blob.
 

Detailed Description

template<FloatType T, int MaxBands = 12>
class dspark::CrossoverFilter< T, MaxBands >

Linkwitz-Riley crossover with 2-12 bands: 6, 12, 24, 36 or 48 dB per octave.

Template Parameters
TSample type (float or double).
MaxBandsMaximum number of output bands (compile-time, default 12).

Definition at line 149 of file CrossoverFilter.h.

Member Enumeration Documentation

◆ FilterMode

template<FloatType T, int MaxBands = 12>
enum class dspark::CrossoverFilter::FilterMode
strong

Filter processing mode.

Enumerator
MinimumPhase 

IIR biquads with allpass phase correction (zero latency).

LinearPhase 

FFT-based FIR crossover (introduces latency, zero phase distortion, introduces pre-ringing).

Definition at line 153 of file CrossoverFilter.h.

◆ Structure

template<FloatType T, int MaxBands = 12>
enum class dspark::CrossoverFilter::Structure
strong

How the minimum-phase sections are realised (see setStructure()).

Enumerator
DirectForm 

Direct form with a double memory, all sections in one pass (default).

Ladder 

Normalized ladders: passive under any movement of the splits.

Definition at line 556 of file CrossoverFilter.h.

◆ Topology

template<FloatType T, int MaxBands = 12>
enum class dspark::CrossoverFilter::Topology
strong

How the splits of the minimum-phase engine are arranged (see setTopology()).

Enumerator
Cascade 

Each split takes what the one below left above it (default).

Balanced 

The middle split first, each half divided the same way.

Definition at line 592 of file CrossoverFilter.h.

Constructor & Destructor Documentation

◆ CrossoverFilter()

template<FloatType T, int MaxBands = 12>
dspark::CrossoverFilter< T, MaxBands >::CrossoverFilter ( )
inline

Definition at line 159 of file CrossoverFilter.h.

Member Function Documentation

◆ getCrossoverFrequency()

template<FloatType T, int MaxBands = 12>
T dspark::CrossoverFilter< T, MaxBands >::getCrossoverFrequency ( int  index) const
inlinenoexcept

Returns the target frequency of split point index.

Definition at line 632 of file CrossoverFilter.h.

◆ getFilterMode()

template<FloatType T, int MaxBands = 12>
FilterMode dspark::CrossoverFilter< T, MaxBands >::getFilterMode ( ) const
inlinenoexcept

Definition at line 629 of file CrossoverFilter.h.

◆ getLatency()

template<FloatType T, int MaxBands = 12>
int dspark::CrossoverFilter< T, MaxBands >::getLatency ( ) const
inlinenoexcept

Returns latency in samples: 0 for IIR; for linear-phase, three quarters of the FIR (3072 at 48 kHz), whatever the block size.

Reports the linear-phase latency only when the engine actually exists (see prepare()); if linear-phase mode fell back to IIR, this returns 0.

Definition at line 645 of file CrossoverFilter.h.

◆ getNumBands()

template<FloatType T, int MaxBands = 12>
int dspark::CrossoverFilter< T, MaxBands >::getNumBands ( ) const
inlinenoexcept

Definition at line 627 of file CrossoverFilter.h.

◆ getOrder()

template<FloatType T, int MaxBands = 12>
int dspark::CrossoverFilter< T, MaxBands >::getOrder ( ) const
inlinenoexcept

Definition at line 628 of file CrossoverFilter.h.

◆ getState()

template<FloatType T, int MaxBands = 12>
std::vector< uint8_t > dspark::CrossoverFilter< T, MaxBands >::getState ( ) const
inline

Serializes the split topology (setup/UI threads; allocates).

Definition at line 681 of file CrossoverFilter.h.

◆ getStructure()

template<FloatType T, int MaxBands = 12>
Structure dspark::CrossoverFilter< T, MaxBands >::getStructure ( ) const
inlinenoexcept

The structure set by setStructure().

Definition at line 589 of file CrossoverFilter.h.

◆ getTopology()

template<FloatType T, int MaxBands = 12>
Topology dspark::CrossoverFilter< T, MaxBands >::getTopology ( ) const
inlinenoexcept

The arrangement set by setTopology().

Definition at line 623 of file CrossoverFilter.h.

◆ prepare()

template<FloatType T, int MaxBands = 12>
void dspark::CrossoverFilter< T, MaxBands >::prepare ( const AudioSpec &  spec)
inline

Prepares the crossover for processing.

Configured split frequencies, band count, order and mode are preserved across prepare() calls (configure-then-prepare is fully supported). The linear-phase engine is always allocated (so a later mode switch works); its size depends on the sample rate and channel count only.

Parameters
specAudio environment (sample rate, block size, channels). Invalid specs are ignored (previous state is kept).

Definition at line 177 of file CrossoverFilter.h.

◆ processBlock()

template<FloatType T, int MaxBands = 12>
int dspark::CrossoverFilter< T, MaxBands >::processBlock ( AudioBufferView< T >  input,
AudioBufferView< T > *  bandOutputs,
int  numOutputBands 
)
inlinenoexcept

Splits input into separate band outputs.

The processed span is clamped to the prepared maxBlockSize and to the shortest output view. The linear-phase engine buffers internally, so any block size up to maxBlockSize gives the same output. Input channels beyond the prepared/16-channel limit are passed through on band 0 (remaining bands silent) so the band sum still reconstructs the input.

Parameters
inputInput audio buffer.
bandOutputsArray of AudioBufferView, one per band.
numOutputBandsNumber of output bands (must match getNumBands(); fewer bands re-purpose the first splits only in IIR mode and is not a supported configuration).
Returns
The number of bands actually written THIS block (0 when nothing was written: unprepared, degenerate span, or null outputs). Callers that sum the bands back together should trust this value rather than a separately read getNumBands(): the band count is an atomic that a concurrent setNumBands() may move between the two reads, and views beyond the returned count are untouched.

Definition at line 334 of file CrossoverFilter.h.

◆ reset()

template<FloatType T, int MaxBands = 12>
void dspark::CrossoverFilter< T, MaxBands >::reset ( )
inlinenoexcept

Definition at line 651 of file CrossoverFilter.h.

◆ setCrossoverFrequency()

template<FloatType T, int MaxBands = 12>
void dspark::CrossoverFilter< T, MaxBands >::setCrossoverFrequency ( int  index,
T  freqHz 
)
inlinenoexcept

Sets a crossover frequency. Automatically maintains sorting.

Non-finite frequencies are ignored; values are floored to 1 Hz here and clamped to [20, 0.499 * sampleRate] at design time.

Definition at line 485 of file CrossoverFilter.h.

◆ setFilterMode()

template<FloatType T, int MaxBands = 12>
void dspark::CrossoverFilter< T, MaxBands >::setFilterMode ( FilterMode  mode)
inlinenoexcept

Sets the processing mode. A live switch resets filter state (transient).

Definition at line 547 of file CrossoverFilter.h.

◆ setNumBands()

template<FloatType T, int MaxBands = 12>
void dspark::CrossoverFilter< T, MaxBands >::setNumBands ( int  n)
inlinenoexcept

Sets the number of output bands (2..MaxBands).

Note
Changing the band count re-initialises ALL split frequencies to log-spaced defaults (100 Hz .. 10 kHz); reapply custom frequencies afterwards, or use setNumBandsKeepingSplits(). A live change goes over without a step: in the minimum-phase engine the old and the new topology run together for 20 ms and the bands move from one to the other (a band that leaves folds into the new top band, one that comes in rises from silence), so the band sum moves from one allpass to the other; in the linear-phase engine the new design comes in over a partition (see the file notes). Measured at LR24 with tones at 100 Hz, 3 kHz and 9 kHz, 3 to 4 bands: band 0's largest step 0.0050 against 0.0049 steady (0.072 when the change took one sample), the new top band's 0.187 against 0.184.

Definition at line 443 of file CrossoverFilter.h.

◆ setNumBandsKeepingSplits()

template<FloatType T, int MaxBands = 12>
void dspark::CrossoverFilter< T, MaxBands >::setNumBandsKeepingSplits ( int  n)
inlinenoexcept

Sets the number of output bands (2..MaxBands) and keeps the splits.

The split frequencies do not depend on the band count: the count only says how many of the splits are used. A split that stays keeps its frequency (and, in the minimum-phase engine, its memory); a split that comes back takes the frequency it had, or, when that is not above the split below it, the point halfway in octaves between that split and 20 kHz. The change itself goes over as setNumBands() describes. Thread-safe.

Definition at line 462 of file CrossoverFilter.h.

◆ setOrder()

template<FloatType T, int MaxBands = 12>
void dspark::CrossoverFilter< T, MaxBands >::setOrder ( int  order)
inlinenoexcept

Sets the crossover slope: 6, 12, 24, 36 or 48 (dB/oct). Other values are ignored.

12 to 48 are Linkwitz-Riley splits (the bands sum to an allpass of the input; each branch is 6 dB down at the split). 6 is a first-order low-pass and its complement (the bands sum to the input itself; each branch is 3 dB down at the split in the minimum-phase engine and 6 dB in the linear-phase one).

Definition at line 536 of file CrossoverFilter.h.

◆ setState()

template<FloatType T, int MaxBands = 12>
bool dspark::CrossoverFilter< T, MaxBands >::setState ( const uint8_t *  data,
size_t  size 
)
inline

Restores the split topology from a blob.

Definition at line 700 of file CrossoverFilter.h.

◆ setStructure()

template<FloatType T, int MaxBands = 12>
void dspark::CrossoverFilter< T, MaxBands >::setStructure ( Structure  structure)
inlinenoexcept

Chooses how the sections of the minimum-phase engine are realised.

Ladder runs every section of the splits and of the phase correction as a normalized ladder (NormalizedLadder): the same band responses (to 4e-9), and a memory that can only lose energy however its coefficients move. During a glide the ladders' coefficients ramp linearly from one refresh of the 32-sample grid to the next instead of stepping. Measured on four bands in stereo it costs 2.4 to 6.3 times the direct form. The 5 ms glide of the splits already keeps the direct form's memory consistent with its coefficients: with a split jumping between 100 Hz and 10 kHz every 64 samples, the band sum of 0.3 of noise peaks at 0.65 either way at LR24 (0.70 ladder, 0.64 direct at LR48), so the ladder is for those who want the guarantee rather than a lower peak. The ladders are allocated by prepare() on the heap, so the switch itself never allocates. Thread-safe; the switch restarts the sections' memory at the next block, which may click: choose it before playback. The linear-phase engine is unaffected.

Parameters
structureDirectForm (default) or Ladder.

Definition at line 582 of file CrossoverFilter.h.

◆ setTopology()

template<FloatType T, int MaxBands = 12>
void dspark::CrossoverFilter< T, MaxBands >::setTopology ( Topology  topology)
inlinenoexcept

Arranges the splits of the minimum-phase engine.

Cascade splits the lowest band off first and passes what is left on; each band then takes the phase correction of every split above it, S(S-1)/2 chains for S splits (10 with 6 bands, 55 with 12). Balanced splits at the middle split first and divides each half the same way; each branch takes the corrections of the splits of the other half once, before it is divided (6 chains with 6 bands, 22 with 12). The bands still sum to the same allpass, but their skirts differ: a band carries the slopes of the splits on its way down the tree rather than every split below it, so some skirts fall faster and others slower than in the cascade. A live change goes over without a step (both arrangements run for 20 ms on separate memories and the bands move from one to the other). Thread-safe. The linear-phase engine is unaffected.

Parameters
topologyCascade (default) or Balanced.

Definition at line 616 of file CrossoverFilter.h.


The documentation for this class was generated from the following file: