1.0.0 release
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
// ParameterSlider.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
/// A labelled continuous slider bound to a `UIParamNode`.
|
||||
///
|
||||
/// Changes flow through the node's atomic update path — RT-safe with no special handling.
|
||||
///
|
||||
/// ```swift
|
||||
/// ParameterSlider("Amplitude", node: synth.$amplitude, range: 0.0...1.0)
|
||||
/// ParameterSlider("Cutoff", node: synth.$cutoff, range: 20.0...20000.0, taper: .exponential)
|
||||
/// ```
|
||||
public struct ParameterSlider: View {
|
||||
|
||||
// MARK: - Taper
|
||||
|
||||
/// Value mapping applied between the slider's linear position and the parameter value.
|
||||
public enum Taper {
|
||||
/// No mapping — slider position maps directly to parameter value.
|
||||
case linear
|
||||
/// Exponential mapping — equal visual steps produce equal ratios (good for frequency, time).
|
||||
case exponential
|
||||
}
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let label: String
|
||||
private let node: UIParamNode
|
||||
private let range: ClosedRange<Double>
|
||||
private let taper: Taper
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a slider bound to a `UIParamNode`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - label: Text displayed beside the slider.
|
||||
/// - node: The `UIParamNode` to bind to (pass `synth.$paramName`).
|
||||
/// - range: The minimum and maximum parameter values.
|
||||
/// - taper: Value mapping. Default `.linear`.
|
||||
public init(
|
||||
_ label: String,
|
||||
node: UIParamNode,
|
||||
range: ClosedRange<Double>,
|
||||
taper: Taper = .linear
|
||||
) {
|
||||
|
||||
self.label = label
|
||||
self.node = node
|
||||
self.range = range
|
||||
self.taper = taper
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
HStack {
|
||||
Text(label)
|
||||
.frame(minWidth: 80, alignment: .leading)
|
||||
Slider(value: binding, in: linearRange) { _ in }
|
||||
Text(formattedValue)
|
||||
.frame(minWidth: 60, alignment: .trailing)
|
||||
.monospacedDigit()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private helpers
|
||||
|
||||
/// Linear range always 0...1; taper mapping happens inside the Binding.
|
||||
private var linearRange: ClosedRange<Double> { 0.0...1.0 }
|
||||
|
||||
private var binding: Binding<Double> {
|
||||
Binding(
|
||||
get: { toLinear(node.value) },
|
||||
set: { node.update(fromLinear($0)) }
|
||||
)
|
||||
}
|
||||
|
||||
private func toLinear(_ value: Double) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
switch taper {
|
||||
case .linear:
|
||||
return (value - lo) / (hi - lo)
|
||||
case .exponential:
|
||||
guard lo > 0, hi > lo else { return (value - lo) / (hi - lo) }
|
||||
return log(value / lo) / log(hi / lo)
|
||||
}
|
||||
}
|
||||
|
||||
private func fromLinear(_ position: Double) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
switch taper {
|
||||
case .linear:
|
||||
return lo + position * (hi - lo)
|
||||
case .exponential:
|
||||
guard lo > 0, hi > lo else { return lo + position * (hi - lo) }
|
||||
return lo * pow(hi / lo, position)
|
||||
}
|
||||
}
|
||||
|
||||
private var formattedValue: String {
|
||||
let v: Double = node.value
|
||||
if abs(v) >= 1000 {
|
||||
return String(format: "%.0f", v)
|
||||
} else if abs(v) >= 10 {
|
||||
return String(format: "%.1f", v)
|
||||
} else {
|
||||
return String(format: "%.3f", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user