Files
josh d1f823e54c feat(pedals): add Karplus Resonator, Tape Looper, and DL4 delay
Three new pedals, each wired at all four points (PedalType, PedalRegistry,
PedalCategory, README) and covered by a dedicated test suite.

Karplus Resonator (texture): a bank of four tuned KarplusStrong strings
plucked by an amplitude-envelope → Schmitt-trigger gate off the note onset,
ringing sympathetically in the tail. Tune / Sustain / Tone / Sensitivity
knobs plus a Voicing selector (Drone / Octaves / Shimmer). Bank summed at
1/stringCount so a full-scale pluck stays at or below unity.

Tape Looper (texture): a simple single-track looper (RecordBuffer /
PlayBuffer) with record / overdub / play and the two classic tape tricks —
half-speed (octave down) and reverse — as live playback-rate controls.
Overdub is gated to forward-normal speed both at engage time and in the
graph, so flipping Reverse/Half-Speed mid-overdub can't misalign layers.

DL4 (delay): an umbrella multi-model delay with a Model selector dispatching
to eight per-model builders — Tube, Tape, Analog, Analog Mod, Multi-Head,
Sweep, Lo-Res, Reverse — over shared Time / Repeats / Tone / Mod controls,
Time tempo-syncable. Multi-Head tap sum gain-staged to 1.0.

Passed /lyrebird_external_review (RT-safety + caveman sweep); all blocking
findings fixed. Full suite 791 tests green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-24 14:47:51 -08:00

