387 lines
14 KiB
Swift
387 lines
14 KiB
Swift
// MusicXMLAnacrusisTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
/// Anacrusis (pickup measure) encoding + parser round-trip.
|
|
///
|
|
/// Constraint: scores with no anacrusis (`meterMap.anacrusis == .zero`) must
|
|
/// produce byte-identical output to the pre-anacrusis implementation.
|
|
@MainActor
|
|
@Suite("MusicXML — anacrusis (pickup measure) notation")
|
|
struct MusicXMLAnacrusisTests {
|
|
|
|
// MARK: - Part A: Encoder
|
|
|
|
/// A score with a one-quarter pickup emits `<measure number="0" implicit="yes">`
|
|
/// as its first measure.
|
|
@Test func pickupMeasureIsEmittedImplicit() {
|
|
let score: Score = Score(
|
|
bpm: 120,
|
|
meterMap: MeterMap(.fourFour, anacrusis: Fraction(1, 1))
|
|
) {
|
|
Part("Violin") {
|
|
Note(.c, 5, dur: .quarter)
|
|
Note(.d, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.f, 5, dur: .quarter)
|
|
Note(.g, 5, dur: .quarter)
|
|
}
|
|
}
|
|
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
// Pickup measure must be number 0 with implicit="yes".
|
|
#expect(
|
|
xml.contains("<measure number=\"0\" implicit=\"yes\">"),
|
|
"encoder must emit <measure number=\"0\" implicit=\"yes\"> for the pickup"
|
|
)
|
|
}
|
|
|
|
/// The pickup measure's `<attributes>` block carries the initial time signature
|
|
/// (i.e. the 4/4 from bar 1, since the pickup uses the initial signature).
|
|
@Test func pickupMeasureCarriesInitialTimeSignature() {
|
|
let score: Score = Score(
|
|
bpm: 120,
|
|
meterMap: MeterMap(.fourFour, anacrusis: Fraction(1, 1))
|
|
) {
|
|
Part("p") {
|
|
// quarter pickup
|
|
Note(.c, 5, dur: .quarter)
|
|
// full 4/4 bar
|
|
Note(.d, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.f, 5, dur: .quarter)
|
|
Note(.g, 5, dur: .quarter)
|
|
}
|
|
}
|
|
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
// The initial <time> must appear inside the pickup (the first <attributes> block).
|
|
// The pickup is the first measure so the attributes block is the first one emitted.
|
|
#expect(
|
|
xml.contains("<beats>4</beats>"),
|
|
"pickup measure's <attributes> must contain <beats>4</beats>"
|
|
)
|
|
#expect(
|
|
xml.contains("<beat-type>4</beat-type>"),
|
|
"pickup measure's <attributes> must contain <beat-type>4</beat-type>"
|
|
)
|
|
}
|
|
|
|
/// After the pickup, the first FULL bar is `<measure number="1">` (not 0, not 2).
|
|
@Test func firstFullBarIsNumberOne() {
|
|
let score: Score = Score(
|
|
bpm: 120,
|
|
meterMap: MeterMap(.fourFour, anacrusis: Fraction(1, 1))
|
|
) {
|
|
Part("p") {
|
|
// quarter pickup
|
|
Note(.c, 5, dur: .quarter)
|
|
// full 4/4 bar
|
|
Note(.d, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.f, 5, dur: .quarter)
|
|
Note(.g, 5, dur: .quarter)
|
|
}
|
|
}
|
|
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
#expect(
|
|
xml.contains("<measure number=\"1\">"),
|
|
"the first full bar must be <measure number=\"1\">"
|
|
)
|
|
// The full bar must NOT be tagged implicit="yes".
|
|
// Check that there is no implicit="yes" on number="1".
|
|
#expect(
|
|
!xml.contains("<measure number=\"1\" implicit=\"yes\">"),
|
|
"the first full bar must NOT carry implicit=\"yes\""
|
|
)
|
|
}
|
|
|
|
/// The pickup measure contains exactly one quarter note (the pickup content),
|
|
/// and the first full bar contains four quarter notes.
|
|
@Test func pickupContainsOneNoteFullBarContainsFour() {
|
|
let score: Score = Score(
|
|
bpm: 120,
|
|
meterMap: MeterMap(.fourFour, anacrusis: Fraction(1, 1))
|
|
) {
|
|
Part("p") {
|
|
// quarter pickup: G5
|
|
Note(.g, 5, dur: .quarter)
|
|
// full 4/4 bar: C D E F
|
|
Note(.c, 5, dur: .quarter)
|
|
Note(.d, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.f, 5, dur: .quarter)
|
|
}
|
|
}
|
|
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
// Locate the pickup measure block and verify it contains exactly one <note>.
|
|
// A simple containment check: the pickup block ends before <measure number="1">.
|
|
guard let pickupStart: String.Index = xml.range(
|
|
of: "<measure number=\"0\" implicit=\"yes\">"
|
|
)?.lowerBound else {
|
|
Issue.record("Pickup measure not found in XML output")
|
|
return
|
|
}
|
|
guard let bar1Start: String.Index = xml.range(
|
|
of: "<measure number=\"1\">"
|
|
)?.lowerBound else {
|
|
Issue.record("<measure number=\"1\"> not found in XML output")
|
|
return
|
|
}
|
|
|
|
let pickupBlock: String = String(xml[pickupStart..<bar1Start])
|
|
// Count <note> elements in the pickup block.
|
|
let noteTagCount: Int = pickupBlock.components(separatedBy: "<note>").count - 1
|
|
#expect(noteTagCount == 1, "pickup measure must contain exactly 1 <note>")
|
|
|
|
// The pickup note should be G5.
|
|
#expect(pickupBlock.contains("<step>G</step>"), "pickup note must be G")
|
|
#expect(pickupBlock.contains("<octave>5</octave>"), "pickup note must be octave 5")
|
|
}
|
|
|
|
// MARK: - Part B: Parser round-trip
|
|
|
|
/// A score with a one-quarter 4/4 anacrusis round-trips through MusicXML and
|
|
/// the parsed `meterMap.anacrusis` equals the original.
|
|
@Test func roundTripAnacrusis() throws {
|
|
let original: Score = Score(
|
|
bpm: 120,
|
|
meterMap: MeterMap(.fourFour, anacrusis: Fraction(1, 1))
|
|
) {
|
|
Part("p") {
|
|
// quarter pickup
|
|
Note(.c, 5, dur: .quarter)
|
|
// two full 4/4 bars
|
|
Note(.d, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.f, 5, dur: .quarter)
|
|
Note(.g, 5, dur: .quarter)
|
|
Note(.a, 5, dur: .quarter)
|
|
Note(.b, 5, dur: .quarter)
|
|
Note(.c, 6, dur: .quarter)
|
|
Note(.d, 6, dur: .quarter)
|
|
}
|
|
}
|
|
|
|
let data: Data = original.toMusicXMLData()
|
|
let parsed: Score = try Score.fromMusicXML(data: data)
|
|
|
|
#expect(
|
|
parsed.meterMap.anacrusis == Fraction(1, 1),
|
|
"round-trip must preserve anacrusis == Fraction(1,1)"
|
|
)
|
|
}
|
|
|
|
/// After round-trip the full-bar meter is preserved.
|
|
@Test func roundTripFullBarMeterPreserved() throws {
|
|
let original: Score = Score(
|
|
bpm: 120,
|
|
meterMap: MeterMap(.fourFour, anacrusis: Fraction(1, 1))
|
|
) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter)
|
|
Note(.d, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.f, 5, dur: .quarter)
|
|
Note(.g, 5, dur: .quarter)
|
|
}
|
|
}
|
|
|
|
let data: Data = original.toMusicXMLData()
|
|
let parsed: Score = try Score.fromMusicXML(data: data)
|
|
|
|
#expect(
|
|
parsed.meterMap.initial.tuple == (4, 4),
|
|
"round-trip must preserve 4/4 time signature for full bars"
|
|
)
|
|
}
|
|
|
|
// MARK: - No-anacrusis byte-identity
|
|
|
|
/// A score without an anacrusis must produce output that does NOT contain
|
|
/// `implicit="yes"`, confirming byte-identical structure to the no-anacrusis baseline.
|
|
@Test func noAnacrusisOutputContainsNoImplicit() {
|
|
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
|
|
Part("p") {
|
|
Note(.c, 4, dur: .quarter)
|
|
Note(.d, 4, dur: .quarter)
|
|
Note(.e, 4, dur: .quarter)
|
|
Note(.f, 4, dur: .quarter)
|
|
}
|
|
}
|
|
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
#expect(
|
|
!xml.contains("implicit=\"yes\""),
|
|
"a no-anacrusis score must not emit implicit=\"yes\" anywhere"
|
|
)
|
|
#expect(
|
|
xml.contains("<measure number=\"1\">"),
|
|
"a no-anacrusis score must start with <measure number=\"1\">"
|
|
)
|
|
#expect(
|
|
!xml.contains("<measure number=\"0\""),
|
|
"a no-anacrusis score must not emit measure number 0"
|
|
)
|
|
}
|
|
|
|
/// A no-anacrusis score's XML is byte-identical to the reference encoder output
|
|
/// (no pickup support). We verify structural markers and the absence of
|
|
/// pickup-specific markup.
|
|
@Test func noAnacrusisRoundTripIsClean() throws {
|
|
let original: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
|
|
Part("Piano") {
|
|
Note(.c, 4, dur: .quarter)
|
|
Note(.d, 4, dur: .quarter)
|
|
Note(.e, 4, dur: .quarter)
|
|
Note(.f, 4, dur: .quarter)
|
|
}
|
|
}
|
|
|
|
let data: Data = original.toMusicXMLData()
|
|
let parsed: Score = try Score.fromMusicXML(data: data)
|
|
|
|
#expect(
|
|
parsed.meterMap.anacrusis == .zero,
|
|
"no-anacrusis score must round-trip with anacrusis == .zero"
|
|
)
|
|
#expect(
|
|
parsed.meterMap.isConstant,
|
|
"no-anacrusis score must round-trip with isConstant == true"
|
|
)
|
|
#expect(
|
|
parsed.meterMap.initial.tuple == (4, 4),
|
|
"no-anacrusis score must round-trip with (4,4) initial signature"
|
|
)
|
|
}
|
|
|
|
// MARK: - Anacrusis + mid-piece meter change
|
|
|
|
/// A score with an anacrusis AND a mid-piece meter change round-trips with
|
|
/// both the anacrusis and all meter segments preserved.
|
|
@Test func anacrusissPlusMeterChangeRoundTrip() throws {
|
|
let meterMap: MeterMap = MeterMap(
|
|
segments: [
|
|
MeterMap.Segment(startBar: 1, .fourFour),
|
|
MeterMap.Segment(startBar: 3, .threeFour)
|
|
],
|
|
anacrusis: Fraction(1, 1)
|
|
)
|
|
let original: Score = Score(bpm: 120, meterMap: meterMap) {
|
|
Part("p") {
|
|
// one-quarter pickup
|
|
Note(.c, 5, dur: .quarter)
|
|
// bar 1 (4/4): 4 quarters
|
|
Note(.d, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.f, 5, dur: .quarter)
|
|
Note(.g, 5, dur: .quarter)
|
|
// bar 2 (4/4): 4 quarters
|
|
Note(.a, 5, dur: .quarter)
|
|
Note(.b, 5, dur: .quarter)
|
|
Note(.c, 6, dur: .quarter)
|
|
Note(.d, 6, dur: .quarter)
|
|
// bar 3 (3/4): 3 quarters
|
|
Note(.e, 6, dur: .quarter)
|
|
Note(.f, 6, dur: .quarter)
|
|
Note(.g, 6, dur: .quarter)
|
|
}
|
|
}
|
|
|
|
let data: Data = original.toMusicXMLData()
|
|
let parsed: Score = try Score.fromMusicXML(data: data)
|
|
|
|
#expect(
|
|
parsed.meterMap.anacrusis == Fraction(1, 1),
|
|
"anacrusis must survive round-trip when combined with mid-piece meter change"
|
|
)
|
|
#expect(
|
|
parsed.meterMap.timeSignature(atBar: 1).tuple == (4, 4),
|
|
"bar-1 signature must be 4/4 after round-trip"
|
|
)
|
|
#expect(
|
|
parsed.meterMap.timeSignature(atBar: 3).tuple == (3, 4),
|
|
"bar-3 signature must be 3/4 after round-trip"
|
|
)
|
|
}
|
|
|
|
// MARK: - Half-bar anacrusis
|
|
|
|
/// A half-bar (two-quarter) pickup in a 4/4 score round-trips with the
|
|
/// correct anacrusis duration.
|
|
@Test func halfBarAnacrusissRoundTrip() throws {
|
|
let original: Score = Score(
|
|
bpm: 96,
|
|
meterMap: MeterMap(.fourFour, anacrusis: Fraction(2, 1))
|
|
) {
|
|
Part("p") {
|
|
// two-quarter pickup
|
|
Note(.c, 5, dur: .quarter)
|
|
Note(.d, 5, dur: .quarter)
|
|
// full 4/4 bar
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.f, 5, dur: .quarter)
|
|
Note(.g, 5, dur: .quarter)
|
|
Note(.a, 5, dur: .quarter)
|
|
}
|
|
}
|
|
|
|
let data: Data = original.toMusicXMLData()
|
|
let xml: String = String(data: data, encoding: .utf8) ?? ""
|
|
let parsed: Score = try Score.fromMusicXML(data: data)
|
|
|
|
#expect(
|
|
xml.contains("<measure number=\"0\" implicit=\"yes\">"),
|
|
"half-bar pickup must emit measure 0 with implicit=\"yes\""
|
|
)
|
|
#expect(
|
|
parsed.meterMap.anacrusis == Fraction(2, 1),
|
|
"half-bar pickup anacrusis must round-trip as Fraction(2,1)"
|
|
)
|
|
}
|
|
|
|
/// Beam grouping must account the pickup against the pickup window (not the
|
|
/// full first-bar window), so the FULL bar after a pickup is beat-aligned
|
|
/// from beat 0. A 1-quarter pickup + a full 4/4 bar of eighths beams as four
|
|
/// pairs (4 `begin`s). Without the pickup-aware beam accounting the bar-1
|
|
/// eighths would be shifted by the pickup duration and mis-grouped.
|
|
@Test func pickupBeamingIsBeatAlignedInFullBar() {
|
|
let score: Score = Score(
|
|
bpm: 120,
|
|
meterMap: MeterMap(.fourFour, anacrusis: Fraction(1, 1))
|
|
) {
|
|
Part("flute") {
|
|
Note(.c, 5, dur: .quarter) // pickup (1 quarter)
|
|
// Full bar 1: eight eighths → four beamed pairs.
|
|
Note(.d, 5, dur: .eighth)
|
|
Note(.e, 5, dur: .eighth)
|
|
Note(.f, 5, dur: .eighth)
|
|
Note(.g, 5, dur: .eighth)
|
|
Note(.a, 5, dur: .eighth)
|
|
Note(.b, 5, dur: .eighth)
|
|
Note(.c, 6, dur: .eighth)
|
|
Note(.d, 6, dur: .eighth)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
let beginCount: Int = xml.components(separatedBy: "<beam number=\"1\">begin</beam>").count - 1
|
|
#expect(
|
|
beginCount == 4,
|
|
"expected 4 beam pairs (beat-aligned) in the full bar after the pickup, got \(beginCount)"
|
|
)
|
|
}
|
|
}
|