313 lines
13 KiB
Swift
313 lines
13 KiB
Swift
// Gendy1Tests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("Gendy1")
|
|
struct Gendy1Tests {
|
|
|
|
// MARK: - Rate
|
|
|
|
@Test func rateIsAudio() {
|
|
let gen: Gendy1.Audio = Gendy1.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: Gendy1.Audio = Gendy1.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: Gendy1.Audio = Gendy1.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: Gendy1.Audio = Gendy1.Audio(seed: 3)
|
|
let block1a: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
|
// Second call in same segment: cache hit, no double advance.
|
|
let block1b: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
|
#expect(block1a == block1b)
|
|
// Advance to next segment.
|
|
advanceBlock(engine)
|
|
graph = makeSynthGraph(engine: engine)
|
|
let block2: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
|
// Block 2 must differ from block 1 (state advanced, different interpolation position).
|
|
#expect(block2 != block1a)
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Output stays in [-1, 1] with mul=1
|
|
|
|
@Test func outputStaysInRange() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
|
try engine.start()
|
|
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let gen: Gendy1.Audio = Gendy1.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 {
|
|
#expect(out[0][i] >= -1.0 && out[0][i] <= 1.0,
|
|
"sample \(i) out of [-1,1]: \(out[0][i])")
|
|
}
|
|
advanceBlock(engine)
|
|
}
|
|
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: Gendy1.Audio = Gendy1.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: Gendy1.Audio = Gendy1.Audio(seed: 99)
|
|
let gen2: Gendy1.Audio = Gendy1.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: Gendy1.Audio = Gendy1.Audio(seed: 100)
|
|
let gen2: Gendy1.Audio = Gendy1.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: - ampscale=0 produces very stable waveform
|
|
|
|
@Test func ampScaleZeroProducesStableWaveform() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
|
try engine.start()
|
|
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
// With ampscale=0, amplitude breakpoints cannot drift.
|
|
let gen: Gendy1.Audio = Gendy1.Audio(ampscale: 0.0, durscale: 0.0, seed: 7)
|
|
// Run two blocks; since neither amplitude nor duration changes, waveform repeats.
|
|
let block1: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
|
advanceBlock(engine)
|
|
graph = makeSynthGraph(engine: engine)
|
|
let block2: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
|
// With zero scale, the waveform pattern should be stable. Both blocks are in [-1,1].
|
|
for sample: Double in block1 { #expect(sample >= -1.0 && sample <= 1.0) }
|
|
for sample: Double in block2 { #expect(sample >= -1.0 && sample <= 1.0) }
|
|
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: Gendy1.Audio = Gendy1.Audio(seed: 42)
|
|
let genMulAdd: Gendy1.Audio = Gendy1.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 Gendy1Synth: SynthesizerBase {
|
|
override func build() {
|
|
let gen: Gendy1.Audio = Gendy1.Audio(minfreq: 100.0, maxfreq: 400.0, initCPs: 6)
|
|
addOutputGenerator(Out.Audio(bus: 0, source: gen * 0.1))
|
|
}
|
|
}
|
|
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
|
try engine.start()
|
|
let synth: Gendy1Synth = Gendy1Synth()
|
|
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: Gendy1.Control = Gendy1.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: Gendy1.Control = Gendy1.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: Gendy1.Control = Gendy1.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: Gendy1.Control = Gendy1.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))
|
|
// Second call in same segment: cache hit — identical output.
|
|
#expect(block1a == block1b)
|
|
advanceBlock(engine)
|
|
graph = makeSynthGraph(engine: engine)
|
|
let block2: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
|
// Different block: state has advanced.
|
|
#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: Gendy1.Control = Gendy1.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 controlAmpscaleZeroProducesStableAmplitudes() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
|
try engine.start()
|
|
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let gen: Gendy1.Control = Gendy1.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 {
|
|
// With ampscale=0 amps can't drift; mul=1 so output stays in [-1,1].
|
|
#expect(out[0][i] >= -1.0 && out[0][i] <= 1.0,
|
|
"sample \(i) out of [-1,1]: \(out[0][i])")
|
|
}
|
|
advanceBlock(engine)
|
|
}
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func controlIntegrationRunsWithoutCrash() throws {
|
|
final class Gendy1ControlSynth: SynthesizerBase {
|
|
override func build() {
|
|
let gen: Gendy1.Control = Gendy1.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: Gendy1ControlSynth = Gendy1ControlSynth()
|
|
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: Gendy1.Control = Gendy1.Control(seed: 99)
|
|
let gen2: Gendy1.Control = Gendy1.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: Gendy1.Control = Gendy1.Control(seed: 100)
|
|
let gen2: Gendy1.Control = Gendy1.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()
|
|
}
|
|
}
|