833 lines
35 KiB
Swift
833 lines
35 KiB
Swift
// PlayBufferTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("PlayBuffer")
|
||
struct PlayBufferTests {
|
||
|
||
// 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 for easy verification.
|
||
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: - Basic playback
|
||
|
||
@Test func processingRateIsAudio() {
|
||
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 16)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf)
|
||
#expect(player.rate == .audio)
|
||
}
|
||
|
||
@Test func rateOneReadsSequentialFrames() throws {
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 16)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf, loop: false, interpolation: .none)
|
||
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 16)
|
||
|
||
for i: Int in 0..<16 {
|
||
#expect(abs(out[0][i] - Double(i)) < 1e-9,
|
||
"sample \(i): expected \(i), got \(out[0][i])")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func rateTwoSkipsAlternateFrames() throws {
|
||
// rate: 2.0 at init — constant, no ramp — phase advances 2 frames per sample.
|
||
// With .none interpolation: samples read frames 0, 2, 4, 6, 8, 10, 12, 14.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: false, interpolation: .none, rate: 2.0
|
||
)
|
||
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
|
||
// Output should be strictly monotonically increasing at 2× normal rate.
|
||
for i: Int in 1..<8 {
|
||
#expect(out[0][i] > out[0][i - 1], "samples should increase at rate > 1")
|
||
}
|
||
// First sample reads frame 0; second reads frame 2 (constant rate = 2).
|
||
#expect(abs(out[0][0] - 0.0) < 1e-9, "first sample should be frame 0")
|
||
#expect(abs(out[0][1] - 2.0) < 1e-9, "second sample should be frame 2 at rate=2")
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Loop behaviour
|
||
|
||
@Test func loopWrapsPlayhead() throws {
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 16)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 8)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf, loop: true, interpolation: .none)
|
||
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 16)
|
||
|
||
// Frames 0–7 then 0–7 again
|
||
let expected: [Double] = [0,1,2,3,4,5,6,7, 0,1,2,3,4,5,6,7]
|
||
for i: Int in 0..<16 {
|
||
#expect(abs(out[0][i] - expected[i]) < 1e-9,
|
||
"sample \(i): expected \(expected[i]), got \(out[0][i])")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func noLoopOutputsSilenceAfterEnd() throws {
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 16)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 4)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf, loop: false, interpolation: .none)
|
||
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 16)
|
||
|
||
// First 4 samples play back the buffer, then silence.
|
||
for i: Int in 4..<16 {
|
||
#expect(out[0][i] == 0.0, "sample \(i) after end should be 0")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - loopLength narrowing
|
||
|
||
@Test func narrowedLoopLengthWrapsWithinBoundNotFullBuffer() throws {
|
||
// 32-frame buffer, loopLength narrowed to 8 — loop:true should wrap every
|
||
// 8 frames instead of every 32.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 16)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32)
|
||
let narrowedLength: Int = 8
|
||
buf.setLoopLength(narrowedLength)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf, loop: true, interpolation: .none)
|
||
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 16)
|
||
|
||
// Frames 0–7 then 0–7 again (loopLength == 8), not 0–15 (full buffer.count).
|
||
let expected: [Double] = [0,1,2,3,4,5,6,7, 0,1,2,3,4,5,6,7]
|
||
for i: Int in 0..<16 {
|
||
#expect(abs(out[0][i] - expected[i]) < 1e-9,
|
||
"sample \(i): expected \(expected[i]), got \(out[0][i])")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func narrowedLoopLengthClampsWithoutLoop() throws {
|
||
// 32-frame buffer, loopLength narrowed to 8, loop:false — non-interpolated reads
|
||
// must clamp to loopLength - 1 (frame 7), not buffer.count - 1 (frame 31).
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32)
|
||
let narrowedLength: Int = 8
|
||
buf.setLoopLength(narrowedLength)
|
||
|
||
let result: Double = lyrebirdBufferRead(
|
||
buf, channel: 0, phase: 100.0, interpolation: .none, loop: false
|
||
)
|
||
#expect(abs(result - Double(narrowedLength - 1)) < 1e-9,
|
||
"clamped read should hold at loopLength-1 (\(narrowedLength - 1)), got \(result)")
|
||
}
|
||
|
||
// MARK: - doneAction
|
||
|
||
@Test func doneActionFreeNodeSetsNeedsFree() throws {
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 16)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 4)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf, loop: false,
|
||
interpolation: .none, doneAction: .freeNode)
|
||
|
||
_ = player.calculatedSamples(graph: graph, count: 16)
|
||
#expect(player.needsFree)
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func doneActionNothingDoesNotSetNeedsFree() throws {
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 16)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 4)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf, loop: false,
|
||
interpolation: .none, doneAction: .nothing)
|
||
|
||
_ = player.calculatedSamples(graph: graph, count: 16)
|
||
#expect(!player.needsFree)
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func doneActionLoopResetsPlayhead() throws {
|
||
// doneAction: .loop resets _phase to 0 and clears _isDone without requiring a trigger.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 16)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 8)
|
||
// loop: false so _phase >= bufferFrames fires handleEndOfBuffer;
|
||
// doneAction: .loop resets phase and continues playing.
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf, loop: false,
|
||
interpolation: .none, doneAction: .loop)
|
||
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 16)
|
||
|
||
// Frames 0–7 then 0–7 again (same as loop: true).
|
||
let expected: [Double] = [0,1,2,3,4,5,6,7, 0,1,2,3,4,5,6,7]
|
||
for i: Int in 0..<16 {
|
||
#expect(abs(out[0][i] - expected[i]) < 1e-9,
|
||
"sample \(i): expected \(expected[i]), got \(out[0][i])")
|
||
}
|
||
#expect(!player.needsFree)
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Trigger (scalar at init)
|
||
|
||
@Test func triggerScalarResetsPlayhead() throws {
|
||
// Block 1: no trigger (0.0) — advance 8 frames.
|
||
// Block 2: trigger=1.0 constant at init; play head resets at the very first sample
|
||
// (scalar trigger is > 0 on sample 0, edge fires immediately).
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32)
|
||
|
||
// Block 1: advance play head 8 frames (trigger = 0.0).
|
||
let player1: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf, loop: false, interpolation: .none)
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = player1.calculatedSamples(graph: graph, count: 8)
|
||
}
|
||
advanceBlock(engine)
|
||
// Phase is now at 8.
|
||
|
||
// Block 2: new player starting at frame 8 with trigger=1.0 — resets to 0 on sample 0.
|
||
// We verify the reset by confirming the first output sample reads from frame 0.
|
||
let player2: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: false, interpolation: .none,
|
||
startPos: 8.0, trigger: 1.0
|
||
)
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player2.calculatedSamples(graph: graph, count: 8)
|
||
// trigger = 1.0 > 0, _prevTrigger = 0 → edge fires on sample 0 → phase resets to 0.
|
||
#expect(abs(out[0][0] - 0.0) < 1e-9, "after trigger, first sample should be frame 0")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func triggerResetsPlayhead() throws {
|
||
// Use a mutable ParameterNode so the trigger fires only in block 2.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32)
|
||
let triggerNode: ParameterNode = ParameterNode(0.0)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: false, interpolation: .none, trigger: triggerNode
|
||
)
|
||
|
||
// Block 1: advance play head 8 frames (triggerNode = 0.0).
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = player.calculatedSamples(graph: graph, count: 8)
|
||
}
|
||
advanceBlock(engine)
|
||
|
||
// Block 2: set trigger to 1.0 → edge fires → play head resets near 0.
|
||
triggerNode.value = 1.0
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
// ParameterNode ramps from 0 to 1 over 8 samples; edge fires at the first
|
||
// sample that crosses 0 → positive. Play head resets → first samples near 0.
|
||
#expect(out[0][0] < 4.0, "after trigger play head should restart near 0")
|
||
}
|
||
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func triggerResetsIsDoneFlag() throws {
|
||
// Use a mutable ParameterNode so the trigger fires only in block 2.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 4)
|
||
let triggerNode: ParameterNode = ParameterNode(0.0)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: false, interpolation: .none,
|
||
doneAction: .nothing, trigger: triggerNode
|
||
)
|
||
|
||
// Block 1: exhaust the buffer.
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = player.calculatedSamples(graph: graph, count: 8)
|
||
}
|
||
advanceBlock(engine)
|
||
|
||
#expect(!player.needsFree) // doneAction = .nothing
|
||
|
||
// Block 2: trigger should restart playback.
|
||
triggerNode.value = 1.0
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
// After trigger, some samples should be non-zero (playing buffer).
|
||
#expect(out[0].prefix(8).contains { $0 != 0.0 })
|
||
}
|
||
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Interpolation
|
||
|
||
@Test func linearInterpolationProducesFractionalValues() throws {
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 4)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
// Buffer: [0, 1, 2, 3]
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 4)
|
||
// rate=0.5: each sample advances phase by 0.5 → fractional reads.
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: false, interpolation: .linear, rate: 0.5
|
||
)
|
||
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 4)
|
||
for i: Int in 0..<4 {
|
||
#expect(out[0][i] >= 0.0 && out[0][i] <= 3.0)
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Multi-channel
|
||
|
||
@Test func stereoBufferProducesTwoChannels() throws {
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 8, channelCount: 2)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf, loop: false, interpolation: .none)
|
||
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
#expect(out.count == 2, "stereo buffer should produce 2 output channels")
|
||
|
||
for i: Int in 0..<8 {
|
||
#expect(abs(out[0][i] - Double(i)) < 1e-9, "L channel frame \(i)")
|
||
#expect(abs(out[1][i] - (Double(i) + 100.0)) < 1e-9, "R channel frame \(i)")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Fan-out cache
|
||
|
||
@Test func fanOutCacheReturnsSameResultWithinSegment() throws {
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf)
|
||
|
||
let a: [[Double]] = player.calculatedSamples(graph: graph, count: 16)
|
||
let b: [[Double]] = player.calculatedSamples(graph: graph, count: 16)
|
||
#expect(a[0] == b[0])
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func phaseAdvancesAcrossBlocks() throws {
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 64)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf, loop: false, interpolation: .none)
|
||
|
||
let block1: [Double]
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
block1 = player.calculatedSamples(graph: graph, count: 8)[0]
|
||
}
|
||
advanceBlock(engine)
|
||
let block2: [Double]
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
block2 = player.calculatedSamples(graph: graph, count: 8)[0]
|
||
}
|
||
|
||
// Block 2 should start where block 1 left off.
|
||
#expect(block2[0] > block1[7],
|
||
"block 2 start (\(block2[0])) should be after block 1 end (\(block1[7]))")
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Invalid buffer
|
||
|
||
@Test func invalidBufferOutputsSilence() throws {
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(buffer: buf)
|
||
buf.invalidate()
|
||
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 16)
|
||
for i: Int in 0..<16 {
|
||
#expect(out[0][i] == 0.0)
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - PlayBuffer.Control
|
||
|
||
@Test func controlProcessingRateIsControl() {
|
||
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 16)
|
||
let player: PlayBuffer.Control = PlayBuffer.Control(buffer: buf)
|
||
#expect(player.rate == .control)
|
||
}
|
||
|
||
@Test func controlAdvancesOneFramePerBlock() throws {
|
||
// Each calculatedSamples call reads exactly one frame and advances phase by 1.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 4)
|
||
let player: PlayBuffer.Control = PlayBuffer.Control(buffer: buf, loop: false, interpolation: .none)
|
||
|
||
var lastValues: [Double] = []
|
||
for blockIdx: Int in 0..<4 {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
lastValues.append(out[0][7]) // settled (last) sample of each block
|
||
if blockIdx < 3 { advanceBlock(engine) }
|
||
}
|
||
|
||
// Settled output for blocks 0–3 should read frames 0–3 of the ramp buffer.
|
||
for i: Int in 0..<4 {
|
||
#expect(abs(lastValues[i] - Double(i)) < 1e-9,
|
||
"block \(i): expected frame \(i) (\(Double(i))), got \(lastValues[i])")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlLoopWrapsPhase() throws {
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 2)
|
||
let player: PlayBuffer.Control = PlayBuffer.Control(buffer: buf, loop: true, interpolation: .none)
|
||
|
||
var settled: [Double] = []
|
||
for blockIdx: Int in 0..<4 {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
settled.append(out[0][7])
|
||
if blockIdx < 3 { advanceBlock(engine) }
|
||
}
|
||
|
||
// Frames: 0, 1, 0, 1 (wraps at end of 2-frame buffer).
|
||
let expected: [Double] = [0, 1, 0, 1]
|
||
for i: Int in 0..<4 {
|
||
#expect(abs(settled[i] - expected[i]) < 1e-9,
|
||
"block \(i): expected \(expected[i]), got \(settled[i])")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlDoneActionFreeNodeSetsNeedsFree() throws {
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
// 1-frame buffer: first block reads it and fires handleEndOfBuffer.
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 1)
|
||
let player: PlayBuffer.Control = PlayBuffer.Control(buffer: buf, loop: false, doneAction: .freeNode)
|
||
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = player.calculatedSamples(graph: graph, count: 8)
|
||
|
||
#expect(player.needsFree)
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlTriggerResetsPhase() throws {
|
||
// Use a mutable ParameterNode so the trigger fires only in block 2.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 8)
|
||
let triggerNode: ParameterNode = ParameterNode(0.0)
|
||
let player: PlayBuffer.Control = PlayBuffer.Control(
|
||
buffer: buf, loop: false, interpolation: .none, trigger: triggerNode
|
||
)
|
||
|
||
// Block 1: advance phase by 1 (reads frame 0, phase → 1).
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = player.calculatedSamples(graph: graph, count: 8)
|
||
}
|
||
advanceBlock(engine)
|
||
|
||
// Block 2: fire trigger — phase should reset to 0.
|
||
triggerNode.value = 1.0
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
// After reset the settled output should be frame 0 = 0.0 (not frame 1 = 1.0).
|
||
#expect(out[0][7] < 1.0, "trigger should reset play head near frame 0")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlOutputRampsFromPreviousToCurrentValue() throws {
|
||
// First block: ramp from 0 (initial previousOutputValue) to frame-0 value (0.0).
|
||
// Second block: reads frame 1 (= 1.0), ramps from 0.0 → 1.0.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 8)
|
||
let player: PlayBuffer.Control = PlayBuffer.Control(buffer: buf, loop: false, interpolation: .none)
|
||
|
||
// Block 1: reads frame 0 (value = 0.0); ramp from previousOutputValue=0 to 0 → flat.
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
for i: Int in 0..<8 {
|
||
#expect(abs(out[0][i]) < 1e-9, "block1 sample \(i) should be 0")
|
||
}
|
||
}
|
||
advanceBlock(engine)
|
||
|
||
// Block 2: reads frame 1 (value = 1.0); ramps from 0.0 → 1.0.
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
for i: Int in 0..<8 {
|
||
let expected: Double = Double(i + 1) / 8.0
|
||
#expect(abs(out[0][i] - expected) < 1e-9,
|
||
"block2 sample \(i): expected \(expected), got \(out[0][i])")
|
||
}
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Reverse playback
|
||
|
||
@Test func reverseNoLoopFiresDone() throws {
|
||
// rate: -1.0 at init — constant, no ramp — phase immediately decreases.
|
||
// After 1 sample: phase = 0 + (-1) = -1 → fires done on first sample.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 32)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: false, interpolation: .none,
|
||
doneAction: .freeNode, startPos: 0.0, rate: -1.0
|
||
)
|
||
|
||
_ = player.calculatedSamples(graph: graph, count: 8)
|
||
#expect(player.needsFree, "reverse no-loop should fire done after phase < 0")
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func reverseLoopWrapsPhaseToEnd() throws {
|
||
// rate: -1.0 at init — constant. Block 1 wraps the phase to the buffer end.
|
||
// Block 2 should read frames in strictly decreasing order.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 8)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: true, interpolation: .none, startPos: 0.0, rate: -1.0
|
||
)
|
||
|
||
// Block 1: first sample reads frame 0, phase → -1 → wraps to 7.
|
||
// Subsequent samples read 7, 6, 5, ...
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = player.calculatedSamples(graph: graph, count: 8)
|
||
}
|
||
advanceBlock(engine)
|
||
|
||
// Block 2: all frames should be strictly decreasing (or wrap from 0 → 7).
|
||
let out: [Double]
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
out = player.calculatedSamples(graph: graph, count: 8)[0]
|
||
}
|
||
|
||
for i: Int in 1..<8 {
|
||
let wrapped: Bool = (out[i] == Double(buf.count - 1) && out[i - 1] == 0)
|
||
#expect(out[i] < out[i - 1] || wrapped,
|
||
"sample \(i): expected decreasing, got \(out[i - 1])→\(out[i])")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func reverseStartPosFirstSampleReadsLastFrame() throws {
|
||
// startPos = last frame, rate: -1.0 constant — first sample reads frame 7.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 8) // buf[7] = 7.0
|
||
let lastFrame: Double = Double(buf.count - 1)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: true, interpolation: .none,
|
||
startPos: lastFrame, rate: -1.0
|
||
)
|
||
|
||
let out: [Double] = player.calculatedSamples(graph: graph, count: 8)[0]
|
||
|
||
// First sample is always read at startPos (= 7).
|
||
#expect(abs(out[0] - 7.0) < 1e-9, "first sample should read frame 7 (startPos)")
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlReverseAdvancesBackward() throws {
|
||
// startPos=2, 4-frame buffer, rate=-1.0, loop=false.
|
||
// Settled values across 4 blocks: 2.0, 1.0, 0.0, 0.0.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 4)
|
||
let player: PlayBuffer.Control = PlayBuffer.Control(
|
||
buffer: buf, loop: false, interpolation: .none,
|
||
doneAction: .nothing, startPos: 2.0, rate: -1.0
|
||
)
|
||
|
||
var settled: [Double] = []
|
||
for blockIdx: Int in 0..<4 {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
settled.append(out[0][7])
|
||
if blockIdx < 3 { advanceBlock(engine) }
|
||
}
|
||
|
||
let expected: [Double] = [2.0, 1.0, 0.0, 0.0]
|
||
for i: Int in 0..<4 {
|
||
#expect(abs(settled[i] - expected[i]) < 1e-9,
|
||
"block \(i): expected \(expected[i]), got \(settled[i])")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func reverseDoneActionLoopResetsPlayheadToEnd() throws {
|
||
// loop: false, doneAction: .loop, rate: -1.0 constant.
|
||
// When phase < 0, handleEndOfBuffer resets _phase to bufferFrames-1 (not 0).
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 8) // buf[7] = 7.0
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: false, interpolation: .none,
|
||
doneAction: .loop, startPos: 0.0, rate: -1.0
|
||
)
|
||
|
||
// Block 1: rate = -1.0 constant, first sample reads @0, phase → -1 → doneAction:.loop fires,
|
||
// resets _isDone=false and _phase=7 (bufferFrames-1).
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
#expect(!player.needsFree, "doneAction:.loop must not set needsFree")
|
||
#expect(!player._isDone, "doneAction:.loop must clear _isDone")
|
||
let _ = out // computed without panic = no infinite re-trigger loop
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlReverseDoneActionLoopResetsPlayheadToEnd() throws {
|
||
// Control variant: loop:false, doneAction:.loop, rate:-1.0, startPos:0.
|
||
// Phase hits -1 → doneAction:.loop → _phase = bufferFrames-1 = 3.
|
||
// Next block reads from frame 3, not stuck at 0.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 4) // buf[3] = 3.0
|
||
let player: PlayBuffer.Control = PlayBuffer.Control(
|
||
buffer: buf, loop: false, interpolation: .none,
|
||
doneAction: .loop, startPos: 0.0, rate: -1.0
|
||
)
|
||
|
||
// Block 1: rateVal = -1.0, reads @0 → 0.0, phase → -1.
|
||
// handleEndOfBuffer fires: _phase = 3.0 (bufferFrames-1), _isDone = false.
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = player.calculatedSamples(graph: graph, count: 8)
|
||
#expect(!player.needsFree)
|
||
#expect(!player._isDone, "doneAction:.loop must clear _isDone")
|
||
}
|
||
advanceBlock(engine)
|
||
|
||
// Block 2: reads from the reset position (frame 3 = 3.0), then phase → 2.
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
// Settled value should be 3.0 (frame 3), confirming reset to end not 0.
|
||
#expect(abs(out[0][7] - 3.0) < 1e-9,
|
||
"after doneAction:.loop reset, next block should read from frame 3 (end)")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func controlReverseLoopWrapsPhase() throws {
|
||
// 2-frame buffer, startPos=1, rate=-1.0, loop=true.
|
||
// Settled values should alternate: 1, 0, 1, 0 (wraps at phase < 0).
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 2)
|
||
let player: PlayBuffer.Control = PlayBuffer.Control(
|
||
buffer: buf, loop: true, interpolation: .none,
|
||
startPos: 1.0, rate: -1.0
|
||
)
|
||
|
||
var settled: [Double] = []
|
||
for blockIdx: Int in 0..<4 {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 8)
|
||
settled.append(out[0][7])
|
||
if blockIdx < 3 { advanceBlock(engine) }
|
||
}
|
||
|
||
// Block 0: reads @1 → 1.0, phase: 1 + (-1) = 0 (no wrap needed)
|
||
// Block 1: reads @0 → 0.0, phase: 0 + (-1) = -1 → wrap to 1.0
|
||
// Block 2: reads @1 → 1.0, phase: 1 + (-1) = 0
|
||
// Block 3: reads @0 → 0.0, phase: 0 + (-1) = -1 → wrap
|
||
let expected: [Double] = [1.0, 0.0, 1.0, 0.0]
|
||
for i: Int in 0..<4 {
|
||
#expect(abs(settled[i] - expected[i]) < 1e-9,
|
||
"block \(i): expected \(expected[i]), got \(settled[i])")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Linear/cubic interpolation with negative phase (floor fix)
|
||
|
||
@Test func linearInterpolationNegativeFractionalPhaseInterpolatesNotExtrapolates() {
|
||
// The floor bug: Int(-0.5) == 0, giving frac = -0.5 → extrapolation (out-of-range).
|
||
// With fix: Int((-0.5).rounded(.down)) == -1, giving frac = 0.5 → interpolation.
|
||
//
|
||
// Buffer [0, 1, 2, 3] with loop=true.
|
||
// At phase -0.5: intPhase=-1, frac=0.5; frame(-1)=3 (wrapped), frame(0)=0
|
||
// Expected: 3.0 + 0.5 * (0.0 − 3.0) = 1.5
|
||
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 4)
|
||
for i: Int in 0..<4 { buf.write(Double(i), frame: i, channel: 0) }
|
||
|
||
let result: Double = lyrebirdBufferRead(
|
||
buf, channel: 0, phase: -0.5, interpolation: .linear, loop: true
|
||
)
|
||
#expect(abs(result - 1.5) < 1e-6,
|
||
"linear interp at phase -0.5 should give 1.5 (not \(result))")
|
||
// Without fix the result was -0.5 — negative for an all-positive buffer.
|
||
#expect(result >= 0.0, "no extrapolation below buffer minimum")
|
||
}
|
||
|
||
@Test func cubicInterpolationNegativeFractionalPhaseInterpolatesNotExtrapolates() {
|
||
// Same floor-fix verification for the cubic arm.
|
||
// Buffer [0, 1, 2, 3] with loop=true.
|
||
// At phase -0.5: intPhase=-1, frac=0.5; result must be within [0, 3].
|
||
let buf: LyrebirdBuffer = LyrebirdBuffer(count: 4)
|
||
for i: Int in 0..<4 { buf.write(Double(i), frame: i, channel: 0) }
|
||
|
||
let result: Double = lyrebirdBufferRead(
|
||
buf, channel: 0, phase: -0.5, interpolation: .cubic, loop: true
|
||
)
|
||
// Catmull-Rom at frac=0.5 between frames 3,0,1,2 (loop wrap) — exact value may vary,
|
||
// but it must lie within buffer range for a correct floor-based intPhase.
|
||
#expect(result >= 0.0 && result <= 3.0,
|
||
"cubic interp at phase -0.5 should stay in buffer range [0,3], got \(result)")
|
||
}
|
||
|
||
@Test func reverseLinearInterpolationStaysInBufferRange() throws {
|
||
// Integration test: PlayBuffer.Audio with .linear + negative rate produces values
|
||
// within the buffer value range. Before the floor fix, a phase in (-1, 0) gave
|
||
// negative output for a non-negative buffer.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 8)
|
||
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 8) // values [0..7]
|
||
// startPos=0.5 so after the first sample (read at 0.5) phase = 0.5 - 1.0 = -0.5,
|
||
// immediately exercising the floor-fix path.
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: true, interpolation: .linear,
|
||
startPos: 0.5, rate: -1.0
|
||
)
|
||
|
||
// Block 1: all output must lie in [0.0, 7.0].
|
||
let out: [Double]
|
||
do {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
out = player.calculatedSamples(graph: graph, count: 8)[0]
|
||
}
|
||
for (i, v) in out.enumerated() {
|
||
#expect(v >= 0.0 && v <= 7.0,
|
||
"sample \(i) = \(v) out of buffer range [0, 7] — floor bug?")
|
||
}
|
||
engine.stop()
|
||
}
|
||
|
||
// MARK: - Generator-driven rate
|
||
|
||
@Test func generatorDrivenRateVariesPhasePerSample() throws {
|
||
// Wire an OscSin.Audio as the rate input. The LFO modulates rate around 1.0.
|
||
// Verify that the per-sample phase deltas vary across the block (not all equal).
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 64)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
// LFO: freq=10 Hz, range [0.5, 1.5] → rate varies enough to see delta changes.
|
||
let lfo: OscSin.Audio = OscSin.Audio(freq: 10.0, mul: 0.5, add: 1.0)
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 1024)
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: true, interpolation: .none, rate: lfo
|
||
)
|
||
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 64)
|
||
|
||
// Compute successive differences. With a modulated rate, they must not all be equal.
|
||
var deltas: [Double] = []
|
||
for i: Int in 1..<64 {
|
||
deltas.append(out[0][i] - out[0][i - 1])
|
||
}
|
||
let firstDelta: Double = deltas[0]
|
||
let allEqual: Bool = deltas.allSatisfy { abs($0 - firstDelta) < 1e-9 }
|
||
#expect(!allEqual, "generator-driven rate must produce varying per-sample phase deltas")
|
||
engine.stop()
|
||
}
|
||
|
||
@Test func generatorDrivenTriggerResetsOnImpulseEdge() throws {
|
||
// Wire an Impulse.Audio as trigger. The impulse fires once per period.
|
||
// Verify the play head resets on the impulse edge and output returns to frame 0.
|
||
let engine: SynthesisEngine = try makeEngine(blockSize: 64)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
// Impulse fires on sample 0 of every block (initPhase: 0.0).
|
||
// With freq = sampleRate/blockSize, it fires once per block.
|
||
let sampleRate: Double = Double(engine.sampleRate)
|
||
let impulseFreq: Double = sampleRate / 64.0
|
||
let imp: Impulse.Audio = Impulse.Audio(freq: impulseFreq)
|
||
|
||
// Buffer: frames 0..63 each contain their index.
|
||
let buf: LyrebirdBuffer = makeRampBuffer(frames: 64)
|
||
// startPos=32 so without trigger the first sample reads frame 32.
|
||
// With trigger on sample 0, it resets to 0.
|
||
let player: PlayBuffer.Audio = PlayBuffer.Audio(
|
||
buffer: buf, loop: false, interpolation: .none,
|
||
startPos: 32.0, trigger: imp
|
||
)
|
||
|
||
let out: [[Double]] = player.calculatedSamples(graph: graph, count: 64)
|
||
|
||
// Impulse fires on sample 0 → phase resets to 0 → first output is frame 0.
|
||
#expect(abs(out[0][0] - 0.0) < 1e-9,
|
||
"impulse on sample 0 should reset play head; expected frame 0, got \(out[0][0])")
|
||
// After reset, samples advance sequentially: 0, 1, 2, ...
|
||
#expect(abs(out[0][1] - 1.0) < 1e-9, "frame after reset should be 1")
|
||
engine.stop()
|
||
}
|
||
|
||
}
|