1.0.0 release
This commit is contained in:
@@ -0,0 +1,322 @@
|
||||
// Gendy3Tests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@Suite("Gendy3")
|
||||
struct Gendy3Tests {
|
||||
|
||||
// MARK: - Rate
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: Gendy3.Audio = Gendy3.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: Gendy3.Audio = Gendy3.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: Gendy3.Audio = Gendy3.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: Gendy3.Audio = Gendy3.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: - 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: Gendy3.Audio = Gendy3.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: Gendy3.Audio = Gendy3.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: Gendy3.Audio = Gendy3.Audio(seed: 99)
|
||||
let gen2: Gendy3.Audio = Gendy3.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: Gendy3.Audio = Gendy3.Audio(seed: 100)
|
||||
let gen2: Gendy3.Audio = Gendy3.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 periodically repeating waveform shape
|
||||
|
||||
@Test func ampScaleZeroProducesStableAmplitudes() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// With ampscale=0, amplitudes never drift — waveform shape is fixed.
|
||||
let gen: Gendy3.Audio = Gendy3.Audio(ampscale: 0.0, seed: 8)
|
||||
var allInRange: Bool = true
|
||||
for _ in 0..<10 {
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
for i: Int in 0..<64 {
|
||||
if out[0][i] < -1.0 || out[0][i] > 1.0 { allInRange = false }
|
||||
}
|
||||
advanceBlock(engine)
|
||||
}
|
||||
#expect(allInRange)
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Different freq produces different rhythm
|
||||
|
||||
@Test func differentFreqProducesDifferentOutput() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 256)
|
||||
try engine.start()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let gen1: Gendy3.Audio = Gendy3.Audio(freq: 220.0, seed: 33)
|
||||
let gen2: Gendy3.Audio = Gendy3.Audio(freq: 880.0, seed: 33)
|
||||
let out1: [Double] = Array(gen1.calculatedSamples(graph: graph, count: 256)[0].prefix(256))
|
||||
let out2: [Double] = Array(gen2.calculatedSamples(graph: graph, count: 256)[0].prefix(256))
|
||||
// Different frequencies → different segment lengths → different output.
|
||||
#expect(out1 != out2)
|
||||
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: Gendy3.Audio = Gendy3.Audio(seed: 42)
|
||||
let genMulAdd: Gendy3.Audio = Gendy3.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 Gendy3Synth: SynthesizerBase {
|
||||
override func build() {
|
||||
let gen: Gendy3.Audio = Gendy3.Audio(freq: 330.0, ampscale: 0.3, initCPs: 8)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: gen * 0.1))
|
||||
}
|
||||
}
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
let synth: Gendy3Synth = Gendy3Synth()
|
||||
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: Gendy3.Control = Gendy3.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: Gendy3.Control = Gendy3.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: Gendy3.Control = Gendy3.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: Gendy3.Control = Gendy3.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: Gendy3.Control = Gendy3.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: Gendy3.Control = Gendy3.Control(ampscale: 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 {
|
||||
#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 Gendy3ControlSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let gen: Gendy3.Control = Gendy3.Control(freq: 2.0, ampscale: 0.3, initCPs: 6)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: gen * 0.1))
|
||||
}
|
||||
}
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
let synth: Gendy3ControlSynth = Gendy3ControlSynth()
|
||||
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: Gendy3.Control = Gendy3.Control(seed: 99)
|
||||
let gen2: Gendy3.Control = Gendy3.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: Gendy3.Control = Gendy3.Control(seed: 100)
|
||||
let gen2: Gendy3.Control = Gendy3.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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user