|
DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
|
Downward expander with ratio control, hysteresis, and sidechain. More...
#include <Expander.h>

Public Types | |
| enum class | State { Closed , Open , Hold } |
| using | KeyEq = KeyEqualizer< T, 16 > |
| The equalizer of the key (see setSidechainBand()). | |
| using | KeyBand = typename KeyEq::Band |
| The settings of one band of the key equalizer. | |
| using | KeyShape = typename KeyEq::Shape |
| What a band of the key equalizer does. | |
| using | KeyPlacement = typename KeyEq::Placement |
| The part of a two-channel key a band of the key equalizer acts on. | |
Public Member Functions | |
| ~Expander ()=default | |
| void | prepare (double sampleRate, int numChannels=2) noexcept |
| Initializes the expander with a specific sample rate. | |
| void | prepare (const AudioSpec &spec) noexcept |
| Initializes the expander using an AudioSpec struct. | |
| void | processBlock (AudioBufferView< T > buffer) noexcept |
| Processes an audio block in place (internal detector key). | |
| void | processBlock (AudioBufferView< T > buffer, AudioBufferView< T > sidechain) noexcept |
| Processes an audio block in place, keyed by an external sidechain. | |
| void | setThreshold (T dB) noexcept |
| Sets the threshold in decibels. | |
| void | setRatio (T ratio) noexcept |
| Sets the expansion ratio (e.g., 4.0 for 4:1). | |
| void | setHysteresis (T dB) noexcept |
| Sets the hysteresis gap in decibels to prevent chattering. | |
| void | setRange (T dB) noexcept |
| Sets the maximum gain reduction limit in decibels (<= 0). | |
| void | setAttack (T ms) noexcept |
| Sets the attack time in milliseconds. | |
| void | setHold (T ms) noexcept |
| Sets the hold time in milliseconds before release begins. | |
| void | setRelease (T ms) noexcept |
| Sets the release time in milliseconds. | |
| void | setSidechainHPF (bool enabled, double cutoffHz=80.0) noexcept |
| Enables and configures the sidechain high-pass filter. | |
| void | setSidechainBand (int band, const KeyBand &settings) noexcept |
| Sets a band of the equalizer of the key. | |
| KeyBand | getSidechainBand (int band) const noexcept |
| The settings of a band of the key equalizer. | |
| KeyEq::Response | getSidechainResponse () const noexcept |
| The curve of the key equalizer, for a display. | |
| void | setSidechainListen (bool on) noexcept |
| Plays the key instead of the expanded audio. | |
| bool | isSidechainListen () const noexcept |
| Whether the key is being played instead of the audio. | |
| void | setLookahead (T ms) noexcept |
| Sets the lookahead: the audio is delayed so the gain moves ahead of it. 0 (default) is latency-free. | |
| T | getLookahead () const noexcept |
| Returns the lookahead in milliseconds. | |
| void | setAutoThreshold (bool on) noexcept |
| Counts the threshold from the key's own recent level. | |
| bool | isAutoThreshold () const noexcept |
| Whether the threshold counts from the key's recent level (see setAutoThreshold()). | |
| int | getLatency () const noexcept |
| Latency in samples (the lookahead), correct right after setLookahead(). | |
| State | getGateState () const noexcept |
| T | getCurrentGainDb () const noexcept |
| void | reset () noexcept |
| Resets all internal DSP states to prevent clicks on playback start. | |
| std::vector< uint8_t > | getState () const |
| Serializes the parameter state (setup/UI threads; allocates). | |
| bool | setState (const uint8_t *data, size_t size) |
| Restores parameters from a blob (tolerant; rejects foreign ids). | |
Protected Member Functions | |
| void | cacheParams () noexcept |
| int | lookaheadSamplesFor (T ms) const noexcept |
| template<bool UseHPF> | |
| T | keyInput (T raw, int ch) noexcept |
| One sample of a channel's key before its equalizer: made finite and, when UseHPF, through the sidechain high-pass. | |
| template<bool UseHPF, bool UseSc> | |
| void | processBlockInternal (AudioBufferView< T > buffer, const AudioBufferView< T > *sidechain) noexcept |
| Internal processing loop templated on HPF/sidechain to avoid branching. | |
| void | publishMeters () noexcept |
| Publishes the metering words for cross-thread readout. | |
| void | updateStateMachine (T levelDb) noexcept |
| T | computeGain (T levelDb) noexcept |
| void | updateCoefficients () noexcept |
Protected Attributes | |
| double | sampleRate_ = 48000.0 |
| bool | prepared_ = false |
| std::atomic< T > | threshold_ { T(-40) } |
| std::atomic< T > | ratio_ { T(4) } |
| std::atomic< T > | hysteresis_ { T(4) } |
| std::atomic< T > | attackMs_ { T(0.5) } |
| std::atomic< T > | holdMs_ { T(50) } |
| std::atomic< T > | releaseMs_ { T(100) } |
| std::atomic< T > | rangeDb_ { T(-80) } |
| std::atomic< T > | rangeLinear_ { T(0.0001) } |
| std::atomic< bool > | scHpfEnabled_ { false } |
| std::atomic< T > | scHpfFreqHz_ { T(80) } |
| std::atomic< T > | scHpfCoeff_ { T(0.995) } |
| std::atomic< T > | scHpfA0_ { T(0.9975) } |
| std::array< T, MAX_CHANNELS > | scHpfState_ {} |
| std::array< T, MAX_CHANNELS > | scHpfPrev_ {} |
| KeyEq | keyEq_ |
| Equalizer of the key, after the high-pass. | |
| KeyMonitor< T, 16 > | keyMonitor_ |
| The listened key: late by the lookahead, under full scale. | |
| std::atomic< bool > | scListen_ { false } |
| The key goes out instead of the audio. | |
| bool | keyOn_ = false |
| The equalizer has bands in the path (audio thread). | |
| bool | listen_ = false |
| scListen_ as the audio thread took it. | |
| std::atomic< T > | lookaheadMs_ { T(0) } |
| int | cachedLookahead_ = 0 |
| Audio-side samples (per block). | |
| int | maxLookaheadSamples_ = 0 |
| Allocated delay (0 before prepare()). | |
| int | lookaheadChannels_ = 0 |
| Channels with a prepared delay line. | |
| std::array< RingBuffer< T >, MAX_CHANNELS > | lookahead_ {} |
| std::atomic< T > | attackCoeff_ { T(0) } |
| std::atomic< T > | releaseCoeff_ { T(0) } |
| std::atomic< T > | detRelCoeff_ { T(0.001) } |
| std::atomic< int > | holdSamples_ { 0 } |
| T | cachedThreshold_ = T(-40) |
| T | cachedRatio_ = T(4) |
| T | cachedHysteresis_ = T(4) |
| T | cachedRangeLinear_ = T(0.0001) |
| T | cachedAttackCoeff_ = T(0) |
| T | cachedReleaseCoeff_ = T(0) |
| T | cachedDetRelCoeff_ = T(0.001) |
| T | cachedScHpfCoeff_ = T(0.995) |
| T | cachedScHpfA0_ = T(0.9975) |
| int | cachedHoldSamples_ = 0 |
| bool | cachedScHpfEnabled_ = false |
| std::atomic< bool > | autoThreshold_ { false } |
| The threshold counts from the key's recent level. | |
| bool | cachedAutoThreshold_ = false |
| double | autoRefDb_ = -100.0 |
| The reference (dB). | |
| double | autoRefFall_ = 0.0 |
| Its fall per sample (dB). | |
| T | thresholdNow_ = T(-40) |
| Threshold of the frame (dB). | |
| State | state_ = State::Closed |
| T | gateGain_ = T(0) |
| std::atomic< State > | publishedState_ { State::Closed } |
| std::atomic< T > | publishedGateGain_ { T(0) } |
| T | envelope_ = T(0) |
| int | holdCounter_ = 0 |
| std::array< HilbertIIR< double >, MAX_CHANNELS > | analytic_ {} |
| std::array< double, kDetChunk > | detIn_ {} |
| std::array< double, kDetChunk > | detMag_ {} |
| std::array< T, kDetChunk > | detLevel_ {} |
| std::array< std::array< T, kDetChunk >, MAX_CHANNELS > | heard_ {} |
| The key of each channel in this chunk. | |
Static Protected Attributes | |
| static constexpr int | MAX_CHANNELS = 16 |
| static constexpr double | kMaxLookaheadMs = 10.0 |
| static constexpr double | kAutoRefFallDb = 1.0 |
| dB per second its reference falls. | |
| static constexpr double | kAutoRefQuiet = 0.25 |
| Share of that fall while the key is under the closing threshold. | |
| static constexpr int | kDetChunk = 64 |
Downward expander with ratio control, hysteresis, and sidechain.
| T | Sample type (float or double). |
Definition at line 75 of file Expander.h.
| using dspark::Expander< T >::KeyBand = typename KeyEq::Band |
The settings of one band of the key equalizer.
Definition at line 258 of file Expander.h.
| using dspark::Expander< T >::KeyEq = KeyEqualizer<T, 16> |
The equalizer of the key (see setSidechainBand()).
Definition at line 256 of file Expander.h.
| using dspark::Expander< T >::KeyPlacement = typename KeyEq::Placement |
The part of a two-channel key a band of the key equalizer acts on.
Definition at line 262 of file Expander.h.
| using dspark::Expander< T >::KeyShape = typename KeyEq::Shape |
What a band of the key equalizer does.
Definition at line 260 of file Expander.h.
|
strong |
| Enumerator | |
|---|---|
| Closed | |
| Open | |
| Hold | |
Definition at line 80 of file Expander.h.
|
default |
|
inlineprotectednoexcept |
Definition at line 436 of file Expander.h.
|
inlineprotectednoexcept |
Definition at line 654 of file Expander.h.
|
inlinenoexcept |
Definition at line 369 of file Expander.h.
|
inlinenoexcept |
Definition at line 361 of file Expander.h.
|
inlinenoexcept |
Latency in samples (the lookahead), correct right after setLookahead().
Definition at line 348 of file Expander.h.
|
inlinenoexcept |
Returns the lookahead in milliseconds.
Definition at line 324 of file Expander.h.
|
inlinenoexcept |
The settings of a band of the key equalizer.
Definition at line 286 of file Expander.h.
|
inlinenoexcept |
The curve of the key equalizer, for a display.
Definition at line 289 of file Expander.h.
|
inline |
Serializes the parameter state (setup/UI threads; allocates).
Definition at line 395 of file Expander.h.
|
inlinenoexcept |
Whether the threshold counts from the key's recent level (see setAutoThreshold()).
Definition at line 345 of file Expander.h.
|
inlinenoexcept |
Whether the key is being played instead of the audio.
Definition at line 304 of file Expander.h.
|
inlineprotectednoexcept |
One sample of a channel's key before its equalizer: made finite and, when UseHPF, through the sidechain high-pass.
Definition at line 468 of file Expander.h.
|
inlineprotectednoexcept |
Lookahead in samples for a ms value (shared by cacheParams() and getLatency()).
Definition at line 457 of file Expander.h.
|
inlinenoexcept |
Initializes the expander using an AudioSpec struct.
| spec | Framework audio specification object (invalid specs are ignored). |
Definition at line 115 of file Expander.h.
|
inlinenoexcept |
Initializes the expander with a specific sample rate.
| sampleRate | The sample rate in Hz. Non-finite or non-positive rates are ignored (previous state is kept). |
| numChannels | Channels that receive the lookahead delay (clamped to [1, 16]); further channels are processed undelayed. |
Definition at line 91 of file Expander.h.
|
inlinenoexcept |
Processes an audio block in place (internal detector key).
| buffer | View of the audio buffer to process. |
Definition at line 125 of file Expander.h.
|
inlinenoexcept |
Processes an audio block in place, keyed by an external sidechain.
The detector (and its optional HPF) runs on the sidechain channels; the resulting gain is applied to the audio buffer. If the sidechain is empty or shorter than the audio block, the internal key is used instead.
| buffer | Audio to process in place. |
| sidechain | Detector key signal (not modified). |
Definition at line 147 of file Expander.h.
|
inlineprotectednoexcept |
Internal processing loop templated on HPF/sidechain to avoid branching.
| UseHPF | Compile-time flag to include HPF processing. |
| UseSc | Compile-time flag: detector keyed by the external sidechain. |
| buffer | View of the audio buffer (gain target). |
| sidechain | Detector key when UseSc (guaranteed >= buffer length). |
Definition at line 492 of file Expander.h.
|
inlineprotectednoexcept |
Publishes the metering words for cross-thread readout.
Two relaxed stores per block, outside the per-sample loop: the getters used to read state_ and gateGain_ directly, which are plain words the audio thread writes every sample.
Definition at line 622 of file Expander.h.
|
inlinenoexcept |
Resets all internal DSP states to prevent clicks on playback start.
Definition at line 375 of file Expander.h.
|
inlinenoexcept |
Sets the attack time in milliseconds.
Definition at line 205 of file Expander.h.
|
inlinenoexcept |
Counts the threshold from the key's own recent level.
A reference follows the detected level: it rises at once and falls 1 dB per second, and the threshold is added to it. The expander then acts the same at any input level: a threshold of -40 dB closes on what lies more than 40 dB under the programme's recent peaks. While the key is under the closing threshold the reference falls a quarter as fast, so a pause lowers the threshold by 1 dB every four seconds: far enough into a long one the floor opens the expander again (the range bounds what that lets through), and a stray peak that left the programme under the threshold is forgotten at that pace. Off by default: the threshold is in dBFS.
State key "autoThreshold" (absent = off).
Definition at line 342 of file Expander.h.
|
inlinenoexcept |
Sets the hold time in milliseconds before release begins.
Definition at line 213 of file Expander.h.
|
inlinenoexcept |
Sets the hysteresis gap in decibels to prevent chattering.
Definition at line 189 of file Expander.h.
|
inlinenoexcept |
Sets the lookahead: the audio is delayed so the gain moves ahead of it. 0 (default) is latency-free.
RT-safe publication, consumed at the next block; change it while stopped (a live change jumps the delay and may click). Report getLatency() to the host for delay compensation.
| ms | Lookahead in milliseconds, clamped to [0, 10]. Non-finite values are ignored. |
Definition at line 317 of file Expander.h.
|
inlinenoexcept |
Sets the maximum gain reduction limit in decibels (<= 0).
Definition at line 196 of file Expander.h.
|
inlinenoexcept |
Sets the expansion ratio (e.g., 4.0 for 4:1).
Definition at line 182 of file Expander.h.
|
inlinenoexcept |
Sets the release time in milliseconds.
Definition at line 229 of file Expander.h.
|
inlinenoexcept |
Sets a band of the equalizer of the key.
Six bands (cuts from 6 to 48 dB per octave, bells, shelves, a tilt) shape what the detector weighs, after the sidechain high-pass, on the programme or on the external key, and never on the audio. A band that keeps its form glides to a new setting over 20 ms and a change of shape crosses over the same time; a band that is Off costs nothing. The detector reads the loudest channel of the key.
With a two-channel key a band can sit on both channels, on the middle of the pair or on its sides (KeyBand::placement): a low cut on the middle keeps a centred bass from holding the expander open while what is wide still does. Keys of one channel or of more than two take every band on every channel.
| band | Band index in [0, KeyEq::kBands); others are ignored. |
| settings | Shape, frequency, gain, q, slope and placement of the band. |
Definition at line 283 of file Expander.h.
|
inlinenoexcept |
Enables and configures the sidechain high-pass filter.
Invalid cutoffs (non-finite or <= 0) keep the previous frequency and only apply the toggle; the design clamp is [1, 0.45 * sampleRate]. A negative cutoff used to flip the one-pole into an unstable growth filter, blowing up the detector and pinning the expander open.
| enabled | True to engage the HPF. |
| cutoffHz | Cutoff frequency in Hz. |
Definition at line 247 of file Expander.h.
|
inlinenoexcept |
Plays the key instead of the expanded audio.
Each channel puts out what the detector hears of it (the key after the high-pass and the equalizer; the last key channel where it has none), late by the lookahead (the latency the expander reports, so the track keeps its place) and held under full scale (KeyMonitor), while the expander keeps working underneath. A monitoring switch: it is not part of the state.
Definition at line 301 of file Expander.h.
|
inline |
Restores parameters from a blob (tolerant; rejects foreign ids).
Definition at line 416 of file Expander.h.
|
inlinenoexcept |
Sets the threshold in decibels.
Definition at line 175 of file Expander.h.
|
inlineprotectednoexcept |
Definition at line 686 of file Expander.h.
|
inlineprotectednoexcept |
Definition at line 628 of file Expander.h.
|
protected |
Definition at line 782 of file Expander.h.
|
protected |
Definition at line 745 of file Expander.h.
|
protected |
Definition at line 716 of file Expander.h.
|
protected |
The reference (dB).
Definition at line 768 of file Expander.h.
|
protected |
Its fall per sample (dB).
Definition at line 769 of file Expander.h.
|
protected |
The threshold counts from the key's recent level.
Definition at line 766 of file Expander.h.
|
protected |
Definition at line 755 of file Expander.h.
|
protected |
Definition at line 767 of file Expander.h.
|
protected |
Definition at line 757 of file Expander.h.
|
protected |
Definition at line 760 of file Expander.h.
|
protected |
Definition at line 753 of file Expander.h.
|
protected |
Audio-side samples (per block).
Definition at line 740 of file Expander.h.
|
protected |
Definition at line 754 of file Expander.h.
|
protected |
Definition at line 752 of file Expander.h.
|
protected |
Definition at line 756 of file Expander.h.
|
protected |
Definition at line 759 of file Expander.h.
|
protected |
Definition at line 758 of file Expander.h.
|
protected |
Definition at line 761 of file Expander.h.
|
protected |
Definition at line 751 of file Expander.h.
|
protected |
Definition at line 783 of file Expander.h.
|
protected |
Definition at line 785 of file Expander.h.
|
protected |
Definition at line 784 of file Expander.h.
|
protected |
Definition at line 747 of file Expander.h.
|
protected |
Definition at line 777 of file Expander.h.
|
protected |
Definition at line 773 of file Expander.h.
|
protected |
The key of each channel in this chunk.
Definition at line 786 of file Expander.h.
|
protected |
Definition at line 778 of file Expander.h.
|
protected |
Definition at line 717 of file Expander.h.
|
protected |
Definition at line 748 of file Expander.h.
|
protected |
Definition at line 715 of file Expander.h.
|
staticconstexprprotected |
dB per second its reference falls.
Definition at line 764 of file Expander.h.
|
staticconstexprprotected |
Share of that fall while the key is under the closing threshold.
Definition at line 765 of file Expander.h.
|
staticconstexprprotected |
Definition at line 781 of file Expander.h.
|
protected |
Equalizer of the key, after the high-pass.
Definition at line 731 of file Expander.h.
|
protected |
The listened key: late by the lookahead, under full scale.
Definition at line 732 of file Expander.h.
|
protected |
The equalizer has bands in the path (audio thread).
Definition at line 734 of file Expander.h.
|
staticconstexprprotected |
Definition at line 738 of file Expander.h.
|
protected |
scListen_ as the audio thread took it.
Definition at line 735 of file Expander.h.
|
protected |
Definition at line 743 of file Expander.h.
|
protected |
Channels with a prepared delay line.
Definition at line 742 of file Expander.h.
|
protected |
Definition at line 739 of file Expander.h.
|
staticconstexprprotected |
Definition at line 728 of file Expander.h.
|
protected |
Allocated delay (0 before prepare()).
Definition at line 741 of file Expander.h.
|
protected |
Definition at line 711 of file Expander.h.
|
protected |
Definition at line 776 of file Expander.h.
|
protected |
Definition at line 775 of file Expander.h.
|
protected |
Definition at line 719 of file Expander.h.
|
protected |
Definition at line 720 of file Expander.h.
|
protected |
Definition at line 714 of file Expander.h.
|
protected |
Definition at line 746 of file Expander.h.
|
protected |
Definition at line 718 of file Expander.h.
|
protected |
Definition at line 710 of file Expander.h.
|
protected |
Definition at line 725 of file Expander.h.
|
protected |
Definition at line 724 of file Expander.h.
|
protected |
Definition at line 722 of file Expander.h.
|
protected |
Definition at line 723 of file Expander.h.
|
protected |
Definition at line 730 of file Expander.h.
|
protected |
Definition at line 729 of file Expander.h.
|
protected |
The key goes out instead of the audio.
Definition at line 733 of file Expander.h.
|
protected |
Definition at line 772 of file Expander.h.
|
protected |
Definition at line 713 of file Expander.h.
|
protected |
Threshold of the frame (dB).
Definition at line 770 of file Expander.h.