// LyrebirdStatusView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if canImport(SwiftUI) import SwiftUI import LyrebirdSynth /// A compact view that displays real-time CPU usage statistics for a `SynthesisEngine`. /// /// Embed in any SwiftUI hierarchy to monitor average and peak CPU load. The view polls the /// engine at 10 Hz and updates automatically while visible. /// /// ```swift /// LyrebirdStatusView(engine: engine) /// ``` /// /// Values are expressed as a fraction of one block duration: `0.0` = idle, `1.0` = full block /// consumed, `> 1.0` = over budget (xruns likely). The bars are color-coded: green below 50 %, /// yellow 50–80 %, red above 80 %. Peak resets automatically every 5 s (engine default). public struct LyrebirdStatusView: View { public let engine: SynthesisEngine @State private var averageCPU: Double = 0.0 @State private var peakCPU: Double = 0.0 private static let pollIntervalNs: UInt64 = 100_000_000 // 100 ms → 10 Hz public init(engine: SynthesisEngine) { self.engine = engine } public var body: some View { VStack(alignment: .leading, spacing: 6) { CPURow(label: "CPU Avg", value: averageCPU) CPURow(label: "CPU Peak", value: peakCPU) } .task { while !Task.isCancelled { averageCPU = engine.averageCPU peakCPU = engine.peakCPU try? await Task.sleep(nanoseconds: LyrebirdStatusView.pollIntervalNs) } } } } private struct CPURow: View { let label: String let value: Double // 0.0 = idle, 1.0 = full block, >1.0 = overload private static let maxDisplayPercent: Double = 999.9 private static let warningThreshold: Double = 0.5 private static let criticalThreshold: Double = 0.8 private var displayPercent: Double { min(value * 100.0, CPURow.maxDisplayPercent) } private var barColor: Color { switch value { case .. 1.0` = over budget (xruns likely). public struct LyrebirdStatusView: View { @State private var model: LyrebirdStatusModel /// Creates a CPU-load readout for the given engine. /// /// - Parameter engine: The `SynthesisEngine` to monitor. public init(engine: SynthesisEngine) { _model = State(wrappedValue: LyrebirdStatusModel(engine: engine)) } public var body: some View { VStack(alignment: .leading) { LinuxCPURow(label: "CPU Avg", value: model.averageCPU) LinuxCPURow(label: "CPU Peak", value: model.peakCPU) } .onAppear { model.start() } .onDisappear { model.stop() } } } /// One labelled CPU bar. Private — an implementation detail of ``LyrebirdStatusView``. private struct LinuxCPURow: View { let label: String let value: Double // 0.0 = idle, 1.0 = full block, > 1.0 = overload private let labelWidth: Double = 60 private let percentWidth: Double = 52 private let barWidth: Double = 120 private let barHeight: Double = 8 private let maxDisplayPercent: Double = 999.9 private let warningThreshold: Double = 0.5 private let criticalThreshold: Double = 0.8 var body: some View { HStack { Text(label) .monospacedDigit() .frame(minWidth: labelWidth, alignment: .leading) Canvas(renderer: { (ctx: DrawingContext, size: CGSize) in let w: Double = Double(size.width) let h: Double = Double(size.height) // Track. ctx.setColor(r: 0.5, g: 0.5, b: 0.5, a: 0.15) ctx.rectangle(x: 0, y: 0, width: w, height: h) ctx.fill() // Filled portion, clamped to the track width. let frac: Double = min(max(value, 0.0), 1.0) let rgb: (r: Double, g: Double, b: Double) = barColor ctx.setColor(r: rgb.r, g: rgb.g, b: rgb.b) ctx.rectangle(x: 0, y: 0, width: w * frac, height: h) ctx.fill() }) .frame(width: barWidth, height: barHeight) Text(percentText) .monospacedDigit() .frame(minWidth: percentWidth, alignment: .trailing) } } private var barColor: (r: Double, g: Double, b: Double) { if value < warningThreshold { return (0.0, 0.8, 0.0) } // green if value < criticalThreshold { return (1.0, 0.8, 0.0) } // yellow return (1.0, 0.2, 0.2) // red } private var percentText: String { let displayPercent: Double = min(value * 100.0, maxDisplayPercent) return String(format: "%.1f%%", displayPercent) } } #endif