Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/ReverseDelayTests.swift
T
josh 077a8ad9f0 Address external review: generator tests, class param docs, nits
- Add deterministic generator-level tests: BitReduceTests (level snapping +
  1-bit floor), SampleRateDecimateTests (sample-and-hold groups), and
  ReverseDelayTests (silent-then-flat-replay). Asserts are output-gain
  independent.
- Add class-level `- Parameters:` doc blocks to BitReduce, SampleRateDecimate,
  and ReverseDelay (dual-location param docs).
- BitReduce: clamp the shift amount directly and note shift-safety; clarify
  mid-tread wording.
- ReverseDelay: comment the unreachable baseAddress guard.

Suite 247.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 21:46:23 -07:00

75 lines
2.9 KiB
Swift

// ReverseDelayTests.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("ReverseDelay")
struct ReverseDelayTests {
/// Minimal synth: a DC value through a `ReverseDelay`, written to a mono bus.
private final class ReverseSynth: SynthesizerBase {
private let value: Double
private let delay: Double
private let outBus: Int
init(value: Double, delay: Double, outBus: Int) {
self.value = value
self.delay = delay
self.outBus = outBus
super.init()
}
override func build() {
output(bus: outBus, source: ReverseDelay(
input: value,
maxDelay: 0.1,
delay: delay,
feedback: 0.0,
sampleRate: 44100.0
))
}
}
private func render(value: Double, delay: 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: ReverseSynth = ReverseSynth(value: value, delay: delay, 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 left(_ samples: [Float], _ frame: Int) -> Float {
samples[frame * 2]
}
@Test func firstSegmentIsSilentThenReplaysRecordedValue() throws {
// delay 0.02 s @ 44100 = 882-frame segments. Segment 1 [0, 882) plays the still-empty
// buffer (silence); segment 2 [882, 1764) reverses the recorded DC. Asserts are
// scale-independent (the harness applies a fixed output gain/pan): check silence vs.
// non-silent, and that the replayed DC is flat (reverse of a constant is the same constant).
let samples: [Float] = try render(value: 0.5, delay: 0.02)
// Mid segment 1: still recording, play buffer empty → silent.
#expect(abs(left(samples, 441)) < 1e-3, "first segment should be silent (nothing recorded yet)")
// Segment 2, clear of the edge fades → non-silent and constant (reversed DC).
let a: Float = left(samples, 1200)
let b: Float = left(samples, 1400)
#expect(abs(a) > 1e-2, "second segment should replay the recorded value (non-silent)")
#expect(abs(a - b) < 1e-3, "reversed DC should be flat across the segment")
}
}