// MuTronPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Envelope-following filter pedal modeled on the Musitronics Mu-Tron III. /// /// Wraps the Lyrebird `MuTronIII` generator. The input amplitude drives an exponential /// cutoff sweep through a resonant Chamberlin state-variable filter. Because `MuTronIII` /// fixes its filter `mode` at `init`, three instances (lowpass, bandpass, highpass) run /// in parallel on the shared dry signal and a `Select` switch picks the active one from /// the `mode` knob — this keeps all three filter states warmed so mode changes are /// click-free (SuperCollider `Select` semantics: every input advances each block). /// /// ## Knobs /// - `mode`: Filter response. `0` = lowpass, `1` = bandpass (classic vowel wah), `2` = highpass. /// - `sensitivity`: Drive into the envelope follower; higher values open the filter on quieter notes. /// - `resonance`: Filter peak emphasis at the swept cutoff. public final class MuTronPedal: Pedal { // MARK: - Parameters /// Filter response selector. `0` = lowpass, `1` = bandpass, `2` = highpass. @UIParam public var mode: Double = 1.0 /// Envelope-follower drive. Higher values make the filter respond to quieter input. @UIParam public var sensitivity: Double = 1.5 /// Filter resonance (Chamberlin SVF peak emphasis). Clamped to `≥ 1.0` internally. @UIParam public var resonance: Double = 4.0 // MARK: - Constants private static let defaultMix: Double = 1.0 /// Sweep direction is fixed to the classic "louder opens the filter" response. private let sweepDirection: MuTronIIISweep = .up /// Cutoff at zero envelope, in Hz. private let freqLo: Double = 80.0 /// Cutoff at maximum envelope, in Hz (≈40× sweep over `freqLo`). private let freqHi: Double = 3200.0 /// Envelope attack/release times, in seconds. private let attackTime: Double = 0.005 private let releaseTime: Double = 0.15 /// Half-step bias added before `Select`'s truncate-toward-zero index, turning the /// `mode` knob into a round-to-nearest 3-position switch over `[0, 2]`. private let modeRoundingBias: Double = 0.5 // MARK: - Ranges private let modeRange: ClosedRange = 0.0 ... 2.0 private let sensitivityRange: ClosedRange = 0.0 ... 4.0 private let resonanceRange: ClosedRange = 1.0 ... 12.0 // MARK: - Knobs public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "Mode", node: $mode, range: modeRange), PedalKnob(label: "Sensitivity", node: $sensitivity, range: sensitivityRange), PedalKnob(label: "Resonance", node: $resonance, range: resonanceRange), ] } // MARK: - Init public init(inputBus: Int = 0, outputBus: Int = 0) { super.init( name: "Mu-Tron", inputBus: inputBus, outputBus: outputBus, initialMix: MuTronPedal.defaultMix ) } // MARK: - Effect public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { let lowpass: MuTronIII.Audio = makeFilter(dry, mode: .lowpass) let bandpass: MuTronIII.Audio = makeFilter(dry, mode: .bandpass) let highpass: MuTronIII.Audio = makeFilter(dry, mode: .highpass) // Round the [0, 2] knob to the nearest integer index, then switch. let which: any ValidGeneratorInput = $mode + modeRoundingBias return Select.Audio( which: which, inputs: [lowpass, bandpass, highpass] ) } private func makeFilter( _ dry: any ValidGeneratorInput, mode: MuTronIIIMode ) -> MuTronIII.Audio { MuTronIII.Audio( input: dry, mode: mode, direction: sweepDirection, sensitivity: $sensitivity, peak: $resonance, freqLo: freqLo, freqHi: freqHi, attackTime: attackTime, releaseTime: releaseTime ) } }