// 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 } let switchValues: [Double] = effectSwitches.map { $0.node.value } let sliderValues: [Double] = effectSliders.map { $0.node.value } let selectorValues: [Double] = effectSelectors.map { $0.node.value } return PedalState( type: displayName, knobValues: values, switchValues: switchValues, sliderValues: sliderValues, selectorValues: selectorValues, 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(_:)`; switch positions are restored the /// same way from `zip(effectSwitches, state.switchValues)`. If either array has fewer /// entries than its controls, the trailing controls retain their current values; extra /// values are ignored. The `bypass` parameter is always applied regardless of any /// count mismatch. Presets saved before switches existed carry an empty /// `switchValues`, leaving switch positions at their defaults. /// /// - 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) } for (pedalSwitch, value) in zip(effectSwitches, state.switchValues) { pedalSwitch.node.update(value) } for (slider, value) in zip(effectSliders, state.sliderValues) { slider.node.update(value) } for (selector, value) in zip(effectSelectors, state.selectorValues) { selector.node.update(value) } bypass = state.bypass } }