b6fdee8bfc
Consolidated release: the cross-platform Linux port (engine, generators, CLI, REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive. See RELEASES.md and LINUX_README.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
471 lines
15 KiB
Swift
471 lines
15 KiB
Swift
// LyrebirdProcessView.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#if canImport(SwiftUI)
|
|
import SwiftUI
|
|
import LyrebirdSynth
|
|
|
|
/// A control panel for driving a single `LyrebirdProcess`.
|
|
///
|
|
/// Displays the process ID, a running indicator (idle / running / releasing),
|
|
/// a master amplitude slider, and Start / Release buttons:
|
|
///
|
|
/// - **Start** — calls `LyrebirdProcess.play(engine:)`. Disabled while the
|
|
/// process is already running.
|
|
/// - **Release** — calls `LyrebirdProcess.release(reltime:)` with `nil`
|
|
/// (envelope-defined release tail). Disabled when the process is idle or
|
|
/// already releasing.
|
|
///
|
|
/// The view polls the process at 10 Hz so external lifecycle changes (a
|
|
/// `LyrebirdEventList` releasing the process, the envelope reaching its
|
|
/// natural end, etc.) stay reflected on screen.
|
|
///
|
|
/// ## Hosting
|
|
///
|
|
/// Embed inside any SwiftUI parent:
|
|
///
|
|
/// ```swift
|
|
/// LyrebirdProcessView(process: proc, engine: engine)
|
|
/// .padding()
|
|
/// ```
|
|
///
|
|
/// On macOS, `presentInWindow(process:engine:title:width:height:)` opens a
|
|
/// standalone `LyrebirdWindow`:
|
|
///
|
|
/// ```swift
|
|
/// let win = LyrebirdProcessView.presentInWindow(process: proc, engine: engine)
|
|
/// // … later …
|
|
/// win.close()
|
|
/// ```
|
|
///
|
|
/// ## Threading
|
|
///
|
|
/// The view body runs on the main thread. `LyrebirdProcess.play(engine:)` /
|
|
/// `release(reltime:)` are documented as main-thread-safe; both transport
|
|
/// actions are dispatched from `@MainActor` SwiftUI contexts.
|
|
public struct LyrebirdProcessView: View {
|
|
|
|
public let process: LyrebirdProcess
|
|
public let engine: SynthesisEngine
|
|
|
|
@State private var isRunning: Bool = false
|
|
@State private var isReleasing: Bool = false
|
|
@State private var amp: Double = 1.0
|
|
|
|
private static let pollIntervalNs: UInt64 = 100_000_000
|
|
|
|
/// Creates a process control view bound to the given process.
|
|
///
|
|
/// - Parameters:
|
|
/// - process: The `LyrebirdProcess` to control.
|
|
/// - engine: The `SynthesisEngine` used by the **Start** button.
|
|
public init(
|
|
process: LyrebirdProcess,
|
|
engine: SynthesisEngine
|
|
) {
|
|
self.process = process
|
|
self.engine = engine
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(alignment: .leading, spacing: 12) {
|
|
header
|
|
Divider()
|
|
ampRow
|
|
Divider()
|
|
transportRow
|
|
}
|
|
.padding(12)
|
|
.background(Color.lyrebirdControlBackground)
|
|
.frame(minWidth: 280)
|
|
.task {
|
|
// Seed amp from the process before the first poll so the slider does
|
|
// not snap from a stale default while the process is still idle.
|
|
amp = process.amp
|
|
while !Task.isCancelled {
|
|
refresh()
|
|
try? await Task.sleep(nanoseconds: LyrebirdProcessView.pollIntervalNs)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Sub-rows
|
|
|
|
private var header: some View {
|
|
HStack(spacing: 8) {
|
|
Circle()
|
|
.fill(stateColor)
|
|
.frame(width: 10, height: 10)
|
|
Text(process.id)
|
|
.font(.headline)
|
|
Spacer()
|
|
Text(stateLabel)
|
|
.font(.caption.monospaced())
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
|
|
private var ampRow: some View {
|
|
HStack(spacing: 8) {
|
|
Text("Amp")
|
|
.font(.caption.monospaced())
|
|
.foregroundStyle(.secondary)
|
|
.frame(width: 36, alignment: .leading)
|
|
Slider(value: Binding(
|
|
get: { dbFromAmp(amp) },
|
|
set: { newDb in
|
|
let newAmp: Double = ampFromDb(newDb)
|
|
amp = newAmp
|
|
process.amp = newAmp
|
|
}
|
|
), in: LyrebirdProcessView.minDb ... maxDb)
|
|
Text(dbDisplay(dbFromAmp(amp)))
|
|
.font(.caption.monospaced())
|
|
.foregroundStyle(.primary)
|
|
.frame(width: 56, alignment: .trailing)
|
|
}
|
|
}
|
|
|
|
// MARK: - dB <-> linear amplitude (shared with `LyrebirdEventListView` via `DbSliderConversion`)
|
|
|
|
private static let minDb: Double = DbSliderConversion.minDb
|
|
|
|
/// Per-process slider ceiling, sourced from ``LyrebirdProcess/maxUIAmpDb``.
|
|
/// Defaults to `+12` dB; raised when the process opts in to extra headroom.
|
|
private var maxDb: Double { process.maxUIAmpDb }
|
|
|
|
private func ampFromDb(_ db: Double) -> Double { DbSliderConversion.ampFromDb(db) }
|
|
private func dbFromAmp(_ amp: Double) -> Double { DbSliderConversion.dbFromAmp(amp, maxDb: maxDb) }
|
|
private func dbDisplay(_ db: Double) -> String { DbSliderConversion.dbDisplay(db) }
|
|
|
|
private var transportRow: some View {
|
|
HStack(spacing: 8) {
|
|
Button("Start") {
|
|
process.play(engine: engine)
|
|
refresh()
|
|
}
|
|
.disabled(isRunning)
|
|
|
|
Button("Release") {
|
|
process.release()
|
|
refresh()
|
|
}
|
|
.disabled(!isRunning || isReleasing)
|
|
|
|
Spacer()
|
|
}
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private func refresh() {
|
|
isRunning = process.isRunning
|
|
isReleasing = process.isReleasing
|
|
amp = process.amp
|
|
}
|
|
|
|
private var stateColor: Color {
|
|
if isReleasing { return .yellow }
|
|
if isRunning { return .green }
|
|
return .secondary.opacity(0.4)
|
|
}
|
|
|
|
private var stateLabel: String {
|
|
if isReleasing { return "Releasing" }
|
|
if isRunning { return "Running" }
|
|
return "Idle"
|
|
}
|
|
}
|
|
|
|
// MARK: - Standalone window (macOS)
|
|
|
|
#if os(macOS)
|
|
extension LyrebirdProcessView {
|
|
|
|
/// Opens a `LyrebirdWindow` hosting a process control view.
|
|
///
|
|
/// Use when no parent SwiftUI view is available — a script, REPL, or CLI
|
|
/// demo can call this to surface process transport in its own window. The
|
|
/// returned `LyrebirdWindow` retains the AppKit window; close it with
|
|
/// `.close()`.
|
|
///
|
|
/// - Parameters:
|
|
/// - process: The `LyrebirdProcess` to control.
|
|
/// - engine: The `SynthesisEngine` used by the **Start** button.
|
|
/// - title: Window title bar text. Defaults to the process ID.
|
|
/// - width: Initial window width in points. Defaults to `300`.
|
|
/// - height: Initial window height in points. Defaults to `180`.
|
|
/// - Returns: The `LyrebirdWindow` hosting the view. Caller retains it to
|
|
/// keep the window alive.
|
|
@MainActor
|
|
@discardableResult
|
|
public static func presentInWindow(
|
|
process: LyrebirdProcess,
|
|
engine: SynthesisEngine,
|
|
title: String? = nil,
|
|
width: CGFloat = 300,
|
|
height: CGFloat = 180
|
|
) -> LyrebirdWindow {
|
|
let win: LyrebirdWindow = LyrebirdWindow(
|
|
title: title ?? process.id,
|
|
width: width,
|
|
height: height
|
|
)
|
|
win.open {
|
|
LyrebirdProcessView(process: process, engine: engine)
|
|
}
|
|
return win
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#else
|
|
// Linux/GTK4 counterpart to `LyrebirdUI.LyrebirdProcessView`: a control panel for
|
|
// one `LyrebirdProcess` (state dot + id, dB amp slider, Start/Release). The 10 Hz
|
|
// lifecycle poll moves off SwiftOpenUI's off-main `.task` onto a `Timer` on
|
|
// `RunLoop.main` inside an `@Observable` model — the amp/running/releasing state
|
|
// lives there so the `@Bindable` body tracks it. dB↔amp maths reuse the
|
|
// cross-platform `DbSliderConversion`.
|
|
//
|
|
// The amp control uses SwiftOpenUI's native `Slider` (GtkScale). Unlike the
|
|
// Canvas-drawn `ParameterSlider`, a native scale does not live-track other
|
|
// controls mid-drag — acceptable here (a single transport slider, not a linked
|
|
// modulation source).
|
|
//
|
|
// SwiftOpenUI is always present in this target (see Package.swift).
|
|
import SwiftOpenUI
|
|
import LyrebirdSynth
|
|
import Observation
|
|
import Foundation
|
|
|
|
/// 10 Hz process lifecycle poll interval.
|
|
private let linuxProcessPollIntervalSeconds: TimeInterval = 0.1
|
|
|
|
/// Polls one `LyrebirdProcess`'s lifecycle + amp on a main-thread timer and
|
|
/// publishes them for ``LyrebirdProcessView``. Internal — only the view is public.
|
|
///
|
|
/// `@unchecked Sendable`: main-thread-confined; the `Timer` block is `@Sendable`,
|
|
/// same rationale as `LyrebirdScopeModel`.
|
|
@Observable
|
|
final class LinuxProcessModel: @unchecked Sendable {
|
|
|
|
var isRunning: Bool = false
|
|
var isReleasing: Bool = false
|
|
var amp: Double = 1.0
|
|
|
|
@ObservationIgnored let process: LyrebirdProcess
|
|
@ObservationIgnored private var timer: Timer?
|
|
|
|
init(process: LyrebirdProcess) {
|
|
self.process = process
|
|
// `amp` is @MainActor; construction happens on the GTK main thread.
|
|
self.amp = MainActor.assumeIsolated { process.amp }
|
|
}
|
|
|
|
deinit {
|
|
timer?.invalidate()
|
|
}
|
|
|
|
func start() {
|
|
guard timer == nil else { return }
|
|
refresh()
|
|
let t: Timer = Timer.scheduledTimer(
|
|
withTimeInterval: linuxProcessPollIntervalSeconds,
|
|
repeats: true
|
|
) { [weak self] _ in
|
|
self?.refresh()
|
|
}
|
|
timer = t
|
|
}
|
|
|
|
func stop() {
|
|
timer?.invalidate()
|
|
timer = nil
|
|
}
|
|
|
|
// SwiftOpenUI view/timer callbacks are nonisolated but always run on the GTK
|
|
// main thread, so `assumeIsolated` reaches `LyrebirdProcess`'s @MainActor
|
|
// `amp` safely. `isRunning` / `isReleasing` are nonisolated already.
|
|
func refresh() {
|
|
isRunning = process.isRunning
|
|
isReleasing = process.isReleasing
|
|
amp = MainActor.assumeIsolated { process.amp }
|
|
}
|
|
}
|
|
|
|
/// A control panel for a single `LyrebirdProcess` on the Linux/GTK4 UI. Mirrors
|
|
/// `LyrebirdUI.LyrebirdProcessView`; same public API/init as the Apple side.
|
|
public struct LyrebirdProcessView: View {
|
|
|
|
public let process: LyrebirdProcess
|
|
public let engine: SynthesisEngine
|
|
|
|
@State private var model: LinuxProcessModel
|
|
|
|
// Layout constants (points).
|
|
private let dotSize: Double = 10
|
|
private let ampLabelWidth: Double = 36
|
|
private let dbReadoutWidth: Double = 56
|
|
private let disabledDotOpacity: Double = 0.4
|
|
|
|
/// Creates a process control view bound to the given process.
|
|
///
|
|
/// - Parameters:
|
|
/// - process: The `LyrebirdProcess` to control.
|
|
/// - engine: The `SynthesisEngine` used by the **Start** button.
|
|
public init(
|
|
process: LyrebirdProcess,
|
|
engine: SynthesisEngine
|
|
) {
|
|
self.process = process
|
|
self.engine = engine
|
|
_model = State(wrappedValue: LinuxProcessModel(process: process))
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(alignment: .leading, spacing: 12) {
|
|
header
|
|
Divider()
|
|
ampRow
|
|
Divider()
|
|
transportRow
|
|
}
|
|
.padding(12)
|
|
.background(Color.lyrebirdControlBackground)
|
|
.frame(minWidth: 280)
|
|
.onAppear { model.start() }
|
|
.onDisappear { model.stop() }
|
|
}
|
|
|
|
// MARK: - Sub-rows
|
|
|
|
private var header: some View {
|
|
HStack(spacing: 8) {
|
|
Circle()
|
|
.fill(stateColor)
|
|
.frame(width: dotSize, height: dotSize)
|
|
Text(process.id)
|
|
.font(.headline)
|
|
Spacer()
|
|
Text(stateLabel)
|
|
.font(.caption)
|
|
.monospacedDigit()
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
|
|
private var ampRow: some View {
|
|
HStack(spacing: 8) {
|
|
Text("Amp")
|
|
.font(.caption)
|
|
.monospacedDigit()
|
|
.foregroundStyle(.secondary)
|
|
.frame(width: ampLabelWidth, alignment: .leading)
|
|
Slider(value: Binding(
|
|
get: { dbFromAmp(model.amp) },
|
|
set: { newDb in
|
|
let newAmp: Double = ampFromDb(newDb)
|
|
model.amp = newAmp
|
|
// Bind `process` (Sendable) to a local so the @MainActor
|
|
// closure captures it, not `self` (the non-Sendable View).
|
|
let p: LyrebirdProcess = process
|
|
MainActor.assumeIsolated { p.amp = newAmp }
|
|
}
|
|
), in: LyrebirdProcessView.minDb ... maxDb)
|
|
Text(dbDisplay(dbFromAmp(model.amp)))
|
|
.font(.caption)
|
|
.monospacedDigit()
|
|
.foregroundStyle(.primary)
|
|
.frame(width: dbReadoutWidth, alignment: .trailing)
|
|
}
|
|
}
|
|
|
|
// MARK: - dB <-> linear amplitude (shared via `DbSliderConversion`)
|
|
|
|
private static let minDb: Double = DbSliderConversion.minDb
|
|
|
|
/// Per-process slider ceiling, sourced from ``LyrebirdProcess/maxUIAmpDb``.
|
|
private var maxDb: Double { process.maxUIAmpDb }
|
|
|
|
private func ampFromDb(_ db: Double) -> Double { DbSliderConversion.ampFromDb(db) }
|
|
private func dbFromAmp(_ amp: Double) -> Double { DbSliderConversion.dbFromAmp(amp, maxDb: maxDb) }
|
|
private func dbDisplay(_ db: Double) -> String { DbSliderConversion.dbDisplay(db) }
|
|
|
|
private var transportRow: some View {
|
|
HStack(spacing: 8) {
|
|
Button("Start") {
|
|
// `process` is Sendable; `engine` is not, so bind it via
|
|
// `nonisolated(unsafe)` (safe — all runs on the GTK main thread).
|
|
// Capturing these locals keeps `self` (the View) out of the
|
|
// @MainActor closure.
|
|
let p: LyrebirdProcess = process
|
|
nonisolated(unsafe) let e: SynthesisEngine = engine
|
|
MainActor.assumeIsolated { p.play(engine: e) }
|
|
model.refresh()
|
|
}
|
|
.disabled(model.isRunning)
|
|
|
|
Button("Release") {
|
|
let p: LyrebirdProcess = process
|
|
MainActor.assumeIsolated { p.release() }
|
|
model.refresh()
|
|
}
|
|
.disabled(!model.isRunning || model.isReleasing)
|
|
|
|
Spacer()
|
|
}
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private var stateColor: Color {
|
|
if model.isReleasing { return .yellow }
|
|
if model.isRunning { return .green }
|
|
return Color.secondary.opacity(disabledDotOpacity)
|
|
}
|
|
|
|
private var stateLabel: String {
|
|
if model.isReleasing { return "Releasing" }
|
|
if model.isRunning { return "Running" }
|
|
return "Idle"
|
|
}
|
|
}
|
|
|
|
// MARK: - Standalone window (Linux)
|
|
|
|
extension LyrebirdProcessView {
|
|
|
|
/// Opens a `LyrebirdWindow` hosting a process control view. Requires a
|
|
/// running `GtkApplication` loop.
|
|
///
|
|
/// - Parameters:
|
|
/// - process: The `LyrebirdProcess` to control.
|
|
/// - engine: The `SynthesisEngine` used by the **Start** button.
|
|
/// - title: Window title-bar text. Defaults to the process ID.
|
|
/// - width: Initial window width in points. Defaults to `300`.
|
|
/// - height: Initial window height in points. Defaults to `180`.
|
|
/// - Returns: The `LyrebirdWindow` hosting the view.
|
|
@discardableResult
|
|
public static func presentInWindow(
|
|
process: LyrebirdProcess,
|
|
engine: SynthesisEngine,
|
|
title: String? = nil,
|
|
width: Double = 300,
|
|
height: Double = 180
|
|
) -> LyrebirdWindow {
|
|
let win: LyrebirdWindow = LyrebirdWindow(
|
|
title: title ?? process.id,
|
|
width: width,
|
|
height: height
|
|
)
|
|
win.open {
|
|
LyrebirdProcessView(process: process, engine: engine)
|
|
}
|
|
return win
|
|
}
|
|
}
|
|
#endif
|