bugfixes, better MusicXML support and 1.1.0
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
// MusicXMLDynamicCollapseTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("MusicXML redundant-dynamic collapse in multi-voice parts")
|
||||
struct MusicXMLDynamicCollapseTests {
|
||||
|
||||
/// Count non-overlapping occurrences of `needle` in `haystack`.
|
||||
private func countOccurrences(of needle: String, in haystack: String) -> Int {
|
||||
haystack.components(separatedBy: needle).count - 1
|
||||
}
|
||||
|
||||
@Test("Identical dynamics across voices on one staff collapse to a single direction")
|
||||
func identicalDynamicsOnOneStaffCollapse() {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("piano", clef: .treble) {
|
||||
Voice(1) { Note(.named(.c, 5), dur: .whole, dyn: .mp) }
|
||||
Voice(2) { Note(.named(.c, 4), dur: .whole, dyn: .mp) }
|
||||
}
|
||||
}
|
||||
// Default (collapse on): both voices open mp at beat 0 → one <mp/>.
|
||||
let collapsed: String = score.toMusicXMLString()
|
||||
#expect(countOccurrences(of: "<mp/>", in: collapsed) == 1)
|
||||
// Disabled: raw one-direction-per-voice → two <mp/>.
|
||||
let raw: String = score.toMusicXMLString(collapseRedundantDynamics: false)
|
||||
#expect(countOccurrences(of: "<mp/>", in: raw) == 2)
|
||||
}
|
||||
|
||||
@Test("Each staff of a grand staff keeps its own dynamic")
|
||||
func grandStaffCollapsesPerStaff() {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("piano", clef: .treble) {
|
||||
Staff(1) {
|
||||
Voice(1) { Note(.named(.c, 5), dur: .whole, dyn: .mp) }
|
||||
Voice(2) { Note(.named(.e, 4), dur: .whole, dyn: .mp) }
|
||||
}
|
||||
Staff(2, clef: .bass) {
|
||||
Voice(3) { Note(.named(.c, 3), dur: .whole, dyn: .mp) }
|
||||
}
|
||||
}
|
||||
}
|
||||
// Per-staff collapse: staff 1 merges V1+V2 (one mp), staff 2 keeps its own
|
||||
// (one mp) → two total, not three.
|
||||
let collapsed: String = score.toMusicXMLString()
|
||||
#expect(countOccurrences(of: "<mp/>", in: collapsed) == 2)
|
||||
// Disabled → one per voice → three.
|
||||
let raw: String = score.toMusicXMLString(collapseRedundantDynamics: false)
|
||||
#expect(countOccurrences(of: "<mp/>", in: raw) == 3)
|
||||
}
|
||||
|
||||
@Test("Different dynamics at the same onset on one staff are both emitted")
|
||||
func differentDynamicsPreserved() {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("piano", clef: .treble) {
|
||||
Voice(1) { Note(.named(.c, 5), dur: .whole, dyn: .f) }
|
||||
Voice(2) { Note(.named(.c, 4), dur: .whole, dyn: .p) }
|
||||
}
|
||||
}
|
||||
// Collapse only merges *identical* dynamics; f and p both survive.
|
||||
let collapsed: String = score.toMusicXMLString()
|
||||
#expect(countOccurrences(of: "<f/>", in: collapsed) == 1)
|
||||
#expect(countOccurrences(of: "<p/>", in: collapsed) == 1)
|
||||
}
|
||||
|
||||
@Test("Collapse record resets each measure so a recurring dynamic re-emits")
|
||||
func collapseRecordResetsPerMeasure() {
|
||||
// Two voices, three bars each: mp, f, mp. The bar-3 mp recurs at the same
|
||||
// onset as the bar-1 mp; a measure-scoped dedup record must re-emit it
|
||||
// (a stale record would wrongly suppress it).
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("piano", clef: .treble) {
|
||||
Voice(1) {
|
||||
Note(.named(.c, 5), dur: .whole, dyn: .mp)
|
||||
Note(.named(.c, 5), dur: .whole, dyn: .f)
|
||||
Note(.named(.c, 5), dur: .whole, dyn: .mp)
|
||||
}
|
||||
Voice(2) {
|
||||
Note(.named(.c, 4), dur: .whole, dyn: .mp)
|
||||
Note(.named(.c, 4), dur: .whole, dyn: .f)
|
||||
Note(.named(.c, 4), dur: .whole, dyn: .mp)
|
||||
}
|
||||
}
|
||||
}
|
||||
let collapsed: String = score.toMusicXMLString()
|
||||
// Bar 1 mp (collapsed to 1) + bar 3 mp (collapsed to 1) = 2; bar 2 f = 1.
|
||||
#expect(countOccurrences(of: "<mp/>", in: collapsed) == 2)
|
||||
#expect(countOccurrences(of: "<f/>", in: collapsed) == 1)
|
||||
}
|
||||
|
||||
@Test("Collapse applies on the barline-split emit path at a non-zero onset")
|
||||
func collapseAppliesOnBarlineSplitPath() {
|
||||
// Each voice rests a half then holds a whole note that STARTS at beat 2
|
||||
// and crosses into the next bar — driving the encoder's barline-split
|
||||
// emit site at onset 2.0 (not 0). The dynamic is emitted only before the
|
||||
// head piece (the tied continuation is suppressed), so the two voices'
|
||||
// identical mp at onset 2.0 must collapse to one.
|
||||
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
|
||||
Part("piano", clef: .treble) {
|
||||
Voice(1) {
|
||||
Rest(dur: .half)
|
||||
Note(.named(.c, 5), dur: .whole, dyn: .mp)
|
||||
}
|
||||
Voice(2) {
|
||||
Rest(dur: .half)
|
||||
Note(.named(.c, 4), dur: .whole, dyn: .mp)
|
||||
}
|
||||
}
|
||||
}
|
||||
let collapsed: String = score.toMusicXMLString()
|
||||
#expect(countOccurrences(of: "<mp/>", in: collapsed) == 1)
|
||||
let raw: String = score.toMusicXMLString(collapseRedundantDynamics: false)
|
||||
#expect(countOccurrences(of: "<mp/>", in: raw) == 2)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user