|
DSPark 1.8.0
Header-only C++20 DSP for real-time and offline audio
|
High-performance noise gate with state machine, hysteresis, and zero-allocation processing. More...
#include <NoiseGate.h>
Public Types | |
| enum class | State { Closed , Open , Hold } |
| Gate state machine phases. More... | |
| enum class | GateMode { Amplitude , Frequency } |
| Operating mode for the gate processing. More... | |
Public Member Functions | |
| ~NoiseGate ()=default | |
| void | prepare (double sampleRate, int numChannels=2) noexcept |
| Prepares the noise gate for processing. | |
| void | prepare (const AudioSpec &spec) noexcept |
| Prepares from AudioSpec (unified API). | |
| void | processBlock (AudioBufferView< T > buffer) noexcept |
| Processes an AudioBufferView in-place. | |
| void | processBlock (AudioBufferView< T > audio, AudioBufferView< T > sidechain) noexcept |
| Processes audio with an external sidechain signal. | |
| void | setThreshold (T dB) noexcept |
| Sets the opening threshold. | |
| void | setHysteresis (T dB) noexcept |
| Sets the hysteresis amount (gap between open and close thresholds). | |
| void | setAttack (T ms) noexcept |
| Sets attack time in milliseconds. Non-finite values are ignored. | |
| void | setHold (T ms) noexcept |
| Sets hold time in milliseconds. Non-finite values are ignored. | |
| void | setRelease (T ms) noexcept |
| Sets release time in milliseconds. Non-finite values are ignored. | |
| void | setRange (T dB) noexcept |
| Sets maximum attenuation range. | |
| void | setDuckMode (bool enabled) noexcept |
| Toggles ducking mode (invert gate logic). | |
| void | setGateMode (GateMode mode) noexcept |
| Selects the processing mode (Amplitude or Frequency; wild enum values clamp). | |
| void | setLookahead (T ms) noexcept |
| Sets the lookahead: the audio is delayed so the gate opens (and closes) 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(). | |
| void | setAdaptiveHold (bool enabled) noexcept |
| Toggles adaptive hold based on zero-crossing rate. | |
| void | setSidechainHPF (bool enabled, double cutoffHz=80.0) noexcept |
| Configures sidechain High-Pass filter. | |
| T | processSample (T input) noexcept |
| Processes a single mono sample. | |
| T | processSampleWithSidechain (T input, T sidechain) noexcept |
| Processes a mono sample using an external sidechain. | |
| void | reset () noexcept |
| Resets DSP state (clears filters, sets state to Closed). | |
| 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 | syncParamsIfDirty () noexcept |
| void | syncParams () noexcept |
| int | lookaheadSamplesFor (T ms) const noexcept |
| T | delayed (T x, int ch) noexcept |
| T | computeEnvelopeFollower (T rawLevel) noexcept |
| Updates envelope state to prevent intra-cycle chattering. | |
| void | updateStateMachine (T envelopeLinear) noexcept |
| void | updateZeroCrossing (T sample) noexcept |
| T | getCurrentGain () noexcept |
| T | applyFrequencyGate (T input, int ch) noexcept |
| T | applyScHpf (T input, int ch) noexcept |
| Per-channel one-pole sidechain high-pass (unity gain at Nyquist). | |
| T | processSampleInternal (T input, T sidechain, int ch) noexcept |
Protected Attributes | |
| double | sampleRate_ = 48000.0 |
| std::atomic< T > | threshold_ { T(-40) } |
| 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< bool > | duckMode_ { false } |
| std::atomic< GateMode > | gateMode_ { GateMode::Amplitude } |
| std::atomic< bool > | adaptiveHold_ { false } |
| std::atomic< bool > | paramsDirty_ { true } |
| std::atomic< bool > | scHpfEnabled_ { false } |
| std::atomic< T > | scHpfFreq_ { T(80) } |
| std::atomic< T > | lookaheadMs_ { T(0) } |
| int | lookaheadSamples_ = 0 |
| Audio-side value (consumed in syncParams()). | |
| int | maxLookaheadSamples_ = 0 |
| Allocated delay (0 before prepare()). | |
| int | lookaheadChannels_ = 0 |
| Channels with a prepared delay line. | |
| std::array< RingBuffer< T >, kMaxLookaheadChannels > | lookahead_ {} |
| T | cachedThresholdLinear_ = T(0.01) |
| T | cachedCloseThresholdLinear_ = T(0.0063) |
| T | cachedRangeLinear_ = T(0.0001) |
| bool | cachedDuck_ = false |
| GateMode | cachedGateMode_ = GateMode::Amplitude |
| bool | cachedAdaptiveHold_ = false |
| bool | cachedScHpfEnabled_ = false |
| T | cachedNyquist_ = T(24000) |
| T | cachedFsInvPi2_ = T(0) |
| T | scHpfCoeff_ = T(0.995) |
| T | scHpfA0_ = T(0.9975) |
| std::array< T, kMaxScChannels > | scHpfState_ {} |
| std::array< T, kMaxScChannels > | scHpfPrev_ {} |
| T | attackCoeff_ = T(0) |
| T | releaseCoeff_ = T(0) |
| int | holdSamples_ = 0 |
| T | envelopeState_ = T(0) |
| T | detectorReleaseCoeff_ = T(0) |
| State | state_ = State::Closed |
| T | gateGain_ = T(0) |
| int | holdCounter_ = 0 |
| T | freqSmoothCoeff_ = T(0) |
| std::array< T, kMaxChannels > | freqLpState_ {} |
| std::array< T, kMaxChannels > | freqHpState_ {} |
| std::array< T, kMaxChannels > | freqHpPrev_ {} |
| std::array< T, kMaxChannels > | freqLpFreq_ {} |
| std::array< T, kMaxChannels > | freqHpFreq_ {} |
| int | zeroCrossCount_ = 0 |
| int | zeroCrossSamples_ = 0 |
| bool | prevSign_ = false |
| int | estimatedPeriod_ = 0 |
Static Protected Attributes | |
| static constexpr int | kMaxChannels = 2 |
| static constexpr double | kMaxLookaheadMs = 10.0 |
| static constexpr int | kMaxLookaheadChannels = 16 |
| static constexpr int | kMaxScChannels = 16 |
| static constexpr int | kZeroCrossWindow = 2048 |
High-performance noise gate with state machine, hysteresis, and zero-allocation processing.
| T | Sample type (float or double). |
Definition at line 81 of file NoiseGate.h.
|
strong |
Operating mode for the gate processing.
| Enumerator | |
|---|---|
| Amplitude | Standard amplitude gain reduction (default). |
| Frequency | Gatelope-style: narrows bandpass dynamically instead of direct gain reduction. |
Definition at line 95 of file NoiseGate.h.
|
strong |
Gate state machine phases.
| Enumerator | |
|---|---|
| Closed | Gate is fully closed (applying range attenuation). |
| Open | Gate is fully open (passing audio). |
| Hold | Gate is open but counting down hold time before closing. |
Definition at line 87 of file NoiseGate.h.
|
default |
|
inlineprotectednoexcept |
Definition at line 638 of file NoiseGate.h.
|
inlineprotectednoexcept |
Per-channel one-pole sidechain high-pass (unity gain at Nyquist).
Definition at line 662 of file NoiseGate.h.
|
inlineprotectednoexcept |
Updates envelope state to prevent intra-cycle chattering.
Definition at line 555 of file NoiseGate.h.
|
inlineprotectednoexcept |
The lookahead-delayed audio sample of a channel (pass-through when off or beyond the prepared channels).
Definition at line 544 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 626 of file NoiseGate.h.
|
inlinenoexcept |
Latency in samples (the lookahead), correct right after setLookahead().
Definition at line 348 of file NoiseGate.h.
|
inlinenoexcept |
Returns the lookahead in milliseconds.
Definition at line 345 of file NoiseGate.h.
|
inline |
Serializes the parameter state (setup/UI threads; allocates).
Definition at line 437 of file NoiseGate.h.
|
inlineprotectednoexcept |
Lookahead in samples for a ms value (shared by syncParams() and getLatency()).
Definition at line 536 of file NoiseGate.h.
|
inlinenoexcept |
Prepares from AudioSpec (unified API).
| spec | AudioSpec containing format details. |
Definition at line 128 of file NoiseGate.h.
|
inlinenoexcept |
Prepares the noise gate for processing.
Release-safe: a non-positive or non-finite sample rate makes this call a no-op (the previous state and rate are kept; +inf used to pass). Allocates the lookahead delay lines (setup thread only).
| sampleRate | Operating sample rate in Hz (must be > 0). |
| numChannels | Channels that receive the lookahead delay (clamped to [1, 16]); further channels are gated undelayed. |
Definition at line 112 of file NoiseGate.h.
|
inlinenoexcept |
Processes audio with an external sidechain signal.
Detection (including the optional HPF, the adaptive-hold zero-crossing tracker and Frequency mode) behaves exactly like the internal-key path; only the level source changes to the sidechain buffer.
| audio | Audio buffer to gate (modified in-place). |
| sidechain | External sidechain signal (read-only). |
Definition at line 197 of file NoiseGate.h.
|
inlinenoexcept |
Processes an AudioBufferView in-place.
Detection is linked across ALL channels (peak), so no channel is left ungated. In Frequency mode, channels beyond kMaxChannels fall back to amplitude gating (per-channel filter state is kept for the first two).
| buffer | Audio buffer (mono or multi-channel). |
Definition at line 139 of file NoiseGate.h.
|
inlinenoexcept |
Processes a single mono sample.
Bit-identical to processBlock() for mono streams.
| input | Source sample. |
Definition at line 391 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 670 of file NoiseGate.h.
|
inlinenoexcept |
Processes a mono sample using an external sidechain.
| input | Source sample. |
| sidechain | Key/Sidechain sample. |
Definition at line 403 of file NoiseGate.h.
|
inlinenoexcept |
Resets DSP state (clears filters, sets state to Closed).
Definition at line 412 of file NoiseGate.h.
|
inlinenoexcept |
Toggles adaptive hold based on zero-crossing rate.
Definition at line 354 of file NoiseGate.h.
|
inlinenoexcept |
Sets attack time in milliseconds. Non-finite values are ignored.
Definition at line 277 of file NoiseGate.h.
|
inlinenoexcept |
Toggles ducking mode (invert gate logic).
Definition at line 312 of file NoiseGate.h.
|
inlinenoexcept |
Selects the processing mode (Amplitude or Frequency; wild enum values clamp).
Definition at line 319 of file NoiseGate.h.
|
inlinenoexcept |
Sets hold time in milliseconds. Non-finite values are ignored.
Definition at line 285 of file NoiseGate.h.
|
inlinenoexcept |
Sets the hysteresis amount (gap between open and close thresholds).
| dB | Hysteresis in decibels (floored to 0). Non-finite values are ignored. |
Definition at line 269 of file NoiseGate.h.
|
inlinenoexcept |
Sets the lookahead: the audio is delayed so the gate opens (and closes) 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 337 of file NoiseGate.h.
|
inlinenoexcept |
Sets maximum attenuation range.
| dB | Range in decibels (capped at 0). Non-finite values are ignored. |
Definition at line 304 of file NoiseGate.h.
|
inlinenoexcept |
Sets release time in milliseconds. Non-finite values are ignored.
Definition at line 293 of file NoiseGate.h.
|
inlinenoexcept |
Configures sidechain High-Pass filter.
An invalid cutoff (non-finite or non-positive) keeps the previous frequency and only applies the toggle: a negative cutoff would make the one-pole coefficient exceed 1 (an unstable, runaway filter).
| enabled | Filter active state. |
| cutoffHz | Filter cutoff in Hz (clamped to [1, 0.45 * fs]). |
Definition at line 370 of file NoiseGate.h.
|
inline |
Restores parameters from a blob (tolerant; rejects foreign ids).
Definition at line 456 of file NoiseGate.h.
|
inlinenoexcept |
Sets the opening threshold.
| dB | Threshold in decibels. Non-finite values are ignored. |
Definition at line 258 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 487 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 478 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 565 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 606 of file NoiseGate.h.
|
protected |
Definition at line 703 of file NoiseGate.h.
|
protected |
Definition at line 735 of file NoiseGate.h.
|
protected |
Definition at line 697 of file NoiseGate.h.
|
protected |
Definition at line 724 of file NoiseGate.h.
|
protected |
Definition at line 720 of file NoiseGate.h.
|
protected |
Definition at line 722 of file NoiseGate.h.
|
protected |
Definition at line 727 of file NoiseGate.h.
|
protected |
Definition at line 723 of file NoiseGate.h.
|
protected |
Definition at line 726 of file NoiseGate.h.
|
protected |
Definition at line 721 of file NoiseGate.h.
|
protected |
Definition at line 725 of file NoiseGate.h.
|
protected |
Definition at line 719 of file NoiseGate.h.
|
protected |
Definition at line 741 of file NoiseGate.h.
|
protected |
Definition at line 701 of file NoiseGate.h.
|
protected |
Definition at line 740 of file NoiseGate.h.
|
protected |
Definition at line 758 of file NoiseGate.h.
|
protected |
Definition at line 752 of file NoiseGate.h.
|
protected |
Definition at line 750 of file NoiseGate.h.
|
protected |
Definition at line 749 of file NoiseGate.h.
|
protected |
Definition at line 751 of file NoiseGate.h.
|
protected |
Definition at line 748 of file NoiseGate.h.
|
protected |
Definition at line 747 of file NoiseGate.h.
|
protected |
Definition at line 744 of file NoiseGate.h.
|
protected |
Definition at line 702 of file NoiseGate.h.
|
protected |
Definition at line 745 of file NoiseGate.h.
|
protected |
Definition at line 698 of file NoiseGate.h.
|
protected |
Definition at line 737 of file NoiseGate.h.
|
protected |
Definition at line 696 of file NoiseGate.h.
|
staticconstexprprotected |
Definition at line 476 of file NoiseGate.h.
|
staticconstexprprotected |
Definition at line 711 of file NoiseGate.h.
|
staticconstexprprotected |
Definition at line 710 of file NoiseGate.h.
|
staticconstexprprotected |
Definition at line 729 of file NoiseGate.h.
|
staticconstexprprotected |
Definition at line 754 of file NoiseGate.h.
|
protected |
Definition at line 716 of file NoiseGate.h.
|
protected |
Channels with a prepared delay line.
Definition at line 715 of file NoiseGate.h.
|
protected |
Definition at line 712 of file NoiseGate.h.
|
protected |
Audio-side value (consumed in syncParams()).
Definition at line 713 of file NoiseGate.h.
|
protected |
Allocated delay (0 before prepare()).
Definition at line 714 of file NoiseGate.h.
|
protected |
Definition at line 704 of file NoiseGate.h.
|
protected |
Definition at line 757 of file NoiseGate.h.
|
protected |
Definition at line 700 of file NoiseGate.h.
|
protected |
Definition at line 736 of file NoiseGate.h.
|
protected |
Definition at line 699 of file NoiseGate.h.
|
protected |
Definition at line 693 of file NoiseGate.h.
|
protected |
Definition at line 731 of file NoiseGate.h.
|
protected |
Definition at line 730 of file NoiseGate.h.
|
protected |
Definition at line 706 of file NoiseGate.h.
|
protected |
Definition at line 707 of file NoiseGate.h.
|
protected |
Definition at line 733 of file NoiseGate.h.
|
protected |
Definition at line 732 of file NoiseGate.h.
|
protected |
Definition at line 743 of file NoiseGate.h.
|
protected |
Definition at line 695 of file NoiseGate.h.
|
protected |
Definition at line 755 of file NoiseGate.h.
|
protected |
Definition at line 756 of file NoiseGate.h.