Files
Lyrebird/Tests/LyrebirdTests/Process/LyrebirdEventListPrePostGroupTests.swift
T
2026-06-01 18:27:40 -07:00

272 lines
9.2 KiB
Swift

// LyrebirdEventListPrePostGroupTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - Test fixtures
private final class SilentSynth: SynthesizerBase {
override func build() {
addOutputGenerator(Out.Audio(bus: 0, source: 0.0))
}
}
private func sustainedEnvelope() -> Envelope {
Envelope(
levels: [0.0, 1.0, 0.0],
durations: [0.02, 0.05],
curves: [.lin, .lin],
releaseNode: 2
)
}
private func makePrePostTestEngine() -> SynthesisEngine {
SynthesisEngine(
blockSize: 64,
sampleRate: 44100,
numOutputChannels: 2,
numInputChannels: 0,
numAudioBuses: 128,
numControlBuses: 64,
driver: OfflineDriver()
)
}
@Suite("LyrebirdEventList preMasterGroup / postMasterGroup")
struct LyrebirdEventListPrePostGroupTests {
@Test("preMasterGroup and postMasterGroup are non-nil before play")
func testGroupsExistBeforePlay() throws {
let engine: SynthesisEngine = makePrePostTestEngine()
let proc: LyrebirdProcess = LyrebirdProcess(
id: "p",
envelope: sustainedEnvelope(),
numChannels: 1,
body: { ctx in await ctx.wait(30.0) }
)
let list: LyrebirdEventList = try LyrebirdEventList(
id: "test-groups-exist",
events: [LyrebirdEvent(starts: [proc])],
engine: engine,
bus: AudioBus(index: 0, channelCount: 2)
)
// Groups are constructed in init — accessible immediately.
#expect(list.preMasterGroup.count == 0)
#expect(list.postMasterGroup.count == 0)
}
@Test("addPreMaster routes synth into preMasterGroup")
func testAddPreMasterRoutes() async throws {
let engine: SynthesisEngine = makePrePostTestEngine()
try engine.start()
defer { engine.stop() }
let proc: LyrebirdProcess = LyrebirdProcess(
id: "p",
envelope: sustainedEnvelope(),
numChannels: 1,
body: { ctx in await ctx.wait(30.0) }
)
let list: LyrebirdEventList = try LyrebirdEventList(
id: "test-add-pre",
events: [LyrebirdEvent(starts: [proc])],
engine: engine,
bus: AudioBus(index: 0, channelCount: 2)
)
// Fire first event so groups are added to the engine.
list.next()
// Drive a block so pendingQueue events apply.
engine.processBlock(inputSamples: [])
let pre: SilentSynth = SilentSynth()
try list.addPreMaster(pre)
engine.processBlock(inputSamples: [])
#expect(list.preMasterGroup.count == 1)
#expect(list.postMasterGroup.count == 0)
list.killAll()
}
@Test("addPostMaster routes synth into postMasterGroup")
func testAddPostMasterRoutes() async throws {
let engine: SynthesisEngine = makePrePostTestEngine()
try engine.start()
defer { engine.stop() }
let proc: LyrebirdProcess = LyrebirdProcess(
id: "p",
envelope: sustainedEnvelope(),
numChannels: 1,
body: { ctx in await ctx.wait(30.0) }
)
let list: LyrebirdEventList = try LyrebirdEventList(
id: "test-add-post",
events: [LyrebirdEvent(starts: [proc])],
engine: engine,
bus: AudioBus(index: 0, channelCount: 2)
)
list.next()
engine.processBlock(inputSamples: [])
let post: SilentSynth = SilentSynth()
try list.addPostMaster(post)
engine.processBlock(inputSamples: [])
#expect(list.postMasterGroup.count == 1)
#expect(list.preMasterGroup.count == 0)
list.killAll()
}
@Test("pre-master synth evaluates before master, post-master evaluates after master (bus content order)")
func testEvaluationOrder() async throws {
let engine: SynthesisEngine = makePrePostTestEngine()
try engine.start()
defer { engine.stop() }
// Drive a process that does nothing (sustained, no writes).
let proc: LyrebirdProcess = LyrebirdProcess(
id: "silent",
envelope: sustainedEnvelope(),
numChannels: 1,
body: { ctx in await ctx.wait(30.0) }
)
// EventList configured with amp=1.0, ampLag~0 so master is effective passthrough.
let list: LyrebirdEventList = try LyrebirdEventList(
id: "test-order",
events: [LyrebirdEvent(starts: [proc])],
engine: engine,
amp: 1.0,
ampLag: 0.0001,
bus: AudioBus(index: 0, channelCount: 1)
)
list.next()
// Drive several blocks to let the master synth's Lag.Audio settle to
// its target. At sr=44100 and ampLag=0.0001 s ≈ 4.4 samples, smoothing
// settles in <1 block (64 samples). The 8-block warm-up is generous —
// it covers the pending-queue dispatch (first event setup → master
// synth build → Lag.Audio init) plus several full blocks of headroom
// so the master synth's amp is at its target by the time the test
// measures bus content.
for _: Int in 0..<8 { engine.processBlock(inputSamples: []) }
// Pre-master synth additively writes 0.3 to bus 0.
final class PreWriter: SynthesizerBase {
override func build() {
addOutputGenerator(Out.Audio(bus: 0, source: 0.3 as Double))
}
}
// Post-master synth additively writes 0.1 to bus 0.
final class PostWriter: SynthesizerBase {
override func build() {
addOutputGenerator(Out.Audio(bus: 0, source: 0.1 as Double))
}
}
try list.addPreMaster(PreWriter())
try list.addPostMaster(PostWriter())
// Drive one block to apply the adds, then a second block where both synths run.
engine.processBlock(inputSamples: [])
engine.processBlock(inputSamples: [])
// Expected sequence in this block:
// process group (no write)
// pre-master: bus[0] += 0.3 → 0.3
// master: bus[0] = 0.3 * 1.0 → 0.3 (ReplaceOut)
// post-master: bus[0] += 0.1 → 0.4
// Tolerance allows for Lag.Audio ramp residuals.
let sample: Float = engine.audioBuses[0][0]
#expect(abs(sample - 0.4) < 0.05)
list.killAll()
}
@Test("pre-master synth contribution is scaled by master amp, post-master is not")
func testMasterScalesPreNotPost() async throws {
let engine: SynthesisEngine = makePrePostTestEngine()
try engine.start()
defer { engine.stop() }
let proc: LyrebirdProcess = LyrebirdProcess(
id: "silent",
envelope: sustainedEnvelope(),
numChannels: 1,
body: { ctx in await ctx.wait(30.0) }
)
let list: LyrebirdEventList = try LyrebirdEventList(
id: "test-master-scaling",
events: [LyrebirdEvent(starts: [proc])],
engine: engine,
amp: 0.5,
ampLag: 0.0001,
bus: AudioBus(index: 0, channelCount: 1)
)
list.next()
for _: Int in 0..<8 { engine.processBlock(inputSamples: []) }
final class PreWriter: SynthesizerBase {
override func build() {
addOutputGenerator(Out.Audio(bus: 0, source: 0.4 as Double))
}
}
final class PostWriter: SynthesizerBase {
override func build() {
addOutputGenerator(Out.Audio(bus: 0, source: 0.1 as Double))
}
}
try list.addPreMaster(PreWriter())
try list.addPostMaster(PostWriter())
engine.processBlock(inputSamples: [])
engine.processBlock(inputSamples: [])
// Expected: (0.4 * 0.5) + 0.1 = 0.3
let sample: Float = engine.audioBuses[0][0]
#expect(abs(sample - 0.3) < 0.05)
list.killAll()
}
@Test("teardown frees pre/post group children")
func testTeardownFreesChildren() async throws {
let engine: SynthesisEngine = makePrePostTestEngine()
try engine.start()
defer { engine.stop() }
let proc: LyrebirdProcess = LyrebirdProcess(
id: "p",
envelope: sustainedEnvelope(),
numChannels: 1,
body: { ctx in await ctx.wait(30.0) }
)
let list: LyrebirdEventList = try LyrebirdEventList(
id: "test-teardown",
events: [LyrebirdEvent(starts: [proc])],
engine: engine,
bus: AudioBus(index: 0, channelCount: 2)
)
list.next()
engine.processBlock(inputSamples: [])
try list.addPreMaster(SilentSynth())
try list.addPostMaster(SilentSynth())
engine.processBlock(inputSamples: [])
#expect(list.preMasterGroup.count == 1)
#expect(list.postMasterGroup.count == 1)
list.killAll()
// After teardown the EventList re-allocates fresh empty groups.
engine.processBlock(inputSamples: [])
#expect(list.preMasterGroup.count == 0)
#expect(list.postMasterGroup.count == 0)
}
}