154 lines
6.1 KiB
Swift
154 lines
6.1 KiB
Swift
// MusicXMLRepeatPartPaddingTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("MusicXML repeat-barline padding for short parts")
|
|
struct MusicXMLRepeatPartPaddingTests {
|
|
|
|
/// Extract the `<part id="P{index}">…</part>` block (1-based part index).
|
|
private func partBlock(_ xml: String, index: Int) -> String {
|
|
let open: String = "<part id=\"P\(index)\">"
|
|
guard let start: Range<String.Index> = xml.range(of: open),
|
|
let end: Range<String.Index> = xml.range(
|
|
of: "</part>",
|
|
range: start.upperBound..<xml.endIndex
|
|
)
|
|
else { return "" }
|
|
return String(xml[start.upperBound..<end.lowerBound])
|
|
}
|
|
|
|
private func measureCount(_ block: String) -> Int {
|
|
block.components(separatedBy: "<measure number=").count - 1
|
|
}
|
|
|
|
private func repeatsBars1to4() -> RepeatStructure {
|
|
RepeatStructure(
|
|
forwardRepeatMeasures: [1],
|
|
backwardRepeatMeasures: [3],
|
|
endings: [
|
|
Ending(number: 1, startMeasure: 3, stopMeasure: 3),
|
|
Ending(number: 2, startMeasure: 4, stopMeasure: 4)
|
|
]
|
|
)
|
|
}
|
|
|
|
@Test("Short single-voice part is padded to the repeat span and carries the barlines")
|
|
func shortSingleVoicePartPadded() {
|
|
let score: Score = Score(
|
|
bpm: 120,
|
|
timeSignature: TimeSignature(4, 4),
|
|
repeats: repeatsBars1to4()
|
|
) {
|
|
Part("long", clef: .treble) {
|
|
for _: Int in 0..<4 { Note(.named(.c, 5), dur: .whole) }
|
|
}
|
|
Part("short", clef: .treble) {
|
|
Note(.named(.c, 4), dur: .whole)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
let shortBlock: String = partBlock(xml, index: 2)
|
|
// Padded from 1 natural bar up to measure 4 (the repeat span).
|
|
#expect(measureCount(shortBlock) == 4)
|
|
// The short part now carries the backward repeat (m3) and both endings.
|
|
#expect(shortBlock.contains("<repeat direction=\"backward\""))
|
|
#expect(shortBlock.contains("<ending number=\"1\""))
|
|
#expect(shortBlock.contains("<ending number=\"2\""))
|
|
// The padded measures are whole-measure rests.
|
|
#expect(shortBlock.contains("<rest measure=\"yes\"/>"))
|
|
}
|
|
|
|
@Test("Short multi-voice part is padded to the repeat span")
|
|
func shortMultiVoicePartPadded() {
|
|
let score: Score = Score(
|
|
bpm: 120,
|
|
timeSignature: TimeSignature(4, 4),
|
|
repeats: repeatsBars1to4()
|
|
) {
|
|
Part("long", clef: .treble) {
|
|
for _: Int in 0..<4 { Note(.named(.c, 5), dur: .whole) }
|
|
}
|
|
Part("short", clef: .treble) {
|
|
Voice(1) { Note(.named(.e, 5), dur: .whole) }
|
|
Voice(2) { Note(.named(.c, 4), dur: .whole) }
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
let shortBlock: String = partBlock(xml, index: 2)
|
|
#expect(measureCount(shortBlock) == 4)
|
|
#expect(shortBlock.contains("<repeat direction=\"backward\""))
|
|
}
|
|
|
|
@Test("Polymetric part is NOT padded — its barline grid cannot align")
|
|
func polymetricPartNotPadded() {
|
|
let score: Score = Score(
|
|
bpm: 120,
|
|
timeSignature: TimeSignature(4, 4),
|
|
repeats: repeatsBars1to4()
|
|
) {
|
|
Part("long", clef: .treble) {
|
|
for _: Int in 0..<4 { Note(.named(.c, 5), dur: .whole) }
|
|
}
|
|
Part("poly", clef: .bass, meterMap: MeterMap(.threeFour)) {
|
|
for _: Int in 0..<3 { Note(.named(.g, 2), dur: .quarter) }
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
let polyBlock: String = partBlock(xml, index: 2)
|
|
// 3 quarters in 3/4 = one bar; left at natural length, not padded to 4.
|
|
#expect(measureCount(polyBlock) == 1)
|
|
// With no measure 3, it carries no backward repeat.
|
|
#expect(!polyBlock.contains("<repeat direction=\"backward\""))
|
|
}
|
|
|
|
@Test("A score with no repeats leaves short parts unchanged")
|
|
func noRepeatsLeavesShortPartUnchanged() {
|
|
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
|
|
Part("long", clef: .treble) {
|
|
for _: Int in 0..<4 { Note(.named(.c, 5), dur: .whole) }
|
|
}
|
|
Part("short", clef: .treble) {
|
|
Note(.named(.c, 4), dur: .whole)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
let shortBlock: String = partBlock(xml, index: 2)
|
|
// No repeat structure → no padding → natural single bar.
|
|
#expect(measureCount(shortBlock) == 1)
|
|
}
|
|
|
|
@Test("Anacrusis part with short content pads full bars 1…N above the pickup")
|
|
func anacrusisShortPartPadded() {
|
|
// A one-quarter pickup (bar 0) then 4/4. The short part fills only the
|
|
// pickup, so it has zero full bars naturally and must pad full bars 1…4
|
|
// without re-emitting or skipping the pickup (bar 0).
|
|
let score: Score = Score(
|
|
bpm: 120,
|
|
meterMap: MeterMap(.fourFour, anacrusis: Fraction(1, 1)),
|
|
repeats: repeatsBars1to4()
|
|
) {
|
|
Part("long", clef: .treble) {
|
|
Note(.named(.e, 5), dur: .quarter) // pickup
|
|
for _: Int in 0..<4 { Note(.named(.c, 5), dur: .whole) }
|
|
}
|
|
Part("short", clef: .treble) {
|
|
Note(.named(.e, 5), dur: .quarter) // fills the pickup only
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
let shortBlock: String = partBlock(xml, index: 2)
|
|
// Pickup (measure 0) + four padded full bars = 5 measures.
|
|
#expect(measureCount(shortBlock) == 5)
|
|
#expect(shortBlock.contains("<measure number=\"0\" implicit=\"yes\""))
|
|
#expect(shortBlock.contains("<repeat direction=\"backward\""))
|
|
#expect(shortBlock.contains("<ending number=\"2\""))
|
|
}
|
|
}
|