427 lines
18 KiB
Swift
427 lines
18 KiB
Swift
// BufferReadTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("BufferRead")
|
|
struct BufferReadTests {
|
|
|
|
// MARK: - Helpers
|
|
|
|
func makeEngine(blockSize: Int = 16) throws -> SynthesisEngine {
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
try engine.start()
|
|
return engine
|
|
}
|
|
|
|
/// Fill buffer frames with their index value per channel.
|
|
/// ch 0: frame index value; ch 1: frame index + 100.0; etc.
|
|
func makeRampBuffer(frames: Int, channelCount: Int = 1) -> LyrebirdBuffer {
|
|
let buf: LyrebirdBuffer = LyrebirdBuffer(count: frames, channelCount: channelCount)
|
|
for frame: Int in 0..<frames {
|
|
for ch: Int in 0..<channelCount {
|
|
buf.write(Double(frame) + Double(ch) * 100.0,
|
|
frame: frame, channel: ch)
|
|
}
|
|
}
|
|
return buf
|
|
}
|
|
|
|
// MARK: - Audio — position semantics
|
|
|
|
@Test func audioPositionZeroReturnsFirstFrame() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32, channelCount: 2)
|
|
let reader: BufferRead.Audio = BufferRead.Audio(buffer: buf, position: 0.0, interpolation: .none)
|
|
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 16)
|
|
|
|
for i: Int in 0..<16 {
|
|
#expect(abs(out[0][i] - 0.0) < 1e-9,
|
|
"ch0 sample \(i): expected 0.0, got \(out[0][i])")
|
|
#expect(abs(out[1][i] - 100.0) < 1e-9,
|
|
"ch1 sample \(i): expected 100.0, got \(out[1][i])")
|
|
}
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func audioPositionOneReturnsLastFrame() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let frames: Int = 32
|
|
let buf: LyrebirdBuffer = makeRampBuffer(frames: frames, channelCount: 2)
|
|
let reader: BufferRead.Audio = BufferRead.Audio(buffer: buf, position: 1.0, interpolation: .none)
|
|
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 16)
|
|
|
|
let lastFrame: Double = Double(frames - 1)
|
|
for i: Int in 0..<16 {
|
|
#expect(abs(out[0][i] - lastFrame) < 1e-9,
|
|
"ch0 sample \(i): expected \(lastFrame), got \(out[0][i])")
|
|
#expect(abs(out[1][i] - (lastFrame + 100.0)) < 1e-9,
|
|
"ch1 sample \(i): expected \(lastFrame + 100.0), got \(out[1][i])")
|
|
}
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func audioPositionHalfReturnsLinearMidpoint() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
// 3-frame buffer: [0.0, 1.0, 2.0]
|
|
// position=0.5 → frameF = 0.5 * 2.0 = 1.0 → exactly frame 1 = 1.0
|
|
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 3)
|
|
buf.write(0.0, frame: 0, channel: 0)
|
|
buf.write(1.0, frame: 1, channel: 0)
|
|
buf.write(2.0, frame: 2, channel: 0)
|
|
|
|
let reader: BufferRead.Audio = BufferRead.Audio(buffer: buf, position: 0.5, interpolation: .linear)
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 1)
|
|
|
|
#expect(abs(out[0][0] - 1.0) < 1e-9,
|
|
"expected midpoint 1.0, got \(out[0][0])")
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func audioLinearInterpolationBetweenFrames() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
// 2-frame buffer: [0.0, 10.0]
|
|
// position=0.25 → frameF = 0.25 * 1.0 = 0.25 → lerp(0, 10, 0.25) = 2.5
|
|
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 2)
|
|
buf.write(0.0, frame: 0, channel: 0)
|
|
buf.write(10.0, frame: 1, channel: 0)
|
|
|
|
let reader: BufferRead.Audio = BufferRead.Audio(buffer: buf, position: 0.25, interpolation: .linear)
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 1)
|
|
|
|
#expect(abs(out[0][0] - 2.5) < 1e-9,
|
|
"expected 2.5, got \(out[0][0])")
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func audioStereoBothChannelsReadAtSamePosition() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let frames: Int = 16
|
|
let buf: LyrebirdBuffer = makeRampBuffer(frames: frames, channelCount: 2)
|
|
// position=0.5 → frameF = 0.5 * 15 = 7.5 → lerp(7, 8, 0.5) = 7.5 for ch0
|
|
// → lerp(107, 108, 0.5) = 107.5 for ch1
|
|
let reader: BufferRead.Audio = BufferRead.Audio(buffer: buf, position: 0.5, interpolation: .linear)
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 1)
|
|
|
|
#expect(abs(out[0][0] - 7.5) < 1e-9,
|
|
"ch0 expected 7.5, got \(out[0][0])")
|
|
#expect(abs(out[1][0] - 107.5) < 1e-9,
|
|
"ch1 expected 107.5, got \(out[1][0])")
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func audioInvalidBufferOutputsZero() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let buf: LyrebirdBuffer = makeRampBuffer(frames: 16)
|
|
buf.invalidate()
|
|
|
|
let reader: BufferRead.Audio = BufferRead.Audio(buffer: buf, position: 0.5)
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 16)
|
|
|
|
for i: Int in 0..<16 {
|
|
#expect(abs(out[0][i]) < 1e-9,
|
|
"sample \(i): expected 0, got \(out[0][i])")
|
|
}
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func audioMulAddApplied() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
// buffer value at position=0.0 is 0.0 → (0.0 * 2.0) + 1.0 = 1.0
|
|
let buf: LyrebirdBuffer = makeRampBuffer(frames: 8)
|
|
let reader: BufferRead.Audio = BufferRead.Audio(buffer: buf, position: 0.0,
|
|
interpolation: .none, mul: 2.0, add: 1.0)
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 1)
|
|
|
|
#expect(abs(out[0][0] - 1.0) < 1e-9,
|
|
"expected (0*2)+1=1.0, got \(out[0][0])")
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Control variant
|
|
|
|
@Test func controlPositionZeroReturnsFirstFrame() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32, channelCount: 2)
|
|
let reader: BufferRead.Control = BufferRead.Control(buffer: buf, position: 0.0, interpolation: .none)
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 16)
|
|
|
|
// Control ramps from 0→target. Initial _rampTargets = [0,0], target = [0, 100].
|
|
// ch0 ramps 0→0, ch1 ramps 0→100. Last sample of ch1 should be 100.
|
|
#expect(abs(out[0][15] - 0.0) < 1e-9,
|
|
"ch0 last sample: expected 0.0, got \(out[0][15])")
|
|
#expect(abs(out[1][15] - 100.0) < 1e-9,
|
|
"ch1 last sample: expected 100.0, got \(out[1][15])")
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func controlMatchesAudioAtSamePosition() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
// 10-frame mono buffer filled with frame index values.
|
|
let buf: LyrebirdBuffer = makeRampBuffer(frames: 10)
|
|
let pos: Double = 0.5 // frameF = 0.5 * 9 = 4.5 → lerp(4, 5, 0.5) = 4.5
|
|
|
|
let audioReader: BufferRead.Audio = BufferRead.Audio(buffer: buf, position: pos, interpolation: .linear)
|
|
let controlReader: BufferRead.Control = BufferRead.Control(buffer: buf, position: pos, interpolation: .linear)
|
|
|
|
let audioOut: [[Double]] = audioReader.calculatedSamples(graph: graph, count: 1)
|
|
let controlOut: [[Double]] = controlReader.calculatedSamples(graph: graph, count: 1)
|
|
|
|
// Audio gives exact result; Control last sample = target = same value.
|
|
#expect(abs(audioOut[0][0] - 4.5) < 1e-9,
|
|
"audio: expected 4.5, got \(audioOut[0][0])")
|
|
#expect(abs(controlOut[0][0] - 4.5) < 1e-9,
|
|
"control: expected 4.5, got \(controlOut[0][0])")
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func controlInvalidBufferOutputsZero() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let buf: LyrebirdBuffer = makeRampBuffer(frames: 16)
|
|
buf.invalidate()
|
|
|
|
let reader: BufferRead.Control = BufferRead.Control(buffer: buf, position: 0.5)
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 16)
|
|
|
|
for i: Int in 0..<16 {
|
|
#expect(abs(out[0][i]) < 1e-9,
|
|
"sample \(i): expected 0, got \(out[0][i])")
|
|
}
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func controlMulAddApplied() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
// buffer value at position=1.0 is 9.0 for a 10-frame buffer.
|
|
// _rampTargets starts at 0, ramps to 9.0 over 16 samples.
|
|
// last sample = (9.0 * 2.0) + 1.0 = 19.0
|
|
let buf: LyrebirdBuffer = makeRampBuffer(frames: 10)
|
|
let reader: BufferRead.Control = BufferRead.Control(buffer: buf, position: 1.0,
|
|
interpolation: .none, mul: 2.0, add: 1.0)
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 16)
|
|
|
|
#expect(abs(out[0][15] - 19.0) < 1e-9,
|
|
"expected (9*2)+1=19.0 at last sample, got \(out[0][15])")
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Generator-input position (exercises _position.calculatedSamples / floatValue paths)
|
|
|
|
@Test func audioPositionDrivenByGenerator() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
// 5-frame mono buffer: [0, 10, 20, 30, 40]
|
|
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 5)
|
|
for f: Int in 0..<5 { buf.write(Double(f) * 10.0, frame: f, channel: 0) }
|
|
|
|
// Closure drives position: sample 0 → 0.0, sample 1 → 0.5, sample 2 → 1.0
|
|
var sampleIndex: Int = 0
|
|
let positions: [Double] = [0.0, 0.5, 1.0]
|
|
let posGen: Closure = Closure { _, _ in
|
|
let v: Double = sampleIndex < positions.count ? positions[sampleIndex] : 0.0
|
|
sampleIndex += 1
|
|
return v
|
|
}
|
|
|
|
let reader: BufferRead.Audio = BufferRead.Audio(buffer: buf, position: posGen, interpolation: .linear)
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 3)
|
|
|
|
// pos=0.0 → frameF=0.0 → 0.0
|
|
#expect(abs(out[0][0] - 0.0) < 1e-9, "pos=0.0: expected 0.0, got \(out[0][0])")
|
|
// pos=0.5 → frameF=2.0 → exactly frame 2 = 20.0
|
|
#expect(abs(out[0][1] - 20.0) < 1e-9, "pos=0.5: expected 20.0, got \(out[0][1])")
|
|
// pos=1.0 → frameF=4.0 → frame 4 = 40.0
|
|
#expect(abs(out[0][2] - 40.0) < 1e-9, "pos=1.0: expected 40.0, got \(out[0][2])")
|
|
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func controlPositionDrivenByGenerator() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
// 5-frame mono buffer: [0, 10, 20, 30, 40]
|
|
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 5)
|
|
for f: Int in 0..<5 { buf.write(Double(f) * 10.0, frame: f, channel: 0) }
|
|
|
|
// Closure returns 0.75: frameF = 0.75 * 4 = 3.0 → frame 3 = 30.0
|
|
let posGen: Closure = Closure { _, _ in 0.75 }
|
|
let reader: BufferRead.Control = BufferRead.Control(buffer: buf, position: posGen, interpolation: .none)
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 1)
|
|
|
|
// Ramps from 0 → 30.0; count=1 so last sample = target = 30.0
|
|
#expect(abs(out[0][0] - 30.0) < 1e-9, "expected 30.0, got \(out[0][0])")
|
|
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Rate checks
|
|
|
|
@Test func audioRateIsAudio() {
|
|
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 16)
|
|
let reader: BufferRead.Audio = BufferRead.Audio(buffer: buf)
|
|
#expect(reader.rate == .audio)
|
|
}
|
|
|
|
@Test func controlRateIsControl() {
|
|
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 16)
|
|
let reader: BufferRead.Control = BufferRead.Control(buffer: buf)
|
|
#expect(reader.rate == .control)
|
|
}
|
|
|
|
// MARK: - Live buffer swap
|
|
|
|
/// Drain freeQueue synchronously so swapped-out buffer refs can be released
|
|
/// without waiting on the 5ms background timer.
|
|
private func drainFreeQueue(_ engine: SynthesisEngine) {
|
|
// Drain both the engine's free queue (for synth/group releases) and the
|
|
// RTBufferSlot kill queue (for displaced buffer releases).
|
|
while let ref: Unmanaged<AnyObject> = engine.freeQueue.dequeue() {
|
|
ref.release()
|
|
}
|
|
_ = engine.drainBufferSwapKillQueue()
|
|
}
|
|
|
|
@Test func audioSetBufferReadsFromNewBuffer() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let first: LyrebirdBuffer = makeRampBuffer(frames: 32, channelCount: 1) // value == frame index
|
|
let second: LyrebirdBuffer = LyrebirdBuffer(count: 32, channelCount: 1)
|
|
second.fill(with: 42.0)
|
|
|
|
let reader: BufferRead.Audio = BufferRead.Audio(buffer: first, position: 0.5, interpolation: .none)
|
|
|
|
let before: [[Double]] = reader.calculatedSamples(graph: graph, count: 8)
|
|
// first buffer at position 0.5 = frame 15 (index 15 in 32-frame ramp) → 15.0
|
|
for i: Int in 0..<8 {
|
|
#expect(abs(before[0][i] - 15.0) < 1e-9, "before-swap sample \(i): expected 15.0, got \(before[0][i])")
|
|
}
|
|
|
|
reader.setBuffer(second, engine: engine)
|
|
advanceBlock(engine)
|
|
|
|
let after: [[Double]] = reader.calculatedSamples(graph: graph, count: 8)
|
|
for i: Int in 0..<8 {
|
|
#expect(abs(after[0][i] - 42.0) < 1e-9, "after-swap sample \(i): expected 42.0, got \(after[0][i])")
|
|
}
|
|
|
|
engine.stop()
|
|
drainFreeQueue(engine)
|
|
}
|
|
|
|
@Test func audioSetBufferToNilOutputsSilence() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32, channelCount: 1)
|
|
let reader: BufferRead.Audio = BufferRead.Audio(buffer: buf, position: 0.5, interpolation: .none)
|
|
|
|
reader.setBuffer(nil, engine: engine)
|
|
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 8)
|
|
for i: Int in 0..<8 {
|
|
#expect(out[0][i] == 0.0, "expected silence at sample \(i), got \(out[0][i])")
|
|
}
|
|
|
|
engine.stop()
|
|
drainFreeQueue(engine)
|
|
}
|
|
|
|
@Test func audioSetBufferEnqueuesPreviousBufferRef() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
weak var weakRef: LyrebirdBuffer?
|
|
let reader: BufferRead.Audio
|
|
do {
|
|
let first: LyrebirdBuffer = LyrebirdBuffer(count: 16, channelCount: 1)
|
|
weakRef = first
|
|
reader = BufferRead.Audio(buffer: first)
|
|
}
|
|
#expect(weakRef != nil, "Reader's slot must keep the initial buffer alive")
|
|
|
|
reader.setBuffer(nil, engine: engine)
|
|
// Trigger a compute pass so the slot drains pending and pushes the displaced
|
|
// buffer to engine.bufferSwapKillQueue.
|
|
_ = reader.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 1)
|
|
#expect(weakRef != nil, "Buffer enqueued but not yet released")
|
|
drainFreeQueue(engine)
|
|
#expect(weakRef == nil, "Buffer should be deallocated after freeQueue drain")
|
|
_ = reader
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func controlSetBufferReadsFromNewBuffer() throws {
|
|
let engine: SynthesisEngine = try makeEngine(blockSize: 4)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let first: LyrebirdBuffer = makeRampBuffer(frames: 32, channelCount: 1)
|
|
let second: LyrebirdBuffer = LyrebirdBuffer(count: 32, channelCount: 1)
|
|
second.fill(with: 7.5)
|
|
|
|
let reader: BufferRead.Control = BufferRead.Control(buffer: first, position: 1.0, interpolation: .none)
|
|
|
|
// Warm the ramp so previousOutputValue tracks the first buffer.
|
|
_ = reader.calculatedSamples(graph: graph, count: 4)
|
|
advanceBlock(engine)
|
|
_ = reader.calculatedSamples(graph: graph, count: 4)
|
|
advanceBlock(engine)
|
|
|
|
reader.setBuffer(second, engine: engine)
|
|
|
|
// After the swap, target becomes 7.5; with count=4, last sample = 7.5.
|
|
let after: [[Double]] = reader.calculatedSamples(graph: graph, count: 4)
|
|
#expect(abs(after[0][3] - 7.5) < 1e-9, "last-block sample should reach 7.5, got \(after[0][3])")
|
|
|
|
engine.stop()
|
|
drainFreeQueue(engine)
|
|
}
|
|
|
|
@Test func controlSetBufferToNilOutputsSilence() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32, channelCount: 1)
|
|
let reader: BufferRead.Control = BufferRead.Control(buffer: buf, position: 1.0)
|
|
|
|
reader.setBuffer(nil, engine: engine)
|
|
|
|
let out: [[Double]] = reader.calculatedSamples(graph: graph, count: 4)
|
|
for i: Int in 0..<4 {
|
|
#expect(out[0][i] == 0.0, "expected silence at sample \(i), got \(out[0][i])")
|
|
}
|
|
#expect(reader.previousOutputValue == 0.0)
|
|
|
|
engine.stop()
|
|
drainFreeQueue(engine)
|
|
}
|
|
}
|