136 lines
5.9 KiB
Swift
136 lines
5.9 KiB
Swift
// BeatPositionTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("BeatPosition — meter-aware resolution to quarter beats")
|
||
struct BeatPositionTests {
|
||
|
||
private let fourFour: MeterMap = MeterMap(.fourFour)
|
||
private let threeFour: MeterMap = MeterMap(.threeFour)
|
||
private let sixEightCompound: MeterMap = MeterMap(.sixEightCompound)
|
||
private let sixEightSimple: MeterMap = MeterMap(.sixEightSimple)
|
||
private let nineEight: MeterMap = MeterMap(.nineEight)
|
||
|
||
// MARK: - quarters (meter-free)
|
||
|
||
@Test func quartersIsIdentity() {
|
||
#expect(BeatPosition.quarters(0.0).resolve(in: fourFour) == Fraction(0, 1))
|
||
#expect(BeatPosition.quarters(4.0).resolve(in: fourFour) == Fraction(4, 1))
|
||
}
|
||
|
||
@Test func quartersExactFraction() {
|
||
#expect(BeatPosition.quarters(Fraction(2, 3)).resolve(in: fourFour) == Fraction(2, 3))
|
||
}
|
||
|
||
// MARK: - flat beat
|
||
|
||
@Test func flatBeatFourFour() {
|
||
// beat 1 → quarter 0; beat 5 → quarter 4 (the from((1.0,…),(5.0,…)) reading).
|
||
#expect(BeatPosition.beat(1.0).resolve(in: fourFour) == Fraction(0, 1))
|
||
#expect(BeatPosition.beat(5.0).resolve(in: fourFour) == Fraction(4, 1))
|
||
}
|
||
|
||
@Test func flatBeatSixEight() {
|
||
// felt beat = dotted-quarter = 1.5 quarters. beat 1 → 0; beat 2 → 1.5;
|
||
// beat 3 → 3.0 (bar-2 downbeat — beat stays the conducted pulse).
|
||
#expect(BeatPosition.beat(1.0).resolve(in: sixEightCompound) == Fraction(0, 1))
|
||
#expect(BeatPosition.beat(2.0).resolve(in: sixEightCompound) == Fraction(3, 2))
|
||
#expect(BeatPosition.beat(3.0).resolve(in: sixEightCompound) == Fraction(3, 1))
|
||
}
|
||
|
||
// MARK: - bar + beat (felt beat via grouping)
|
||
|
||
@Test func barBeatFourFour() {
|
||
// 4/4: felt beat == quarter (grouping [1,1,1,1]).
|
||
#expect(BeatPosition.bar(1, beat: 1.0).resolve(in: fourFour) == Fraction(0, 1))
|
||
#expect(BeatPosition.bar(2, beat: 1.0).resolve(in: fourFour) == Fraction(4, 1))
|
||
#expect(BeatPosition.bar(2, beat: 3.0).resolve(in: fourFour) == Fraction(6, 1))
|
||
}
|
||
|
||
@Test func barBeatThreeFour() {
|
||
#expect(BeatPosition.bar(2, beat: 1.0).resolve(in: threeFour) == Fraction(3, 1))
|
||
}
|
||
|
||
@Test func barBeatCompoundSixEight() {
|
||
// [3,3] → felt beat onsets 0, 1.5; bar 2 beat 1 = 3.0.
|
||
#expect(BeatPosition.bar(1, beat: 1.0).resolve(in: sixEightCompound) == Fraction(0, 1))
|
||
#expect(BeatPosition.bar(1, beat: 2.0).resolve(in: sixEightCompound) == Fraction(3, 2))
|
||
#expect(BeatPosition.bar(2, beat: 1.0).resolve(in: sixEightCompound) == Fraction(3, 1))
|
||
}
|
||
|
||
@Test func barBeatSimpleSixEight() {
|
||
// [1,1,1,1,1,1] → felt beat 2 = one eighth = 0.5 quarter.
|
||
#expect(BeatPosition.bar(1, beat: 2.0).resolve(in: sixEightSimple) == Fraction(1, 2))
|
||
}
|
||
|
||
@Test func barBeatNineEight() {
|
||
// [3,3,3] → felt beat 3 onset = 3.0 quarters.
|
||
#expect(BeatPosition.bar(1, beat: 3.0).resolve(in: nineEight) == Fraction(3, 1))
|
||
}
|
||
|
||
@Test func barBeatAdditive332() {
|
||
// 8/8 grouped 3+3+2 → felt beats at 0, 1.5, 3.0; lengths 1.5, 1.5, 1.0.
|
||
let map: MeterMap = MeterMap(TimeSignature(8, 8, grouping: [3, 3, 2]))
|
||
#expect(BeatPosition.bar(1, beat: 1.0).resolve(in: map) == Fraction(0, 1))
|
||
#expect(BeatPosition.bar(1, beat: 2.0).resolve(in: map) == Fraction(3, 2))
|
||
#expect(BeatPosition.bar(1, beat: 3.0).resolve(in: map) == Fraction(3, 1))
|
||
}
|
||
|
||
@Test func barBeatFractionalInterpolates() {
|
||
// 6/8 [3,3]: beat 1.5 = halfway through the first dotted-quarter (0.75q).
|
||
#expect(BeatPosition.bar(1, beat: 1.5).resolve(in: sixEightCompound) == Fraction(3, 4))
|
||
}
|
||
|
||
@Test func barBeatFractionalInLastBeatStaysInBar() {
|
||
// 6/8 [3,3]: beat 2.5 = onset 1.5 + 0.5 × length 1.5 = 2.25q (< bar end 3).
|
||
#expect(BeatPosition.bar(1, beat: 2.5).resolve(in: sixEightCompound) == Fraction(9, 4))
|
||
}
|
||
|
||
@Test func barBeatUpperBoundaryValid() {
|
||
// beat == beatsPerBar resolves to the last felt-beat onset (not a trap).
|
||
#expect(BeatPosition.bar(1, beat: 2.0).resolve(in: sixEightCompound) == Fraction(3, 2))
|
||
}
|
||
|
||
// MARK: - bar + division (denominator-note granular addressing)
|
||
|
||
@Test func barDivisionSixEightCountsEighths() {
|
||
// 6/8: division = eighth. division 4 = 4th eighth = 1.5q; bar 2 div 1 = 3.0.
|
||
#expect(BeatPosition.bar(1, division: 1.0).resolve(in: sixEightCompound) == Fraction(0, 1))
|
||
#expect(BeatPosition.bar(1, division: 4.0).resolve(in: sixEightCompound) == Fraction(3, 2))
|
||
#expect(BeatPosition.bar(2, division: 1.0).resolve(in: sixEightCompound) == Fraction(3, 1))
|
||
}
|
||
|
||
@Test func barDivisionFourFourEqualsBeat() {
|
||
// In simple meters division == beat (quarter).
|
||
#expect(BeatPosition.bar(2, division: 3.0).resolve(in: fourFour) == Fraction(6, 1))
|
||
}
|
||
|
||
// MARK: - anacrusis
|
||
|
||
@Test func anacrusisOffsetsFullBars() {
|
||
let map: MeterMap = MeterMap(.fourFour, anacrusis: Fraction(1, 1))
|
||
// bar 1 (first full measure) starts after the 1-quarter pickup.
|
||
#expect(BeatPosition.bar(1, beat: 1.0).resolve(in: map) == Fraction(1, 1))
|
||
#expect(BeatPosition.bar(2, beat: 1.0).resolve(in: map) == Fraction(5, 1))
|
||
// pickup (bar 0) starts at quarter 0.
|
||
#expect(BeatPosition.bar(0, beat: 1.0).resolve(in: map) == Fraction(0, 1))
|
||
}
|
||
|
||
// MARK: - mid-piece meter change
|
||
|
||
@Test func meterChangeAccumulatesCapacities() {
|
||
let map: MeterMap = MeterMap(segments: [
|
||
MeterMap.Segment(startBar: 1, .fourFour),
|
||
MeterMap.Segment(startBar: 3, .threeFour),
|
||
])
|
||
#expect(BeatPosition.bar(3, beat: 1.0).resolve(in: map) == Fraction(8, 1)) // 4+4
|
||
#expect(BeatPosition.bar(4, beat: 1.0).resolve(in: map) == Fraction(11, 1)) // 4+4+3
|
||
}
|
||
}
|