Files
Lyrebird/Tests/LyrebirdTests/BusErgonomicsTests.swift
T

200 lines
7.1 KiB
Swift

// BusErgonomicsTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - Test synthesizers
/// Uses `output(bus:source:)` to write a constant to bus 0.
private final class SingleOutputSynth: SynthesizerBase {
let value: Double
let bus: Int
init(value: Double, bus: Int = 0) {
self.value = value
self.bus = bus
super.init()
}
override func build() {
output(bus: bus, source: value)
}
}
/// Uses `output(buses:source:)` to write a constant to a range of audio buses.
private final class MultiOutputSynth: SynthesizerBase {
let value: Double
let buses: Range<Int>
init(value: Double, buses: Range<Int>) {
self.value = value
self.buses = buses
super.init()
}
override func build() {
output(buses: buses, source: value)
}
}
/// Uses `controlOutput(bus:source:)` to write a constant to a control bus.
private final class ControlOutputSynth: SynthesizerBase {
let value: Double
let bus: Int
init(value: Double, bus: Int = 0) {
self.value = value
self.bus = bus
super.init()
}
override func build() {
controlOutput(bus: bus, source: value)
}
}
/// Uses `input(bus:)` to read an audio bus and routes it to another bus.
private final class InputRoutingSynth: SynthesizerBase {
let sourceBus: Int
let destBus: Int
init(sourceBus: Int, destBus: Int) {
self.sourceBus = sourceBus
self.destBus = destBus
super.init()
}
override func build() {
output(bus: destBus, source: input(bus: sourceBus))
}
}
/// Uses `controlInput(bus:)` to read a control bus and routes it to an audio bus.
private final class ControlInputRoutingSynth: SynthesizerBase {
let controlBusIndex: Int
let audioBusIndex: Int
init(controlBusIndex: Int, audioBusIndex: Int) {
self.controlBusIndex = controlBusIndex
self.audioBusIndex = audioBusIndex
super.init()
}
override func build() {
output(bus: audioBusIndex, source: controlInput(bus: controlBusIndex))
}
}
// MARK: - Tests
@MainActor
@Suite("Bus Ergonomics — SynthesizerBase convenience helpers")
struct BusErgonomicsTests {
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
init() throws {
try engine.start()
}
// MARK: output(bus:source:)
@Test func outputBusWritesToCorrectBus() throws {
let synth: SingleOutputSynth = SingleOutputSynth(value: 0.5, bus: 0)
try engine.add(synth)
engine.processBlock(inputSamples: [])
for i: Int in 0..<engine.blockSize {
#expect(abs(engine.audioBuses[0][i] - 0.5) < 1e-6,
"bus 0 sample \(i) should be 0.5")
}
}
@Test func outputBusDoesNotWriteToOtherBuses() throws {
let synth: SingleOutputSynth = SingleOutputSynth(value: 0.7, bus: 2)
try engine.add(synth)
engine.processBlock(inputSamples: [])
// Bus 2 should have the value
#expect(abs(engine.audioBuses[2][0] - 0.7) < 1e-6)
// Bus 0 should remain 0
#expect(engine.audioBuses[0][0] == 0.0)
}
// MARK: output(buses:source:)
@Test func outputBusesWritesToAllBusesInRange() throws {
let synth: MultiOutputSynth = MultiOutputSynth(value: 0.3, buses: 0..<4)
try engine.add(synth)
engine.processBlock(inputSamples: [])
for bus: Int in 0..<4 {
#expect(abs(engine.audioBuses[bus][0] - 0.3) < 1e-6,
"bus \(bus) sample 0 should be 0.3")
}
}
@Test func outputBusesDoesNotWriteOutsideRange() throws {
let synth: MultiOutputSynth = MultiOutputSynth(value: 0.6, buses: 1..<3)
try engine.add(synth)
engine.processBlock(inputSamples: [])
// Bus 0 untouched
#expect(engine.audioBuses[0][0] == 0.0)
// Buses 1 and 2 written
#expect(abs(engine.audioBuses[1][0] - 0.6) < 1e-6)
#expect(abs(engine.audioBuses[2][0] - 0.6) < 1e-6)
// Bus 3 untouched
#expect(engine.audioBuses[3][0] == 0.0)
}
@Test func outputBusesCreatesIndependentInstances() throws {
// Each bus should have exactly the source value — not double-accumulated.
// If instances were shared, bus writes would accumulate onto each other incorrectly.
let synth: MultiOutputSynth = MultiOutputSynth(value: 0.4, buses: 0..<2)
try engine.add(synth)
engine.processBlock(inputSamples: [])
#expect(abs(engine.audioBuses[0][0] - 0.4) < 1e-6,
"bus 0 should be 0.4, not 0.8 (would be 0.8 if same Out instance accumulated twice)")
#expect(abs(engine.audioBuses[1][0] - 0.4) < 1e-6,
"bus 1 should be 0.4, not 0.8")
}
// MARK: controlOutput(bus:source:)
@Test func controlOutputWritesToCorrectControlBus() throws {
let synth: ControlOutputSynth = ControlOutputSynth(value: 1.5, bus: 3)
try engine.add(synth)
engine.processBlock(inputSamples: [])
#expect(abs(engine.controlBuses[3] - 1.5) < 1e-6)
}
@Test func controlOutputDoesNotWriteToOtherBuses() throws {
let synth: ControlOutputSynth = ControlOutputSynth(value: 0.9, bus: 5)
try engine.add(synth)
engine.processBlock(inputSamples: [])
#expect(abs(engine.controlBuses[5] - 0.9) < 1e-6)
#expect(engine.controlBuses[0] == 0.0)
}
// MARK: input(bus:)
@Test func inputReturnsWorkingInGenerator() throws {
// Write a known value to bus 4; processBlock zeroes audio buses before evaluating,
// so the writer (sourceSynth) must evaluate before the reader (routingSynth).
// engine.add uses addToHead: the last-added synth evaluates first.
// Add reader first (tail), then writer (head) so writer evaluates before reader.
let sourceSynth: SingleOutputSynth = SingleOutputSynth(value: 0.25, bus: 4)
let routingSynth: InputRoutingSynth = InputRoutingSynth(sourceBus: 4, destBus: 6)
try engine.add(routingSynth) // reader first → tail
try engine.add(sourceSynth) // writer last → head (evaluates before routingSynth)
engine.processBlock(inputSamples: [])
for i: Int in 0..<engine.blockSize {
#expect(abs(engine.audioBuses[6][i] - 0.25) < 1e-6,
"bus 6 sample \(i) should be 0.25 (routed from bus 4)")
}
}
// MARK: controlInput(bus:)
@Test func controlInputReturnsWorkingControlInGenerator() throws {
engine.controlBuses[2] = Float(0.8)
let synth: ControlInputRoutingSynth = ControlInputRoutingSynth(controlBusIndex: 2, audioBusIndex: 7)
try engine.add(synth)
engine.processBlock(inputSamples: [])
// ControlIn interpolates from 0.0 to 0.8; last sample should equal the bus value.
let lastSample: Float = engine.audioBuses[7][engine.blockSize - 1]
#expect(abs(Double(lastSample) - 0.8) < 1e-5,
"last sample of bus 7 should reach control bus value 0.8")
}
}