DSPark 1.8.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::Gain< T > Class Template Reference

Professional click-free gain processor. More...

#include <Gain.h>

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).
 
T getGainDb () const noexcept
 Returns current target gain in dB.
 
T getGainLinear () const noexcept
 Returns current target linear gain.
 
T 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 publishGain () noexcept
 Publishes the smoother's value for cross-thread metering.
 
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
 
T currentTarget_ { T(1) }
 
SmoothedValue< T > gainSmooth_
 
std::atomic< T > publishedGain_ { T(1) }
 Cross-thread metering readout.
 
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 332 of file Gain.h.

◆ getCurrentGain()

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

Returns the current internal smoothed value.

Note
Safe from any thread: it loads an atomic word the processing call publishes once per block, so off the audio thread it may be up to one block behind. It used to read the smoother directly, which is a plain word the audio thread writes – a data race, not merely an approximate number.

Definition at line 121 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 273 of file Gain.h.

◆ isInverted()

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

Definition at line 145 of file Gain.h.

◆ isMuted()

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

Definition at line 134 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 236 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 210 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 187 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 163 of file Gain.h.

◆ publishGain()

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

Publishes the smoother's value for cross-thread metering.

getCurrentGain() used to read the smoother directly: a plain word the audio thread advances every sample, read from another thread. One relaxed store per processing call, outside every per-sample loop.

Definition at line 303 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 266 of file Gain.h.

◆ setGainDb()

template<FloatType T>
void dspark::Gain< T >::setGainDb ( T  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 ( T  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 140 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 129 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 151 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 284 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 260 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 312 of file Gain.h.

Member Data Documentation

◆ currentTarget_

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

Definition at line 348 of file Gain.h.

◆ gainSmooth_

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

Definition at line 349 of file Gain.h.

◆ inverted_

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

Definition at line 357 of file Gain.h.

◆ muted_

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

Definition at line 356 of file Gain.h.

◆ publishedGain_

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

Cross-thread metering readout.

Definition at line 352 of file Gain.h.

◆ rampTimeChanged_

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

Definition at line 355 of file Gain.h.

◆ rampTimeMs_

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

Definition at line 354 of file Gain.h.

◆ sampleRate_

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

Definition at line 345 of file Gain.h.

◆ targetGainLinear_

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

Definition at line 353 of file Gain.h.


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