8775134d4f
Sanitise control-rate reads (finite check + clamp) before they reach seconds→frames Int conversions or feedback math, so a NaN/inf/huge size, delay, downsample, bits, decay, shimmer, damp, or feedback can never trap the Int() conversion nor poison a feedback loop: - ShimmerReverb: clamp() helper; bound size to maxSizeSeconds before Int. - ReverseDelay: finite-clamp delay (to maxDelaySeconds) and feedback. - SampleRateDecimate: clamp downsample to [1, maximumFactor] before Int. - BitReduce: finite-guard bits before Int(bits.rounded()). Adds a ShimmerReverb regression test (NaN controls → finite output). Keeps `import Foundation` in ShimmerReverb (cos for the init-time Hann table) with a clarifying comment. Suite 256. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
116 lines
4.9 KiB
Swift
116 lines
4.9 KiB
Swift
// ShimmerReverbTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
@testable import LyrebirdShoeGaze
|
|
|
|
@MainActor
|
|
@Suite("ShimmerReverb")
|
|
struct ShimmerReverbTests {
|
|
|
|
/// Minimal synth: an input source through a `ShimmerReverb`, written to a mono bus.
|
|
private final class ShimmerSynth: SynthesizerBase {
|
|
private let oscFreq: Double?
|
|
private let dc: Double
|
|
private let size: Double
|
|
private let decay: Double
|
|
private let shimmer: Double
|
|
private let outBus: Int
|
|
|
|
init(oscFreq: Double?, dc: Double, size: Double, decay: Double, shimmer: Double, outBus: Int) {
|
|
self.oscFreq = oscFreq
|
|
self.dc = dc
|
|
self.size = size
|
|
self.decay = decay
|
|
self.shimmer = shimmer
|
|
self.outBus = outBus
|
|
super.init()
|
|
}
|
|
|
|
override func build() {
|
|
let source: any ValidGeneratorInput = oscFreq.map { OscSin.Audio(freq: $0) } ?? dc
|
|
output(bus: outBus, source: ShimmerReverb(
|
|
input: source,
|
|
maxDelay: 0.12,
|
|
size: size,
|
|
decay: decay,
|
|
shimmer: shimmer,
|
|
damp: 0.4,
|
|
sampleRate: 44100.0
|
|
))
|
|
}
|
|
}
|
|
|
|
private func render(
|
|
oscFreq: Double?,
|
|
dc: Double,
|
|
size: Double = 0.05,
|
|
decay: Double,
|
|
shimmer: Double
|
|
) throws -> [Float] {
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, sampleRate: 44100.0)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
|
defer { engine.busAllocator.releaseAudioBus(dstBus) }
|
|
let synth: ShimmerSynth = ShimmerSynth(
|
|
oscFreq: oscFreq, dc: dc, size: size, decay: decay, shimmer: shimmer, outBus: dstBus.index
|
|
)
|
|
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
|
|
try engine.add(synth)
|
|
try engine.add(master, position: NodePosition.after(synth))
|
|
try engine.start()
|
|
return readStereoCAF(url: url, sampleRate: 44100.0)
|
|
}
|
|
|
|
private func totalSSD(_ a: [Float], _ b: [Float]) -> Double {
|
|
zip(a, b).reduce(0.0) { acc, pair in
|
|
let diff: Double = Double(pair.0 - pair.1)
|
|
return acc + diff * diff
|
|
}
|
|
}
|
|
|
|
@Test func silentInputProducesSilence() throws {
|
|
// No input energy → the feedback loop has nothing to sustain → output stays at zero.
|
|
let samples: [Float] = try render(oscFreq: nil, dc: 0.0, decay: 0.9, shimmer: 0.8)
|
|
#expect(!hasNonSilentOutput(samples), "shimmer reverb rang with no input")
|
|
}
|
|
|
|
@Test func passesDCWithUnityGainAndLinearScaling() throws {
|
|
// With decay = shimmer = damp = 0, the tank is just the input through two Schroeder
|
|
// allpasses (unity at DC steady state) delayed by `size`. The output is therefore a fixed
|
|
// gain (the harness output gain/pan) times the DC input — exactly linear. Compare two DC
|
|
// levels at a settled frame so the harness scale cancels: the ratio must be 2.0.
|
|
let high: [Float] = try render(oscFreq: nil, dc: 1.0, size: 0.01, decay: 0.0, shimmer: 0.0)
|
|
let low: [Float] = try render(oscFreq: nil, dc: 0.5, size: 0.01, decay: 0.0, shimmer: 0.0)
|
|
let frame: Int = 1500 // past the size delay + allpass settling, within the read window
|
|
let h: Double = Double(high[frame * 2])
|
|
let l: Double = Double(low[frame * 2])
|
|
#expect(abs(h) > 1e-3, "settled DC output should be non-zero")
|
|
#expect(abs(h / l - 2.0) < 0.01, "DC gain should be exactly linear (ratio 2.0)")
|
|
}
|
|
|
|
@Test func nonFiniteControlsProduceFiniteOutput() throws {
|
|
// NaN control reads must be sanitised (not trap the seconds→frames conversion nor poison
|
|
// the feedback): the output must stay finite.
|
|
let samples: [Float] = try render(
|
|
oscFreq: 440.0, dc: 0.0, size: Double.nan, decay: Double.nan, shimmer: Double.nan
|
|
)
|
|
#expect(samples.allSatisfy { $0.isFinite }, "non-finite controls produced NaN/inf output")
|
|
}
|
|
|
|
@Test func decayChangesTheTail() throws {
|
|
// Small tank so feedback round-trips compound within the reader's window; different decay
|
|
// must change the tail (direction-agnostic — a tuned comb is not monotonic in level).
|
|
let low: [Float] = try render(oscFreq: 440.0, dc: 0.0, size: 0.01, decay: 0.1, shimmer: 0.0)
|
|
let high: [Float] = try render(oscFreq: 440.0, dc: 0.0, size: 0.01, decay: 0.9, shimmer: 0.0)
|
|
#expect(hasNonSilentOutput(high), "decayed tail was silent")
|
|
#expect(totalSSD(low, high) > 0, "changing decay did not change the tail")
|
|
}
|
|
}
|