|
DSPark 1.8.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 } |
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 | 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. | |
| 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, 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_ {} |
| 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 > | detAttCoeff_ { T(0.01) } |
| 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 | cachedDetAttCoeff_ = T(0.01) |
| T | cachedDetRelCoeff_ = T(0.001) |
| T | cachedScHpfCoeff_ = T(0.995) |
| T | cachedScHpfA0_ = T(0.9975) |
| int | cachedHoldSamples_ = 0 |
| bool | cachedScHpfEnabled_ = false |
| 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 |
Static Protected Attributes | |
| static constexpr int | MAX_CHANNELS = 16 |
| static constexpr double | kMaxLookaheadMs = 10.0 |
Downward expander with ratio control, hysteresis, and sidechain.
| T | Sample type (float or double). |
Definition at line 64 of file Expander.h.
|
strong |
| Enumerator | |
|---|---|
| Closed | |
| Open | |
| Hold | |
Definition at line 69 of file Expander.h.
|
default |
|
inlineprotectednoexcept |
Definition at line 342 of file Expander.h.
|
inlineprotectednoexcept |
Definition at line 475 of file Expander.h.
|
inlinenoexcept |
Definition at line 284 of file Expander.h.
|
inlinenoexcept |
Definition at line 276 of file Expander.h.
|
inlinenoexcept |
Latency in samples (the lookahead), correct right after setLookahead().
Definition at line 263 of file Expander.h.
|
inlinenoexcept |
Returns the lookahead in milliseconds.
Definition at line 260 of file Expander.h.
|
inline |
Serializes the parameter state (setup/UI threads; allocates).
Definition at line 305 of file Expander.h.
|
inlineprotectednoexcept |
Lookahead in samples for a ms value (shared by cacheParams() and getLatency()).
Definition at line 360 of file Expander.h.
|
inlinenoexcept |
Initializes the expander using an AudioSpec struct.
| spec | Framework audio specification object (invalid specs are ignored). |
Definition at line 102 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 80 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 112 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 134 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 374 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 443 of file Expander.h.
|
inlinenoexcept |
Resets all internal DSP states to prevent clicks on playback start.
Definition at line 290 of file Expander.h.
|
inlinenoexcept |
Sets the attack time in milliseconds.
Definition at line 192 of file Expander.h.
|
inlinenoexcept |
Sets the hold time in milliseconds before release begins.
Definition at line 200 of file Expander.h.
|
inlinenoexcept |
Sets the hysteresis gap in decibels to prevent chattering.
Definition at line 176 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 253 of file Expander.h.
|
inlinenoexcept |
Sets the maximum gain reduction limit in decibels (<= 0).
Definition at line 183 of file Expander.h.
|
inlinenoexcept |
Sets the expansion ratio (e.g., 4.0 for 4:1).
Definition at line 169 of file Expander.h.
|
inlinenoexcept |
Sets the release time in milliseconds.
Definition at line 216 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 234 of file Expander.h.
|
inline |
Restores parameters from a blob (tolerant; rejects foreign ids).
Definition at line 324 of file Expander.h.
|
inlinenoexcept |
Sets the threshold in decibels.
Definition at line 162 of file Expander.h.
|
inlineprotectednoexcept |
Definition at line 507 of file Expander.h.
|
inlineprotectednoexcept |
Definition at line 449 of file Expander.h.
|
protected |
Definition at line 562 of file Expander.h.
|
protected |
Definition at line 538 of file Expander.h.
|
protected |
Definition at line 573 of file Expander.h.
|
protected |
Definition at line 575 of file Expander.h.
|
protected |
Definition at line 576 of file Expander.h.
|
protected |
Definition at line 579 of file Expander.h.
|
protected |
Definition at line 571 of file Expander.h.
|
protected |
Audio-side samples (per block).
Definition at line 557 of file Expander.h.
|
protected |
Definition at line 572 of file Expander.h.
|
protected |
Definition at line 570 of file Expander.h.
|
protected |
Definition at line 574 of file Expander.h.
|
protected |
Definition at line 578 of file Expander.h.
|
protected |
Definition at line 577 of file Expander.h.
|
protected |
Definition at line 580 of file Expander.h.
|
protected |
Definition at line 569 of file Expander.h.
|
protected |
Definition at line 564 of file Expander.h.
|
protected |
Definition at line 565 of file Expander.h.
|
protected |
Definition at line 587 of file Expander.h.
|
protected |
Definition at line 583 of file Expander.h.
|
protected |
Definition at line 588 of file Expander.h.
|
protected |
Definition at line 539 of file Expander.h.
|
protected |
Definition at line 566 of file Expander.h.
|
protected |
Definition at line 537 of file Expander.h.
|
staticconstexprprotected |
Definition at line 555 of file Expander.h.
|
protected |
Definition at line 560 of file Expander.h.
|
protected |
Channels with a prepared delay line.
Definition at line 559 of file Expander.h.
|
protected |
Definition at line 556 of file Expander.h.
|
staticconstexprprotected |
Definition at line 550 of file Expander.h.
|
protected |
Allocated delay (0 before prepare()).
Definition at line 558 of file Expander.h.
|
protected |
Definition at line 533 of file Expander.h.
|
protected |
Definition at line 586 of file Expander.h.
|
protected |
Definition at line 585 of file Expander.h.
|
protected |
Definition at line 541 of file Expander.h.
|
protected |
Definition at line 542 of file Expander.h.
|
protected |
Definition at line 536 of file Expander.h.
|
protected |
Definition at line 563 of file Expander.h.
|
protected |
Definition at line 540 of file Expander.h.
|
protected |
Definition at line 532 of file Expander.h.
|
protected |
Definition at line 547 of file Expander.h.
|
protected |
Definition at line 546 of file Expander.h.
|
protected |
Definition at line 544 of file Expander.h.
|
protected |
Definition at line 545 of file Expander.h.
|
protected |
Definition at line 552 of file Expander.h.
|
protected |
Definition at line 551 of file Expander.h.
|
protected |
Definition at line 582 of file Expander.h.
|
protected |
Definition at line 535 of file Expander.h.