Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/FreezePedalTests.swift
T
josh b1e59d2c03 rename(reverb): Freeze Reverb → Freeze
Shorten the pedal's display name to just "Freeze". Renames the type, registry
key, category entry, class (FreezeReverbPedal → FreezePedal) and its files/tests,
and updates the FreezVerb doc cross-reference. Behaviour unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-24 20:07:11 -08:00

314 lines
12 KiB
Swift

// FreezePedalTests.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 whose level can be muted mid-render via its `gain` parameter — lets a test silence
/// the live input while a snapshot is held, to prove the pad sustains from the freeze and not from
/// the ongoing signal.
@MainActor
private final class GatedSineSource: SynthesizerBase {
private let targetBus: Int
private let freq: Double
@UIParam var gain: Double = 0.5
init(targetBus: Int, freq: Double) {
self.targetBus = targetBus
self.freq = freq
super.init()
}
override func build() {
output(bus: targetBus, source: OscSin.Audio(freq: freq) * $gain)
}
}
@MainActor
@Suite("FreezePedal")
struct FreezePedalTests {
private let sampleRate: Double = 44_100.0
private let sineHz: Double = 220.0
// MARK: - One-shot NRT render (no mid-render control)
private func render(
duration: Double,
configure: (FreezePedal) -> 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: GatedSineSource = GatedSineSource(targetBus: srcBus.index, freq: sineHz)
let pedal: FreezePedal = FreezePedal(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)
}
// MARK: - Block-stepped render with mid-render freeze / mute control
/// Steps the engine block-by-block, invoking `onFrame` before each block so a test can engage the
/// freeze or mute the source at chosen frames. Returns the left output channel.
private func stepRender(
totalFrames: Int,
blockSize: Int,
configure: (FreezePedal) -> Void,
onFrame: (_ frame: Int, _ source: GatedSineSource, _ pedal: FreezePedal) -> Void
) 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: GatedSineSource = GatedSineSource(targetBus: srcBus.index, freq: sineHz)
let pedal: FreezePedal = FreezePedal(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()
let noInput: [[Float]] = []
var left: [Float] = []
left.reserveCapacity(totalFrames)
var frame: Int = 0
while frame < totalFrames {
onFrame(frame, source, pedal)
engine.processBlock(inputSamples: noInput)
left.append(contentsOf: engine.audioBuses[0])
frame += blockSize
}
engine.stop()
return left
}
// MARK: - Analysis
private func rms(_ samples: [Float], loFrame: Int, hiFrame: Int) -> Double {
let a: Int = max(0, loFrame)
let b: Int = min(hiFrame, samples.count)
guard b > a else { return 0.0 }
var sum: Double = 0.0
for i: Int in a ..< b {
let v: Double = Double(samples[i])
sum += v * v
}
return (sum / Double(b - a)).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 {
// Unfrozen, the dry signal passes straight through — output is never silent.
let out: [Float] = try render(duration: 1.0) { _ in }
#expect(!out.isEmpty)
#expect(hasNonSilentOutput(out), "Freeze output was silent")
}
@Test func renderIsDeterministic() throws {
// No RNG anywhere in the graph, so two identical renders are bit-identical.
let a: [Float] = try render(duration: 1.0) { pedal in pedal.verb = 0.6; pedal.tone = 0.4 }
let b: [Float] = try render(duration: 1.0) { pedal in pedal.verb = 0.6; pedal.tone = 0.4 }
#expect(a == b, "deterministic freeze-reverb renders of the same setting should be bit-identical")
}
@Test func snapshotSustainsAfterInputStops() throws {
// The defining behaviour: engage Freeze while a tone plays, then MUTE the input. A held
// spectral snapshot keeps ringing; without the freeze the pad gate is shut, so once the dry
// input is gone the output falls silent. Compare the two in the same late window.
let blockSize: Int = 512
let totalFrames: Int = Int(1.6 * sampleRate)
let freezeFrame: Int = Int(0.45 * sampleRate)
let muteFrame: Int = Int(0.75 * sampleRate)
let windowLo: Int = Int(1.1 * sampleRate)
let windowHi: Int = Int(1.5 * sampleRate)
let frozen: [Float] = try stepRender(totalFrames: totalFrames, blockSize: blockSize) { pedal in
pedal.verb = 0.6; pedal.tone = 0.5; pedal.rise = 0.05
} onFrame: { frame, source, pedal in
if frame >= freezeFrame, !pedal.holding { pedal.toggleFreeze() }
if frame >= muteFrame { source.gain = 0.0 }
}
let control: [Float] = try stepRender(totalFrames: totalFrames, blockSize: blockSize) { pedal in
pedal.verb = 0.6; pedal.tone = 0.5; pedal.rise = 0.05
} onFrame: { frame, source, _ in
// Never freeze; mute at the same frame.
if frame >= muteFrame { source.gain = 0.0 }
}
let heldTail: Double = rms(frozen, loFrame: windowLo, hiFrame: windowHi)
let unfrozenTail: Double = rms(control, loFrame: windowLo, hiFrame: windowHi)
#expect(heldTail > 1.0e-3, "a held snapshot should still ring after the input stops, tail RMS \(heldTail)")
#expect(heldTail > unfrozenTail * 10.0,
"frozen tail \(heldTail) should dwarf the unfrozen tail \(unfrozenTail) once the input is muted")
}
@Test func releaseFadesThePadOut() throws {
// Freeze, mute the input, then release: after the Rise fade the held pad should fall away,
// leaving the late window far quieter than while it was held.
let blockSize: Int = 512
let totalFrames: Int = Int(2.0 * sampleRate)
let freezeFrame: Int = Int(0.4 * sampleRate)
let muteFrame: Int = Int(0.7 * sampleRate)
let releaseFrame: Int = Int(1.1 * sampleRate)
let heldLo: Int = Int(0.95 * sampleRate)
let heldHi: Int = Int(1.05 * sampleRate)
let afterLo: Int = Int(1.7 * sampleRate)
let afterHi: Int = Int(1.95 * sampleRate)
var froze: Bool = false
var released: Bool = false
let out: [Float] = try stepRender(totalFrames: totalFrames, blockSize: blockSize) { pedal in
pedal.verb = 0.5; pedal.tone = 0.5; pedal.rise = 0.1
} onFrame: { frame, source, pedal in
if frame >= freezeFrame, !froze { pedal.toggleFreeze(); froze = true }
if frame >= muteFrame { source.gain = 0.0 }
if frame >= releaseFrame, froze, !released { pedal.toggleFreeze(); released = true }
}
let heldRMS: Double = rms(out, loFrame: heldLo, hiFrame: heldHi)
let afterRMS: Double = rms(out, loFrame: afterLo, hiFrame: afterHi)
#expect(heldRMS > 1.0e-3, "pad should be audible while held, got \(heldRMS)")
#expect(afterRMS < heldRMS * 0.5, "releasing should fade the pad out: after \(afterRMS) vs held \(heldRMS)")
}
// MARK: - Bypass
@Test func bypassSwitchIsClickFree() throws {
let blockSize: Int = 512
let totalFrames: Int = Int(0.6 * sampleRate)
let flipFrame: Int = Int(0.35 * sampleRate)
// Steady-state window ends before the flip; a settle margin after it covers the fade tail.
let steadyLo: Int = Int(0.22 * sampleRate)
let steadyHi: Int = Int(0.33 * sampleRate)
let settleMargin: Int = Int(0.045 * sampleRate)
let left: [Float] = try stepRender(totalFrames: totalFrames, blockSize: blockSize) { pedal in
pedal.verb = 0.5
} onFrame: { frame, _, pedal in
if frame >= flipFrame { pedal.bypass = 1.0 }
}
#expect(hasNonSilentOutput(left), "bypass-flip render was silent")
let steadyStep: Double = maxStep(left, lo: steadyLo, hi: steadyHi)
let fadeFrames: Int = Int(0.03 * sampleRate)
let transStep: Double = maxStep(left, lo: flipFrame - blockSize, hi: flipFrame + fadeFrames + settleMargin)
#expect(transStep <= steadyStep * 4.0,
"bypass transition stepped \(transStep) vs steady \(steadyStep) — a click leaked through")
}
// MARK: - Wiring
@Test func exposesControls() throws {
let pedal: FreezePedal = FreezePedal()
#expect(pedal.effectKnobs.map { $0.label } == ["Verb", "Tone", "Rise"])
#expect(pedal.effectTransportButtons.map { $0.label } == ["Freeze"])
#expect(pedal.showsMixControl == false)
}
@Test func freezeTogglesHoldState() throws {
// The transport latch flips the held state without needing a running engine.
let pedal: FreezePedal = FreezePedal()
#expect(pedal.holding == false)
pedal.toggleFreeze()
#expect(pedal.holding == true)
#expect(pedal.frozen == 1.0)
pedal.toggleFreeze()
#expect(pedal.holding == false)
#expect(pedal.frozen == 0.0)
}
@Test func presetRoundTripPreservesControls() throws {
let pedal: FreezePedal = FreezePedal()
pedal.verb = 0.33
pedal.tone = 0.8
pedal.rise = 1.5
let state: PedalState = pedal.captureState()
let restored: FreezePedal = FreezePedal()
restored.applyState(state)
#expect(restored.verb == 0.33)
#expect(restored.tone == 0.8)
#expect(restored.rise == 1.5)
}
@Test func heldSnapshotIsNotPersisted() throws {
// `frozen` is transport state, not a preset control: a pedal captured mid-freeze must restore
// released, with no phantom pad blooming on load.
let pedal: FreezePedal = FreezePedal()
pedal.verb = 0.4
pedal.toggleFreeze()
#expect(pedal.holding == true)
#expect(pedal.frozen == 1.0)
let state: PedalState = pedal.captureState()
let restored: FreezePedal = FreezePedal()
restored.applyState(state)
#expect(restored.frozen == 0.0, "a held snapshot must not persist into a preset")
#expect(restored.holding == false)
#expect(restored.verb == 0.4)
}
@Test func registeredInBuiltInRegistry() throws {
let pedal: Pedal? = PedalRegistry.makePedal(type: "Freeze")
#expect(pedal is FreezePedal)
}
}