Files
josh 3e3f86b737 feat(karplus): collapse tuning UI to a Free/Pitched switch + one Root knob
Replace the four-control tuning surface (continuous Tune Hz knob + Tuning
selector + separate Note and Octave selectors) with a single Free/Pitched
switch and one chromatic Root selector that steps every semitone from E1
(MIDI 28) to E5 (MIDI 76), captioned by note name ("E1", "A3", "E5"). The
Root selector is shown only in Pitched mode; in Free mode the continuous
Tune Hz knob drives the root as before.

- Drop the `note`/`octave` @UIParams and `noteFrequency(pitchClass:octave:)`;
  add a `pitchedNote` @UIParam (MIDI note number, A3=57=220 Hz default) and
  `frequency(forMidiNote:)` / `caption(forMidiNote:)` helpers.
- Knob positions are unchanged (Tune always present), so positional presets
  stay aligned; only the trailing selector set varies by mode, with Voicing
  pinned at index 0.
- Document the switch-before-selector pass order in Pedal.applyState as
  contractual: a mode-dependent selector set (like this one) relies on it.

798 tests pass; lyrebird_external_review clean (no blocking findings).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-24 17:29:15 -08:00

131 lines
6.6 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 }
let switchValues: [Double] = effectSwitches.map { $0.node.value }
let sliderValues: [Double] = effectSliders.map { $0.node.value }
let selectorValues: [Double] = effectSelectors.map { $0.node.value }
let advancedValues: [Double] = advancedKnobs.map { $0.node.value }
return PedalState(
id: id,
type: displayName,
knobValues: values,
switchValues: switchValues,
sliderValues: sliderValues,
selectorValues: selectorValues,
advancedValues: advancedValues,
bypass: bypass,
tempoSync: tempoSync.isEmpty ? nil : tempoSync,
resourcePath: resourcePath
)
}
/// Modern High-Gain's display name — the one shipped amp whose `Sag` control started life as a
/// face knob and later moved to the advanced pop-over. Used to detect its legacy `PedalState`
/// layout in ``legacyMigrated(_:)``.
private static let modernHighGainType: String = "Modern High-Gain"
/// A legacy Modern High-Gain `PedalState` has 11 `knobValues`: `Gain, Bass, Mid, Treble,
/// Presence, Depth, Master, Drive, Sag, Mix, Level`.
private static let modernHighGainLegacyKnobCount: Int = 11
/// Index of `Sag` in that legacy `knobValues` array (the 9th knob).
private static let modernHighGainLegacySagIndex: Int = 8
/// Normalizes a `PedalState` saved under a superseded on-disk layout to the current shape, so old
/// rigs load without corruption.
///
/// Currently one case: **Modern High-Gain** shipped `Sag` as its 9th face knob (`knobValues`
/// index 8, ahead of the trailing `Mix`/`Level`); `Sag` has since moved to the advanced pop-over,
/// so it is no longer in `knobValues`. Because `knobValues` is a flat positional array, loading a
/// legacy preset against the current 10-knob shape would misassign the old `Sag` onto `Mix` and
/// the old `Mix` onto `Level`. Detected by the legacy shape (11 `knobValues`, empty
/// `advancedValues`) and rewritten: `Sag` is lifted out of `knobValues` into `advancedValues`,
/// leaving `Gain…Drive, Mix, Level` in order. `Bias` (the second advanced knob) had no legacy
/// value, so it stays at its default.
///
/// - Parameter state: The decoded state, possibly in a legacy layout.
/// - Returns: The state in the current layout, or `state` unchanged when no migration applies.
private static func legacyMigrated(_ state: PedalState) -> PedalState {
guard state.type == modernHighGainType,
state.advancedValues.isEmpty,
state.knobValues.count == modernHighGainLegacyKnobCount else {
return state
}
let sag: Double = state.knobValues[modernHighGainLegacySagIndex]
var knobValues: [Double] = state.knobValues
knobValues.remove(at: modernHighGainLegacySagIndex)
return PedalState(
id: state.id,
type: state.type,
knobValues: knobValues,
switchValues: state.switchValues,
sliderValues: state.sliderValues,
selectorValues: state.selectorValues,
advancedValues: [sag],
bypass: state.bypass,
tempoSync: state.tempoSync,
resourcePath: state.resourcePath
)
}
/// Applies a previously captured `PedalState` to this pedal's parameters.
///
/// The incoming state is first normalized by ``legacyMigrated(_:)`` for older on-disk layouts.
/// For each (knob, value) pair in `zip(knobs, state.knobValues)`, the knob's `UIParamNode` is
/// updated via `node.update(_:)`; switch, slider, selector, and advanced-knob positions are
/// restored the same way. 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 a given control existed
/// carry an empty array for it, leaving those controls at their defaults.
///
/// **Pass order is contractual: switches are restored before selectors.** A pedal whose selector
/// *set* depends on a switch value (e.g. ``KarplusResonatorPedal``, whose Root note selector
/// appears only in the Pitched tuning mode) relies on `tuning` being written in the switch pass
/// before `effectSelectors` is re-read in the selector pass, so the mode-dependent selector exists
/// to receive its value. Do not reorder these passes without revisiting such pedals.
///
/// - Parameter rawState: 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(_ rawState: PedalState) {
let state: PedalState = Pedal.legacyMigrated(rawState)
// Restore the saved identity so the pedal keeps its id across rebuilds; MIDI bindings
// address pedals by this id.
id = state.id
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)
}
for (advancedKnob, value) in zip(advancedKnobs, state.advancedValues) {
advancedKnob.node.update(value)
}
bypass = state.bypass
// Restore tempo-sync state. The synced values themselves are recomputed from the rig's
// tempo by `ShoeGaze` after the rebuild, so no clock is required here.
tempoSync = state.tempoSync ?? [:]
tempoSyncRevision.bump()
resourcePath = state.resourcePath
}
}