Files
Lyrebird/Sources/Lyrebird/Patterns/Events/EventSustainAware.swift
T

41 lines
2.2 KiB
Swift

// EventSustainAware.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// Opt-in refinement a voice adopts so an event pattern can tell it how long the current note
/// should sound.
///
/// An event pattern binds parameter values and handles each voice's removal, but it does not
/// own the voice's amplitude envelope — the voice does. A voice that conforms to
/// `EventSustainAware` is handed the computed sustain (in seconds) for each event, so it can
/// shape or trigger a fixed-duration, self-freeing envelope that matches the pattern's timing.
/// This keeps the division of labour central to the event-driven system: the pattern owns timing
/// and merely supplies the note's sustain; the voice owns its envelope.
///
/// Conformance is optional. A voice that does not conform still plays — ``EventPattern`` simply
/// removes it after `sustain + releaseTail` beats with no envelope hand-off.
///
/// `EventSustainAware` and ``EventGated`` are **alternative** release mechanisms — a self-timed
/// envelope versus a pattern-driven gate note-off. A voice may conform to both, but the **gate
/// wins**: when a voice is also ``EventGated``, the pattern drives release through the gate and
/// does *not* call ``setEventSustain(seconds:)``, so the two never both fire. `setEventSustain`
/// is therefore only invoked on a voice that is not gated.
///
/// - SeeAlso: ``EventPattern``, ``MonoEventPattern``, ``EventGated``
public protocol EventSustainAware: Synthesizer {
/// Informs the voice how long this note should sustain, in seconds.
///
/// Called on the main thread. ``EventPattern`` calls it once per voice, just before the
/// voice is added to the engine. ``MonoEventPattern`` calls it once per event — before each
/// `retrigger()` — on the single persistent voice, so it may already be in the engine. The
/// voice typically uses this to configure a fixed-duration envelope whose `doneAction` frees
/// or re-articulates it, so it terminates on its own schedule.
///
/// - Parameter seconds: Note duration in seconds (`sustain` beats converted at the
/// pattern's tempo). Always `≥ 0`.
@MainActor func setEventSustain(seconds: Double)
}