320 lines
13 KiB
Swift
320 lines
13 KiB
Swift
// Gendy2Tests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@Suite("Gendy2")
|
||
struct Gendy2Tests {
|
||
|
||
// MARK: - Rate
|
||
|
||
@Test func rateIsAudio() {
|
||
let gen: Gendy2.Audio = Gendy2.Audio()
|
||
#expect(gen.rate == .audio)
|
||
}
|
||
|
||
// MARK: - Output count
|
||
|
||
@Test func outputCountMatchesRequest() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen: Gendy2.Audio = Gendy2.Audio(seed: 1)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||
#expect(Array(out[0].prefix(64)).count == 64)
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Fan-out
|
||
|
||
@Test func fanOutReturnsCachedResult() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen: Gendy2.Audio = Gendy2.Audio(seed: 2)
|
||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||
#expect(Array(out1[0].prefix(64)) == Array(out2[0].prefix(64)))
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - State advances once per block
|
||
|
||
@Test func stateAdvancesOncePerBlock() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen: Gendy2.Audio = Gendy2.Audio(seed: 3)
|
||
let block1a: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
let block1b: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
#expect(block1a == block1b)
|
||
advanceBlock(engine)
|
||
graph = makeSynthGraph(engine: engine)
|
||
let block2: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
#expect(block2 != block1a)
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Non-silent output
|
||
|
||
@Test func producesNonSilentOutput() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen: Gendy2.Audio = Gendy2.Audio(seed: 5)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||
let nonZero: Bool = out[0].prefix(64).contains { $0 != 0.0 }
|
||
#expect(nonZero)
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Deterministic output with fixed seed
|
||
|
||
@Test func deterministicOutputWithSeed() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen1: Gendy2.Audio = Gendy2.Audio(seed: 99)
|
||
let gen2: Gendy2.Audio = Gendy2.Audio(seed: 99)
|
||
let out1: [Double] = Array(gen1.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
let out2: [Double] = Array(gen2.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
#expect(out1 == out2)
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Different seeds produce different output
|
||
|
||
@Test func differentSeedsProduceDifferentOutput() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen1: Gendy2.Audio = Gendy2.Audio(seed: 100)
|
||
let gen2: Gendy2.Audio = Gendy2.Audio(seed: 200)
|
||
let out1: [Double] = Array(gen1.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
let out2: [Double] = Array(gen2.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
#expect(out1 != out2)
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Output stays in bounded range (Catmull-Rom may overshoot [-1,1] slightly)
|
||
|
||
@Test func outputStaysInBoundedRange() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
// Catmull-Rom can overshoot breakpoint bounds slightly; verify output is finite
|
||
// and within a reasonable margin. Gendy2 amps are clamped to [-1,1] but
|
||
// cubic interpolation can produce values up to ~1.5× the breakpoint range.
|
||
let catmullOvershootMargin: Double = 0.6
|
||
let gen: Gendy2.Audio = Gendy2.Audio(seed: 4)
|
||
for _ in 0..<20 {
|
||
graph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||
for i: Int in 0..<64 {
|
||
let s: Double = out[0][i]
|
||
#expect(s.isFinite, "sample \(i) is not finite: \(s)")
|
||
#expect(s >= -(1.0 + catmullOvershootMargin) && s <= (1.0 + catmullOvershootMargin),
|
||
"sample \(i) out of reasonable range: \(s)")
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Differs from Gendy1 (cubic vs linear interpolation)
|
||
|
||
@Test func differsByInterpolationFromGendy1() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
// Using same seed: both generators start with same breakpoint amplitudes,
|
||
// but differ because Gendy2 uses cubic interpolation.
|
||
let gen1: Gendy1.Audio = Gendy1.Audio(seed: 77)
|
||
let gen2: Gendy2.Audio = Gendy2.Audio(seed: 77)
|
||
let out1: [Double] = Array(gen1.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
let out2: [Double] = Array(gen2.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
// The two generators differ in interpolation, so their output should differ.
|
||
// (Both start from same RNG seed but Gendy2 also uses initCPs differently
|
||
// due to different internal structures — outputs will differ regardless.)
|
||
// Check that we get finite, non-identical output.
|
||
let allFinite1: Bool = out1.allSatisfy { $0.isFinite }
|
||
let allFinite2: Bool = out2.allSatisfy { $0.isFinite }
|
||
#expect(allFinite1)
|
||
#expect(allFinite2)
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - mulAdd scales output
|
||
|
||
@Test func mulAddScalesOutput() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let genPlain: Gendy2.Audio = Gendy2.Audio(seed: 42)
|
||
let genMulAdd: Gendy2.Audio = Gendy2.Audio(mul: 2.0, add: 1.0, seed: 42)
|
||
let plain: [Double] = Array(genPlain.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
let scaled: [Double] = Array(genMulAdd.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
for i: Int in 0..<64 {
|
||
let expected: Double = plain[i] * 2.0 + 1.0
|
||
#expect(abs(scaled[i] - expected) < 1e-9, "sample \(i): expected \(expected), got \(scaled[i])")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Integration
|
||
|
||
@Test func integrationRunsWithoutCrash() throws {
|
||
final class Gendy2Synth: SynthesizerBase {
|
||
override func build() {
|
||
let gen: Gendy2.Audio = Gendy2.Audio(minfreq: 80.0, maxfreq: 300.0, initCPs: 8)
|
||
addOutputGenerator(Out.Audio(bus: 0, source: gen * 0.1))
|
||
}
|
||
}
|
||
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let synth: Gendy2Synth = Gendy2Synth()
|
||
synth.build()
|
||
|
||
for _ in 0..<10 {
|
||
engine.advanceSegmentSerial()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
synth.evaluate(graph: graph, count: 64)
|
||
}
|
||
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Control rate
|
||
|
||
@Test func controlRateIsControl() {
|
||
let gen: Gendy2.Control = Gendy2.Control()
|
||
#expect(gen.rate == .control)
|
||
}
|
||
|
||
@Test func controlOutputCountMatchesRequest() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen: Gendy2.Control = Gendy2.Control(seed: 10)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||
#expect(Array(out[0].prefix(64)).count == 64)
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlFanOutReturnsCachedResult() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen: Gendy2.Control = Gendy2.Control(seed: 11)
|
||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||
#expect(Array(out1[0].prefix(64)) == Array(out2[0].prefix(64)))
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlStateAdvancesOncePerBlock() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen: Gendy2.Control = Gendy2.Control(seed: 12)
|
||
let block1a: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
let block1b: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
#expect(block1a == block1b)
|
||
advanceBlock(engine)
|
||
graph = makeSynthGraph(engine: engine)
|
||
let block2: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
#expect(block2 != block1a)
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlProducesNonSilentOutput() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen: Gendy2.Control = Gendy2.Control(seed: 13)
|
||
var nonZeroFound: Bool = false
|
||
for _ in 0..<10 {
|
||
graph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||
if out[0].prefix(64).contains(where: { $0 != 0.0 }) { nonZeroFound = true }
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(nonZeroFound)
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlAmpscaleZeroProducesFiniteOutput() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
// ampscale=0: amplitude breakpoints cannot drift; Catmull-Rom may overshoot slightly.
|
||
let catmullOvershootMargin: Double = 0.6
|
||
let gen: Gendy2.Control = Gendy2.Control(ampscale: 0.0, durscale: 0.0, seed: 14)
|
||
for _ in 0..<10 {
|
||
graph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||
for i: Int in 0..<64 {
|
||
let s: Double = out[0][i]
|
||
#expect(s.isFinite, "sample \(i) is not finite: \(s)")
|
||
#expect(s >= -(1.0 + catmullOvershootMargin) && s <= (1.0 + catmullOvershootMargin),
|
||
"sample \(i) out of expected range: \(s)")
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlIntegrationRunsWithoutCrash() throws {
|
||
final class Gendy2ControlSynth: SynthesizerBase {
|
||
override func build() {
|
||
let gen: Gendy2.Control = Gendy2.Control(minfreq: 0.5, maxfreq: 4.0, initCPs: 6)
|
||
addOutputGenerator(Out.Audio(bus: 0, source: gen * 0.1))
|
||
}
|
||
}
|
||
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let synth: Gendy2ControlSynth = Gendy2ControlSynth()
|
||
synth.build()
|
||
|
||
for _ in 0..<10 {
|
||
engine.advanceSegmentSerial()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
synth.evaluate(graph: graph, count: 64)
|
||
}
|
||
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Deterministic Control output with fixed seed
|
||
|
||
@Test func controlDeterministicOutputWithSeed() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen1: Gendy2.Control = Gendy2.Control(seed: 99)
|
||
let gen2: Gendy2.Control = Gendy2.Control(seed: 99)
|
||
let out1: [Double] = Array(gen1.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
let out2: [Double] = Array(gen2.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
#expect(out1 == out2)
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlDifferentSeedsProduceDifferentOutput() throws {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||
try engine.start()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let gen1: Gendy2.Control = Gendy2.Control(seed: 100)
|
||
let gen2: Gendy2.Control = Gendy2.Control(seed: 200)
|
||
let out1: [Double] = Array(gen1.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
let out2: [Double] = Array(gen2.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||
#expect(out1 != out2)
|
||
engine.stop()
|
||
}
|
||
}
|