prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,261 @@
|
||||
// InGeneratorTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("In (audio bus)")
|
||||
struct InGeneratorTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: In.Audio = In.Audio(bus: 0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
@Test func readsFromCorrectBus() {
|
||||
// Write a known pattern to bus 5
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
engine.audioBuses[5][i] = Float(i) * 0.1
|
||||
}
|
||||
let gen: In.Audio = In.Audio(bus: 5)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
#expect(abs(samples[0][i] - Double(i) * 0.1) < 1e-6)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func returnsLyrebirdFloatNotFloat() {
|
||||
let gen: In.Audio = In.Audio(bus: 0)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
// Double is Double — just check the type compiles correctly
|
||||
let _: [Double] = samples[0]
|
||||
// In now pre-allocates lyrebirdDefaultResultBufferSize elements like all generators;
|
||||
// callers take prefix(count) for the valid portion.
|
||||
#expect(samples[0].count >= 8)
|
||||
}
|
||||
|
||||
@Test func returnsCountSamples() {
|
||||
let gen: In.Audio = In.Audio(bus: 0)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
// Pre-allocated buffer is at least lyrebirdDefaultResultBufferSize; first 64 are valid.
|
||||
#expect(samples[0].count >= 64)
|
||||
}
|
||||
|
||||
@Test func readsAtBusOffset() {
|
||||
// Set bus 2 to distinct values
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
engine.audioBuses[2][i] = Float(i + 100)
|
||||
}
|
||||
let gen: In.Audio = In.Audio(bus: 2)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine, busOffset: 10)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 4)
|
||||
#expect(samples[0][0] == Double(110))
|
||||
#expect(samples[0][1] == Double(111))
|
||||
#expect(samples[0][2] == Double(112))
|
||||
#expect(samples[0][3] == Double(113))
|
||||
}
|
||||
|
||||
@Test func doesNotModifyBus() {
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
engine.audioBuses[1][i] = Float(i) * 0.5
|
||||
}
|
||||
let snapshot: [Float] = engine.audioBuses[1]
|
||||
let gen: In.Audio = In.Audio(bus: 1)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
#expect(engine.audioBuses[1] == snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("ControlIn")
|
||||
struct ControlInTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: In.Control = In.Control(bus: 0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
@Test func readsFromControlBus() {
|
||||
engine.controlBuses[3] = 0.75
|
||||
let gen: In.Control = In.Control(bus: 3)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
#expect(abs(gen.floatValue(graph: graph) - 0.75) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func calculatedSamplesInterpolatesToBusValue() throws {
|
||||
// ControlIn ramps from previousOutputValue (0.0 initially) to the current bus value.
|
||||
// With count=8 and target=1.5: step = 1.5/8 = 0.1875
|
||||
// Last valid sample (index 7) must equal the bus value exactly.
|
||||
// The pre-allocated buffer is lyrebirdDefaultResultBufferSize (512); only first 8 are valid.
|
||||
engine.controlBuses[7] = 1.5
|
||||
let gen: In.Control = In.Control(bus: 7)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].count >= 8)
|
||||
let validSlice: Array<Double>.SubSequence = samples[0].prefix(8)
|
||||
let last: Double = try #require(validSlice.last, "samples[0] must not be empty")
|
||||
#expect(abs(last - 1.5) < 1e-6)
|
||||
// Verify it is strictly increasing (ramp from 0 toward 1.5)
|
||||
let arr: [Double] = Array(validSlice)
|
||||
for i: Int in 0..<7 {
|
||||
#expect(arr[i] < arr[i + 1])
|
||||
}
|
||||
}
|
||||
|
||||
@Test func calculatedSamplesUpdatesPreviousOutputValue() {
|
||||
engine.controlBuses[7] = 1.5
|
||||
let gen: In.Control = In.Control(bus: 7)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(gen.previousOutputValue - 1.5) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func doesNotModifyControlBus() {
|
||||
engine.controlBuses[4] = 2.0
|
||||
let gen: In.Control = In.Control(bus: 4)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(engine.controlBuses[4] == 2.0)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Multi-channel In.Audio
|
||||
|
||||
@MainActor
|
||||
@Suite("In.Audio multi-channel")
|
||||
struct InAudioMultiChannelTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func multiChannelReturnsCorrectChannelCount() {
|
||||
let gen: In.Audio = In.Audio(bus: 4, channels: 3)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 3)
|
||||
}
|
||||
|
||||
@Test func multiChannelReadsConsecutiveBuses() {
|
||||
engine.audioBuses[4][0] = 0.1
|
||||
engine.audioBuses[5][0] = 0.5
|
||||
engine.audioBuses[6][0] = 0.9
|
||||
let gen: In.Audio = In.Audio(bus: 4, channels: 3)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(abs(out[0][0] - 0.1) < 1e-6)
|
||||
#expect(abs(out[1][0] - 0.5) < 1e-6)
|
||||
#expect(abs(out[2][0] - 0.9) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func singleChannelDefaultBehaviourUnchanged() {
|
||||
engine.audioBuses[7][0] = 0.42
|
||||
let gen: In.Audio = In.Audio(bus: 7)
|
||||
#expect(gen.channels == 1)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(out.count == 1)
|
||||
#expect(abs(out[0][0] - 0.42) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func floatValueAlwaysReturnsChannel0() {
|
||||
engine.audioBuses[4][0] = 0.25
|
||||
engine.audioBuses[5][0] = 0.75
|
||||
let gen: In.Audio = In.Audio(bus: 4, channels: 2)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
#expect(abs(gen.floatValue(graph: graph) - 0.25) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func audioBusConvenienceInitUsesChannelCount() throws {
|
||||
let bus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 2)
|
||||
engine.audioBuses[bus.index][0] = 0.3
|
||||
engine.audioBuses[bus.index + 1][0] = 0.7
|
||||
let gen: In.Audio = In.Audio(bus: bus)
|
||||
#expect(gen.channels == 2)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(out.count == 2)
|
||||
#expect(abs(out[0][0] - 0.3) < 1e-6)
|
||||
#expect(abs(out[1][0] - 0.7) < 1e-6)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Multi-channel In.Control
|
||||
|
||||
@MainActor
|
||||
@Suite("In.Control multi-channel")
|
||||
struct InControlMultiChannelTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func multiChannelReturnsCorrectChannelCount() {
|
||||
let gen: In.Control = In.Control(bus: 10, channels: 3)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 3)
|
||||
}
|
||||
|
||||
@Test func multiChannelReadsConsecutiveControlBuses() {
|
||||
engine.controlBuses[10] = 1.0
|
||||
engine.controlBuses[11] = 2.0
|
||||
engine.controlBuses[12] = 3.0
|
||||
let gen: In.Control = In.Control(bus: 10, channels: 3)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
// Each channel ramps from 0 to its bus value; last sample equals bus value
|
||||
#expect(abs(out[0].prefix(64).last! - 1.0) < 1e-6)
|
||||
#expect(abs(out[1].prefix(64).last! - 2.0) < 1e-6)
|
||||
#expect(abs(out[2].prefix(64).last! - 3.0) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func perChannelRampStateIsIndependent() {
|
||||
engine.controlBuses[20] = 1.0
|
||||
engine.controlBuses[21] = 0.0
|
||||
let gen: In.Control = In.Control(bus: 20, channels: 2)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
// ch0 ramps up, ch1 stays flat at 0
|
||||
#expect(out[0].prefix(64).last! > 0.5)
|
||||
#expect(out[1].prefix(64).allSatisfy { $0 == 0.0 })
|
||||
}
|
||||
|
||||
@Test func previousOutputValueProxiesChannel0() {
|
||||
engine.controlBuses[30] = 0.8
|
||||
let gen: In.Control = In.Control(bus: 30, channels: 2)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(abs(gen.previousOutputValue - 0.8) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func singleChannelDefaultBehaviourUnchanged() {
|
||||
engine.controlBuses[5] = 0.5
|
||||
let gen: In.Control = In.Control(bus: 5)
|
||||
#expect(gen.channels == 1)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 1)
|
||||
#expect(abs(out[0].prefix(64).last! - 0.5) < 1e-6)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
// MixInAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("MixIn.Audio")
|
||||
struct MixInAudioTests {
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 32)
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
/// Mono fold-down of two consecutive buses: output[i] = bus0[i] + bus1[i].
|
||||
@Test func sumsTwoMonoBuses() {
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
engine.audioBuses[3][i] = Float(i) * 0.1
|
||||
engine.audioBuses[4][i] = Float(i) * 0.01
|
||||
}
|
||||
let b0: AudioBus = AudioBus(index: 3, channelCount: 1)
|
||||
let b1: AudioBus = AudioBus(index: 4, channelCount: 1)
|
||||
let mix: MixIn.Audio = MixIn.Audio(buses: [b0, b1])
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = mix.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
|
||||
#expect(out.count == 1)
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
let expected: Double = Double(i) * 0.1 + Double(i) * 0.01
|
||||
#expect(abs(out[0][i] - expected) < 1e-6,
|
||||
"frame \(i): expected \(expected), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Non-contiguous bus indices: confirm the gap doesn't matter (this is the
|
||||
/// raison d'être of MixIn vs. `In.Audio(bus:channels:)`).
|
||||
@Test func sumsNonContiguousBuses() {
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
engine.audioBuses[2][i] = 0.25
|
||||
engine.audioBuses[7][i] = -0.75
|
||||
engine.audioBuses[15][i] = 0.5
|
||||
}
|
||||
let mix: MixIn.Audio = MixIn.Audio(buses: [
|
||||
AudioBus(index: 2, channelCount: 1),
|
||||
AudioBus(index: 7, channelCount: 1),
|
||||
AudioBus(index: 15, channelCount: 1)
|
||||
])
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = mix.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
|
||||
let expected: Double = 0.25 + (-0.75) + 0.5 // 0.0
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
#expect(abs(out[0][i] - expected) < 1e-6,
|
||||
"frame \(i): expected \(expected), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Parity vs. `+` chain of `In.Audio`. Same buses through both routes must
|
||||
/// produce identical output sample-by-sample.
|
||||
@Test func parityVsPlusChain() {
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
engine.audioBuses[8][i] = Float(i % 7) * 0.1
|
||||
engine.audioBuses[9][i] = Float(i % 11) * 0.05
|
||||
engine.audioBuses[10][i] = Float(i % 13) * 0.025
|
||||
engine.audioBuses[11][i] = Float(i % 17) * 0.0125
|
||||
}
|
||||
let buses: [AudioBus] = [
|
||||
AudioBus(index: 8, channelCount: 1),
|
||||
AudioBus(index: 9, channelCount: 1),
|
||||
AudioBus(index: 10, channelCount: 1),
|
||||
AudioBus(index: 11, channelCount: 1)
|
||||
]
|
||||
let mix: MixIn.Audio = MixIn.Audio(buses: buses)
|
||||
let chain: any AudioGenerator = (
|
||||
In.Audio(bus: 8) + In.Audio(bus: 9) + In.Audio(bus: 10) + In.Audio(bus: 11)
|
||||
)
|
||||
|
||||
// Use independent graphs so each route can pull its own bus reads.
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let mixOut: [[Double]] = mix.calculatedSamples(graph: g1, count: engine.blockSize)
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let chainOut: [[Double]] = chain.calculatedSamples(graph: g2, count: engine.blockSize)
|
||||
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
#expect(abs(mixOut[0][i] - chainOut[0][i]) < 1e-6,
|
||||
"frame \(i): mix \(mixOut[0][i]) vs chain \(chainOut[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Stereo buses: per-channel sum, output `outputChannelCount == 2`.
|
||||
@Test func sumsStereoBusesChannelWise() {
|
||||
// Bus 4 + 5 = stereo bus A; bus 6 + 7 = stereo bus B.
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
engine.audioBuses[4][i] = 0.1 // A left
|
||||
engine.audioBuses[5][i] = 0.2 // A right
|
||||
engine.audioBuses[6][i] = 0.3 // B left
|
||||
engine.audioBuses[7][i] = 0.4 // B right
|
||||
}
|
||||
let mix: MixIn.Audio = MixIn.Audio(buses: [
|
||||
AudioBus(index: 4, channelCount: 2),
|
||||
AudioBus(index: 6, channelCount: 2)
|
||||
])
|
||||
#expect(mix.outputChannelCount == 2)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = mix.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
|
||||
#expect(out.count == 2)
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
#expect(abs(out[0][i] - (0.1 + 0.3)) < 1e-6, "left frame \(i)")
|
||||
#expect(abs(out[1][i] - (0.2 + 0.4)) < 1e-6, "right frame \(i)")
|
||||
}
|
||||
}
|
||||
|
||||
/// MulAdd scalar fold: output scaled and offset by the final `mul`/`add`.
|
||||
@Test func mulAddAppliesScalar() {
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
engine.audioBuses[20][i] = 1.0
|
||||
engine.audioBuses[21][i] = 2.0
|
||||
}
|
||||
let mix: MixIn.Audio = MixIn.Audio(
|
||||
buses: [
|
||||
AudioBus(index: 20, channelCount: 1),
|
||||
AudioBus(index: 21, channelCount: 1)
|
||||
],
|
||||
mul: 0.5,
|
||||
add: 0.25
|
||||
)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = mix.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
|
||||
// (1 + 2) * 0.5 + 0.25 = 1.75
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
#expect(abs(out[0][i] - 1.75) < 1e-6, "frame \(i)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Single-bus mix: output equals input bus values (mul/add identity).
|
||||
@Test func singleBusPassthroughish() {
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
engine.audioBuses[12][i] = Float(i) * 0.01
|
||||
}
|
||||
let mix: MixIn.Audio = MixIn.Audio(buses: [AudioBus(index: 12, channelCount: 1)])
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = mix.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
#expect(abs(out[0][i] - Double(i) * 0.01) < 1e-6, "frame \(i)")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func outputChannelCountMatchesBusChannelCount() {
|
||||
let mixMono: MixIn.Audio = MixIn.Audio(buses: [AudioBus(index: 0, channelCount: 1)])
|
||||
#expect(mixMono.outputChannelCount == 1)
|
||||
|
||||
let mixStereo: MixIn.Audio = MixIn.Audio(buses: [
|
||||
AudioBus(index: 0, channelCount: 2),
|
||||
AudioBus(index: 4, channelCount: 2)
|
||||
])
|
||||
#expect(mixStereo.outputChannelCount == 2)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
// OutGeneratorTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Out (audio bus)")
|
||||
struct OutGeneratorTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
@Test func accumulatesIntoBus() {
|
||||
let gen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
for i: Int in 0..<8 {
|
||||
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func accumulatesAdditively() {
|
||||
// Two independent Out instances writing the same value to the same bus must sum to 2×value.
|
||||
// (Fan-out cache: calling the same instance twice in one segment is a no-op on the
|
||||
// second call — use two distinct instances to exercise additive accumulation.)
|
||||
let genA: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
let genB: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = genA.calculatedSamples(graph: graph, count: 8)
|
||||
_ = genB.calculatedSamples(graph: graph, count: 8)
|
||||
for i: Int in 0..<8 {
|
||||
#expect(abs(engine.audioBuses[0][i] - 1.0) < 1e-6)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func writesOnlyCountSamples() {
|
||||
let gen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine, busOffset: 0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 4)
|
||||
// First 4 samples should be 0.5
|
||||
for i: Int in 0..<4 {
|
||||
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6)
|
||||
}
|
||||
// Rest should remain 0
|
||||
for i: Int in 4..<engine.blockSize {
|
||||
#expect(engine.audioBuses[0][i] == 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func respectsBusOffset() {
|
||||
let gen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine, busOffset: 4)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 4)
|
||||
// Samples 0..3 should remain 0
|
||||
for i: Int in 0..<4 {
|
||||
#expect(engine.audioBuses[0][i] == 0.0)
|
||||
}
|
||||
// Samples 4..7 should be 0.5
|
||||
for i: Int in 4..<8 {
|
||||
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func writesToCorrectBus() {
|
||||
let gen: Out.Audio = Out.Audio(bus: 3, source: ConstantAudioGen(0.7))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
// Bus 3 should have values
|
||||
for i: Int in 0..<8 {
|
||||
#expect(abs(engine.audioBuses[3][i] - 0.7) < 1e-6)
|
||||
}
|
||||
// Bus 0 should remain 0
|
||||
for i: Int in 0..<8 {
|
||||
#expect(engine.audioBuses[0][i] == 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func passesThroughSamples() {
|
||||
let gen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.3))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(result[0].allSatisfy { abs($0 - 0.3) < 1e-6 })
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("ControlOut")
|
||||
struct ControlOutTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: Out.Control = Out.Control(bus: 0, source: 0.3 as Double)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
@Test func accumulatesIntoControlBus() {
|
||||
let gen: Out.Control = Out.Control(bus: 0, source: 0.3 as Double)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(engine.controlBuses[0] - 0.3) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func accumulatesAdditively() {
|
||||
// Two independent ControlOut instances writing the same value must sum to 2×value.
|
||||
// (Fan-out cache: same instance called twice in one segment is a no-op second call.)
|
||||
let genA: Out.Control = Out.Control(bus: 0, source: 0.3 as Double)
|
||||
let genB: Out.Control = Out.Control(bus: 0, source: 0.3 as Double)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = genA.calculatedSamples(graph: graph, count: 8)
|
||||
_ = genB.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(engine.controlBuses[0] - 0.6) < 1e-5)
|
||||
}
|
||||
|
||||
@Test func writesToCorrectBus() {
|
||||
let gen: Out.Control = Out.Control(bus: 5, source: 1.2 as Double)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(engine.controlBuses[5] - 1.2) < 1e-6)
|
||||
#expect(engine.controlBuses[0] == 0.0)
|
||||
}
|
||||
|
||||
/// Return value must be the source value, NOT the post-accumulated bus state.
|
||||
/// If the return were a bus readback, a second call on a new block (e.g. sub-block
|
||||
/// splitting) would return the already-accumulated total instead of the contribution.
|
||||
@Test func returnsSourceValueNotBusReadback() {
|
||||
let count: Int = 4
|
||||
let gen: Out.Control = Out.Control(bus: 0, source: 0.4 as Double)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// Block 1: writes 0.4 to bus, returns source value 0.4
|
||||
let result1: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
// Advance serial to simulate a new engine block (sub-block split scenario)
|
||||
advanceBlock(engine)
|
||||
// Block 2: writes another 0.4 to bus, must return 0.4 not the bus total
|
||||
let result2: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
// Both calls must return the source value (0.4), not the running bus total (0.4, 0.8).
|
||||
// The pre-allocated buffer is larger than count; only the first `count` elements are valid.
|
||||
#expect(result1[0].prefix(count).allSatisfy { abs($0 - 0.4) < 1e-6 })
|
||||
#expect(result2[0].prefix(count).allSatisfy { abs($0 - 0.4) < 1e-6 })
|
||||
// Bus has accumulated both contributions correctly.
|
||||
#expect(abs(engine.controlBuses[0] - 0.8) < 1e-5)
|
||||
}
|
||||
|
||||
/// Two independent ControlOut instances writing to the same bus sum correctly (polyphony).
|
||||
@Test func twoSourcesAccumulateCorrectly() {
|
||||
let genA: Out.Control = Out.Control(bus: 0, source: 0.3 as Double)
|
||||
let genB: Out.Control = Out.Control(bus: 0, source: 0.5 as Double)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = genA.calculatedSamples(graph: graph, count: 8)
|
||||
_ = genB.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(engine.controlBuses[0] - 0.8) < 1e-5)
|
||||
}
|
||||
|
||||
/// Out.Control must write to the bus exactly once per audio block even when
|
||||
/// controlBlockSize < blockSize. The ControlGenerator sub-block loop must be bypassed.
|
||||
@Test func singleBusWritePerBlockInSubBlockMode() throws {
|
||||
// blockSize=64, controlBlockSize=8 → 8 sub-blocks. Bus must accumulate 0.5 once, not 8×.
|
||||
let subEngine: SynthesisEngine = SynthesisEngine(
|
||||
blockSize: 64, controlBlockSize: 8,
|
||||
sampleRate: 44100,
|
||||
numOutputChannels: 2, numInputChannels: 0,
|
||||
driver: OfflineDriver()
|
||||
)
|
||||
try subEngine.start()
|
||||
let gen: Out.Control = Out.Control(bus: 0, source: 0.5 as Double)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: subEngine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(abs(subEngine.controlBuses[0] - 0.5) < 1e-6)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
// ReplaceOutAudioGeneratorTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("ReplaceOut.Audio")
|
||||
struct ReplaceOutAudioGeneratorTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
@Test func overwritesBus() {
|
||||
let gen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
for i: Int in 0..<8 {
|
||||
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func wipesPriorAdditiveWrites() {
|
||||
// Sequence: additive Out writes 0.3, then ReplaceOut writes 0.5.
|
||||
// Final bus must equal 0.5 (not 0.8).
|
||||
let outGen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.3))
|
||||
let repGen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = outGen.calculatedSamples(graph: graph, count: 8)
|
||||
_ = repGen.calculatedSamples(graph: graph, count: 8)
|
||||
for i: Int in 0..<8 {
|
||||
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func additiveAfterReplaceAccumulates() {
|
||||
// Sequence: ReplaceOut writes 0.5, then additive Out adds 0.3.
|
||||
// Final bus must equal 0.8.
|
||||
let repGen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
let outGen: Out.Audio = Out.Audio(bus: 0, source: ConstantAudioGen(0.3))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = repGen.calculatedSamples(graph: graph, count: 8)
|
||||
_ = outGen.calculatedSamples(graph: graph, count: 8)
|
||||
for i: Int in 0..<8 {
|
||||
#expect(abs(engine.audioBuses[0][i] - 0.8) < 1e-6)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func writesOnlyCountSamples() {
|
||||
let gen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine, busOffset: 0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 4)
|
||||
for i: Int in 0..<4 {
|
||||
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6)
|
||||
}
|
||||
for i: Int in 4..<engine.blockSize {
|
||||
#expect(engine.audioBuses[0][i] == 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func respectsBusOffset() {
|
||||
let gen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.5))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine, busOffset: 4)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 4)
|
||||
for i: Int in 0..<4 {
|
||||
#expect(engine.audioBuses[0][i] == 0.0)
|
||||
}
|
||||
for i: Int in 4..<8 {
|
||||
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func writesToCorrectBus() {
|
||||
let gen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 3, source: ConstantAudioGen(0.7))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
for i: Int in 0..<8 {
|
||||
#expect(abs(engine.audioBuses[3][i] - 0.7) < 1e-6)
|
||||
}
|
||||
for i: Int in 0..<8 {
|
||||
#expect(engine.audioBuses[0][i] == 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func audioBusClampsToChannelCount() {
|
||||
// 2-channel AudioBus, multi-channel source. Should overwrite only 2 buses.
|
||||
let bus: AudioBus = AudioBus(index: 1, channelCount: 2)
|
||||
let lhs: ConstantAudioGen = ConstantAudioGen(0.5)
|
||||
let rhs: ConstantAudioGen = ConstantAudioGen(0.7)
|
||||
// Use GeneratorArray to build a 2-row source.
|
||||
let stereo: GeneratorArray = GeneratorArray([lhs, rhs])
|
||||
let gen: ReplaceOut.Audio = ReplaceOut.Audio(bus: bus, source: stereo)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
for i: Int in 0..<8 {
|
||||
#expect(abs(engine.audioBuses[1][i] - 0.5) < 1e-6)
|
||||
#expect(abs(engine.audioBuses[2][i] - 0.7) < 1e-6)
|
||||
}
|
||||
// Adjacent buses must be untouched.
|
||||
for i: Int in 0..<8 {
|
||||
#expect(engine.audioBuses[0][i] == 0.0)
|
||||
#expect(engine.audioBuses[3][i] == 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func passesThroughSamples() {
|
||||
let gen: ReplaceOut.Audio = ReplaceOut.Audio(bus: 0, source: ConstantAudioGen(0.3))
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(result[0].prefix(8).allSatisfy { abs($0 - 0.3) < 1e-6 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// ReplaceOutControlGeneratorTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("ReplaceOut.Control")
|
||||
struct ReplaceOutControlGeneratorTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: ReplaceOut.Control = ReplaceOut.Control(bus: 0, source: 0.3 as Double)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
@Test func overwritesControlBus() {
|
||||
let gen: ReplaceOut.Control = ReplaceOut.Control(bus: 0, source: 0.3 as Double)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(engine.controlBuses[0] - 0.3) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func wipesPriorAdditiveControlWrite() {
|
||||
let outGen: Out.Control = Out.Control(bus: 0, source: 0.4 as Double)
|
||||
let repGen: ReplaceOut.Control = ReplaceOut.Control(bus: 0, source: 0.9 as Double)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = outGen.calculatedSamples(graph: graph, count: 8)
|
||||
_ = repGen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(engine.controlBuses[0] - 0.9) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func writesToCorrectBus() {
|
||||
let gen: ReplaceOut.Control = ReplaceOut.Control(bus: 5, source: 1.2 as Double)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(engine.controlBuses[5] - 1.2) < 1e-6)
|
||||
#expect(engine.controlBuses[0] == 0.0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user