1.0.0 release
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
// TestHelpers.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
func makeTestEngine(
|
||||
blockSize: Int = 512,
|
||||
sampleRate: Double = 44100,
|
||||
numOutputChannels: Int = 2,
|
||||
numInputChannels: Int = 2,
|
||||
numAudioBuses: Int = 128,
|
||||
numControlBuses: Int = 4096,
|
||||
eventQueueCapacity: Int = lyrebirdDefaultEventQueueCapacity
|
||||
) -> SynthesisEngine {
|
||||
SynthesisEngine(
|
||||
blockSize: blockSize,
|
||||
sampleRate: sampleRate,
|
||||
numOutputChannels: numOutputChannels,
|
||||
numInputChannels: numInputChannels,
|
||||
numAudioBuses: numAudioBuses,
|
||||
numControlBuses: numControlBuses,
|
||||
eventQueueCapacity: eventQueueCapacity,
|
||||
driver: OfflineDriver()
|
||||
)
|
||||
}
|
||||
|
||||
/// Create a started engine suitable for step-sequencer tests (small blockSize for fine timing).
|
||||
func makeSeqEngine() throws -> SynthesisEngine {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
return engine
|
||||
}
|
||||
|
||||
func makeSynthGraph(engine: SynthesisEngine, busOffset: Int = 0) -> SynthGraph {
|
||||
SynthGraph(engine: engine, busOffset: busOffset)
|
||||
}
|
||||
|
||||
// MARK: - Synthesizer test fixtures
|
||||
|
||||
/// Writes a constant value to an audio bus every block.
|
||||
final class ConstantSynth: SynthesizerBase {
|
||||
let value: Double
|
||||
let outputBus: Int
|
||||
|
||||
init(value: Double, outputBus: Int = 0) {
|
||||
self.value = value
|
||||
self.outputBus = outputBus
|
||||
super.init()
|
||||
}
|
||||
|
||||
override func build() {
|
||||
addOutputGenerator(Out.Audio(bus: outputBus, source: value))
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads from one audio bus and accumulates into another — for ordering tests.
|
||||
final class RoutingSynth: SynthesizerBase {
|
||||
let inputBus: Int
|
||||
let outputBus: Int
|
||||
|
||||
init(inputBus: Int, outputBus: Int) {
|
||||
self.inputBus = inputBus
|
||||
self.outputBus = outputBus
|
||||
super.init()
|
||||
}
|
||||
|
||||
override func build() {
|
||||
addOutputGenerator(Out.Audio(bus: outputBus, source: In.Audio(bus: inputBus)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Counts build() invocations. Does not subclass SynthesizerBase so evaluate() is freely
|
||||
/// implementable — SynthesizerBase.evaluate is final (by design), so overriding it in a
|
||||
/// test subclass would be a compile error.
|
||||
final class TrackingSynth: Synthesizer {
|
||||
var buildCallCount: Int = 0
|
||||
var evaluateCallCount: Int = 0
|
||||
|
||||
func build() {
|
||||
buildCallCount += 1
|
||||
}
|
||||
|
||||
func evaluate(graph: SynthGraph, count: Int) {
|
||||
evaluateCallCount += 1
|
||||
}
|
||||
}
|
||||
|
||||
/// Drive a generator for `blocks` blocks and return exactly `blockSize` samples per block.
|
||||
func collectBlocks(
|
||||
_ gen: any Generator,
|
||||
engine: SynthesisEngine,
|
||||
blocks: Int
|
||||
) -> [Double] {
|
||||
let bs: Int = engine.blockSize
|
||||
var out: [Double] = []
|
||||
out.reserveCapacity(blocks * bs)
|
||||
for _ in 0..<blocks {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let block: [Double] = gen.calculatedSamples(graph: g, count: bs)[0]
|
||||
out += block.prefix(bs)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/// Advances the fan-out cache serial as if `evaluateSegment` had been called.
|
||||
///
|
||||
/// Use between multiple `calculatedSamples` calls in a single test to simulate block
|
||||
/// boundaries. Tests that call the same generator instance twice within one test function
|
||||
/// would otherwise receive a cache hit on the second call because `engine.segmentSerial`
|
||||
/// is only incremented by `evaluateSegment`, which is not invoked directly by unit tests.
|
||||
func advanceBlock(_ engine: SynthesisEngine) {
|
||||
engine.advanceSegmentSerial()
|
||||
}
|
||||
|
||||
/// Root-mean-square of a sample buffer. Returns 0 for empty input.
|
||||
func rmsValue(_ samples: [Double]) -> Double {
|
||||
guard !samples.isEmpty else { return 0 }
|
||||
var sumSq: Double = 0.0
|
||||
for s: Double in samples { sumSq += s * s }
|
||||
return (sumSq / Double(samples.count)).squareRoot()
|
||||
}
|
||||
|
||||
/// Minimal Synthesizer conformer for Group isolation tests (no engine startup needed).
|
||||
/// Records how many times evaluate() is called and what count was passed.
|
||||
final class TrackingNode: Synthesizer {
|
||||
var evaluateCallCount: Int = 0
|
||||
var lastCount: Int = 0
|
||||
|
||||
func build() {}
|
||||
|
||||
func evaluate(graph: SynthGraph, count: Int) {
|
||||
evaluateCallCount += 1
|
||||
lastCount = count
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user