diff --git a/Sources/LyrebirdShoeGaze/Presets/Pedal+Preset.swift b/Sources/LyrebirdShoeGaze/Presets/Pedal+Preset.swift index cfbff3d..29a358d 100644 --- a/Sources/LyrebirdShoeGaze/Presets/Pedal+Preset.swift +++ b/Sources/LyrebirdShoeGaze/Presets/Pedal+Preset.swift @@ -48,7 +48,55 @@ extension Pedal { /// - 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) { + /// 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 + ) + } + + 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 diff --git a/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift b/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift index ab3c9f8..3edd427 100644 --- a/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift +++ b/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift @@ -101,6 +101,15 @@ public enum PedalRegistry { "Looper": { LooperPedal() }, ] + /// Maps a pedal `displayName` that has since been renamed to its current registry key, so rigs + /// saved under the old name still load (the `displayName` **is** the serialization key — see + /// ``BoardSegmentState`` — so a bare rename would silently drop the pedal on load). A loaded + /// pedal takes the new name and re-saves under it, so this is self-healing. Extend when renaming + /// any shipped pedal's `displayName`. + static let legacyTypeAliases: [String: String] = [ + "Fullerton Princeton": "Fullerton Ivy League", + ] + /// Returns a new `Pedal` instance for the given type string, or `nil` if the type /// is not in the built-in registry. /// @@ -108,11 +117,15 @@ public enum PedalRegistry { /// the caller is responsible for routing it via `board.add(_:)` before the engine /// processes any audio. /// + /// A ``legacyTypeAliases`` entry is resolved first, so a rig saved under a pedal's old + /// `displayName` still reconstructs the renamed pedal. + /// /// - Parameter type: The `displayName` of the pedal (e.g. `"Overdrive"`, `"Delay"`). /// - Returns: A freshly allocated `Pedal` subclass instance, or `nil` for unknown types. @MainActor public static func makePedal(type: String) -> Pedal? { - guard let constructor: @MainActor () -> Pedal = builtIn[type] else { return nil } + let resolvedType: String = legacyTypeAliases[type] ?? type + guard let constructor: @MainActor () -> Pedal = builtIn[resolvedType] else { return nil } return constructor() } diff --git a/Sources/LyrebirdShoeGazeUI/PedalView.swift b/Sources/LyrebirdShoeGazeUI/PedalView.swift index b72ac56..7d3f271 100644 --- a/Sources/LyrebirdShoeGazeUI/PedalView.swift +++ b/Sources/LyrebirdShoeGazeUI/PedalView.swift @@ -490,7 +490,7 @@ public struct PedalView: View { /// Presentation flag for the pedal info panel (description + control ranges). @State private var showInfo: Bool = false - /// Presentation flag for the advanced-controls pop-over (amp Sag / Bias / Bias X). + /// Presentation flag for the advanced-controls pop-over (amp Sag / Bias). @State private var showAdvanced: Bool = false /// Presentation flag for the Neural Amp model-file importer. diff --git a/Tests/LyrebirdShoeGazeTests/ChimeVariantsTests.swift b/Tests/LyrebirdShoeGazeTests/ChimeVariantsTests.swift index da1d5b2..50515a3 100644 --- a/Tests/LyrebirdShoeGazeTests/ChimeVariantsTests.swift +++ b/Tests/LyrebirdShoeGazeTests/ChimeVariantsTests.swift @@ -11,7 +11,7 @@ import LyrebirdUI @testable import LyrebirdShoeGaze @MainActor -@Suite("Chime Vox variants") +@Suite("Chime variants") struct ChimeVariantsTests { private let sampleRate: Double = 48000.0 @@ -71,7 +71,7 @@ struct ChimeVariantsTests { } } - @Test func inheritCutSurfaceAndVoxStack() throws { + @Test func inheritCutSurfaceAndChimeStack() throws { for v: (name: String, make: () -> ChimeAmpPedal) in variants { let pedal: ChimeAmpPedal = v.make() #expect(pedal.effectKnobs.map { $0.label } == ["Gain", "Bass", "Mid", "Treble", "Cut"], "\(v.name) knobs") diff --git a/Tests/LyrebirdShoeGazeTests/FullertonVariantsTests.swift b/Tests/LyrebirdShoeGazeTests/FullertonVariantsTests.swift index 7640d8d..ee303bf 100644 --- a/Tests/LyrebirdShoeGazeTests/FullertonVariantsTests.swift +++ b/Tests/LyrebirdShoeGazeTests/FullertonVariantsTests.swift @@ -124,4 +124,11 @@ struct FullertonVariantsTests { #expect(PedalRegistry.makePedal(type: "Fullerton Deluxe") is FullertonDeluxeAmpPedal) #expect(PedalRegistry.makePedal(type: "Fullerton Ivy League") is FullertonPrincetonAmpPedal) } + + @Test func legacyPrincetonNameStillResolves() throws { + // The pedal's display name was "Fullerton Princeton" when first shipped; rigs saved then key + // on that string. The legacy alias must still reconstruct the (renamed) pedal so it isn't + // silently dropped on load. + #expect(PedalRegistry.makePedal(type: "Fullerton Princeton") is FullertonPrincetonAmpPedal) + } } diff --git a/Tests/LyrebirdShoeGazeTests/ModernHighGainAmpPedalTests.swift b/Tests/LyrebirdShoeGazeTests/ModernHighGainAmpPedalTests.swift index 33febe7..cd65ae3 100644 --- a/Tests/LyrebirdShoeGazeTests/ModernHighGainAmpPedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/ModernHighGainAmpPedalTests.swift @@ -121,4 +121,26 @@ struct ModernHighGainAmpPedalTests { @Test func registeredInBuiltInRegistry() throws { #expect(PedalRegistry.makePedal(type: "Modern High-Gain") is ModernHighGainAmpPedal) } + + @Test func legacyPresetWithSagOnFaceMigratesWithoutShiftingMixLevel() throws { + // A rig saved when Sag was the 9th face knob has 11 knobValues (Gain…Drive, Sag, Mix, Level) + // and no advancedValues. Sag has since moved to the advanced pop-over, so the current shape + // has 10 knobValues (Gain…Drive, Mix, Level) + 2 advancedValues (Sag, Bias). Loading the + // legacy preset must lift Sag into advancedValues and keep Mix/Level in place — never shift + // the old Sag onto Mix or the old Mix onto Level. + let legacy: PedalState = PedalState( + id: UUID(), + type: "Modern High-Gain", + knobValues: [10.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 15.0, 0.7, 0.8, 0.9], + bypass: 0.0 + ) + let pedal: ModernHighGainAmpPedal = ModernHighGainAmpPedal() + pedal.applyState(legacy) + let restored: PedalState = pedal.captureState() + // Gain…Drive, then Mix (0.8), Level (0.9) — the old Sag (0.7) is gone from knobValues. + #expect(restored.knobValues == [10.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 15.0, 0.8, 0.9], + "legacy Sag shifted Mix/Level: \(restored.knobValues)") + // Sag lifted into advanced slot 0; Bias had no legacy value → default 0.5. + #expect(restored.advancedValues == [0.7, 0.5], "Sag/Bias advanced values wrong: \(restored.advancedValues)") + } }