1.0.0 release
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
// LyrebirdScopeView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
/// A real-time dual-channel oscilloscope view.
|
||||
///
|
||||
/// Polls `ScopeCapture` at ~30 Hz and renders each channel's waveform using
|
||||
/// `Canvas`. Channels are stacked vertically, separated by a hairline rule.
|
||||
///
|
||||
/// Channel 0 (left) is drawn in cyan; channel 1 (right) in orange.
|
||||
/// The zero line for each channel is drawn as a dim horizontal guide.
|
||||
///
|
||||
/// ```swift
|
||||
/// LyrebirdScopeView(capture: scopeCapture)
|
||||
/// .frame(height: 120)
|
||||
/// ```
|
||||
public struct LyrebirdScopeView: View {
|
||||
|
||||
public let capture: ScopeCapture
|
||||
|
||||
@State private var channels: [[Float]] = []
|
||||
|
||||
private static let pollIntervalNs: UInt64 = 33_333_333 // ~30 Hz
|
||||
private static let channelColors: [Color] = [.cyan, .orange, .green, .yellow]
|
||||
private static let backgroundColor: Color = Color(white: 0.08)
|
||||
private static let zeroLineOpacity: Double = 0.25
|
||||
private static let separatorOpacity: Double = 0.3
|
||||
/// Per-half-height fraction for waveform amplitude (0.44 × half-height = 0.88 total row
|
||||
/// excursion), leaving headroom so ±1 signals don't clip against channel boundaries.
|
||||
private static let waveformHeightFraction: CGFloat = 0.44
|
||||
|
||||
public init(capture: ScopeCapture) {
|
||||
self.capture = capture
|
||||
}
|
||||
|
||||
public var body: some View {
|
||||
Canvas { ctx, size in
|
||||
_drawBackground(ctx: ctx, size: size)
|
||||
guard !channels.isEmpty else { return }
|
||||
let count: Int = channels.count
|
||||
let rowH: CGFloat = size.height / CGFloat(count)
|
||||
|
||||
for (ch, samples) in channels.enumerated() {
|
||||
let midY: CGFloat = rowH * (CGFloat(ch) + 0.5)
|
||||
let halfH: CGFloat = rowH * LyrebirdScopeView.waveformHeightFraction
|
||||
|
||||
// Zero guide line
|
||||
var guide: Path = Path()
|
||||
guide.move(to: CGPoint(x: 0, y: midY))
|
||||
guide.addLine(to: CGPoint(x: size.width, y: midY))
|
||||
ctx.stroke(guide, with: .color(.white.opacity(LyrebirdScopeView.zeroLineOpacity)),
|
||||
lineWidth: 0.5)
|
||||
|
||||
// Channel separator (between channels, not after last)
|
||||
if ch < count - 1 {
|
||||
let sepY: CGFloat = rowH * CGFloat(ch + 1)
|
||||
var sep: Path = Path()
|
||||
sep.move(to: CGPoint(x: 0, y: sepY))
|
||||
sep.addLine(to: CGPoint(x: size.width, y: sepY))
|
||||
ctx.stroke(sep, with: .color(.white.opacity(LyrebirdScopeView.separatorOpacity)),
|
||||
lineWidth: 0.5)
|
||||
}
|
||||
|
||||
guard samples.count > 1 else { continue }
|
||||
let color: Color = LyrebirdScopeView.channelColors[ch % LyrebirdScopeView.channelColors.count]
|
||||
let xStep: CGFloat = size.width / CGFloat(samples.count - 1)
|
||||
var path: Path = Path()
|
||||
for (i, sample) in samples.enumerated() {
|
||||
let x: CGFloat = CGFloat(i) * xStep
|
||||
let y: CGFloat = midY - CGFloat(sample) * halfH
|
||||
if i == 0 { path.move(to: CGPoint(x: x, y: y)) }
|
||||
else { path.addLine(to: CGPoint(x: x, y: y)) }
|
||||
}
|
||||
ctx.stroke(path, with: .color(color), lineWidth: 1.0)
|
||||
}
|
||||
}
|
||||
.background(LyrebirdScopeView.backgroundColor)
|
||||
.clipShape(RoundedRectangle(cornerRadius: 4))
|
||||
.task {
|
||||
while !Task.isCancelled {
|
||||
channels = capture.snapshot()
|
||||
try? await Task.sleep(nanoseconds: LyrebirdScopeView.pollIntervalNs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func _drawBackground(
|
||||
ctx: GraphicsContext,
|
||||
size: CGSize
|
||||
) {
|
||||
ctx.fill(Path(CGRect(origin: .zero, size: size)),
|
||||
with: .color(LyrebirdScopeView.backgroundColor))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user