// SpectralHarmonicEnhanceTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("SpectralHarmonicEnhance") struct SpectralHarmonicEnhanceTests { /// Single source bin produces magnitudes at expected harmonic targets with 1/i scaling. @Test func singleBinProducesHarmonics() { let fftSize: Int = 256 // halfSize = 128 — plenty of headroom for 4 harmonics of bin 8 at ratio 2 let sourceBin: Int = 8 let sourceMag: Float = 1.0 let strength: Float = 0.5 let ratio: Float = 2.0 let numPartials: Int = 3 let enh: SpectralHarmonicEnhance = SpectralHarmonicEnhance( fftSize: fftSize, numPartials: Double(numPartials), ratio: Double(ratio), strength: Double(strength) ) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) buf[magnitude: sourceBin] = sourceMag enh.process(spectral: buf, engine: makeTestEngine()) // Source bin keeps its mag (only added-to never zeroed by the algorithm). // Targets at i=1: bin 16 → += 1.0 * 0.5 / 1 = 0.5 // Targets at i=2: bin 32 → += 1.0 * 0.5 / 2 = 0.25 // Targets at i=3: bin 48 → += 1.0 * 0.5 / 3 ≈ 0.1667 #expect(abs(buf[magnitude: 16] - 0.5) < 1e-5, "Harmonic 1 at bin 16 should be 0.5, got \(buf[magnitude: 16])") #expect(abs(buf[magnitude: 32] - 0.25) < 1e-5, "Harmonic 2 at bin 32 should be 0.25, got \(buf[magnitude: 32])") #expect(abs(buf[magnitude: 48] - (0.5 / 3.0)) < 1e-5, "Harmonic 3 at bin 48 should be 0.1667, got \(buf[magnitude: 48])") } /// Out-of-range targets must be silently skipped — no crash, no out-of-bounds writes. @Test func outOfRangeHarmonicsSkipped() { let fftSize: Int = 64 // halfSize = 32 let sourceBin: Int = 12 // ratio 2 × 12 = 24, ratio 2 × 24 = 48 (out of range) let enh: SpectralHarmonicEnhance = SpectralHarmonicEnhance(fftSize: fftSize, numPartials: 4, ratio: 2.0, strength: 1.0) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) buf[magnitude: sourceBin] = 1.0 // Should not crash and should produce only the in-range partial. enh.process(spectral: buf, engine: makeTestEngine()) #expect(buf[magnitude: 24] > 0, "First harmonic at bin 24 should have been added") // Higher harmonics at 48, 72, 96 are out of range and silently skipped. } /// `ratio <= 1.0` produces no change (start bin collapses). @Test func ratioOneIsNoOp() { let fftSize: Int = 64 let enh: SpectralHarmonicEnhance = SpectralHarmonicEnhance(fftSize: fftSize, numPartials: 4, ratio: 1.0, strength: 1.0) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) buf[magnitude: 8] = 1.0 buf[magnitude: 16] = 0.0 enh.process(spectral: buf, engine: makeTestEngine()) #expect(buf[magnitude: 8] == 1.0, "Source bin must not change with ratio=1") #expect(buf[magnitude: 16] == 0.0, "No harmonic should be added with ratio=1") } /// Live parameter update path. Values are stored as `Float` internally; the /// `Double`-typed accessor returns the Float-rounded approximation, so we /// compare with a tolerance. @Test func parametersAreLive() { let enh: SpectralHarmonicEnhance = SpectralHarmonicEnhance(fftSize: 64, numPartials: 4, ratio: 2.0, strength: 0.2) #expect(abs(enh.numPartials - 4.0) < 1e-5) #expect(abs(enh.ratio - 2.0) < 1e-5) #expect(abs(enh.strength - 0.2) < 1e-5) enh.numPartials = 6 enh.ratio = 3.0 enh.strength = 0.5 #expect(abs(enh.numPartials - 6.0) < 1e-5) #expect(abs(enh.ratio - 3.0) < 1e-5) #expect(abs(enh.strength - 0.5) < 1e-5) } }