DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
dspark::NoiseGate< T > Class Template Reference

High-performance noise gate with state machine, hysteresis, and zero-allocation processing. More...

#include <NoiseGate.h>

Collaboration diagram for dspark::NoiseGate< T >:

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) 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 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.
 
processSample (T input) noexcept
 Processes a single mono sample.
 
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
 
computeEnvelopeFollower (T rawLevel) noexcept
 Updates envelope state to prevent intra-cycle chattering.
 
void updateStateMachine (T envelopeLinear) noexcept
 
void updateZeroCrossing (T sample) noexcept
 
getCurrentGain () noexcept
 
applyFrequencyGate (T input, int ch) noexcept
 
applyScHpf (T input, int ch) noexcept
 Per-channel one-pole sidechain high-pass (unity gain at Nyquist).
 
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< GateModegateMode_ { GateMode::Amplitude }
 
std::atomic< bool > adaptiveHold_ { false }
 
std::atomic< bool > paramsDirty_ { true }
 
std::atomic< bool > scHpfEnabled_ { false }
 
std::atomic< T > scHpfFreq_ { T(80) }
 
cachedThresholdLinear_ = T(0.01)
 
cachedCloseThresholdLinear_ = T(0.0063)
 
cachedRangeLinear_ = T(0.0001)
 
bool cachedDuck_ = false
 
GateMode cachedGateMode_ = GateMode::Amplitude
 
bool cachedAdaptiveHold_ = false
 
bool cachedScHpfEnabled_ = false
 
cachedNyquist_ = T(24000)
 
cachedFsInvPi2_ = T(0)
 
scHpfCoeff_ = T(0.995)
 
scHpfA0_ = T(0.9975)
 
std::array< T, kMaxScChannelsscHpfState_ {}
 
std::array< T, kMaxScChannelsscHpfPrev_ {}
 
attackCoeff_ = T(0)
 
releaseCoeff_ = T(0)
 
int holdSamples_ = 0
 
envelopeState_ = T(0)
 
detectorReleaseCoeff_ = T(0)
 
State state_ = State::Closed
 
gateGain_ = T(0)
 
int holdCounter_ = 0
 
freqSmoothCoeff_ = T(0)
 
std::array< T, kMaxChannelsfreqLpState_ {}
 
std::array< T, kMaxChannelsfreqHpState_ {}
 
std::array< T, kMaxChannelsfreqHpPrev_ {}
 
std::array< T, kMaxChannelsfreqLpFreq_ {}
 
std::array< T, kMaxChannelsfreqHpFreq_ {}
 
int zeroCrossCount_ = 0
 
int zeroCrossSamples_ = 0
 
bool prevSign_ = false
 
int estimatedPeriod_ = 0
 

Static Protected Attributes

static constexpr int kMaxChannels = 2
 
static constexpr int kMaxScChannels = 16
 
static constexpr int kZeroCrossWindow = 2048
 

Detailed Description

template<FloatType T>
class dspark::NoiseGate< T >

High-performance noise gate with state machine, hysteresis, and zero-allocation processing.

Template Parameters
TSample type (float or double).

Definition at line 77 of file NoiseGate.h.

Member Enumeration Documentation

◆ GateMode

template<FloatType T>
enum class dspark::NoiseGate::GateMode
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 91 of file NoiseGate.h.

◆ State

template<FloatType T>
enum class dspark::NoiseGate::State
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 83 of file NoiseGate.h.

Constructor & Destructor Documentation

◆ ~NoiseGate()

template<FloatType T>
dspark::NoiseGate< T >::~NoiseGate ( )
default

Member Function Documentation

◆ applyFrequencyGate()

template<FloatType T>
T dspark::NoiseGate< T >::applyFrequencyGate ( input,
int  ch 
)
inlineprotectednoexcept

Definition at line 576 of file NoiseGate.h.

◆ applyScHpf()

template<FloatType T>
T dspark::NoiseGate< T >::applyScHpf ( input,
int  ch 
)
inlineprotectednoexcept

Per-channel one-pole sidechain high-pass (unity gain at Nyquist).

