// DelayPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Delay pedal: feedback delay line. /// /// Signal chain (inside `effect(_:)`): /// ``` /// dry → DelayLineFeedback(delay: time, decayTime: feedback) → mono wet out /// ``` /// /// `effect` returns the pure wet delay signal. The base `Pedal` class handles /// wet/dry blending via `mix`, so do not pre-blend inside `effect`. /// /// ## Parameters /// /// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus: /// /// - Parameters: /// - time: Delay time in seconds. Range `[0.01, 1.5]`. Defaults to `0.25`. /// - feedback: Feedback amount expressed as decay time in seconds — the time /// for echoes to fall to `targetAmp` (−60 dB). Longer values produce more /// repeats. Range `[0.05, 6.0]` seconds. Defaults to `1.0`. public final class DelayPedal: Pedal { // MARK: - Parameters /// Delay time in seconds. Range [0.01, 1.5]. @UIParam public var time: Double = 0.25 /// Feedback decay time in seconds (time to reach −60 dB amplitude). @UIParam public var feedback: Double = 1.0 // MARK: - Constants /// Maximum supported delay time in seconds. Sizes the delay line buffer at init. private let maxDelayTime: Double = 1.5 /// Amplitude target for the `decayTime` calculation (−60 dB). private let targetAmp: Double = 0.001 /// Default wet/dry blend — a partial mix so dry signal and echoes coexist. private static let defaultMix: Double = 0.35 // MARK: - UI knobs /// Delay-time knob range in seconds. private let timeRange: ClosedRange = 0.01 ... 1.5 /// Feedback (decay-time) knob range in seconds. private let feedbackRange: ClosedRange = 0.05 ... 6.0 // MARK: - Init /// Creates a 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: "Delay", inputBus: inputBus, outputBus: outputBus, initialMix: DelayPedal.defaultMix ) } public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "Time", node: $time, range: timeRange), PedalKnob(label: "Feedback", node: $feedback, range: feedbackRange), ] } // MARK: - Effect /// Applies a feedback delay line. Returns the pure wet (delayed) signal. /// /// The base class `Pedal` mixes this with the dry signal via the `mix` parameter. /// /// - Parameter dry: Dry mono input signal. /// - Returns: Pure wet mono delayed output. public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { DelayLineFeedback( input: dry, maxDelay: maxDelayTime, delay: $time, decayTime: $feedback, targetAmp: targetAmp, sampleRate: sampleRate, interpolation: .linear ) } }