// NormalizerTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth private func peak(_ samples: [Double]) -> Double { samples.map { abs($0) }.max() ?? 0 } @MainActor @Suite("Normalizer") struct NormalizerTests { let engine: SynthesisEngine let sr: Double = 44100.0 init() throws { engine = makeTestEngine(blockSize: 64, sampleRate: 44100) try engine.start() } @Test func quietSineNormalizedUpToLevel() { // A sine well below `level` is scaled UP so its steady-state peak ≈ level. let level: Double = 0.8 let sine: OscSin.Audio = OscSin.Audio(freq: 200.0, mul: 0.2) let norm: Normalizer = Normalizer(in: sine, level: level, dur: 0.01, sampleRate: sr) let samples: [Double] = collectBlocks(norm, engine: engine, blocks: 300) let tail: [Double] = Array(samples.suffix(engine.blockSize * 100)) let p: Double = peak(tail) #expect(p > level - 0.05) #expect(p < level + 0.02) } @Test func loudSineNormalizedDownToLevel() { // A sine above `level` is scaled DOWN; steady-state peak ≈ level, no overshoot. let level: Double = 0.5 let sine: OscSin.Audio = OscSin.Audio(freq: 220.0, mul: 2.0) let norm: Normalizer = Normalizer(in: sine, level: level, dur: 0.01, sampleRate: sr) let samples: [Double] = collectBlocks(norm, engine: engine, blocks: 200) let tail: [Double] = Array(samples.suffix(engine.blockSize * 100)) let p: Double = peak(tail) #expect(p > level - 0.05) #expect(p < level + 0.001) } @Test func peakNeverExceedsLevelOnTransient() { // Look-ahead must pre-emptively reduce gain so a sudden loud burst never // overshoots `level`. let level: Double = 0.5 let sine: OscSin.Audio = OscSin.Audio(freq: 110.0, mul: 3.0) let norm: Normalizer = Normalizer(in: sine, level: level, dur: 0.01, sampleRate: sr) let samples: [Double] = collectBlocks(norm, engine: engine, blocks: 200) let violators: [Double] = samples.filter { abs($0) > level + 0.001 } #expect(violators.isEmpty) } @Test func delayedByDurSamples() { // An impulse at sample 0 appears delayed by Int(dur·sr); the first dur·sr // output samples are zero. let dur: Double = 0.003 let delaySamples: Int = Int(dur * sr) let impulse: Impulse.Audio = Impulse.Audio(freq: 0.0) let norm: Normalizer = Normalizer(in: impulse, level: 1.0, dur: dur, sampleRate: sr) let blocks: Int = (delaySamples / engine.blockSize) + 5 let samples: [Double] = collectBlocks(norm, engine: engine, blocks: blocks) let prefix: [Double] = Array(samples.prefix(delaySamples)) #expect(prefix.allSatisfy { abs($0) < 0.001 }) } @Test func levelParamScales() { // Doubling `level` doubles the normalized steady-state peak. let sine: OscSin.Audio = OscSin.Audio(freq: 200.0, mul: 0.3) let low: Normalizer = Normalizer(in: sine, level: 0.3, dur: 0.01, sampleRate: sr) let lowSamples: [Double] = collectBlocks(low, engine: engine, blocks: 300) let sine2: OscSin.Audio = OscSin.Audio(freq: 200.0, mul: 0.3) let high: Normalizer = Normalizer(in: sine2, level: 0.6, dur: 0.01, sampleRate: sr) let highSamples: [Double] = collectBlocks(high, engine: engine, blocks: 300) let pLow: Double = peak(Array(lowSamples.suffix(engine.blockSize * 100))) let pHigh: Double = peak(Array(highSamples.suffix(engine.blockSize * 100))) #expect(abs(pHigh - 2.0 * pLow) < 0.05) } @Test func silenceStaysSilentAndFinite() { // Zero input must not blow up (no level/0 division producing NaN/Inf). let silence: ConstantGeneratorInput = ConstantGeneratorInput(0.0) let norm: Normalizer = Normalizer(in: silence, level: 1.0, dur: 0.01, sampleRate: sr) let samples: [Double] = collectBlocks(norm, engine: engine, blocks: 50) #expect(samples.allSatisfy { $0.isFinite }) #expect(peak(samples) < 1e-9) } @Test func dcConvergesToLevel() { // Constant DC v with level L → gain converges to L/v, output → L. let level: Double = 0.5 let dc: ConstantGeneratorInput = ConstantGeneratorInput(0.25) let norm: Normalizer = Normalizer(in: dc, level: level, dur: 0.01, sampleRate: sr) let all: [Double] = collectBlocks(norm, engine: engine, blocks: 300) let tail: [Double] = Array(all.suffix(engine.blockSize * 50)) let avg: Double = tail.reduce(0.0, +) / Double(tail.count) #expect(abs(avg - level) < 0.01) } @Test func zeroDelayNormalizesDownExactly() { // dur == 0 fast path: releaseCoeff == 0, attackComp == 1 → gain reaches // level/|x| within one sample. DC 2.0 at level 1.0 → every output == 1.0. let dc: ConstantGeneratorInput = ConstantGeneratorInput(2.0) let norm: Normalizer = Normalizer(in: dc, level: 1.0, dur: 0.0, sampleRate: sr) let samples: [Double] = collectBlocks(norm, engine: engine, blocks: 2) #expect(samples.allSatisfy { abs($0 - 1.0) < 1e-12 }) } @Test func zeroDelayScalesUpExactly() { // dur == 0 fast path scaling up: DC 0.25 at level 1.0 → gain 4, output 1.0. let dc: ConstantGeneratorInput = ConstantGeneratorInput(0.25) let norm: Normalizer = Normalizer(in: dc, level: 1.0, dur: 0.0, sampleRate: sr) let samples: [Double] = collectBlocks(norm, engine: engine, blocks: 2) #expect(samples.allSatisfy { abs($0 - 1.0) < 1e-12 }) } @Test func finiteWithNoise() { let norm: Normalizer = Normalizer(in: WhiteNoise.Audio(mul: 0.4), level: 0.9, dur: 0.005, sampleRate: sr) let samples: [Double] = collectBlocks(norm, engine: engine, blocks: 200) #expect(samples.allSatisfy { $0.isFinite }) // Peak should be bounded near level once settled. let tail: [Double] = Array(samples.suffix(engine.blockSize * 100)) #expect(peak(tail) < 0.9 + 0.01) } }