Definition at line 600 of file NoiseGate.h.

◆ computeEnvelopeFollower()

template<FloatType T>
T dspark::NoiseGate< T >::computeEnvelopeFollower ( rawLevel)
inlineprotectednoexcept

Updates envelope state to prevent intra-cycle chattering.

Definition at line 493 of file NoiseGate.h.

◆ getCurrentGain()

template<FloatType T>
T dspark::NoiseGate< T >::getCurrentGain ( )
inlineprotectednoexcept

Definition at line 564 of file NoiseGate.h.

◆ getState()

template<FloatType T>
std::vector< uint8_t > dspark::NoiseGate< T >::getState ( ) const
inline

Serializes the parameter state (setup/UI threads; allocates).

Definition at line 396 of file NoiseGate.h.

◆ prepare() [1/2]

template<FloatType T>
void dspark::NoiseGate< T >::prepare ( const AudioSpec spec)
inlinenoexcept

Prepares from AudioSpec (unified API).

Parameters
specAudioSpec containing format details.

Definition at line 117 of file NoiseGate.h.

◆ prepare() [2/2]

template<FloatType T>
void dspark::NoiseGate< T >::prepare ( double  sampleRate)
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).

Parameters
sampleRateOperating sample rate in Hz (must be > 0).

Definition at line 105 of file NoiseGate.h.

◆ processBlock() [1/2]

template<FloatType T>
void dspark::NoiseGate< T >::processBlock ( AudioBufferView< T >  audio,
AudioBufferView< T >  sidechain 
)
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.

Parameters
audioAudio buffer to gate (modified in-place).
sidechainExternal sidechain signal (read-only).

Definition at line 185 of file NoiseGate.h.

◆ processBlock() [2/2]

template<FloatType T>
void dspark::NoiseGate< T >::processBlock ( AudioBufferView< T >  buffer)
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).

Parameters
bufferAudio buffer (mono or multi-channel).

Definition at line 128 of file NoiseGate.h.

◆ processSample()

template<FloatType T>
T dspark::NoiseGate< T >::processSample ( input)
inlinenoexcept

Processes a single mono sample.

Bit-identical to processBlock() for mono streams.

Note
No DenormalGuard here (per-sample hot path); per-sample callers are expected to guard their own processing loop.
Parameters
inputSource sample.
Returns
Gated output sample.

Definition at line 351 of file NoiseGate.h.

◆ processSampleInternal()

template<FloatType T>
T dspark::NoiseGate< T >::processSampleInternal ( input,
sidechain,
int  ch 
)
inlineprotectednoexcept

Definition at line 608 of file NoiseGate.h.

◆ processSampleWithSidechain()

template<FloatType T>
T dspark::NoiseGate< T >::processSampleWithSidechain ( input,
sidechain 
)
inlinenoexcept

Processes a mono sample using an external sidechain.

Parameters
inputSource sample.
sidechainKey/Sidechain sample.
Returns
Gated output sample.

Definition at line 363 of file NoiseGate.h.

◆ reset()

template<FloatType T>
void dspark::NoiseGate< T >::reset ( )
inlinenoexcept

Resets DSP state (clears filters, sets state to Closed).

Definition at line 372 of file NoiseGate.h.

◆ setAdaptiveHold()

template<FloatType T>
void dspark::NoiseGate< T >::setAdaptiveHold ( bool  enabled)
inlinenoexcept

Toggles adaptive hold based on zero-crossing rate.

Definition at line 314 of file NoiseGate.h.

◆ setAttack()

template<FloatType T>
void dspark::NoiseGate< T >::setAttack ( ms)
inlinenoexcept

Sets attack time in milliseconds. Non-finite values are ignored.

Definition at line 264 of file NoiseGate.h.

◆ setDuckMode()

template<FloatType T>
void dspark::NoiseGate< T >::setDuckMode ( bool  enabled)
inlinenoexcept

Toggles ducking mode (invert gate logic).

Definition at line 299 of file NoiseGate.h.

◆ setGateMode()

template<FloatType T>
void dspark::NoiseGate< T >::setGateMode ( GateMode  mode)
inlinenoexcept

