prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
// BeatTrackFeatureTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("BeatTrack.Control — feature-fed (feature)")
|
||||
struct BeatTrackFeatureTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 512
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
@Test func fourOutputRows() {
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.0)
|
||||
#expect(gen.outputChannelCount == 4)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(out.count == 4)
|
||||
for row: Int in 0..<4 {
|
||||
#expect(Array(out[row].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
}
|
||||
|
||||
/// Each output row is flat across the block (block-quantised triggers / tempo).
|
||||
@Test func rowsAreFlat() {
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.5)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for row: Int in 0..<4 {
|
||||
let first: Double = out[row][0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[row][i] == first, "row \(row) not flat at \(i)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Pre-lock tempo (row 3) defaults to the preferred BPM.
|
||||
@Test func preLockTempoIsPreferred() {
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(out[3][0] - beatTrackPreferredBPM) < 1e-9)
|
||||
}
|
||||
|
||||
/// A 2 Hz impulse feature (120 BPM) drives the tempo output to ≈120.
|
||||
@Test func locksToImpulseFeature() {
|
||||
// 2 Hz impulse → beat every 0.5 s → 120 BPM.
|
||||
let feature: Impulse.Audio = Impulse.Audio(freq: 2.0)
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(feature: feature)
|
||||
var lastTempo: Double = 0.0
|
||||
// ~9 s of audio at 512-sample blocks.
|
||||
for _ in 0..<800 {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
||||
lastTempo = out[3][0]
|
||||
advanceBlock(engine)
|
||||
}
|
||||
#expect(abs(lastTempo - 120.0) < 120.0 * 0.10,
|
||||
"expected ≈120 BPM, got \(lastTempo)")
|
||||
}
|
||||
|
||||
/// The `odf:` enum init and the `feature:` convenience init are equivalent.
|
||||
@Test func enumInitMatchesConvenience() {
|
||||
let feature: Impulse.Audio = Impulse.Audio(freq: 2.0)
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(odf: .feature(feature))
|
||||
var lastTempo: Double = 0.0
|
||||
for _ in 0..<800 {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
||||
lastTempo = out[3][0]
|
||||
advanceBlock(engine)
|
||||
}
|
||||
#expect(abs(lastTempo - 120.0) < 120.0 * 0.10,
|
||||
"enum-init feature path: expected ≈120 BPM, got \(lastTempo)")
|
||||
}
|
||||
|
||||
/// Silence: no NaN/Inf in any row, tempo finite.
|
||||
@Test func silenceProducesNoNaN() {
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.0)
|
||||
for _ in 0..<200 {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
||||
for row: Int in 0..<4 {
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[row][i].isFinite, "row \(row) sample \(i) not finite")
|
||||
}
|
||||
}
|
||||
advanceBlock(engine)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func floatValueReturnsBeatRow() {
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
// BeatTrackSpectralFluxTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("BeatTrack.Control — FFT-fed (spectralFlux)")
|
||||
struct BeatTrackSpectralFluxTests {
|
||||
|
||||
let sampleRate: Double = 44100
|
||||
let fftSize: Int = 1024
|
||||
let hopSize: Int = 512
|
||||
let blockSize: Int = 512
|
||||
|
||||
private func makeEngine() throws -> SynthesisEngine {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||||
try engine.start()
|
||||
return engine
|
||||
}
|
||||
|
||||
@Test func rateIsControl() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
defer { engine.stop() }
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(0.0), fftSize: fftSize, hopSize: hopSize, analyzeOnly: true)
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(analysis: chain)
|
||||
#expect(gen.rate == .control)
|
||||
#expect(gen.outputChannelCount == 4)
|
||||
}
|
||||
|
||||
/// The `odf:` enum init and the `analysis:` convenience init are equivalent.
|
||||
@Test func enumInitMatchesConvenience() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
defer { engine.stop() }
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(0.0), fftSize: fftSize, hopSize: hopSize, analyzeOnly: true)
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(odf: .spectralFlux(analysis: chain))
|
||||
#expect(gen.rate == .control)
|
||||
#expect(gen.outputChannelCount == 4)
|
||||
}
|
||||
|
||||
/// FFT-fed path: a 2 Hz click track (120 BPM) drives spectral flux that locks
|
||||
/// the tempo near 120. Looser tolerance than the feature-fed path due to FFT
|
||||
/// time-smearing of the onset.
|
||||
@Test func locksToClickTrack() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
defer { engine.stop() }
|
||||
|
||||
// Broadband clicks at 2 Hz → spectral flux spikes at the beat rate.
|
||||
let clicks: Impulse.Audio = Impulse.Audio(freq: 2.0)
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: clicks, fftSize: fftSize, hopSize: hopSize, analyzeOnly: true)
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(analysis: chain)
|
||||
|
||||
var lastTempo: Double = 0.0
|
||||
for _ in 0..<900 {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
||||
lastTempo = out[3][0]
|
||||
advanceBlock(engine)
|
||||
}
|
||||
#expect(abs(lastTempo - 120.0) < 120.0 * 0.15,
|
||||
"expected ≈120 BPM from click track, got \(lastTempo)")
|
||||
}
|
||||
|
||||
/// Silent input: no NaN/Inf, tempo finite (holds the default).
|
||||
@Test func silenceProducesNoNaN() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
defer { engine.stop() }
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: Double(0.0), fftSize: fftSize, hopSize: hopSize, analyzeOnly: true)
|
||||
let gen: BeatTrack.Control = BeatTrack.Control(analysis: chain)
|
||||
for _ in 0..<200 {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
||||
for row: Int in 0..<4 {
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[row][i].isFinite)
|
||||
}
|
||||
}
|
||||
advanceBlock(engine)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
// BeatTrackerCoreTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@Suite("BeatTrackerCore")
|
||||
struct BeatTrackerCoreTests {
|
||||
|
||||
let sampleRate: Double = 44100
|
||||
/// Samples per feature at the default feature rate — drive one feature per `processBlock`.
|
||||
var featureHop: Int { max(1, Int((sampleRate / beatTrackFeatureRateHz).rounded())) }
|
||||
|
||||
private func makeCore(
|
||||
minBPM: Double = beatTrackMinBPM,
|
||||
maxBPM: Double = beatTrackMaxBPM,
|
||||
preferredBPM: Double = beatTrackPreferredBPM
|
||||
) -> BeatTrackerCore {
|
||||
BeatTrackerCore(
|
||||
minBPM: minBPM,
|
||||
maxBPM: maxBPM,
|
||||
preferredBPM: preferredBPM,
|
||||
windowSeconds: beatTrackMaxWindowSeconds,
|
||||
featureRateHz: beatTrackFeatureRateHz
|
||||
)
|
||||
}
|
||||
|
||||
/// Feed `features` features (one per block), firing an impulse ODF every
|
||||
/// `periodFeatures`-th feature. Returns counts of each trigger.
|
||||
@discardableResult
|
||||
private func driveImpulseTrain(
|
||||
_ core: BeatTrackerCore,
|
||||
periodFeatures: Int,
|
||||
features: Int
|
||||
) -> (beats: Int, eighths: Int, sixteenths: Int) {
|
||||
var beats: Int = 0
|
||||
var eighths: Int = 0
|
||||
var sixteenths: Int = 0
|
||||
for f: Int in 0..<features {
|
||||
let odf: Double = (f % periodFeatures == 0) ? 1.0 : 0.0
|
||||
core.processBlock(odf: odf, sampleCount: featureHop, sampleRate: sampleRate)
|
||||
if core.beatTriggered { beats += 1 }
|
||||
if core.eighthTriggered { eighths += 1 }
|
||||
if core.sixteenthTriggered { sixteenths += 1 }
|
||||
}
|
||||
return (beats, eighths, sixteenths)
|
||||
}
|
||||
|
||||
// MARK: - Tempo lock
|
||||
|
||||
/// Impulse train at 90 features-period-ish locks the tempo near the
|
||||
/// corresponding BPM (below the 120 preference centre).
|
||||
@Test func locksTempoBelowPreferred() {
|
||||
let core: BeatTrackerCore = makeCore()
|
||||
// 57 features ≈ 60·86/57 ≈ 90.5 BPM.
|
||||
let period: Int = 57
|
||||
driveImpulseTrain(core, periodFeatures: period, features: 800)
|
||||
let expected: Double = 60.0 * beatTrackFeatureRateHz / Double(period)
|
||||
#expect(abs(core.tempoBPM - expected) < expected * 0.06,
|
||||
"expected ≈\(expected) BPM, got \(core.tempoBPM)")
|
||||
}
|
||||
|
||||
/// Impulse train above the preference centre also locks — guards against a
|
||||
/// hard-coded / preferred-only result.
|
||||
@Test func locksTempoAbovePreferred() {
|
||||
let core: BeatTrackerCore = makeCore()
|
||||
// 36 features ≈ 60·86/36 ≈ 143.3 BPM.
|
||||
let period: Int = 36
|
||||
driveImpulseTrain(core, periodFeatures: period, features: 800)
|
||||
let expected: Double = 60.0 * beatTrackFeatureRateHz / Double(period)
|
||||
#expect(abs(core.tempoBPM - expected) < expected * 0.06,
|
||||
"expected ≈\(expected) BPM, got \(core.tempoBPM)")
|
||||
}
|
||||
|
||||
/// Two distinct inputs must produce two distinct tempo estimates.
|
||||
@Test func distinctInputsGiveDistinctTempi() {
|
||||
let slow: BeatTrackerCore = makeCore()
|
||||
let fast: BeatTrackerCore = makeCore()
|
||||
driveImpulseTrain(slow, periodFeatures: 57, features: 800)
|
||||
driveImpulseTrain(fast, periodFeatures: 36, features: 800)
|
||||
#expect(fast.tempoBPM > slow.tempoBPM + 30.0,
|
||||
"fast \(fast.tempoBPM) should clearly exceed slow \(slow.tempoBPM)")
|
||||
}
|
||||
|
||||
// MARK: - Beat spacing / subdivisions
|
||||
|
||||
@Test func beatCountMatchesPeriod() {
|
||||
let core: BeatTrackerCore = makeCore()
|
||||
let period: Int = 43 // ≈120 BPM
|
||||
let features: Int = 860 // ~20 beats
|
||||
let counts: (beats: Int, eighths: Int, sixteenths: Int) =
|
||||
driveImpulseTrain(core, periodFeatures: period, features: features)
|
||||
let expectedBeats: Double = Double(features) / Double(period)
|
||||
#expect(abs(Double(counts.beats) - expectedBeats) <= 2.0,
|
||||
"expected ≈\(expectedBeats) beats, got \(counts.beats)")
|
||||
}
|
||||
|
||||
@Test func subdivisionsFireFasterThanBeat() {
|
||||
let core: BeatTrackerCore = makeCore()
|
||||
let counts: (beats: Int, eighths: Int, sixteenths: Int) =
|
||||
driveImpulseTrain(core, periodFeatures: 43, features: 860)
|
||||
// Eighth ≈ 2×, sixteenth ≈ 4× the beat rate.
|
||||
#expect(abs(Double(counts.eighths) - 2.0 * Double(counts.beats)) <= 3.0,
|
||||
"eighths \(counts.eighths) ≉ 2× beats \(counts.beats)")
|
||||
#expect(abs(Double(counts.sixteenths) - 4.0 * Double(counts.beats)) <= 5.0,
|
||||
"sixteenths \(counts.sixteenths) ≉ 4× beats \(counts.beats)")
|
||||
}
|
||||
|
||||
// MARK: - Edge cases
|
||||
|
||||
/// Silence: tempo holds the pre-lock default, stays finite, and the phase
|
||||
/// free-runs (beats still fire).
|
||||
@Test func silenceHoldsTempoAndFreeRuns() {
|
||||
let core: BeatTrackerCore = makeCore()
|
||||
var beats: Int = 0
|
||||
for _ in 0..<800 {
|
||||
core.processBlock(odf: 0.0, sampleCount: featureHop, sampleRate: sampleRate)
|
||||
if core.beatTriggered { beats += 1 }
|
||||
}
|
||||
#expect(core.tempoBPM.isFinite)
|
||||
#expect(abs(core.tempoBPM - beatTrackPreferredBPM) < 1e-9,
|
||||
"silence should hold the preferred tempo, got \(core.tempoBPM)")
|
||||
#expect(beats > 0, "phase should free-run at the default tempo")
|
||||
}
|
||||
|
||||
/// Constant (DC) ODF: whitening removes the offset, so no NaN/Inf and tempo
|
||||
/// stays finite and sane (no degenerate lock).
|
||||
@Test func dcInputStaysFinite() {
|
||||
let core: BeatTrackerCore = makeCore()
|
||||
for _ in 0..<800 {
|
||||
core.processBlock(odf: 0.5, sampleCount: featureHop, sampleRate: sampleRate)
|
||||
}
|
||||
#expect(core.tempoBPM.isFinite)
|
||||
#expect(core.tempoBPM >= beatTrackMinBPM && core.tempoBPM <= beatTrackMaxBPM,
|
||||
"tempo \(core.tempoBPM) out of bounds for DC input")
|
||||
}
|
||||
|
||||
/// Blocks longer than one feature hop still advance the tracker (multiple
|
||||
/// features per block) and lock tempo. Each big block spans 4 feature hops;
|
||||
/// an onset every 11 big blocks → 44 features ≈ 117 BPM.
|
||||
@Test func longBlockEmitsMultipleFeaturesAndLocks() {
|
||||
let core: BeatTrackerCore = makeCore()
|
||||
let bigBlock: Int = featureHop * 4
|
||||
let periodBlocks: Int = 11
|
||||
for b: Int in 0..<260 {
|
||||
let odf: Double = (b % periodBlocks == 0) ? 1.0 : 0.0
|
||||
core.processBlock(odf: odf, sampleCount: bigBlock, sampleRate: sampleRate)
|
||||
}
|
||||
let expected: Double = 60.0 * beatTrackFeatureRateHz / Double(periodBlocks * 4)
|
||||
#expect(core.tempoBPM.isFinite)
|
||||
#expect(abs(core.tempoBPM - expected) < expected * 0.08,
|
||||
"expected ≈\(expected) BPM, got \(core.tempoBPM)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
// LoudnessControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Loudness.Control")
|
||||
struct LoudnessControlTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 512
|
||||
let sampleRate: Double = 44100
|
||||
let fftSize: Int = 2048
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
/// A spectral buffer holding a single tone (one bin) at `freqHz` with the
|
||||
/// given magnitude; every other bin is zero.
|
||||
private func toneBuffer(freqHz: Double, magnitude: Float) -> SpectralBuffer {
|
||||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||||
let binHz: Double = sampleRate / Double(fftSize)
|
||||
let bin: Int = Int((freqHz / binHz).rounded())
|
||||
buf.setMagnitude(magnitude, at: bin)
|
||||
return buf
|
||||
}
|
||||
|
||||
/// dB offset so a unit-magnitude tone bin reads `targetDB` SPL.
|
||||
/// `bandLevel = 20·log10(mag) + calibrationDB`; with `mag = 1` → `calibrationDB`.
|
||||
private func calibrationFor(targetDB: Double, magnitude: Float) -> Double {
|
||||
targetDB - 20.0 * Foundation.log10(Double(magnitude))
|
||||
}
|
||||
|
||||
private func readLoudness(_ gen: Loudness.Control) -> Double {
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
return out[0][blockSize - 1]
|
||||
}
|
||||
|
||||
private let models: [LoudnessModel] = [
|
||||
.barkEstimate,
|
||||
.realtimeZwicker(smask: loudnessDefaultSMask, tmask: loudnessDefaultTMask),
|
||||
.iso532b(field: .free)
|
||||
]
|
||||
|
||||
// MARK: - Shape
|
||||
|
||||
@Test func rateIsControlSingleRow() {
|
||||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||||
let gen: Loudness.Control = Loudness.Control(spectral: buf, sampleRate: sampleRate)
|
||||
#expect(gen.rate == .control)
|
||||
#expect(gen.outputChannelCount == 1)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(out.count == 1)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
// MARK: - Silence
|
||||
|
||||
@Test func silenceIsZeroAndFinite() {
|
||||
for model: LoudnessModel in models {
|
||||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||||
let gen: Loudness.Control = Loudness.Control(
|
||||
model: model, spectral: buf, sampleRate: sampleRate)
|
||||
let n: Double = readLoudness(gen)
|
||||
#expect(n.isFinite, "silence not finite for \(model)")
|
||||
#expect(n < 0.05, "silence should be ~0 sones, got \(n) for \(model)")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Monotonic
|
||||
|
||||
@Test func louderToneGivesMoreSones() {
|
||||
for model: LoudnessModel in models {
|
||||
let quiet: Loudness.Control = Loudness.Control(
|
||||
model: model, spectral: toneBuffer(freqHz: 1000, magnitude: 1.0),
|
||||
sampleRate: sampleRate, calibrationDB: 70)
|
||||
let loud: Loudness.Control = Loudness.Control(
|
||||
model: model, spectral: toneBuffer(freqHz: 1000, magnitude: 10.0),
|
||||
sampleRate: sampleRate, calibrationDB: 70)
|
||||
let nQuiet: Double = readLoudness(quiet)
|
||||
let nLoud: Double = readLoudness(loud)
|
||||
#expect(nLoud > nQuiet,
|
||||
"louder tone should be louder for \(model): \(nLoud) vs \(nQuiet)")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Stevens doubling (barkEstimate is exact power-law)
|
||||
|
||||
@Test func tenDBRoughlyDoublesBarkEstimate() {
|
||||
// +10 dB = magnitude × sqrt(10).
|
||||
let m0: Float = 1.0
|
||||
let m1: Float = Float(Foundation.pow(10.0, 0.5))
|
||||
let lo: Loudness.Control = Loudness.Control(
|
||||
model: .barkEstimate, spectral: toneBuffer(freqHz: 1000, magnitude: m0),
|
||||
sampleRate: sampleRate, calibrationDB: 60)
|
||||
let hi: Loudness.Control = Loudness.Control(
|
||||
model: .barkEstimate, spectral: toneBuffer(freqHz: 1000, magnitude: m1),
|
||||
sampleRate: sampleRate, calibrationDB: 60)
|
||||
let ratio: Double = readLoudness(hi) / max(1e-9, readLoudness(lo))
|
||||
#expect(abs(ratio - 2.0) < 0.2, "expected ≈2× per +10 dB, got \(ratio)")
|
||||
}
|
||||
|
||||
// MARK: - Calibration anchor: 1 kHz @ 40 dB ≈ 1 sone
|
||||
|
||||
@Test func anchorBarkEstimate() {
|
||||
let cal: Double = calibrationFor(targetDB: 40, magnitude: 1.0)
|
||||
let gen: Loudness.Control = Loudness.Control(
|
||||
model: .barkEstimate, spectral: toneBuffer(freqHz: 1000, magnitude: 1.0),
|
||||
sampleRate: sampleRate, calibrationDB: cal)
|
||||
let n: Double = readLoudness(gen)
|
||||
#expect(abs(n - 1.0) < 0.3, "barkEstimate 1 kHz@40 dB expected ≈1 sone, got \(n)")
|
||||
}
|
||||
|
||||
@Test func anchorISO532B() {
|
||||
let cal: Double = calibrationFor(targetDB: 40, magnitude: 1.0)
|
||||
let gen: Loudness.Control = Loudness.Control(
|
||||
model: .iso532b(field: .free), spectral: toneBuffer(freqHz: 1000, magnitude: 1.0),
|
||||
sampleRate: sampleRate, calibrationDB: cal)
|
||||
let n: Double = readLoudness(gen)
|
||||
#expect(n > 0.5 && n < 1.7, "iso532b 1 kHz@40 dB expected ≈1 sone, got \(n)")
|
||||
}
|
||||
|
||||
@Test func anchorRealtimeZwicker() {
|
||||
let cal: Double = calibrationFor(targetDB: 40, magnitude: 1.0)
|
||||
let gen: Loudness.Control = Loudness.Control(
|
||||
model: .realtimeZwicker(smask: loudnessDefaultSMask, tmask: loudnessDefaultTMask),
|
||||
spectral: toneBuffer(freqHz: 1000, magnitude: 1.0),
|
||||
sampleRate: sampleRate, calibrationDB: cal)
|
||||
let n: Double = readLoudness(gen)
|
||||
#expect(n > 0.5 && n < 2.0, "realtimeZwicker 1 kHz@40 dB expected ≈1 sone, got \(n)")
|
||||
}
|
||||
|
||||
// MARK: - Field
|
||||
|
||||
@Test func diffuseDiffersFromFreeForHighTone() {
|
||||
// 5 kHz: DDF correction is large (+4 dB), so diffuse ≠ free.
|
||||
let cal: Double = calibrationFor(targetDB: 60, magnitude: 1.0)
|
||||
let free: Loudness.Control = Loudness.Control(
|
||||
model: .iso532b(field: .free), spectral: toneBuffer(freqHz: 5000, magnitude: 1.0),
|
||||
sampleRate: sampleRate, calibrationDB: cal)
|
||||
let diffuse: Loudness.Control = Loudness.Control(
|
||||
model: .iso532b(field: .diffuse), spectral: toneBuffer(freqHz: 5000, magnitude: 1.0),
|
||||
sampleRate: sampleRate, calibrationDB: cal)
|
||||
#expect(abs(readLoudness(free) - readLoudness(diffuse)) > 0.05,
|
||||
"free vs diffuse should differ for a 5 kHz tone")
|
||||
}
|
||||
|
||||
// MARK: - Live chain integration
|
||||
|
||||
@Test func runsOverLiveChainFinite() throws {
|
||||
let osc: OscSin.Audio = OscSin.Audio(freq: 1000.0, mul: 0.5)
|
||||
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
||||
input: osc, fftSize: fftSize, hopSize: 512, analyzeOnly: true)
|
||||
let gen: Loudness.Control = Loudness.Control(
|
||||
model: .iso532b(field: .free), analysis: chain, sampleRate: sampleRate)
|
||||
for _ in 0..<60 {
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
||||
for i: Int in 0..<blockSize { #expect(out[0][i].isFinite) }
|
||||
advanceBlock(engine)
|
||||
}
|
||||
#expect(gen.previousOutputValue > 0.0, "a 1 kHz tone through the chain should read > 0 sones")
|
||||
}
|
||||
|
||||
@Test func floatValueReturnsLatest() {
|
||||
let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize)
|
||||
let gen: Loudness.Control = Loudness.Control(spectral: buf, sampleRate: sampleRate)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
// RunningMaxAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("RunningMax.Audio")
|
||||
struct RunningMaxAudioTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: RunningMax.Audio = RunningMax.Audio(input: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Output count matches request.
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: RunningMax.Audio = RunningMax.Audio(input: 0.5)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// First sample initializes extreme to the first input value.
|
||||
@Test func firstSampleInitializesToFirstInput() {
|
||||
let inputVal: Double = 0.3
|
||||
let gen: RunningMax.Audio = RunningMax.Audio(input: inputVal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(out[0][0] - inputVal) < 1e-12,
|
||||
"first sample should equal first input \(inputVal), got \(out[0][0])")
|
||||
}
|
||||
|
||||
/// Tracks increasing extreme over a varying input.
|
||||
///
|
||||
/// Input starts at 0.0 and increases by 0.01 per sample.
|
||||
/// The running maximum should track every new maximum.
|
||||
@Test func tracksIncreasingExtreme() {
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
||||
let gen: RunningMax.Audio = RunningMax.Audio(input: inputSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = Double(i) * 0.01
|
||||
#expect(abs(out[0][i] - expected) < 1e-12,
|
||||
"sample \(i): expected max \(expected), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Ignores values less than the current maximum.
|
||||
@Test func ignoresValuesLessThanCurrentMax() {
|
||||
// Input: 0.8 at sample 0 (init), then alternating 0.1 and 0.8.
|
||||
// The maximum should stay at 0.8 throughout.
|
||||
let inputSignal: Closure = Closure { _, i in i == 0 ? 0.8 : (i % 2 == 0 ? 0.8 : 0.1) }
|
||||
let gen: RunningMax.Audio = RunningMax.Audio(input: inputSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[0][i] - 0.8) < 1e-12,
|
||||
"maximum should stay 0.8, got \(out[0][i]) at sample \(i)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Rising trig edge resets extreme to the current input value at that frame.
|
||||
@Test func trigRisingEdgeResetsToCurrentInput() {
|
||||
// Input: 0.9 for first 32 samples, then 0.2 for 32+.
|
||||
// Trig rises at sample 32 while input is 0.2, resetting max to 0.2.
|
||||
let inputSignal: Closure = Closure { _, i in i < 32 ? 0.9 : 0.2 }
|
||||
let trigSignal: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
|
||||
let gen: RunningMax.Audio = RunningMax.Audio(input: inputSignal, trig: trigSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Samples 0..31: running max = 0.9.
|
||||
for i: Int in 0..<32 {
|
||||
#expect(abs(out[0][i] - 0.9) < 1e-12,
|
||||
"sample \(i): expected 0.9, got \(out[0][i])")
|
||||
}
|
||||
// Sample 32: reset to input at frame 32 = 0.2.
|
||||
#expect(abs(out[0][32] - 0.2) < 1e-12,
|
||||
"sample 32 after reset: expected 0.2, got \(out[0][32])")
|
||||
// Samples 33+: running max of 0.2 (input stays at 0.2, no further reset).
|
||||
for i: Int in 33..<blockSize {
|
||||
#expect(abs(out[0][i] - 0.2) < 1e-12,
|
||||
"sample \(i): expected 0.2 (no new maximum), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Cross-block continuity: extreme carries across audio blocks.
|
||||
@Test func crossBlockContinuity() {
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
// Block 1: input = 0.7 (sets max to 0.7).
|
||||
// Block 2: input = 0.3 (max stays 0.7).
|
||||
let inputSignal: Closure = Closure { graph, _ in
|
||||
graph.engine.segmentSerial == firstSerial ? 0.7 : 0.3
|
||||
}
|
||||
let gen: RunningMax.Audio = RunningMax.Audio(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// The maximum from block 1 (0.7) should persist in block 2.
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out2[0][i] - 0.7) < 1e-12,
|
||||
"block 2 sample \(i): expected max 0.7, got \(out2[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Fan-out: two calls same segment return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
||||
let gen: RunningMax.Audio = RunningMax.Audio(input: inputSignal)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
/// mul/add: output is original*2+1.
|
||||
@Test func mulAdd() {
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
||||
let genBase: RunningMax.Audio = RunningMax.Audio(input: inputSignal)
|
||||
let genScaled: RunningMax.Audio = RunningMax.Audio(
|
||||
input: inputSignal,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
let base: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
|
||||
let scaled: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = base[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaled[0][i] - expected) < 1e-12,
|
||||
"mulAdd sample \(i): expected \(expected) got \(scaled[0][i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
// RunningMaxControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("RunningMax.Control")
|
||||
struct RunningMaxControlTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: RunningMax.Control = RunningMax.Control(input: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Output count matches request.
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: RunningMax.Control = RunningMax.Control(input: 0.5)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// Block-final extreme is flat (vDSP_vfillD): all samples equal the final max.
|
||||
@Test func outputBlockIsFlat() {
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * 0.001 }
|
||||
let gen: RunningMax.Control = RunningMax.Control(input: inputSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let expected: Double = out[0][blockSize - 1]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[0][i] == expected,
|
||||
"expected flat block, sample \(i) = \(out[0][i]) differs from last = \(expected)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Block-final value matches the running maximum of all audio samples in the block.
|
||||
@Test func blockFinalValueIsRunningMaxOfBlock() {
|
||||
// Input ascends 0.0 → 0.63 over the block. The maximum after scanning = (blockSize-1)*0.01.
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
||||
let gen: RunningMax.Control = RunningMax.Control(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let expectedMax: Double = Double(blockSize - 1) * 0.01
|
||||
#expect(abs(gen.previousOutputValue - expectedMax) < 1e-12,
|
||||
"block final value should be \(expectedMax), got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Trig rising edge in a block resets extreme to current input at that frame.
|
||||
@Test func trigEdgeInBlockResetsExtreme() {
|
||||
// Input: 0.9 for first 32 samples, then 0.2 for 32..63.
|
||||
// Trig rises at sample 32. At reset, current input = 0.2,
|
||||
// so new max after the reset = 0.2 (input stays at 0.2 for remaining samples).
|
||||
let inputSignal: Closure = Closure { _, i in i < 32 ? 0.9 : 0.2 }
|
||||
let trigSignal: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
|
||||
let gen: RunningMax.Control = RunningMax.Control(input: inputSignal, trig: trigSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// After reset at sample 32 (input=0.2), remaining samples are also 0.2.
|
||||
// Running max after reset = 0.2.
|
||||
#expect(abs(gen.previousOutputValue - 0.2) < 1e-12,
|
||||
"after trig reset at sample 32 (input=0.2), block max should be 0.2, got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: RunningMax.Control = RunningMax.Control(input: 0.5)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
|
||||
/// Fan-out: two calls same segment return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
||||
let gen: RunningMax.Control = RunningMax.Control(input: inputSignal)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
/// Sub-block bypass: second call in same segment is cache hit; state not re-advanced.
|
||||
@Test func subBlockBypassCacheHit() {
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
||||
let gen: RunningMax.Control = RunningMax.Control(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let stateAfterFirst: Double = gen.previousOutputValue
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == stateAfterFirst,
|
||||
"second call must be a cache hit — state must not re-advance")
|
||||
}
|
||||
|
||||
/// Cross-block continuity: extreme carries across audio blocks.
|
||||
@Test func crossBlockContinuity() {
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
// Block 1: input = 0.8, sets max to 0.8.
|
||||
// Block 2: input = 0.1, max stays 0.8.
|
||||
let inputSignal: Closure = Closure { graph, _ in
|
||||
graph.engine.segmentSerial == firstSerial ? 0.8 : 0.1
|
||||
}
|
||||
let gen: RunningMax.Control = RunningMax.Control(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 0.8) < 1e-12)
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 0.8) < 1e-12,
|
||||
"extreme 0.8 from block 1 should persist in block 2 (0.1 < 0.8)")
|
||||
}
|
||||
|
||||
/// mul/add: output is original*2+1.
|
||||
@Test func mulAdd() {
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
||||
let genBase: RunningMax.Control = RunningMax.Control(input: inputSignal)
|
||||
let genScaled: RunningMax.Control = RunningMax.Control(
|
||||
input: inputSignal,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
let base: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
|
||||
let scaled: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = base[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaled[0][i] - expected) < 1e-12,
|
||||
"mulAdd sample \(i): expected \(expected) got \(scaled[0][i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
// RunningMinAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("RunningMin.Audio")
|
||||
struct RunningMinAudioTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: RunningMin.Audio = RunningMin.Audio(input: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Output count matches request.
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: RunningMin.Audio = RunningMin.Audio(input: 0.5)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// First sample initializes extreme to the first input value.
|
||||
@Test func firstSampleInitializesToFirstInput() {
|
||||
let inputVal: Double = 0.7
|
||||
let gen: RunningMin.Audio = RunningMin.Audio(input: inputVal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(out[0][0] - inputVal) < 1e-12,
|
||||
"first sample should equal first input \(inputVal), got \(out[0][0])")
|
||||
}
|
||||
|
||||
/// Tracks decreasing extreme over a varying input.
|
||||
///
|
||||
/// Input starts at 1.0 and decreases by 0.01 per sample.
|
||||
/// The running minimum should track every new minimum.
|
||||
@Test func tracksDecreasingExtreme() {
|
||||
// Input: 1.0, 0.99, 0.98, ... → the minimum decreases each sample.
|
||||
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
|
||||
let gen: RunningMin.Audio = RunningMin.Audio(input: inputSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = 1.0 - Double(i) * 0.01
|
||||
#expect(abs(out[0][i] - expected) < 1e-12,
|
||||
"sample \(i): expected min \(expected), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Ignores values that are greater than the current minimum.
|
||||
@Test func ignoresValuesGreaterThanCurrentMin() {
|
||||
// Input: 0.5 at sample 0 (init), then alternating 1.0 and 0.5.
|
||||
// The minimum should stay at 0.5 throughout.
|
||||
let inputSignal: Closure = Closure { _, i in i == 0 ? 0.5 : (i % 2 == 0 ? 0.5 : 1.0) }
|
||||
let gen: RunningMin.Audio = RunningMin.Audio(input: inputSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[0][i] - 0.5) < 1e-12,
|
||||
"minimum should stay 0.5, got \(out[0][i]) at sample \(i)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Rising trig edge resets extreme to the current input value at that frame.
|
||||
@Test func trigRisingEdgeResetsToCurrentInput() {
|
||||
// Input: starts at 0.1, trig rises at sample 32 while input is at 0.5.
|
||||
// Before sample 32: running min of 0.1.
|
||||
// At sample 32: reset to input value at frame 32.
|
||||
let inputSignal: Closure = Closure { _, i in i < 32 ? 0.1 : 0.5 }
|
||||
let trigSignal: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
|
||||
let gen: RunningMin.Audio = RunningMin.Audio(input: inputSignal, trig: trigSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Samples 0..31: running min from 0.1 initial = 0.1.
|
||||
for i: Int in 0..<32 {
|
||||
#expect(abs(out[0][i] - 0.1) < 1e-12,
|
||||
"sample \(i): expected 0.1, got \(out[0][i])")
|
||||
}
|
||||
// Sample 32: reset to input at frame 32 = 0.5.
|
||||
#expect(abs(out[0][32] - 0.5) < 1e-12,
|
||||
"sample 32 after reset: expected 0.5, got \(out[0][32])")
|
||||
// Samples 33+: running min of 0.5 (input stays at 0.5, no further reset).
|
||||
for i: Int in 33..<blockSize {
|
||||
#expect(abs(out[0][i] - 0.5) < 1e-12,
|
||||
"sample \(i): expected 0.5 (no new minimum), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Cross-block continuity: extreme carries across audio blocks.
|
||||
@Test func crossBlockContinuity() {
|
||||
// Block 1: input is 0.3 (sets min to 0.3).
|
||||
// Block 2: input is 0.5 (min stays 0.3 since 0.5 > 0.3).
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
let inputSignal: Closure = Closure { graph, _ in
|
||||
graph.engine.segmentSerial == firstSerial ? 0.3 : 0.5
|
||||
}
|
||||
let gen: RunningMin.Audio = RunningMin.Audio(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// The minimum from block 1 (0.3) should persist in block 2 since input is 0.5.
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out2[0][i] - 0.3) < 1e-12,
|
||||
"block 2 sample \(i): expected min 0.3, got \(out2[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Fan-out: two calls same segment return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
||||
let gen: RunningMin.Audio = RunningMin.Audio(input: inputSignal)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
/// mul/add: output is original*2+1.
|
||||
@Test func mulAdd() {
|
||||
// Decreasing input so min updates each sample.
|
||||
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
|
||||
let genBase: RunningMin.Audio = RunningMin.Audio(input: inputSignal)
|
||||
let genScaled: RunningMin.Audio = RunningMin.Audio(
|
||||
input: inputSignal,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
let base: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
|
||||
let scaled: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = base[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaled[0][i] - expected) < 1e-12,
|
||||
"mulAdd sample \(i): expected \(expected) got \(scaled[0][i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
// RunningMinControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("RunningMin.Control")
|
||||
struct RunningMinControlTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: RunningMin.Control = RunningMin.Control(input: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Output count matches request.
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: RunningMin.Control = RunningMin.Control(input: 0.5)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// Block-final extreme is flat (vDSP_vfillD): all samples equal the final min.
|
||||
@Test func outputBlockIsFlat() {
|
||||
// Decreasing input: the scan finds a new minimum on each audio sample in the block.
|
||||
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.001 }
|
||||
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let expected: Double = out[0][blockSize - 1]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[0][i] == expected,
|
||||
"expected flat block, sample \(i) = \(out[0][i]) differs from last = \(expected)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Block-final value matches the running minimum of all audio samples in the block.
|
||||
@Test func blockFinalValueIsRunningMinOfBlock() {
|
||||
// Input descends 1.0 → 0.0 over the block. The minimum after scanning = 1 - (blockSize-1)*0.01.
|
||||
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
|
||||
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let expectedMin: Double = 1.0 - Double(blockSize - 1) * 0.01
|
||||
#expect(abs(gen.previousOutputValue - expectedMin) < 1e-12,
|
||||
"block final value should be \(expectedMin), got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Trig rising edge in a block resets extreme to current input at that frame.
|
||||
@Test func trigEdgeInBlockResetsExtreme() {
|
||||
// Input is 0.1 for samples 0..31, then 0.8 for 32..63.
|
||||
// Trig rises at sample 32. At reset, the current input is 0.8, so the
|
||||
// new minimum after the block is 0.8 (input stays at 0.8 after reset).
|
||||
let inputSignal: Closure = Closure { _, i in i < 32 ? 0.1 : 0.8 }
|
||||
let trigSignal: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
|
||||
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal, trig: trigSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// After reset at sample 32 (input=0.8), remaining samples are also 0.8.
|
||||
// Running min after reset = 0.8.
|
||||
#expect(abs(gen.previousOutputValue - 0.8) < 1e-12,
|
||||
"after trig reset at sample 32 (input=0.8), block min should be 0.8, got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: RunningMin.Control = RunningMin.Control(input: 0.5)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
|
||||
/// Fan-out: two calls same segment return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
|
||||
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
/// Sub-block bypass: second call in same segment is cache hit; state not re-advanced.
|
||||
@Test func subBlockBypassCacheHit() {
|
||||
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
|
||||
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let stateAfterFirst: Double = gen.previousOutputValue
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == stateAfterFirst,
|
||||
"second call must be a cache hit — state must not re-advance")
|
||||
}
|
||||
|
||||
/// Cross-block continuity: extreme carries across audio blocks.
|
||||
@Test func crossBlockContinuity() {
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
// Block 1: input = 0.2, sets min to 0.2.
|
||||
// Block 2: input = 0.9, min stays 0.2.
|
||||
let inputSignal: Closure = Closure { graph, _ in
|
||||
graph.engine.segmentSerial == firstSerial ? 0.2 : 0.9
|
||||
}
|
||||
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 0.2) < 1e-12)
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 0.2) < 1e-12,
|
||||
"extreme 0.2 from block 1 should persist in block 2 (0.9 > 0.2)")
|
||||
}
|
||||
|
||||
/// mul/add: output is original*2+1.
|
||||
@Test func mulAdd() {
|
||||
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
|
||||
let genBase: RunningMin.Control = RunningMin.Control(input: inputSignal)
|
||||
let genScaled: RunningMin.Control = RunningMin.Control(
|
||||
input: inputSignal,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
let base: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
|
||||
let scaled: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = base[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaled[0][i] - expected) < 1e-12,
|
||||
"mulAdd sample \(i): expected \(expected) got \(scaled[0][i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
// SlopeAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Slope.Audio")
|
||||
struct SlopeAudioTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: Slope.Audio = Slope.Audio(input: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Output count matches request.
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: Slope.Audio = Slope.Audio(input: 0.5)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// First sample outputs 0.0 (lazy-init — no spike on startup).
|
||||
@Test func firstSampleOutputsZero() {
|
||||
let gen: Slope.Audio = Slope.Audio(input: 5.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(out[0][0] - 0.0) < 1e-12,
|
||||
"first sample should be 0.0 (lazy-init), got \(out[0][0])")
|
||||
}
|
||||
|
||||
/// Constant input produces slope 0 after the first sample.
|
||||
@Test func constantInputSlopeIsZero() {
|
||||
let gen: Slope.Audio = Slope.Audio(input: 3.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Sample 0 = 0 (init). Samples 1+ = (3.0 - 3.0) * sr = 0.
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[0][i] - 0.0) < 1e-12,
|
||||
"constant input: slope should be 0 at sample \(i), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Linear ramp input x[i] = i*k → slope = k * sampleRate for samples >= 1.
|
||||
///
|
||||
/// x[n] = n*k, x[n-1] = (n-1)*k → slope = (n*k - (n-1)*k)*sr = k*sr.
|
||||
@Test func linearRampProducesConstantSlope() {
|
||||
let k: Double = 0.001 // increment per sample
|
||||
let expectedSlope: Double = k * sampleRate
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * k }
|
||||
let gen: Slope.Audio = Slope.Audio(input: inputSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Sample 0: lazy-init (output 0).
|
||||
#expect(abs(out[0][0] - 0.0) < 1e-12)
|
||||
// Samples 1..blockSize-1: slope = k * sampleRate.
|
||||
for i: Int in 1..<blockSize {
|
||||
#expect(abs(out[0][i] - expectedSlope) < 1e-9,
|
||||
"sample \(i): expected slope \(expectedSlope), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Cross-block continuity: prevSample carries across blocks.
|
||||
@Test func crossBlockContinuity() {
|
||||
// A step function: 0.0 in block 1, 1.0 in block 2.
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
let inputSignal: Closure = Closure { graph, _ in
|
||||
graph.engine.segmentSerial == firstSerial ? 0.0 : 1.0
|
||||
}
|
||||
let gen: Slope.Audio = Slope.Audio(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// First sample of block 2: prev = 0.0 (last sample of block 1), x = 1.0.
|
||||
// slope = (1.0 - 0.0) * sampleRate.
|
||||
let expectedSlope: Double = sampleRate
|
||||
#expect(abs(out2[0][0] - expectedSlope) < 1e-9,
|
||||
"block 2 sample 0: expected slope \(expectedSlope) (step from 0→1), got \(out2[0][0])")
|
||||
}
|
||||
|
||||
/// Fan-out: two calls same segment return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
||||
let gen: Slope.Audio = Slope.Audio(input: inputSignal)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
/// mul/add: output is original*2+1.
|
||||
@Test func mulAdd() {
|
||||
let k: Double = 0.001
|
||||
let inputSignal: Closure = Closure { _, i in Double(i) * k }
|
||||
let genBase: Slope.Audio = Slope.Audio(input: inputSignal)
|
||||
let genScaled: Slope.Audio = Slope.Audio(input: inputSignal, mul: 2.0, add: 1.0)
|
||||
let base: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
|
||||
let scaled: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = base[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaled[0][i] - expected) < 1e-12,
|
||||
"mulAdd sample \(i): expected \(expected) got \(scaled[0][i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
// SlopeControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Slope.Control")
|
||||
struct SlopeControlTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: Slope.Control = Slope.Control(input: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Output count matches request.
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: Slope.Control = Slope.Control(input: 0.5)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// First block outputs 0.0 (lazy-init — no startup spike).
|
||||
@Test func firstBlockOutputsZero() {
|
||||
let gen: Slope.Control = Slope.Control(input: 5.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 0.0) < 1e-12,
|
||||
"first block should output 0.0 (lazy-init), got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Constant input produces slope 0 after initialization.
|
||||
@Test func constantInputSlopeIsZero() {
|
||||
let gen: Slope.Control = Slope.Control(input: 3.0)
|
||||
// Block 1: lazy-init → output 0.
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 0.0) < 1e-12,
|
||||
"first block of constant input: slope = 0")
|
||||
|
||||
// Block 2: x stays at 3.0, prev = 3.0 → slope = 0.
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 0.0) < 1e-12,
|
||||
"second block of constant input: slope should still be 0")
|
||||
}
|
||||
|
||||
/// Control-rate slope uses controlRate = sampleRate/blockSize.
|
||||
///
|
||||
/// If the input steps from 0 to 1 between blocks, the slope is
|
||||
/// (1.0 - 0.0) * controlRate = sampleRate / blockSize.
|
||||
@Test func stepInputProducesControlRateSlope() {
|
||||
let controlRate: Double = sampleRate / Double(blockSize)
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
let inputSignal: Closure = Closure { graph, _ in
|
||||
graph.engine.segmentSerial == firstSerial ? 0.0 : 1.0
|
||||
}
|
||||
let gen: Slope.Control = Slope.Control(input: inputSignal)
|
||||
|
||||
// Block 1: lazy-init → output 0, prevSample becomes 0.0.
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 0.0) < 1e-12)
|
||||
|
||||
// Block 2: x = 1.0, prev = 0.0 → slope = (1.0 - 0.0) * controlRate.
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let expectedSlope: Double = controlRate
|
||||
#expect(abs(gen.previousOutputValue - expectedSlope) < 1e-9,
|
||||
"step slope: expected \(expectedSlope), got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Output block is flat (vDSP_vfillD): all samples equal the computed slope.
|
||||
@Test func outputBlockIsFlat() {
|
||||
let gen: Slope.Control = Slope.Control(input: 0.5)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let expected: Double = out[0][blockSize - 1]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[0][i] == expected,
|
||||
"expected flat block, sample \(i) = \(out[0][i]) differs from last = \(expected)")
|
||||
}
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: Slope.Control = Slope.Control(input: 0.0)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
|
||||
/// Fan-out: two calls same segment return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let gen: Slope.Control = Slope.Control(input: 0.3)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
/// Sub-block bypass: second call in same segment is cache hit; state not re-advanced.
|
||||
@Test func subBlockBypassCacheHit() {
|
||||
let gen: Slope.Control = Slope.Control(input: 1.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let stateAfterFirst: Double = gen.previousOutputValue
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == stateAfterFirst,
|
||||
"second call must be a cache hit — state must not re-advance")
|
||||
}
|
||||
|
||||
/// Cross-block continuity: prevSample carries across blocks.
|
||||
@Test func crossBlockContinuity() {
|
||||
let controlRate: Double = sampleRate / Double(blockSize)
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
// Block 1 input = 0.0, block 2+ = 0.5.
|
||||
let inputSignal: Closure = Closure { graph, _ in
|
||||
graph.engine.segmentSerial == firstSerial ? 0.0 : 0.5
|
||||
}
|
||||
let gen: Slope.Control = Slope.Control(input: inputSignal)
|
||||
// Block 1: lazy-init, prevSample = 0.0, output = 0.
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
// Block 2: x = 0.5, prev = 0.0 → slope = 0.5 * controlRate.
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let expectedSlope: Double = 0.5 * controlRate
|
||||
#expect(abs(gen.previousOutputValue - expectedSlope) < 1e-9,
|
||||
"cross-block slope: expected \(expectedSlope), got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// mul/add: output is original*2+1.
|
||||
@Test func mulAdd() {
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
let inputSignal: Closure = Closure { graph, _ in
|
||||
graph.engine.segmentSerial == firstSerial ? 0.0 : 1.0
|
||||
}
|
||||
let genBase: Slope.Control = Slope.Control(input: inputSignal)
|
||||
let genScaled: Slope.Control = Slope.Control(input: inputSignal, mul: 2.0, add: 1.0)
|
||||
|
||||
// Block 1: both init to 0 → output 0 (base) and 1 (scaled: 0*2+1).
|
||||
let base1: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
|
||||
let scaled1: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = base1[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaled1[0][i] - expected) < 1e-12,
|
||||
"block 1 mulAdd sample \(i): expected \(expected) got \(scaled1[0][i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
// ZeroCrossingAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("ZeroCrossing.Audio")
|
||||
struct ZeroCrossingAudioTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: ZeroCrossing.Audio = ZeroCrossing.Audio(input: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Output count matches request.
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: ZeroCrossing.Audio = ZeroCrossing.Audio(input: 0.5)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// Constant positive (DC) input produces no zero crossing and freq stays at `0.0`.
|
||||
///
|
||||
/// `_prevSample` is lazy-initialised to the first input sample (`+1`). Since `prevSample`
|
||||
/// equals `x` on every subsequent sample, the rising-edge condition
|
||||
/// `prevSample <= 0 && x > 0` is never satisfied. SuperCollider `ZeroCrossing`
|
||||
/// exhibits identical behaviour (`m_previn = ZIN0(0)`).
|
||||
@Test func constantPositiveDCInputYieldsZeroFreq() {
|
||||
let gen: ZeroCrossing.Audio = ZeroCrossing.Audio(input: 1.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[0][i] == 0.0,
|
||||
"constant positive DC input: freq should be 0 (no crossing), got \(out[0][i]) at sample \(i)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Constant negative input: no rising zero crossing from the initial state (prevSample=0,
|
||||
/// x=-1 — condition prevSample<=0 && x>0 is false). Freq stays 0.
|
||||
@Test func constantNegativeInputNoZeroCrossing() {
|
||||
let gen: ZeroCrossing.Audio = ZeroCrossing.Audio(input: -1.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[0][i] == 0.0,
|
||||
"constant negative input should yield freq 0, got \(out[0][i]) at sample \(i)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Square wave with period P samples yields freq = sampleRate / P after the second
|
||||
/// measured rising zero crossing.
|
||||
///
|
||||
/// A square wave alternates every P/2 samples. The rising ZC (≤0 → >0) fires every P
|
||||
/// samples from the second crossing onward. `_prevSample` is lazy-initialised to the
|
||||
/// first input sample (-1), so no spurious crossing fires on sample 0. The first
|
||||
/// crossing at sample 8 has a count of 9 (incremented for samples 0..8); the second
|
||||
/// and subsequent crossings measure exactly P = 16.
|
||||
@Test func squareWaveKnownPeriodYieldsExpectedFreq() {
|
||||
// Period = 16 samples. Signal: -1 for samples 0..7, +1 for 8..15, -1 for 16..23, +1 for 24..31 ...
|
||||
// Sample 0: lazy-init sets prevSample=-1. count=1. No crossing (x=-1).
|
||||
// Sample 8: count=9 (>4). prevSample=-1<=0, x=+1>0 → crossing fires. freq=sr/9. count=0.
|
||||
// Sample 24: count=16 (>4). prev=-1<=0, x=+1>0 → crossing. freq=sr/16.
|
||||
// From sample 24 onward, freq is sr/16.
|
||||
let period: Int = 16
|
||||
let halfPeriod: Int = period / 2
|
||||
let expectedFreq: Double = sampleRate / Double(period)
|
||||
|
||||
let inputSignal: Closure = Closure { _, i in
|
||||
((i / halfPeriod) % 2 == 0) ? -1.0 : 1.0
|
||||
}
|
||||
let gen: ZeroCrossing.Audio = ZeroCrossing.Audio(input: inputSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
// The second rising crossing is at sample 24 (period=16, half=8: -1 @ 0..7, +1 @ 8..15,
|
||||
// -1 @ 16..23, +1 @ 24..31). From sample 24 onward, freq should be sr/16.
|
||||
let secondCrossingAt: Int = period + halfPeriod // 24
|
||||
for i: Int in secondCrossingAt..<blockSize {
|
||||
#expect(abs(out[0][i] - expectedFreq) < 1e-9,
|
||||
"sample \(i): expected \(expectedFreq) (stable period after 2nd crossing), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Frequency is held between crossings (output stays constant between rising edges).
|
||||
///
|
||||
/// Uses a signal starting at -1 so that `_prevSample` is lazy-initialised to -1 on
|
||||
/// sample 0 and no spurious crossing fires. Signal: -1 for samples 0..9, +1 for
|
||||
/// 10..19, -1 for 20..29, +1 for 30..39 …
|
||||
///
|
||||
/// Sample 0: lazy-init sets prevSample=-1. count incremented to 1. No crossing (x=-1).
|
||||
/// First rising crossing fires at sample 10 with count = halfPeriod+1 = 11 (samples
|
||||
/// 0..10 each increment count, 11 > zeroCrossingMinCount). Freq = sr/11. Count resets.
|
||||
/// Second rising crossing fires at sample 30 with count = halfPeriod*2 = 20. Freq = sr/20.
|
||||
/// Between crossings the output holds the last measured freq.
|
||||
@Test func freqHeldBetweenCrossings() {
|
||||
let halfPeriod: Int = 10
|
||||
// Start with -1 for first half-period so lazy-init sets prevSample=-1 (no crossing).
|
||||
let inputSignal: Closure = Closure { _, i in
|
||||
((i / halfPeriod) % 2 == 0) ? -1.0 : 1.0
|
||||
}
|
||||
let gen: ZeroCrossing.Audio = ZeroCrossing.Audio(input: inputSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
// Samples 0..9: no crossing yet (count stays below threshold or x never >0), freq = 0.
|
||||
for i: Int in 0..<halfPeriod {
|
||||
#expect(out[0][i] == 0.0,
|
||||
"sample \(i): expected 0 (no crossing yet), got \(out[0][i])")
|
||||
}
|
||||
// Sample 10: first rising crossing. Count at crossing = halfPeriod+1 = 11 (>4). Freq = sr/11.
|
||||
let firstCrossingCount: Int = halfPeriod + 1
|
||||
let freqAfterFirstCrossing: Double = sampleRate / Double(firstCrossingCount)
|
||||
// Samples 10..29: freq held at freqAfterFirstCrossing (next crossing not until sample 30).
|
||||
for i: Int in halfPeriod..<(halfPeriod * 3) {
|
||||
#expect(abs(out[0][i] - freqAfterFirstCrossing) < 1e-9,
|
||||
"sample \(i): expected \(freqAfterFirstCrossing) (held after 1st crossing), got \(out[0][i])")
|
||||
}
|
||||
// Sample 30: second rising crossing. Count since reset = halfPeriod*2 = 20 (>4). Freq = sr/20.
|
||||
let secondCrossingCount: Int = halfPeriod * 2
|
||||
let freqAfterSecondCrossing: Double = sampleRate / Double(secondCrossingCount)
|
||||
// Samples 30..49: freq held at freqAfterSecondCrossing.
|
||||
for i: Int in (halfPeriod * 3)..<(halfPeriod * 5) {
|
||||
#expect(abs(out[0][i] - freqAfterSecondCrossing) < 1e-9,
|
||||
"sample \(i): expected \(freqAfterSecondCrossing) (held after 2nd crossing), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Cross-block continuity: state (counter) carries across audio blocks.
|
||||
@Test func crossBlockContinuity() {
|
||||
// Square wave with period 32: crossing happens at sample 16 and 32 (=0 of block 2).
|
||||
// Block 1 measures 32 samples between crossings (at sample 16 the count includes samples
|
||||
// from the previous block which starts at 0, so count = 16+prev if counter carried).
|
||||
// This test verifies counter survives block boundary — non-zero freq in block 2.
|
||||
let halfPeriod: Int = 16
|
||||
let inputSignal: Closure = Closure { _, i in
|
||||
((i / halfPeriod) % 2 == 0) ? 1.0 : -1.0
|
||||
}
|
||||
let gen: ZeroCrossing.Audio = ZeroCrossing.Audio(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Block 2 should still detect crossings and emit a non-zero freq.
|
||||
var hasNonZero: Bool = false
|
||||
for i: Int in 0..<blockSize {
|
||||
if out2[0][i] > 0.0 {
|
||||
hasNonZero = true
|
||||
break
|
||||
}
|
||||
}
|
||||
#expect(hasNonZero, "expected non-zero freq in block 2 (counter should carry across blocks)")
|
||||
}
|
||||
|
||||
/// Fan-out: two calls same segment return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let inputSignal: Closure = Closure { _, i in i % 8 < 4 ? 1.0 : -1.0 }
|
||||
let gen: ZeroCrossing.Audio = ZeroCrossing.Audio(input: inputSignal)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
/// mul/add: output is original*2+1.
|
||||
@Test func mulAdd() {
|
||||
let halfPeriod: Int = 8
|
||||
let inputSignal: Closure = Closure { _, i in ((i / halfPeriod) % 2 == 0) ? -1.0 : 1.0 }
|
||||
let genBase: ZeroCrossing.Audio = ZeroCrossing.Audio(input: inputSignal)
|
||||
let genScaled: ZeroCrossing.Audio = ZeroCrossing.Audio(
|
||||
input: inputSignal,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
let base: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
|
||||
let scaled: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = base[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaled[0][i] - expected) < 1e-12,
|
||||
"mulAdd sample \(i): expected \(expected) got \(scaled[0][i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
// ZeroCrossingControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("ZeroCrossing.Control")
|
||||
struct ZeroCrossingControlTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Output count matches request.
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: 0.5)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// Constant negative input: no rising crossing fires (prevSample=0 ≤ 0, x=-1 — condition
|
||||
/// `prevSample<=0 && x>0` is false). Freq stays 0 throughout the block.
|
||||
@Test func constantNegativeInputNoZeroCrossing() {
|
||||
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: -1.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == 0.0,
|
||||
"constant negative input: freq should remain 0, got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Square wave with period P samples yields freq = sampleRate / P (measured
|
||||
/// over the full audio block at audio rate, reported as flat block-final value).
|
||||
///
|
||||
/// Signal starts at -1, so lazy-init sets `_prevSample = -1` on sample 0 and no
|
||||
/// spurious crossing fires. The first crossing fires at sample 8 (count=9>4, freq=sr/9);
|
||||
/// subsequent crossings measure exactly P=16 (count=16>4, freq=sr/16). Block-final
|
||||
/// value after 64 samples is sr/16.
|
||||
@Test func squareWaveKnownPeriodYieldsExpectedFreq() {
|
||||
// Period = 16 samples → expected freq = 44100 / 16 = 2756.25 Hz.
|
||||
let period: Int = 16
|
||||
let expectedFreq: Double = sampleRate / Double(period)
|
||||
// -1 for first half, +1 for second half, repeating.
|
||||
let inputSignal: Closure = Closure { _, i in
|
||||
((i / (period / 2)) % 2 == 0) ? -1.0 : 1.0
|
||||
}
|
||||
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - expectedFreq) < 1e-9,
|
||||
"expected freq \(expectedFreq), got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Output block is flat: all samples equal the block-final freq.
|
||||
@Test func outputBlockIsFlat() {
|
||||
let period: Int = 8
|
||||
let inputSignal: Closure = Closure { _, i in
|
||||
((i / (period / 2)) % 2 == 0) ? -1.0 : 1.0
|
||||
}
|
||||
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: inputSignal)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let expected: Double = out[0][blockSize - 1]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[0][i] == expected,
|
||||
"expected flat block, sample \(i) = \(out[0][i]) differs from last = \(expected)")
|
||||
}
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: 0.0)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
|
||||
/// Fan-out: two calls same segment return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let inputSignal: Closure = Closure { _, i in i % 8 < 4 ? 1.0 : -1.0 }
|
||||
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: inputSignal)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
/// Sub-block bypass: second call in same segment is a cache hit; state not re-advanced.
|
||||
@Test func subBlockBypassCacheHit() {
|
||||
let inputSignal: Closure = Closure { _, i in i % 8 < 4 ? 1.0 : -1.0 }
|
||||
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let stateAfterFirst: Double = gen.previousOutputValue
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == stateAfterFirst,
|
||||
"second call in same segment must be a cache hit — state must not re-advance")
|
||||
}
|
||||
|
||||
/// Cross-block continuity: counter carries across audio blocks.
|
||||
@Test func crossBlockContinuity() {
|
||||
// Use a slow square wave (period = 128 samples) that spans two blocks.
|
||||
// After block 1, the counter should be non-zero and the freq in block 2 should be valid.
|
||||
let period: Int = 32
|
||||
let inputSignal: Closure = Closure { _, i in ((i / (period / 2)) % 2 == 0) ? -1.0 : 1.0 }
|
||||
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: inputSignal)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// In block 2, crossings still detected — previousOutputValue should be non-zero.
|
||||
#expect(gen.previousOutputValue > 0.0,
|
||||
"freq should be non-zero in block 2 (state carries across blocks)")
|
||||
}
|
||||
|
||||
/// mul/add: output is original*2+1.
|
||||
@Test func mulAdd() {
|
||||
let period: Int = 16
|
||||
let inputSignal: Closure = Closure { _, i in
|
||||
((i / (period / 2)) % 2 == 0) ? -1.0 : 1.0
|
||||
}
|
||||
let genBase: ZeroCrossing.Control = ZeroCrossing.Control(input: inputSignal)
|
||||
let genScaled: ZeroCrossing.Control = ZeroCrossing.Control(
|
||||
input: inputSignal,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
let base: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
|
||||
let scaled: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Both should have a consistent relationship: scaled[i] = base[i]*2+1.
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = base[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaled[0][i] - expected) < 1e-12,
|
||||
"mulAdd sample \(i): expected \(expected) got \(scaled[0][i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user