138 lines
4.8 KiB
Swift
138 lines
4.8 KiB
Swift
// SynthesisEngineConfigTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// MARK: - Pre-run config setters
|
|
|
|
@Suite("Pre-run config setters") struct PreRunConfigSetterTests {
|
|
|
|
@Test func setSampleRateWhenIdleUpdatesValue() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
try engine.setSampleRate(96000)
|
|
#expect(engine.sampleRate == 96000)
|
|
}
|
|
|
|
@Test func setSampleRateAfterStopUpdatesValue() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
try engine.start()
|
|
engine.stop()
|
|
try engine.setSampleRate(48000)
|
|
#expect(engine.sampleRate == 48000)
|
|
}
|
|
|
|
@Test func setSampleRateWhileRunningThrows() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
try engine.start()
|
|
defer { engine.stop() }
|
|
#expect(throws: SynthesisEngineConfigError.self) {
|
|
try engine.setSampleRate(48000)
|
|
}
|
|
#expect(engine.sampleRate == 44100)
|
|
}
|
|
|
|
@Test func setSampleRateRejectsNonPositive() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
#expect(throws: SynthesisEngineConfigError.self) {
|
|
try engine.setSampleRate(0)
|
|
}
|
|
#expect(throws: SynthesisEngineConfigError.self) {
|
|
try engine.setSampleRate(-1)
|
|
}
|
|
#expect(engine.sampleRate == 44100)
|
|
}
|
|
|
|
@Test func setSampleRateRejectsNonFinite() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
#expect(throws: SynthesisEngineConfigError.self) {
|
|
try engine.setSampleRate(.nan)
|
|
}
|
|
#expect(throws: SynthesisEngineConfigError.self) {
|
|
try engine.setSampleRate(.infinity)
|
|
}
|
|
#expect(engine.sampleRate == 44100)
|
|
}
|
|
|
|
@Test func setNumOutputChannelsUpdatesValue() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(numOutputChannels: 2)
|
|
try engine.setNumOutputChannels(4)
|
|
#expect(engine.numOutputChannels == 4)
|
|
}
|
|
|
|
@Test func setNumOutputChannelsWhileRunningThrows() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(numOutputChannels: 2)
|
|
try engine.start()
|
|
defer { engine.stop() }
|
|
#expect(throws: SynthesisEngineConfigError.self) {
|
|
try engine.setNumOutputChannels(4)
|
|
}
|
|
#expect(engine.numOutputChannels == 2)
|
|
}
|
|
|
|
@Test func setNumOutputChannelsRejectsZero() {
|
|
let engine: SynthesisEngine = makeTestEngine(numOutputChannels: 2)
|
|
#expect(throws: SynthesisEngineConfigError.self) {
|
|
try engine.setNumOutputChannels(0)
|
|
}
|
|
}
|
|
|
|
@Test func setNumOutputChannelsRejectsOverflow() {
|
|
let engine: SynthesisEngine = makeTestEngine(
|
|
numOutputChannels: 2,
|
|
numInputChannels: 2,
|
|
numAudioBuses: 8
|
|
)
|
|
#expect(throws: SynthesisEngineConfigError.self) {
|
|
try engine.setNumOutputChannels(7) // 7 + 2 > 8
|
|
}
|
|
}
|
|
|
|
@Test func setNumInputChannelsAcceptsZero() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(numInputChannels: 2)
|
|
try engine.setNumInputChannels(0)
|
|
#expect(engine.numInputChannels == 0)
|
|
}
|
|
|
|
@Test func setNumInputChannelsRejectsNegative() {
|
|
let engine: SynthesisEngine = makeTestEngine(numInputChannels: 2)
|
|
#expect(throws: SynthesisEngineConfigError.self) {
|
|
try engine.setNumInputChannels(-1)
|
|
}
|
|
}
|
|
|
|
@Test func setNumInputChannelsWhileRunningThrows() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(numInputChannels: 0)
|
|
try engine.start()
|
|
defer { engine.stop() }
|
|
#expect(throws: SynthesisEngineConfigError.self) {
|
|
try engine.setNumInputChannels(2)
|
|
}
|
|
#expect(engine.numInputChannels == 0)
|
|
}
|
|
|
|
@Test func newSampleRatePropagatesToBusAllocationOnStart() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(
|
|
blockSize: 64,
|
|
sampleRate: 44100,
|
|
numOutputChannels: 2,
|
|
numAudioBuses: 16
|
|
)
|
|
try engine.setSampleRate(96000)
|
|
try engine.setNumOutputChannels(4)
|
|
try engine.start()
|
|
#expect(engine.sampleRate == 96000)
|
|
#expect(engine.audioBuses.count == 16)
|
|
#expect(engine.audioBuses[0].count == 64)
|
|
// Output reservation now spans buses 0..3; the next allocator request
|
|
// must start at index 4 (numOutputChannels) + numInputChannels.
|
|
let bus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
|
#expect(bus.index == engine.numOutputChannels + engine.numInputChannels)
|
|
engine.busAllocator.releaseAudioBus(bus)
|
|
}
|
|
}
|
|
|