1.0.0 release
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
// DenormalHandlingTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - denormalsToZero helper
|
||||
|
||||
@Suite("denormalsToZero")
|
||||
struct DenormalsToZeroTests {
|
||||
|
||||
@Test func zeroIsUnchanged() {
|
||||
#expect(denormalsToZero(0.0) == 0.0)
|
||||
}
|
||||
|
||||
@Test func normalPositiveValueIsUnchanged() {
|
||||
let x: Double = 0.5
|
||||
#expect(denormalsToZero(x) == x)
|
||||
}
|
||||
|
||||
@Test func normalNegativeValueIsUnchanged() {
|
||||
let x: Double = -0.5
|
||||
#expect(denormalsToZero(x) == x)
|
||||
}
|
||||
|
||||
@Test func valueBelowThresholdIsFlushedToZero() {
|
||||
// 1e-31 is strictly below the 1e-30 threshold.
|
||||
#expect(denormalsToZero(1e-31 as Double) == 0.0)
|
||||
#expect(denormalsToZero(-1e-31 as Double) == 0.0)
|
||||
}
|
||||
|
||||
@Test func actualDoubleSubnormalIsFlushedToZero() {
|
||||
let subnormal: Double = Double.leastNonzeroMagnitude // ~5e-324, a true Double subnormal
|
||||
#expect(subnormal.isSubnormal)
|
||||
#expect(denormalsToZero(subnormal) == 0.0)
|
||||
}
|
||||
|
||||
@Test func valueExactlyAtThresholdPassesThrough() {
|
||||
// abs(1e-30) is NOT < 1e-30, so it must pass through unchanged.
|
||||
let x: Double = 1e-30
|
||||
#expect(denormalsToZero(x) == x)
|
||||
}
|
||||
|
||||
@Test func largeValueIsUnchanged() {
|
||||
let x: Double = 1e10
|
||||
#expect(denormalsToZero(x) == x)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - FTZ + filter subnormal guard: no subnormals in output with FTZ active
|
||||
|
||||
// These tests call enableFlushToZero() to simulate the RT-thread context that
|
||||
// CoreAudioDriver.render() establishes before each processBlock call. With FTZ set,
|
||||
// any computation that would produce a subnormal result is flushed to zero by the
|
||||
// hardware, so filter outputs and delay-line writes must never be subnormal.
|
||||
|
||||
@Suite("FirstOrderSection — no subnormals under FTZ")
|
||||
struct FirstOrderSectionFTZTests {
|
||||
|
||||
/// Feed a subnormal-magnitude signal through a near-resonant one-pole filter.
|
||||
/// With FTZ active the hardware flushes subnormal products to zero; the
|
||||
/// denormalsToZero guard on the state variables provides a second line of defence.
|
||||
@Test func outputIsNeverSubnormalWithSubnormalInput() throws {
|
||||
enableFlushToZero()
|
||||
|
||||
let blockSize: Int = 64
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: 44100)
|
||||
try engine.start()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Subnormal Double as input — without FTZ this would propagate subnormals.
|
||||
let tinyInput: Double = Double.leastNonzeroMagnitude
|
||||
let filter: FirstOrderSection = FirstOrderSection(input: tinyInput, a0: 1.0, a1: 0.0, b1: 0.9999)
|
||||
|
||||
for _ in 0..<500 {
|
||||
let samples: [[Double]] = filter.calculatedSamples(graph: graph, count: blockSize)
|
||||
let block: Array<Double>.SubSequence = samples[0].prefix(blockSize)
|
||||
for sample: Double in block {
|
||||
#expect(!sample.isSubnormal,
|
||||
"FirstOrderSection produced a subnormal output with FTZ active")
|
||||
}
|
||||
advanceBlock(engine)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Suite("SecondOrderSection — no subnormals under FTZ")
|
||||
struct SecondOrderSectionFTZTests {
|
||||
|
||||
@Test func outputIsNeverSubnormalWithSubnormalInput() throws {
|
||||
enableFlushToZero()
|
||||
|
||||
let blockSize: Int = 64
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: 44100)
|
||||
try engine.start()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let tinyInput: Double = Double.leastNonzeroMagnitude
|
||||
let filter: SecondOrderSection = SecondOrderSection(
|
||||
input: tinyInput, a0: 1.0, a1: 0.0, a2: 0.0, b1: 0.9999, b2: 0.0
|
||||
)
|
||||
|
||||
for _ in 0..<500 {
|
||||
let samples: [[Double]] = filter.calculatedSamples(graph: graph, count: blockSize)
|
||||
let block: Array<Double>.SubSequence = samples[0].prefix(blockSize)
|
||||
for sample: Double in block {
|
||||
#expect(!sample.isSubnormal,
|
||||
"SecondOrderSection produced a subnormal output with FTZ active")
|
||||
}
|
||||
advanceBlock(engine)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Suite("RBJBandPass — no subnormals under FTZ")
|
||||
struct RBJBandPassFTZTests {
|
||||
|
||||
@Test func outputIsNeverSubnormalWithSubnormalInput() throws {
|
||||
enableFlushToZero()
|
||||
|
||||
let blockSize: Int = 64
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: 44100)
|
||||
try engine.start()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let tinyInput: Double = Double.leastNonzeroMagnitude
|
||||
let filter: RBJBandPass.Audio = RBJBandPass.Audio(input: tinyInput, freq: 440.0, bandwidth: 1.0)
|
||||
|
||||
for _ in 0..<500 {
|
||||
let samples: [[Double]] = filter.calculatedSamples(graph: graph, count: blockSize)
|
||||
let block: Array<Double>.SubSequence = samples[0].prefix(blockSize)
|
||||
for sample: Double in block {
|
||||
#expect(!sample.isSubnormal,
|
||||
"RBJBandPass produced a subnormal output with FTZ active")
|
||||
}
|
||||
advanceBlock(engine)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Suite("DelayLineFeedback — no subnormals under FTZ")
|
||||
struct DelayLineFeedbackFTZTests {
|
||||
|
||||
@Test func outputIsNeverSubnormalWithSubnormalInput() throws {
|
||||
enableFlushToZero()
|
||||
|
||||
let blockSize: Int = 64
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: 44100)
|
||||
try engine.start()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let tinyInput: Double = Double.leastNonzeroMagnitude
|
||||
let delay: DelayLineFeedback = DelayLineFeedback(
|
||||
input: tinyInput,
|
||||
maxDelay: 0.1,
|
||||
delay: 0.01,
|
||||
decayTime: 10.0,
|
||||
targetAmp: 0.001,
|
||||
sampleRate: 44100
|
||||
)
|
||||
|
||||
for _ in 0..<500 {
|
||||
let samples: [[Double]] = delay.calculatedSamples(graph: graph, count: blockSize)
|
||||
let block: Array<Double>.SubSequence = samples[0].prefix(blockSize)
|
||||
for sample: Double in block {
|
||||
#expect(!sample.isSubnormal,
|
||||
"DelayLineFeedback produced a subnormal output with FTZ active")
|
||||
}
|
||||
advanceBlock(engine)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user