219 lines
8.4 KiB
Swift
219 lines
8.4 KiB
Swift
// StaffPedalRoundTripTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
/// Round-trip tests for the Staff / Pedal encode→decode path.
|
|
///
|
|
/// ## What round-trips
|
|
///
|
|
/// - Event count and sounding pitches survive round-trip (flatten() on the
|
|
/// decoded Score produces the same number of events as flatten() on the source).
|
|
/// - Pedal marks (`<pedal type="start|stop">`) are decoded back to `.pedal`
|
|
/// elements in the reconstructed `ScorePart`.
|
|
/// - Per-staff clef values are decoded from `<clef number="k">` and stored on
|
|
/// the `ScorePart.clef` (first-staff clef wins for the part-level clef).
|
|
///
|
|
/// ## What does NOT round-trip (documented-lossy)
|
|
///
|
|
/// - The `.staff(n, clef, [children])` grouping structure is NOT reconstructed.
|
|
/// After decode, notes that were in Staff(1) and Staff(2) are partitioned only
|
|
/// by voice number — the structural `Staff(...)` wrapper is lost. This is
|
|
/// analogous to how tuplet structure is not reconstructed from `<time-modification>`.
|
|
/// - Multiple distinct staves collapse back to a flat multi-voice structure
|
|
/// (each voice's notes are wrapped in a `.voice(n, [...])` block, unaffected
|
|
/// by the original staff grouping).
|
|
@MainActor
|
|
@Suite("Staff and Pedal — MusicXML round-trip")
|
|
struct StaffPedalRoundTripTests {
|
|
|
|
// MARK: - Single-staff parts — regression guard
|
|
|
|
@Test func singleStaffPartRoundTripsUnchanged() throws {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("piano") {
|
|
Note(.c, 4, dur: .quarter)
|
|
Note(.d, 4, dur: .quarter)
|
|
Note(.e, 4, dur: .quarter)
|
|
Note(.f, 4, dur: .quarter)
|
|
}
|
|
}
|
|
let data: Data = score.toMusicXMLData()
|
|
let decoded: Score = try Score.fromMusicXML(data: data)
|
|
#expect(decoded.parts.count == 1)
|
|
let sourceEvents: [ScoreEvent] = score.parts[0].flatten()
|
|
let decodedEvents: [ScoreEvent] = decoded.parts[0].flatten()
|
|
#expect(decodedEvents.count == sourceEvents.count)
|
|
let sourceMidis: [Int] = sourceEvents.compactMap { $0.pitch.midiNote }.sorted()
|
|
let decodedMidis: [Int] = decodedEvents.compactMap { $0.pitch.midiNote }.sorted()
|
|
#expect(decodedMidis == sourceMidis)
|
|
}
|
|
|
|
// MARK: - Multi-staff round-trip — event count and pitches
|
|
|
|
@Test func multiStaffEventCountSurvivesRoundTrip() throws {
|
|
// Two-staff piano part with 3 events total (2 in staff 1, 1 in staff 2).
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("piano", clef: .treble) {
|
|
Staff(1) {
|
|
Voice(1) { Note(.e, 5, dur: .half) }
|
|
Voice(2) { Note(.c, 5, dur: .half) }
|
|
}
|
|
Staff(2, clef: .bass) {
|
|
Voice(3) { Note(.c, 3, dur: .whole) }
|
|
}
|
|
}
|
|
}
|
|
let sourceEvents: [ScoreEvent] = score.parts[0].flatten()
|
|
#expect(sourceEvents.count == 3)
|
|
|
|
let data: Data = score.toMusicXMLData()
|
|
let decoded: Score = try Score.fromMusicXML(data: data)
|
|
let decodedEvents: [ScoreEvent] = decoded.parts[0].flatten()
|
|
#expect(decodedEvents.count == sourceEvents.count)
|
|
}
|
|
|
|
@Test func multiStaffPitchesSurviveRoundTrip() throws {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("piano", clef: .treble) {
|
|
Staff(1) {
|
|
Voice(1) { Note(.c, 5, dur: .whole) }
|
|
}
|
|
Staff(2, clef: .bass) {
|
|
Voice(2) { Note(.c, 3, dur: .whole) }
|
|
}
|
|
}
|
|
}
|
|
let data: Data = score.toMusicXMLData()
|
|
let decoded: Score = try Score.fromMusicXML(data: data)
|
|
let decodedEvents: [ScoreEvent] = decoded.parts[0].flatten()
|
|
let midis: [Int] = decodedEvents.compactMap { $0.pitch.midiNote }.sorted()
|
|
// C3 = MIDI 48, C5 = MIDI 72
|
|
#expect(midis == [48, 72])
|
|
}
|
|
|
|
@Test func multiStaffClefSurvivesRoundTrip() throws {
|
|
// Staff 2 uses bass clef — verify the decoded part picks it up.
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("piano", clef: .treble) {
|
|
Staff(1) {
|
|
Voice(1) { Note(.c, 5, dur: .whole) }
|
|
}
|
|
Staff(2, clef: .bass) {
|
|
Voice(2) { Note(.c, 3, dur: .whole) }
|
|
}
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Sanity: encoded XML has both clef numbers.
|
|
#expect(xml.contains("<clef number=\"1\">"))
|
|
#expect(xml.contains("<clef number=\"2\">"))
|
|
|
|
let data: Data = Data(xml.utf8)
|
|
let decoded: Score = try Score.fromMusicXML(data: data)
|
|
// The decoded part's clef comes from staff 1's clef (first clef in the part
|
|
// wins for the part-level clef — existing round-trip convention).
|
|
// Staff 1 is treble, so the decoded part clef is treble.
|
|
#expect(decoded.parts[0].clef == .treble)
|
|
}
|
|
|
|
// MARK: - Pedal round-trip
|
|
|
|
@Test func pedalStartStopSurviveRoundTrip() throws {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("piano") {
|
|
Pedal(.start)
|
|
Note(.c, 4, dur: .half)
|
|
Note(.d, 4, dur: .half)
|
|
Pedal(.stop)
|
|
}
|
|
}
|
|
let data: Data = score.toMusicXMLData()
|
|
let decoded: Score = try Score.fromMusicXML(data: data)
|
|
|
|
// Event count should be preserved (2 sounding notes).
|
|
let decodedEvents: [ScoreEvent] = decoded.parts[0].flatten()
|
|
#expect(decodedEvents.count == 2)
|
|
|
|
// The decoded part should contain pedal elements.
|
|
let decodedElements: [ScoreElement] = decoded.parts[0].elements
|
|
let pedalElements: [ScoreElement] = decodedElements.filter {
|
|
if case .pedal = $0 { return true }
|
|
return false
|
|
}
|
|
#expect(pedalElements.count == 2)
|
|
|
|
// Verify start and stop are both present.
|
|
let hasStart: Bool = pedalElements.contains {
|
|
if case .pedal(let k) = $0 { return k == .start }
|
|
return false
|
|
}
|
|
let hasStop: Bool = pedalElements.contains {
|
|
if case .pedal(let k) = $0 { return k == .stop }
|
|
return false
|
|
}
|
|
#expect(hasStart)
|
|
#expect(hasStop)
|
|
}
|
|
|
|
@Test func pedalDoesNotProduceScoreEventsAfterRoundTrip() throws {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("piano") {
|
|
Pedal(.start)
|
|
Note(.c, 4, dur: .quarter)
|
|
Pedal(.stop)
|
|
}
|
|
}
|
|
let data: Data = score.toMusicXMLData()
|
|
let decoded: Score = try Score.fromMusicXML(data: data)
|
|
let events: [ScoreEvent] = decoded.parts[0].flatten()
|
|
// Only the quarter note should be an event; pedal marks are zero-duration.
|
|
#expect(events.count == 1)
|
|
#expect(events[0].pitch.midiNote == 60) // C4 = MIDI 60
|
|
}
|
|
|
|
// MARK: - Multi-staff + pedal combined round-trip
|
|
|
|
@Test func multiStaffWithPedalPreservesEventCountAndPedals() throws {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("piano", clef: .treble) {
|
|
Staff(1) {
|
|
Voice(1) { Note(.c, 5, dur: .whole) }
|
|
}
|
|
Staff(2, clef: .bass) {
|
|
Voice(2) {
|
|
Pedal(.start)
|
|
Note(.c, 3, dur: .whole)
|
|
Pedal(.stop)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let data: Data = score.toMusicXMLData()
|
|
let decoded: Score = try Score.fromMusicXML(data: data)
|
|
|
|
// Event count: 2 sounding notes (one per staff).
|
|
let decodedEvents: [ScoreEvent] = decoded.parts[0].flatten()
|
|
#expect(decodedEvents.count == 2)
|
|
|
|
// Pedal elements should be present somewhere in the decoded part.
|
|
func containsPedal(_ elements: [ScoreElement]) -> Bool {
|
|
for element: ScoreElement in elements {
|
|
switch element {
|
|
case .pedal: return true
|
|
case .voice(_, let children): if containsPedal(children) { return true }
|
|
case .staff(_, _, let children): if containsPedal(children) { return true }
|
|
default: break
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
#expect(containsPedal(decoded.parts[0].elements))
|
|
}
|
|
}
|