90 lines
3.5 KiB
Swift
90 lines
3.5 KiB
Swift
// BeatTrackSpectralFluxTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("BeatTrack.Control — FFT-fed (spectralFlux)")
|
|
struct BeatTrackSpectralFluxTests {
|
|
|
|
let sampleRate: Double = 44100
|
|
let fftSize: Int = 1024
|
|
let hopSize: Int = 512
|
|
let blockSize: Int = 512
|
|
|
|
private func makeEngine() throws -> SynthesisEngine {
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
|
try engine.start()
|
|
return engine
|
|
}
|
|
|
|
@Test func rateIsControl() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
defer { engine.stop() }
|
|
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
|
input: Double(0.0), fftSize: fftSize, hopSize: hopSize, analyzeOnly: true)
|
|
let gen: BeatTrack.Control = BeatTrack.Control(analysis: chain)
|
|
#expect(gen.rate == .control)
|
|
#expect(gen.outputChannelCount == 4)
|
|
}
|
|
|
|
/// The `odf:` enum init and the `analysis:` convenience init are equivalent.
|
|
@Test func enumInitMatchesConvenience() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
defer { engine.stop() }
|
|
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
|
input: Double(0.0), fftSize: fftSize, hopSize: hopSize, analyzeOnly: true)
|
|
let gen: BeatTrack.Control = BeatTrack.Control(odf: .spectralFlux(analysis: chain))
|
|
#expect(gen.rate == .control)
|
|
#expect(gen.outputChannelCount == 4)
|
|
}
|
|
|
|
/// FFT-fed path: a 2 Hz click track (120 BPM) drives spectral flux that locks
|
|
/// the tempo near 120. Looser tolerance than the feature-fed path due to FFT
|
|
/// time-smearing of the onset.
|
|
@Test func locksToClickTrack() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
defer { engine.stop() }
|
|
|
|
// Broadband clicks at 2 Hz → spectral flux spikes at the beat rate.
|
|
let clicks: Impulse.Audio = Impulse.Audio(freq: 2.0)
|
|
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
|
input: clicks, fftSize: fftSize, hopSize: hopSize, analyzeOnly: true)
|
|
let gen: BeatTrack.Control = BeatTrack.Control(analysis: chain)
|
|
|
|
var lastTempo: Double = 0.0
|
|
for _ in 0..<900 {
|
|
let g: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
|
lastTempo = out[3][0]
|
|
advanceBlock(engine)
|
|
}
|
|
#expect(abs(lastTempo - 120.0) < 120.0 * 0.15,
|
|
"expected ≈120 BPM from click track, got \(lastTempo)")
|
|
}
|
|
|
|
/// Silent input: no NaN/Inf, tempo finite (holds the default).
|
|
@Test func silenceProducesNoNaN() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
defer { engine.stop() }
|
|
let chain: SpectralChain.Audio = SpectralChain.Audio(
|
|
input: Double(0.0), fftSize: fftSize, hopSize: hopSize, analyzeOnly: true)
|
|
let gen: BeatTrack.Control = BeatTrack.Control(analysis: chain)
|
|
for _ in 0..<200 {
|
|
let g: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
|
for row: Int in 0..<4 {
|
|
for i: Int in 0..<blockSize {
|
|
#expect(out[row][i].isFinite)
|
|
}
|
|
}
|
|
advanceBlock(engine)
|
|
}
|
|
}
|
|
}
|