Files
Lyrebird/Demos/LyrebirdGUIDemo/Sources/LyrebirdGUIDemo/main.swift
T
josh 06bdcbbe92 v2.3.1 release: cross-platform LyrebirdGUIDemo + Linux demo ports + doc refresh
- New Demos/LyrebirdGUIDemo: one shared source (synths + node-based views) that
  bootstraps SwiftUI on macOS and GTK4/SwiftOpenUI on Linux; LYREBIRD_DEMO=vector|sine.
- The four CLI demos (Quickstart/Demo/NRT/Score) now build and run on Linux.
- README + Linux getting-started refreshed; LyrebirdATK row removed from the
  Extensions section pending collaborator sign-off.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-29 11:34:24 -08:00

182 lines
5.9 KiB
Swift

// main.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
// Thin cross-platform entry point. The shared synths + views live in
// `LyrebirdGUIDemoKit`; this file only bootstraps the native toolkit and opens a
// window hosting the selected demo view:
//
// • macOS — AppKit `NSApplication` + `LyrebirdWindow` (SwiftUI content).
// • 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`.
//
// `@ViewBuilder` and the window hosts come from SwiftUI on macOS and from
// SwiftOpenUI (re-exported through `LyrebirdUI`) on Linux — hence the single
// `#if canImport(SwiftUI)` import guard.
#if canImport(SwiftUI)
import SwiftUI
#endif
import Foundation
import LyrebirdSynth
import LyrebirdUI
import LyrebirdGUIDemoKit
// MARK: - Shared setup (runs on both platforms)
let selection: DemoSelection = DemoSelection.fromEnvironment()
private let demoBlockSize: Int = 512
private let demoSampleRate: Double = 44_100.0
private let demoScopeBufferSize: Int = 2048
// Oscilloscope tap on the stereo output buses. Must be set before `start()`.
let demoScope: ScopeCapture = ScopeCapture(buses: [0, 1], bufferSize: demoScopeBufferSize)
// Core Audio on Apple, ALSA on a Linux build with libasound, else a silent
// OfflineDriver. The GUI runs either way; `isLiveAudioAvailable` reports which.
let demoDriver: any AudioDriver = makeDefaultAudioDriver()
let demoEngine: SynthesisEngine = SynthesisEngine(
blockSize: demoBlockSize,
sampleRate: demoSampleRate,
numOutputChannels: 2,
numInputChannels: 0,
driver: demoDriver
)
demoEngine.scopeTap = demoScope
// Both synths are cheap to build; only the selected one is added to the engine.
// Building both keeps the node extraction below branch-free (no force-unwrap).
let vectorSynth: VectorOscillatorUI = VectorOscillatorUI()
let sineSynth: SineToneUI = SineToneUI()
let activeSynth: SynthesizerBase = (selection == .vector) ? vectorSynth : sineSynth
// Extract the projected parameter nodes here, in main.swift's top-level
// `@MainActor` context: `SynthesizerBase` is `@MainActor`, so `synth.$…` is only
// reachable on the main actor. The nodes are not actor-isolated, so the
// (nonisolated, on Linux) view body can read them freely.
let vectorX: UIParamNode = vectorSynth.$x
let vectorY: UIParamNode = vectorSynth.$y
let vectorFreq: UIParamNode = vectorSynth.$frequency
let vectorAmp: UIParamNode = vectorSynth.$amplitude
let sineFreq: UIParamNode = sineSynth.$frequency
let sineAmp: UIParamNode = sineSynth.$amplitude
let sinePan: UIParamNode = sineSynth.$pan
// A static example envelope for the sine demo's envelope plot.
let demoEnvelope: Envelope = Envelope(
levels: [0.0001, 1.0, 0.6, 0.4, 0.0001],
durations: [0.05, 0.15, 0.4, 0.3],
curves: [.exponential, .exponential, .lin, .exponential],
loopNode: 2,
releaseNode: 4
)
// `SynthesisEngine` is not Sendable; the status view only reads plain CPU-load
// doubles off it, and this demo runs entirely on the UI thread, so a nonisolated
// handle is safe (mirrors the Linux UI demo).
nonisolated(unsafe) let statusEngine: SynthesisEngine = demoEngine
/// Builds the view for the active selection. `if`/`else` maps to the DSL's
/// `buildEither`, which both backends provide.
///
/// Isolation differs by backend: SwiftUI's `View` is `@MainActor`, so on macOS
/// this must be `@MainActor` to construct the view (and it is called from
/// AppKit's main-actor launch). SwiftOpenUI's `View` is nonisolated, so on Linux
/// this stays nonisolated and is callable from the nonisolated scene closure.
#if canImport(SwiftUI)
@MainActor
#endif
@ViewBuilder
func demoContent() -> some View {
if selection == .vector {
VectorOscillatorDemoView(
xNode: vectorX,
yNode: vectorY,
frequencyNode: vectorFreq,
amplitudeNode: vectorAmp,
scope: demoScope,
engine: statusEngine
)
} else {
SineToneDemoView(
frequencyNode: sineFreq,
amplitudeNode: sineAmp,
panNode: sinePan,
scope: demoScope,
engine: statusEngine,
envelope: demoEnvelope
)
}
}
// 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)")
}
if !isLiveAudioAvailable {
print("[Lyrebird] no live audio backend — the controls still work, but there is no sound.")
}
// MARK: - macOS front end (AppKit + SwiftUI)
#if os(macOS)
import AppKit
final class DemoAppDelegate: NSObject, NSApplicationDelegate {
var window: LyrebirdWindow?
func applicationDidFinishLaunching(_ notification: Notification) {
let w: LyrebirdWindow = LyrebirdWindow(
title: selection.windowTitle,
width: 380,
height: 620
)
w.open { demoContent() }
window = w
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
true
}
func applicationWillTerminate(_ notification: Notification) {
demoEngine.stop()
}
}
MainActor.assumeIsolated {
let app: NSApplication = NSApplication.shared
app.setActivationPolicy(.regular)
let delegate: DemoAppDelegate = DemoAppDelegate()
app.delegate = delegate
app.run()
}
// MARK: - Linux front end (GTK4 + SwiftOpenUI)
#else
struct DemoApp: App {
var body: some Scene {
WindowGroup(selection.windowTitle) {
demoContent()
}
}
}
GTK4Backend().run(DemoApp.self)
#endif