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

Artifact-free, SIMD-friendly crossfader for two audio signals. More...

#include <Crossfade.h>

Public Types

enum class  Curve { Linear , EqualPower , SCurve }
 Defines the amplitude response of the crossfade transition. More...
 

Public Member Functions

 Crossfade ()=default
 
 ~Crossfade ()=default
 
void prepare (double sampleRate) noexcept
 Enables the time-based glide (optional).
 
void prepare (const AudioSpec &spec) noexcept
 Enables the time-based glide from an audio spec (see prepare(double)).
 
void reset () noexcept
 Lands the applied position and curve on the requested ones, with no glide (for example at transport start). Settings changed before the next processing call are applied at once too.
 
void setCurve (Curve curve) noexcept
 Sets the crossfade curve type. Thread-safe. Can be called from the GUI thread. A curve change is blended from the old law to the new one across the next block.
 
void setPosition (T position) noexcept
 Sets the target crossfade blend position.
 
T getPosition () const noexcept
 Retrieves the last requested position.
 
T process (T a, T b) noexcept
 Crossfades two individual samples.
 
void process (const T *inputA, const T *inputB, T *output, int numSamples) noexcept
 Crossfades two audio buffers into an output buffer with automatic parameter smoothing.
 
void processAutomated (const T *inputA, const T *inputB, const T *positions, T *output, int numSamples) noexcept
 Processes crossfading using a per-sample automation buffer.
 
T getGainA () const noexcept
 Gets the current internal gain multiplier for signal A. Safe from any thread: loads a published atomic word, so it may be up to one processing call behind.
 
T getGainB () const noexcept
 Gets the current internal gain multiplier for signal B. Safe from any thread: loads a published atomic word, so it may be up to one processing call behind.
 

Static Public Member Functions

static void gainsFor (Curve curve, T position, T &gainA, T &gainB) noexcept
 The gains a curve applies at a position.
 

Detailed Description

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

Artifact-free, SIMD-friendly crossfader for two audio signals.

Marked as final to explicitly prohibit inheritance and avoid vtable overhead, adhering to the framework's zero virtual dispatch policy in DSP nodes.

Template Parameters
TSample type (float or double). Requires std::is_floating_point_v<T>.

Definition at line 62 of file Crossfade.h.

Member Enumeration Documentation

◆ Curve

template<FloatType T>
enum class dspark::Crossfade::Curve
strong

Defines the amplitude response of the crossfade transition.

Enumerator
Linear 

Linear interpolation. Constant amplitude, drops power at center.

EqualPower 

Sine/cosine law. Constant power, no volume drop, smooth at both ends.

SCurve 

Smoothstep interpolation. Slower progression at extremes.

Definition at line 66 of file Crossfade.h.

Constructor & Destructor Documentation

◆ Crossfade()

template<FloatType T>
dspark::Crossfade< T >::Crossfade ( )
default

◆ ~Crossfade()

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

Member Function Documentation

◆ gainsFor()

template<FloatType T>
static void dspark::Crossfade< T >::gainsFor ( Curve  curve,
T  position,
T &  gainA,
T &  gainB 
)
inlinestaticnoexcept

The gains a curve applies at a position.

The exact law the processing calls use, so a caller can predict or normalize a blend without running one.

Parameters
curveCrossfade curve.
positionBlend position, clamped to [0, 1] (NaN reads as 0).
gainAReceives the gain of signal A.
gainBReceives the gain of signal B.

Definition at line 159 of file Crossfade.h.

◆ getGainA()

template<FloatType T>
T dspark::Crossfade< T >::getGainA ( ) const
inlinenoexcept

Gets the current internal gain multiplier for signal A. Safe from any thread: loads a published atomic word, so it may be up to one processing call behind.

Definition at line 314 of file Crossfade.h.

◆ getGainB()

template<FloatType T>
T dspark::Crossfade< T >::getGainB ( ) const
inlinenoexcept

Gets the current internal gain multiplier for signal B. Safe from any thread: loads a published atomic word, so it may be up to one processing call behind.

