|
DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
|
Real-time audio clipper with analog modeling and anti-aliasing features. More...
#include <Clipper.h>

Classes | |
| struct | AdaaStage |
Public Types | |
| enum class | Mode { Hard , Soft , Analog , GoldenRatio } |
| Defines the harmonic waveshaping algorithm used for clipping. More... | |
| enum class | OversamplingMode { LinearPhase , MinimumPhase } |
| How the oversampler filters (see setOversamplingMode()). More... | |
| using | OversamplingBand = typename Oversampling< T >::Band |
| The band the oversampler's first stage keeps (see setOversamplingBand()). | |
Public Member Functions | |
| void | prepare (const AudioSpec &spec) |
| Prepares the clipper for processing, allocating any necessary internal buffers. | |
| void | reset () noexcept |
| Resets the internal state of the clipper. | |
| void | processBlock (AudioBufferView< T > buffer) noexcept |
| Processes an audio buffer in-place through the clipping algorithm. | |
| void | setMode (Mode mode) noexcept |
| Sets the clipping algorithm. | |
| void | setCeiling (T dB) noexcept |
| Sets the absolute maximum output level. | |
| void | setInputGain (T dB) noexcept |
| Sets the input drive/gain before clipping. | |
| void | setStages (int count) noexcept |
| Sets the number of cascaded clipping stages. | |
| void | setMix (T amount) noexcept |
| Sets the dry/wet ratio of the processor. | |
| void | setSlewLimit (T ms) noexcept |
| Enables slew limiting to soften clipping edges. | |
| void | setOversampling (int factor) noexcept |
| Sets the oversampling multiplier to mitigate aliasing. | |
| void | setAutomaticOversampling (bool on) noexcept |
| Lets the host rate choose the factor (applied by the next prepare(), as setOversampling()). | |
| bool | isOversamplingAutomatic () const noexcept |
| Whether the factor follows the host rate (setAutomaticOversampling()). | |
| void | setOversamplingMode (OversamplingMode mode) noexcept |
| Chooses the oversampler's filters (applied by the next prepare()). | |
| OversamplingMode | getOversamplingMode () const noexcept |
| The oversampler's filters (setOversamplingMode()). | |
| void | setOversamplingBand (OversamplingBand band) noexcept |
| Chooses the band the oversampler's first stage keeps (applied by the next prepare()). | |
| OversamplingBand | getOversamplingBand () const noexcept |
| The band of the oversampler's first stage (setOversamplingBand()). | |
| Mode | getMode () const noexcept |
| T | getCeiling () const noexcept |
| T | getInputGain () const noexcept |
| int | getStages () const noexcept |
| T | getMix () const noexcept |
| T | getSlewLimit () const noexcept |
| int | getOversampling () const noexcept |
| Returns the published oversampling factor (applied on the next prepare()). | |
| void | setAntialiasing (bool on) noexcept |
| Enables second-order antiderivative antialiasing on every mode. | |
| bool | getAntialiasing () const noexcept |
| Whether second-order antialiasing is enabled. | |
| int | getLatency () const noexcept |
| Latency in samples: oversampling filters plus, when enabled, the antialiasing delay. | |
| T | getGainReductionDb () const noexcept |
| Retrieves the maximum gain reduction applied during the last block (for UI metering). | |
| 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. Oversampling factor applies on the next prepare() as usual. | |
Protected Member Functions | |
| void | processInternal (AudioBufferView< T > &buffer, double currentSampleRate) noexcept |
| Core DSP routing. Resolves atomics and branches to the per-mode template. | |
| int | adaaBaseLatency () const noexcept |
| void | resetAntialiasState () noexcept |
| void | pushDryAligned (AudioBufferView< T > buffer, int extra) noexcept |
| template<class Curve > | |
| void | dispatchAntialiased (AudioBufferView< T > &buffer, int numStages, T slewScale) noexcept |
| Second-order antialiased per-mode loop. Works in units of the ceiling: each stage maps u -> curve(u) with u = stage gain times the previous stage's output. Every stage delays by one processing sample; a pad brings the total to factor * adaaBaseLatency() samples for any stage count. | |
| void | snapParameters () noexcept |
| template<Mode M> | |
| void | dispatchClipping (AudioBufferView< T > &buffer, int numStages, T slewScale) noexcept |
| Per-mode processing loop (waveshaper inlined at compile time; the slew state and peak metering keep the loop serial per channel). | |
Static Protected Member Functions | |
| static T | stageGainFor (T totalGainLin, int numStages) noexcept |
| template<Mode M> | |
| static T | processSample (T sample, T ceiling) noexcept |
| Compile-time resolution of the waveshaping math. | |
| static T | dbToLinear (T dB) noexcept |
| static T | gainToDecibels (T linear, T minusInfinityDb) noexcept |
Protected Attributes | |
| AudioSpec | spec_ {} |
| std::atomic< bool > | prepared_ { false } |
| DryWetMixer< T > | mixer_ |
| std::unique_ptr< Oversampling< T > > | oversampler_ |
| std::atomic< Mode > | mode_ { Mode::Hard } |
| std::atomic< T > | ceilingDb_ { T(0) } |
| std::atomic< T > | inputGainDb_ { T(0) } |
| std::atomic< int > | stages_ { 1 } |
| std::atomic< T > | mix_ { T(1) } |
| std::atomic< T > | slewLimitMs_ { T(0) } |
| std::atomic< int > | osFactor_ { 1 } |
| std::atomic< bool > | osAutomatic_ { false } |
| The factor follows the host rate. | |
| std::atomic< OversamplingMode > | osMode_ { OversamplingMode::LinearPhase } |
| setOversamplingMode(). | |
| std::atomic< OversamplingBand > | osBand_ { OversamplingBand::Preset } |
| setOversamplingBand(). | |
| OversamplingDryPath< T > | dryPath_ |
| The dry copy through minimum-phase filters. | |
| bool | dryThroughFilters_ = false |
| The dry copy crosses the filters (minimum phase). | |
| T | slewPrev_ [kMaxChannels] {} |
| SmoothedValue< T > | stageGainSm_ |
| SmoothedValue< T > | ceilingSm_ |
| int | lastStages_ = 1 |
| Stage count the stage-gain glide refers to. | |
| std::array< T, kChunk > | gainRamp_ {} |
| std::array< T, kChunk > | ceilRamp_ {} |
| std::atomic< T > | gainReductionDb_ { T(0) } |
| std::atomic< bool > | antialias_ { false } |
| bool | adaaActive_ = false |
| antialias_ as applied (audio thread). | |
| bool | adaaFresh_ = true |
| Seed the stage history on the next block. | |
| Mode | adaaMode_ = Mode::Hard |
| Curve the cached values belong to. | |
| int | adaaStageCount_ = 1 |
| int | processFactor_ = 1 |
| Oversampling factor in use. | |
| std::array< std::array< AdaaStage, kMaxStages >, kMaxChannels > | adaaStages_ {} |
| std::array< std::array< double, kPadRing >, kMaxChannels > | padRing_ {} |
| int | padPos_ = 0 |
| std::array< std::array< T, kMaxStages >, kMaxChannels > | dryRing_ {} |
| Dry delay, <= 4 samples. | |
| int | dryPos_ = 0 |
| AudioBuffer< T > | dryScratch_ |
Static Protected Attributes | |
| static constexpr int | kMaxStages = 4 |
| static constexpr int | kMaxChannels = 16 |
| static constexpr T | kPhi = static_cast<T>(1.6180339887498948482) |
| Mathematical Golden Ratio used for the GoldenRatio soft-knee transition. | |
| static constexpr double | kAutomaticInternalRate = 88200.0 |
| Rate the automatic factor reaches. | |
| static constexpr double | kParamRampMs = 20.0 |
| static constexpr int | kChunk = 256 |
| static constexpr int | kPadRing = 32 |
| |
Real-time audio clipper with analog modeling and anti-aliasing features.
| T | Sample type (must satisfy dspark::FloatType, typically float or double). |
| using dspark::Clipper< T >::OversamplingBand = typename Oversampling<T>::Band |
The band the oversampler's first stage keeps (see setOversamplingBand()).
|
strong |
Defines the harmonic waveshaping algorithm used for clipping.
|
strong |
How the oversampler filters (see setOversamplingMode()).
|
inlineprotectednoexcept |
|
inlinestaticprotectednoexcept |
|
inlineprotectednoexcept |
Second-order antialiased per-mode loop. Works in units of the ceiling: each stage maps u -> curve(u) with u = stage gain times the previous stage's output. Every stage delays by one processing sample; a pad brings the total to factor * adaaBaseLatency() samples for any stage count.
|
inlineprotectednoexcept |
|
inlinestaticprotectednoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
The band of the oversampler's first stage (setOversamplingBand()).
|
inlinenoexcept |
The oversampler's filters (setOversamplingMode()).
|
inlinenoexcept |
|
inlinenoexcept |
|
inline |
|
inlinenoexcept |
Whether the factor follows the host rate (setAutomaticOversampling()).
|
inline |
Prepares the clipper for processing, allocating any necessary internal buffers.
If oversampling is configured > 1x, this method allocates the oversampling filters. Call it from the main/setup thread before playback begins (never concurrently with processBlock), and again whenever the sample rate changes.
Invalid specs (non-positive/non-finite rate, block size or channel count) are ignored: the previous state is kept and an unprepared instance stays pass-through.
| spec | The current audio environment specifications (sample rate, max block size). |
|
inlinenoexcept |
|
inlineprotectednoexcept |
|
inlinestaticprotectednoexcept |
|
inlineprotectednoexcept |
|
inlinenoexcept |
|
inlineprotectednoexcept |
|
inlinenoexcept |
Enables second-order antiderivative antialiasing on every mode.
Off by default. Any thread; applied from the next block. Adds ceil(4 / oversampling factor) base samples of latency (reported by getLatency() immediately) and the passband droop described in the file documentation, so pair it with oversampling.
|
inlinenoexcept |
Lets the host rate choose the factor (applied by the next prepare(), as setOversampling()).
With it on, every prepare() takes the smallest factor that brings the clipper to an internal rate of at least 88.2 kHz: 2x at 44.1 and 48 kHz and none from 88.2 kHz up. The nonlinearity then runs at about the same rate whatever the session rate, and a high-rate session stops paying for a factor it does not need. Off by default; an explicit setOversampling() turns it off.
| on | True to choose the factor from the rate. |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Sets the oversampling multiplier to mitigate aliasing.
Values are rounded UP to the next power of two and capped at 16. At 16x every decimation stage is at least 100 dB deep (Oversampling::Depth::Deep; 95 samples of latency, 73 at 8x): a hard clip at +24 dB with antialiasing aliases at -115 dBc there (-95 with the preset's stages).
prepare() to allocate filters.| factor | Oversampling ratio (1 = off, 2, 4, 8, 16). |
|
inlinenoexcept |
Chooses the band the oversampler's first stage keeps (applied by the next prepare()).
Preset, the default, keeps the band up to 0.46 of the host rate at every rate. Audible keeps it up to 20 kHz and stops everything from the image of 20 kHz up, to the same depth: the stage is fitted to the host rate and is shorter the higher the rate (see Oversampling::Band). At 2x the linear-phase latency goes from 64 samples to 56 at 44.1 kHz, 32 at 48 kHz and 10 at 96 kHz. What is given up lies over 20 kHz.
| band | Preset (default) or Audible. |
|
inlinenoexcept |
Chooses the oversampler's filters (applied by the next prepare()).
MinimumPhase runs polyphase allpass half-bands: a few samples of latency instead of the factor's linear-phase latency, and a phase that bends toward the band edge, which the dry copy follows through the same filters so a partial mix does not comb-filter.
| mode | LinearPhase (default) or MinimumPhase. |
|
inlinenoexcept |
Enables slew limiting to soften clipping edges.
Limits how fast the output can change over time. Defined in milliseconds, so the effect stays consistent regardless of the sample rate or oversampling factor. Note that a slew limiter is itself nonlinear: it darkens the transient edges and top octave rather than strictly removing aliasing (use oversampling for that).
| ms | Rise time in milliseconds to reach the ceiling from zero. 0 = off; non-finite values are ignored. |
|
inlinenoexcept |
Sets the number of cascaded clipping stages.
Multi-stage clipping distributes the input gain logarithmically across multiple clipping algorithms. This alters the harmonic profile, making heavy distortion sound smoother compared to a single aggressive stage.
| count | Number of stages. Clamped between 1 and 4. |
|
inline |
Restores parameters from a blob. Oversampling factor applies on the next prepare() as usual.
|
inlineprotectednoexcept |
|
inlinestaticprotectednoexcept |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
staticconstexprprotected |
|
staticconstexprprotected |
|
staticconstexprprotected |
|
staticconstexprprotected |
|
staticconstexprprotected |
|
staticconstexprprotected |
|
staticconstexprprotected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
Oversampling factor in use.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |