|
DSPark 1.9.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... | |
| 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 | |
| ~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 | 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()). | |
| 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. | |
| 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 gated audio. | |
| bool | isSidechainListen () const noexcept |
| Whether the key is being played instead of the audio. | |
| 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). | |
| template<typename Sample > | |
| T | placedKeyFrame (Sample sample, T &det0) noexcept |
| One frame of a two-channel key whose equalizer has bands on the middle or the sides: both channels made finite and high-passed, then through the equalizer together (into heard_). | |
| T | processSampleInternal (T input, T sidechain, int ch) noexcept |
Static Protected Member Functions | |
| static T | detectorSafe (T x) noexcept |
| Non-finite detector samples read as silence (they would latch in the envelope and filter state). | |
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_ {} |
| 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::array< T, kMaxScChannels > | heard_ {} |
| The key of each channel in this frame. | |
| T | attackCoeff_ = T(0) |
| T | releaseCoeff_ = T(0) |
| int | holdSamples_ = 0 |
| std::atomic< bool > | autoThreshold_ { false } |
| The thresholds count from the key's recent level. | |
| bool | cachedAutoThreshold_ = false |
| double | autoRef_ = kAutoRefFloor |
| The reference (linear). | |
| double | autoFall_ = 1.0 |
| Its fall per sample (a factor). | |
| double | autoFallQuiet_ = 1.0 |
| The same while the key is quiet. | |
| 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 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 double | kAutoRefFloor = 1e-5 |
| The reference never goes under -100 dBFS. | |
| 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 84 of file NoiseGate.h.
| using dspark::NoiseGate< T >::KeyBand = typename KeyEq::Band |
The settings of one band of the key equalizer.
Definition at line 448 of file NoiseGate.h.
| using dspark::NoiseGate< T >::KeyEq = KeyEqualizer<T, 16> |
The equalizer of the key (see setSidechainBand()).
Definition at line 446 of file NoiseGate.h.
| using dspark::NoiseGate< T >::KeyPlacement = typename KeyEq::Placement |
The part of a two-channel key a band of the key equalizer acts on.
Definition at line 452 of file NoiseGate.h.
| using dspark::NoiseGate< T >::KeyShape = typename KeyEq::Shape |
What a band of the key equalizer does.
Definition at line 450 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 98 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 90 of file NoiseGate.h.
|
default |
|
inlineprotectednoexcept |
Definition at line 784 of file NoiseGate.h.
|
inlineprotectednoexcept |
Per-channel one-pole sidechain high-pass (unity gain at Nyquist).
Definition at line 811 of file NoiseGate.h.
|
inlineprotectednoexcept |
Updates envelope state to prevent intra-cycle chattering.
Definition at line 688 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 677 of file NoiseGate.h.
|
inlinestaticprotectednoexcept |
Non-finite detector samples read as silence (they would latch in the envelope and filter state).
Definition at line 821 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 772 of file NoiseGate.h.
|
inlinenoexcept |
Latency in samples (the lookahead), correct right after setLookahead().
Definition at line 415 of file NoiseGate.h.
|
inlinenoexcept |
Returns the lookahead in milliseconds.
Definition at line 412 of file NoiseGate.h.
|
inlinenoexcept |
The settings of a band of the key equalizer.
Definition at line 477 of file NoiseGate.h.
|
inlinenoexcept |
The curve of the key equalizer, for a display.
Definition at line 480 of file NoiseGate.h.
|
inline |
Serializes the parameter state (setup/UI threads; allocates).
Definition at line 558 of file NoiseGate.h.
|
inlinenoexcept |
Whether the threshold counts from the key's recent level (see setAutoThreshold()).
Definition at line 330 of file NoiseGate.h.
|
inlinenoexcept |
Whether the key is being played instead of the audio.
Definition at line 494 of file NoiseGate.h.
|
inlineprotectednoexcept |
Lookahead in samples for a ms value (shared by syncParams() and getLatency()).
Definition at line 669 of file NoiseGate.h.
|
inlineprotectednoexcept |
One frame of a two-channel key whose equalizer has bands on the middle or the sides: both channels made finite and high-passed, then through the equalizer together (into heard_).
| sample | The key of a channel in this frame. |
| det0 | Set to channel 0's key, for the zero-crossing tracker. |
Definition at line 832 of file NoiseGate.h.
|
inlinenoexcept |
Prepares from AudioSpec (unified API).
| spec | AudioSpec containing format details. |
Definition at line 133 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 115 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 217 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 144 of file NoiseGate.h.
|
inlinenoexcept |
Processes a single mono sample.
Bit-identical to processBlock() for mono streams.
| input | Source sample. |
Definition at line 509 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 845 of file NoiseGate.h.
|
inlinenoexcept |
Processes a mono sample using an external sidechain.
| input | Source sample. |
| sidechain | Key/Sidechain sample. |
Definition at line 521 of file NoiseGate.h.
|
inlinenoexcept |
Resets DSP state (clears filters, sets state to Closed).
Definition at line 530 of file NoiseGate.h.
|
inlinenoexcept |
Toggles adaptive hold based on zero-crossing rate.
Definition at line 421 of file NoiseGate.h.
|
inlinenoexcept |
Sets attack time in milliseconds. Non-finite values are ignored.
Definition at line 344 of file NoiseGate.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 thresholds are counted from it. The gate 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 gate 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 323 of file NoiseGate.h.
|
inlinenoexcept |
Toggles ducking mode (invert gate logic).
Definition at line 379 of file NoiseGate.h.
|
inlinenoexcept |
Selects the processing mode (Amplitude or Frequency; wild enum values clamp).
Definition at line 386 of file NoiseGate.h.
|
inlinenoexcept |
Sets hold time in milliseconds. Non-finite values are ignored.
Definition at line 352 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 336 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 404 of file NoiseGate.h.
|
inlinenoexcept |
Sets maximum attenuation range.
| dB | Range in decibels (capped at 0). Non-finite values are ignored. |
Definition at line 371 of file NoiseGate.h.
|
inlinenoexcept |
Sets release time in milliseconds. Non-finite values are ignored.
Definition at line 360 of file NoiseGate.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. Key channels beyond 16 are read unshaped. 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 opening the gate while what is wide still does. Keys of one channel or of more than two, and processSample(), 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 474 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 437 of file NoiseGate.h.
|
inlinenoexcept |
Plays the key instead of the gated audio.
Each channel puts out what the detector hears of it (the key after the high-pass and the equalizer), late by the lookahead (the latency the gate reports, so the track keeps its place) and held under full scale (KeyMonitor), while the gate keeps working underneath. A monitoring switch: it is not part of the state.
Definition at line 491 of file NoiseGate.h.
|
inline |
Restores parameters from a blob (tolerant; rejects foreign ids).
Definition at line 579 of file NoiseGate.h.
|
inlinenoexcept |
Sets the opening threshold.
| dB | Threshold in decibels. Non-finite values are ignored. |
Definition at line 300 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 615 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 603 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 698 of file NoiseGate.h.
|
inlineprotectednoexcept |
Definition at line 752 of file NoiseGate.h.
|
protected |
Definition at line 890 of file NoiseGate.h.
|
protected |
Definition at line 928 of file NoiseGate.h.
|
protected |
Definition at line 884 of file NoiseGate.h.
|
protected |
Its fall per sample (a factor).
Definition at line 939 of file NoiseGate.h.
|
protected |
The same while the key is quiet.
Definition at line 940 of file NoiseGate.h.
|
protected |
The reference (linear).
Definition at line 938 of file NoiseGate.h.
|
protected |
The thresholds count from the key's recent level.
Definition at line 936 of file NoiseGate.h.
|
protected |
Definition at line 911 of file NoiseGate.h.
|
protected |
Definition at line 937 of file NoiseGate.h.
|
protected |
Definition at line 907 of file NoiseGate.h.
|
protected |
Definition at line 909 of file NoiseGate.h.
|
protected |
Definition at line 914 of file NoiseGate.h.
|
protected |
Definition at line 910 of file NoiseGate.h.
|
protected |
Definition at line 913 of file NoiseGate.h.
|
protected |
Definition at line 908 of file NoiseGate.h.
|
protected |
Definition at line 912 of file NoiseGate.h.
|
protected |
Definition at line 906 of file NoiseGate.h.
|
protected |
Definition at line 944 of file NoiseGate.h.
|
protected |
Definition at line 888 of file NoiseGate.h.
|
protected |
Definition at line 943 of file NoiseGate.h.
|
protected |
Definition at line 961 of file NoiseGate.h.
|
protected |
Definition at line 955 of file NoiseGate.h.
|
protected |
Definition at line 953 of file NoiseGate.h.
|
protected |
Definition at line 952 of file NoiseGate.h.
|
protected |
Definition at line 954 of file NoiseGate.h.
|
protected |
Definition at line 951 of file NoiseGate.h.
|
protected |
Definition at line 950 of file NoiseGate.h.
|
protected |
Definition at line 947 of file NoiseGate.h.
|
protected |
Definition at line 889 of file NoiseGate.h.
|
protected |
The key of each channel in this frame.
Definition at line 926 of file NoiseGate.h.
|
protected |
Definition at line 948 of file NoiseGate.h.
|
protected |
Definition at line 885 of file NoiseGate.h.
|
protected |
Definition at line 930 of file NoiseGate.h.
|
protected |
Definition at line 883 of file NoiseGate.h.
|
staticconstexprprotected |
dB per second its reference falls.
Definition at line 933 of file NoiseGate.h.
|
staticconstexprprotected |
The reference never goes under -100 dBFS.
Definition at line 935 of file NoiseGate.h.
|
staticconstexprprotected |
Share of that fall while the key is under the closing threshold.
Definition at line 934 of file NoiseGate.h.
|
protected |
Equalizer of the key, after the high-pass.
Definition at line 921 of file NoiseGate.h.
|
protected |
The listened key: late by the lookahead, under full scale.
Definition at line 922 of file NoiseGate.h.
|
protected |
The equalizer has bands in the path (audio thread).
Definition at line 924 of file NoiseGate.h.
|
staticconstexprprotected |
Definition at line 601 of file NoiseGate.h.
|
staticconstexprprotected |
Definition at line 898 of file NoiseGate.h.
|
staticconstexprprotected |
Definition at line 897 of file NoiseGate.h.
|
staticconstexprprotected |
Definition at line 916 of file NoiseGate.h.
|
staticconstexprprotected |
Definition at line 957 of file NoiseGate.h.
|
protected |
scListen_ as the audio thread took it.
Definition at line 925 of file NoiseGate.h.
|
protected |
Definition at line 903 of file NoiseGate.h.
|
protected |
Channels with a prepared delay line.
Definition at line 902 of file NoiseGate.h.
|
protected |
Definition at line 899 of file NoiseGate.h.
|
protected |
Audio-side value (consumed in syncParams()).
Definition at line 900 of file NoiseGate.h.
|
protected |
Allocated delay (0 before prepare()).
Definition at line 901 of file NoiseGate.h.
|
protected |
Definition at line 891 of file NoiseGate.h.
|
protected |
Definition at line 960 of file NoiseGate.h.
|
protected |
Definition at line 887 of file NoiseGate.h.
|
protected |
Definition at line 929 of file NoiseGate.h.
|
protected |
Definition at line 886 of file NoiseGate.h.
|
protected |
Definition at line 880 of file NoiseGate.h.
|
protected |
Definition at line 918 of file NoiseGate.h.
|
protected |
Definition at line 917 of file NoiseGate.h.
|
protected |
Definition at line 893 of file NoiseGate.h.
|
protected |
Definition at line 894 of file NoiseGate.h.
|
protected |
Definition at line 920 of file NoiseGate.h.
|
protected |
Definition at line 919 of file NoiseGate.h.
|
protected |
The key goes out instead of the audio.
Definition at line 923 of file NoiseGate.h.
|
protected |
Definition at line 946 of file NoiseGate.h.
|
protected |
Definition at line 882 of file NoiseGate.h.
|
protected |
Definition at line 958 of file NoiseGate.h.
|
protected |
Definition at line 959 of file NoiseGate.h.