Files
Lyrebird/Tests/LyrebirdPortableTests/PortableTestSupport.swift
T
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-10 21:09:11 -07:00

77 lines
2.7 KiB
Swift

// PortableTestSupport.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@_spi(LyrebirdExtensions) import LyrebirdSynth
// Local copies of the engine/collect helpers used by `LyrebirdTests`. SwiftPM
// test targets cannot share sources, and this target is deliberately limited to
// the public (+ extension-SPI) surface — part of what is being proven is that
// the portable surface is reachable and correct off Apple.
/// Offline engine for deterministic block-by-block generator evaluation.
func makePortableTestEngine(
blockSize: Int = 64,
sampleRate: Double = 44100
) -> SynthesisEngine {
SynthesisEngine(
blockSize: blockSize,
sampleRate: sampleRate,
numOutputChannels: 2,
numInputChannels: 2,
driver: OfflineDriver()
)
}
/// Drive a generator for `blocks` blocks and return `blockSize` samples per block.
func collectPortableBlocks(
_ gen: any Generator,
engine: SynthesisEngine,
blocks: Int,
channel: Int = 0
) -> [Double] {
let bs: Int = engine.blockSize
var out: [Double] = []
out.reserveCapacity(blocks * bs)
for _ in 0..<blocks {
let g: SynthGraph = SynthGraph(engine: engine)
let block: [Double] = gen.calculatedSamples(graph: g, count: bs)[channel]
out += block.prefix(bs)
engine.advanceSegmentSerial()
}
return out
}
/// Per-sample comparison against a recorded golden vector.
///
/// Goldens are captured on Apple (vDSP-backed, known good). Off Apple the same
/// maths runs through the portable scalar cores in `LyrebirdDSP`, and on any
/// platform the compiler is free to contract multiplies and adds differently,
/// so agreement is to a tolerance rather than bit-exact. The tolerance is
/// `absolute + relative * |golden|` — tight enough that a packing, scaling or
/// operand-order error cannot pass, loose enough to survive
/// last-bit rounding differences between implementations and architectures.
func expectMatchesGolden(
_ actual: [Double],
_ golden: [Double],
absolute: Double,
relative: Double = 0,
_ label: String,
sourceLocation: SourceLocation = #_sourceLocation
) {
#expect(actual.count >= golden.count,
"\(label): produced \(actual.count) samples, golden has \(golden.count)",
sourceLocation: sourceLocation)
guard actual.count >= golden.count else { return }
for i: Int in 0..<golden.count {
let tol: Double = absolute + relative * abs(golden[i])
let diff: Double = abs(actual[i] - golden[i])
#expect(diff <= tol,
"\(label): index \(i) actual=\(actual[i]) golden=\(golden[i]) diff=\(diff) tol=\(tol)",
sourceLocation: sourceLocation)
}
}