From 75de04160bd96e045545a7ad0b21efb9d75df355 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Tue, 24 Nov 2026 12:34:16 -0800 Subject: [PATCH] feat(script-phaser): one-knob four-stage phaser (orange script voicing) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add ScriptPhaserPedal, modeled on the classic one-knob four-stage orange phaser. A single Speed knob sweeps a fixed four-stage all-pass cascade (Phaser.Audio with stages: 4), and a Voicing two-state switch picks Script (smoother, lower feedback) or Block (more resonant, higher feedback). The voicing maps to the feedback amount through a live linear expression — no rebuild, since feedback is a live Phaser parameter — and a short Lag glides the coefficient so a Script↔Block toggle doesn't step audibly. Distinct from the general-purpose Phaser (six stages, with rate and feedback knobs). Named to avoid the trademark (per the amp convention) and the bare "Phase " the code-style rule bans. Wire into PedalType, the registry, and the modulation category; add to the README modulation catalog. 8 tests: voicing and speed each change the output by a magnitude (RMS difference), determinism (bit-identical, no RNG), a click-free bypass flip at the 512-sample block, knob/switch exposure, preset round-trip, registry presence. Co-Authored-By: Claude Opus 4.8 --- README.md | 2 +- Sources/LyrebirdShoeGaze/PedalType.swift | 1 + .../Pedals/ScriptPhaserPedal.swift | 147 +++++++++++ .../Presets/PedalRegistry.swift | 1 + .../LyrebirdShoeGazeUI/PedalCategory.swift | 1 + .../ScriptPhaserPedalTests.swift | 232 ++++++++++++++++++ 6 files changed, 383 insertions(+), 1 deletion(-) create mode 100644 Sources/LyrebirdShoeGaze/Pedals/ScriptPhaserPedal.swift create mode 100644 Tests/LyrebirdShoeGazeTests/ScriptPhaserPedalTests.swift diff --git a/README.md b/README.md index 3a2062e..891814f 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ signal (no hardware needed) or on live guitar through an audio input. | Gain | Overdrive, Distortion, Fuzz, Sustain Fuzz | | Dynamics | Compressor, Sustainer, Limiter, Noise Gate, Volume | | EQ & filter | Graphic EQ, Auto-Wah, Wah, Mutant | -| Modulation | Tremolo, Phaser, Modulation, Chorus, Flanger, Vibrato, Vibe, Rotary, Ring Modulator | +| Modulation | Tremolo, Phaser, Script Phaser, Modulation, Chorus, Flanger, Vibrato, Vibe, Rotary, Ring Modulator | | Pitch | Bass Octaver, Octave Splitter, Harmonizer, Whammy, Poly Octave | | Delay | Delay, Feedback Delay, Multi-Tap Delay, Ping-Pong, Reverse Delay | | Reverb | Reverb, Shimmer Reverb, Plate Reverb, Room Reverb, Spring Reverb, Reverse Reverb, Granular Reverb | diff --git a/Sources/LyrebirdShoeGaze/PedalType.swift b/Sources/LyrebirdShoeGaze/PedalType.swift index 1f62b15..7568e54 100644 --- a/Sources/LyrebirdShoeGaze/PedalType.swift +++ b/Sources/LyrebirdShoeGaze/PedalType.swift @@ -96,6 +96,7 @@ public extension PedalType { static let vibe = PedalType("Vibe") static let rotary = PedalType("Rotary") static let ringModulator = PedalType("Ring Modulator") + static let scriptPhaser = PedalType("Script Phaser") static let diffuser = PedalType("Diffuser") static let bitcrusher = PedalType("Bitcrusher") static let granulator = PedalType("Granulator") diff --git a/Sources/LyrebirdShoeGaze/Pedals/ScriptPhaserPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/ScriptPhaserPedal.swift new file mode 100644 index 0000000..be64705 --- /dev/null +++ b/Sources/LyrebirdShoeGaze/Pedals/ScriptPhaserPedal.swift @@ -0,0 +1,147 @@ +// ScriptPhaserPedal.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import LyrebirdSynth +import LyrebirdUI + +/// Script phaser — the classic one-knob, four-stage orange phaser (the script-logo voicing). +/// +/// A single **Speed** knob sweeps a fixed four-stage all-pass cascade; there is no depth or +/// feedback knob, just the signature liquid whoosh. A **Voicing** switch picks the character: the +/// smoother *Script* voicing (lower feedback, gentler notch) or the more resonant *Block* voicing +/// (more feedback, sharper notch) — the two eras of the hardware. +/// +/// Distinct from the general-purpose ``PhaserPedal`` (six stages, with rate **and** feedback knobs): +/// this is deliberately the fixed, four-stage, one-knob classic. Wraps the shared `Phaser.Audio` +/// generator run fully wet; the base `Pedal` `mix` sets the wet/dry blend. +/// +/// ## Knobs +/// - `speed`: LFO sweep rate in Hz. Range `[0.05, 5.0]`. +/// +/// ## Switches +/// - `voicing` (two-state): `0` = Script (smoother), `1` = Block (more resonant). +public final class ScriptPhaserPedal: Pedal { + + /// One-line description shown in the picker and info panel. + public override var summary: String { + "The one-knob four-stage orange phaser — liquid whoosh, script or block voicing." + } + + // MARK: - Parameters + + /// LFO sweep rate in Hz. Range [0.05, 5.0]. + @UIParam public var speed: Double = 0.5 + + /// Voicing selector. `0` = Script (smoother, lower feedback), `1` = Block (more resonant). + @UIParam public var voicing: Double = 0.0 + + // MARK: - Constants + + /// Default wet/dry blend — an even mix for the classic comb-filter sweep. + private static let defaultMix: Double = 0.5 + + /// Internal `Phaser.Audio` mix — `1.0` so the base `Pedal` handles dry/wet. + private let phaserInternalMix: Double = 1.0 + + /// LFO depth fraction — sweeps the all-pass coefficient across its range. + private let phaserDepth: Double = 0.9 + + /// Center frequency in Hz for the all-pass coefficient sweep. + private let phaserCenterFreq: Double = 800.0 + + /// Four cascaded all-pass stages — the defining topology of this phaser (`Phaser.Audio` + /// requires 4, 6, or 8). + private let phaserStages: Int = 4 + + /// Feedback for the Script voicing — low, for a smooth gentle notch. + private let scriptFeedbackValue: Double = 0.2 + + /// Feedback for the Block voicing — higher, for a sharper, more resonant notch. + private let blockFeedbackValue: Double = 0.45 + + // MARK: - Switch values + + private let voicingScriptValue: Double = 0.0 + private let voicingBlockValue: Double = 1.0 + + /// Smoothing time in seconds for the voicing→feedback change, so flipping Script↔Block glides + /// the feedback coefficient instead of stepping it (which could tick on a live toggle). + private let voicingSmoothTime: Double = 0.03 + + // MARK: - Ranges + + private let speedRange: ClosedRange = 0.05 ... 5.0 + + // MARK: - Knobs + + public override var tempoSyncableParams: [TempoSyncableParam] { + [ + TempoSyncableParam(key: "speed", label: "Speed", node: $speed, kind: .rate, range: speedRange), + ] + } + + public override var effectKnobs: [PedalKnob] { + [ + PedalKnob(label: "Speed", node: $speed, range: speedRange), + ] + } + + // MARK: - Switches + + public override var effectSwitches: [PedalSwitch] { + [ + PedalSwitch( + label: "Voicing", + node: $voicing, + options: [ + PedalSwitch.Option(title: "Script", value: voicingScriptValue), + PedalSwitch.Option(title: "Block", value: voicingBlockValue), + ] + ) + ] + } + + // MARK: - Init + + /// Creates a script-phaser 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: "Script Phaser", + inputBus: inputBus, + outputBus: outputBus, + initialMix: ScriptPhaserPedal.defaultMix + ) + } + + // MARK: - Effect + + public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { + // Voicing (0 = Script, 1 = Block) selects the feedback amount live — a linear map between the + // two fixed values, no rebuild needed since feedback is a live Phaser parameter. A short Lag + // glides the coefficient on a Script↔Block toggle so it doesn't step audibly. + let feedbackSpan: Double = blockFeedbackValue - scriptFeedbackValue + let voicedFeedback: any ValidGeneratorInput = ($voicing * feedbackSpan) + scriptFeedbackValue + let smoothedFeedback: Lag.Audio = Lag.Audio(input: voicedFeedback, time: voicingSmoothTime) + + return Phaser.Audio( + input: dry, + rate: $speed, + depth: phaserDepth, + centerFreq: phaserCenterFreq, + feedback: smoothedFeedback, + stages: phaserStages, + mix: phaserInternalMix, + sampleRate: sampleRate + ) + } +} diff --git a/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift b/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift index a1e417c..82fdc1c 100644 --- a/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift +++ b/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift @@ -69,6 +69,7 @@ public enum PedalRegistry { "Sustain Fuzz": { SustainFuzzPedal() }, "Tremolo": { TremoloPedal() }, "Phaser": { PhaserPedal() }, + "Script Phaser": { ScriptPhaserPedal() }, "Compressor": { CompressorPedal() }, "Mutant": { MutantPedal() }, "Auto-Wah": { AutoWahPedal() }, diff --git a/Sources/LyrebirdShoeGazeUI/PedalCategory.swift b/Sources/LyrebirdShoeGazeUI/PedalCategory.swift index 47cb9cf..63df1c7 100644 --- a/Sources/LyrebirdShoeGazeUI/PedalCategory.swift +++ b/Sources/LyrebirdShoeGazeUI/PedalCategory.swift @@ -53,6 +53,7 @@ public enum PedalCategory: String, CaseIterable, Identifiable, Sendable { "Cabinet": .cabinet, "Tremolo": .modulation, "Phaser": .modulation, "Modulation": .modulation, "Chorus": .modulation, "Flanger": .modulation, "Vibrato": .modulation, "Rotary": .modulation, "Ring Modulator": .modulation, + "Script Phaser": .modulation, "Bass Octaver": .pitch, "Octave Splitter": .pitch, "Harmonizer": .pitch, "Whammy": .pitch, "Poly Octave": .pitch, "Delay": .delay, "Feedback Delay": .delay, "Multi-Tap Delay": .delay, "Ping-Pong": .delay, diff --git a/Tests/LyrebirdShoeGazeTests/ScriptPhaserPedalTests.swift b/Tests/LyrebirdShoeGazeTests/ScriptPhaserPedalTests.swift new file mode 100644 index 0000000..735accc --- /dev/null +++ b/Tests/LyrebirdShoeGazeTests/ScriptPhaserPedalTests.swift @@ -0,0 +1,232 @@ +// ScriptPhaserPedalTests.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import Testing +import Foundation +import LyrebirdSynth +import LyrebirdUI +@testable import LyrebirdShoeGaze + +/// A steady sine source — a stable tone the phaser's sweeping notches act on. +@MainActor +private final class TestSineSource: SynthesizerBase { + private let targetBus: Int + private let freq: Double + private let amp: Double + + init(targetBus: Int, freq: Double, amplitude: Double = 0.5) { + self.targetBus = targetBus + self.freq = freq + self.amp = amplitude + super.init() + } + + override func build() { + output(bus: targetBus, source: OscSin.Audio(freq: freq, mul: amp)) + } +} + +@MainActor +@Suite("ScriptPhaserPedal") +struct ScriptPhaserPedalTests { + + private let sampleRate: Double = 44_100.0 + private let sineHz: Double = 220.0 + /// Minimum RMS difference between two renders to count as a substantial, audible change. + private let renderDiffEpsilon: Double = 1.0e-3 + + // MARK: - Render + + private func render( + duration: Double, + configure: (ScriptPhaserPedal) -> Void + ) throws -> [Float] { + let numAudioBuses: Int = 64 + let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( + duration: duration, + numAudioBuses: numAudioBuses + ) + 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: TestSineSource = TestSineSource(targetBus: srcBus.index, freq: sineHz) + let pedal: ScriptPhaserPedal = ScriptPhaserPedal(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) + } + + /// RMS of the sample-by-sample difference between two renders' left channels — a magnitude of how + /// much two settings actually diverge, not just whether any bit differs. + private func rmsDiff(_ a: [Float], _ b: [Float]) -> Double { + let n: Int = min(a.count, b.count) / 2 + guard n > 0 else { return 0.0 } + var sum: Double = 0.0 + for i: Int in 0 ..< n { + let d: Double = Double(a[2 * i]) - Double(b[2 * i]) + sum += d * d + } + return (sum / Double(n)).squareRoot() + } + + private func maxStep(_ x: [Float], lo: Int, hi: Int) -> Double { + let a: Int = max(1, lo) + let b: Int = min(hi, x.count) + guard b > a else { return 0.0 } + var m: Double = 0.0 + for i: Int in a ..< b { + let d: Double = abs(Double(x[i]) - Double(x[i - 1])) + if d > m { m = d } + } + return m + } + + // MARK: - Behaviour + + @Test func buildsAndProducesNonSilentOutput() throws { + let out: [Float] = try render(duration: 0.8) { _ in } + #expect(!out.isEmpty) + #expect(hasNonSilentOutput(out), "script-phaser output was silent") + } + + @Test func voicingChangesCharacter() throws { + // Script (feedback 0.2) and Block (0.45) are different notch depths, so the rendered output + // must differ between them. + let script: [Float] = try render(duration: 1.0) { pedal in + pedal.mix = 1.0; pedal.speed = 0.8; pedal.voicing = 0.0 + } + let block: [Float] = try render(duration: 1.0) { pedal in + pedal.mix = 1.0; pedal.speed = 0.8; pedal.voicing = 1.0 + } + #expect(hasNonSilentOutput(script) && hasNonSilentOutput(block), "a voicing render was silent") + #expect(rmsDiff(script, block) > renderDiffEpsilon, + "Script and Block voicings should differ substantially, diff \(rmsDiff(script, block))") + } + + @Test func speedChangesModulation() throws { + // A faster LFO sweeps the notches more often, so a slow and a fast render must differ. + let slow: [Float] = try render(duration: 1.0) { pedal in + pedal.mix = 1.0; pedal.speed = 0.2 + } + let fast: [Float] = try render(duration: 1.0) { pedal in + pedal.mix = 1.0; pedal.speed = 3.0 + } + #expect(hasNonSilentOutput(slow) && hasNonSilentOutput(fast), "a speed render was silent") + #expect(rmsDiff(slow, fast) > renderDiffEpsilon, + "slow and fast sweeps should differ substantially, diff \(rmsDiff(slow, fast))") + } + + @Test func renderIsDeterministic() throws { + // The phaser LFO is a deterministic sine (no RNG), so two identical renders are bit-identical. + let a: [Float] = try render(duration: 0.8) { pedal in pedal.speed = 1.0; pedal.voicing = 1.0 } + let b: [Float] = try render(duration: 0.8) { pedal in pedal.speed = 1.0; pedal.voicing = 1.0 } + #expect(a == b, "deterministic script-phaser renders of the same setting should be bit-identical") + } + + // MARK: - Bypass + + private func renderBypassFlip(flipFrame: Int, totalFrames: Int, blockSize: Int) throws -> [Float] { + let numAudioBuses: Int = 64 + let numControlBuses: Int = 512 + let driver: OfflineDriver = OfflineDriver() + let engine: SynthesisEngine = SynthesisEngine( + blockSize: blockSize, + sampleRate: sampleRate, + numOutputChannels: 2, + numInputChannels: 0, + numAudioBuses: numAudioBuses, + numControlBuses: numControlBuses, + driver: driver + ) + + 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: TestSineSource = TestSineSource(targetBus: srcBus.index, freq: sineHz) + let pedal: ScriptPhaserPedal = ScriptPhaserPedal(inputBus: srcBus.index, outputBus: dstBus.index) + 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() + + let noInput: [[Float]] = [] + var left: [Float] = [] + left.reserveCapacity(totalFrames) + var frame: Int = 0 + var flipped: Bool = false + while frame < totalFrames { + if !flipped, frame >= flipFrame { + pedal.bypass = 1.0 + flipped = true + } + engine.processBlock(inputSamples: noInput) + left.append(contentsOf: engine.audioBuses[0]) + frame += blockSize + } + engine.stop() + return left + } + + @Test func bypassSwitchIsClickFree() throws { + let blockSize: Int = 512 + let totalFrames: Int = 26_460 + let flipFrame: Int = 15_435 + let left: [Float] = try renderBypassFlip(flipFrame: flipFrame, totalFrames: totalFrames, blockSize: blockSize) + #expect(hasNonSilentOutput(left), "bypass-flip render was silent") + + let steadyStep: Double = maxStep(left, lo: 9_700, hi: 14_500) + let fadeFrames: Int = Int(0.03 * sampleRate) + let transStep: Double = maxStep(left, lo: flipFrame - blockSize, hi: flipFrame + fadeFrames + 2_000) + #expect(transStep <= steadyStep * 4.0, + "bypass transition stepped \(transStep) vs steady \(steadyStep) — a click leaked through") + } + + // MARK: - Wiring + + @Test func exposesSpeedKnobAndVoicingSwitch() throws { + let pedal: ScriptPhaserPedal = ScriptPhaserPedal() + #expect(pedal.effectKnobs.map { $0.label } == ["Speed"]) + let switches: [PedalSwitch] = pedal.effectSwitches + #expect(switches.map { $0.label } == ["Voicing"]) + #expect(switches.first?.options.map { $0.title } == ["Script", "Block"]) + } + + @Test func presetRoundTripPreservesKnobs() throws { + let pedal: ScriptPhaserPedal = ScriptPhaserPedal() + pedal.speed = 1.7 + pedal.voicing = 1.0 + + let state: PedalState = pedal.captureState() + let restored: ScriptPhaserPedal = ScriptPhaserPedal() + restored.applyState(state) + + #expect(restored.speed == 1.7) + #expect(restored.voicing == 1.0) + } + + @Test func registeredInBuiltInRegistry() throws { + let pedal: Pedal? = PedalRegistry.makePedal(type: "Script Phaser") + #expect(pedal is ScriptPhaserPedal) + } +}