236 lines
9.0 KiB
Swift
236 lines
9.0 KiB
Swift
// SynthesisEngineTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// MARK: - Init State
|
|
|
|
@Suite("Init state") struct InitStateTests {
|
|
@Test func storesBlockSize() {
|
|
let e: SynthesisEngine = makeTestEngine(blockSize: 256)
|
|
#expect(e.blockSize == 256)
|
|
}
|
|
@Test func storesSampleRate() {
|
|
let e: SynthesisEngine = makeTestEngine(sampleRate: 48000)
|
|
#expect(e.sampleRate == 48000)
|
|
}
|
|
@Test func storesNumOutputChannels() {
|
|
let e: SynthesisEngine = makeTestEngine(numOutputChannels: 4)
|
|
#expect(e.numOutputChannels == 4)
|
|
}
|
|
@Test func storesNumInputChannels() {
|
|
let e: SynthesisEngine = makeTestEngine(numInputChannels: 1)
|
|
#expect(e.numInputChannels == 1)
|
|
}
|
|
@Test func storesNumAudioBuses() {
|
|
let e: SynthesisEngine = makeTestEngine(numAudioBuses: 64)
|
|
#expect(e.numAudioBuses == 64)
|
|
}
|
|
@Test func storesNumControlBuses() {
|
|
let e: SynthesisEngine = makeTestEngine(numControlBuses: 1024)
|
|
#expect(e.numControlBuses == 1024)
|
|
}
|
|
@Test func notRunningAfterInit() {
|
|
#expect(makeTestEngine().isRunning == false)
|
|
}
|
|
@Test func busesEmptyBeforeStart() {
|
|
let e: SynthesisEngine = makeTestEngine()
|
|
#expect(e.audioBuses.isEmpty)
|
|
#expect(e.controlBuses.isEmpty)
|
|
}
|
|
}
|
|
|
|
// MARK: - Bus Allocation
|
|
|
|
@Suite("Bus allocation") struct BusAllocationTests {
|
|
@Test func audioBusCountAfterStart() throws {
|
|
let e: SynthesisEngine = makeTestEngine(numAudioBuses: 128)
|
|
try e.start()
|
|
#expect(e.audioBuses.count == 128)
|
|
}
|
|
@Test func controlBusCountAfterStart() throws {
|
|
let e: SynthesisEngine = makeTestEngine(numControlBuses: 4096)
|
|
try e.start()
|
|
#expect(e.controlBuses.count == 4096)
|
|
}
|
|
@Test func audioBusCountRespectsSmallerValue() throws {
|
|
let e: SynthesisEngine = makeTestEngine(numAudioBuses: 16)
|
|
try e.start()
|
|
#expect(e.audioBuses.count == 16)
|
|
}
|
|
@Test func eachAudioBusHasBlockSizeSamples() throws {
|
|
let e: SynthesisEngine = makeTestEngine(blockSize: 256, numAudioBuses: 8)
|
|
try e.start()
|
|
for bus: [Float] in e.audioBuses { #expect(bus.count == 256) }
|
|
}
|
|
@Test func audioBusesInitiallyZero() throws {
|
|
let e: SynthesisEngine = makeTestEngine(blockSize: 64, numAudioBuses: 4)
|
|
try e.start()
|
|
for bus: [Float] in e.audioBuses { for s: Float in bus { #expect(s == 0.0) } }
|
|
}
|
|
@Test func controlBusesInitiallyZero() throws {
|
|
let e: SynthesisEngine = makeTestEngine(numControlBuses: 16)
|
|
try e.start()
|
|
for v: Float in e.controlBuses { #expect(v == 0.0) }
|
|
}
|
|
}
|
|
|
|
// MARK: - Lifecycle
|
|
|
|
@Suite("Lifecycle") struct LifecycleTests {
|
|
@Test func isRunningAfterStart() throws {
|
|
let e: SynthesisEngine = makeTestEngine()
|
|
try e.start()
|
|
#expect(e.isRunning == true)
|
|
}
|
|
@Test func isNotRunningAfterStop() throws {
|
|
let e: SynthesisEngine = makeTestEngine()
|
|
try e.start()
|
|
e.stop()
|
|
#expect(e.isRunning == false)
|
|
}
|
|
@Test func isNotRunningBeforeStart() {
|
|
#expect(makeTestEngine().isRunning == false)
|
|
}
|
|
@Test func doubleStartIsIdempotent() throws {
|
|
let e: SynthesisEngine = makeTestEngine(numAudioBuses: 8)
|
|
try e.start()
|
|
let count: Int = e.audioBuses.count
|
|
try e.start()
|
|
#expect(e.audioBuses.count == count)
|
|
#expect(e.isRunning == true)
|
|
}
|
|
@Test func doubleStopDoesNotCrash() throws {
|
|
let e: SynthesisEngine = makeTestEngine()
|
|
try e.start()
|
|
e.stop()
|
|
e.stop()
|
|
#expect(e.isRunning == false)
|
|
}
|
|
}
|
|
|
|
// MARK: - Bus Index Convention (SC layout)
|
|
|
|
@Suite("Bus index convention") struct BusConventionTests {
|
|
@Test func outputBusesAtIndexZero() throws {
|
|
let e: SynthesisEngine = makeTestEngine(numOutputChannels: 2)
|
|
try e.start()
|
|
#expect(e.audioBuses[0].count == e.blockSize)
|
|
#expect(e.audioBuses[1].count == e.blockSize)
|
|
}
|
|
@Test func inputBusesStartAfterOutputBuses() throws {
|
|
let e: SynthesisEngine = makeTestEngine(numOutputChannels: 2, numInputChannels: 2)
|
|
try e.start()
|
|
let start: Int = e.numOutputChannels
|
|
#expect(e.audioBuses[start].count == e.blockSize)
|
|
#expect(e.audioBuses[start + 1].count == e.blockSize)
|
|
}
|
|
@Test func userBusesStartAfterHardwareBuses() throws {
|
|
let e: SynthesisEngine = makeTestEngine(numOutputChannels: 2, numInputChannels: 2, numAudioBuses: 16)
|
|
try e.start()
|
|
let userStart: Int = e.numOutputChannels + e.numInputChannels
|
|
#expect(userStart == 4)
|
|
#expect(e.audioBuses[userStart].count == e.blockSize)
|
|
}
|
|
@Test func totalBusCountExceedsHardwareChannels() throws {
|
|
let e: SynthesisEngine = makeTestEngine(numOutputChannels: 2, numInputChannels: 2, numAudioBuses: 128)
|
|
try e.start()
|
|
#expect(e.audioBuses.count == 128)
|
|
}
|
|
}
|
|
|
|
// MARK: - processBlock Cycle Semantics
|
|
|
|
@Suite("processBlock") struct ProcessBlockTests {
|
|
@Test func audioBusesZeroedEachBlock() throws {
|
|
let e: SynthesisEngine = makeTestEngine(blockSize: 4, numOutputChannels: 2,
|
|
numInputChannels: 0, numAudioBuses: 8)
|
|
try e.start()
|
|
// Call twice with no generators — user buses must be zero both times
|
|
e.processBlock(inputSamples: [])
|
|
e.processBlock(inputSamples: [])
|
|
let userStart: Int = e.numOutputChannels + e.numInputChannels
|
|
for bus: [Float] in e.audioBuses[userStart...] {
|
|
for s: Float in bus { #expect(s == 0.0) }
|
|
}
|
|
}
|
|
@Test func controlBusesNotZeroedByProcessBlock() throws {
|
|
let e: SynthesisEngine = makeTestEngine(numControlBuses: 8)
|
|
try e.start()
|
|
// All start at zero; processBlock must not alter them
|
|
e.processBlock(inputSamples: [])
|
|
for v: Float in e.controlBuses { #expect(v == 0.0) }
|
|
}
|
|
@Test func inputSamplesCopiedToInputBuses() throws {
|
|
let e: SynthesisEngine = makeTestEngine(blockSize: 4, numOutputChannels: 2,
|
|
numInputChannels: 2, numAudioBuses: 16)
|
|
try e.start()
|
|
let ch0: [Float] = [0.1, 0.2, 0.3, 0.4]
|
|
let ch1: [Float] = [0.5, 0.6, 0.7, 0.8]
|
|
e.processBlock(inputSamples: [ch0, ch1])
|
|
let inBus0: [Float] = e.audioBuses[e.numOutputChannels]
|
|
let inBus1: [Float] = e.audioBuses[e.numOutputChannels + 1]
|
|
#expect(inBus0 == [0.1, 0.2, 0.3, 0.4])
|
|
#expect(inBus1 == [0.5, 0.6, 0.7, 0.8])
|
|
}
|
|
@Test func inputBusOverwrittenEachBlock() throws {
|
|
let e: SynthesisEngine = makeTestEngine(blockSize: 4, numOutputChannels: 2,
|
|
numInputChannels: 1, numAudioBuses: 8)
|
|
try e.start()
|
|
e.processBlock(inputSamples: [[1.0, 1.0, 1.0, 1.0]])
|
|
e.processBlock(inputSamples: [[0.0, 0.0, 0.0, 0.0]])
|
|
#expect(e.audioBuses[e.numOutputChannels][0] == 0.0)
|
|
}
|
|
@Test func outputBusesZeroWithNoGenerators() throws {
|
|
let e: SynthesisEngine = makeTestEngine(blockSize: 8, numOutputChannels: 2)
|
|
try e.start()
|
|
e.processBlock(inputSamples: [])
|
|
for i: Int in 0..<e.numOutputChannels {
|
|
for s: Float in e.audioBuses[i] { #expect(s == 0.0) }
|
|
}
|
|
}
|
|
}
|
|
|
|
@Suite("pendingQueueDroppedCount") struct PendingQueueDropTests {
|
|
private final class _Empty: SynthesizerBase { override func build() {} }
|
|
|
|
@Test func zeroDropsOnHappyPath() throws {
|
|
let e: SynthesisEngine = makeTestEngine(blockSize: 8, eventQueueCapacity: 16)
|
|
try e.start()
|
|
for _ in 0..<8 { try? e.add(_Empty()) }
|
|
#expect(e.pendingQueueDroppedCount == 0)
|
|
}
|
|
|
|
@Test func dropsCountedWhenQueueFull() throws {
|
|
// Fill the queue without draining (do NOT start the engine — processBlock drains it).
|
|
// Phase 196.2: engine.add now throws EngineAddError.pendingQueueFull
|
|
// instead of returning Bool false. Test reformulated to count
|
|
// successful enqueues via do/catch.
|
|
let e: SynthesisEngine = makeTestEngine(blockSize: 8, eventQueueCapacity: 4)
|
|
e.resetPendingQueueDroppedCount()
|
|
var enqueued: Int = 0
|
|
for _ in 0..<16 {
|
|
do {
|
|
try e.add(_Empty())
|
|
enqueued += 1
|
|
} catch {
|
|
// Expected once queue fills — pendingQueueFull
|
|
}
|
|
}
|
|
#expect(enqueued == 4, "queue capacity 4 should accept exactly 4 events before filling")
|
|
#expect(e.pendingQueueDroppedCount == 12, "remaining 12 attempts should each record a drop")
|
|
}
|
|
|
|
@Test func resetClearsCounter() throws {
|
|
let e: SynthesisEngine = makeTestEngine(blockSize: 8, eventQueueCapacity: 2)
|
|
for _ in 0..<8 { try? e.add(_Empty()) }
|
|
#expect(e.pendingQueueDroppedCount > 0)
|
|
e.resetPendingQueueDroppedCount()
|
|
#expect(e.pendingQueueDroppedCount == 0)
|
|
}
|
|
}
|