94 lines
3.4 KiB
Swift
94 lines
3.4 KiB
Swift
// ScorePartBuilder.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
|
|
/// Result-builder for composing ``ScorePart`` arrays inline at the
|
|
/// ``Score`` level.
|
|
///
|
|
/// Paired with the free-function DSL constructor ``Part(_:channel:clef:elements:)``.
|
|
@resultBuilder
|
|
public enum ScorePartBuilder {
|
|
|
|
public static func buildBlock(_ components: [ScorePart]...) -> [ScorePart] {
|
|
components.flatMap { $0 }
|
|
}
|
|
|
|
public static func buildExpression(_ expression: ScorePart) -> [ScorePart] {
|
|
[expression]
|
|
}
|
|
|
|
public static func buildExpression(_ expression: [ScorePart]) -> [ScorePart] {
|
|
expression
|
|
}
|
|
|
|
public static func buildOptional(_ component: [ScorePart]?) -> [ScorePart] {
|
|
component ?? []
|
|
}
|
|
|
|
public static func buildEither(first component: [ScorePart]) -> [ScorePart] {
|
|
component
|
|
}
|
|
|
|
public static func buildEither(second component: [ScorePart]) -> [ScorePart] {
|
|
component
|
|
}
|
|
|
|
public static func buildArray(_ components: [[ScorePart]]) -> [ScorePart] {
|
|
components.flatMap { $0 }
|
|
}
|
|
}
|
|
|
|
/// DSL constructor for a ``ScorePart``.
|
|
///
|
|
/// - Parameters:
|
|
/// - name: Part name (e.g. "lead", "bass", "drums").
|
|
/// - channel: MIDI channel hint `[0, 15]`. Default `0`.
|
|
/// - clef: Clef for MusicXML encoding. Default ``Clef/auto``, which infers
|
|
/// ``Clef/treble`` or ``Clef/bass`` from the part's median MIDI pitch at
|
|
/// encode time. Pass an explicit value to override (e.g. `clef: .bass` for
|
|
/// a bass-voice part that might start on high notes).
|
|
/// - keySignature: Per-part key signature override. When non-nil, this part
|
|
/// emits its own `<key>` block instead of inheriting ``Score/keySignature``.
|
|
/// Use for transposing instruments whose written key differs from the
|
|
/// concert key (e.g. a B♭ clarinet written in A major when the concert key
|
|
/// is G major). Defaults to `nil` (inherit score-level key).
|
|
/// - transpose: Transpose specification for this part. When non-nil, a
|
|
/// `<transpose>` block is emitted in measure-1 `<attributes>` after
|
|
/// `<clef>`. Describes written→sounding pitch interval. Use one of the
|
|
/// named conveniences on ``Transpose`` (e.g. `.bFlat`, `.fHorn`) or
|
|
/// provide custom `chromatic`/`diatonic`/`octaveChange` values.
|
|
/// Defaults to `nil` (non-transposing, no `<transpose>` element emitted).
|
|
/// - meterMap: Per-part meter-map override for polymetric music. When non-nil,
|
|
/// this part uses its own complete ``MeterMap`` instead of the score-level
|
|
/// ``Score/meterMap`` — an all-or-nothing override. Defaults to `nil`
|
|
/// (inherit the score map).
|
|
/// - id: Cross-section identity key. When `nil` (the default), `id` is set
|
|
/// equal to `name`. Used by ``ScoreSection`` lowering to match the same
|
|
/// instrument across sections even when the display `name` changes.
|
|
/// - elements: Result-builder block of ``ScoreElement``.
|
|
public func Part(
|
|
_ name: String,
|
|
channel: Int = 0,
|
|
clef: Clef = .auto,
|
|
keySignature: KeySignature? = nil,
|
|
transpose: Transpose? = nil,
|
|
meterMap: MeterMap? = nil,
|
|
id: String? = nil,
|
|
@ScoreBuilder elements: () -> [ScoreElement]
|
|
) -> ScorePart {
|
|
ScorePart(
|
|
name: name,
|
|
channel: channel,
|
|
clef: clef,
|
|
keySignature: keySignature,
|
|
transpose: transpose,
|
|
meterMap: meterMap,
|
|
elements: elements(),
|
|
id: id
|
|
)
|
|
}
|