Files
ShoeGaze/Sources/LyrebirdShoeGaze/Pedals/MutantPedal.swift
T
josh 4a9bf0fb30 feat(mutant): add Range switch, Sweep, and Attack/Release controls
The envelope filter's cutoff sweep was fixed (80 Hz -> 3200 Hz, 5 ms / 150 ms),
so it read as either muted (resting too low) or extreme (40x span). The
underlying MuTronIII generator already takes freqLo/freqHi/attack/release as
live params, so expose them:

- Range switch (Lo/Hi) on the face — shifts the whole sweep window low
  (rhythm/bass) or high (harmonics/solos), matching the original Mu-Tron III.
- Sweep, Attack, Release in the advanced pop-over — Sweep sets how far input
  amplitude drives the cutoff (peak = resting x sweep); Attack/Release shape
  the envelope response.
- Tamer defaults: resting 80 -> 120 Hz, span 40x -> 20x.

Serialization-safe: new continuous params live in advancedValues and Range is
a trailing switch, so a rig saved with the old 2-switch / no-advanced layout
loads unchanged with the new controls at their defaults (test covers it).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-13 09:42:52 -07:00

224 lines
9.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// MutantPedal.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Envelope-following auto-wah / funk-filter pedal.
///
/// Wraps the Lyrebird `MuTronIII` generator. The input amplitude drives an exponential
/// cutoff sweep through a resonant Chamberlin state-variable filter. Because `MuTronIII`
/// fixes both its filter `mode` and sweep `direction` at `init`, six instances (three
/// filter modes × two sweep directions) run in parallel on the shared dry signal and a
/// nested `Select` picks the active one from the `mode` and `direction` switches — this
/// keeps every filter state warmed so switch changes are click-free (SuperCollider
/// `Select` semantics: every input advances each block).
///
/// The cutoff sweeps exponentially from a resting frequency (`freqLo`) up to a peak
/// (`freqHi`) as the envelope opens: `cutoff = freqLo·(freqHi/freqLo)^envelope`. The
/// **Range** switch shifts that whole window low (rhythm / bass) or high (harmonics /
/// solos), matching the original pedal; the **Sweep** knob sets how far above the resting
/// frequency the envelope drives the cutoff (its "openness"), and **Attack** / **Release**
/// shape how quickly the filter follows the input dynamics.
///
/// ## Knobs
/// - `sensitivity`: Drive into the envelope follower; higher values open the filter on quieter notes.
/// - `resonance`: Filter peak emphasis at the swept cutoff.
///
/// ## Switches
/// - `direction` (two-state): `0` = Down (louder closes the filter), `1` = Up (louder opens it).
/// - `mode` (three-state): `0` = lowpass, `1` = bandpass (classic vowel wah), `2` = highpass.
/// - `range` (two-state): `0` = Low band (rhythm / bass), `1` = High band (harmonics / solos).
///
/// ## Advanced
/// - `sweep`: How far the envelope drives the cutoff above its resting frequency (peak = resting × sweep).
/// - `attack`: Envelope attack time in seconds — how fast the filter opens on a transient.
/// - `release`: Envelope release time in seconds — how fast it closes after a note.
public final class MutantPedal: Pedal {
/// One-line description shown in the picker and info panel.
public override var summary: String {
"Envelope-following auto-wah / funk filter that opens with your picking."
}
// MARK: - Parameters
/// Filter response selector. `0` = lowpass, `1` = bandpass, `2` = highpass. Driven by a three-state switch.
@UIParam public var mode: Double = 1.0
/// Sweep direction selector. `0` = down (louder closes), `1` = up (louder opens). Driven by a two-state switch.
@UIParam public var direction: 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
/// Frequency-band selector. `0` = Low band, `1` = High band. Shifts the resting cutoff (and with
/// it the whole sweep window) up or down. Driven by a two-state switch.
@UIParam public var range: Double = 0.0
/// Sweep amount: the peak cutoff is `restingCutoff × sweep`, so higher values open the filter
/// further as the input drives it. Advanced knob.
@UIParam public var sweep: Double = 20.0
/// Envelope attack time in seconds — how fast the filter opens on a transient. Advanced knob.
@UIParam public var attack: Double = 0.008
/// Envelope release time in seconds — how fast the filter closes after a note. Advanced knob.
@UIParam public var release: Double = 0.14
// MARK: - Constants
private static let defaultMix: Double = 1.0
/// Resting cutoff (in Hz) at `range == Low`.
private let rangeLowBaseFreq: Double = 120.0
/// Resting cutoff (in Hz) at `range == High`.
private let rangeHighBaseFreq: Double = 380.0
/// Half-step bias added before each `Select`'s truncate-toward-zero index, turning a
/// switch parameter into a round-to-nearest position selector.
private let selectRoundingBias: Double = 0.5
/// Switch position values.
private let modeLowpassValue: Double = 0.0
private let modeBandpassValue: Double = 1.0
private let modeHighpassValue: Double = 2.0
private let directionDownValue: Double = 0.0
private let directionUpValue: Double = 1.0
private let rangeLowValue: Double = 0.0
private let rangeHighValue: Double = 1.0
// MARK: - Ranges
private let sensitivityRange: ClosedRange<Double> = 0.0 ... 4.0
private let resonanceRange: ClosedRange<Double> = 1.0 ... 12.0
/// Sweep multiplier: peak cutoff = resting × sweep. `1` = no sweep; `50` ≈ 5.6 octaves.
private let sweepRange: ClosedRange<Double> = 1.0 ... 50.0
private let attackRange: ClosedRange<Double> = 0.001 ... 0.08
private let releaseRange: ClosedRange<Double> = 0.02 ... 0.6
// MARK: - Knobs
public override var effectKnobs: [PedalKnob] {
[
PedalKnob(label: "Sensitivity", node: $sensitivity, range: sensitivityRange),
PedalKnob(label: "Resonance", node: $resonance, range: resonanceRange),
]
}
public override var advancedKnobs: [PedalKnob] {
[
PedalKnob(label: "Sweep", node: $sweep, range: sweepRange),
PedalKnob(label: "Attack", node: $attack, range: attackRange),
PedalKnob(label: "Release", node: $release, range: releaseRange),
]
}
// MARK: - Switches
public override var effectSwitches: [PedalSwitch] {
[
PedalSwitch(
label: "Direction",
node: $direction,
options: [
PedalSwitch.Option(title: "Down", value: directionDownValue),
PedalSwitch.Option(title: "Up", value: directionUpValue),
]
),
PedalSwitch(
label: "Mode",
node: $mode,
options: [
PedalSwitch.Option(title: "LP", value: modeLowpassValue),
PedalSwitch.Option(title: "BP", value: modeBandpassValue),
PedalSwitch.Option(title: "HP", value: modeHighpassValue),
]
),
PedalSwitch(
label: "Range",
node: $range,
options: [
PedalSwitch.Option(title: "Lo", value: rangeLowValue),
PedalSwitch.Option(title: "Hi", value: rangeHighValue),
]
),
]
}
// MARK: - Init
public init(inputBus: Int = 0, outputBus: Int = 0) {
super.init(
name: "Mutant",
inputBus: inputBus,
outputBus: outputBus,
initialMix: MutantPedal.defaultMix
)
}
// MARK: - Effect
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
// Resting cutoff shifts with the Range switch (Low band vs High band); the peak the envelope
// reaches is `restingCutoff × sweep`. Both feed all six warmed filters (fan-out).
let restingFreq: any ValidGeneratorInput =
$range * (rangeHighBaseFreq - rangeLowBaseFreq) + rangeLowBaseFreq
let peakFreq: any ValidGeneratorInput = restingFreq * $sweep
// Inner mode selectors per sweep direction. `Select` rounds the [0, 2] mode switch
// to the nearest position and advances all three filters each block (warmed state).
let modeWhich: any ValidGeneratorInput = $mode + selectRoundingBias
let downModes: Select.Audio = Select.Audio(
which: modeWhich,
inputs: [
makeFilter(dry, mode: .lowpass, direction: .down, freqLo: restingFreq, freqHi: peakFreq),
makeFilter(dry, mode: .bandpass, direction: .down, freqLo: restingFreq, freqHi: peakFreq),
makeFilter(dry, mode: .highpass, direction: .down, freqLo: restingFreq, freqHi: peakFreq),
]
)
let upModes: Select.Audio = Select.Audio(
which: modeWhich,
inputs: [
makeFilter(dry, mode: .lowpass, direction: .up, freqLo: restingFreq, freqHi: peakFreq),
makeFilter(dry, mode: .bandpass, direction: .up, freqLo: restingFreq, freqHi: peakFreq),
makeFilter(dry, mode: .highpass, direction: .up, freqLo: restingFreq, freqHi: peakFreq),
]
)
// Outer selector picks the sweep direction (Down = 0, Up = 1).
let directionWhich: any ValidGeneratorInput = $direction + selectRoundingBias
return Select.Audio(
which: directionWhich,
inputs: [downModes, upModes]
)
}
private func makeFilter(
_ dry: any ValidGeneratorInput,
mode: MuTronIIIMode,
direction: MuTronIIISweep,
freqLo: any ValidGeneratorInput,
freqHi: any ValidGeneratorInput
) -> MuTronIII.Audio {
MuTronIII.Audio(
input: dry,
mode: mode,
direction: direction,
sensitivity: $sensitivity,
peak: $resonance,
freqLo: freqLo,
freqHi: freqHi,
attackTime: $attack,
releaseTime: $release
)
}
}