v2.4.0 release: event patterns + live event conductor
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
// • Linux — a GTK4 `App`/`WindowGroup` scene run by `GTK4Backend`.
|
||||
//
|
||||
// Run either with `swift run LyrebirdGUIDemo`. Pick the synth with
|
||||
// `LYREBIRD_DEMO=vector` (default) or `LYREBIRD_DEMO=sine`.
|
||||
// `LYREBIRD_DEMO=vector` (default), `sine`, or `conductor`.
|
||||
//
|
||||
// `@ViewBuilder` and the window hosts come from SwiftUI on macOS and from
|
||||
// SwiftOpenUI (re-exported through `LyrebirdUI`) on Linux — hence the single
|
||||
@@ -30,7 +30,7 @@ import LyrebirdGUIDemoKit
|
||||
let selection: DemoSelection = DemoSelection.fromEnvironment()
|
||||
|
||||
private let demoBlockSize: Int = 512
|
||||
private let demoSampleRate: Double = 44_100.0
|
||||
private let demoSampleRate: Double = 48_000.0
|
||||
private let demoScopeBufferSize: Int = 2048
|
||||
|
||||
// Oscilloscope tap on the stereo output buses. Must be set before `start()`.
|
||||
@@ -102,6 +102,8 @@ func demoContent() -> some View {
|
||||
scope: demoScope,
|
||||
engine: statusEngine
|
||||
)
|
||||
} else if selection == .conductor {
|
||||
ConductorDemoView(scope: demoScope, engine: statusEngine)
|
||||
} else {
|
||||
SineToneDemoView(
|
||||
frequencyNode: sineFreq,
|
||||
@@ -117,11 +119,14 @@ func demoContent() -> some View {
|
||||
// Start the engine and add the selected synth. `engine.start()` / `add` are
|
||||
// @MainActor; main.swift's top level is main-actor isolated, so these are direct
|
||||
// calls. On a build with no live backend the GUI still runs (silently).
|
||||
do {
|
||||
try demoEngine.start()
|
||||
try demoEngine.add(activeSynth)
|
||||
} catch {
|
||||
print("[Lyrebird] demo engine setup failed: \(error)")
|
||||
// The conductor demo is the exception: its view starts and stops the engine itself.
|
||||
if selection != .conductor {
|
||||
do {
|
||||
try demoEngine.start()
|
||||
try demoEngine.add(activeSynth)
|
||||
} catch {
|
||||
print("[Lyrebird] demo engine setup failed: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
if !isLiveAudioAvailable {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// ConductorDemoView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if canImport(SwiftUI)
|
||||
import SwiftUI
|
||||
#endif
|
||||
import LyrebirdSynth
|
||||
import LyrebirdUI
|
||||
|
||||
/// Demonstrates ``EventConductor``: start the engine, then toggle a top and a bottom part on and
|
||||
/// off. Each part loops forever while on and enters on the next beat of the conductor's clock, so
|
||||
/// the two lock together however late they are pressed. Includes the CPU status strip and an
|
||||
/// oscilloscope.
|
||||
///
|
||||
/// Written to the View-DSL subset shared by SwiftUI and SwiftOpenUI, so it runs on macOS and Linux.
|
||||
/// Buttons use the `action:label:` form, which fires reliably on both backends.
|
||||
public struct ConductorDemoView: View {
|
||||
|
||||
let scope: ScopeCapture
|
||||
let engine: SynthesisEngine
|
||||
|
||||
@State private var controller: ConductorDemoController
|
||||
|
||||
public init(scope: ScopeCapture, engine: SynthesisEngine) {
|
||||
self.scope = scope
|
||||
self.engine = engine
|
||||
_controller = State(wrappedValue: ConductorDemoController(engine: engine))
|
||||
}
|
||||
|
||||
public var body: some View {
|
||||
VStack {
|
||||
Text("Lyrebird — Event Conductor")
|
||||
.font(.headline)
|
||||
Button {
|
||||
controller.toggleEngine()
|
||||
} label: {
|
||||
Text(controller.engineRunning ? "Stop Engine" : "Start Engine")
|
||||
}
|
||||
Button {
|
||||
controller.toggleTop()
|
||||
} label: {
|
||||
Text(controller.topOn ? "Top Part: ON (tap to stop)" : "Top Part: off (starts on next beat)")
|
||||
}
|
||||
.disabled(!controller.engineRunning)
|
||||
Button {
|
||||
controller.toggleBottom()
|
||||
} label: {
|
||||
Text(controller.bottomOn
|
||||
? "Bottom Part: ON (tap to stop)"
|
||||
: "Bottom Part: off (starts on next beat)")
|
||||
}
|
||||
.disabled(!controller.engineRunning)
|
||||
LyrebirdStatusView(engine: engine)
|
||||
LyrebirdScopeView(capture: scope)
|
||||
.frame(height: 120)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// ConductorDemoVoice.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import LyrebirdSynth
|
||||
|
||||
/// A gated sine voice for the conductor demo. ``EventPattern`` holds its gate open for the note's
|
||||
/// sustain, then closes it; the envelope plays its release and frees the voice, so a part that is
|
||||
/// toggled off rings out cleanly instead of being cut.
|
||||
final class ConductorDemoVoice: SynthesizerBase, EventGated {
|
||||
|
||||
/// Envelope attack length in seconds.
|
||||
nonisolated static let attackSeconds: Double = 0.01
|
||||
|
||||
/// Envelope release length in seconds — how long a note rings after its gate closes.
|
||||
nonisolated static let releaseSeconds: Double = 0.3
|
||||
|
||||
/// Oscillator frequency in Hz. Bound per event by the pattern.
|
||||
@LiveParam var freq: Double = 440.0
|
||||
|
||||
/// Linear output gain, 0...1. Bound per event by the pattern.
|
||||
@LiveParam var amp: Double = 0.2
|
||||
|
||||
/// Envelope gate. Open by default so the attack fires when the voice is added.
|
||||
@LiveParam var gate: Double = 1.0
|
||||
|
||||
var eventGate: ParameterNode { $gate }
|
||||
|
||||
override func build() {
|
||||
let envelope: Envelope = Envelope(
|
||||
levels: [0.0, 1.0, 0.0],
|
||||
durations: [ConductorDemoVoice.attackSeconds, ConductorDemoVoice.releaseSeconds],
|
||||
curves: [.welch, .welch],
|
||||
releaseNode: 1
|
||||
)
|
||||
let shaped: EnvelopeGen.Audio = EnvelopeGen.Audio(
|
||||
envelope: envelope,
|
||||
gate: $gate,
|
||||
doneAction: .freeNode
|
||||
)
|
||||
let source: OscSin.Audio = OscSin.Audio(freq: $freq, mul: $amp)
|
||||
let mono: any ValidGeneratorInput = source * shaped
|
||||
output(bus: 0, source: Pan2(input: mono, pan: 0.0))
|
||||
}
|
||||
}
|
||||
@@ -17,10 +17,14 @@ public enum DemoSelection: String, Sendable {
|
||||
/// Stereo sine tone with frequency / amplitude / pan controls.
|
||||
case sine
|
||||
|
||||
/// Live event ensemble: engine start/stop plus top and bottom parts driven by an
|
||||
/// ``EventConductor``. The view owns the engine, so nothing starts at launch.
|
||||
case conductor
|
||||
|
||||
/// Resolve the selection from `LYREBIRD_DEMO` (defaults to `.vector`).
|
||||
public static func fromEnvironment() -> DemoSelection {
|
||||
let raw: String = ProcessInfo.processInfo.environment["LYREBIRD_DEMO"]?.lowercased() ?? "vector"
|
||||
return DemoSelection(rawValue: raw) ?? .vector
|
||||
let raw: String = ProcessInfo.processInfo.environment["LYREBIRD_DEMO"]?.lowercased() ?? "conductor"
|
||||
return DemoSelection(rawValue: raw) ?? .conductor
|
||||
}
|
||||
|
||||
/// Title-bar text for the window hosting this demo.
|
||||
@@ -28,6 +32,7 @@ public enum DemoSelection: String, Sendable {
|
||||
switch self {
|
||||
case .vector: return "Lyrebird — Vector Oscillator"
|
||||
case .sine: return "Lyrebird — Sine Tone"
|
||||
case .conductor: return "Lyrebird — Event Conductor"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user