Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/TempoTests.swift
T
josh 947dfe427a Add looper pedal: beat-quantized transport, undo, reverse, timeline
Extend the looper into a double-width tempo-aware pedal:

- Beat-quantized transport: record start and loop close both snap to the
  next beat via a LyrebirdRoutine waiting on the shared clock; loop length
  is an exact whole-beat span. Falls back to immediate/unquantized when no
  clock is present.
- Loop-length read-outs (frames/seconds/beats), transport state, and a
  sample-clock-derived play-head position for the UI timeline.
- Overdub undo (pre-dub snapshot + restore), reverse playback (live rate
  flip), and feedback-decay overdub (overdub preLevel via a Feedback knob).
- Inject the shared LyrebirdClock + SynthesisEngine into Pedal via
  PedalBoard/ChainRouter so tempo-aware pedals can reach them.
- New double-width LooperTimelineView (state badge, length read-out,
  sweeping play-head) wired into PedalView via Pedal.widthUnits.

Concurrency hardening from RT-safety review: a transportGeneration counter
drops superseded deferred beat/undo callbacks that LyrebirdRoutine.stop()
cannot recall once enqueued; the undo restore is deferred past the RT
overdub gate-close; transport routines use non-blocking stop().

Note: this commit also snapshots pre-existing uncommitted work-in-progress
already in the tree (MIDI control refactor, Tuner, Tempo, preset changes),
which is interleaved with the looper changes through shared files and could
not be split into a separately-building commit.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 09:22:20 -07:00

84 lines
3.2 KiB
Swift

// TempoTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
@testable import LyrebirdShoeGaze
@MainActor
@Suite("ShoeGaze Tempo")
struct TempoTests {
private func makeShoeGaze(duration: Double = 0.1) -> (ShoeGaze, URL) {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: duration, numAudioBuses: 64)
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine, inputSource: .testLoop)
return (shoeGaze, url)
}
@Test func clockDefaultsToStandardTempo() throws {
let (shoeGaze, url): (ShoeGaze, URL) = makeShoeGaze()
defer { try? FileManager.default.removeItem(at: url) }
#expect(shoeGaze.clock.bpm == ShoeGaze.defaultTempoBpm)
}
@Test func tapTempoAtOneSecondIntervalsSetsSixtyBpm() throws {
let (shoeGaze, url): (ShoeGaze, URL) = makeShoeGaze()
defer { try? FileManager.default.removeItem(at: url) }
// Simulate taps exactly 1s apart by manipulating the tap history directly —
// real taps come from wall-clock `Date()`, so back-date synthetic timestamps
// instead of sleeping the test for real seconds.
let now: Date = Date()
shoeGaze.tapTimestamps = [
now.addingTimeInterval(-3.0),
now.addingTimeInterval(-2.0),
now.addingTimeInterval(-1.0),
]
shoeGaze.tapTempo() // landing "now", completing four 1s-spaced taps
#expect(abs(shoeGaze.clock.bpm - 60.0) < 0.5)
}
@Test func singleTapDoesNotSetTempo() throws {
let (shoeGaze, url): (ShoeGaze, URL) = makeShoeGaze()
defer { try? FileManager.default.removeItem(at: url) }
let startingBpm: Double = shoeGaze.clock.bpm
shoeGaze.tapTempo()
#expect(shoeGaze.clock.bpm == startingBpm, "A single tap has no interval to measure — tempo must not change")
}
@Test func longPauseResetsTapHistory() throws {
let (shoeGaze, url): (ShoeGaze, URL) = makeShoeGaze()
defer { try? FileManager.default.removeItem(at: url) }
let now: Date = Date()
// Two taps 1s apart, then a long pause before the next tap.
shoeGaze.tapTimestamps = [now.addingTimeInterval(-10.0), now.addingTimeInterval(-9.0)]
shoeGaze.tapTempo()
#expect(shoeGaze.tapTimestamps.count == 1, "A >2s gap should discard prior taps and start a fresh sequence")
}
@Test func tapTempoRespectsBpmRangeViaClockPrecondition() throws {
let (shoeGaze, url): (ShoeGaze, URL) = makeShoeGaze()
defer { try? FileManager.default.removeItem(at: url) }
let now: Date = Date()
// Very fast taps (well under minTapInterval == 0.2s) are filtered out of the average.
shoeGaze.tapTimestamps = [
now.addingTimeInterval(-0.15),
now.addingTimeInterval(-0.05),
]
let startingBpm: Double = shoeGaze.clock.bpm
shoeGaze.tapTempo()
// The sub-threshold interval is filtered; with no valid interval left the tempo
// is left unchanged rather than jumping to an implausible value.
#expect(shoeGaze.clock.bpm == startingBpm)
}
}