Files
ShoeGaze/Sources/LyrebirdShoeGaze/Presets/PedalState.swift
T
josh 40fa590196 feat(pedals): Neural Amp pedal (S3) + PedalState resource path; Lyrebird 1.7.1
Bump Lyrebird to 1.7.1 (ProteusModelImporter) and add a neural amp-capture pedal.

- NeuralAmpPedal: loads a GuitarML Proteus/NeuralPi model (via ProteusModelImporter)
  and runs it with LSTMAmp on the gain-trimmed input; passes through clean when no
  model is loaded. Model files are user-supplied, not bundled (GuitarML = GPL-3.0).
- PedalState gains an optional `resourcePath` (backward-compatible, decodeIfPresent)
  and Pedal a matching `resourcePath` property, wired through captureState/applyState
  — so the loaded model file persists across rig save/load. The neural pedal reads it
  at build() (applied on rebuild). This foundation also unblocks a future
  custom-cabinet-IR path.
- Registered in PedalRegistry / PedalType / PedalCategory (.gain) / palette /
  enclosure color; Help acknowledges the GuitarML model format.

Tests: no-model passthrough; loaded model transforms output & differs from
passthrough; gain affects modeled output; resourcePath round-trips through
PedalState; gain knob exposed; registry resolves "Neural Amp". Full suite 501.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-01 20:48:44 -07:00

118 lines
5.4 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]
/// 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.
/// - 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] = [],
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.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 bypass
case tempoSync
case resourcePath
}
/// Decodes a snapshot, tolerating presets saved before `switchValues` / `sliderValues` /
/// `selectorValues` / `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.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)
}
}