// ModulationPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Modulation pedal: stereo chorus folded to mono. /// /// Signal chain (inside `effect(_:)`): /// ``` /// dry → Chorus.Audio(mix: 1.0, rate:, depth:) → stereo (2 ch) /// → SumChannels.Audio(mul: 0.5) → mono /// ``` /// /// `Chorus.Audio` has a built-in `mix` parameter that blends dry+wet internally. /// Here it is set to `1.0` (fully wet) so the base `Pedal` class handles dry/wet /// blending uniformly via its own `mix` parameter. /// /// The stereo output from `Chorus` is summed to mono with a ×0.5 gain to compensate /// for channel doubling. /// /// ## Parameters /// /// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus: /// /// - Parameters: /// - rate: Chorus LFO frequency in Hz. Range `[0.1, 10]`. Defaults to `1.5`. /// - depth: Chorus modulation depth as a fraction of the delay time in [0, 1]. /// Smaller values (0.001–0.01) produce classic chorus width. Defaults to `0.002`. public final class ModulationPedal: Pedal { /// One-line description shown in the picker and info panel. public override var summary: String { "Classic mono chorus for subtle thickening and shimmer." } // MARK: - Parameters /// Chorus LFO rate in Hz. Range [0.1, 10]. @UIParam public var rate: Double = 1.5 /// Chorus modulation depth as a fraction of delay time in [0, 1]. @UIParam public var depth: Double = 0.002 // MARK: - Constants /// Chorus `mix` value. Set to 1.0 (fully wet) so the base `Pedal` handles /// the dry/wet blend via its own `mix` parameter. private let chorusWetMix: Double = 1.0 /// Base delay time for the chorus in seconds. private let chorusDelayTime: Double = 0.025 /// Maximum delay time that the chorus delay line is allocated for. private let chorusMaxDelayTime: Double = 0.1 /// Gain applied to the summed stereo→mono output to compensate for channel doubling. private let stereoSumGain: Double = 0.5 /// Default wet/dry blend — an even mix for classic chorus width. private static let defaultMix: Double = 0.5 // MARK: - UI knobs /// Rate knob range in Hz (chorus LFO frequency). private let rateRange: ClosedRange = 0.1 ... 8.0 /// Depth knob range (fraction of delay time). private let depthRange: ClosedRange = 0.0005 ... 0.01 // MARK: - Init /// Creates a modulation (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: "Modulation", inputBus: inputBus, outputBus: outputBus, initialMix: ModulationPedal.defaultMix ) } 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: - Effect /// Applies stereo chorus and folds the result to mono. /// /// Returns a mono wet signal. The base `Pedal` class mixes this with the /// dry signal according to the `mix` parameter. /// /// - Parameter dry: Dry mono input signal. /// - Returns: Wet mono chorus output. public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { // Chorus.Audio produces a 2-channel stereo output. let stereoChorus: Chorus.Audio = Chorus.Audio( input: dry, delayTime: chorusDelayTime, depth: $depth, rate: $rate, mix: chorusWetMix, sampleRate: sampleRate, maxDelayTime: chorusMaxDelayTime ) // Sum stereo → mono with ×0.5 to avoid level doubling. return SumChannels.Audio( input: stereoChorus, mul: stereoSumGain ) } }