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>
392 lines
16 KiB
Swift
392 lines
16 KiB
Swift
// KnobDemoView.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import LyrebirdSynth // UIParamNode
|
|
import LyrebirdUI // View DSL + ParameterKnob, via @_exported re-exports
|
|
import Foundation // CGSize (narrowtest Canvas renderer)
|
|
import Observation // @Observable (mouse readout model)
|
|
|
|
/// Two knobs bound to a synthesizer's `@UIParam` slots — frequency and
|
|
/// amplitude. Drag up to increase; the sine tone tracks in real time.
|
|
///
|
|
/// The view holds the projected `UIParamNode`s directly rather than the
|
|
/// `SynthesizerBase`. `SynthesizerBase` is `@MainActor`-isolated, so reading
|
|
/// `synth.$frequency` is only legal from a main-actor context — and SwiftOpenUI's
|
|
/// `View.body` is *nonisolated* (unlike SwiftUI's `@MainActor` body). So the
|
|
/// caller extracts the nodes on the main actor (`main.swift` top-level code is
|
|
/// `@MainActor`) and hands them in; `UIParamNode` itself is not actor-isolated,
|
|
/// so reading `node.value` from the nonisolated body is fine.
|
|
struct KnobDemoView: View {
|
|
let frequencyNode: UIParamNode
|
|
let amplitudeNode: UIParamNode
|
|
let scope: ScopeCapture
|
|
let envelope: Envelope
|
|
let engine: SynthesisEngine
|
|
|
|
/// Frequency knob range, in Hz — roughly A2…A5.
|
|
private let freqMin: Double = 110.0
|
|
private let freqMax: Double = 880.0
|
|
|
|
/// Values the reset button restores.
|
|
private let resetFreq: Double = 220.0
|
|
private let resetAmp: Double = 0.2
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Text("Lyrebird — Linux UI demo")
|
|
// Live CPU-load meters (engine average / peak).
|
|
LyrebirdStatusView(engine: engine)
|
|
// Live oscilloscope of the stereo output.
|
|
LyrebirdScopeView(capture: scope)
|
|
// Static plot of an example envelope (cyan pre-loop, green loop,
|
|
// orange release; dashed markers at the loop/release boundaries).
|
|
LyrebirdEnvelopeView(envelope)
|
|
// XY pad drives both nodes at once (X = freq, Y = amp); it shares the
|
|
// same nodes as the controls below, so all of them stay in sync.
|
|
ParameterXY(
|
|
xLabel: "freq", xNode: frequencyNode, xRange: freqMin...freqMax,
|
|
yLabel: "amp", yNode: amplitudeNode, yRange: 0.0...1.0
|
|
)
|
|
// Knob and slider bind the SAME node, so moving one updates the
|
|
// other — the shared `@UIParam` model observed from several controls.
|
|
ParameterKnob("freq", node: frequencyNode, range: freqMin...freqMax)
|
|
ParameterSlider("freq", node: frequencyNode, range: freqMin...freqMax, taper: .exponential)
|
|
ParameterLabel("freq (Hz)", node: frequencyNode, decimalPlaces: 1)
|
|
ParameterKnob("amp", node: amplitudeNode, range: 0.0...1.0)
|
|
ParameterSlider("amp", node: amplitudeNode, range: 0.0...1.0)
|
|
// Toggle gates the amp node on/off (0 ↔ 1); the amp knob/slider
|
|
// follow it, since all three observe the same node. Highlights when on.
|
|
ParameterButton("gate", node: amplitudeNode, style: .toggle)
|
|
// Momentary: restores defaults. Writes through the nodes (not the
|
|
// @MainActor synth), so it is callable from the nonisolated handler.
|
|
ParameterButton("reset") {
|
|
frequencyNode.update(resetFreq)
|
|
amplitudeNode.update(resetAmp)
|
|
}
|
|
}
|
|
.padding()
|
|
}
|
|
}
|
|
|
|
// MARK: - Diagnostic variants (selected by the LYREBIRD_DEMO env var)
|
|
|
|
/// Diagnostic: a single knob, nothing else. Isolates the control from the
|
|
/// multi-view / animation context.
|
|
struct DemoKnobOnlyView: View {
|
|
let frequencyNode: UIParamNode
|
|
|
|
private let freqMin: Double = 110.0
|
|
private let freqMax: Double = 880.0
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Text("knob only")
|
|
ParameterKnob("freq", node: frequencyNode, range: freqMin...freqMax)
|
|
}
|
|
.padding()
|
|
}
|
|
}
|
|
|
|
/// Diagnostic: all parameter controls, but NO animated views (scope / CPU) and
|
|
/// no envelope. Isolates the interactive controls from the timer-driven redraws.
|
|
struct DemoControlsView: View {
|
|
let frequencyNode: UIParamNode
|
|
let amplitudeNode: UIParamNode
|
|
|
|
private let freqMin: Double = 110.0
|
|
private let freqMax: Double = 880.0
|
|
private let resetFreq: Double = 220.0
|
|
private let resetAmp: Double = 0.2
|
|
|
|
var body: some View {
|
|
VStack {
|
|
Text("controls (no animated views)")
|
|
ParameterXY(
|
|
xLabel: "freq", xNode: frequencyNode, xRange: freqMin...freqMax,
|
|
yLabel: "amp", yNode: amplitudeNode, yRange: 0.0...1.0
|
|
)
|
|
ParameterKnob("freq", node: frequencyNode, range: freqMin...freqMax)
|
|
ParameterSlider("freq", node: frequencyNode, range: freqMin...freqMax, taper: .exponential)
|
|
ParameterLabel("freq (Hz)", node: frequencyNode, decimalPlaces: 1)
|
|
ParameterKnob("amp", node: amplitudeNode, range: 0.0...1.0)
|
|
ParameterSlider("amp", node: amplitudeNode, range: 0.0...1.0)
|
|
ParameterButton("gate", node: amplitudeNode, style: .toggle)
|
|
ParameterButton("reset") {
|
|
frequencyNode.update(resetFreq)
|
|
amplitudeNode.update(resetAmp)
|
|
}
|
|
}
|
|
.padding()
|
|
}
|
|
}
|
|
|
|
/// Showcase of the ported diagnostics / monitor views (`LYREBIRD_DEMO=monitors`):
|
|
/// the parameter knobs, a `LyrebirdScatterView`, a per-bin `MultiSlider`, and
|
|
/// `LyrebirdEngineStatusView` — the last of which itself embeds the node-tree and
|
|
/// message sections, so it exercises `LyrebirdNodeTreeView` +
|
|
/// `LyrebirdMessageBanner`/`LyrebirdMessageView` too.
|
|
///
|
|
/// No outer `ScrollView`: a GTK `ScrolledWindow` pans on pointer-drag, which
|
|
/// fights the Canvas controls' own drag gestures (dragging a knob or the
|
|
/// MultiSlider would scroll the page). Instead the panels are split into two
|
|
/// side-by-side columns so the total height fits without scrolling — the tall
|
|
/// `LyrebirdEngineStatusView` gets its own column. Widen (or the panels stack
|
|
/// off-screen); the knobs are placed directly (not the scrolling
|
|
/// `LyrebirdSynthView` grid) to keep them drag-friendly.
|
|
struct DemoMonitorsView: View {
|
|
let entries: [LyrebirdSynthView.Entry]
|
|
let scatter: [Double]
|
|
let multiBuffer: LyrebirdBuffer
|
|
let multiCount: Int
|
|
let engine: SynthesisEngine
|
|
let onEngineStarted: (() -> Void)?
|
|
|
|
var body: some View {
|
|
HStack(alignment: .top, spacing: 20) {
|
|
// Left column: knobs + read-only visualisers + the per-bin slider.
|
|
VStack(alignment: .leading) {
|
|
Text("Lyrebird — Linux monitors demo")
|
|
|
|
HStack {
|
|
ForEach(entries) { entry in
|
|
ParameterKnob(entry.displayName, node: entry.node, range: entry.range)
|
|
}
|
|
}
|
|
|
|
Text("Scatter (LyrebirdScatterView)")
|
|
LyrebirdScatterView(scatter)
|
|
|
|
Text("Per-bin MultiSlider — drag to edit")
|
|
MultiSlider(buffer: multiBuffer, count: multiCount, range: 0.0...1.0, height: 100)
|
|
}
|
|
|
|
// Right column: the tall engine-status panel (embeds node tree +
|
|
// messages + config + output).
|
|
LyrebirdEngineStatusView(
|
|
engine: engine,
|
|
onEngineStarted: onEngineStarted
|
|
)
|
|
}
|
|
.padding()
|
|
}
|
|
}
|
|
|
|
/// Narrow-path isolation view (`LYREBIRD_DEMO=narrowtest`). A Canvas slider plus a
|
|
/// `Text` readout and an editable `TextField`, each wrapped in the modifiers that
|
|
/// used to poison the narrow-mutation path (`.onChange` / `.focused` /
|
|
/// `.textFieldStyle` / `.onSubmit` / `.monospacedDigit`). Crucially it contains NO
|
|
/// opaque Class-2 widgets (no Picker/Stepper/Toggle), so it isolates the Phase-1a
|
|
/// transparent-describe fix: if dragging the slider updates the label AND the
|
|
/// field LIVE (not only at mouse-up), the transparent-describe fix works and the opaque-leaf fix is worth doing.
|
|
struct DemoNarrowTestView: View {
|
|
@State private var db: Double = -6.0
|
|
@State private var dbText: String = "-6.0"
|
|
@FocusState private var focused: Bool
|
|
|
|
private let lo: Double = -12
|
|
private let hi: Double = 12
|
|
private let trackWidth: Double = 220
|
|
private let trackHeight: Double = 24
|
|
private let thumbRadius: Double = 7
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 12) {
|
|
Text("Narrow-path test")
|
|
Text("Drag the slider: the label and the text field should track LIVE.")
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
|
|
HStack(spacing: 8) {
|
|
Text("Gain")
|
|
.frame(width: 44, alignment: .leading)
|
|
slider
|
|
// Live label wrapped in ex-poison modifiers.
|
|
Text(String(format: "%+.1f dB", db))
|
|
.font(.caption)
|
|
.monospacedDigit()
|
|
.frame(width: 70, alignment: .trailing)
|
|
.onChange(of: db) { _, _ in }
|
|
// Editable field wrapped in ex-poison modifiers.
|
|
TextField("dB", text: $dbText)
|
|
.font(.caption)
|
|
.monospacedDigit()
|
|
.focused($focused)
|
|
.frame(width: 70)
|
|
.textFieldStyle(.roundedBorder)
|
|
.onSubmit { }
|
|
.onChange(of: dbText) { _, _ in }
|
|
}
|
|
}
|
|
.padding()
|
|
}
|
|
|
|
private var slider: some View {
|
|
Canvas(renderer: { (ctx: DrawingContext, size: CGSize) in
|
|
let w: Double = Double(size.width)
|
|
let h: Double = Double(size.height)
|
|
let cy: Double = h / 2
|
|
let x0: Double = thumbRadius
|
|
let x1: Double = w - thumbRadius
|
|
let usable: Double = max(x1 - x0, 1)
|
|
let span: Double = hi - lo
|
|
let pos: Double = span > 0 ? (db - lo) / span : 0
|
|
let thumbX: Double = x0 + min(1, max(0, pos)) * usable
|
|
ctx.setColor(r: 0.20, g: 0.20, b: 0.20)
|
|
ctx.setLineWidth(4)
|
|
ctx.moveTo(x: x0, y: cy)
|
|
ctx.lineTo(x: x1, y: cy)
|
|
ctx.stroke()
|
|
ctx.setColor(r: 0.30, g: 0.80, b: 1.0)
|
|
ctx.setLineWidth(4)
|
|
ctx.moveTo(x: x0, y: cy)
|
|
ctx.lineTo(x: thumbX, y: cy)
|
|
ctx.stroke()
|
|
ctx.setColor(r: 0.90, g: 0.90, b: 0.95)
|
|
ctx.arc(centerX: thumbX, centerY: cy, radius: thumbRadius,
|
|
startAngle: 0, endAngle: 2 * Double.pi)
|
|
ctx.fill()
|
|
})
|
|
.frame(width: trackWidth, height: trackHeight)
|
|
.onDrag(minimumDistance: 0, onChanged: { (drag: DragGestureValue) in
|
|
let usable: Double = max(trackWidth - 2 * thumbRadius, 1)
|
|
let pos: Double = min(1, max(0, (drag.location.x - thumbRadius) / usable))
|
|
let v: Double = lo + pos * (hi - lo)
|
|
db = v
|
|
dbText = String(format: "%+.1f", v)
|
|
})
|
|
}
|
|
}
|
|
|
|
// MARK: - Mouse input demo (LYREBIRD_DEMO=mouse)
|
|
|
|
/// Polls the installed `MouseInput` bridge (fed by `MouseTracker`) on a
|
|
/// main-thread `Timer`, publishing normalized pointer state for the readout —
|
|
/// same idiom as the CPU / scope models.
|
|
///
|
|
/// `@unchecked Sendable`: main-thread-confined (the poll `Timer` fires on
|
|
/// `RunLoop.main`; all access is on that thread), but `Timer`'s block is
|
|
/// `@Sendable`, so the capture requires the conformance — same rationale as
|
|
/// `LyrebirdScopeModel` / `LyrebirdStatusModel`.
|
|
@Observable
|
|
final class MouseReadoutModel: @unchecked Sendable {
|
|
|
|
var x: Double = 0.5
|
|
var y: Double = 0.5
|
|
var button: Double = 0.0
|
|
|
|
@ObservationIgnored private var timer: Timer?
|
|
private let pollInterval: TimeInterval = 0.05 // 20 Hz — snappy readout
|
|
|
|
deinit {
|
|
timer?.invalidate()
|
|
}
|
|
|
|
func start() {
|
|
guard timer == nil else { return }
|
|
refresh()
|
|
let t: Timer = Timer.scheduledTimer(withTimeInterval: pollInterval, repeats: true) { [weak self] _ in
|
|
self?.refresh()
|
|
}
|
|
timer = t
|
|
}
|
|
|
|
func stop() {
|
|
timer?.invalidate()
|
|
timer = nil
|
|
}
|
|
|
|
private func refresh() {
|
|
x = MouseInput.currentX
|
|
y = MouseInput.currentY
|
|
button = MouseInput.currentButton
|
|
}
|
|
}
|
|
|
|
/// Live readout of the pointer source (`LYREBIRD_DEMO=mouse`). Move the pointer
|
|
/// over the window: X→frequency, Y→amplitude drive the `MouseDemoSynth` sine, and
|
|
/// the values here should track. Validates the Linux `MouseTracker` +
|
|
/// `GtkEventControllerMotion` pointer bridge.
|
|
struct DemoMouseView: View {
|
|
@State private var model: MouseReadoutModel = MouseReadoutModel()
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 12) {
|
|
Text("Mouse input demo")
|
|
Text("Move the pointer over this window (X → frequency, Y → amplitude); click to test the button.")
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
|
|
Text(String(format: "Mouse.X: %.3f", model.x))
|
|
.monospacedDigit()
|
|
Text(String(format: "Mouse.Y: %.3f", model.y))
|
|
.monospacedDigit()
|
|
Text(String(format: "Mouse.Click: %.0f", model.button))
|
|
.monospacedDigit()
|
|
}
|
|
.padding()
|
|
.onAppear { model.start() }
|
|
.onDisappear { model.stop() }
|
|
}
|
|
}
|
|
|
|
// MARK: - Events / process views demo (LYREBIRD_DEMO=events)
|
|
|
|
/// On-display exercise of the ported process/event-list views
|
|
/// (`LYREBIRD_DEMO=events`). Embeds `LyrebirdEventListPerformanceView` — which
|
|
/// itself hosts `LyrebirdEventListView` (transport) plus one `LyrebirdProcessView`
|
|
/// strip per running process — and offers buttons to open each view in its own
|
|
/// detached `LyrebirdWindow` via `presentInWindow`, so the standalone-window path
|
|
/// is exercised for these views too (not just EngineStatus).
|
|
///
|
|
/// Press **Next** in the transport to fire the queued events; each starts a
|
|
/// sustaining `EventVoiceSynth`, so a running-process strip appears (and a tone
|
|
/// sounds when a live audio backend is present).
|
|
struct DemoEventsView: View {
|
|
let eventList: LyrebirdEventList
|
|
let engine: SynthesisEngine
|
|
|
|
@State private var perfWindow: LyrebirdWindow?
|
|
@State private var listWindow: LyrebirdWindow?
|
|
@State private var runningWindow: LyrebirdWindow?
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 12) {
|
|
Text("Lyrebird — Linux events / processes demo")
|
|
Text("Press Next to fire events; each starts a process strip below.")
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
|
|
LyrebirdEventListPerformanceView(eventList: eventList, engine: engine)
|
|
|
|
HStack(spacing: 8) {
|
|
Button {
|
|
perfWindow?.close()
|
|
perfWindow = LyrebirdEventListPerformanceView.presentInWindow(
|
|
eventList: eventList, engine: engine
|
|
)
|
|
} label: {
|
|
Text("Open Perf Window").font(.caption)
|
|
}
|
|
Button {
|
|
listWindow?.close()
|
|
listWindow = LyrebirdEventListView.presentInWindow(eventList: eventList)
|
|
} label: {
|
|
Text("Open EventList Window").font(.caption)
|
|
}
|
|
Button {
|
|
runningWindow?.close()
|
|
runningWindow = LyrebirdRunningProcessesView.presentInWindow(
|
|
eventList: eventList, engine: engine
|
|
)
|
|
} label: {
|
|
Text("Open Running Window").font(.caption)
|
|
}
|
|
}
|
|
}
|
|
.padding()
|
|
}
|
|
}
|