// 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 = xml.range(of: ""), let partEnd: Range = xml.range( of: "", range: partStart.upperBound.. = chunk.range(of: "\"") else { continue } let number: String = String(chunk[chunk.startIndex..` of every non-chord `` in a measure body. private func durationSum(_ measureBody: String) -> Int { var total: Int = 0 let notes: [String] = measureBody.components(separatedBy: "") for note: String in notes.dropFirst() { if note.contains("") { continue } guard let open: Range = note.range(of: ""), let close: Range = note.range( of: "", range: open.upperBound..1")) } /// 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("1")) } /// 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 = [] for block: (number: String, body: String) in measureBlocks(xml, part: "P1") { if block.body.contains("1")) } }