Selects the processing mode (Amplitude or Frequency; wild enum values clamp).

Definition at line 306 of file NoiseGate.h.

◆ setHold()

template<FloatType T>
void dspark::NoiseGate< T >::setHold ( ms)
inlinenoexcept

Sets hold time in milliseconds. Non-finite values are ignored.

Definition at line 272 of file NoiseGate.h.

◆ setHysteresis()

template<FloatType T>
void dspark::NoiseGate< T >::setHysteresis ( dB)
inlinenoexcept

Sets the hysteresis amount (gap between open and close thresholds).

Parameters
dBHysteresis in decibels (floored to 0). Non-finite values are ignored.

Definition at line 256 of file NoiseGate.h.

◆ setRange()

template<FloatType T>
void dspark::NoiseGate< T >::setRange ( dB)
inlinenoexcept

Sets maximum attenuation range.

Parameters
dBRange in decibels (capped at 0). Non-finite values are ignored.

Definition at line 291 of file NoiseGate.h.

◆ setRelease()

template<FloatType T>
void dspark::NoiseGate< T >::setRelease ( ms)
inlinenoexcept

Sets release time in milliseconds. Non-finite values are ignored.

Definition at line 280 of file NoiseGate.h.

◆ setSidechainHPF()

template<FloatType T>
void dspark::NoiseGate< T >::setSidechainHPF ( bool  enabled,
double  cutoffHz = 80.0 
)
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).

Parameters
enabledFilter active state.
cutoffHzFilter cutoff in Hz (clamped to [1, 0.45 * fs]).

Definition at line 330 of file NoiseGate.h.

◆ setState()

template<FloatType T>
bool dspark::NoiseGate< T >::setState ( const uint8_t *  data,
size_t  size 
)
inline

Restores parameters from a blob (tolerant; rejects foreign ids).

Definition at line 414 of file NoiseGate.h.

◆ setThreshold()

template<FloatType T>
void dspark::NoiseGate< T >::setThreshold ( dB)
inlinenoexcept

Sets the opening threshold.

Parameters
dBThreshold in decibels. Non-finite values are ignored.

Definition at line 245 of file NoiseGate.h.

◆ syncParams()

template<FloatType T>
void dspark::NoiseGate< T >::syncParams ( )
inlineprotectednoexcept

Definition at line 444 of file NoiseGate.h.

◆ syncParamsIfDirty()

template<FloatType T>
void dspark::NoiseGate< T >::syncParamsIfDirty ( )
inlineprotectednoexcept

Definition at line 435 of file NoiseGate.h.

◆ updateStateMachine()

template<FloatType T>
void dspark::NoiseGate< T >::updateStateMachine ( envelopeLinear)
inlineprotectednoexcept

Definition at line 503 of file NoiseGate.h.

◆ updateZeroCrossing()

template<FloatType T>
void dspark::NoiseGate< T >::updateZeroCrossing ( sample)
inlineprotectednoexcept

Definition at line 544 of file NoiseGate.h.

Member Data Documentation

◆ adaptiveHold_

template<FloatType T>
std::atomic<bool> dspark::NoiseGate< T >::adaptiveHold_ { false }
protected

Definition at line 640 of file NoiseGate.h.

◆ attackCoeff_

template<FloatType T>
T dspark::NoiseGate< T >::attackCoeff_ = T(0)
protected

Definition at line 663 of file NoiseGate.h.

◆ attackMs_

template<FloatType T>
std::atomic<T> dspark::NoiseGate< T >::attackMs_ { T(0.5) }
protected

Definition at line 634 of file NoiseGate.h.

◆ cachedAdaptiveHold_

template<FloatType T>
bool dspark::NoiseGate< T >::cachedAdaptiveHold_ = false
protected

Definition at line 652 of file NoiseGate.h.

◆ cachedCloseThresholdLinear_

template<FloatType T>
T dspark::NoiseGate< T >::cachedCloseThresholdLinear_ = T(0.0063)
protected

Definition at line 648 of file NoiseGate.h.

◆ cachedDuck_