Definition at line 322 of file Crossfade.h.

◆ getPosition()

template<FloatType T>
T dspark::Crossfade< T >::getPosition ( ) const
inlinenoexcept

Retrieves the last requested position.

Returns
Current requested blend position [0, 1].

Definition at line 143 of file Crossfade.h.

◆ prepare() [1/2]

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

Enables the time-based glide from an audio spec (see prepare(double)).

Parameters
specAudio environment; only the sample rate is used.

Definition at line 98 of file Crossfade.h.

◆ prepare() [2/2]

template<FloatType T>
void dspark::Crossfade< T >::prepare ( double  sampleRate)
inlinenoexcept

Enables the time-based glide (optional).

Once prepared, position changes move the applied position by at most one full sweep per 20 ms in both processing calls, independently of the block size: a per-block ramp lands in 0.7 ms with 32-sample blocks and clicks. Also lands the applied position on the requested one (reset()). Invalid rates (non-positive or non-finite) are ignored.

Parameters
sampleRateSample rate in Hz.

Definition at line 87 of file Crossfade.h.

◆ process() [1/2]

template<FloatType T>
void dspark::Crossfade< T >::process ( const T *  inputA,
const T *  inputB,
T *  output,
int  numSamples 
)
inlinenoexcept

Crossfades two audio buffers into an output buffer with automatic parameter smoothing.

While the applied position is settled this is a vectorized static gain loop. During a glide (see the file overview) the curve is evaluated at every sample; a curve change is blended from the old law to the new one across the block.

Parameters
inputAPointer to the first input buffer array. Must not be null.
inputBPointer to the second input buffer array. Must not be null.
outputPointer to the output buffer array. Must not be null. May alias either input.
numSamplesNumber of samples to process. Must be > 0.

Definition at line 207 of file Crossfade.h.

◆ process() [2/2]

template<FloatType T>
T dspark::Crossfade< T >::process ( T  a,
T  b 
)
inlinenoexcept

Crossfades two individual samples.

Unprepared, the gains follow the position at once, which suits per-sample automation through setPosition(). Prepared, the applied position glides at the same rate as in the block call. Must only be called from the audio thread.

Parameters
aInput sample A (Dry/Left).
bInput sample B (Wet/Right).
Returns
Blended output sample.

Definition at line 178 of file Crossfade.h.

◆ processAutomated()

template<FloatType T>
void dspark::Crossfade< T >::processAutomated ( const T *  inputA,
const T *  inputB,
const T *  positions,
T *  output,
int  numSamples 
)
inlinenoexcept

Processes crossfading using a per-sample automation buffer.

The positions are applied as given (no glide). The next call of the other processing methods glides from the last automated position to the requested one.

Parameters
inputAPointer to the first input buffer.
inputBPointer to the second input buffer.
positionsArray of target positions [0, 1] per sample. Values are clamped; NaN reads as 0 (100% A).
outputPointer to the output buffer.
numSamplesNumber of samples to process.

Definition at line 279 of file Crossfade.h.

◆ reset()

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

Lands the applied position and curve on the requested ones, with no glide (for example at transport start). Settings changed before the next processing call are applied at once too.

Definition at line 105 of file Crossfade.h.

◆ setCurve()

template<FloatType T>
void dspark::Crossfade< T >::setCurve ( Curve  curve)
inlinenoexcept

Sets the crossfade curve type. Thread-safe. Can be called from the GUI thread. A curve change is blended from the old law to the new one across the next block.

Parameters
curveThe desired crossfade curve. Out-of-range values (a wild cast) are clamped into the enum range.

Definition at line 118 of file Crossfade.h.

◆ setPosition()

template<FloatType T>
void dspark::Crossfade< T >::setPosition ( T  position)
inlinenoexcept

Sets the target crossfade blend position.

Thread-safe. The processing calls glide to it (see the file overview).

Parameters
positionTarget blend: 0.0 = 100% A, 1.0 = 100% B. Automatically clamped [0, 1]; non-finite values are ignored.

Definition at line 133 of file Crossfade.h.


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