Files
ShoeGaze/Sources/LyrebirdShoeGaze/Presets/PedalState.swift
T
josh eafc68d283 Add Harmonizer pedal and a stepped selector-knob control
Harmonizer: two granular PitchShift voices at fixed musical intervals
(ratio 2^(semitones/12) via Exp), mixed under the dry signal with V1/V2/
Direct level knobs. Two interval "knob pickers" snap through octave,
fifth, fourth, and minor/major third and sixth above and below.

New reusable control:
- PedalSelectorKnob (model) + PedalSelectorKnobView (rotary knob that snaps
  to labelled stops; drag up/down to step, shows the stop caption).
- Pedal.effectSelectors hook; PedalView renders a selector row.
- PedalState.selectorValues for preset round-trip (forward-compatible).

Registered + PedalType.harmonizer + enclosure color + palette slot.
Tests: build/non-silent, interval-change-changes-output, selector preset
round-trip, registry. Full suite 213 passing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 20:50:22 -07:00

86 lines
3.8 KiB
Swift

// PedalState.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// 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 {
/// 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
/// 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).
public init(
type: String,
knobValues: [Double],
switchValues: [Double] = [],
sliderValues: [Double] = [],
selectorValues: [Double] = [],
bypass: Double
) {
self.type = type
self.knobValues = knobValues
self.switchValues = switchValues
self.sliderValues = sliderValues
self.selectorValues = selectorValues
self.bypass = bypass
}
private enum CodingKeys: String, CodingKey {
case type
case knobValues
case switchValues
case sliderValues
case selectorValues
case bypass
}
/// Decodes a snapshot, tolerating presets saved before `switchValues` / `sliderValues` /
/// `selectorValues` existed by defaulting the missing keys to an empty array.
public init(from decoder: any Decoder) throws {
let container: KeyedDecodingContainer<CodingKeys> = try decoder.container(keyedBy: CodingKeys.self)
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)
}
}