template<FloatType T>
bool dspark::NoiseGate< T >::cachedDuck_ = false
protected

Definition at line 650 of file NoiseGate.h.

◆ cachedFsInvPi2_

template<FloatType T>
T dspark::NoiseGate< T >::cachedFsInvPi2_ = T(0)
protected

Definition at line 655 of file NoiseGate.h.

◆ cachedGateMode_

template<FloatType T>
GateMode dspark::NoiseGate< T >::cachedGateMode_ = GateMode::Amplitude
protected

Definition at line 651 of file NoiseGate.h.

◆ cachedNyquist_

template<FloatType T>
T dspark::NoiseGate< T >::cachedNyquist_ = T(24000)
protected

Definition at line 654 of file NoiseGate.h.

◆ cachedRangeLinear_

template<FloatType T>
T dspark::NoiseGate< T >::cachedRangeLinear_ = T(0.0001)
protected

Definition at line 649 of file NoiseGate.h.

◆ cachedScHpfEnabled_

template<FloatType T>
bool dspark::NoiseGate< T >::cachedScHpfEnabled_ = false
protected

Definition at line 653 of file NoiseGate.h.

◆ cachedThresholdLinear_

template<FloatType T>
T dspark::NoiseGate< T >::cachedThresholdLinear_ = T(0.01)
protected

Definition at line 647 of file NoiseGate.h.

◆ detectorReleaseCoeff_

template<FloatType T>
T dspark::NoiseGate< T >::detectorReleaseCoeff_ = T(0)
protected

Definition at line 669 of file NoiseGate.h.

◆ duckMode_

template<FloatType T>
std::atomic<bool> dspark::NoiseGate< T >::duckMode_ { false }
protected

Definition at line 638 of file NoiseGate.h.

◆ envelopeState_

template<FloatType T>
T dspark::NoiseGate< T >::envelopeState_ = T(0)
protected

Definition at line 668 of file NoiseGate.h.

◆ estimatedPeriod_

template<FloatType T>
int dspark::NoiseGate< T >::estimatedPeriod_ = 0
protected

Definition at line 686 of file NoiseGate.h.

◆ freqHpFreq_

template<FloatType T>
std::array<T, kMaxChannels> dspark::NoiseGate< T >::freqHpFreq_ {}
protected

Definition at line 680 of file NoiseGate.h.

◆ freqHpPrev_

template<FloatType T>
std::array<T, kMaxChannels> dspark::NoiseGate< T >::freqHpPrev_ {}
protected

Definition at line 678 of file NoiseGate.h.

◆ freqHpState_

template<FloatType T>
std::array<T, kMaxChannels> dspark::NoiseGate< T >::freqHpState_ {}
protected

Definition at line 677 of file NoiseGate.h.

◆ freqLpFreq_

template<FloatType T>
std::array<T, kMaxChannels> dspark::NoiseGate< T >::freqLpFreq_ {}
protected

Definition at line 679 of file NoiseGate.h.

◆ freqLpState_

template<FloatType T>
std::array<T, kMaxChannels> dspark::NoiseGate< T >::freqLpState_ {}
protected

Definition at line 676 of file NoiseGate.h.

◆ freqSmoothCoeff_

template<FloatType T>
T dspark::NoiseGate< T >::freqSmoothCoeff_ = T(0)
protected

Definition at line 675 of file NoiseGate.h.

◆ gateGain_

template<FloatType T>
T dspark::NoiseGate< T >::gateGain_ = T(0)
protected

Definition at line 672 of file NoiseGate.h.

◆ gateMode_

template<FloatType T>
std::atomic<GateMode> dspark::NoiseGate< T >::gateMode_ { GateMode::Amplitude }
protected

Definition at line 639 of file NoiseGate.h.

◆ holdCounter_

template<FloatType T>
int dspark::NoiseGate< T >::holdCounter_ = 0
protected

Definition at line 673 of file NoiseGate.h.

◆ holdMs_

template<FloatType T>
std::atomic<T> dspark::NoiseGate< T >::holdMs_ { T(50) }
protected

Definition at line 635 of file NoiseGate.h.

