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

212 lines
8.4 KiB
Swift

// AUv3DriverTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import AudioToolbox
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - Test fixtures
/// Synth that writes a deterministic counter into each output channel: sample i
/// of channel c receives the value `Double(sampleIndex + i + c * 10000)`. Lets
/// tests verify the ring buffer preserves temporal + channel ordering.
private final class CounterSynth: SynthesizerBase {
override func build() {
let gen: CounterGen = CounterGen()
output(bus: 0, source: gen[0])
output(bus: 1, source: gen[1])
}
}
private final class CounterGen: GeneratorBase, AudioGenerator {
private var _sampleIndex: Int = 0
override init(channels: Int = 2) {
super.init(channels: channels)
}
public override var outputChannelCount: Int { _result.count }
public func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
_checkCount(count)
for i: Int in 0..<count {
_result[0][i] = Double(_sampleIndex + i)
_result[1][i] = Double(_sampleIndex + i + 10000)
}
_sampleIndex += count
return _result
}
public func floatValue(graph: SynthGraph) -> Double {
calculatedSamples(graph: graph, count: graph.engine.blockSize).first?.first ?? 0
}
}
// MARK: - Helpers
/// Builds a heap-allocated AudioBufferList for `channels` mono Float channels,
/// each `frames` long. Returns the buffer-list pointer + the per-channel storage
/// arrays so the caller can inspect samples after `render`.
private struct ABLFixture {
let abl: UnsafeMutablePointer<AudioBufferList>
let channelStorage: [UnsafeMutablePointer<Float>]
let frames: Int
let channels: Int
init(channels: Int, frames: Int) {
self.channels = channels
self.frames = frames
let size: Int = AudioBufferList.sizeInBytes(maximumBuffers: channels)
let raw: UnsafeMutableRawPointer = UnsafeMutableRawPointer.allocate(byteCount: size, alignment: MemoryLayout<AudioBufferList>.alignment)
let abl: UnsafeMutablePointer<AudioBufferList> = raw.assumingMemoryBound(to: AudioBufferList.self)
abl.pointee = AudioBufferList(mNumberBuffers: UInt32(channels),
mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil))
var storage: [UnsafeMutablePointer<Float>] = []
let listPtr: UnsafeMutableAudioBufferListPointer = UnsafeMutableAudioBufferListPointer(abl)
for c: Int in 0..<channels {
let ptr: UnsafeMutablePointer<Float> = UnsafeMutablePointer<Float>.allocate(capacity: frames)
for i: Int in 0..<frames { ptr[i] = 0 }
storage.append(ptr)
listPtr[c] = AudioBuffer(
mNumberChannels: 1,
mDataByteSize: UInt32(frames * MemoryLayout<Float>.size),
mData: UnsafeMutableRawPointer(ptr)
)
}
self.abl = abl
self.channelStorage = storage
}
func channelSlice(_ c: Int) -> [Float] {
let ptr: UnsafeMutablePointer<Float> = channelStorage[c]
return Array(UnsafeBufferPointer(start: ptr, count: frames))
}
func deallocate() {
for p: UnsafeMutablePointer<Float> in channelStorage { p.deallocate() }
UnsafeMutableRawPointer(abl).deallocate()
}
}
@Suite("AUv3Driver") struct AUv3DriverTests {
@Test func returnsUninitializedBeforeSetup() {
let driver: AUv3Driver = AUv3Driver()
let fixture: ABLFixture = ABLFixture(channels: 2, frames: 64)
defer { fixture.deallocate() }
let status: AUAudioUnitStatus = driver.render(frameCount: 64, outputData: fixture.abl)
#expect(status == kAudioUnitErr_Uninitialized)
}
@Test func rejectsFrameCountAboveMax() throws {
let driver: AUv3Driver = AUv3Driver(maxFramesToRender: 256)
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 128, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 0,
driver: driver
)
try engine.start()
let fixture: ABLFixture = ABLFixture(channels: 2, frames: 512)
defer { fixture.deallocate() }
let status: AUAudioUnitStatus = driver.render(frameCount: 512, outputData: fixture.abl)
#expect(status == kAudioUnitErr_TooManyFramesToProcess)
engine.stop()
}
@Test func smallHostFrameCountDrivesMultipleEngineBlocks() throws {
// Engine block 128; host requests 64 per call — two calls per engine block.
let driver: AUv3Driver = AUv3Driver(maxFramesToRender: 64)
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 128, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 0,
driver: driver
)
let synth: CounterSynth = CounterSynth()
try engine.start()
try engine.add(synth)
let fixture: ABLFixture = ABLFixture(channels: 2, frames: 64)
defer { fixture.deallocate() }
// First render — engine produces one block (128 frames); ring delivers 64.
var status: AUAudioUnitStatus = driver.render(frameCount: 64, outputData: fixture.abl)
#expect(status == noErr)
let firstL: [Float] = fixture.channelSlice(0)
let firstR: [Float] = fixture.channelSlice(1)
#expect(firstL.first == 0)
#expect(firstL.last == 63)
#expect(firstR.first == 10000)
#expect(firstR.last == 10063)
// Second render — pulls remaining 64 from the same engine block.
status = driver.render(frameCount: 64, outputData: fixture.abl)
#expect(status == noErr)
let secondL: [Float] = fixture.channelSlice(0)
#expect(secondL.first == 64)
#expect(secondL.last == 127)
// Third render — engine must advance another block.
status = driver.render(frameCount: 64, outputData: fixture.abl)
#expect(status == noErr)
let thirdL: [Float] = fixture.channelSlice(0)
#expect(thirdL.first == 128)
#expect(thirdL.last == 191)
engine.stop()
}
@Test func largeHostFrameCountPullsMultipleEngineBlocks() throws {
// Host wants 1024 per call but engine block is 256 — 4 engine blocks per
// render call.
let driver: AUv3Driver = AUv3Driver(maxFramesToRender: 1024)
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 256, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 0,
driver: driver
)
let synth: CounterSynth = CounterSynth()
try engine.start()
try engine.add(synth)
let fixture: ABLFixture = ABLFixture(channels: 2, frames: 1024)
defer { fixture.deallocate() }
let status: AUAudioUnitStatus = driver.render(frameCount: 1024, outputData: fixture.abl)
#expect(status == noErr)
let samples: [Float] = fixture.channelSlice(0)
#expect(samples.first == 0)
#expect(samples[256] == 256)
#expect(samples[512] == 512)
#expect(samples.last == 1023)
engine.stop()
}
@Test func alternatingFrameCountsPreserveContinuity() throws {
// Hosts may switch frame counts mid-stream; the ring must keep sample
// continuity intact.
let driver: AUv3Driver = AUv3Driver(maxFramesToRender: 1024)
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 128, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 0,
driver: driver
)
let synth: CounterSynth = CounterSynth()
try engine.start()
try engine.add(synth)
let counts: [Int] = [1, 7, 64, 1, 200, 33, 511, 1]
var nextExpected: Float = 0
for count: Int in counts {
let fixture: ABLFixture = ABLFixture(channels: 2, frames: count)
defer { fixture.deallocate() }
let status: AUAudioUnitStatus = driver.render(frameCount: count, outputData: fixture.abl)
#expect(status == noErr)
let samples: [Float] = fixture.channelSlice(0)
#expect(samples.first == nextExpected, "first mismatch at count=\(count)")
#expect(samples.last == nextExpected + Float(count - 1), "last mismatch at count=\(count)")
nextExpected += Float(count)
}
engine.stop()
}
}