165 lines
6.5 KiB
Swift
165 lines
6.5 KiB
Swift
// ExponentialLineTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@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()
|
|
}
|
|
}
|