168 lines
7.8 KiB
Swift
168 lines
7.8 KiB
Swift
// MusicXMLBarlineExactFillTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
/// Regression coverage for two MusicXML-export defects exposed by sectioned
|
|
/// scores with rest-padded short parts.
|
|
///
|
|
/// 1. **Barline-exact-fill rounding.** In the single-voice encoder, when a
|
|
/// note/rest that crosses a barline has its FINAL piece land exactly on the
|
|
/// next barline (`leftover` and the measure's `remaining` capacity reach zero
|
|
/// together), the measure was left open with `remaining == 0`. The next
|
|
/// sounding atom could then neither fit nor split cleanly: the split loop
|
|
/// emitted a spurious `max(1, …)` one-division piece before the bar finally
|
|
/// closed, tying it forward and shifting every later note by one division
|
|
/// (visually turning constant 4/4 bars into 5/4).
|
|
/// 2. **Non-bar-aligned rest padding.** Section auto-padding decomposed the
|
|
/// whole gap greedily into the largest representable rests (a 21-beat gap
|
|
/// became three double-dotted-whole rests) with no bar alignment, so the
|
|
/// encoder split them into metrically-wrong fragments (a quarter + dotted-half
|
|
/// rest crossing the mid-bar strong beat) instead of clean whole-measure rests.
|
|
@MainActor
|
|
@Suite("MusicXML — barline-exact-fill rounding")
|
|
struct MusicXMLBarlineExactFillTests {
|
|
|
|
/// Split a part's body into `(measureNumber, measureBody)` pairs using the
|
|
/// codebase's string-splitting idiom (no regex).
|
|
private func measureBlocks(_ xml: String, part pid: String) -> [(number: String, body: String)] {
|
|
guard let partStart: Range<String.Index> = xml.range(of: "<part id=\"\(pid)\">"),
|
|
let partEnd: Range<String.Index> = xml.range(
|
|
of: "</part>",
|
|
range: partStart.upperBound..<xml.endIndex
|
|
) else {
|
|
return []
|
|
}
|
|
let partBody: String = String(xml[partStart.upperBound..<partEnd.lowerBound])
|
|
var blocks: [(number: String, body: String)] = []
|
|
let chunks: [String] = partBody.components(separatedBy: "<measure number=\"")
|
|
for chunk: String in chunks.dropFirst() {
|
|
guard let quote: Range<String.Index> = chunk.range(of: "\"") else { continue }
|
|
let number: String = String(chunk[chunk.startIndex..<quote.lowerBound])
|
|
blocks.append((number: number, body: chunk))
|
|
}
|
|
return blocks
|
|
}
|
|
|
|
/// Sum the `<duration>` of every non-chord `<note>` in a measure body.
|
|
private func durationSum(_ measureBody: String) -> Int {
|
|
var total: Int = 0
|
|
let notes: [String] = measureBody.components(separatedBy: "<note>")
|
|
for note: String in notes.dropFirst() {
|
|
if note.contains("<chord/>") { continue }
|
|
guard let open: Range<String.Index> = note.range(of: "<duration>"),
|
|
let close: Range<String.Index> = note.range(
|
|
of: "</duration>",
|
|
range: open.upperBound..<note.endIndex
|
|
),
|
|
let value: Int = Int(note[open.upperBound..<close.lowerBound]) else {
|
|
continue
|
|
}
|
|
total += value
|
|
}
|
|
return total
|
|
}
|
|
|
|
/// A breve (8 beats) in 4/4 exactly fills two bars; the following whole note
|
|
/// must land cleanly on bar 3, NOT be split into a 1-division sliver in bar 2.
|
|
@Test func breveThenWholeNoteNoSliver() {
|
|
let divisions: Int = 480
|
|
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
|
|
Part("lead") {
|
|
Note(.c, 4, dur: .doubleWhole) // fills bars 1-2 exactly (8 beats)
|
|
Note(.d, 4, dur: .whole) // must be bar 3, all 4 beats
|
|
Note(.e, 4, dur: .whole) // bar 4
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString(divisions: divisions)
|
|
for block: (number: String, body: String) in measureBlocks(xml, part: "P1") {
|
|
#expect(
|
|
durationSum(block.body) == 4 * divisions,
|
|
"measure \(block.number) holds \(durationSum(block.body)) divisions, expected \(4 * divisions)"
|
|
)
|
|
}
|
|
// No one-division phantom note anywhere.
|
|
#expect(!xml.contains("<duration>1</duration>"))
|
|
}
|
|
|
|
/// The sectioned-score path that originally surfaced the bug: a part with a
|
|
/// long rest-pad tail that ends exactly on a barline, immediately followed by
|
|
/// a new section whose note enters on the downbeat. Models the
|
|
/// melody-section-then-chord-section boundary.
|
|
@Test func sectionPadTailThenDownbeatNoteNoSliver() {
|
|
let divisions: Int = 480
|
|
let melody: ScoreSection = Section(
|
|
"A",
|
|
meterMap: MeterMap(TimeSignature(4, 4))
|
|
) {
|
|
// Short part: 23 beats of content, padded to the section's 44-beat
|
|
// span (max part length) with rests whose tail ends on a barline.
|
|
Part("lead") {
|
|
for _: Int in 0..<23 { Note(.c, 4, dur: .quarter) }
|
|
}
|
|
// Long part forces the section span to 11 bars (44 beats).
|
|
Part("bass") {
|
|
for _: Int in 0..<44 { Note(.c, 2, dur: .quarter) }
|
|
}
|
|
}
|
|
let chords: ScoreSection = Section(
|
|
"B",
|
|
meterMap: MeterMap(TimeSignature(4, 4))
|
|
) {
|
|
Part("lead") { Note(.f, 5, dur: .whole) }
|
|
Part("bass") { Note(.f, 2, dur: .whole) }
|
|
}
|
|
let score: Score = Score(sections: [melody, chords], bpm: 120)
|
|
let xml: String = score.toMusicXMLString(divisions: divisions)
|
|
for pid: String in ["P1", "P2"] {
|
|
for block: (number: String, body: String) in measureBlocks(xml, part: pid) {
|
|
#expect(
|
|
durationSum(block.body) == 4 * divisions,
|
|
"\(pid) measure \(block.number) holds \(durationSum(block.body)) divisions, expected \(4 * divisions)"
|
|
)
|
|
}
|
|
}
|
|
#expect(!xml.contains("<duration>1</duration>"))
|
|
}
|
|
|
|
/// A short part padded to its section's beat span must fill whole empty bars
|
|
/// with single whole-measure rests, not greedy largest-value rests that
|
|
/// straddle barlines and fragment into metrically-wrong groupings (a quarter
|
|
/// + dotted-half rest crossing the mid-bar strong beat reads as malformed).
|
|
@Test func sectionPadFillsWholeBarsAsMeasureRests() {
|
|
let divisions: Int = 480
|
|
// 23 quarter-beats of content → ends mid-bar (bar 6, beat 3). The 44-beat
|
|
// section span (set by the long part) leaves a 21-beat pad spanning the
|
|
// rest of bar 6 plus full bars 7-11.
|
|
let section: ScoreSection = Section(
|
|
"A",
|
|
meterMap: MeterMap(TimeSignature(4, 4))
|
|
) {
|
|
Part("lead") { for _: Int in 0..<23 { Note(.c, 4, dur: .quarter) } }
|
|
Part("bass") { for _: Int in 0..<44 { Note(.c, 2, dur: .quarter) } }
|
|
}
|
|
let score: Score = Score(sections: [section], bpm: 120)
|
|
let xml: String = score.toMusicXMLString(divisions: divisions)
|
|
|
|
// The lead part's empty bars 7-11 must each be a whole-measure rest.
|
|
var measureRestBars: Set<String> = []
|
|
for block: (number: String, body: String) in measureBlocks(xml, part: "P1") {
|
|
if block.body.contains("<rest measure=\"yes\"") {
|
|
measureRestBars.insert(block.number)
|
|
}
|
|
}
|
|
for bar: String in ["7", "8", "9", "10", "11"] {
|
|
#expect(
|
|
measureRestBars.contains(bar),
|
|
"bar \(bar) should be a single whole-measure rest"
|
|
)
|
|
}
|
|
// No fragmented one-division phantom anywhere.
|
|
#expect(!xml.contains("<duration>1</duration>"))
|
|
}
|
|
}
|