◆ holdSamples_

template<FloatType T>
int dspark::NoiseGate< T >::holdSamples_ = 0
protected

Definition at line 665 of file NoiseGate.h.

◆ hysteresis_

template<FloatType T>
std::atomic<T> dspark::NoiseGate< T >::hysteresis_ { T(4) }
protected

Definition at line 633 of file NoiseGate.h.

◆ kMaxChannels

template<FloatType T>
constexpr int dspark::NoiseGate< T >::kMaxChannels = 2
staticconstexprprotected

Definition at line 433 of file NoiseGate.h.

◆ kMaxScChannels

template<FloatType T>
constexpr int dspark::NoiseGate< T >::kMaxScChannels = 16
staticconstexprprotected

Definition at line 657 of file NoiseGate.h.

◆ kZeroCrossWindow

template<FloatType T>
constexpr int dspark::NoiseGate< T >::kZeroCrossWindow = 2048
staticconstexprprotected

Definition at line 682 of file NoiseGate.h.

◆ paramsDirty_

template<FloatType T>
std::atomic<bool> dspark::NoiseGate< T >::paramsDirty_ { true }
protected

Definition at line 641 of file NoiseGate.h.

◆ prevSign_

template<FloatType T>
bool dspark::NoiseGate< T >::prevSign_ = false
protected

Definition at line 685 of file NoiseGate.h.

◆ rangeDb_

template<FloatType T>
std::atomic<T> dspark::NoiseGate< T >::rangeDb_ { T(-80) }
protected

Definition at line 637 of file NoiseGate.h.

◆ releaseCoeff_

template<FloatType T>
T dspark::NoiseGate< T >::releaseCoeff_ = T(0)
protected

Definition at line 664 of file NoiseGate.h.

◆ releaseMs_

template<FloatType T>
std::atomic<T> dspark::NoiseGate< T >::releaseMs_ { T(100) }
protected

Definition at line 636 of file NoiseGate.h.

◆ sampleRate_

template<FloatType T>
double dspark::NoiseGate< T >::sampleRate_ = 48000.0
protected

Definition at line 630 of file NoiseGate.h.

◆ scHpfA0_

template<FloatType T>
T dspark::NoiseGate< T >::scHpfA0_ = T(0.9975)
protected

Definition at line 659 of file NoiseGate.h.

◆ scHpfCoeff_

template<FloatType T>
T dspark::NoiseGate< T >::scHpfCoeff_ = T(0.995)
protected

Definition at line 658 of file NoiseGate.h.

◆ scHpfEnabled_

template<FloatType T>
std::atomic<bool> dspark::NoiseGate< T >::scHpfEnabled_ { false }
protected

Definition at line 643 of file NoiseGate.h.

◆ scHpfFreq_

template<FloatType T>
std::atomic<T> dspark::NoiseGate< T >::scHpfFreq_ { T(80) }
protected

Definition at line 644 of file NoiseGate.h.

◆ scHpfPrev_

template<FloatType T>
std::array<T, kMaxScChannels> dspark::NoiseGate< T >::scHpfPrev_ {}
protected

Definition at line 661 of file NoiseGate.h.

◆ scHpfState_

template<FloatType T>
std::array<T, kMaxScChannels> dspark::NoiseGate< T >::scHpfState_ {}
protected

Definition at line 660 of file NoiseGate.h.

◆ state_

template<FloatType T>
State dspark::NoiseGate< T >::state_ = State::Closed
protected

Definition at line 671 of file NoiseGate.h.

◆ threshold_

template<FloatType T>
std::atomic<T> dspark::NoiseGate< T >::threshold_ { T(-40) }
protected

Definition at line 632 of file NoiseGate.h.

◆ zeroCrossCount_

template<FloatType T>
int dspark::NoiseGate< T >::zeroCrossCount_ = 0
protected

Definition at line 683 of file NoiseGate.h.

◆ zeroCrossSamples_

template<FloatType T>
int dspark::NoiseGate< T >::zeroCrossSamples_ = 0
protected

Definition at line 684 of file NoiseGate.h.


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