// RotaryPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Rotary-speaker (Leslie) emulation. /// /// A spinning speaker produces correlated amplitude modulation (volume throb) and frequency /// modulation (Doppler pitch wobble). This pedal builds both from one rotor LFO per channel, with /// the left and right rotors in anti-phase so the image rotates across the stereo field. A two-speed /// switch (Chorale / Tremolo) selects the rotor speed, and the **Accel** knob sets how quickly the /// rotor ramps between speeds — the authentic spin-up / spin-down glide — via a `Lag` on the rate. /// /// Signal path (per channel, inside `wideEffect(_:)`): /// ``` /// dry → DelayLine(delay: base ± fm·sin(rate)) → × (1 ± am·sin(rate)) → wet /// ``` /// /// ## Knobs /// - `depth`: Rotor intensity — scales both the AM throb and the FM Doppler. Range `[0, 1]`. /// - `accel`: Spin-up / spin-down ramp time in seconds. Range `[0.05, 3]`. /// /// ## Switches /// - `speed` (two-state): `0` = Chorale (slow), `1` = Tremolo (fast). public final class RotaryPedal: Pedal { /// One-line description shown in the picker and info panel. public override var summary: String { "Spinning Leslie rotor: Doppler pitch wobble plus volume throb, two-speed with ramp." } // MARK: - Parameters /// Rotor intensity — scales both AM and FM. Range [0, 1]. @UIParam public var depth: Double = 0.7 /// Spin-up / spin-down ramp time in seconds. Range [0.05, 3]. @UIParam public var accel: Double = 0.5 /// Rotor speed selector. `0` = Chorale (slow), `1` = Tremolo (fast). Driven by a two-state switch. @UIParam public var speed: Double = 0.0 // MARK: - Constants /// Rotary is heard fully wet; the `Mix` knob can dial it back if desired. private static let defaultMix: Double = 1.0 /// Chorale (slow) rotor rate in Hz. private let slowRateHz: Double = 0.8 /// Tremolo (fast) rotor rate in Hz. private let fastRateHz: Double = 6.5 /// Maximum FM (Doppler) sweep half-swing in seconds, at full depth. private let fmDepthScale: Double = 0.0008 /// Maximum AM (throb) depth, at full depth. private let amDepthScale: Double = 0.4 /// Center delay the FM sweeps around, in seconds. private let baseDelayTime: Double = 0.004 /// Maximum delay the lines are allocated for, in seconds. private let maxDelayTime: Double = 0.02 /// LFO phase (cycles) for the left rotor. private let leftPhase: Double = 0.0 /// LFO phase (cycles) for the right rotor — a half cycle opposite the left. private let rightPhase: Double = 0.5 // MARK: - Ranges private let depthRange: ClosedRange = 0.0 ... 1.0 private let accelRange: ClosedRange = 0.05 ... 3.0 // MARK: - Knobs public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "Depth", node: $depth, range: depthRange), PedalKnob(label: "Accel", node: $accel, range: accelRange), ] } // MARK: - Switches private let speedChoraleValue: Double = 0.0 private let speedTremoloValue: Double = 1.0 public override var effectSwitches: [PedalSwitch] { [ PedalSwitch( label: "Speed", node: $speed, options: [ PedalSwitch.Option(title: "Chorale", value: speedChoraleValue), PedalSwitch.Option(title: "Tremolo", value: speedTremoloValue), ] ) ] } // MARK: - Channel topology public override var channelKind: PedalChannelKind { .widener } // MARK: - Init /// Creates a rotary-speaker 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: "Rotary", inputBus: inputBus, outputBus: outputBus, initialMix: RotaryPedal.defaultMix ) } // MARK: - Effect public override func wideEffect( _ dry: any ValidGeneratorInput ) -> (left: any ValidGeneratorInput, right: any ValidGeneratorInput) { // Rotor rate glides between the two speeds — the spin-up / spin-down ramp. Shared by both // rotors (fan-out is cached per block). let rateTarget: any ValidGeneratorInput = $speed * (fastRateHz - slowRateHz) + slowRateHz let rate: Lag.Audio = Lag.Audio(input: rateTarget, time: $accel) return (voice(dry, rate: rate, phase: leftPhase), voice(dry, rate: rate, phase: rightPhase)) } /// One rotor voice: a Doppler-swept delay line whose output is amplitude-modulated by the same /// rotor LFO, both at `rate` and the given `phase`. private func voice( _ dry: any ValidGeneratorInput, rate: any ValidGeneratorInput, phase: Double ) -> any ValidGeneratorInput { let fmSweep: OscSin.Audio = OscSin.Audio( freq: rate, phase: phase, mul: $depth * fmDepthScale, add: baseDelayTime ) let delayed: DelayLine = DelayLine( input: dry, maxDelay: maxDelayTime, delay: fmSweep, sampleRate: sampleRate, interpolation: .linear ) // Throb gain in [1 - 2·depth·amScale, 1]. let amGain: OscSin.Audio = OscSin.Audio( freq: rate, phase: phase, mul: $depth * amDepthScale, add: 1.0 - $depth * amDepthScale ) return delayed * amGain } }