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

Professional click-free gain processor. More...

#include <Gain.h>

Collaboration diagram for dspark::Gain< T >:

Public Member Functions

 Gain ()
 
 ~Gain ()=default
 
void prepare (double sampleRate, double rampTimeMs=10.0)
 Prepares the gain processor for playback.
 
void prepare (const AudioSpec &spec)
 Prepares from AudioSpec, preserving existing ramp time.
 
void setGainDb (T dB) noexcept
 Sets the target gain in decibels. (Thread-safe, callable from UI).
 
void setGainLinear (T linear) noexcept
 Sets the target gain as a linear multiplier. (Thread-safe, callable from UI).
 
getGainDb () const noexcept
 Returns current target gain in dB.
 
getGainLinear () const noexcept
 Returns current target linear gain.
 
getCurrentGain () const noexcept
 Returns the current internal smoothed value.
 
void setMuted (bool muted) noexcept
 Enables or disables mute smoothly. (Thread-safe, callable from UI).
 
bool isMuted () const noexcept
 
void setInverted (bool inverted) noexcept
 Enables smooth phase inversion. (Thread-safe, callable from UI). Reverses polarity by ramping smoothly through zero, avoiding clicks.
 
bool isInverted () const noexcept
 
void setRampTime (double rampTimeMs) noexcept
 Sets the smoothing ramp time. (Thread-safe). Non-finite values are ignored; negatives clamp to 0.
 
void processBlock (AudioBufferView< T > buffer) noexcept
 Processes an AudioBufferView in-place. (Audio Thread only).
 
void process (T *data, int numSamples) noexcept
 Processes an interleaved or single-channel buffer in-place.
 
void process (T **channelData, int numChannels, int numSamples) noexcept
 Processes separate channel buffers in-place.
 
void process (const T *input, T *output, int numSamples) noexcept
 Processes input to output (not in-place).
 
void skipRamp () noexcept
 Skips smoothing - immediately sets current gain to target.
 
void reset () noexcept
 Resets gain internal state to match targets immediately.
 
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 updateInternalState () noexcept
 Synchronizes atomic UI variables with audio thread state. Must be called at the start of any audio processing block.
 
void forceSynchronize () noexcept
 Forces instantaneous synchronization of state without ramping.
 

Protected Attributes

double sampleRate_ = 48000.0
 
currentTarget_ { T(1) }
 
SmoothedValue< T > gainSmooth_
 
std::atomic< T > targetGainLinear_ { T(1) }
 
std::atomic< double > rampTimeMs_ { 10.0 }
 
std::atomic< bool > rampTimeChanged_ { false }
 
std::atomic< bool > muted_ { false }
 
std::atomic< bool > inverted_ { false }
 

Detailed Description

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

Professional click-free gain processor.

Features:

  • Thread-safe lock-free parameter updates.
  • Exponential smoothing for perceptually uniform transitions.
  • Smooth cross-zero transitions for phase inversion (no clicks).
  • SIMD-accelerated bulk processing.
Template Parameters
TSample type (float or double).

Definition at line 47 of file Gain.h.

Constructor & Destructor Documentation

◆ Gain()

template<FloatType T>
dspark::Gain< T >::Gain ( )
inline

Definition at line 50 of file Gain.h.

◆ ~Gain()

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

Member Function Documentation

◆ forceSynchronize()

template<FloatType T>
void dspark::Gain< T >::forceSynchronize ( )
inlineprotectednoexcept

Forces instantaneous synchronization of state without ramping.

Definition at line 311 of file Gain.h.

◆ getCurrentGain()

template<FloatType T>
T dspark::Gain< T >::getCurrentGain ( ) const
inlinenoexcept

Returns the current internal smoothed value.

Note
Exact on the audio thread; from other threads (metering) the read is unsynchronized and merely approximate.

Definition at line 118 of file Gain.h.

◆ getGainDb()

template<FloatType T>
T dspark::Gain< T >::getGainDb ( ) const
inlinenoexcept

Returns current target gain in dB.

Definition at line 104 of file Gain.h.

◆ getGainLinear()

template<FloatType T>
T dspark::Gain< T >::getGainLinear ( ) const
inlinenoexcept

Returns current target linear gain.

Definition at line 110 of file Gain.h.

◆ getState()

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

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

Definition at line 264 of file Gain.h.

◆ isInverted()

template<FloatType T>
bool dspark::Gain< T >::isInverted ( ) const
inlinenoexcept

Definition at line 142 of file Gain.h.

◆ isMuted()

template<FloatType T>
bool dspark::Gain< T >::isMuted ( ) const
inlinenoexcept

Definition at line 131 of file Gain.h.

◆ prepare() [1/2]

template<FloatType T>
void dspark::Gain< T >::prepare ( const AudioSpec spec)
inline

Prepares from AudioSpec, preserving existing ramp time.

Definition at line 76 of file Gain.h.

◆ prepare() [2/2]

template<FloatType T>
void dspark::Gain< T >::prepare ( double  sampleRate,
double  rampTimeMs = 10.0 
)
inline

