v2.4.0 release: event patterns + live event conductor

This commit is contained in:
2026-12-03 08:45:07 -08:00
parent a175c437de
commit 6da3615707
40 changed files with 4223 additions and 19 deletions
@@ -0,0 +1,156 @@
// ConductorDemoController.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import Observation
import LyrebirdSynth
/// Owns the engine lifecycle, the ``EventConductor`` and the on/off state of the two parts for
/// ``ConductorDemoView``.
///
/// A pattern is single-shot, so each toggle-on builds a fresh ``EventPattern`` and hands it to the
/// conductor, entering on the next beat. ``EventConductor/add(_:quantize:)`` returns an
/// ``EventConductorHandle``; toggling a part off calls ``EventConductorHandle/stop()`` on its
/// handle, which releases that part alone (its gated voices ring out) without touching the other.
///
/// `@unchecked Sendable`: main-thread-confined — every method is called from a button action.
@Observable
final class ConductorDemoController: @unchecked Sendable {
/// Shared tempo of the conductor and both parts, in beats per minute.
private static let tempoBPM: Double = 120.0
/// Top part: events per beat grid, in beats.
private static let topEventBeats: Double = 0.5
/// Bottom part: one event per beat.
private static let bottomEventBeats: Double = 1.0
/// Sustain fraction of each event's duration.
private static let legato: Double = 0.8
/// Top part: A-minor pentatonic phrase.
private static let topPhrase: [Pitch] = [
.named(.a, 4), .named(.c, 5), .named(.d, 5), .named(.e, 5),
.named(.g, 5), .named(.e, 5), .named(.d, 5), .named(.c, 5),
]
/// Bottom part: a simple root-fifth bass figure.
private static let bottomPhrase: [Pitch] = [
.named(.a, 2), .named(.a, 2), .named(.g, 2), .named(.e, 2),
]
private static let topAmpLow: Double = 0.10
private static let topAmpHigh: Double = 0.18
private static let bottomAmpLow: Double = 0.20
private static let bottomAmpHigh: Double = 0.28
private let engine: SynthesisEngine
/// `true` while the engine (and conductor) is running.
private(set) var engineRunning: Bool = false
/// `true` while the top part is switched on.
private(set) var topOn: Bool = false
/// `true` while the bottom part is switched on.
private(set) var bottomOn: Bool = false
@ObservationIgnored private var conductor: EventConductor?
@ObservationIgnored private var topHandle: EventConductorHandle?
@ObservationIgnored private var bottomHandle: EventConductorHandle?
init(engine: SynthesisEngine) {
self.engine = engine
}
/// Starts the engine and a fresh conductor, or stops both (releasing any playing parts).
func toggleEngine() {
if engineRunning {
topHandle?.stop()
bottomHandle?.stop()
conductor?.stop()
conductor = nil
topHandle = nil
bottomHandle = nil
topOn = false
bottomOn = false
MainActor.assumeIsolated { engine.stop() }
engineRunning = false
} else {
do {
try MainActor.assumeIsolated { try engine.start() }
} catch {
print("[Lyrebird] engine.start failed: \(error)")
return
}
let newConductor: EventConductor = EventConductor(tempo: ConductorDemoController.tempoBPM)
MainActor.assumeIsolated { newConductor.start(on: engine) }
conductor = newConductor
engineRunning = true
}
}
/// Toggles the top (melodic) part. Entering is quantised to the next beat; stopping releases it
/// immediately through its handle.
func toggleTop() {
guard let active: EventConductor = conductor else { return }
if topOn {
topHandle?.stop()
topHandle = nil
topOn = false
} else {
let pattern: EventPattern<ConductorDemoVoice> = makePattern(
phrase: ConductorDemoController.topPhrase,
eventBeats: ConductorDemoController.topEventBeats,
ampLow: ConductorDemoController.topAmpLow,
ampHigh: ConductorDemoController.topAmpHigh
)
topHandle = active.add(pattern, quantize: .nextBeat)
topOn = true
}
}
/// Toggles the bottom (bass) part. Entering is quantised to the next beat; stopping releases it
/// immediately through its handle.
func toggleBottom() {
guard let active: EventConductor = conductor else { return }
if bottomOn {
bottomHandle?.stop()
bottomHandle = nil
bottomOn = false
} else {
let pattern: EventPattern<ConductorDemoVoice> = makePattern(
phrase: ConductorDemoController.bottomPhrase,
eventBeats: ConductorDemoController.bottomEventBeats,
ampLow: ConductorDemoController.bottomAmpLow,
ampHigh: ConductorDemoController.bottomAmpHigh
)
bottomHandle = active.add(pattern, quantize: .nextBeat)
bottomOn = true
}
}
/// Builds a fresh, endlessly looping pattern: both the phrase and the duration are infinite
/// `.sequence` patterns, so the part plays until its handle is stopped.
private func makePattern(
phrase: [Pitch],
eventBeats: Double,
ampLow: Double,
ampHigh: Double
) -> EventPattern<ConductorDemoVoice> {
let freqs: [Double] = phrase.compactMap(\.frequency)
// Key-path formation on a `@LiveParam` needs the main actor.
return MainActor.assumeIsolated {
EventPattern(make: { ConductorDemoVoice() })
.bind(\.freq, .sequence(freqs))
.bind(\.amp, .uniformRandom(low: ampLow, high: ampHigh))
.dur(.sequence([eventBeats]))
.legato(ConductorDemoController.legato)
.tempo(ConductorDemoController.tempoBPM)
}
}
}