Files
josh 5633ede630 Replace test tone with an E-minor Karplus-Strong arpeggio
TestLoopSource now plays a repeating pluck arpeggio over the open lower
four guitar strings (E2, A2, D3, G3) in E minor: one Impulse drives both
the KarplusStrong excitation and a Demand/DemandSequence that steps the
pitch per pluck. Fixed-seed WhiteNoise exciter keeps the tone deterministic
(so two source renders match, e.g. the overdrive bypass-equals-dry test).
Adds a sampleRate init param (default 48k) sizing the KS delay line;
ShoeGaze.addBoard passes the engine's actual rate.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 13:25:56 -07:00

104 lines
4.0 KiB
Swift

// TestLoopSource.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// A synthesized test signal source that writes to a target audio bus.
///
/// Plays a repeating E-minor arpeggio on a Karplus-Strong plucked string, cycling the open
/// pitches of a guitar's lower four strings — E2, A2, D3, G3 — so the board has a musical,
/// guitar-like signal to process without any hardware connected. A single ``Impulse`` drives
/// both the pluck excitation and a ``Demand``/``DemandSequence`` that advances the pitch one step
/// per pluck, keeping note changes locked to the plucks.
///
/// The output is written to `targetBus` so downstream pedals read it from that bus.
/// `TestLoopSource` should be added to the engine **before** the first pedal in the chain to
/// ensure signal-order correctness (source evaluates first).
///
/// ## Parameters
///
/// - Parameters:
/// - targetBus: Audio bus index to write the test signal to. Typically the pedalboard's
/// head bus index.
/// - sampleRate: Engine sample rate, used to size the Karplus-Strong delay line. Defaults to
/// `48000`; the pedalboard passes the engine's actual rate so the pitch is accurate.
/// - amplitude: Output amplitude in [0, 1]. Defaults to `0.25`.
public final class TestLoopSource: SynthesizerBase {
// MARK: - Parameters
/// Output amplitude in [0, 1].
@UIParam public var amplitude: Double = 0.25
// MARK: - Constants
/// Open pitches of the guitar's lower four strings, in Hz: E2, A2, D3, G3. Arpeggiated in
/// order and repeated — an E-minor flavored figure (all four are in E natural minor).
private let arpeggioPitches: [Double] = [82.41, 110.00, 146.83, 196.00]
/// Plucks per second (the arpeggio step rate).
private let pluckRate: Double = 3.0
/// Karplus-Strong feedback gain in [0, 1]; higher = longer sustain (strings ring into the
/// next pluck like a real arpeggio).
private let feedback: Double = 0.99
/// Karplus-Strong one-pole damping coefficient (`0.5` = classic natural damping).
private let damping: Double = 0.5
/// Maximum delay time in seconds, sizing the delay line. `0.05 s` covers fundamentals down to
/// 20 Hz — well below the lowest arpeggio pitch (E2, ~82 Hz).
private let maxDelayTime: Double = 0.05
/// Fixed seed for the pluck-excitation noise, so the test tone is deterministic and identical
/// across instances (two renders of the source produce the same output).
private let exciterSeed: UInt64 = 0x5E1E_C7ED_5017_C35A
// MARK: - Routing
/// The bus this source writes its output to.
private let targetBus: Int
/// Engine sample rate used to size the Karplus-Strong delay line.
private let sampleRate: Double
// MARK: - Init
/// Creates a test loop source that writes to `targetBus`.
///
/// - Parameters:
/// - targetBus: Audio bus index to write the synthesized test signal to.
/// - sampleRate: Engine sample rate (sizes the Karplus-Strong delay line). Defaults to `48000`.
public init(targetBus: Int, sampleRate: Double = 48_000.0) {
self.targetBus = targetBus
self.sampleRate = sampleRate
super.init()
}
// MARK: - Build
public override func build() {
// One impulse drives both the pluck excitation and the pitch-stepping demand, so each
// pluck lands on the next arpeggio note.
let pluck: Impulse.Audio = Impulse.Audio(freq: pluckRate)
let pitch: Demand.Audio = Demand.Audio(
trig: pluck,
demand: DemandSequence(values: arpeggioPitches, repeats: -1)
)
let string: KarplusStrong.Audio = KarplusStrong.Audio(
exciter: WhiteNoise.Audio(seed: exciterSeed),
trigger: pluck,
freq: pitch,
feedback: feedback,
coef: damping,
sampleRate: sampleRate,
maxDelayTime: maxDelayTime
)
output(bus: targetBus, source: string * $amplitude)
}
}