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>
260 lines
10 KiB
Swift
260 lines
10 KiB
Swift
// MultiSlider.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
#if canImport(SwiftUI)
|
||
import SwiftUI
|
||
import LyrebirdSynth
|
||
|
||
/// A row of `count` adjacent vertical sliders bound to consecutive cells of a
|
||
/// `LyrebirdBuffer`.
|
||
///
|
||
/// Designed for driving per-bin parameter buffers — for example the per-bin
|
||
/// delay times and feedbacks read by `SpectralBinDelay`, or the magnitude
|
||
/// transfer curve read by `SpectralMagnitudeMap`. Each bar represents one
|
||
/// `LyrebirdBuffer` cell starting at `startIndex`; dragging vertically over a
|
||
/// bar writes the new value via `LyrebirdBuffer.setValue(_:at:)` (lock-free on
|
||
/// the RT side, see the Lyrebird module's `BufferGuide`).
|
||
///
|
||
/// ```swift
|
||
/// let delays = LyrebirdBuffer(count: 1024)
|
||
/// delays.fill { i, n in Double(i) / Double(n) * 0.5 }
|
||
///
|
||
/// MultiSlider(
|
||
/// buffer: delays,
|
||
/// count: 64,
|
||
/// range: 0.0...0.5,
|
||
/// height: 120
|
||
/// )
|
||
/// ```
|
||
public struct MultiSlider: View {
|
||
|
||
// MARK: - Configuration
|
||
|
||
private let buffer: LyrebirdBuffer
|
||
private let count: Int
|
||
private let startIndex: Int
|
||
private let range: ClosedRange<Double>
|
||
private let height: CGFloat
|
||
|
||
// MARK: - State
|
||
|
||
@State private var values: [Double]
|
||
|
||
// MARK: - Init
|
||
|
||
/// - Parameters:
|
||
/// - buffer: Backing `LyrebirdBuffer`. Mono only. Initial values shown by
|
||
/// the sliders are read once from `buffer[startIndex + i]` at view
|
||
/// construction; thereafter the buffer is treated as one-way (this
|
||
/// view writes; external readers consume).
|
||
/// - count: Number of sliders in the row. Must be ≥ 1 and
|
||
/// `startIndex + count <= buffer.count`.
|
||
/// - startIndex: First buffer index this view writes to. Defaults to `0`.
|
||
/// - range: Value range. Drag-top of a bar maps to `range.upperBound`,
|
||
/// drag-bottom to `range.lowerBound`.
|
||
/// - height: View height in points. Default `100`.
|
||
public init(
|
||
buffer: LyrebirdBuffer,
|
||
count: Int,
|
||
startIndex: Int = 0,
|
||
range: ClosedRange<Double>,
|
||
height: CGFloat = 100
|
||
) {
|
||
precondition(buffer.channelCount == 1,
|
||
"MultiSlider: buffer must be mono, got channelCount=\(buffer.channelCount)")
|
||
precondition(count >= 1,
|
||
"MultiSlider: count must be >= 1, got \(count)")
|
||
precondition(startIndex >= 0 && startIndex + count <= buffer.count,
|
||
"MultiSlider: startIndex \(startIndex) + count \(count) "
|
||
+ "exceeds buffer.count \(buffer.count)")
|
||
precondition(range.lowerBound < range.upperBound,
|
||
"MultiSlider: range must be non-empty, got \(range)")
|
||
|
||
self.buffer = buffer
|
||
self.count = count
|
||
self.startIndex = startIndex
|
||
self.range = range
|
||
self.height = height
|
||
|
||
var initial: [Double] = [Double](repeating: range.lowerBound, count: count)
|
||
for i: Int in 0..<count {
|
||
let raw: Double = buffer[startIndex + i]
|
||
initial[i] = min(max(raw, range.lowerBound), range.upperBound)
|
||
}
|
||
_values = State(initialValue: initial)
|
||
}
|
||
|
||
// MARK: - Body
|
||
|
||
public var body: some View {
|
||
GeometryReader { geo in
|
||
let columnWidth: CGFloat = geo.size.width / CGFloat(count)
|
||
ZStack(alignment: .topLeading) {
|
||
Color.lyrebirdControlBackground
|
||
|
||
Canvas { ctx, size in
|
||
let h: CGFloat = size.height
|
||
let w: CGFloat = columnWidth
|
||
for i: Int in 0..<count {
|
||
let v: Double = values[i]
|
||
let norm: Double = (v - range.lowerBound) / (range.upperBound - range.lowerBound)
|
||
let barH: CGFloat = h * CGFloat(norm)
|
||
let x: CGFloat = CGFloat(i) * w
|
||
let rect: CGRect = CGRect(x: x + 1, y: h - barH, width: max(1, w - 2), height: barH)
|
||
ctx.fill(Path(rect), with: .color(.accentColor))
|
||
}
|
||
}
|
||
}
|
||
.contentShape(Rectangle())
|
||
.gesture(
|
||
DragGesture(minimumDistance: 0)
|
||
.onChanged { drag in
|
||
let column: Int = Int(drag.location.x / columnWidth)
|
||
guard column >= 0 && column < count else { return }
|
||
let yClamped: CGFloat = min(max(drag.location.y, 0), geo.size.height)
|
||
let norm: Double = 1 - Double(yClamped / geo.size.height)
|
||
let span: Double = range.upperBound - range.lowerBound
|
||
let value: Double = range.lowerBound + norm * span
|
||
// Drag fires many ticks per second; the SwiftUI diff
|
||
// and `setValue`'s NSLock both make repeated identical
|
||
// values cheap, so we don't bother short-circuiting on
|
||
// equality (Double-equality on continuous drag is
|
||
// almost always false).
|
||
values[column] = value
|
||
buffer.setValue(value, at: startIndex + column)
|
||
}
|
||
)
|
||
}
|
||
.frame(height: height)
|
||
.overlay(
|
||
Rectangle().stroke(Color.lyrebirdSeparator, lineWidth: 1)
|
||
)
|
||
}
|
||
}
|
||
|
||
#else
|
||
// Linux/GTK4 counterpart to `LyrebirdUI.MultiSlider`: a row of `count` vertical
|
||
// bars bound to consecutive `LyrebirdBuffer` cells, drawn on a `Canvas` with an
|
||
// `.onDrag` gesture (the same custom-drawn approach as `ParameterSlider`). The
|
||
// SwiftUI `GeometryReader`/`ZStack`/`DragGesture` are replaced: the Canvas has a
|
||
// fixed total width (`count × columnWidth`) so the drag maps pointer-x to a
|
||
// column directly, and pointer-y over the fixed `height` gives the value — a
|
||
// Canvas renderer gets its `size`, so no `GeometryReader` is needed. Per-bar
|
||
// writes go through `LyrebirdBuffer.setValue(_:at:)` exactly as on the Apple side.
|
||
//
|
||
// SwiftOpenUI + BackendGTK4 are always present in this target (see Package.swift).
|
||
import SwiftOpenUI
|
||
import BackendGTK4
|
||
import LyrebirdSynth
|
||
import Foundation
|
||
|
||
/// A row of adjacent vertical sliders bound to a `LyrebirdBuffer` for the
|
||
/// Linux/GTK4 UI. Mirrors `LyrebirdUI.MultiSlider`; same public API/init as the
|
||
/// Apple side.
|
||
public struct MultiSlider: View {
|
||
|
||
// MARK: - Configuration
|
||
|
||
private let buffer: LyrebirdBuffer
|
||
private let count: Int
|
||
private let startIndex: Int
|
||
private let range: ClosedRange<Double>
|
||
private let height: CGFloat
|
||
|
||
// MARK: - State
|
||
|
||
@State private var values: [Double]
|
||
|
||
// Fixed per-column width (points). The Apple side fills the available width
|
||
// via GeometryReader; on Linux the Canvas needs a concrete width, so each bar
|
||
// gets a fixed column and the row is `count × columnWidth` wide.
|
||
private let columnWidth: Double = 14
|
||
|
||
// MARK: - Init
|
||
|
||
/// - Parameters:
|
||
/// - buffer: Backing `LyrebirdBuffer`. Mono only. Initial values are read
|
||
/// once from `buffer[startIndex + i]` at construction.
|
||
/// - count: Number of sliders. Must be ≥ 1 and `startIndex + count <= buffer.count`.
|
||
/// - startIndex: First buffer index this view writes to. Defaults to `0`.
|
||
/// - range: Value range. Bar-top maps to `range.upperBound`, bar-bottom to
|
||
/// `range.lowerBound`.
|
||
/// - height: View height in points. Default `100`.
|
||
public init(
|
||
buffer: LyrebirdBuffer,
|
||
count: Int,
|
||
startIndex: Int = 0,
|
||
range: ClosedRange<Double>,
|
||
height: CGFloat = 100
|
||
) {
|
||
precondition(buffer.channelCount == 1,
|
||
"MultiSlider: buffer must be mono, got channelCount=\(buffer.channelCount)")
|
||
precondition(count >= 1,
|
||
"MultiSlider: count must be >= 1, got \(count)")
|
||
precondition(startIndex >= 0 && startIndex + count <= buffer.count,
|
||
"MultiSlider: startIndex \(startIndex) + count \(count) "
|
||
+ "exceeds buffer.count \(buffer.count)")
|
||
precondition(range.lowerBound < range.upperBound,
|
||
"MultiSlider: range must be non-empty, got \(range)")
|
||
|
||
self.buffer = buffer
|
||
self.count = count
|
||
self.startIndex = startIndex
|
||
self.range = range
|
||
self.height = height
|
||
|
||
var initial: [Double] = [Double](repeating: range.lowerBound, count: count)
|
||
for i: Int in 0..<count {
|
||
let raw: Double = buffer[startIndex + i]
|
||
initial[i] = min(max(raw, range.lowerBound), range.upperBound)
|
||
}
|
||
_values = State(wrappedValue: initial)
|
||
}
|
||
|
||
// MARK: - Body
|
||
|
||
public var body: some View {
|
||
let totalWidth: Double = Double(count) * columnWidth
|
||
return Canvas(renderer: { (ctx: DrawingContext, size: CGSize) in
|
||
let h: Double = Double(size.height)
|
||
let w: Double = columnWidth
|
||
|
||
// Panel background.
|
||
ctx.setColor(r: 0.5, g: 0.5, b: 0.5, a: 0.08)
|
||
ctx.rectangle(x: 0, y: 0, width: Double(size.width), height: h)
|
||
ctx.fill()
|
||
|
||
// Bars.
|
||
let span: Double = range.upperBound - range.lowerBound
|
||
ctx.setColor(r: 0.30, g: 0.80, b: 1.0)
|
||
for i: Int in 0..<count {
|
||
let v: Double = values[i]
|
||
let norm: Double = (v - range.lowerBound) / span
|
||
let barH: Double = h * norm
|
||
let x: Double = Double(i) * w
|
||
ctx.rectangle(x: x + 1, y: h - barH, width: max(1, w - 2), height: barH)
|
||
ctx.fill()
|
||
}
|
||
})
|
||
.frame(width: totalWidth, height: Double(height))
|
||
.onDrag(
|
||
minimumDistance: 0,
|
||
onChanged: { (drag: DragGestureValue) in
|
||
let column: Int = Int(drag.location.x / columnWidth)
|
||
guard column >= 0 && column < count else { return }
|
||
let hgt: Double = Double(height)
|
||
let yClamped: Double = min(max(drag.location.y, 0), hgt)
|
||
let norm: Double = 1 - (yClamped / hgt)
|
||
let span: Double = range.upperBound - range.lowerBound
|
||
let value: Double = range.lowerBound + norm * span
|
||
values[column] = value
|
||
buffer.setValue(value, at: startIndex + column)
|
||
}
|
||
)
|
||
}
|
||
}
|
||
#endif
|