prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
// ExponentialLineTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("ExponentialLine")
|
||||
struct ExponentialLineTests {
|
||||
|
||||
private func makeEngine() -> SynthesisEngine {
|
||||
makeTestEngine(blockSize: 64, sampleRate: 64)
|
||||
}
|
||||
|
||||
// MARK: - Rate
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: ExponentialLine.Control = ExponentialLine.Control(start: 1.0, end: 100.0, dur: 1.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
// MARK: - Geometric progression: end-of-block ratio is constant
|
||||
|
||||
@Test func endOfBlockRatioIsConstant() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
// start=1, end=16, dur=4 → block ends are 2, 4, 8, 16. Ratio 2.0 each.
|
||||
let line: ExponentialLine.Control = ExponentialLine.Control(start: 1.0, end: 16.0, dur: 4.0)
|
||||
let samples: [Double] = collectBlocks(line, engine: engine, blocks: 4)
|
||||
let endValues: [Double] = (0..<4).map { samples[$0 * 64 + 63] }
|
||||
let r1: Double = endValues[1] / endValues[0]
|
||||
let r2: Double = endValues[2] / endValues[1]
|
||||
let r3: Double = endValues[3] / endValues[2]
|
||||
#expect(abs(r1 - 2.0) < 1e-9)
|
||||
#expect(abs(r2 - 2.0) < 1e-9)
|
||||
#expect(abs(r3 - 2.0) < 1e-9)
|
||||
#expect(abs(endValues[3] - 16.0) < 1e-9)
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Reaches end at duration
|
||||
|
||||
@Test func reachesEndAtDuration() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
let line: ExponentialLine.Control = ExponentialLine.Control(start: 0.001, 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: - Negative ramp: shares sign
|
||||
|
||||
@Test func negativeRampSharesSign() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
let line: ExponentialLine.Control = ExponentialLine.Control(start: -1.0, end: -16.0, dur: 4.0)
|
||||
let samples: [Double] = collectBlocks(line, engine: engine, blocks: 4)
|
||||
for s: Double in samples {
|
||||
#expect(s <= 0.0, "expected non-positive sample")
|
||||
}
|
||||
let lastSample: Double = samples.last ?? 0.0
|
||||
#expect(abs(lastSample - (-16.0)) < 1e-9)
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Done action — .freeNode
|
||||
|
||||
@Test func doneActionFreeNodeSetsNeedsFree() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
let line: ExponentialLine.Control = ExponentialLine.Control(start: 1.0, end: 100.0, dur: 2.0, doneAction: .freeNode)
|
||||
_ = collectBlocks(line, engine: engine, blocks: 3)
|
||||
#expect(line.needsFree == true)
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func doneActionNothingDoesNotSetNeedsFree() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
let line: ExponentialLine.Control = ExponentialLine.Control(start: 1.0, end: 100.0, dur: 2.0, doneAction: .nothing)
|
||||
let samples: [Double] = collectBlocks(line, engine: engine, blocks: 4)
|
||||
#expect(line.needsFree == false)
|
||||
// Frozen at end after completion.
|
||||
for i: Int in (samples.count - 64)..<samples.count {
|
||||
#expect(abs(samples[i] - 100.0) < 1e-9)
|
||||
}
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - mul/add
|
||||
|
||||
@Test func mulAddScalesOutput() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
let plain: ExponentialLine.Control = ExponentialLine.Control(start: 1.0, end: 16.0, dur: 4.0)
|
||||
let scaled: ExponentialLine.Control = ExponentialLine.Control(start: 1.0, end: 16.0, dur: 4.0, mul: 0.5, 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] * 0.5 + 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: ExponentialLine.Control = ExponentialLine.Control(start: 1.0, end: 100.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: - Done action: .loop restarts
|
||||
|
||||
@Test func doneActionLoopRestarts() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
// start=1, end=4, dur=2 → block-end values 2.0 (block 1) and 4.0 (block 2 = end).
|
||||
// After completion .loop resets _pos and _rampTarget=start=1.
|
||||
// Block 3: ramps from 1.0 toward block-end-of-cycle-1 = 2.0.
|
||||
let line: ExponentialLine.Control = ExponentialLine.Control(start: 1.0, end: 4.0, dur: 2.0, doneAction: .loop)
|
||||
let samples: [Double] = collectBlocks(line, engine: engine, blocks: 4)
|
||||
let block2End: Double = samples[127]
|
||||
let block3End: Double = samples[191]
|
||||
let block4End: Double = samples[255]
|
||||
#expect(abs(block2End - 4.0) < 1e-9)
|
||||
#expect(abs(block3End - 2.0) < 1e-9, "got \(block3End)")
|
||||
#expect(abs(block4End - 4.0) < 1e-9, "got \(block4End)")
|
||||
#expect(line.needsFree == false)
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Sub-block bypass: state advances exactly once per audio block
|
||||
|
||||
@Test func stateAdvancesOncePerAudioBlock() throws {
|
||||
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: ExponentialLine.Control = ExponentialLine.Control(start: 1.0, end: 16.0, dur: 4.0)
|
||||
let samples: [Double] = collectBlocks(line, engine: engine, blocks: 4)
|
||||
let lastSample: Double = samples.last ?? 0.0
|
||||
#expect(abs(lastSample - 16.0) < 1e-9, "got \(lastSample)")
|
||||
engine.stop()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
// LineTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
// RateRampTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("RateRamp")
|
||||
struct RateRampTests {
|
||||
|
||||
private func makeEngine() -> SynthesisEngine {
|
||||
makeTestEngine(blockSize: 64, sampleRate: 64)
|
||||
}
|
||||
|
||||
// MARK: - Rate
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 1.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
// MARK: - First block ramps from start (not zero)
|
||||
|
||||
@Test func firstBlockStartsAtStart() throws {
|
||||
// start=5.0, rate=1.0, no trigger. _acc seeds to 5 on first call.
|
||||
// Block 1: ramp 5 → 6. Block 2: 6 → 7. Block 3: 7 → 8.
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
let ramp: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 1.0, start: 5.0)
|
||||
let samples: [Double] = collectBlocks(ramp, engine: engine, blocks: 3)
|
||||
let endValues: [Double] = (0..<3).map { samples[$0 * 64 + 63] }
|
||||
#expect(abs(endValues[0] - 6.0) < 1e-9, "got \(endValues[0])")
|
||||
#expect(abs(endValues[1] - 7.0) < 1e-9)
|
||||
#expect(abs(endValues[2] - 8.0) < 1e-9)
|
||||
// First sample of block 1 should be near start (5 + step), well above 0.
|
||||
#expect(samples[0] > 5.0)
|
||||
#expect(samples[0] <= 6.0)
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Accumulation: rate × seconds
|
||||
|
||||
@Test func accumulatorAdvancesAtRate() throws {
|
||||
// sampleRate = blockSize = 64 → 1 block = 1 second. rate=2.0 → +2 per block.
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
let ramp: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 2.0)
|
||||
let samples: [Double] = collectBlocks(ramp, engine: engine, blocks: 4)
|
||||
// Block ends should be 2, 4, 6, 8 (accumulator value at end of each block).
|
||||
let endValues: [Double] = (0..<4).map { samples[$0 * 64 + 63] }
|
||||
#expect(abs(endValues[0] - 2.0) < 1e-9)
|
||||
#expect(abs(endValues[1] - 4.0) < 1e-9)
|
||||
#expect(abs(endValues[2] - 6.0) < 1e-9)
|
||||
#expect(abs(endValues[3] - 8.0) < 1e-9)
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Trigger edge resets accumulator to start
|
||||
|
||||
@Test func triggerResetsToStart() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
// Pre-accumulate for 3 blocks with no trigger.
|
||||
let trigger: ParameterNode = ParameterNode(0.0)
|
||||
let ramp: RateRamp.Control = RateRamp.Control(trigger: trigger, rate: 1.0, start: 0.0)
|
||||
_ = collectBlocks(ramp, engine: engine, blocks: 3) // _acc ≈ 3.0
|
||||
|
||||
// Raise trigger HIGH. The rising edge is detected from _prevTrig=0 → t=1.
|
||||
trigger.value = 1.0
|
||||
let blockAfter: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
|
||||
// After reset: _acc = start = 0, then _acc += rate * 1s = 1.0. Last sample == 1.0.
|
||||
let lastSample: Double = blockAfter.last ?? 0.0
|
||||
#expect(abs(lastSample - 1.0) < 1e-9, "expected ~1.0, got \(lastSample)")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Modulating start between triggers has no effect on output
|
||||
|
||||
@Test func startModulationOnlyAtTriggerEdge() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
let trigger: ParameterNode = ParameterNode(0.0)
|
||||
let startParam: ParameterNode = ParameterNode(0.0)
|
||||
let ramp: RateRamp.Control = RateRamp.Control(trigger: trigger, rate: 1.0, start: startParam)
|
||||
|
||||
// Block 1: no trigger, start=0; accumulator should be 1.0 at end.
|
||||
let b1: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
|
||||
#expect(abs((b1.last ?? 0.0) - 1.0) < 1e-9)
|
||||
|
||||
// Change start to 100 — should have no effect because no trigger fires.
|
||||
startParam.value = 100.0
|
||||
let b2: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
|
||||
// Still accumulating from previous _acc=1.0 → end of block = 2.0.
|
||||
#expect(abs((b2.last ?? 0.0) - 2.0) < 1e-9, "got \(b2.last ?? 0.0)")
|
||||
|
||||
// Now fire trigger: _acc resets to startParam.value=100, then +=1 → end=101.
|
||||
trigger.value = 1.0
|
||||
let b3: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
|
||||
#expect(abs((b3.last ?? 0.0) - 101.0) < 1e-9, "got \(b3.last ?? 0.0)")
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Rate modulation changes growth speed
|
||||
|
||||
@Test func rateModulationChangesGrowth() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
let rate: ParameterNode = ParameterNode(1.0)
|
||||
let ramp: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: rate)
|
||||
let b1: [Double] = collectBlocks(ramp, engine: engine, blocks: 1) // +1
|
||||
rate.value = 5.0
|
||||
let b2: [Double] = collectBlocks(ramp, engine: engine, blocks: 1) // +5
|
||||
#expect(abs((b1.last ?? 0.0) - 1.0) < 1e-9)
|
||||
#expect(abs((b2.last ?? 0.0) - 6.0) < 1e-9, "got \(b2.last ?? 0.0)")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Cross-block edge detection (high-to-low to high)
|
||||
|
||||
@Test func crossBlockEdgeDetection() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
let trigger: ParameterNode = ParameterNode(1.0)
|
||||
let ramp: RateRamp.Control = RateRamp.Control(trigger: trigger, rate: 1.0, start: 0.0)
|
||||
// Block 1: trigger goes 0 → 1 (initial edge from _prevTrig=0). Reset to 0. End=1.0.
|
||||
let b1: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
|
||||
#expect(abs((b1.last ?? 0.0) - 1.0) < 1e-9)
|
||||
|
||||
// Block 2: trigger stays 1. No new edge. _acc continues from 1.0 → 2.0.
|
||||
let b2: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
|
||||
#expect(abs((b2.last ?? 0.0) - 2.0) < 1e-9)
|
||||
|
||||
// Drop trigger low, then back high. Edge fires again.
|
||||
trigger.value = 0.0
|
||||
let b3: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
|
||||
#expect(abs((b3.last ?? 0.0) - 3.0) < 1e-9) // no edge — accumulator continues
|
||||
trigger.value = 1.0
|
||||
let b4: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
|
||||
// Edge fired → reset to start (0) + rate (1) = 1.0.
|
||||
#expect(abs((b4.last ?? 0.0) - 1.0) < 1e-9, "got \(b4.last ?? 0.0)")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - mul / add
|
||||
|
||||
@Test func mulAddScalesOutput() throws {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
try engine.start()
|
||||
let plain: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 1.0)
|
||||
let scaled: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 1.0, mul: 2.0, add: 1.0)
|
||||
let p: [Double] = collectBlocks(plain, engine: engine, blocks: 3)
|
||||
let s: [Double] = collectBlocks(scaled, engine: engine, blocks: 3)
|
||||
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 ramp: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 1.0)
|
||||
let out1: [Double] = Array(ramp.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
let out2: [Double] = Array(ramp.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 {
|
||||
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 ramp: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 2.0)
|
||||
let samples: [Double] = collectBlocks(ramp, engine: engine, blocks: 4)
|
||||
// Despite controlBlockSize=16, _acc must advance once per audio block of 64 samples.
|
||||
// After 4 blocks: _acc = 4 * 2 = 8.
|
||||
let lastSample: Double = samples.last ?? 0.0
|
||||
#expect(abs(lastSample - 8.0) < 1e-9, "got \(lastSample)")
|
||||
engine.stop()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user