feat(script-phaser): one-knob four-stage phaser (orange script voicing)
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 <n>" 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 <noreply@anthropic.com>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user