// Pedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Abstract base class for a single-signal-chain guitar pedal effect. /// /// Each pedal reads from `inputBus`, applies `effect(_:)` (implemented by subclasses), /// then crossfades wet/dry and bypass before writing to `outputBus`. /// /// ## Signal chain /// /// ``` /// inputBus → dry → effect(_:) → wet /// ↕ mix (equal-power crossfade) /// blended /// ↕ bypass (lagged equal-power crossfade with dry) /// switched → × level → outputBus /// ``` /// /// ## Subclassing /// /// Override `effect(_:)` to return a mono processed signal. The base `build()` handles /// wet/dry mixing, smooth bypass switching, and output gain automatically. /// /// ## Parameters /// /// - `mix`: Wet/dry blend in `[0, 1]`. `0` = fully dry, `1` = fully wet. /// - `bypass`: Bypass state. `0` = engaged (effect active), `1` = bypassed (pure dry). /// Transitions are smoothed with a 20 ms lag to avoid clicks. /// - `level`: Output gain as a linear multiplier. `1.0` = unity gain. /// /// - Parameters: /// - mix: Wet/dry ratio in [0, 1]. `0` = dry only, `1` = wet only. Defaults to `1.0`. /// - bypass: Bypass flag. `0` = engaged, `1` = bypassed. Defaults to `0.0`. /// - level: Output gain multiplier in [0, ∞). Defaults to `1.0`. open class Pedal: SynthesizerBase { // MARK: - Parameters /// Wet/dry blend ratio in [0, 1]. `0` = dry only, `1` = wet only. @UIParam public var mix: Double = 1.0 /// Bypass state. `0` = engaged (effect active), `1` = bypassed (dry signal only). /// Transitions are smoothed by `bypassLagTime` seconds to prevent clicks. @UIParam public var bypass: Double = 0.0 /// Output gain as a linear multiplier. `1.0` = unity gain. @UIParam public var level: Double = 1.0 // MARK: - Sample rate /// Engine sample rate, injected by `PedalBoard.add(_:)` before the pedal is added /// to the engine; effect stages that need the sample rate read this. Defaults to a /// safe 48 kHz fallback. public var sampleRate: Double = 48000.0 // MARK: - Identity / routing /// Human-readable name for the pedal (e.g. "Overdrive", "Delay"). public let displayName: String /// Index of the audio bus from which this pedal reads its dry input. public internal(set) var inputBus: Int /// Index of the audio bus to which this pedal writes its output. public internal(set) var outputBus: Int // MARK: - Constants /// Lag time constant in seconds (60 dB convergence) for the bypass crossfade. /// Smooths engaged ↔ bypassed transitions to prevent audible clicks. private let bypassLagTime: Double = 0.02 // Bipolar-span constant used to remap a [0, 1] value to [-1, +1] // for CrossFade's equal-power pan parameter. private let bipolarSpan: Double = 2.0 private let bipolarOffset: Double = 1.0 // MARK: - Init /// Creates a pedal with a display name and initial bus assignments. /// /// Bus assignments are updated by `PedalBoard.add(_:)` before `engine.add`; the /// defaults here are only placeholders. Always configure via the board. /// /// - Parameters: /// - name: Human-readable pedal name shown in UI and debug output. /// - 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`. /// - initialMix: Initial wet/dry blend in `[0, 1]`. Time-based pedals (delay, /// reverb, modulation) pass a partial blend so the dry signal stays present; /// gain-shaping pedals (overdrive) leave the default fully-wet `1.0`. public init( name: String, inputBus: Int = 0, outputBus: Int = 0, initialMix: Double = 1.0 ) { self.displayName = name self.inputBus = inputBus self.outputBus = outputBus super.init() self.mix = initialMix } // MARK: - UI knob descriptors /// Effect-specific knobs for this pedal, listed before the common knobs in the UI. /// /// Override in subclasses to publish each `@UIParam` effect parameter with its /// display label and value range. Defaults to an empty list. open var effectKnobs: [PedalKnob] { [] } /// All knobs a generic UI should render for this pedal: effect-specific knobs /// first, then the common `mix` and `level` knobs. The `bypass` parameter is /// surfaced separately as a header toggle, not as a knob. public var knobs: [PedalKnob] { effectKnobs + [ PedalKnob(label: "Mix", node: $mix, range: 0.0 ... 1.0), PedalKnob(label: "Level", node: $level, range: 0.0 ... 1.0), ] } // MARK: - Subclass hook /// Returns the processed (wet) mono signal for this pedal. /// /// The default implementation is a pass-through — override in subclasses to apply /// the desired effect. The return value must be mono. /// /// - Parameter dry: The dry signal read from `inputBus`. /// - Returns: The wet processed signal (mono). open func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { dry } // MARK: - Build /// Builds the signal graph: dry → effect → wet/dry blend → bypass switch → output. public override func build() { let dry: In.Audio = input(bus: inputBus) let wet: any ValidGeneratorInput = effect(dry) // Map mix [0, 1] → pan [-1, +1] for CrossFade (−1 = full inA/dry, +1 = full inB/wet). let mixPan: any ValidGeneratorInput = ($mix * bipolarSpan) - bipolarOffset // Equal-power crossfade between dry and wet signal. let blended: CrossFade.Audio = CrossFade.Audio( inA: dry, inB: wet, pan: mixPan ) // Smooth bypass transitions with a 20 ms lag to prevent clicks. let bypassLagged: Lag.Control = Lag.Control( input: $bypass, time: bypassLagTime ) // Map bypass [0, 1] → pan [-1, +1]: −1 = blended (engaged), +1 = dry (bypassed). let bypassPan: any ValidGeneratorInput = (bypassLagged * bipolarSpan) - bipolarOffset // Equal-power crossfade: engaged → blended; bypassed → dry. let switched: CrossFade.Audio = CrossFade.Audio( inA: blended, inB: dry, pan: bypassPan ) output(bus: outputBus, source: switched * $level) } }