Claude
Skills
Sign in
Back

dsp-cookbook

Included with Lifetime
$97 forever

Production-ready DSP algorithms including filters, compressors, delays, modulation effects, saturation, and distortion with JUCE integration and optimization techniques. Use when implementing audio processing, DSP algorithms, audio effects, dynamics processors, or need code examples for common audio operations.

Image & Video

What this skill does


# DSP Cookbook

Practical DSP algorithm implementations for audio plugins. Production-ready code examples with JUCE framework integration, covering filters, dynamics, modulation, delays, and common audio effects.

## Table of Contents

1. [Filters](#filters)
2. [Dynamics Processors](#dynamics-processors)
3. [Modulation Effects](#modulation-effects)
4. [Delay-Based Effects](#delay-based-effects)
5. [Saturation & Distortion](#saturation--distortion)
6. [Parameter Smoothing](#parameter-smoothing)
7. [Utility Functions](#utility-functions)

---

## Filters

### Biquad Filter (2nd Order IIR)

**Use for**: EQ, lowpass, highpass, bandpass, notch filters

```cpp
class BiquadFilter {
public:
    enum class Type {
        Lowpass,
        Highpass,
        Bandpass,
        Notch,
        Allpass,
        PeakingEQ,
        LowShelf,
        HighShelf
    };

    void setCoefficients(Type type, float frequency, float sampleRate,
                        float Q = 0.707f, float gainDB = 0.0f) {
        const float w0 = juce::MathConstants<float>::twoPi * frequency / sampleRate;
        const float cosw0 = std::cos(w0);
        const float sinw0 = std::sin(w0);
        const float alpha = sinw0 / (2.0f * Q);
        const float A = std::pow(10.0f, gainDB / 40.0f);  // For shelf/peak

        float b0, b1, b2, a0, a1, a2;

        switch (type) {
            case Type::Lowpass:
                b0 = (1.0f - cosw0) / 2.0f;
                b1 = 1.0f - cosw0;
                b2 = (1.0f - cosw0) / 2.0f;
                a0 = 1.0f + alpha;
                a1 = -2.0f * cosw0;
                a2 = 1.0f - alpha;
                break;

            case Type::Highpass:
                b0 = (1.0f + cosw0) / 2.0f;
                b1 = -(1.0f + cosw0);
                b2 = (1.0f + cosw0) / 2.0f;
                a0 = 1.0f + alpha;
                a1 = -2.0f * cosw0;
                a2 = 1.0f - alpha;
                break;

            case Type::Bandpass:
                b0 = alpha;
                b1 = 0.0f;
                b2 = -alpha;
                a0 = 1.0f + alpha;
                a1 = -2.0f * cosw0;
                a2 = 1.0f - alpha;
                break;

            case Type::PeakingEQ:
                b0 = 1.0f + alpha * A;
                b1 = -2.0f * cosw0;
                b2 = 1.0f - alpha * A;
                a0 = 1.0f + alpha / A;
                a1 = -2.0f * cosw0;
                a2 = 1.0f - alpha / A;
                break;

            // Add other types as needed...
        }

        // Normalize coefficients
        coeffs.b0 = b0 / a0;
        coeffs.b1 = b1 / a0;
        coeffs.b2 = b2 / a0;
        coeffs.a1 = a1 / a0;
        coeffs.a2 = a2 / a0;
    }

    float processSample(float input) {
        const float output = coeffs.b0 * input
                           + coeffs.b1 * z1
                           + coeffs.b2 * z2
                           - coeffs.a1 * y1
                           - coeffs.a2 * y2;

        // Update state
        z2 = z1;
        z1 = input;
        y2 = y1;
        y1 = output;

        return output;
    }

    void reset() {
        z1 = z2 = y1 = y2 = 0.0f;
    }

private:
    struct Coefficients {
        float b0 = 1.0f, b1 = 0.0f, b2 = 0.0f;
        float a1 = 0.0f, a2 = 0.0f;
    } coeffs;

    float z1 = 0.0f, z2 = 0.0f;  // Input delays
    float y1 = 0.0f, y2 = 0.0f;  // Output delays
};
```

**Usage:**
```cpp
BiquadFilter filter;
filter.setCoefficients(BiquadFilter::Type::Lowpass, 1000.0f, 48000.0f, 0.707f);

for (int i = 0; i < buffer.getNumSamples(); ++i) {
    float input = buffer.getSample(0, i);
    float output = filter.processSample(input);
    buffer.setSample(0, i, output);
}
```

### State Variable Filter (SVF)

**Use for**: Smooth parameter changes, multimode filters

```cpp
class StateVariableFilter {
public:
    enum class Mode { Lowpass, Highpass, Bandpass };

    void prepare(double sampleRate) {
        this->sampleRate = sampleRate;
    }

    void setParameters(float cutoff, float resonance, Mode mode) {
        this->mode = mode;

        // Calculate coefficients (Chamberlin SVF)
        const float g = std::tan(juce::MathConstants<float>::pi * cutoff / sampleRate);
        const float k = 2.0f - 2.0f * resonance;  // resonance 0-1

        a1 = 1.0f / (1.0f + g * (g + k));
        a2 = g * a1;
        a3 = g * a2;
    }

    float processSample(float input) {
        const float v3 = input - ic2eq;
        const float v1 = a1 * ic1eq + a2 * v3;
        const float v2 = ic2eq + a2 * ic1eq + a3 * v3;

        ic1eq = 2.0f * v1 - ic1eq;
        ic2eq = 2.0f * v2 - ic2eq;

        switch (mode) {
            case Mode::Lowpass:  return v2;
            case Mode::Highpass: return input - k * v1 - v2;
            case Mode::Bandpass: return v1;
            default: return v2;
        }
    }

    void reset() {
        ic1eq = ic2eq = 0.0f;
    }

private:
    Mode mode = Mode::Lowpass;
    double sampleRate = 44100.0;
    float a1 = 0.0f, a2 = 0.0f, a3 = 0.0f;
    float ic1eq = 0.0f, ic2eq = 0.0f;  // Integrator state
};
```

---

## Dynamics Processors

### Compressor

**Use for**: Dynamics control, leveling, punchy mixes

```cpp
class Compressor {
public:
    void prepare(double sampleRate) {
        this->sampleRate = sampleRate;
        envelope = 0.0f;
    }

    void setParameters(float thresholdDB, float ratio, float attackMs, float releaseMs) {
        threshold = juce::Decibels::decibelsToGain(thresholdDB);
        this->ratio = ratio;

        // Calculate time constants
        attackCoeff = std::exp(-1.0f / (attackMs * 0.001f * sampleRate));
        releaseCoeff = std::exp(-1.0f / (releaseMs * 0.001f * sampleRate));
    }

    float processSample(float input) {
        const float inputLevel = std::abs(input);

        // Envelope follower
        if (inputLevel > envelope)
            envelope = attackCoeff * envelope + (1.0f - attackCoeff) * inputLevel;
        else
            envelope = releaseCoeff * envelope + (1.0f - releaseCoeff) * inputLevel;

        // Compute gain reduction
        float gainReduction = 1.0f;
        if (envelope > threshold) {
            const float excess = envelope / threshold;
            gainReduction = std::pow(excess, 1.0f / ratio - 1.0f);
        }

        return input * gainReduction;
    }

    float getGainReductionDB() const {
        return juce::Decibels::gainToDecibels(envelope > threshold
            ? std::pow(envelope / threshold, 1.0f / ratio - 1.0f)
            : 1.0f);
    }

    void reset() {
        envelope = 0.0f;
    }

private:
    double sampleRate = 44100.0;
    float threshold = 1.0f;
    float ratio = 4.0f;
    float attackCoeff = 0.0f;
    float releaseCoeff = 0.0f;
    float envelope = 0.0f;
};
```

### Limiter (Look-Ahead)

```cpp
class Limiter {
public:
    void prepare(double sampleRate, int maxBlockSize) {
        this->sampleRate = sampleRate;

        // Look-ahead buffer (5ms typical)
        const int lookAheadSamples = static_cast<int>(0.005 * sampleRate);
        delayBuffer.setSize(2, lookAheadSamples);
        delayBuffer.clear();
        writePos = 0;
    }

    void setThreshold(float thresholdDB) {
        threshold = juce::Decibels::decibelsToGain(thresholdDB);
    }

    float processSample(float input, int channel) {
        // Write to delay buffer
        delayBuffer.setSample(channel, writePos, input);

        // Read delayed sample
        const float delayed = delayBuffer.getSample(channel, writePos);

        // Analyze future peak
        float peak = 0.0f;
        for (int i = 0; i < delayBuffer.getNumSamples(); ++i) {
            peak = std::max(peak, std::abs(delayBuffer.getSample(channel, i)));
        }

        // Calculate gain
        float gain = 1.0f;
        if (peak > threshold) {
            gain = threshold / peak;
        }

        writePos = (writePos + 1) % delayBuffer.getNumSamples();

        return delayed * gain;
    }

    void reset() {
        delayBuffer.clear()

Related in Image & Video