diff --git a/Sources/LyrebirdShoeGaze/Pedals/MutantPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/MutantPedal.swift index 21487a7..485eb1b 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/MutantPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/MutantPedal.swift @@ -17,6 +17,13 @@ import LyrebirdUI /// 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. @@ -24,6 +31,12 @@ import LyrebirdUI /// ## 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. @@ -45,19 +58,29 @@ public final class MutantPedal: Pedal { /// 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 - /// Cutoff at zero envelope, in Hz. - private let freqLo: Double = 80.0 + /// Resting cutoff (in Hz) at `range == Low`. + private let rangeLowBaseFreq: Double = 120.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 + /// 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. @@ -69,11 +92,17 @@ public final class MutantPedal: Pedal { 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 = 0.0 ... 4.0 private let resonanceRange: ClosedRange = 1.0 ... 12.0 + /// Sweep multiplier: peak cutoff = resting × sweep. `1` = no sweep; `50` ≈ 5.6 octaves. + private let sweepRange: ClosedRange = 1.0 ... 50.0 + private let attackRange: ClosedRange = 0.001 ... 0.08 + private let releaseRange: ClosedRange = 0.02 ... 0.6 // MARK: - Knobs @@ -84,12 +113,20 @@ public final class MutantPedal: Pedal { ] } + 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: "Sweep", + label: "Direction", node: $direction, options: [ PedalSwitch.Option(title: "Down", value: directionDownValue), @@ -105,6 +142,14 @@ public final class MutantPedal: Pedal { PedalSwitch.Option(title: "HP", value: modeHighpassValue), ] ), + PedalSwitch( + label: "Range", + node: $range, + options: [ + PedalSwitch.Option(title: "Lo", value: rangeLowValue), + PedalSwitch.Option(title: "Hi", value: rangeHighValue), + ] + ), ] } @@ -122,23 +167,29 @@ public final class MutantPedal: Pedal { // 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), - makeFilter(dry, mode: .bandpass, direction: .down), - makeFilter(dry, mode: .highpass, direction: .down), + 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), - makeFilter(dry, mode: .bandpass, direction: .up), - makeFilter(dry, mode: .highpass, direction: .up), + 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), ] ) @@ -153,7 +204,9 @@ public final class MutantPedal: Pedal { private func makeFilter( _ dry: any ValidGeneratorInput, mode: MuTronIIIMode, - direction: MuTronIIISweep + direction: MuTronIIISweep, + freqLo: any ValidGeneratorInput, + freqHi: any ValidGeneratorInput ) -> MuTronIII.Audio { MuTronIII.Audio( input: dry, @@ -163,8 +216,8 @@ public final class MutantPedal: Pedal { peak: $resonance, freqLo: freqLo, freqHi: freqHi, - attackTime: attackTime, - releaseTime: releaseTime + attackTime: $attack, + releaseTime: $release ) } } diff --git a/Tests/LyrebirdShoeGazeTests/MutantPedalTests.swift b/Tests/LyrebirdShoeGazeTests/MutantPedalTests.swift index d5d550c..495ca7d 100644 --- a/Tests/LyrebirdShoeGazeTests/MutantPedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/MutantPedalTests.swift @@ -100,28 +100,110 @@ struct MutantPedalTests { pedal.mode = 2.0 // highpass pedal.direction = 0.0 // down + pedal.range = 1.0 // high band + pedal.sweep = 12.0 + pedal.attack = 0.02 + pedal.release = 0.3 + let state: PedalState = pedal.captureState() - #expect(state.switchValues == [0.0, 2.0]) // [direction, mode] + #expect(state.switchValues == [0.0, 2.0, 1.0]) // [direction, mode, range] + #expect(state.advancedValues == [12.0, 0.02, 0.3]) // [sweep, attack, release] let restored: MutantPedal = MutantPedal() restored.applyState(state) #expect(restored.mode == 2.0) #expect(restored.direction == 0.0) + #expect(restored.range == 1.0) + #expect(restored.sweep == 12.0) + #expect(restored.attack == 0.02) + #expect(restored.release == 0.3) + } + + @Test func oldTwoSwitchStateStillLoads() throws { + // A rig saved before Range / the advanced knobs existed carries only [direction, mode] and + // an empty advancedValues; it must load without mis-mapping, leaving the new controls at + // their defaults. + let legacy: PedalState = PedalState( + type: "Mutant", + knobValues: [2.5, 4.0, 1, 1], // sensitivity, resonance, mix, level + switchValues: [1.0, 0.0], // direction=up, mode=lowpass + bypass: 0 + ) + let pedal: MutantPedal = MutantPedal() + pedal.applyState(legacy) + #expect(pedal.direction == 1.0) + #expect(pedal.mode == 0.0) + #expect(pedal.sensitivity == 2.5) + #expect(pedal.resonance == 4.0) + // New controls keep their defaults. + #expect(pedal.range == 0.0) + #expect(pedal.sweep == 20.0) } @Test func exposesSensitivityAndResonanceKnobs() throws { let pedal: MutantPedal = MutantPedal() - let labels: [String] = pedal.effectKnobs.map { $0.label } - #expect(labels == ["Sensitivity", "Resonance"]) + #expect(pedal.effectKnobs.map { $0.label } == ["Sensitivity", "Resonance"]) + #expect(pedal.advancedKnobs.map { $0.label } == ["Sweep", "Attack", "Release"]) } - @Test func exposesTwoStateSweepAndThreeStateModeSwitches() throws { + @Test func exposesDirectionModeAndRangeSwitches() throws { let pedal: MutantPedal = MutantPedal() let switches: [PedalSwitch] = pedal.effectSwitches - #expect(switches.map { $0.label } == ["Sweep", "Mode"]) - // Two-state sweep (Down/Up), three-state mode (LP/BP/HP). - #expect(switches.first?.options.map { $0.title } == ["Down", "Up"]) - #expect(switches.last?.options.map { $0.title } == ["LP", "BP", "HP"]) + #expect(switches.map { $0.label } == ["Direction", "Mode", "Range"]) + #expect(switches[0].options.map { $0.title } == ["Down", "Up"]) + #expect(switches[1].options.map { $0.title } == ["LP", "BP", "HP"]) + #expect(switches[2].options.map { $0.title } == ["Lo", "Hi"]) + } + + /// Renders the test loop through a Mutant configured by `configure`, returning a mid-render window. + private func renderWindow(configure: (MutantPedal) -> Void) throws -> [Float] { + let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 64) + defer { try? FileManager.default.removeItem(at: url) } + let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) + let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) + defer { + engine.busAllocator.releaseAudioBus(srcBus) + engine.busAllocator.releaseAudioBus(dstBus) + } + let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index) + let pedal: MutantPedal = MutantPedal(inputBus: srcBus.index, outputBus: dstBus.index) + configure(pedal) + let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index) + try engine.add(source) + try engine.add(pedal, position: NodePosition.after(source)) + try engine.add(master, position: NodePosition.after(pedal)) + try engine.start() + return readStereoCAF(url: url) + } + + private func windowSSD(_ a: [Float], _ b: [Float]) -> Double { + let skip: Int = 1024 + let window: Int = 2048 + guard a.count >= skip + window, b.count >= skip + window else { return 0 } + return zip(a[skip..<(skip + window)], b[skip..<(skip + window)]).reduce(0.0) { acc, pair in + let d: Double = Double(pair.0 - pair.1) + return acc + d * d + } + } + + @Test func sweepAmountChangesOutput() throws { + let narrow: [Float] = try renderWindow { $0.sweep = 3.0 } + let wide: [Float] = try renderWindow { $0.sweep = 45.0 } + #expect(hasNonSilentOutput(narrow)) + #expect(hasNonSilentOutput(wide)) + #expect(windowSSD(narrow, wide) > 0, "sweep amount did not change output") + } + + @Test func rangeSwitchChangesOutput() throws { + let low: [Float] = try renderWindow { $0.range = 0.0 } + let high: [Float] = try renderWindow { $0.range = 1.0 } + #expect(windowSSD(low, high) > 0, "range band did not change output") + } + + @Test func attackReleaseChangesOutput() throws { + let fast: [Float] = try renderWindow { $0.attack = 0.001; $0.release = 0.03 } + let slow: [Float] = try renderWindow { $0.attack = 0.06; $0.release = 0.5 } + #expect(windowSSD(fast, slow) > 0, "attack/release did not change output") } @Test func sweepDirectionChangesOutput() throws {