// BalanceTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Balance") struct BalanceTests { let engine: SynthesisEngine init() throws { engine = makeTestEngine(blockSize: 64, sampleRate: 44100) try engine.start() } @Test func quietInMatchedUpToTestRMS() { // in: quiet sine (amp 0.2); test: louder sine (amp 0.5). Output amplitude // should rise to ~0.5 → RMS ≈ 0.5/√2 ≈ 0.354. let quiet: OscSin.Audio = OscSin.Audio(freq: 200.0, mul: 0.2) let loud: OscSin.Audio = OscSin.Audio(freq: 200.0, mul: 0.5) let bal: Balance = Balance(in: quiet, test: loud) let samples: [Double] = collectBlocks(bal, engine: engine, blocks: 300) let tail: [Double] = Array(samples.suffix(engine.blockSize * 100)) let expected: Double = 0.5 / 2.0.squareRoot() #expect(abs(rmsValue(tail) - expected) < 0.02) } @Test func loudInMatchedDownToTestRMS() { let loud: OscSin.Audio = OscSin.Audio(freq: 200.0, mul: 1.0) let quiet: OscSin.Audio = OscSin.Audio(freq: 200.0, mul: 0.3) let bal: Balance = Balance(in: loud, test: quiet) let samples: [Double] = collectBlocks(bal, engine: engine, blocks: 300) let tail: [Double] = Array(samples.suffix(engine.blockSize * 100)) let expected: Double = 0.3 / 2.0.squareRoot() #expect(abs(rmsValue(tail) - expected) < 0.02) } @Test func dcSteadyStateMatchesTestLevel() { // in DC 0.25, test DC 0.5. ms_in → 0.0625, ms_test → 0.25, // gain → sqrt(0.25/0.0625) = 2, out → 0.25·2 = 0.5 = test level. let inDC: ConstantGeneratorInput = ConstantGeneratorInput(0.25) let testDC: ConstantGeneratorInput = ConstantGeneratorInput(0.5) let bal: Balance = Balance(in: inDC, test: testDC) let all: [Double] = collectBlocks(bal, engine: engine, blocks: 400) let tail: [Double] = Array(all.suffix(engine.blockSize * 50)) let avg: Double = tail.reduce(0.0, +) / Double(tail.count) #expect(abs(avg - 0.5) < 0.01) } @Test func silenceStaysSilentAndFinite() { let inSil: ConstantGeneratorInput = ConstantGeneratorInput(0.0) let testSil: ConstantGeneratorInput = ConstantGeneratorInput(0.0) let bal: Balance = Balance(in: inSil, test: testSil) let samples: [Double] = collectBlocks(bal, engine: engine, blocks: 50) #expect(samples.allSatisfy { $0.isFinite }) #expect(rmsValue(samples) < 1e-9) } @Test func silentTestDrivesInputDown() { // test is silent → in should be scaled toward zero (can't match RMS of silence). let sine: OscSin.Audio = OscSin.Audio(freq: 200.0, mul: 0.5) let testSil: ConstantGeneratorInput = ConstantGeneratorInput(0.0) let bal: Balance = Balance(in: sine, test: testSil) let samples: [Double] = collectBlocks(bal, engine: engine, blocks: 200) let tail: [Double] = Array(samples.suffix(engine.blockSize * 50)) #expect(rmsValue(tail) < 0.01) #expect(samples.allSatisfy { $0.isFinite }) } @Test func silentInputStaysSilent() { // in is silent → output stays zero regardless of loud test (0 has no RMS to scale). let inSil: ConstantGeneratorInput = ConstantGeneratorInput(0.0) let loud: OscSin.Audio = OscSin.Audio(freq: 200.0, mul: 0.8) let bal: Balance = Balance(in: inSil, test: loud) let samples: [Double] = collectBlocks(bal, engine: engine, blocks: 100) #expect(samples.allSatisfy { $0.isFinite }) #expect(rmsValue(samples) < 1e-9) } @Test func hpControlsTrackingWindow() { // Both hp settings converge to the same steady-state DC level; just verify // both are finite and matched (window length affects speed, not target). let inDC: ConstantGeneratorInput = ConstantGeneratorInput(0.25) let testDC: ConstantGeneratorInput = ConstantGeneratorInput(0.5) let slow: Balance = Balance(in: inDC, test: testDC, hp: 2.0) let fast: Balance = Balance(in: ConstantGeneratorInput(0.25), test: ConstantGeneratorInput(0.5), hp: 50.0) let slowTail: [Double] = Array(collectBlocks(slow, engine: engine, blocks: 600).suffix(engine.blockSize * 50)) let fastTail: [Double] = Array(collectBlocks(fast, engine: engine, blocks: 600).suffix(engine.blockSize * 50)) let slowAvg: Double = slowTail.reduce(0.0, +) / Double(slowTail.count) let fastAvg: Double = fastTail.reduce(0.0, +) / Double(fastTail.count) #expect(abs(slowAvg - 0.5) < 0.02) #expect(abs(fastAvg - 0.5) < 0.02) } @Test func mulScalesOutput() { let inDC: ConstantGeneratorInput = ConstantGeneratorInput(0.25) let testDC: ConstantGeneratorInput = ConstantGeneratorInput(0.5) let plain: Balance = Balance(in: ConstantGeneratorInput(0.25), test: ConstantGeneratorInput(0.5)) let scaled: Balance = Balance(in: inDC, test: testDC, mul: 0.5) let p: [Double] = Array(collectBlocks(plain, engine: engine, blocks: 400).suffix(engine.blockSize * 50)) let s: [Double] = Array(collectBlocks(scaled, engine: engine, blocks: 400).suffix(engine.blockSize * 50)) let pAvg: Double = p.reduce(0.0, +) / Double(p.count) let sAvg: Double = s.reduce(0.0, +) / Double(s.count) #expect(abs(sAvg - 0.5 * pAvg) < 0.01) } @Test func finiteWithNoise() { let bal: Balance = Balance(in: WhiteNoise.Audio(mul: 0.1), test: WhiteNoise.Audio(mul: 0.5)) let samples: [Double] = collectBlocks(bal, engine: engine, blocks: 200) #expect(samples.allSatisfy { $0.isFinite }) } // MARK: - smooth (decoupled gain lag) @Test func smoothDefaultIsBackwardCompatible() { // smooth == 0.0 (default) must reproduce the un-smoothed steady-state exactly. let bal: Balance = Balance(in: ConstantGeneratorInput(0.25), test: ConstantGeneratorInput(0.5), smooth: 0.0) let all: [Double] = collectBlocks(bal, engine: engine, blocks: 400) let tail: [Double] = Array(all.suffix(engine.blockSize * 50)) let avg: Double = tail.reduce(0.0, +) / Double(tail.count) #expect(abs(avg - 0.5) < 0.01) } @Test func smoothPreservesSteadyLevel() { // A gain lag must not change the converged level, only the path to it. let bal: Balance = Balance(in: ConstantGeneratorInput(0.25), test: ConstantGeneratorInput(0.5), smooth: 0.05) let all: [Double] = collectBlocks(bal, engine: engine, blocks: 600) let tail: [Double] = Array(all.suffix(engine.blockSize * 50)) let avg: Double = tail.reduce(0.0, +) / Double(tail.count) #expect(abs(avg - 0.5) < 0.01) } @Test func largerSmoothSlowsInitialResponse() { // From the unity-gain start, a quiet-in/loud-test pair needs gain > 1. With a gain // lag the early output rises more slowly, so its early-window RMS is lower; both // converge to the same tail level. let unsmoothed: Balance = Balance(in: OscSin.Audio(freq: 200.0, mul: 0.2), test: OscSin.Audio(freq: 200.0, mul: 0.5), smooth: 0.0) let smoothed: Balance = Balance(in: OscSin.Audio(freq: 200.0, mul: 0.2), test: OscSin.Audio(freq: 200.0, mul: 0.5), smooth: 0.1) let u: [Double] = collectBlocks(unsmoothed, engine: engine, blocks: 300) let s: [Double] = collectBlocks(smoothed, engine: engine, blocks: 300) let earlyWin: Int = engine.blockSize * 15 #expect(rmsValue(Array(u.prefix(earlyWin))) > rmsValue(Array(s.prefix(earlyWin)))) let target: Double = 0.5 / 2.0.squareRoot() #expect(abs(rmsValue(Array(u.suffix(engine.blockSize * 100))) - target) < 0.02) #expect(abs(rmsValue(Array(s.suffix(engine.blockSize * 100))) - target) < 0.02) } // MARK: - logDomain (decibel-domain gain glide) @Test func logDomainDCSteadyStateMatchesTestLevel() { let bal: Balance = Balance(in: ConstantGeneratorInput(0.25), test: ConstantGeneratorInput(0.5), smooth: 0.02, logDomain: true) let all: [Double] = collectBlocks(bal, engine: engine, blocks: 500) let tail: [Double] = Array(all.suffix(engine.blockSize * 50)) let avg: Double = tail.reduce(0.0, +) / Double(tail.count) #expect(abs(avg - 0.5) < 0.01) } @Test func logDomainQuietSineMatchedUpToTestRMS() { let quiet: OscSin.Audio = OscSin.Audio(freq: 200.0, mul: 0.2) let loud: OscSin.Audio = OscSin.Audio(freq: 200.0, mul: 0.5) let bal: Balance = Balance(in: quiet, test: loud, smooth: 0.02, logDomain: true) let samples: [Double] = collectBlocks(bal, engine: engine, blocks: 500) let tail: [Double] = Array(samples.suffix(engine.blockSize * 100)) let expected: Double = 0.5 / 2.0.squareRoot() #expect(abs(rmsValue(tail) - expected) < 0.02) } @Test func logDomainSilenceStaysSilentAndFinite() { let bal: Balance = Balance(in: ConstantGeneratorInput(0.0), test: ConstantGeneratorInput(0.0), logDomain: true) let samples: [Double] = collectBlocks(bal, engine: engine, blocks: 50) #expect(samples.allSatisfy { $0.isFinite }) #expect(rmsValue(samples) < 1e-9) } @Test func logDomainSilentInputStaysSilent() { let bal: Balance = Balance(in: ConstantGeneratorInput(0.0), test: OscSin.Audio(freq: 200.0, mul: 0.8), logDomain: true) let samples: [Double] = collectBlocks(bal, engine: engine, blocks: 100) #expect(samples.allSatisfy { $0.isFinite }) #expect(rmsValue(samples) < 1e-9) } @Test func logDomainSilentTestDrivesInputDown() { let bal: Balance = Balance(in: OscSin.Audio(freq: 200.0, mul: 0.5), test: ConstantGeneratorInput(0.0), smooth: 0.02, logDomain: true) let samples: [Double] = collectBlocks(bal, engine: engine, blocks: 300) let tail: [Double] = Array(samples.suffix(engine.blockSize * 50)) #expect(rmsValue(tail) < 0.01) #expect(samples.allSatisfy { $0.isFinite }) } @Test func logDomainMulScalesOutput() { let plain: Balance = Balance(in: ConstantGeneratorInput(0.25), test: ConstantGeneratorInput(0.5), smooth: 0.02, logDomain: true) let scaled: Balance = Balance(in: ConstantGeneratorInput(0.25), test: ConstantGeneratorInput(0.5), smooth: 0.02, logDomain: true, mul: 0.5) let p: [Double] = Array(collectBlocks(plain, engine: engine, blocks: 500).suffix(engine.blockSize * 50)) let s: [Double] = Array(collectBlocks(scaled, engine: engine, blocks: 500).suffix(engine.blockSize * 50)) let pAvg: Double = p.reduce(0.0, +) / Double(p.count) let sAvg: Double = s.reduce(0.0, +) / Double(s.count) #expect(abs(sAvg - 0.5 * pAvg) < 0.01) } @Test func logDomainFiniteWithNoise() { let bal: Balance = Balance(in: WhiteNoise.Audio(mul: 0.1), test: WhiteNoise.Audio(mul: 0.5), logDomain: true) let samples: [Double] = collectBlocks(bal, engine: engine, blocks: 200) #expect(samples.allSatisfy { $0.isFinite }) } }