136 lines
5.3 KiB
Swift
136 lines
5.3 KiB
Swift
// MultiSlider.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
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)
|
|
)
|
|
}
|
|
}
|