1.0.0 release
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
// LyrebirdClockTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@Suite("LyrebirdClock")
|
||||
struct LyrebirdClockTests {
|
||||
|
||||
func makeEngine(sampleRate: Double = 44100) -> SynthesisEngine {
|
||||
makeTestEngine(sampleRate: sampleRate)
|
||||
}
|
||||
|
||||
// MARK: - sampleTime(beat:)
|
||||
|
||||
@Test func sampleTimeAtBeatZeroEqualsEpoch() {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
#expect(clock.sampleTime(beat: 0.0) == clock.epoch)
|
||||
}
|
||||
|
||||
@Test func sampleTimeAtOneBeatIs120bpm() {
|
||||
// 120 bpm, 44100 Hz → 1 beat = 22050 samples
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
let expected: SampleTime = clock.epoch + SampleTime(22050)
|
||||
#expect(clock.sampleTime(beat: 1.0) == expected)
|
||||
}
|
||||
|
||||
@Test func sampleTimeScalesWithBpm() {
|
||||
// 60 bpm → 1 beat = 44100 samples
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 60.0)
|
||||
let expected: SampleTime = clock.epoch + SampleTime(44100)
|
||||
#expect(clock.sampleTime(beat: 1.0) == expected)
|
||||
}
|
||||
|
||||
@Test func sampleTimeLinearAcrossBeats() {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
let spb: SampleTime = SampleTime(22050)
|
||||
#expect(clock.sampleTime(beat: 2.0) == clock.epoch + spb * 2)
|
||||
#expect(clock.sampleTime(beat: 4.0) == clock.epoch + spb * 4)
|
||||
}
|
||||
|
||||
@Test func sampleTimeFractionalBeat() {
|
||||
// beat 0.5 at 120 bpm = 11025 samples
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
let expected: SampleTime = clock.epoch + SampleTime(11025)
|
||||
#expect(clock.sampleTime(beat: 0.5) == expected)
|
||||
}
|
||||
|
||||
// MARK: - currentBeat
|
||||
|
||||
@Test func currentBeatStartsNearZero() {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
// currentTime == epoch at init → 0 beats elapsed
|
||||
#expect(clock.currentBeat == 0.0)
|
||||
}
|
||||
|
||||
// MARK: - nextBeat
|
||||
|
||||
@Test func nextBeatFromZeroIsOne() {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
// currentBeat == 0 → next beat is 1
|
||||
let expected: SampleTime = clock.sampleTime(beat: 1.0)
|
||||
#expect(clock.nextBeat() == expected)
|
||||
}
|
||||
|
||||
@Test func nextBeatAfterFractionalBeat() {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
// after beat 2.3 → next whole beat is 3
|
||||
let expected: SampleTime = clock.sampleTime(beat: 3.0)
|
||||
#expect(clock.nextBeat(after: 2.3) == expected)
|
||||
}
|
||||
|
||||
@Test func nextBeatAfterWholeNumber() {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
// after beat 4.0 → next whole beat is 5
|
||||
let expected: SampleTime = clock.sampleTime(beat: 5.0)
|
||||
#expect(clock.nextBeat(after: 4.0) == expected)
|
||||
}
|
||||
|
||||
// MARK: - bpm setter accumulation
|
||||
|
||||
@Test func bpmChangeRecomputesSampleTimes() {
|
||||
// advanceBlock does not advance engine.currentTime (only segmentSerial).
|
||||
// Test that changing bpm at epoch (time=0) yields correct sampleTime values
|
||||
// under the new tempo from the same epoch.
|
||||
let engine: SynthesisEngine = makeEngine(sampleRate: 44100)
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
// At time=0: 0 beats elapsed. Change to 60 bpm — _beatOffset stays 0.
|
||||
clock.bpm = 60.0
|
||||
// 1 beat at 60 bpm = 44100 samples from epoch
|
||||
#expect(clock.sampleTime(beat: 1.0) == clock.epoch + SampleTime(44100))
|
||||
// 2 beats at 60 bpm = 88200 samples from epoch
|
||||
#expect(clock.sampleTime(beat: 2.0) == clock.epoch + SampleTime(88200))
|
||||
}
|
||||
|
||||
@Test func bpmChangeDoesNotJumpBeatCount() {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
let beatBefore: Double = clock.currentBeat
|
||||
clock.bpm = 60.0
|
||||
let beatAfter: Double = clock.currentBeat
|
||||
// Beat count must be continuous across a bpm change
|
||||
#expect(abs(beatAfter - beatBefore) < 0.001)
|
||||
}
|
||||
|
||||
// Verifies valid assignment succeeds. The precondition crash paths (bpm <= 0) are not
|
||||
// catchable via Swift Testing — they crash the process and cannot be exercised in-process.
|
||||
@Test func bpmSetterAcceptsPositiveValue() {
|
||||
let engine: SynthesisEngine = makeEngine()
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
#expect(throws: Never.self) { clock.bpm = 60.0 }
|
||||
}
|
||||
|
||||
// MARK: - Past-beat sampleTime
|
||||
|
||||
@Test func sampleTimeForPastBeatIsBeforeEpoch() {
|
||||
// beat -1 at 120 bpm = 22050 samples before epoch
|
||||
let engine: SynthesisEngine = makeEngine(sampleRate: 44100)
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
let expected: SampleTime = clock.epoch - SampleTime(22050)
|
||||
#expect(clock.sampleTime(beat: -1.0) == expected)
|
||||
}
|
||||
|
||||
@Test func sampleTimeForPastFractionalBeat() {
|
||||
// beat -0.5 at 120 bpm = 11025 samples before epoch
|
||||
let engine: SynthesisEngine = makeEngine(sampleRate: 44100)
|
||||
let clock: LyrebirdClock = LyrebirdClock(engine: engine, bpm: 120.0)
|
||||
let expected: SampleTime = clock.epoch - SampleTime(11025)
|
||||
#expect(clock.sampleTime(beat: -0.5) == expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user