Files
ShoeGaze/Sources/LyrebirdShoeGaze/Presets/Pedal+Preset.swift
T
2026-06-21 12:25:18 -07:00

45 lines
1.7 KiB
Swift

// Pedal+Preset.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
extension Pedal {
/// Captures the current parameter values of this pedal as a `PedalState`.
///
/// The returned snapshot includes:
/// - `type`: the pedal's `displayName` (used as a factory key on load).
/// - `knobValues`: one `Double` per entry in `knobs`, in order
/// (effect-specific knobs first, then Mix, then Level).
/// - `bypass`: the current bypass parameter value.
///
/// - Returns: A `PedalState` reflecting the pedal's current parameter values.
public func captureState() -> PedalState {
let values: [Double] = knobs.map { $0.node.value }
return PedalState(
type: displayName,
knobValues: values,
bypass: bypass
)
}
/// Applies a previously captured `PedalState` to this pedal's parameters.
///
/// For each (knob, value) pair in `zip(knobs, state.knobValues)`, the knob's
/// `UIParamNode` is updated via `node.update(_:)`. If `state.knobValues` has
/// fewer entries than `knobs`, the trailing knobs retain their current values.
/// Extra values beyond `knobs.count` are ignored. The `bypass` parameter is
/// always applied regardless of knob-count mismatches.
///
/// - Parameter state: The state to restore. The `state.type` is not validated
/// against `displayName` — the caller is responsible for matching types before
/// calling this method.
public func applyState(_ state: PedalState) {
for (knob, value) in zip(knobs, state.knobValues) {
knob.node.update(value)
}
bypass = state.bypass
}
}