300 lines
12 KiB
Swift
300 lines
12 KiB
Swift
// MIDIClockFollowerTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
/// Mutable sample-time counter for test injection.
|
|
/// A class (reference type) so it can be captured by the `timeProvider` closure
|
|
/// and shared with helper functions without `~Copyable` constraints.
|
|
private final class FakeClock: @unchecked Sendable {
|
|
var sampleTime: SampleTime = 0
|
|
func advance(by n: SampleTime) { sampleTime += n }
|
|
}
|
|
|
|
/// Build a `MIDIClockFollower` whose time source is a `FakeClock`.
|
|
private func makeFollower(
|
|
clock: LyrebirdClock,
|
|
fakeClock: FakeClock,
|
|
sampleRate: Double = 44100,
|
|
smoothingWindowTicks: Int = 24,
|
|
minBPM: Double = 20.0,
|
|
maxBPM: Double = 999.0
|
|
) -> MIDIClockFollower {
|
|
MIDIClockFollower(
|
|
clock: clock,
|
|
sampleRate: sampleRate,
|
|
smoothingWindowTicks: smoothingWindowTicks,
|
|
minBPM: minBPM,
|
|
maxBPM: maxBPM,
|
|
timeProvider: { fakeClock.sampleTime }
|
|
)
|
|
}
|
|
|
|
/// Dummy engine whose `currentTime` stays at 0. Only used to create a `LyrebirdClock`.
|
|
private func makeDummyEngine(sampleRate: Double = 44100) -> SynthesisEngine {
|
|
makeTestEngine(sampleRate: sampleRate)
|
|
}
|
|
|
|
/// Feed `count` clock ticks at an exact sample spacing.
|
|
/// Advances `fakeClock` by `spacingSamples` between ticks.
|
|
private func feedTicks(
|
|
_ follower: MIDIClockFollower,
|
|
fakeClock: FakeClock,
|
|
count: Int,
|
|
spacingSamples: SampleTime
|
|
) {
|
|
for _ in 0..<count {
|
|
follower.handle(MIDIMessage.clock)
|
|
fakeClock.advance(by: spacingSamples)
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
@Suite("MIDIClockFollower")
|
|
struct MIDIClockFollowerTests {
|
|
|
|
// MARK: - Tests
|
|
|
|
@Test func steadyTempo120BPM() {
|
|
// 120 BPM, 44100 Hz → tickSpacing = 44100 / (120/60 * 24) = 918.75
|
|
// Use integer spacing 919. Expected BPM from that spacing:
|
|
// bpm = 60 / (919/44100 * 24) ≈ 119.88
|
|
let spacingSamples: SampleTime = 919
|
|
let expectedBPM: Double = 44100.0 / (Double(spacingSamples) * 24.0) * 60.0
|
|
|
|
let engine: SynthesisEngine = makeDummyEngine()
|
|
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 100.0)
|
|
let fakeClock: FakeClock = FakeClock()
|
|
let follower: MIDIClockFollower = makeFollower(clock: clock, fakeClock: fakeClock)
|
|
|
|
// Feed one full window + 1 tick so the window can compute inter-tick deltas.
|
|
feedTicks(follower, fakeClock: fakeClock, count: 25, spacingSamples: spacingSamples)
|
|
|
|
#expect(abs(follower.estimatedBPM - expectedBPM) < 0.01)
|
|
#expect(abs(clock.bpm - expectedBPM) < 0.01)
|
|
}
|
|
|
|
@Test func tempoChange120To140() {
|
|
// 120 BPM → spacing = 919 samples; 140 BPM → spacing = 788 samples.
|
|
let spacing120: SampleTime = 919
|
|
let spacing140: SampleTime = 788
|
|
let expectedBPM140: Double = 44100.0 / (Double(spacing140) * 24.0) * 60.0
|
|
|
|
let engine: SynthesisEngine = makeDummyEngine()
|
|
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 100.0)
|
|
let fakeClock: FakeClock = FakeClock()
|
|
let follower: MIDIClockFollower = makeFollower(clock: clock, fakeClock: fakeClock)
|
|
|
|
// Fill the window at 120 BPM.
|
|
feedTicks(follower, fakeClock: fakeClock, count: 25, spacingSamples: spacing120)
|
|
|
|
// Feed another full window at 140 BPM to flush the 120 BPM ticks out.
|
|
feedTicks(follower, fakeClock: fakeClock, count: 25, spacingSamples: spacing140)
|
|
|
|
// Estimate should now track 140 BPM within 1 BPM (integer-spacing rounding).
|
|
#expect(abs(follower.estimatedBPM - expectedBPM140) < 1.0)
|
|
#expect(abs(clock.bpm - expectedBPM140) < 1.0)
|
|
}
|
|
|
|
@Test func startClearsHistory() {
|
|
let engine: SynthesisEngine = makeDummyEngine()
|
|
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 100.0)
|
|
let fakeClock: FakeClock = FakeClock()
|
|
let follower: MIDIClockFollower = makeFollower(clock: clock, fakeClock: fakeClock)
|
|
|
|
// Establish an estimate.
|
|
feedTicks(follower, fakeClock: fakeClock, count: 25, spacingSamples: 919)
|
|
#expect(follower.estimatedBPM > 0)
|
|
|
|
// .start clears history and sets isRunning.
|
|
follower.handle(MIDIMessage.start)
|
|
#expect(follower.isRunning == true)
|
|
#expect(follower.estimatedBPM == 0.0)
|
|
}
|
|
|
|
@Test func stopSetsIsRunningFalseAndDoesNotMutateBPM() {
|
|
let engine: SynthesisEngine = makeDummyEngine()
|
|
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 100.0)
|
|
let fakeClock: FakeClock = FakeClock()
|
|
let follower: MIDIClockFollower = makeFollower(clock: clock, fakeClock: fakeClock)
|
|
|
|
follower.handle(MIDIMessage.start)
|
|
feedTicks(follower, fakeClock: fakeClock, count: 25, spacingSamples: 919)
|
|
let bpmAfterTicks: Double = clock.bpm
|
|
|
|
follower.handle(MIDIMessage.stop)
|
|
#expect(follower.isRunning == false)
|
|
// clock.bpm must not change due to .stop.
|
|
#expect(clock.bpm == bpmAfterTicks)
|
|
}
|
|
|
|
@Test func ticksAfterStopDoNotMutateClockBPM() {
|
|
let engine: SynthesisEngine = makeDummyEngine()
|
|
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 100.0)
|
|
let fakeClock: FakeClock = FakeClock()
|
|
let follower: MIDIClockFollower = makeFollower(clock: clock, fakeClock: fakeClock)
|
|
|
|
follower.handle(MIDIMessage.start)
|
|
feedTicks(follower, fakeClock: fakeClock, count: 25, spacingSamples: 919)
|
|
let bpmAfterStart: Double = clock.bpm
|
|
|
|
follower.handle(MIDIMessage.stop)
|
|
// Feed a different tempo's worth of ticks while stopped — these must
|
|
// be ignored. spacingSamples=1378 corresponds to ~80 BPM at 44.1 kHz,
|
|
// far from the ~120 BPM established before .stop.
|
|
feedTicks(follower, fakeClock: fakeClock, count: 50, spacingSamples: 1378)
|
|
#expect(clock.bpm == bpmAfterStart)
|
|
#expect(follower.estimatedBPM == 0.0)
|
|
}
|
|
|
|
@Test func continueSetsIsRunningWithoutChangingClockBPM() {
|
|
let engine: SynthesisEngine = makeDummyEngine()
|
|
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
|
let fakeClock: FakeClock = FakeClock()
|
|
let follower: MIDIClockFollower = makeFollower(clock: clock, fakeClock: fakeClock)
|
|
|
|
follower.handle(MIDIMessage.start)
|
|
feedTicks(follower, fakeClock: fakeClock, count: 25, spacingSamples: 919)
|
|
let bpmBeforeStop: Double = clock.bpm
|
|
|
|
follower.handle(MIDIMessage.stop)
|
|
#expect(follower.isRunning == false)
|
|
|
|
// .continue resumes transport; clock.bpm is unchanged (no new ticks yet).
|
|
follower.handle(MIDIMessage.continue)
|
|
#expect(follower.isRunning == true)
|
|
#expect(clock.bpm == bpmBeforeStop)
|
|
|
|
// History was cleared on .continue.
|
|
#expect(follower.estimatedBPM == 0.0)
|
|
}
|
|
|
|
@Test func outOfBoundBPMClampedToMinBPM() {
|
|
// Gap of 10_000_000 samples at 44100 Hz:
|
|
// bpm = 60 / (10_000_000 / 44100 * 24) ≈ 0.011 BPM → clamp to minBPM 20.0.
|
|
let hugeSampleGap: SampleTime = 10_000_000
|
|
let engine: SynthesisEngine = makeDummyEngine()
|
|
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 100.0)
|
|
let fakeClock: FakeClock = FakeClock()
|
|
let follower: MIDIClockFollower = makeFollower(
|
|
clock: clock,
|
|
fakeClock: fakeClock,
|
|
smoothingWindowTicks: 2,
|
|
minBPM: 20.0,
|
|
maxBPM: 999.0
|
|
)
|
|
|
|
// First tick at t=0.
|
|
follower.handle(MIDIMessage.clock)
|
|
// Advance by huge gap.
|
|
fakeClock.advance(by: hugeSampleGap)
|
|
// Second tick — triggers estimate computation.
|
|
follower.handle(MIDIMessage.clock)
|
|
|
|
// Raw BPM ≈ 0.011, clamped to 20.0.
|
|
#expect(follower.estimatedBPM == 20.0)
|
|
#expect(clock.bpm == 20.0)
|
|
}
|
|
|
|
@Test func outOfBoundBPMClampedToMaxBPM() {
|
|
// Gap of 10 samples at 44100 Hz:
|
|
// bpm = 60 / (10 / 44100 * 24) = 11025 BPM → clamp to maxBPM 999.0.
|
|
let tinySampleGap: SampleTime = 10
|
|
let engine: SynthesisEngine = makeDummyEngine()
|
|
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 100.0)
|
|
let fakeClock: FakeClock = FakeClock()
|
|
let follower: MIDIClockFollower = makeFollower(
|
|
clock: clock,
|
|
fakeClock: fakeClock,
|
|
smoothingWindowTicks: 2,
|
|
minBPM: 20.0,
|
|
maxBPM: 999.0
|
|
)
|
|
|
|
follower.handle(MIDIMessage.clock)
|
|
fakeClock.advance(by: tinySampleGap)
|
|
follower.handle(MIDIMessage.clock)
|
|
|
|
#expect(follower.estimatedBPM == 999.0)
|
|
#expect(clock.bpm == 999.0)
|
|
}
|
|
|
|
@Test func nonClockMessagesAreIgnored() {
|
|
let engine: SynthesisEngine = makeDummyEngine()
|
|
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 100.0)
|
|
let fakeClock: FakeClock = FakeClock()
|
|
let follower: MIDIClockFollower = makeFollower(clock: clock, fakeClock: fakeClock)
|
|
|
|
// Feed irrelevant messages — estimate stays 0.0 and clock.bpm unchanged.
|
|
follower.handle(MIDIMessage.noteOn(channel: 0, note: 60, velocity: 100))
|
|
follower.handle(MIDIMessage.controlChange(channel: 0, controller: 7, value: 64))
|
|
follower.handle(MIDIMessage.pitchBend(channel: 0, value: 0))
|
|
follower.handle(MIDIMessage.activeSensing)
|
|
|
|
#expect(follower.estimatedBPM == 0.0)
|
|
#expect(clock.bpm == 100.0)
|
|
}
|
|
|
|
@Test func resetClearsHistoryWithoutAffectingClockBPM() {
|
|
let engine: SynthesisEngine = makeDummyEngine()
|
|
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 100.0)
|
|
let fakeClock: FakeClock = FakeClock()
|
|
let follower: MIDIClockFollower = makeFollower(clock: clock, fakeClock: fakeClock)
|
|
|
|
feedTicks(follower, fakeClock: fakeClock, count: 25, spacingSamples: 919)
|
|
let bpmAfterTicks: Double = clock.bpm
|
|
#expect(follower.estimatedBPM > 0)
|
|
|
|
follower.reset()
|
|
|
|
// Estimate cleared; clock.bpm and isRunning are unchanged. The follower
|
|
// defaults to `isRunning == true` so free-running clocks work without
|
|
// transport messages.
|
|
#expect(follower.estimatedBPM == 0.0)
|
|
#expect(clock.bpm == bpmAfterTicks)
|
|
#expect(follower.isRunning == true)
|
|
}
|
|
|
|
@Test func bpmNotRepushedWhenStable() {
|
|
// After stabilization, feeding identical-spaced ticks should not cause
|
|
// `clock.bpm` to drift (the epsilon gate suppresses redundant writes).
|
|
let spacingSamples: SampleTime = 919
|
|
let engine: SynthesisEngine = makeDummyEngine()
|
|
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 100.0)
|
|
let fakeClock: FakeClock = FakeClock()
|
|
let follower: MIDIClockFollower = makeFollower(clock: clock, fakeClock: fakeClock)
|
|
|
|
feedTicks(follower, fakeClock: fakeClock, count: 25, spacingSamples: spacingSamples)
|
|
let stabilisedBPM: Double = clock.bpm
|
|
|
|
// Feed 24 more identical-spaced ticks — clock.bpm should stay within epsilon.
|
|
feedTicks(follower, fakeClock: fakeClock, count: 24, spacingSamples: spacingSamples)
|
|
|
|
#expect(abs(clock.bpm - stabilisedBPM) < 0.01)
|
|
}
|
|
|
|
@Test func continueRoundTrip() {
|
|
// Verify .continue (0xFB) parse and bytes() round-trip correctly.
|
|
let msg: MIDIMessage = MIDIMessage.from(status: 0xFB)
|
|
#expect(msg == MIDIMessage.continue)
|
|
let (status, d1, d2) = msg.bytes()
|
|
#expect(status == 0xFB)
|
|
#expect(d1 == 0)
|
|
#expect(d2 == 0)
|
|
#expect(MIDIMessage.from(status: status, data1: d1, data2: d2) == MIDIMessage.continue)
|
|
}
|
|
|
|
@Test func systemRealtimeIncludesContinue() {
|
|
// Verify .continue is correctly identified and distinct from adjacent status bytes.
|
|
#expect(MIDIMessage.from(status: 0xFB) == MIDIMessage.continue)
|
|
#expect(MIDIMessage.from(status: 0xFB) != MIDIMessage.start)
|
|
#expect(MIDIMessage.from(status: 0xFB) != MIDIMessage.stop)
|
|
#expect(MIDIMessage.from(status: 0xFB) != MIDIMessage.clock)
|
|
}
|
|
}
|