Files
ShoeGaze/Sources/LyrebirdShoeGaze/Presets/PedalState.swift
T
josh 1de14fd879 feat(amp): shared power-amp Sag + Bias in an advanced pop-over
Phase 11. Promote Sag from Modern High-Gain to a shared AmpVoicePedal
capability and add a Bias control, both surfaced in a new pedal-card
pop-over rather than on the amp face.

- AmpVoicePedal gains `sag` [0,1] (AmplitudeFollower gain-droop, clamped
  [0.15,1] to prevent phase inversion) and `bias` [0,1] mapped to the
  power-amp TriodeStage grid bias ([-0.25,0.25], neutral 0.5 → 0.0 so
  existing voices are unchanged at defaults). Both applied in
  preCabinetCore; new `applySag(_:)` + `powerAmpBiasInput` helpers.
- Modern High-Gain drops its bespoke sag (now inherited); keeps sag=0.3
  default. Sag moves off its face into the pop-over.
- Pedal gains an `advancedKnobs` surface; PedalView draws a gear button
  (top-right) presenting them in a popover, reusing the existing pattern.
- Advanced knobs round-trip through PedalState (new `advancedValues`,
  backward-compatible decode) and are MIDI-CC-mappable (appended last).

De-branding (review nits): "Vox" removed from the Chime family's
user-visible summaries + docs; "Fullerton Princeton" renamed to display
"Fullerton Ivy League". Palette horizontal picker second row now shows
Routing before the Amp Model group.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-03 21:08:23 -07:00

127 lines
6.0 KiB
Swift

// PedalState.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// Serialisable snapshot of one pedal's parameter values.
///
/// `type` stores the pedal's `displayName` and is used by `PedalRegistry` to
/// reconstruct the correct subclass on load. `knobValues` mirrors `Pedal.knobs`
/// in order — effect-specific knobs first, then `mix`, then `level`. `bypass`
/// stores the bypass parameter value (`0.0` = engaged, `1.0` = bypassed).
///
/// - Note: Extra knob values beyond the pedal's current knob count are silently
/// ignored on apply; missing values (fewer entries than knobs) leave trailing
/// knobs at their current value.
public struct PedalState: Codable, Equatable {
/// Stable per-pedal identity, persisted so MIDI bindings can address a pedal by id rather
/// than by chain position. Survives capture → rebuild round-trips (an edit rebuilds the board
/// from captured segments, carrying each pedal's id forward). Legacy presets without an id
/// decode to a fresh `UUID()`.
public let id: UUID
/// The pedal's `displayName` — used as a factory key by `PedalRegistry`.
public let type: String
/// Knob values in the same order as `Pedal.knobs` (effect knobs then Mix, Level).
public let knobValues: [Double]
/// Switch values in the same order as `Pedal.effectSwitches`. Empty for pedals with
/// no switches. Absent in presets saved before switches existed — decodes to `[]`.
public let switchValues: [Double]
/// Slider values in the same order as `Pedal.effectSliders`. Empty for pedals with
/// no sliders. Absent in presets saved before sliders existed — decodes to `[]`.
public let sliderValues: [Double]
/// Selector-knob values in the same order as `Pedal.effectSelectors`. Empty for pedals with
/// no selectors. Absent in presets saved before selectors existed — decodes to `[]`.
public let selectorValues: [Double]
/// Advanced (pop-over) knob values in the same order as `Pedal.advancedKnobs`. Empty for pedals
/// with no advanced knobs. Absent in presets saved before advanced knobs existed — decodes to `[]`.
public let advancedValues: [Double]
/// Bypass parameter value. `0.0` = engaged, `1.0` = bypassed.
public let bypass: Double
/// Tempo-sync state per syncable parameter, keyed by ``TempoSyncableParam/key``. `nil` for a
/// pedal with no synced parameters, and for presets saved before tempo sync existed — both
/// decode to "all parameters free".
public let tempoSync: [String: TempoSync]?
/// Optional path to an external resource the pedal references (e.g. a neural-amp model file).
/// `nil` for pedals that use no external file, and for presets saved before this existed.
public let resourcePath: String?
/// Creates a pedal state snapshot.
///
/// - Parameters:
/// - type: The pedal's `displayName`, used to identify the pedal type on load.
/// - knobValues: Current knob values in `Pedal.knobs` order.
/// - switchValues: Current switch values in `Pedal.effectSwitches` order. Defaults to empty.
/// - sliderValues: Current slider values in `Pedal.effectSliders` order. Defaults to empty.
/// - selectorValues: Current selector values in `Pedal.effectSelectors` order. Defaults to empty.
/// - advancedValues: Current advanced-knob values in `Pedal.advancedKnobs` order. Defaults to empty.
/// - bypass: Bypass parameter value (`0.0` = engaged, `1.0` = bypassed).
/// - tempoSync: Tempo-sync state per syncable parameter, or `nil` for none. Defaults to `nil`.
/// - resourcePath: Path to an external resource the pedal references (e.g. a neural-amp
/// model file), or `nil`. Defaults to `nil`.
public init(
id: UUID = UUID(),
type: String,
knobValues: [Double],
switchValues: [Double] = [],
sliderValues: [Double] = [],
selectorValues: [Double] = [],
advancedValues: [Double] = [],
bypass: Double,
tempoSync: [String: TempoSync]? = nil,
resourcePath: String? = nil
) {
self.id = id
self.type = type
self.knobValues = knobValues
self.switchValues = switchValues
self.sliderValues = sliderValues
self.selectorValues = selectorValues
self.advancedValues = advancedValues
self.bypass = bypass
self.tempoSync = tempoSync
self.resourcePath = resourcePath
}
private enum CodingKeys: String, CodingKey {
case id
case type
case knobValues
case switchValues
case sliderValues
case selectorValues
case advancedValues
case bypass
case tempoSync
case resourcePath
}
/// Decodes a snapshot, tolerating presets saved before `switchValues` / `sliderValues` /
/// `selectorValues` / `advancedValues` / `tempoSync` existed by defaulting the missing keys.
public init(from decoder: any Decoder) throws {
let container: KeyedDecodingContainer<CodingKeys> = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decodeIfPresent(UUID.self, forKey: .id) ?? UUID()
self.type = try container.decode(String.self, forKey: .type)
self.knobValues = try container.decode([Double].self, forKey: .knobValues)
self.switchValues = try container.decodeIfPresent([Double].self, forKey: .switchValues) ?? []
self.sliderValues = try container.decodeIfPresent([Double].self, forKey: .sliderValues) ?? []
self.selectorValues = try container.decodeIfPresent([Double].self, forKey: .selectorValues) ?? []
self.advancedValues = try container.decodeIfPresent([Double].self, forKey: .advancedValues) ?? []
self.bypass = try container.decode(Double.self, forKey: .bypass)
self.tempoSync = try container.decodeIfPresent([String: TempoSync].self, forKey: .tempoSync)
self.resourcePath = try container.decodeIfPresent(String.self, forKey: .resourcePath)
}
}