// SpectralBrickwallTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("SpectralBrickwall") struct SpectralBrickwallTests { private static let sampleRate: Double = 48_000 // MARK: - Helpers /// Fill every bin (DC, complex, Nyquist) with magnitude `1.0`. private static func fillAllBins(_ buf: SpectralBuffer) { buf.dcValue = 1.0 buf.nyquistValue = 1.0 let halfSize: Int = buf.fftSize / 2 for k: Int in 1.. Int { let halfSize: Int = fftSize / 2 let f: Double = hz * Double(fftSize) / sampleRate if f <= 0 { return 0 } if f >= Double(halfSize) { return halfSize } return Int(f.rounded()) } // MARK: - .lp @Test func lpZeroesAboveCutoffPreservesBelow() { let fftSize: Int = 64 let cutoffHz: Double = 6_000 // bin 8 at SR 48k, fft 64 (8 * 750 = 6000) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) Self.fillAllBins(buf) let f: SpectralBrickwall = SpectralBrickwall( fftSize: fftSize, sampleRate: Self.sampleRate, mode: .lp, highHz: cutoffHz ) f.process(spectral: buf, engine: makeTestEngine()) let cutoffBin: Int = Self.binAt(cutoffHz, fftSize: fftSize) let halfSize: Int = fftSize / 2 // Below + at cutoff preserved. #expect(buf.dcValue == 1.0) for k: Int in 1...cutoffBin { if k < halfSize { #expect(buf[magnitude: k] == 1.0, "bin \(k) should be preserved") } } // Above cutoff zeroed (complex bins). for k: Int in (cutoffBin + 1)..= 1 && cutoffBin < halfSize { #expect(buf[phase: cutoffBin] == 0.5, "phase should be untouched") } } // MARK: - .hp @Test func hpZeroesBelowCutoffPreservesAbove() { let fftSize: Int = 64 let cutoffHz: Double = 6_000 // bin 8 let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) Self.fillAllBins(buf) let f: SpectralBrickwall = SpectralBrickwall( fftSize: fftSize, sampleRate: Self.sampleRate, mode: .hp, lowHz: cutoffHz ) f.process(spectral: buf, engine: makeTestEngine()) let cutoffBin: Int = Self.binAt(cutoffHz, fftSize: fftSize) let halfSize: Int = fftSize / 2 // DC zeroed (below cutoff). #expect(buf.dcValue == 0.0) // Below cutoff zeroed. for k: Int in 1..= 1 { #expect(buf[magnitude: k] == 1.0, "bin \(k) should be preserved") } } #expect(buf.nyquistValue == 1.0) } // MARK: - .bp @Test func bpKeepsBandZeroesEnds() { let fftSize: Int = 128 let lowHz: Double = 3_000 // bin 8 let highHz: Double = 9_000 // bin 24 let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) Self.fillAllBins(buf) let f: SpectralBrickwall = SpectralBrickwall( fftSize: fftSize, sampleRate: Self.sampleRate, mode: .bp, lowHz: lowHz, highHz: highHz ) f.process(spectral: buf, engine: makeTestEngine()) let bLow: Int = Self.binAt(lowHz, fftSize: fftSize) let bHigh: Int = Self.binAt(highHz, fftSize: fftSize) let halfSize: Int = fftSize / 2 #expect(buf.dcValue == 0.0) for k: Int in 1..= 1 && k < halfSize { #expect(buf[magnitude: k] == 0.0) } } for k: Int in (bHigh + 1)..