Files
Lyrebird/Tests/LyrebirdTests/MusicXMLMeterChangeTests.swift
T

335 lines
14 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// MusicXMLMeterChangeTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("MusicXML encoder — mid-piece meter-change notation")
struct MusicXMLMeterChangeTests {
// MARK: - Constant-meter guard: output must remain byte-identical
/// A constant-meter score (single-segment meterMap) must produce the same
/// output whether it is built with the `timeSignature:` parameter or an
/// explicit `meterMap:` wrapping the same signature.
@Test func constantMeterOutputIsUnchangedByExplicitMeterMap() {
// Build via the timeSignature: parameter.
let scoreTuple: 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)
}
}
// Build via explicit single-segment meterMap.
let scoreMeter: Score = Score(
bpm: 120,
meterMap: MeterMap(.fourFour)
) {
Part("p") {
Note(.c, 4, dur: .quarter)
Note(.d, 4, dur: .quarter)
Note(.e, 4, dur: .quarter)
Note(.f, 4, dur: .quarter)
}
}
let xmlTuple: String = scoreTuple.toMusicXMLString()
let xmlMeter: String = scoreMeter.toMusicXMLString()
#expect(xmlTuple == xmlMeter, "Constant-meter output must be byte-identical via both paths")
}
// MARK: - Measure-1 time signature
/// A score with meterMap starting 4/4 emits `<beats>4</beats><beat-type>4</beat-type>`
/// inside `<measure number="1">`.
@Test func meterChangeBarOneEmitsFourFour() {
let score: Score = Score(
bpm: 120,
meterMap: MeterMap(segments: [
MeterMap.Segment(startBar: 1, .fourFour),
MeterMap.Segment(startBar: 3, .threeFour)
])
) {
Part("p") {
// 4 quarters × 2 bars = bar 1–2 in 4/4, then bar 3 in 3/4.
Note(.c, 4, dur: .quarter)
Note(.d, 4, dur: .quarter)
Note(.e, 4, dur: .quarter)
Note(.f, 4, dur: .quarter)
// bar 2
Note(.g, 4, dur: .quarter)
Note(.a, 4, dur: .quarter)
Note(.b, 4, dur: .quarter)
Note(.c, 5, dur: .quarter)
// bar 3 (3/4)
Note(.d, 5, dur: .quarter)
Note(.e, 5, dur: .quarter)
Note(.f, 5, dur: .quarter)
}
}
let xml: String = score.toMusicXMLString()
// Measure 1: 4/4 in the attributes block.
let measure1Range: Range<String.Index>? = xml.range(of: "<measure number=\"1\">")
#expect(measure1Range != nil, "measure 1 must exist")
// The measure-1 <time> block must say 4/4.
#expect(
xml.contains("<beats>4</beats>"),
"bar-1 time must emit <beats>4</beats>"
)
#expect(
xml.contains("<beat-type>4</beat-type>"),
"bar-1 time must emit <beat-type>4</beat-type>"
)
}
// MARK: - Meter change at bar 3: 4/4 → 3/4
/// Bar 3 must carry a `<time>` change block and bar 2 must NOT carry one.
@Test func meterChangeEmitsTimeAtChangeBar() {
let score: Score = Score(
bpm: 120,
meterMap: MeterMap(segments: [
MeterMap.Segment(startBar: 1, .fourFour),
MeterMap.Segment(startBar: 3, .threeFour)
])
) {
Part("p") {
// bar 1 (4/4): four quarters
Note(.c, 4, dur: .quarter)
Note(.d, 4, dur: .quarter)
Note(.e, 4, dur: .quarter)
Note(.f, 4, dur: .quarter)
// bar 2 (4/4): four quarters
Note(.g, 4, dur: .quarter)
Note(.a, 4, dur: .quarter)
Note(.b, 4, dur: .quarter)
Note(.c, 5, dur: .quarter)
// bar 3 (3/4): three quarters
Note(.d, 5, dur: .quarter)
Note(.e, 5, dur: .quarter)
Note(.f, 5, dur: .quarter)
}
}
let xml: String = score.toMusicXMLString()
// Locate the three measure blocks.
let m1Start: String.Index? = xml.range(of: "<measure number=\"1\">")?.lowerBound
let m2Start: String.Index? = xml.range(of: "<measure number=\"2\">")?.lowerBound
let m3Start: String.Index? = xml.range(of: "<measure number=\"3\">")?.lowerBound
let m4Start: String.Index? = xml.range(of: "<measure number=\"4\">")?.lowerBound
#expect(m1Start != nil, "measure 1 must exist")
#expect(m2Start != nil, "measure 2 must exist")
#expect(m3Start != nil, "measure 3 must exist")
guard let m2Idx: String.Index = m2Start,
let m3Idx: String.Index = m3Start else {
return
}
// Isolate the measure-2 and measure-3 text spans.
let m3EndIdx: String.Index = m4Start ?? xml.endIndex
let measure2Text: String = String(xml[m2Idx..<m3Idx])
let measure3Text: String = String(xml[m3Idx..<m3EndIdx])
// Measure 2: must NOT carry a new <time> element (same meter as bar 1).
#expect(
!measure2Text.contains("<time>"),
"bar 2 must not emit a new <time> when meter is unchanged"
)
// Measure 3: MUST carry a <time> change for 3/4.
#expect(
measure3Text.contains("<time>"),
"bar 3 must emit a <time> block at the meter change"
)
#expect(
measure3Text.contains("<beats>3</beats>"),
"bar 3 <time> must say <beats>3</beats>"
)
#expect(
measure3Text.contains("<beat-type>4</beat-type>"),
"bar 3 <time> must say <beat-type>4</beat-type>"
)
// Measure 3 time block must be inside <attributes>, NOT carry <divisions> or <key>.
let hasTimeInAttributes: Bool = measure3Text.contains("<attributes>")
&& measure3Text.contains("<time>")
#expect(hasTimeInAttributes, "bar 3 <time> must be wrapped in <attributes>")
#expect(
!measure3Text.contains("<divisions>"),
"bar 3 meter-change <attributes> must not re-emit <divisions>"
)
#expect(
!measure3Text.contains("<key>"),
"bar 3 meter-change <attributes> must not re-emit <key>"
)
}
// MARK: - Bar capacity: bar 3 holds 3 quarters, not 4
/// After a 4/4 → 3/4 change at bar 3, a whole note written before bar 3
/// that overflows into bar 3 is split using the correct capacities:
/// the bar-2 fragment fills the 4 remaining quarters of bar 2, and the
/// bar-3 fragment uses 3 quarters (not 4) as the bar-3 capacity.
@Test func meterChangeBarCapacitySplitsNoteCorrectly() {
// Build a score where bar 2 starts at quarter 4, bar 3 at quarter 8.
// A whole note (4 quarters) beginning at the start of bar 2 will
// exactly fill bar 2. Then a dotted-half note (3 quarters) will fill bar 3.
let score: Score = Score(
bpm: 120,
meterMap: MeterMap(segments: [
MeterMap.Segment(startBar: 1, .fourFour),
MeterMap.Segment(startBar: 3, .threeFour)
])
) {
Part("p") {
// bar 1 (4/4): four quarters → 4 quarters consumed
Note(.c, 4, dur: .quarter)
Note(.d, 4, dur: .quarter)
Note(.e, 4, dur: .quarter)
Note(.f, 4, dur: .quarter)
// bar 2 (4/4): whole note → fills bar 2 exactly (4 quarters)
Note(.g, 4, dur: .whole)
// bar 3 (3/4): dotted half → fills bar 3 exactly (3 quarters)
Note(.a, 4, dur: .half.dotted)
}
}
let xml: String = score.toMusicXMLString()
// Three measures must exist.
#expect(xml.contains("<measure number=\"1\">"), "measure 1 must exist")
#expect(xml.contains("<measure number=\"2\">"), "measure 2 must exist")
#expect(xml.contains("<measure number=\"3\">"), "measure 3 must exist")
// No measure 4 — all notes fit exactly into 3 bars.
#expect(!xml.contains("<measure number=\"4\">"), "no measure 4 should be emitted")
// Locate bar 2 and bar 3 text spans for isolated inspection.
let m2Start: String.Index? = xml.range(of: "<measure number=\"2\">")?.lowerBound
let m3Start: String.Index? = xml.range(of: "<measure number=\"3\">")?.lowerBound
guard let m2Idx: String.Index = m2Start,
let m3Idx: String.Index = m3Start else {
return
}
let measure2Text: String = String(xml[m2Idx..<m3Idx])
let measure3Text: String = String(xml[m3Idx...])
// Bar 2 contains a whole note (type "whole").
#expect(measure2Text.contains("<type>whole</type>"), "bar 2 must contain the whole note")
// Bar 2 must NOT be split (only 1 note element — the whole note).
let measure2NoteCount: Int = measure2Text.components(separatedBy: "<note>").count - 1
#expect(measure2NoteCount == 1, "bar 2 should hold exactly the whole note (no split needed)")
// Bar 3 contains a dotted-half note (type "half" with one <dot/>).
#expect(measure3Text.contains("<type>half</type>"), "bar 3 must contain the dotted-half note")
#expect(measure3Text.contains("<dot/>"), "bar 3 dotted-half must carry a <dot/>")
// Bar 3 meter change <time> emits 3/4.
#expect(measure3Text.contains("<beats>3</beats>"), "bar 3 must emit 3/4 time signature")
}
// MARK: - Cross-barline tie correctly splits using different capacities
/// A note longer than a 4/4 bar that begins in bar 1 and crosses into a
/// 3/4 bar 3 (after also crossing bar 2 in 4/4) is split into three tied
/// pieces whose durations match each bar's capacity.
///
/// Score: bar 1 = 4/4, bar 3 = 3/4.
/// Place a note of 11 quarters beginning at measure 1 beat 1:
/// piece 1 → bar 1 (4 quarters), piece 2 → bar 2 (4 quarters),
/// piece 3 → bar 3 (3 quarters). Total = 11 quarters.
@Test func crossBarlineTieSplitsWithVariableCapacity() {
// 11 quarter-beats = 1 whole (4) + 1 whole (4) + 1 dottedHalf (3)
// expressed as a raw Fraction duration so it spans all three bars.
let elevenQuarters: NoteDuration = .fraction(Fraction(11, 1))
let score: Score = Score(
bpm: 120,
meterMap: MeterMap(segments: [
MeterMap.Segment(startBar: 1, .fourFour),
MeterMap.Segment(startBar: 3, .threeFour)
])
) {
Part("p") {
Note(.c, 4, dur: elevenQuarters)
}
}
let xml: String = score.toMusicXMLString()
// Three measures must be emitted.
#expect(xml.contains("<measure number=\"1\">"), "measure 1 must exist")
#expect(xml.contains("<measure number=\"2\">"), "measure 2 must exist")
#expect(xml.contains("<measure number=\"3\">"), "measure 3 must exist")
#expect(!xml.contains("<measure number=\"4\">"), "no measure 4 should be emitted")
// Exactly three <note> elements should exist (one tied piece per bar).
let noteCount: Int = xml.components(separatedBy: "<note>").count - 1
#expect(noteCount == 3, "11-quarter note crossing 4/4|4/4|3/4 barlines should split into 3 tied pieces")
// All pieces must carry tie-start or tie-stop elements (the split mechanism).
let tieStartCount: Int = xml.components(separatedBy: "<tie type=\"start\"/>").count - 1
let tieStopCount: Int = xml.components(separatedBy: "<tie type=\"stop\"/>").count - 1
#expect(tieStartCount >= 1, "tied continuation must carry at least one tie-start")
#expect(tieStopCount >= 1, "tied continuation must carry at least one tie-stop")
// Bar 3 must emit the 3/4 meter change.
let m3Start: String.Index? = xml.range(of: "<measure number=\"3\">")?.lowerBound
guard let m3Idx: String.Index = m3Start else { return }
let measure3Text: String = String(xml[m3Idx...])
#expect(
measure3Text.contains("<beats>3</beats>"),
"bar 3 must emit 3/4 time-signature change"
)
}
// MARK: - measureCount variable-capacity correctness
/// A 4/4→3/4 change at bar 3 with exactly 11 quarter-notes of content
/// should require exactly 3 bars (not the 3 bars ceiling from 4/4 alone
/// which would be ceiling(11/4) = 3 but from a different arithmetic path).
/// This test anchors the count via the total measure count in the XML.
@Test func meterChangeMeasureCountIsCorrect() {
// 4 + 4 + 3 = 11 quarters, exactly filling 3 bars.
let score: Score = Score(
bpm: 120,
meterMap: MeterMap(segments: [
MeterMap.Segment(startBar: 1, .fourFour),
MeterMap.Segment(startBar: 3, .threeFour)
])
) {
Part("p") {
// bar 1 (4/4)
Note(.c, 4, dur: .quarter)
Note(.d, 4, dur: .quarter)
Note(.e, 4, dur: .quarter)
Note(.f, 4, dur: .quarter)
// bar 2 (4/4)
Note(.g, 4, dur: .quarter)
Note(.a, 4, dur: .quarter)
Note(.b, 4, dur: .quarter)
Note(.c, 5, dur: .quarter)
// bar 3 (3/4)
Note(.d, 5, dur: .quarter)
Note(.e, 5, dur: .quarter)
Note(.f, 5, dur: .quarter)
}
}
let xml: String = score.toMusicXMLString()
#expect(xml.contains("<measure number=\"3\">"), "must emit exactly 3 measures")
#expect(!xml.contains("<measure number=\"4\">"), "must not emit a 4th measure")
}
}