Prepares the gain processor for playback.

Parameters
sampleRateSample rate in Hz. Non-positive or NaN rates are ignored.
rampTimeMsSmoothing time in milliseconds (default: 10 ms). Non-finite values are ignored; negatives clamp to 0.

Definition at line 63 of file Gain.h.

◆ process() [1/3]

template<FloatType T>
void dspark::Gain< T >::process ( const T *  input,
T *  output,
int  numSamples 
)
inlinenoexcept

Processes input to output (not in-place).

Definition at line 229 of file Gain.h.

◆ process() [2/3]

template<FloatType T>
void dspark::Gain< T >::process ( T **  channelData,
int  numChannels,
int  numSamples 
)
inlinenoexcept

Processes separate channel buffers in-place.

Definition at line 205 of file Gain.h.

◆ process() [3/3]

template<FloatType T>
void dspark::Gain< T >::process ( T *  data,
int  numSamples 
)
inlinenoexcept

Processes an interleaved or single-channel buffer in-place.

Definition at line 184 of file Gain.h.

◆ processBlock()

template<FloatType T>
void dspark::Gain< T >::processBlock ( AudioBufferView< T >  buffer)
inlinenoexcept

Processes an AudioBufferView in-place. (Audio Thread only).

Note
Channels beyond the 16th pass through unprocessed (the pointer scratch is fixed-size).

Definition at line 160 of file Gain.h.

◆ reset()

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

Resets gain internal state to match targets immediately.

Definition at line 257 of file Gain.h.

◆ setGainDb()

template<FloatType T>
void dspark::Gain< T >::setGainDb ( dB)
inlinenoexcept

Sets the target gain in decibels. (Thread-safe, callable from UI).

Parameters
dBGain in dB (0 = unity). Non-finite values are ignored.

Definition at line 85 of file Gain.h.

◆ setGainLinear()

template<FloatType T>
void dspark::Gain< T >::setGainLinear ( linear)
inlinenoexcept

Sets the target gain as a linear multiplier. (Thread-safe, callable from UI).

Parameters
linearGain multiplier (>= 0). Non-finite values are ignored (without the guard, max(0, NaN) resolved to 0, i.e. a silent accidental mute).

Definition at line 97 of file Gain.h.

◆ setInverted()

template<FloatType T>
void dspark::Gain< T >::setInverted ( bool  inverted)
inlinenoexcept

Enables smooth phase inversion. (Thread-safe, callable from UI). Reverses polarity by ramping smoothly through zero, avoiding clicks.

Definition at line 137 of file Gain.h.

◆ setMuted()

template<FloatType T>
void dspark::Gain< T >::setMuted ( bool  muted)
inlinenoexcept

Enables or disables mute smoothly. (Thread-safe, callable from UI).

Definition at line 126 of file Gain.h.

◆ setRampTime()

template<FloatType T>
void dspark::Gain< T >::setRampTime ( double  rampTimeMs)
inlinenoexcept

Sets the smoothing ramp time. (Thread-safe). Non-finite values are ignored; negatives clamp to 0.

Definition at line 148 of file Gain.h.

◆ setState()

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

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

Definition at line 275 of file Gain.h.

◆ skipRamp()

template<FloatType T>
void dspark::Gain< T >::skipRamp ( )
inlinenoexcept

Skips smoothing - immediately sets current gain to target.

Definition at line 251 of file Gain.h.

◆ updateInternalState()

template<FloatType T>
void dspark::Gain< T >::updateInternalState ( )
inlineprotectednoexcept

Synchronizes atomic UI variables with audio thread state. Must be called at the start of any audio processing block.

Definition at line 291 of file Gain.h.

Member Data Documentation

◆ currentTarget_

template<FloatType T>
T dspark::Gain< T >::currentTarget_ { T(1) }
protected

Definition at line 326 of file Gain.h.

◆ gainSmooth_

template<FloatType T>
SmoothedValue<T> dspark::Gain< T >::gainSmooth_
protected

Definition at line 327 of file Gain.h.

◆ inverted_

template<FloatType T>
std::atomic<bool> dspark::Gain< T >::inverted_ { false }
protected

Definition at line 334 of file Gain.h.

◆ muted_

template<FloatType T>
std::atomic<bool> dspark::Gain< T >::muted_ { false }
protected

Definition at line 333 of file Gain.h.

◆ rampTimeChanged_

template<FloatType T>
std::atomic<bool> dspark::Gain< T >::rampTimeChanged_ { false }
protected

Definition at line 332 of file Gain.h.

◆ rampTimeMs_

template<FloatType T>
std::atomic<double> dspark::Gain< T >::rampTimeMs_ { 10.0 }
protected

Definition at line 331 of file Gain.h.

◆ sampleRate_

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

Definition at line 323 of file Gain.h.

◆ targetGainLinear_

template<FloatType T>
std::atomic<T> dspark::Gain< T >::targetGainLinear_ { T(1) }
protected

Definition at line 330 of file Gain.h.


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