318 lines
13 KiB
Swift
318 lines
13 KiB
Swift
// 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.Index> = string.startIndex..<string.endIndex
|
|
while let range: Range<String.Index> = string.range(of: substring, range: searchRange) {
|
|
result += 1
|
|
searchRange = range.upperBound..<string.endIndex
|
|
}
|
|
return result
|
|
}
|
|
|
|
/// Build a one-part score containing the supplied elements and return the
|
|
/// MusicXML string using the default `divisions` (480 PPQ).
|
|
private func xmlForElements(
|
|
_ elements: ScoreElement...,
|
|
divisions: Int = Score.defaultMusicXMLDivisions
|
|
) -> 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 <n/>
|
|
#expect(xml.contains("<n/>"))
|
|
// 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: <pp/> at 0, <f/> at 0.5, <pp/> at 1.0
|
|
#expect(xml.contains("<pp/>"))
|
|
#expect(xml.contains("<f/>"))
|
|
// Two <pp/> instances: one at the start and one at the end
|
|
#expect(count("<pp/>", in: xml) == 2)
|
|
// One crescendo wedge start, one diminuendo wedge start
|
|
#expect(xml.contains("<wedge type=\"crescendo\""))
|
|
#expect(xml.contains("<wedge type=\"diminuendo\""))
|
|
// Two wedge stops
|
|
#expect(count("<wedge type=\"stop\"", in: xml) == 2)
|
|
// Check offsets: 0 at position 0, 1920 at 0.5, 3840 at 1.0
|
|
let offset0: String = "<offset>0</offset>"
|
|
let offset1920: String = "<offset>1920</offset>"
|
|
let offset3840: String = "<offset>3840</offset>"
|
|
#expect(xml.contains(offset0))
|
|
#expect(xml.contains(offset1920))
|
|
#expect(xml.contains(offset3840))
|
|
// Crescendo wedge is at offset 0, stop at 1920
|
|
// Diminuendo wedge is at offset 1920, stop at 3840
|
|
// Verify the crescendo/stop ordering: crescendo precedes its stop
|
|
let crescendoRange: Range<String.Index>? = xml.range(of: "crescendo")
|
|
let dimRange: Range<String.Index>? = xml.range(of: "diminuendo")
|
|
let stopFirst: Range<String.Index>? = xml.range(of: "<wedge type=\"stop\"")
|
|
#expect(crescendoRange != nil)
|
|
#expect(dimRange != nil)
|
|
#expect(stopFirst != nil)
|
|
if let cr: Range<String.Index> = crescendoRange,
|
|
let st: Range<String.Index> = 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)])
|
|
)
|
|
// <f/> should appear exactly once (change-only for the same value at 0.3 and 0.7)
|
|
#expect(count("<f/>", in: xml) == 1)
|
|
// Exactly one crescendo (pp→f) and one diminuendo (f→pp)
|
|
#expect(count("<wedge type=\"crescendo\"", in: xml) == 1)
|
|
#expect(count("<wedge type=\"diminuendo\"", in: xml) == 1)
|
|
// Two stops
|
|
#expect(count("<wedge type=\"stop\"", in: xml) == 2)
|
|
// The diminuendo start is at offset corresponding to 0.7
|
|
let offset336: String = "<offset>336</offset>"
|
|
#expect(xml.contains(offset336))
|
|
}
|
|
|
|
// MARK: - Niente hairpins
|
|
|
|
@Test("niente curve sets niente attribute on wedge start")
|
|
func nienteCurve() {
|
|
// (0, .niente) -> (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 <n/> (one at start, one at end)
|
|
#expect(count("<n/>", 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 <mf/> 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 <dynamics> 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("<dynamics>", in: xml)
|
|
#expect(dynamicsCount == 3)
|
|
// mf must not appear
|
|
#expect(!xml.contains("<mf/>"))
|
|
}
|
|
|
|
// 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)
|
|
)
|
|
// <f/> appears at least once in the second note's direction
|
|
// (it also appears inside the curve, so count >= 2 total).
|
|
#expect(count("<f/>", in: xml) >= 2)
|
|
// The last <f/> 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 <dynamics> includes an extra one beyond the curve.
|
|
let dynamicsCount: Int = count("<dynamics>", 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 <mp/> must be suppressed.
|
|
#expect(xml.contains("<pp/>"))
|
|
#expect(xml.contains("<f/>"))
|
|
#expect(!xml.contains("<mp/>"))
|
|
#expect(xml.contains("<wedge type=\"crescendo\""))
|
|
#expect(xml.contains("<wedge type=\"diminuendo\""))
|
|
}
|
|
|
|
// MARK: - Multi-voice path
|
|
|
|
@Test("curve suppresses its own dyn but a sibling voice still emits")
|
|
func curveInMultiVoiceSuppressesOnlyItsOwnDynamic() {
|
|
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
|
|
Part("p") {
|
|
Voice(1) {
|
|
Note(.c, 5, dur: .whole, dyn: .mp,
|
|
dynamicCurve: [(0.0, .pp), (0.5, .f), (1.0, .pp)])
|
|
}
|
|
Voice(2) {
|
|
Note(.c, 4, dur: .whole, dyn: .ff)
|
|
}
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Voice 1's curve emits hairpins; its plain mp: is suppressed.
|
|
#expect(xml.contains("<wedge type=\"crescendo\""))
|
|
#expect(!xml.contains("<mp/>"))
|
|
// Voice 2's plain dynamic still emits.
|
|
#expect(xml.contains("<ff/>"))
|
|
}
|
|
|
|
// 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("<mf/>"))
|
|
#expect(xml.contains("<offset>960</offset>"))
|
|
#expect(count("<wedge", in: xml) == 0)
|
|
#expect(!xml.contains("<mp/>")) // 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 → <offset>3840</offset> (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("<wedge type=\"crescendo\""))
|
|
#expect(xml.contains("<wedge type=\"diminuendo\""))
|
|
#expect(xml.contains("<offset>3840</offset>"))
|
|
// Curve emitted once (on the head), not duplicated on the tied continuation.
|
|
#expect(count("<wedge type=\"crescendo\"", in: xml) == 1)
|
|
}
|
|
}
|