1.0.0 release

This commit is contained in:
2026-06-01 18:27:40 -07:00
parent b1ab0f102c
commit 1f20fcab20
1151 changed files with 230703 additions and 14 deletions
+240
View File
@@ -0,0 +1,240 @@
// NodeTreeTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// Integration tests for the Synthesizer protocol, SynthesizerBase, and node tree
// wired into SynthesisEngine. All tests use OfflineDriver and call processBlock directly.
// MARK: - SynthesizerBase
@Suite("SynthesizerBase")
struct SynthesizerBaseTests {
@Test func buildNotCalledAtInit() throws {
let synth: TrackingSynth = TrackingSynth()
#expect(synth.buildCallCount == 0)
}
@Test func addOutputGeneratorRegisteredGeneratorsAreDriven() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 4)
try engine.start()
let synth: ConstantSynth = ConstantSynth(value: 0.5, outputBus: 0)
synth.rebuildGraph() // simulate what applyEvent does: clear + build
let graph: SynthGraph = makeSynthGraph(engine: engine)
synth.evaluate(graph: graph, count: 8)
for i: Int in 0..<8 {
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6)
}
}
@Test func rebuildGraphClearsPreviousOutputGenerators() throws {
// Calling rebuildGraph() twice must not double-register generators (additive 0.5+0.5=1.0).
let engine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 4)
try engine.start()
let synth: ConstantSynth = ConstantSynth(value: 0.5, outputBus: 0)
synth.rebuildGraph()
synth.rebuildGraph() // second call should clear and rebuild, not stack
let graph: SynthGraph = makeSynthGraph(engine: engine)
synth.evaluate(graph: graph, count: 8)
for i: Int in 0..<8 {
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6,
"rebuildGraph twice should not double-register; got \(engine.audioBuses[0][i])")
}
}
}
// MARK: - Node tree integration
@Suite("Node tree integration")
struct NodeTreeIntegrationTests {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8, numAudioBuses: 16)
init() throws {
try engine.start()
}
// MARK: - Basic add / evaluate
@Test func busIsZeroWithNoSynths() throws {
engine.processBlock(inputSamples: [])
for i: Int in 0..<8 {
#expect(engine.audioBuses[0][i] == 0.0)
}
}
@Test func synthProducesOutputAfterAdd() throws {
try engine.add(ConstantSynth(value: 1.0, outputBus: 0))
engine.processBlock(inputSamples: [])
for i: Int in 0..<8 {
#expect(abs(engine.audioBuses[0][i] - 1.0) < 1e-6)
}
}
@Test func buildCalledExactlyOnceOnAdd() throws {
let synth: TrackingSynth = TrackingSynth()
try engine.add(synth)
engine.processBlock(inputSamples: [])
#expect(synth.buildCallCount == 1)
}
@Test func buildCalledDuringAddNotDuringProcessBlock() throws {
// build() fires during try engine.add() on the scheduling thread — not during
// processBlock (RT thread). RT-safe: no allocation occurs in the audio callback.
let synth: TrackingSynth = TrackingSynth()
try engine.add(synth)
#expect(synth.buildCallCount == 1, "build() must be called during try engine.add(), before processBlock")
engine.processBlock(inputSamples: [])
#expect(synth.buildCallCount == 1, "build() must not be called again during processBlock")
}
@Test func evaluateCalledEachBlockAfterAdd() throws {
let synth: TrackingSynth = TrackingSynth()
try engine.add(synth)
engine.processBlock(inputSamples: [])
engine.processBlock(inputSamples: [])
engine.processBlock(inputSamples: [])
#expect(synth.evaluateCallCount == 3)
}
@Test func evaluateNotCalledBeforeAdd() throws {
let synth: TrackingSynth = TrackingSynth()
engine.processBlock(inputSamples: [])
#expect(synth.evaluateCallCount == 0)
}
// MARK: - Mixing
@Test func twoSynthsMixOnSameBus() throws {
try engine.add(ConstantSynth(value: 0.3, outputBus: 0))
try engine.add(ConstantSynth(value: 0.4, outputBus: 0))
engine.processBlock(inputSamples: [])
for i: Int in 0..<8 {
#expect(abs(engine.audioBuses[0][i] - 0.7) < 1e-5)
}
}
@Test func synthsWritingDifferentBusesDoNotInterfere() throws {
try engine.add(ConstantSynth(value: 0.5, outputBus: 4))
try engine.add(ConstantSynth(value: 0.9, outputBus: 5))
engine.processBlock(inputSamples: [])
for i: Int in 0..<8 {
#expect(abs(engine.audioBuses[4][i] - 0.5) < 1e-6)
#expect(abs(engine.audioBuses[5][i] - 0.9) < 1e-6)
}
}
// MARK: - Remove
@Test func removedSynthProducesNoOutput() throws {
let synth: ConstantSynth = ConstantSynth(value: 1.0, outputBus: 0)
try engine.add(synth)
engine.processBlock(inputSamples: []) // block 1: synth is active
engine.remove(synth)
engine.processBlock(inputSamples: []) // block 2: synth is removed
for i: Int in 0..<8 {
#expect(engine.audioBuses[0][i] == 0.0)
}
}
@Test func removingNonMemberSynthDoesNotCrash() throws {
let stranger: ConstantSynth = ConstantSynth(value: 1.0, outputBus: 0)
engine.remove(stranger)
engine.processBlock(inputSamples: [])
for i: Int in 0..<8 {
#expect(engine.audioBuses[0][i] == 0.0)
}
}
@Test func mainGroupCountReflectsAddedSynths() throws {
try engine.add(ConstantSynth(value: 0.1, outputBus: 0))
try engine.add(ConstantSynth(value: 0.2, outputBus: 0))
engine.processBlock(inputSamples: [])
// Synths land in mainGroup (not rootGroup directly).
#expect(engine.mainGroup.count == 2)
// rootGroup always has exactly 1 direct child: mainGroup.
#expect(engine.rootGroup.count == 1)
}
@Test func mainGroupCountDecreasesAfterRemove() throws {
let synth: ConstantSynth = ConstantSynth(value: 1.0, outputBus: 0)
try engine.add(synth)
engine.processBlock(inputSamples: [])
engine.remove(synth)
engine.processBlock(inputSamples: [])
// mainGroup empties; rootGroup still holds mainGroup (persistent).
#expect(engine.mainGroup.count == 0)
#expect(engine.rootGroup.count == 1)
}
// MARK: - Evaluation order and bus routing
//
// try engine.add(_:) uses addToHead: the LAST-added synth is evaluated FIRST.
// To have a writer evaluate before a reader, add the reader first (it lands
// at the head, then the writer pushes it further and takes the new head).
@Test func writerAddedLastEvaluatesFirstAndPopulatesBus() throws {
// synthA writes 1.0 to bus 4; synthB reads bus 4 and writes to bus 5.
// B added first (pushed to tail when A arrives); A added second → head.
// Evaluation order: A then B → bus 4 has 1.0 when B reads it.
let synthA: ConstantSynth = ConstantSynth(value: 1.0, outputBus: 4)
let synthB: RoutingSynth = RoutingSynth(inputBus: 4, outputBus: 5)
try engine.add(synthB) // reader first → tail
try engine.add(synthA) // writer last → head (evaluates before B)
engine.processBlock(inputSamples: [])
for i: Int in 0..<8 {
#expect(abs(engine.audioBuses[5][i] - 1.0) < 1e-6)
}
}
@Test func writerAddedFirstEvaluatesLastAndMissesUpstreamRead() throws {
// A added first → at head briefly, then B's add pushes A to tail.
// Evaluation order: B then A → B reads bus 4 before A writes → zero.
let synthA: ConstantSynth = ConstantSynth(value: 1.0, outputBus: 4)
let synthB: RoutingSynth = RoutingSynth(inputBus: 4, outputBus: 5)
try engine.add(synthA) // writer first → tail (evaluated last)
try engine.add(synthB) // reader second → head (evaluated first)
engine.processBlock(inputSamples: [])
for i: Int in 0..<8 {
#expect(engine.audioBuses[5][i] == 0.0)
}
}
// MARK: - Sample-accurate scheduling
@Test func scheduledAddAtMidBlockTimeSampleProducesPartialOutput() throws {
// Add synth at offset 4 within 8-sample block.
// Samples 0–3: silent (synth not yet present). Samples 4–7: 1.0.
let addTime: Int64 = engine.currentTime + 4
try engine.schedule(ConstantSynth(value: 1.0, outputBus: 0), at: addTime)
engine.processBlock(inputSamples: [])
for i: Int in 0..<4 {
#expect(engine.audioBuses[0][i] == 0.0, "sample \(i) should be silent before add time")
}
for i: Int in 4..<8 {
#expect(abs(engine.audioBuses[0][i] - 1.0) < 1e-6, "sample \(i) should be 1.0 after add time")
}
}
@Test func scheduledRemoveAtMidBlockTimeSampleProducesPartialOutput() throws {
// Active synth removed at offset 4. Samples 0–3: 1.0. Samples 4–7: silent.
let synth: ConstantSynth = ConstantSynth(value: 1.0, outputBus: 0)
try engine.add(synth)
engine.processBlock(inputSamples: []) // establish synth in tree
let removeTime: Int64 = engine.currentTime + 4
engine.remove(synth, at: removeTime)
engine.processBlock(inputSamples: [])
for i: Int in 0..<4 {
#expect(abs(engine.audioBuses[0][i] - 1.0) < 1e-6, "sample \(i) should be 1.0 before remove time")
}
for i: Int in 4..<8 {
#expect(engine.audioBuses[0][i] == 0.0, "sample \(i) should be silent after remove time")
}
}
}