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>
This commit is contained in:
2026-11-24 14:47:51 -08:00
parent 75de04160b
commit d1f823e54c
10 changed files with 2070 additions and 3 deletions
@@ -0,0 +1,314 @@
// DL4PedalTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
import LyrebirdUI
@testable import LyrebirdShoeGaze
/// A single decaying pluck so a test can hear the delay repeats in the silence after the note.
@MainActor
private final class TestPluckSource: SynthesizerBase {
private let targetBus: Int
private let freq: Double
private let amp: Double
private let burstDur: Double
init(targetBus: Int, freq: Double, amplitude: Double, burstDur: Double) {
self.targetBus = targetBus
self.freq = freq
self.amp = amplitude
self.burstDur = burstDur
super.init()
}
override func build() {
let env: Line.Control = Line.Control(start: amp, end: 0.0, dur: burstDur)
output(bus: targetBus, source: OscSin.Audio(freq: freq) * env)
}
}
/// A steady sine for the bypass click test.
@MainActor
private final class TestSineSource: SynthesizerBase {
private let targetBus: Int
private let freq: Double
init(targetBus: Int, freq: Double) {
self.targetBus = targetBus
self.freq = freq
super.init()
}
override func build() {
output(bus: targetBus, source: OscSin.Audio(freq: freq, mul: 0.5))
}
}
@MainActor
@Suite("DL4Pedal")
struct DL4PedalTests {
private let sampleRate: Double = 44_100.0
private let pluckHz: Double = 180.0
private let burstDur: Double = 0.08
private let renderDiffEpsilon: Double = 1.0e-3
private let modelCount: Int = 8
// MARK: - Render
private func render(
duration: Double = 1.2,
pluck: Bool = true,
amplitude: Double = 0.7,
configure: (DL4Pedal) -> Void
) throws -> [Float] {
let numAudioBuses: Int = 64
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: duration,
numAudioBuses: numAudioBuses
)
defer { try? FileManager.default.removeItem(at: url) }
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
defer {
engine.busAllocator.releaseAudioBus(srcBus)
engine.busAllocator.releaseAudioBus(dstBus)
}
let source: SynthesizerBase = pluck
? TestPluckSource(targetBus: srcBus.index, freq: pluckHz, amplitude: amplitude, burstDur: burstDur)
: TestSineSource(targetBus: srcBus.index, freq: pluckHz)
let pedal: DL4Pedal = DL4Pedal(inputBus: srcBus.index, outputBus: dstBus.index)
configure(pedal)
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
try engine.add(source)
try engine.add(pedal, position: NodePosition.after(source))
try engine.add(master, position: NodePosition.after(pedal))
try engine.start()
return readStereoCAF(url: url)
}
// MARK: - Analysis
private func rms(_ samples: [Float], loFrame: Int, hiFrame: Int) -> Double {
let a: Int = max(0, loFrame)
let b: Int = min(hiFrame, samples.count / 2)
guard b > a else { return 0.0 }
var sum: Double = 0.0
for i: Int in a ..< b {
let v: Double = Double(samples[2 * i])
sum += v * v
}
return (sum / Double(b - a)).squareRoot()
}
private func rmsDiff(_ a: [Float], _ b: [Float]) -> Double {
let n: Int = min(a.count, b.count) / 2
guard n > 0 else { return 0.0 }
var sum: Double = 0.0
for i: Int in 0 ..< n {
let d: Double = Double(a[2 * i]) - Double(b[2 * i])
sum += d * d
}
return (sum / Double(n)).squareRoot()
}
private func maxStep(_ x: [Float], lo: Int, hi: Int) -> Double {
let a: Int = max(1, lo)
let b: Int = min(hi, x.count)
guard b > a else { return 0.0 }
var m: Double = 0.0
for i: Int in a ..< b {
let d: Double = abs(Double(x[i]) - Double(x[i - 1]))
if d > m { m = d }
}
return m
}
/// Tail window (after the pluck has decayed), in interleaved frames.
private var tailSkip: Int { Int(0.5 * sampleRate) }
private var tailWindow: Int { 16_384 }
// MARK: - Behaviour
@Test func everyModelProducesEchoesAfterTheNote() throws {
// The pluck decays by burstDur (0.08 s). Every model must still produce audible repeats well
// into the tail at a long Time / Repeats.
for index: Int in 0 ..< modelCount {
let out: [Float] = try render { pedal in
pedal.mix = 1.0; pedal.model = Double(index)
pedal.time = 0.25; pedal.repeats = 4.0; pedal.mod = 0.5
}
let tail: Double = rms(out, loFrame: tailSkip, hiFrame: tailSkip + tailWindow)
#expect(tail > 1.0e-3, "model index \(index) produced no echo tail, RMS \(tail)")
}
}
@Test func modelsRenderDifferently() throws {
// Two different models at the same settings should produce substantially different output.
let tube: [Float] = try render { pedal in
pedal.mix = 1.0; pedal.model = 0.0; pedal.time = 0.25; pedal.repeats = 3.0
}
let reverse: [Float] = try render { pedal in
pedal.mix = 1.0; pedal.model = 7.0; pedal.time = 0.25; pedal.repeats = 3.0
}
#expect(rmsDiff(tube, reverse) > renderDiffEpsilon,
"Tube and Reverse models should differ substantially, diff \(rmsDiff(tube, reverse))")
}
@Test func timeChangesTheEcho() throws {
let short: [Float] = try render { pedal in
pedal.mix = 1.0; pedal.model = 0.0; pedal.time = 0.1; pedal.repeats = 3.0
}
let long: [Float] = try render { pedal in
pedal.mix = 1.0; pedal.model = 0.0; pedal.time = 0.5; pedal.repeats = 3.0
}
#expect(rmsDiff(short, long) > renderDiffEpsilon,
"different delay times should render differently, diff \(rmsDiff(short, long))")
}
@Test func repeatsChangesTheTailLength() throws {
// More feedback (longer decay time) = a louder tail at the same point.
let few: [Float] = try render { pedal in
pedal.mix = 1.0; pedal.model = 0.0; pedal.time = 0.2; pedal.repeats = 0.3
}
let many: [Float] = try render { pedal in
pedal.mix = 1.0; pedal.model = 0.0; pedal.time = 0.2; pedal.repeats = 6.0
}
let tailFew: Double = rms(few, loFrame: tailSkip, hiFrame: tailSkip + tailWindow)
let tailMany: Double = rms(many, loFrame: tailSkip, hiFrame: tailSkip + tailWindow)
#expect(tailMany > tailFew,
"more repeats should ring louder in the tail: many \(tailMany) vs few \(tailFew)")
}
@Test func multiHeadStaysBelowClipping() throws {
// The Multi-Head tap sum (3 taps + a feedback repeat) is gain-staged to total 1.0, so a
// full-scale input, fully wet, must not clip even where the taps align.
let out: [Float] = try render(amplitude: 1.0) { pedal in
pedal.mix = 1.0; pedal.model = 4.0; pedal.time = 0.15; pedal.repeats = 4.0
}
var peak: Double = 0.0
for i: Int in 0 ..< (out.count / 2) {
let v: Double = abs(Double(out[2 * i]))
if v > peak { peak = v }
}
#expect(peak <= 1.05, "Multi-Head peaked at \(peak), above the 1.05 ceiling")
}
@Test func renderIsDeterministic() throws {
// Every model's node graph is deterministic (no RNG), so two identical renders are identical.
for index: Int in 0 ..< modelCount {
let a: [Float] = try render(duration: 0.6) { pedal in pedal.model = Double(index); pedal.time = 0.2 }
let b: [Float] = try render(duration: 0.6) { pedal in pedal.model = Double(index); pedal.time = 0.2 }
#expect(a == b, "model index \(index) should render bit-identically")
}
}
// MARK: - Bypass
private func renderBypassFlip(flipFrame: Int, totalFrames: Int, blockSize: Int) throws -> [Float] {
let numAudioBuses: Int = 64
let numControlBuses: Int = 512
let driver: OfflineDriver = OfflineDriver()
let engine: SynthesisEngine = SynthesisEngine(
blockSize: blockSize,
sampleRate: sampleRate,
numOutputChannels: 2,
numInputChannels: 0,
numAudioBuses: numAudioBuses,
numControlBuses: numControlBuses,
driver: driver
)
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
defer {
engine.busAllocator.releaseAudioBus(srcBus)
engine.busAllocator.releaseAudioBus(dstBus)
}
let source: TestSineSource = TestSineSource(targetBus: srcBus.index, freq: pluckHz)
let pedal: DL4Pedal = DL4Pedal(inputBus: srcBus.index, outputBus: dstBus.index)
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
try engine.add(source)
try engine.add(pedal, position: NodePosition.after(source))
try engine.add(master, position: NodePosition.after(pedal))
try engine.start()
let noInput: [[Float]] = []
var left: [Float] = []
left.reserveCapacity(totalFrames)
var frame: Int = 0
var flipped: Bool = false
while frame < totalFrames {
if !flipped, frame >= flipFrame {
pedal.bypass = 1.0
flipped = true
}
engine.processBlock(inputSamples: noInput)
left.append(contentsOf: engine.audioBuses[0])
frame += blockSize
}
engine.stop()
return left
}
@Test func bypassSwitchIsClickFree() throws {
let blockSize: Int = 512
let totalFrames: Int = 26_460
let flipFrame: Int = 15_435
let left: [Float] = try renderBypassFlip(flipFrame: flipFrame, totalFrames: totalFrames, blockSize: blockSize)
#expect(hasNonSilentOutput(left), "bypass-flip render was silent")
let steadyStep: Double = maxStep(left, lo: 9_700, hi: 14_500)
let fadeFrames: Int = Int(0.03 * sampleRate)
let transStep: Double = maxStep(left, lo: flipFrame - blockSize, hi: flipFrame + fadeFrames + 2_000)
#expect(transStep <= steadyStep * 4.0,
"bypass transition stepped \(transStep) vs steady \(steadyStep) — a click leaked through")
}
// MARK: - Wiring
@Test func exposesKnobsAndModelSelector() throws {
let pedal: DL4Pedal = DL4Pedal()
#expect(pedal.effectKnobs.map { $0.label } == ["Time", "Repeats", "Tone", "Mod"])
let selectors: [PedalSelectorKnob] = pedal.effectSelectors
#expect(selectors.map { $0.label } == ["Model"])
#expect(selectors.first?.stops.count == modelCount)
#expect(selectors.first?.stops.map { $0.title } ==
["Tube", "Tape", "Analog", "Analog Mod", "Multi-Head", "Sweep", "Lo-Res", "Reverse"])
}
@Test func timeIsTempoSyncable() throws {
let pedal: DL4Pedal = DL4Pedal()
#expect(pedal.tempoSyncableParams.map { $0.key } == ["time"])
}
@Test func presetRoundTripPreservesControls() throws {
let pedal: DL4Pedal = DL4Pedal()
pedal.time = 0.33
pedal.repeats = 2.2
pedal.tone = 4000.0
pedal.mod = 0.7
pedal.model = 5.0
let state: PedalState = pedal.captureState()
let restored: DL4Pedal = DL4Pedal()
restored.applyState(state)
#expect(restored.time == 0.33)
#expect(restored.repeats == 2.2)
#expect(restored.tone == 4000.0)
#expect(restored.mod == 0.7)
#expect(restored.model == 5.0)
}
@Test func registeredInBuiltInRegistry() throws {
let pedal: Pedal? = PedalRegistry.makePedal(type: "DL4")
#expect(pedal is DL4Pedal)
}
}