192 lines
7.3 KiB
Swift
192 lines
7.3 KiB
Swift
// LineTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("Line")
|
|
struct LineTests {
|
|
|
|
// sampleRate == blockSize → 1 audio block per second of duration. Makes durations
|
|
// expressible in whole blocks for clean assertions on done dispatch.
|
|
private func makeEngine() -> SynthesisEngine {
|
|
makeTestEngine(blockSize: 64, sampleRate: 64)
|
|
}
|
|
|
|
// MARK: - Rate / channel count
|
|
|
|
@Test func rateIsControl() {
|
|
let gen: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 1.0)
|
|
#expect(gen.rate == .control)
|
|
#expect(gen.outputChannelCount == 1)
|
|
}
|
|
|
|
// MARK: - Linear interpolation reaches end at duration
|
|
|
|
@Test func reachesEndAtDuration() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
try engine.start()
|
|
let line: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 4.0)
|
|
let samples: [Double] = collectBlocks(line, engine: engine, blocks: 4)
|
|
let lastSample: Double = samples.last ?? 0.0
|
|
#expect(abs(lastSample - 1.0) < 1e-9)
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Monotonic increasing for ascending ramp
|
|
|
|
@Test func monotonicIncreasing() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
try engine.start()
|
|
let line: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 4.0)
|
|
let samples: [Double] = collectBlocks(line, engine: engine, blocks: 4)
|
|
for i: Int in 1..<samples.count {
|
|
#expect(samples[i] >= samples[i - 1] - 1e-12,
|
|
"non-monotonic at i=\(i): \(samples[i-1]) → \(samples[i])")
|
|
}
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Negative ramp
|
|
|
|
@Test func negativeRampDescends() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
try engine.start()
|
|
let line: Line.Control = Line.Control(start: 1.0, end: -1.0, dur: 4.0)
|
|
let samples: [Double] = collectBlocks(line, engine: engine, blocks: 4)
|
|
let lastSample: Double = samples.last ?? 0.0
|
|
#expect(abs(lastSample - (-1.0)) < 1e-9)
|
|
for i: Int in 1..<samples.count {
|
|
#expect(samples[i] <= samples[i - 1] + 1e-12)
|
|
}
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Done action: .nothing freezes at end
|
|
|
|
@Test func doneActionNothingFreezesAtEnd() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
try engine.start()
|
|
let line: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 2.0, doneAction: .nothing)
|
|
// Run past completion (4 blocks, dur is 2 blocks).
|
|
let samples: [Double] = collectBlocks(line, engine: engine, blocks: 4)
|
|
// Last 64 samples should all be 1.0.
|
|
for i: Int in (samples.count - 64)..<samples.count {
|
|
#expect(abs(samples[i] - 1.0) < 1e-9)
|
|
}
|
|
#expect(line.needsFree == false)
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Done action: .freeNode sets needsFree
|
|
|
|
@Test func doneActionFreeNodeSetsNeedsFree() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
try engine.start()
|
|
let line: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 2.0, doneAction: .freeNode)
|
|
// After 2 blocks (1 dur worth), completion should fire.
|
|
_ = collectBlocks(line, engine: engine, blocks: 3)
|
|
#expect(line.needsFree == true)
|
|
engine.stop()
|
|
}
|
|
|
|
@Test func doneActionFreeNodeNotSetBeforeCompletion() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
try engine.start()
|
|
let line: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 4.0, doneAction: .freeNode)
|
|
_ = collectBlocks(line, engine: engine, blocks: 2) // halfway
|
|
#expect(line.needsFree == false)
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Done action: .loop restarts
|
|
|
|
@Test func doneActionLoopRestarts() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
try engine.start()
|
|
let line: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 2.0, doneAction: .loop)
|
|
|
|
// Block 1: 0 → 0.5
|
|
// Block 2: 0.5 → 1.0 (completes; handleDone resets _pos and _rampTarget)
|
|
// Block 3: 0 → 0.5 (loop restart)
|
|
// Block 4: 0.5 → 1.0 (completes again)
|
|
let samples: [Double] = collectBlocks(line, engine: engine, blocks: 4)
|
|
let block2End: Double = samples[127] // last sample of block 2
|
|
let block3End: Double = samples[191] // last sample of block 3
|
|
let block4End: Double = samples[255] // last sample of block 4
|
|
#expect(abs(block2End - 1.0) < 1e-9)
|
|
#expect(abs(block3End - 0.5) < 1e-9)
|
|
#expect(abs(block4End - 1.0) < 1e-9)
|
|
|
|
// After completion .loop never sets needsFree.
|
|
#expect(line.needsFree == false)
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Zero duration — completes immediately
|
|
|
|
@Test func zeroDurationCompletesImmediately() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
try engine.start()
|
|
let line: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 0.0, doneAction: .freeNode)
|
|
_ = collectBlocks(line, engine: engine, blocks: 1)
|
|
#expect(line.needsFree == true)
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - mul / add
|
|
|
|
@Test func mulAddScalesOutput() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
try engine.start()
|
|
let plain: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 4.0)
|
|
let scaled: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 4.0, mul: 2.0, add: 1.0)
|
|
let p: [Double] = collectBlocks(plain, engine: engine, blocks: 4)
|
|
let s: [Double] = collectBlocks(scaled, engine: engine, blocks: 4)
|
|
for i: Int in 0..<p.count {
|
|
#expect(abs(s[i] - (p[i] * 2.0 + 1.0)) < 1e-9)
|
|
}
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Fan-out cache
|
|
|
|
@Test func fanOutReturnsCachedResult() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
try engine.start()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let line: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 4.0)
|
|
let out1: [Double] = Array(line.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
|
let out2: [Double] = Array(line.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
|
#expect(out1 == out2)
|
|
engine.stop()
|
|
}
|
|
|
|
// MARK: - Sub-block bypass: state advances exactly once per audio block
|
|
|
|
@Test func stateAdvancesOncePerAudioBlock() throws {
|
|
// controlBlockSize < blockSize so the sub-block path would normally fire — the
|
|
// calculatedSamples override must bypass it for block-rate state correctness.
|
|
let engine: SynthesisEngine = SynthesisEngine(
|
|
blockSize: 64,
|
|
controlBlockSize: 16,
|
|
sampleRate: 64,
|
|
numOutputChannels: 2,
|
|
numInputChannels: 2,
|
|
numAudioBuses: 16,
|
|
numControlBuses: 16,
|
|
eventQueueCapacity: lyrebirdDefaultEventQueueCapacity,
|
|
driver: OfflineDriver()
|
|
)
|
|
try engine.start()
|
|
let line: Line.Control = Line.Control(start: 0.0, end: 1.0, dur: 4.0)
|
|
let samples: [Double] = collectBlocks(line, engine: engine, blocks: 4)
|
|
let lastSample: Double = samples.last ?? 0.0
|
|
#expect(abs(lastSample - 1.0) < 1e-9)
|
|
engine.stop()
|
|
}
|
|
}
|