Files
Lyrebird/Sources/Lyrebird/Score/ScoreSectionBuilder.swift
T

103 lines
3.8 KiB
Swift

// ScoreSectionBuilder.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// Result-builder for composing ``ScoreSection`` arrays inline at the
/// ``Score`` level.
///
/// Paired with the free-function DSL constructor ``Section(_:meterMap:keySignature:tempoChanges:repeats:endBarline:parts:)``.
///
/// ```swift
/// let score = Score(bpm: 120) {
/// Section("A") {
/// Part("lead") { Note(.c, 4, dur: .whole) }
/// }
/// Section("B", meterMap: MeterMap(TimeSignature(3, 4))) {
/// Part("lead") { Note(.g, 4, dur: .half); Note(.e, 4, dur: .quarter) }
/// }
/// }
/// ```
@resultBuilder
public enum ScoreSectionBuilder {
public static func buildBlock(_ components: [ScoreSection]...) -> [ScoreSection] {
components.flatMap { $0 }
}
public static func buildExpression(_ expression: ScoreSection) -> [ScoreSection] {
[expression]
}
public static func buildExpression(_ expression: [ScoreSection]) -> [ScoreSection] {
expression
}
public static func buildOptional(_ component: [ScoreSection]?) -> [ScoreSection] {
component ?? []
}
public static func buildEither(first component: [ScoreSection]) -> [ScoreSection] {
component
}
public static func buildEither(second component: [ScoreSection]) -> [ScoreSection] {
component
}
public static func buildArray(_ components: [[ScoreSection]]) -> [ScoreSection] {
components.flatMap { $0 }
}
}
/// DSL constructor for a ``ScoreSection``.
///
/// All measure indices (`tempoChanges`, `repeats`) are LOCAL — bar 1 is the
/// first bar of this section. The ``Score`` assembler offsets them to global
/// measure numbers during lowering.
///
/// - Parameters:
/// - name: Rehearsal-mark label rendered at the section's first global measure.
/// Prefer single letters (`"A"`, `"B"`, `"C"`): rehearsal marks are MusicXML
/// system objects that editors render as an auto-advanced letter sequence, so
/// single letters round-trip reliably while custom words (`"Intro"`, `"Chorus"`)
/// are often re-sequenced and hidden by editors such as Dorico.
/// - meterMap: Local meter map. `nil` (the default) inherits the score-level
/// `timeSignature` (which itself defaults to 4/4 at ``Score`` init); provide an
/// explicit map to override regardless of the score-level setting. All measure
/// indices in `tempoChanges` and `repeats` are resolved against this map.
/// - keySignature: Optional key signature override for this section.
/// Defaults to `nil` (no per-section key override).
/// - tempoChanges: Mid-section tempo changes, local measure-indexed. Defaults
/// to `[]`.
/// - repeats: Local repeat structure. Defaults to ``RepeatStructure/none``.
/// - endBarline: Right-barline style drawn at the section's last measure.
/// `nil` (the default) leaves a normal thin barline; `.double` divides
/// sections, `.final` closes a movement / the piece. Suppressed when a
/// backward repeat sits on the same barline. See ``BarlineStyle``.
/// - parts: Result-builder block of ``ScorePart`` values present in this
/// section. Instruments absent from this section are TACET and receive
/// whole-measure rest fill.
public func Section(
_ name: String,
meterMap: MeterMap? = nil,
keySignature: KeySignature? = nil,
tempoChanges: [TempoChange] = [],
repeats: RepeatStructure = .none,
endBarline: BarlineStyle? = nil,
@ScorePartBuilder parts: () -> [ScorePart]
) -> ScoreSection {
ScoreSection(
name: name,
meterMap: meterMap,
keySignature: keySignature,
tempoChanges: tempoChanges,
repeats: repeats,
endBarline: endBarline,
parts: parts()
)
}