// 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 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, 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..= 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 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, 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..= 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