112 lines
4.2 KiB
Swift
112 lines
4.2 KiB
Swift
// BeatTrackFeatureTests.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 — feature-fed (feature)")
|
|
struct BeatTrackFeatureTests {
|
|
|
|
let engine: SynthesisEngine
|
|
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
|
let blockSize: Int = 512
|
|
let sampleRate: Double = 44100
|
|
|
|
init() throws {
|
|
engine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
|
try engine.start()
|
|
}
|
|
|
|
@Test func rateIsControl() {
|
|
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.0)
|
|
#expect(gen.rate == .control)
|
|
}
|
|
|
|
@Test func fourOutputRows() {
|
|
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.0)
|
|
#expect(gen.outputChannelCount == 4)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(out.count == 4)
|
|
for row: Int in 0..<4 {
|
|
#expect(Array(out[row].prefix(blockSize)).count == blockSize)
|
|
}
|
|
}
|
|
|
|
/// Each output row is flat across the block (block-quantised triggers / tempo).
|
|
@Test func rowsAreFlat() {
|
|
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.5)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
for row: Int in 0..<4 {
|
|
let first: Double = out[row][0]
|
|
for i: Int in 0..<blockSize {
|
|
#expect(out[row][i] == first, "row \(row) not flat at \(i)")
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Pre-lock tempo (row 3) defaults to the preferred BPM.
|
|
@Test func preLockTempoIsPreferred() {
|
|
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.0)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(abs(out[3][0] - beatTrackPreferredBPM) < 1e-9)
|
|
}
|
|
|
|
/// A 2 Hz impulse feature (120 BPM) drives the tempo output to ≈120.
|
|
@Test func locksToImpulseFeature() {
|
|
// 2 Hz impulse → beat every 0.5 s → 120 BPM.
|
|
let feature: Impulse.Audio = Impulse.Audio(freq: 2.0)
|
|
let gen: BeatTrack.Control = BeatTrack.Control(feature: feature)
|
|
var lastTempo: Double = 0.0
|
|
// ~9 s of audio at 512-sample blocks.
|
|
for _ in 0..<800 {
|
|
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.10,
|
|
"expected ≈120 BPM, got \(lastTempo)")
|
|
}
|
|
|
|
/// The `odf:` enum init and the `feature:` convenience init are equivalent.
|
|
@Test func enumInitMatchesConvenience() {
|
|
let feature: Impulse.Audio = Impulse.Audio(freq: 2.0)
|
|
let gen: BeatTrack.Control = BeatTrack.Control(odf: .feature(feature))
|
|
var lastTempo: Double = 0.0
|
|
for _ in 0..<800 {
|
|
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.10,
|
|
"enum-init feature path: expected ≈120 BPM, got \(lastTempo)")
|
|
}
|
|
|
|
/// Silence: no NaN/Inf in any row, tempo finite.
|
|
@Test func silenceProducesNoNaN() {
|
|
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.0)
|
|
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, "row \(row) sample \(i) not finite")
|
|
}
|
|
}
|
|
advanceBlock(engine)
|
|
}
|
|
}
|
|
|
|
@Test func floatValueReturnsBeatRow() {
|
|
let gen: BeatTrack.Control = BeatTrack.Control(feature: 0.0)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
|
}
|
|
}
|