333 lines
13 KiB
Swift
333 lines
13 KiB
Swift
// ScoreTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("Score — note/rest/chord/tuplet flattening")
|
||
struct ScoreTests {
|
||
|
||
// MARK: - 1. Single notes
|
||
|
||
@Test func singleNoteFlatten() {
|
||
let part: ScorePart = ScorePart(name: "lead", elements: [
|
||
Note(.c, 4, dur: .quarter),
|
||
Note(.d, 4, dur: .quarter),
|
||
Note(.e, 4, dur: .quarter),
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
#expect(events.count == 3)
|
||
#expect(events[0].beat == 0.0)
|
||
#expect(events[1].beat == 1.0)
|
||
#expect(events[2].beat == 2.0)
|
||
#expect(events[0].duration == 1.0)
|
||
#expect(events[0].pitch.midiNote == 60)
|
||
#expect(events[1].pitch.midiNote == 62)
|
||
#expect(events[2].pitch.midiNote == 64)
|
||
}
|
||
|
||
// MARK: - 2. Rests advance cursor without emitting
|
||
|
||
@Test func restAdvancesCursor() {
|
||
let part: ScorePart = ScorePart(name: "p", elements: [
|
||
Note(.c, 4, dur: .quarter),
|
||
Rest(dur: .quarter),
|
||
Note(.d, 4, dur: .quarter),
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
#expect(events.count == 2)
|
||
#expect(events[0].beat == 0.0)
|
||
#expect(events[1].beat == 2.0)
|
||
}
|
||
|
||
// MARK: - 3. Chord splits into N events at same beat
|
||
|
||
@Test func chordSplitsIntoSimultaneousEvents() {
|
||
let part: ScorePart = ScorePart(name: "p", elements: [
|
||
Chord([(.c, 4), (.e, 4), (.g, 4)], dur: .half),
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
#expect(events.count == 3)
|
||
// All three at beat 0
|
||
for e: ScoreEvent in events {
|
||
#expect(e.beat == 0.0)
|
||
#expect(e.duration == 2.0)
|
||
}
|
||
let midis: [Int] = events.compactMap { $0.pitch.midiNote }.sorted()
|
||
#expect(midis == [60, 64, 67])
|
||
}
|
||
|
||
// MARK: - 4. Triplet — three quarters in two beats
|
||
|
||
@Test func tripletFlattening() {
|
||
let part: ScorePart = ScorePart(name: "p", elements: [
|
||
Tuplet(3, inTimeOf: 2) {
|
||
Note(.c, 4, dur: .quarter)
|
||
Note(.d, 4, dur: .quarter)
|
||
Note(.e, 4, dur: .quarter)
|
||
},
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
#expect(events.count == 3)
|
||
// Each note: quarter * 2/3 = 2/3 beat
|
||
let third: Double = 2.0 / 3.0
|
||
#expect(abs(events[0].beat - 0.0) < 1e-15)
|
||
#expect(abs(events[1].beat - third) < 1e-15)
|
||
#expect(abs(events[2].beat - 2.0 * third) < 1e-15)
|
||
// Total span = 2 beats — verify via lastBeat()
|
||
// duration is the notated duration; nominal=2/3, articulation=.normal (scale=1.0)
|
||
// so notated == performed == 2/3 beat.
|
||
for e: ScoreEvent in events {
|
||
#expect(abs(e.duration - third) < 1e-15)
|
||
#expect(abs(e.performedDuration - third) < 1e-15)
|
||
}
|
||
}
|
||
|
||
// MARK: - 5. Tuplet leaves cursor at correct exact beat
|
||
|
||
@Test func tripletAdvancesCursorExactly() {
|
||
// After a triplet of three quarter notes (3-in-time-of-2), the next event
|
||
// should land exactly at beat 2.0 — NOT 1.9999999999999998 from FP drift.
|
||
let part: ScorePart = ScorePart(name: "p", elements: [
|
||
Tuplet(3, inTimeOf: 2) {
|
||
Note(.c, 4, dur: .quarter)
|
||
Note(.d, 4, dur: .quarter)
|
||
Note(.e, 4, dur: .quarter)
|
||
},
|
||
Note(.f, 4, dur: .quarter),
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
#expect(events.count == 4)
|
||
#expect(events[3].beat == 2.0,
|
||
"After 3-in-time-of-2 triplet of quarters, follower should land exactly at beat 2.0; got \(events[3].beat)")
|
||
}
|
||
|
||
// MARK: - 6. Nested tuplets compose ratios
|
||
|
||
@Test func nestedTupletScaleComposes() {
|
||
// Quintuplet(5, inTimeOf 4) of eighths, with a 3-in-time-of-2 triplet inside.
|
||
// Outer scale: 4/5; inner scale: 4/5 * 2/3 = 8/15
|
||
let part: ScorePart = ScorePart(name: "p", elements: [
|
||
Tuplet(5, inTimeOf: 4) {
|
||
Note(.c, 4, dur: .eighth) // scaled to 1/2 * 4/5 = 2/5
|
||
Tuplet(3, inTimeOf: 2) {
|
||
Note(.d, 5, dur: .sixteenth) // scaled to 1/4 * 8/15 = 2/15
|
||
Note(.e, 5, dur: .sixteenth)
|
||
Note(.f, 5, dur: .sixteenth)
|
||
}
|
||
Note(.g, 4, dur: .eighth) // 2/5
|
||
Note(.a, 4, dur: .eighth)
|
||
Note(.b, 4, dur: .eighth)
|
||
},
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
// 4 outer eighth notes + 3 inner triplet sixteenths = 7 events.
|
||
#expect(events.count == 7)
|
||
|
||
// First outer eighth at beat 0
|
||
#expect(events[0].beat == 0.0)
|
||
// Second event = first triplet sixteenth = at beat 2/5 (one outer slot in)
|
||
#expect(abs(events[1].beat - 2.0/5.0) < 1e-15)
|
||
// After all 5 quintuplet positions: total span = 5 * 2/5 = 2.0 beats
|
||
// (one of those positions is replaced by 3 triplet sixteenths summing
|
||
// to 3 * 2/15 = 2/5 — same as one outer eighth slot. ✓)
|
||
guard let last: ScoreEvent = events.last else { Issue.record("no events"); return }
|
||
let total: Double = last.beat + last.duration
|
||
#expect(abs(total - 2.0) < 1e-15)
|
||
}
|
||
|
||
// MARK: - 7. Dotted durations
|
||
|
||
@Test func dottedQuarter() {
|
||
let part: ScorePart = ScorePart(name: "p", elements: [
|
||
Note(.c, 4, dur: .dotted(.quarter)),
|
||
Note(.d, 4, dur: .quarter),
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
#expect(events[0].duration == 1.5)
|
||
#expect(events[1].beat == 1.5)
|
||
}
|
||
|
||
@Test func dotConveniencePropertiesMatchStatics() {
|
||
// `.dotted` / `.doubleDotted` computed properties equal the static funcs.
|
||
#expect(NoteDuration.quarter.dotted.beats == NoteDuration.dotted(.quarter).beats)
|
||
#expect(NoteDuration.quarter.dotted.beats == 1.5)
|
||
#expect(NoteDuration.half.doubleDotted.beats == NoteDuration.doubleDotted(.half).beats)
|
||
#expect(NoteDuration.quarter.doubleDotted.beats == 1.75)
|
||
// Double-dotted is 7/4, NOT the 9/4 of chaining `.dotted.dotted`.
|
||
#expect(NoteDuration.quarter.doubleDotted.beats != NoteDuration.quarter.dotted.dotted.beats)
|
||
#expect(NoteDuration.quarter.dotted.dotted.beats == 2.25)
|
||
}
|
||
|
||
// MARK: - 8. Dynamic → velocity mapping
|
||
|
||
@Test func dynamicMapsToVelocity() {
|
||
let part: ScorePart = ScorePart(name: "p", elements: [
|
||
Note(.c, 4, dur: .quarter, dyn: .pp),
|
||
Note(.c, 4, dur: .quarter, dyn: .ff),
|
||
Note(.c, 4, dur: .quarter, dyn: .raw(0.5)),
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
#expect(events[0].velocity == Dynamic.pp.velocity)
|
||
#expect(events[1].velocity == Dynamic.ff.velocity)
|
||
#expect(events[2].velocity == 0.5)
|
||
}
|
||
|
||
// MARK: - 9. Articulation effect on duration + velocity
|
||
|
||
@Test func articulationScalesDurationAndAddsVelocity() {
|
||
let part: ScorePart = ScorePart(name: "p", elements: [
|
||
Note(.c, 4, dur: .quarter, dyn: .mf, art: .staccato),
|
||
Note(.c, 4, dur: .quarter, dyn: .mf, art: .accent),
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
// staccato: notated duration is unchanged (quarter = 1.0 beat);
|
||
// performedDuration applies the 0.5 scale.
|
||
#expect(events[0].duration == 1.0, "notated duration is always the written beat value")
|
||
#expect(abs(events[0].performedDuration - 0.5) < 1e-15,
|
||
"staccato performed duration = notated × 0.5")
|
||
// accent: notated duration unchanged, velocity = mf + 0.15
|
||
#expect(events[1].duration == 1.0)
|
||
#expect(events[1].performedDuration == 1.0,
|
||
"accent does not alter performed duration (scale = 1.0)")
|
||
#expect(abs(events[1].velocity - (Dynamic.mf.velocity + 0.15)) < 1e-15)
|
||
}
|
||
|
||
@Test func velocityClampsAtOne() {
|
||
let part: ScorePart = ScorePart(name: "p", elements: [
|
||
Note(.c, 4, dur: .quarter, dyn: .fff, art: .marcato),
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
// fff = 1.0; marcato bonus = 0.25; clamp to 1.0
|
||
#expect(events[0].velocity == 1.0)
|
||
}
|
||
|
||
// MARK: - 9b. performedDuration covers all articulation cases
|
||
|
||
@Test func performedDurationAllArticulations() {
|
||
// Half note (2.0 beats notated) under each articulation.
|
||
let part: ScorePart = ScorePart(name: "p", elements: [
|
||
Note(.c, 4, dur: .half, art: .normal),
|
||
Note(.c, 4, dur: .half, art: .staccato),
|
||
Note(.c, 4, dur: .half, art: .staccatissimo),
|
||
Note(.c, 4, dur: .half, art: .legato),
|
||
Note(.c, 4, dur: .half, art: .tenuto),
|
||
Note(.c, 4, dur: .half, art: .accent),
|
||
Note(.c, 4, dur: .half, art: .marcato),
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
// All notated durations must be the half note (2.0 beats).
|
||
for e: ScoreEvent in events {
|
||
#expect(e.duration == 2.0,
|
||
"notated duration must always equal the written beat value (\(e.articulation))")
|
||
}
|
||
// Performed durations apply each articulation's durationScale.
|
||
#expect(abs(events[0].performedDuration - 2.0) < 1e-15, "normal ×1.0")
|
||
#expect(abs(events[1].performedDuration - 1.0) < 1e-15, "staccato ×0.5")
|
||
#expect(abs(events[2].performedDuration - 0.5) < 1e-15, "staccatissimo ×0.25")
|
||
#expect(abs(events[3].performedDuration - 2.1) < 1e-12, "legato ×1.05")
|
||
#expect(abs(events[4].performedDuration - 2.0) < 1e-15, "tenuto ×1.0")
|
||
#expect(abs(events[5].performedDuration - 2.0) < 1e-15, "accent ×1.0")
|
||
#expect(abs(events[6].performedDuration - 1.7) < 1e-12, "marcato ×0.85")
|
||
}
|
||
|
||
// MARK: - 10. DSL: full Score with parts + builders
|
||
|
||
@Test func dslAssemblesFullScore() {
|
||
let score: Score = Score(bpm: 100) {
|
||
Part("lead") {
|
||
Note(.c, 4, dur: .quarter)
|
||
Tuplet(3, inTimeOf: 2) {
|
||
Note(.d, 4, dur: .quarter)
|
||
Note(.e, 4, dur: .quarter)
|
||
Note(.f, 4, dur: .quarter)
|
||
}
|
||
}
|
||
Part("bass", channel: 1) {
|
||
Note(.c, 2, dur: .whole)
|
||
}
|
||
}
|
||
#expect(score.bpm == 100)
|
||
#expect(score.parts.count == 2)
|
||
#expect(score.parts[0].name == "lead")
|
||
#expect(score.parts[1].name == "bass")
|
||
#expect(score.parts[1].channel == 1)
|
||
|
||
let lead: [ScoreEvent] = score.parts[0].flatten()
|
||
#expect(lead.count == 4) // 1 + 3 triplet members
|
||
|
||
let bass: [ScoreEvent] = score.parts[1].flatten()
|
||
#expect(bass.count == 1)
|
||
#expect(bass[0].duration == 4.0)
|
||
}
|
||
|
||
// MARK: - 11. Pitch conversions
|
||
|
||
@Test func midiAndNamedAgree() {
|
||
#expect(Pitch.named(.c, 4).midiNote == 60)
|
||
#expect(Pitch.named(.a, 4).midiNote == 69)
|
||
#expect(Pitch.midi(60).frequency != nil)
|
||
let aFreq: Double? = Pitch.named(.a, 4).frequency
|
||
#expect(abs((aFreq ?? 0) - 440.0) < 1e-9)
|
||
}
|
||
|
||
@Test func enharmonicEquivalence() {
|
||
#expect(Pitch.named(.cSharp, 4).midiNote == Pitch.named(.dFlat, 4).midiNote)
|
||
#expect(Pitch.named(.gSharp, 3).midiNote == Pitch.named(.aFlat, 3).midiNote)
|
||
}
|
||
|
||
@Test func midiOutOfRangeReturnsNil() {
|
||
#expect(Pitch.midi(-1).midiNote == nil)
|
||
#expect(Pitch.midi(128).midiNote == nil)
|
||
#expect(Pitch.midi(200).midiNote == nil)
|
||
#expect(Pitch.midi(0).midiNote == 0)
|
||
#expect(Pitch.midi(127).midiNote == 127)
|
||
}
|
||
|
||
@Test func restMidiNoteIsNil() {
|
||
#expect(Pitch.rest.midiNote == nil)
|
||
#expect(Pitch.rest.isRest)
|
||
}
|
||
|
||
@Test func midiVelocityBoundaries() {
|
||
#expect(Dynamic.fff.midiVelocity == 127)
|
||
#expect(Dynamic.raw(0.0).midiVelocity == 0)
|
||
#expect(Dynamic.raw(1.0).midiVelocity == 127)
|
||
#expect(Dynamic.raw(-0.5).midiVelocity == 0) // clamped
|
||
#expect(Dynamic.raw(2.0).midiVelocity == 127) // clamped
|
||
}
|
||
|
||
// MARK: - 12. totalBeats across parts
|
||
|
||
@Test func totalBeatsIsLatestPartEnd() {
|
||
let score: Score = Score(bpm: 120) {
|
||
Part("a") { Note(.c, 4, dur: .quarter); Note(.c, 4, dur: .quarter) } // 2 beats
|
||
Part("b") { Note(.c, 4, dur: .whole) } // 4 beats
|
||
}
|
||
#expect(score.totalBeats == 4.0)
|
||
}
|
||
|
||
// MARK: - 13. Voice override
|
||
|
||
@Test func voiceOverrideOnNote() {
|
||
let part: ScorePart = ScorePart(name: "p", elements: [
|
||
Note(.c, 4, dur: .quarter, voice: 1),
|
||
Note(.d, 4, dur: .quarter, voice: 2),
|
||
])
|
||
let events: [ScoreEvent] = part.flatten()
|
||
#expect(events[0].voice == 1)
|
||
#expect(events[1].voice == 2)
|
||
}
|
||
|
||
// MARK: - 14. Rest pitch sentinel
|
||
|
||
@Test func restSentinel() {
|
||
#expect(Pitch.rest.isRest)
|
||
#expect(Pitch.midi(60).isRest == false)
|
||
}
|
||
}
|