// SpectralMagnitudeMapTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("SpectralMagnitudeMap") struct SpectralMagnitudeMapTests { /// Identity transfer (linear ramp 0 → 1) should leave magnitudes unchanged /// within float precision. @Test func identityTransferPreservesMagnitudes() { let fftSize: Int = 64 let halfSize: Int = fftSize / 2 let curve: LyrebirdBuffer = LyrebirdBuffer(count: 256) curve.fill { i, n in Double(i) / Double(n - 1) } let map: SpectralMagnitudeMap = SpectralMagnitudeMap(buffer: curve) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) // Populate bins with known magnitudes; mag at testBin sets the maximum. let inputs: [Float] = [0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0] for k: Int in 1..= 0.5 ? Double(i) / Double(n - 1) : 0.0 } let map: SpectralMagnitudeMap = SpectralMagnitudeMap(buffer: curve) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) // Set max at one bin; everything else proportionally. buf[magnitude: 5] = 1.0 // max → normalised 1.0 → above gate buf[magnitude: 10] = 0.2 // 0.2 → below gate buf[magnitude: 15] = 0.6 // 0.6 → above gate map.process(spectral: buf, engine: makeTestEngine()) #expect(buf[magnitude: 5] > 0.5, "Loud bin 5 should pass through, got \(buf[magnitude: 5])") #expect(buf[magnitude: 10] == 0.0, "Quiet bin 10 should be gated, got \(buf[magnitude: 10])") #expect(buf[magnitude: 15] > 0.0, "Above-gate bin 15 should pass, got \(buf[magnitude: 15])") } }