// ChorusPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// True-stereo chorus pedal. /// /// Two modulated delay lines, fed the same dry signal but driven by sine LFOs 180° apart, /// produce a wide left/right shimmer. Unlike `ModulationPedal` (which sums Lyrebird's /// `Chorus` to mono), this pedal is a ``PedalChannelKind/widener`` — it keeps the two voices /// on separate channels for genuine stereo width. /// /// Signal path (per channel, inside `wideEffect(_:)`): /// ``` /// dry → DelayLine(delay: base ± depth·sin(rate)) → wet L / wet R /// ``` /// /// The wet/dry blend and bypass are handled by the `Pedal` base class. /// /// ## Parameters /// /// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus: /// /// - Parameters: /// - rate: LFO rate in Hz. Range `[0.05, 6]`. Defaults to `0.8`. /// - depth: Delay-sweep depth in seconds (half-swing around the base delay). Range /// `[0.0005, 0.008]`. Defaults to `0.003`. public final class ChorusPedal: Pedal { // MARK: - Parameters /// LFO rate in Hz. Range [0.05, 6]. @UIParam public var rate: Double = 0.8 /// Delay-sweep depth in seconds. Range [0.0005, 0.008]. @UIParam public var depth: Double = 0.003 // MARK: - Constants /// Default wet/dry blend — chorus sits under the dry signal. private static let defaultMix: Double = 0.5 /// Base delay around which the LFO sweeps, in seconds. private let baseDelayTime: Double = 0.012 /// Maximum delay the lines are allocated for, in seconds. Covers `baseDelayTime + max depth`. private let maxDelayTime: Double = 0.03 /// LFO phase for the left voice, in cycles. private let leftPhase: Double = 0.0 /// LFO phase for the right voice, in cycles — a half cycle (180°) opposite the left. private let rightPhase: Double = 0.5 // MARK: - UI knobs private let rateRange: ClosedRange = 0.05 ... 6.0 private let depthRange: ClosedRange = 0.0005 ... 0.008 public override var tempoSyncableParams: [TempoSyncableParam] { [ TempoSyncableParam(key: "rate", label: "Rate", node: $rate, kind: .rate, range: rateRange), ] } public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "Rate", node: $rate, range: rateRange), PedalKnob(label: "Depth", node: $depth, range: depthRange), ] } // MARK: - Channel topology public override var channelKind: PedalChannelKind { .widener } // MARK: - Init /// Creates a stereo chorus 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: "Chorus", inputBus: inputBus, outputBus: outputBus, initialMix: ChorusPedal.defaultMix ) } public override var summary: String { "Lush stereo chorus: two detuned voices panned wide for shimmer and depth." } // MARK: - Effect /// Builds one modulated delay line per channel, with the two LFOs in anti-phase. /// /// - Parameter dry: Dry mono input signal. /// - Returns: The left and right wet voices. public override func wideEffect( _ dry: any ValidGeneratorInput ) -> (left: any ValidGeneratorInput, right: any ValidGeneratorInput) { (voice(dry, phase: leftPhase), voice(dry, phase: rightPhase)) } /// One chorus voice: a delay line whose time is swept by a sine LFO around `baseDelayTime`. private func voice( _ dry: any ValidGeneratorInput, phase: Double ) -> any ValidGeneratorInput { let sweep: OscSin.Audio = OscSin.Audio( freq: $rate, phase: phase, mul: $depth, add: baseDelayTime ) return DelayLine( input: dry, maxDelay: maxDelayTime, delay: sweep, sampleRate: sampleRate, interpolation: .linear ) } }