// DynamicCurveTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation import Testing @testable import LyrebirdSynth // MARK: - Helpers /// Count occurrences of a substring in a string. private func count(_ substring: String, in string: String) -> Int { var result: Int = 0 var searchRange: Range = string.startIndex.. = string.range(of: substring, range: searchRange) { result += 1 searchRange = range.upperBound.. String { let part: ScorePart = ScorePart( name: "test", elements: elements ) let score: Score = Score(parts: [part]) return score.toMusicXMLString(divisions: divisions) } // MARK: - Suite @Suite("DynamicCurve") struct DynamicCurveTests { // MARK: - Dynamic.niente @Test("niente velocity is zero") func nienteVelocity() { #expect(Dynamic.niente.velocity == 0.0) } @Test("niente musicXMLDynamic is n") func nienteMusicXMLDynamic() { #expect(Dynamic.niente.musicXMLDynamic == "n") } @Test("fromMusicXMLName n returns niente") func fromMusicXMLNameN() { #expect(Dynamic.fromMusicXMLName("n") == .niente) } @Test("plain Note dyn:.niente emits n element and round-trips") func plainNoteNiente() { let xml: String = xmlForElements(Note(.c, 5, dur: .quarter, dyn: .niente)) // The dynamic direction should contain #expect(xml.contains("")) // Round-trip: the decoded score should have .niente as the dynamic guard let decoded: Score = try? Score.fromMusicXML(data: Data(xml.utf8)) else { Issue.record("Failed to decode MusicXML") return } let events: [ScoreEvent] = decoded.parts.first?.flatten() ?? [] #expect(!events.isEmpty) #expect(events.first?.velocity == Dynamic.niente.velocity) } // MARK: - 3-point messa di voce (pp < f > pp) @Test("3-point messa di voce emits correct dynamics and hairpins") func messaDiVoce() { // doubleWhole = 8 quarter-beats, 480 PPQ → noteDivisions = 3840 // Point 0 offset = 0, point 0.5 offset = 1920, point 1.0 offset = 3840 let xml: String = xmlForElements( Note(.c, 5, dur: .doubleWhole, dyn: .pp, dynamicCurve: [(0.0, .pp), (0.5, .f), (1.0, .pp)]) ) // Dynamics: at 0, at 0.5, at 1.0 #expect(xml.contains("")) #expect(xml.contains("")) // Two instances: one at the start and one at the end #expect(count("", in: xml) == 2) // One crescendo wedge start, one diminuendo wedge start #expect(xml.contains("? = xml.range(of: "crescendo") let dimRange: Range? = xml.range(of: "diminuendo") let stopFirst: Range? = xml.range(of: " = crescendoRange, let st: Range = stopFirst { #expect(cr.lowerBound < st.lowerBound) } } // MARK: - Equal-hold 4-point @Test("4-point curve with hold emits one crescendo and one diminuendo") func equalHold() { // pp -> f -> f -> pp: the hold (f->f) produces NO wedge; one < and one >. // Quarter note = 1 quarter-beat, 480 PPQ → noteDivisions = 480. // 0.0 → 0, 0.3 → 144, 0.7 → 336, 1.0 → 480 let xml: String = xmlForElements( Note(.c, 5, dur: .quarter, dyn: .pp, dynamicCurve: [(0.0, .pp), (0.3, .f), (0.7, .f), (1.0, .pp)]) ) // should appear exactly once (change-only for the same value at 0.3 and 0.7) #expect(count("", in: xml) == 1) // Exactly one crescendo (pp→f) and one diminuendo (f→pp) #expect(count(" (0.5, .f) -> (1, .niente) // crescendo starts at .niente → niente="yes" on crescendo // diminuendo ends at .niente → niente="yes" on diminuendo let xml: String = xmlForElements( Note(.c, 5, dur: .whole, dyn: .mf, dynamicCurve: [(0.0, .niente), (0.5, .f), (1.0, .niente)]) ) // Two (one at start, one at end) #expect(count("", in: xml) == 2) // Crescendo with niente="yes" #expect(xml.contains("type=\"crescendo\" niente=\"yes\"")) // Diminuendo with niente="yes" #expect(xml.contains("type=\"diminuendo\" niente=\"yes\"")) } // MARK: - Suppression of plain dyn: direction @Test("a curve-bearing note does NOT also emit its plain dyn: direction") func suppression() { // With a 3-point curve the plain must NOT appear (the curve owns // the dynamics). The curve emits pp and f but not mf. let xml: String = xmlForElements( Note(.c, 5, dur: .whole, dyn: .mf, dynamicCurve: [(0.0, .pp), (0.5, .f), (1.0, .pp)]) ) // Count blocks: should equal the number of change-points in // the curve (pp at 0, f at 0.5, pp at 1.0 = 3 blocks), NOT 3+1. let dynamicsCount: Int = count("", in: xml) #expect(dynamicsCount == 3) // mf must not appear #expect(!xml.contains("")) } // MARK: - Next-note dynamic tracker @Test("next note after a curve emits its direction when different from curve end") func nextNoteTracking() { // Curve ends at .pp; next note is .f → .f must be emitted. let xml: String = xmlForElements( Note(.c, 5, dur: .whole, dyn: .pp, dynamicCurve: [(0.0, .pp), (0.5, .f), (1.0, .pp)]), Note(.d, 5, dur: .quarter, dyn: .f) ) // appears at least once in the second note's direction // (it also appears inside the curve, so count >= 2 total). #expect(count("", in: xml) >= 2) // The last appearance is for the plain direction on the D note. // Confirm by checking the note appears after all curve directions. // We just confirm the second note's standalone direction is present // by verifying count includes an extra one beyond the curve. let dynamicsCount: Int = count("", in: xml) // Curve has 3 change-points (pp, f, pp); the following D note adds 1 more. #expect(dynamicsCount == 4) } // MARK: - Flatten transparency @Test("flatten of dynamicCurve note produces same events as unwrapped note") func flattenTransparency() { let partWithCurve: ScorePart = ScorePart( name: "test", elements: [ Note(.c, 5, dur: .whole, dyn: .mf, dynamicCurve: [(0.0, .pp), (0.5, .f), (1.0, .pp)]) ] ) let partPlain: ScorePart = ScorePart( name: "test", elements: [Note(.c, 5, dur: .whole, dyn: .mf)] ) let curveEvents: [ScoreEvent] = partWithCurve.flatten() let plainEvents: [ScoreEvent] = partPlain.flatten() #expect(curveEvents.count == plainEvents.count) if curveEvents.count == plainEvents.count { for (curveEvent, plainEvent) in zip(curveEvents, plainEvents) { #expect(curveEvent.pitch == plainEvent.pitch) #expect(curveEvent.duration == plainEvent.duration) #expect(curveEvent.velocity == plainEvent.velocity) #expect(curveEvent.beat == plainEvent.beat) } } } // MARK: - Empty dynamicCurve is byte-identical @Test("empty dynamicCurve produces byte-identical output") func emptyCurveIdentical() { let withEmpty: String = xmlForElements( Note(.c, 5, dur: .quarter, dyn: .mf, dynamicCurve: []) ) let withoutParam: String = xmlForElements( Note(.c, 5, dur: .quarter, dyn: .mf) ) #expect(withEmpty == withoutParam) } // MARK: - Chord supports dynamicCurve @Test("Chord supports dynamicCurve and emits curve directions") func chordWithCurve() { let xml: String = xmlForElements( Chord( [Pitch.named(.c, 5), Pitch.named(.e, 5), Pitch.named(.g, 5)], dur: .whole, dyn: .mp, dynamicCurve: [(0.0, .pp), (0.5, .f), (1.0, .pp)] ) ) // Curve directions must appear and plain must be suppressed. #expect(xml.contains("")) #expect(xml.contains("")) #expect(!xml.contains("")) #expect(xml.contains("")) // Voice 2's plain dynamic still emits. #expect(xml.contains("")) } // MARK: - Single-point curve @Test("single-point curve emits one dynamic at its offset, no wedge") func singlePointCurveEmitsDynamicNoWedge() { // whole note = 4 beats → 1920 divisions; position 0.5 → offset 960. let xml: String = xmlForElements( Note(.c, 5, dur: .whole, dyn: .mp, dynamicCurve: [(0.5, .mf)]) ) #expect(xml.contains("")) #expect(xml.contains("960")) #expect(count("")) // plain dyn suppressed by the curve } // MARK: - Barline-crossing curve @Test("curve on a barline-crossing note emits with offsets past the bar") func curveOnBarlineCrossingNoteIsEmitted() { // A breve (8 beats) in 4/4 crosses the barline. noteDivisions = 8*480 = // 3840; position 1.0 → 3840 (past the first bar — a // documented limitation, but the curve must still be emitted, not dropped). let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) { Part("p") { Note(.c, 5, dur: .doubleWhole, dyn: .mp, dynamicCurve: [(0.0, .pp), (0.5, .f), (1.0, .pp)]) } } let xml: String = score.toMusicXMLString() #expect(xml.contains("3840")) // Curve emitted once (on the head), not duplicated on the tied continuation. #expect(count("