92 lines
3.1 KiB
Swift
92 lines
3.1 KiB
Swift
// LyrebirdStatusView.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
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 ..<CPURow.warningThreshold: return .green
|
||
case ..<CPURow.criticalThreshold: return .yellow
|
||
default: return .red
|
||
}
|
||
}
|
||
|
||
var body: some View {
|
||
HStack(spacing: 8) {
|
||
Text(label)
|
||
.font(.caption.monospaced())
|
||
.foregroundStyle(.secondary)
|
||
.frame(width: 60, alignment: .leading)
|
||
GeometryReader { geo in
|
||
ZStack(alignment: .leading) {
|
||
RoundedRectangle(cornerRadius: 2)
|
||
.fill(Color.secondary.opacity(0.15))
|
||
RoundedRectangle(cornerRadius: 2)
|
||
.fill(barColor)
|
||
.frame(width: geo.size.width * min(CGFloat(value), 1.0))
|
||
.animation(.linear(duration: 0.1), value: value)
|
||
}
|
||
}
|
||
.frame(height: 6)
|
||
Text(String(format: "%.1f%%", displayPercent))
|
||
.font(.caption.monospaced())
|
||
.foregroundStyle(.primary)
|
||
.frame(width: 46, alignment: .trailing)
|
||
}
|
||
}
|
||
}
|