297 lines
13 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// DL4Pedal.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// An umbrella multi-model delay in the mould of the classic green four-knob delay modeller: one
/// pedal, a **Model** selector, and four shared controls (**Time**, **Repeats**, **Tone**, **Mod**)
/// whose character changes with the chosen model. This is the delay-models side only — no looper.
///
/// Each model is a distinct delay recipe built from Lyrebird primitives:
/// - **Tube** — a driven, warm echo (soft-clipped input into a filtered delay).
/// - **Tape** — a tape echo with slow wow modulating the delay time and a tape-style HF rolloff.
/// - **Analog** — a dark bucket-brigade voice (heavily low-passed, gently saturated repeats).
/// - **Analog Mod** — the analog voice with a chorus-rate wobble on the delay time.
/// - **Multi-Head** — three fixed delay taps plus a feedback repeat, à la a multi-head tape unit.
/// - **Sweep** — a delay fed through a sweeping all-pass for a phased, resonant echo.
/// - **Lo-Res** — a down-sampled, gritty lo-fi delay.
/// - **Reverse** — backward echo segments (the ``ReverseDelay`` generator).
///
/// `effect` returns the pure wet signal; the base `Pedal` `mix` sets the wet/dry blend.
///
/// ## Knobs
/// - `time`: Delay time in seconds. Range `[0.02, 2.0]`. Tempo-syncable.
/// - `repeats`: Feedback, as the decay time in seconds for echoes to fall to −60 dB. Range `[0.05, 8.0]`.
/// - `tone`: Repeat brightness — a low-pass cutoff in Hz. Range `[500, 12000]`.
/// - `mod`: Modulation amount in `[0, 1]` for the models that modulate — delay-time wow depth for
/// Tape and Analog Mod, and the sweep rate for Sweep; inert for the others.
///
/// ## Selectors
/// - `model`: The delay model (rebuilds on change).
public final class DL4Pedal: Pedal {
/// One-line description shown in the picker and info panel.
public override var summary: String {
"Multi-model delay: tube, tape, analog, multi-head, sweep, lo-res, and reverse voices."
}
// MARK: - Parameters
/// Delay time in seconds. Range `[0.02, 2.0]`.
@UIParam public var time: Double = 0.4
/// Feedback as decay time in seconds (−60 dB). Range `[0.05, 8.0]`.
@UIParam public var repeats: Double = 1.5
/// Repeat brightness — low-pass cutoff in Hz. Range `[500, 12000]`.
@UIParam public var tone: Double = 6000.0
/// Modulation amount in `[0, 1]` — Tape wow depth, Analog Mod wobble depth, or Sweep rate.
@UIParam public var mod: Double = 0.3
/// Selected model index into ``models``. A knob-picker selector; rebuilds on change.
@UIParam public var model: Double = 0.0
// MARK: - Models
/// The ordered model list. The index is written to ``model`` by the selector and read at build
/// time by ``effect(_:)`` to pick the recipe.
private static let models: [String] = [
"Tube", "Tape", "Analog", "Analog Mod", "Multi-Head", "Sweep", "Lo-Res", "Reverse",
]
// MARK: - Constants
/// Default wet/dry blend — a partial mix so dry and echoes coexist.
private static let defaultMix: Double = 0.35
/// Maximum delay time in seconds. Sizes every delay line; kept above `timeRange.upperBound` so
/// the modulated models can push the delay time a little past the knob maximum without wrapping.
private let maxDelayTime: Double = 2.2
/// Amplitude target for the feedback `decayTime` calculation (−60 dB).
private let targetAmp: Double = 0.001
/// Butterworth Q (no resonant peak) for the tone low-pass.
private let filterQ: Double = 0.707
// Tube
private let tubeDrive: Double = 2.0
// Tape
private let tapeWowRate: Double = 0.7 // Hz
private let tapeWowDepth: Double = 0.003 // seconds at mod = 1
private let tapeToneScale: Double = 0.8 // tape HF rolloff
// Analog
private let analogToneScale: Double = 0.5 // dark BBD voice
private let analogDrive: Double = 1.3
// Analog Mod
private let analogModRate: Double = 1.6 // Hz
private let analogModDepth: Double = 0.0025 // seconds at mod = 1
// Multi-Head
private let headBFraction: Double = 0.5 // second tap at half the time
private let headCFraction: Double = 0.75 // third tap at three-quarters
private let headGain: Double = 0.2 // per-tap level (3 taps → 0.6)
private let repeatGain: Double = 0.4 // feedback-repeat level (sum 3·0.2 + 0.4 = 1.0)
// Sweep
private let sweepRateMin: Double = 0.05 // Hz at mod = 0
private let sweepRateSpan: Double = 1.45 // + span at mod = 1 → up to 1.5 Hz
private let sweepDepth: Double = 0.8
private let sweepCenterFreq: Double = 900.0
private let sweepFeedback: Double = 0.3
private let sweepStages: Int = 4
private let sweepInternalMix: Double = 1.0
// Lo-Res
private let loResDownsample: Double = 4.0 // hold length in samples
private let loResToneScale: Double = 0.6
// Reverse
private let reverseFeedbackScale: Double = 0.95 / 8.0 // maps repeats (≤ 8 s) → [0, 0.95]
private let reverseFeedbackMax: Double = 0.95
// MARK: - Ranges
private let timeRange: ClosedRange<Double> = 0.02 ... 2.0
private let repeatsRange: ClosedRange<Double> = 0.05 ... 8.0
private let toneRange: ClosedRange<Double> = 500.0 ... 12000.0
private let modRange: ClosedRange<Double> = 0.0 ... 1.0
// MARK: - Knobs
public override var effectKnobs: [PedalKnob] {
[
PedalKnob(label: "Time", node: $time, range: timeRange),
PedalKnob(label: "Repeats", node: $repeats, range: repeatsRange),
PedalKnob(label: "Tone", node: $tone, range: toneRange),
PedalKnob(label: "Mod", node: $mod, range: modRange),
]
}
public override var tempoSyncableParams: [TempoSyncableParam] {
[
TempoSyncableParam(key: "time", label: "Time", node: $time, kind: .time, range: timeRange),
]
}
// MARK: - Selectors
public override var effectSelectors: [PedalSelectorKnob] {
let stops: [PedalSelectorKnob.Stop] = DL4Pedal.models.enumerated().map { index, name in
PedalSelectorKnob.Stop(title: name, value: Double(index))
}
return [
// Each model is a different delay topology read at build time, so switching rebuilds.
PedalSelectorKnob(label: "Model", node: $model, stops: stops, rebuildsOnChange: true),
]
}
// MARK: - Init
/// Creates a multi-model delay pedal.
///
/// - Parameters:
/// - inputBus: Audio bus index to read the dry input from. Defaults to `0`.
/// - outputBus: Audio bus index to write the processed output to. Defaults to `0`.
public init(
inputBus: Int = 0,
outputBus: Int = 0
) {
super.init(
name: "DL4",
inputBus: inputBus,
outputBus: outputBus,
initialMix: DL4Pedal.defaultMix
)
}
// MARK: - Effect
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
// `model` is read as a plain value at build time; the board rebuilds when it changes (each
// model is a different delay topology). Time / Repeats / Tone / Mod stay live.
let index: Int = min(max(0, Int(model.rounded())), DL4Pedal.models.count - 1)
switch index {
case 0: return makeTube(dry: dry)
case 1: return makeTape(dry: dry)
case 2: return makeAnalog(dry: dry)
case 3: return makeAnalogMod(dry: dry)
case 4: return makeMultiHead(dry: dry)
case 5: return makeSweep(dry: dry)
case 6: return makeLoRes(dry: dry)
default: return makeReverse(dry: dry)
}
}
// MARK: - Model builders
/// A feedback delay line on `input` at the current Time / Repeats.
private func feedbackDelay(input: any ValidGeneratorInput, delay: any ValidGeneratorInput) -> DelayLineFeedback {
DelayLineFeedback(
input: input,
maxDelay: maxDelayTime,
delay: delay,
decayTime: $repeats,
targetAmp: targetAmp,
sampleRate: sampleRate,
interpolation: .linear
)
}
/// Tone low-pass at the given cutoff.
private func toned(_ input: any ValidGeneratorInput, cutoff: any ValidGeneratorInput) -> RBJLowPass.Audio {
RBJLowPass.Audio(input: input, freq: cutoff, q: filterQ)
}
/// Tube: soft-clipped input into a filtered feedback delay — a driven, warm echo.
private func makeTube(dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let driven: SoftClip.Audio = SoftClip.Audio(dry, drive: tubeDrive)
let fb: DelayLineFeedback = feedbackDelay(input: driven, delay: $time)
return toned(fb, cutoff: $tone)
}
/// Tape: slow wow modulates the delay time, with a tape-style HF rolloff.
private func makeTape(dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let wow: OscSin.Audio = OscSin.Audio(freq: tapeWowRate, mul: $mod * tapeWowDepth)
let delay: any ValidGeneratorInput = $time + wow
let fb: DelayLineFeedback = feedbackDelay(input: dry, delay: delay)
return toned(fb, cutoff: $tone * tapeToneScale)
}
/// Analog: dark, gently saturated bucket-brigade voice.
private func makeAnalog(dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let fb: DelayLineFeedback = feedbackDelay(input: dry, delay: $time)
let dark: RBJLowPass.Audio = toned(fb, cutoff: $tone * analogToneScale)
return SoftClip.Audio(dark, drive: analogDrive)
}
/// Analog Mod: the analog voice with a chorus-rate wobble on the delay time.
private func makeAnalogMod(dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let wobble: OscSin.Audio = OscSin.Audio(freq: analogModRate, mul: $mod * analogModDepth)
let delay: any ValidGeneratorInput = $time + wobble
let fb: DelayLineFeedback = feedbackDelay(input: dry, delay: delay)
return toned(fb, cutoff: $tone * analogToneScale)
}
/// Multi-Head: three fixed delay taps plus a feedback repeat, like a multi-head tape unit.
private func makeMultiHead(dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let tapA: DelayLine = DelayLine(input: dry, maxDelay: maxDelayTime, delay: $time, sampleRate: sampleRate)
let tapB: DelayLine = DelayLine(input: dry, maxDelay: maxDelayTime, delay: $time * headBFraction, sampleRate: sampleRate)
let tapC: DelayLine = DelayLine(input: dry, maxDelay: maxDelayTime, delay: $time * headCFraction, sampleRate: sampleRate)
let repeatsTap: DelayLineFeedback = feedbackDelay(input: dry, delay: $time)
let sum: any ValidGeneratorInput =
(tapA * headGain) + (tapB * headGain) + (tapC * headGain) + (repeatsTap * repeatGain)
return toned(sum, cutoff: $tone)
}
/// Sweep: a filtered delay fed through a sweeping all-pass for a phased, resonant echo.
private func makeSweep(dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let fb: DelayLineFeedback = feedbackDelay(input: dry, delay: $time)
let base: RBJLowPass.Audio = toned(fb, cutoff: $tone)
let sweepRate: any ValidGeneratorInput = ($mod * sweepRateSpan) + sweepRateMin
return Phaser.Audio(
input: base,
rate: sweepRate,
depth: sweepDepth,
centerFreq: sweepCenterFreq,
feedback: sweepFeedback,
stages: sweepStages,
mix: sweepInternalMix,
sampleRate: sampleRate
)
}
/// Lo-Res: a down-sampled, gritty lo-fi delay.
private func makeLoRes(dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let fb: DelayLineFeedback = feedbackDelay(input: dry, delay: $time)
let crushed: SampleRateDecimate = SampleRateDecimate(input: fb, downsample: loResDownsample)
return toned(crushed, cutoff: $tone * loResToneScale)
}
/// Reverse: backward echo segments at the current Time, with Repeats mapped to the reverse-trail
/// feedback factor.
private func makeReverse(dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let revFeedback: Clip.Audio = Clip.Audio(
$repeats * reverseFeedbackScale,
lo: 0.0,
hi: reverseFeedbackMax
)
let rev: ReverseDelay = ReverseDelay(
input: dry,
maxDelay: maxDelayTime,
delay: $time,
feedback: revFeedback,
sampleRate: sampleRate
)
return toned(rev, cutoff: $tone)
}
}