159 lines
6.7 KiB
Swift
159 lines
6.7 KiB
Swift
// BeatTrackerCoreTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
import Foundation
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@Suite("BeatTrackerCore")
|
||
struct BeatTrackerCoreTests {
|
||
|
||
let sampleRate: Double = 44100
|
||
/// Samples per feature at the default feature rate — drive one feature per `processBlock`.
|
||
var featureHop: Int { max(1, Int((sampleRate / beatTrackFeatureRateHz).rounded())) }
|
||
|
||
private func makeCore(
|
||
minBPM: Double = beatTrackMinBPM,
|
||
maxBPM: Double = beatTrackMaxBPM,
|
||
preferredBPM: Double = beatTrackPreferredBPM
|
||
) -> BeatTrackerCore {
|
||
BeatTrackerCore(
|
||
minBPM: minBPM,
|
||
maxBPM: maxBPM,
|
||
preferredBPM: preferredBPM,
|
||
windowSeconds: beatTrackMaxWindowSeconds,
|
||
featureRateHz: beatTrackFeatureRateHz
|
||
)
|
||
}
|
||
|
||
/// Feed `features` features (one per block), firing an impulse ODF every
|
||
/// `periodFeatures`-th feature. Returns counts of each trigger.
|
||
@discardableResult
|
||
private func driveImpulseTrain(
|
||
_ core: BeatTrackerCore,
|
||
periodFeatures: Int,
|
||
features: Int
|
||
) -> (beats: Int, eighths: Int, sixteenths: Int) {
|
||
var beats: Int = 0
|
||
var eighths: Int = 0
|
||
var sixteenths: Int = 0
|
||
for f: Int in 0..<features {
|
||
let odf: Double = (f % periodFeatures == 0) ? 1.0 : 0.0
|
||
core.processBlock(odf: odf, sampleCount: featureHop, sampleRate: sampleRate)
|
||
if core.beatTriggered { beats += 1 }
|
||
if core.eighthTriggered { eighths += 1 }
|
||
if core.sixteenthTriggered { sixteenths += 1 }
|
||
}
|
||
return (beats, eighths, sixteenths)
|
||
}
|
||
|
||
// MARK: - Tempo lock
|
||
|
||
/// Impulse train at 90 features-period-ish locks the tempo near the
|
||
/// corresponding BPM (below the 120 preference centre).
|
||
@Test func locksTempoBelowPreferred() {
|
||
let core: BeatTrackerCore = makeCore()
|
||
// 57 features ≈ 60·86/57 ≈ 90.5 BPM.
|
||
let period: Int = 57
|
||
driveImpulseTrain(core, periodFeatures: period, features: 800)
|
||
let expected: Double = 60.0 * beatTrackFeatureRateHz / Double(period)
|
||
#expect(abs(core.tempoBPM - expected) < expected * 0.06,
|
||
"expected ≈\(expected) BPM, got \(core.tempoBPM)")
|
||
}
|
||
|
||
/// Impulse train above the preference centre also locks — guards against a
|
||
/// hard-coded / preferred-only result.
|
||
@Test func locksTempoAbovePreferred() {
|
||
let core: BeatTrackerCore = makeCore()
|
||
// 36 features ≈ 60·86/36 ≈ 143.3 BPM.
|
||
let period: Int = 36
|
||
driveImpulseTrain(core, periodFeatures: period, features: 800)
|
||
let expected: Double = 60.0 * beatTrackFeatureRateHz / Double(period)
|
||
#expect(abs(core.tempoBPM - expected) < expected * 0.06,
|
||
"expected ≈\(expected) BPM, got \(core.tempoBPM)")
|
||
}
|
||
|
||
/// Two distinct inputs must produce two distinct tempo estimates.
|
||
@Test func distinctInputsGiveDistinctTempi() {
|
||
let slow: BeatTrackerCore = makeCore()
|
||
let fast: BeatTrackerCore = makeCore()
|
||
driveImpulseTrain(slow, periodFeatures: 57, features: 800)
|
||
driveImpulseTrain(fast, periodFeatures: 36, features: 800)
|
||
#expect(fast.tempoBPM > slow.tempoBPM + 30.0,
|
||
"fast \(fast.tempoBPM) should clearly exceed slow \(slow.tempoBPM)")
|
||
}
|
||
|
||
// MARK: - Beat spacing / subdivisions
|
||
|
||
@Test func beatCountMatchesPeriod() {
|
||
let core: BeatTrackerCore = makeCore()
|
||
let period: Int = 43 // ≈120 BPM
|
||
let features: Int = 860 // ~20 beats
|
||
let counts: (beats: Int, eighths: Int, sixteenths: Int) =
|
||
driveImpulseTrain(core, periodFeatures: period, features: features)
|
||
let expectedBeats: Double = Double(features) / Double(period)
|
||
#expect(abs(Double(counts.beats) - expectedBeats) <= 2.0,
|
||
"expected ≈\(expectedBeats) beats, got \(counts.beats)")
|
||
}
|
||
|
||
@Test func subdivisionsFireFasterThanBeat() {
|
||
let core: BeatTrackerCore = makeCore()
|
||
let counts: (beats: Int, eighths: Int, sixteenths: Int) =
|
||
driveImpulseTrain(core, periodFeatures: 43, features: 860)
|
||
// Eighth ≈ 2×, sixteenth ≈ 4× the beat rate.
|
||
#expect(abs(Double(counts.eighths) - 2.0 * Double(counts.beats)) <= 3.0,
|
||
"eighths \(counts.eighths) ≉ 2× beats \(counts.beats)")
|
||
#expect(abs(Double(counts.sixteenths) - 4.0 * Double(counts.beats)) <= 5.0,
|
||
"sixteenths \(counts.sixteenths) ≉ 4× beats \(counts.beats)")
|
||
}
|
||
|
||
// MARK: - Edge cases
|
||
|
||
/// Silence: tempo holds the pre-lock default, stays finite, and the phase
|
||
/// free-runs (beats still fire).
|
||
@Test func silenceHoldsTempoAndFreeRuns() {
|
||
let core: BeatTrackerCore = makeCore()
|
||
var beats: Int = 0
|
||
for _ in 0..<800 {
|
||
core.processBlock(odf: 0.0, sampleCount: featureHop, sampleRate: sampleRate)
|
||
if core.beatTriggered { beats += 1 }
|
||
}
|
||
#expect(core.tempoBPM.isFinite)
|
||
#expect(abs(core.tempoBPM - beatTrackPreferredBPM) < 1e-9,
|
||
"silence should hold the preferred tempo, got \(core.tempoBPM)")
|
||
#expect(beats > 0, "phase should free-run at the default tempo")
|
||
}
|
||
|
||
/// Constant (DC) ODF: whitening removes the offset, so no NaN/Inf and tempo
|
||
/// stays finite and sane (no degenerate lock).
|
||
@Test func dcInputStaysFinite() {
|
||
let core: BeatTrackerCore = makeCore()
|
||
for _ in 0..<800 {
|
||
core.processBlock(odf: 0.5, sampleCount: featureHop, sampleRate: sampleRate)
|
||
}
|
||
#expect(core.tempoBPM.isFinite)
|
||
#expect(core.tempoBPM >= beatTrackMinBPM && core.tempoBPM <= beatTrackMaxBPM,
|
||
"tempo \(core.tempoBPM) out of bounds for DC input")
|
||
}
|
||
|
||
/// Blocks longer than one feature hop still advance the tracker (multiple
|
||
/// features per block) and lock tempo. Each big block spans 4 feature hops;
|
||
/// an onset every 11 big blocks → 44 features ≈ 117 BPM.
|
||
@Test func longBlockEmitsMultipleFeaturesAndLocks() {
|
||
let core: BeatTrackerCore = makeCore()
|
||
let bigBlock: Int = featureHop * 4
|
||
let periodBlocks: Int = 11
|
||
for b: Int in 0..<260 {
|
||
let odf: Double = (b % periodBlocks == 0) ? 1.0 : 0.0
|
||
core.processBlock(odf: odf, sampleCount: bigBlock, sampleRate: sampleRate)
|
||
}
|
||
let expected: Double = 60.0 * beatTrackFeatureRateHz / Double(periodBlocks * 4)
|
||
#expect(core.tempoBPM.isFinite)
|
||
#expect(abs(core.tempoBPM - expected) < expected * 0.08,
|
||
"expected ≈\(expected) BPM, got \(core.tempoBPM)")
|
||
}
|
||
}
|