// LyrebirdProcessView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT 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