// OnsetDetectorTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth /// Convenience: drive a detector directly with a sequence of magnitude profiles. /// `magsByHop[h][k]` is the magnitude for bin `k` (`k` in `1.. Float ) { let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) let halfSize: Int = fftSize / 2 for h: Int in 0.. 0) } } // MARK: - Complex-domain ODF @Suite("OnsetDetector .complexDomain") struct OnsetDetectorComplexDomainTests { /// Phase-stable (predictable rotation) input never triggers. @Test func stablePhaseDoesNotFire() { let fftSize: Int = 64 let halfSize: Int = fftSize / 2 let medianWindow: Int = 5 let detector: OnsetDetector = OnsetDetector( fftSize: fftSize, hopSize: 16, method: .complexDomain, threshold: 2.0, medianWindowFrames: medianWindow, minHopsBetweenOnsets: 0 ) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) let phaseStep: Float = 0.5 // Drive each bin with constant magnitude and constant phase increment. var phase: Float = 0 let totalHops: Int = medianWindow + 30 let serialBefore: UInt64 = detector._loadOnsetSerial() for _ in 0.. Float.pi { phase -= 2 * Float.pi } detector.process(spectral: buf, engine: makeTestEngine()) } #expect(detector._loadOnsetSerial() == serialBefore, "Phase-stable input should not produce onsets") } /// A sudden magnitude jump on phase-stable input fires one onset. @Test func magnitudeJumpFires() { let fftSize: Int = 64 let halfSize: Int = fftSize / 2 let medianWindow: Int = 5 let detector: OnsetDetector = OnsetDetector( fftSize: fftSize, hopSize: 16, method: .complexDomain, threshold: 1.5, medianWindowFrames: medianWindow, minHopsBetweenOnsets: 100 ) let buf: SpectralBuffer = SpectralBuffer(fftSize: fftSize) // Warm up with quiet, phase-stable signal. var phase: Float = 0 let phaseStep: Float = 0.3 for _ in 0..<(medianWindow + 5) { for k: Int in 1.. Float.pi { phase -= 2 * Float.pi } detector.process(spectral: buf, engine: makeTestEngine()) } let baseline: UInt64 = detector._loadOnsetSerial() // One hop with much larger magnitude — same phase progression. for k: Int in 1.. Float.pi { phase -= 2 * Float.pi } detector.process(spectral: buf, engine: makeTestEngine()) #expect(detector._loadOnsetSerial() == baseline &+ 1) } } // MARK: - .Control output @Suite("OnsetDetector.Control output") struct OnsetDetectorControlTests { let engine: SynthesisEngine var graph: SynthGraph { makeSynthGraph(engine: engine) } let blockSize: Int = 64 init() throws { engine = makeTestEngine(blockSize: 64, sampleRate: 44100) try engine.start() } @Test func rateIsControl() { let detector: OnsetDetector = OnsetDetector(fftSize: 64, hopSize: 16) let ctl: OnsetDetector.Control = OnsetDetector.Control(detector: detector) #expect(ctl.rate == .control) } @Test func outputsZeroBeforeAnyOnset() { let detector: OnsetDetector = OnsetDetector(fftSize: 64, hopSize: 16) let ctl: OnsetDetector.Control = OnsetDetector.Control(detector: detector) let result: [[Double]] = ctl.calculatedSamples(graph: graph, count: blockSize) for i: Int in 0.. 0.5 { sawPulse = true; break } advanceBlock(engine) } #expect(detector._loadOnsetSerial() > baselineSerial, "Detector should fire at least one onset on the level step") #expect(sawPulse, "Control should expose at least one ≤0→>0 pulse on the level step") } } // MARK: - Registration precondition @Suite("OnsetDetector.Control registration check") struct OnsetDetectorRegistrationTests { /// Constructing `OnsetDetector.Control(detector:analysis:)` against a /// `SpectralChain.Audio` that does NOT include the detector must trap — a /// silent "no triggers ever" outcome would be much harder to debug. @Test func mismatchedAnalysisIsAccepted() { // Smoke test of the happy path — wrong-analysis construction can't be // probed via Swift Testing without `expectCrash` infrastructure, so we // just confirm matching analysis succeeds. A misuse traps at runtime // via the precondition in `OnsetDetector.Control(detector:analysis:)`. let detector: OnsetDetector = OnsetDetector(fftSize: 64, hopSize: 16) let analysis: SpectralChain.Audio = SpectralChain.Audio( input: Double(0.1), fftSize: 64, hopSize: 16, stages: [.mono(detector)], analyzeOnly: true ) _ = OnsetDetector.Control(detector: detector, analysis: analysis) } }