Files
ShoeGaze/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift
T
josh 20b2034fed feat(poly-octave): EHX POG-style polyphonic octave pedal
Add PolyOctavePedal: a fixed-interval polyphonic octave generator. Four
PitchShift voices at exact power-of-two ratios — −2, −1, +1, +2 octaves,
each single-stage within PitchShift's [0.25, 4.0] clamp — stacked under a
dry voice, each with its own level. Because the ratios need no pitch
tracking, chords stack as cleanly as single notes.

A Tone low-pass shapes the shifted voices (dry stays clean). An Attack
knob adds the POG's volume swell via a "slow-gear" gain: the ratio of a
slow envelope follower to a fast one, clamped to [0, 1] and guarded
against divide-by-zero — unity (instant) at Attack 0, blooming in as it
rises, independent of playing level. The five voice levels sum without an
internal limiter (as with BassOctaver); the level knob and board master
set final gain.

Wire into PedalType, the registry, and the pitch category; add to the
README pitch catalog. 12 tests: per-voice pitch (±1/±2 oct + unison) by
autocorrelation, swell neutral-vs-bloom, seeded determinism, a click-free
bypass flip at the real 512-sample block, knob labels, preset round-trip,
registry presence.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-24 09:53:43 -08:00

157 lines
7.5 KiB
Swift

// PedalRegistry.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// A factory registry mapping built-in pedal display names to their constructors.
///
/// `PedalRegistry` is the authoritative lookup used by `ShoeGaze.loadRig(from:)` to
/// instantiate the correct `Pedal` subclass for each `PedalState.type` string found
/// in a saved preset. Unknown type strings return `nil` from `makePedal(type:)` and
/// are silently skipped by the loader.
///
/// To register custom pedal types, extend `builtIn` by building your own dictionary
/// and merging it before calling `loadRig`. The built-in registry covers the factory
/// pedals: Overdrive, Delay, Reverb, Modulation, Fuzz, Tremolo, Phaser, Compressor,
/// Mutant, Auto-Wah, Feedback Delay, Multi-Tap Delay, Octave Splitter, and Ping-Pong.
///
/// > Note: A saved rig captures each board's *linear* pedal chain only (see `BoardState`).
/// > When a registered widener (e.g. Octave Splitter) or stereo-native pedal (e.g.
/// > Ping-Pong) appears in that chain it round-trips fully — `board.add(_:)` replays the
/// > mono → stereo width transition on load. Parallel sections (`split()`/`merge()`) and
/// > stereo split regions (`splitStereo()`) are *not* part of the captured linear chain
/// > and are therefore not serialized.
public enum PedalRegistry {
/// Maps each built-in pedal display name to a `@MainActor` no-argument constructor.
///
/// Keys are the `displayName` values returned by the corresponding `Pedal` subclass.
/// Values are closures that allocate a fresh instance with default bus indices —
/// `ShoeGaze.loadRig(from:)` updates `inputBus`/`outputBus` when adding the pedal
/// to a board via `board.add(_:)`.
public static let builtIn: [String: @MainActor () -> Pedal] = [
"California": { CaliforniaAmpPedal() },
"British": { BritishAmpPedal() },
"British Plexi": { BritishPlexiAmpPedal() },
"British JTM": { BritishJTMAmpPedal() },
"British Bluesbreaker": { BritishBluesbreakerAmpPedal() },
"British Master": { BritishMasterAmpPedal() },
"British Dual": { BritishDualAmpPedal() },
"Boutique Lead": { BoutiqueLeadAmpPedal() },
"Boutique OD": { BoutiqueODAmpPedal() },
"Cali Hi-Gain": { CaliHiGainAmpPedal() },
"Command": { CommandAmpPedal() },
"Citrus": { CitrusAmpPedal() },
"Sixties Combo": { SixtiesComboAmpPedal() },
"Magna Vibrato": { MagnaVibratoAmpPedal() },
"Anaheim": { AnaheimAmpPedal() },
"Modern High-Gain": { ModernHighGainAmpPedal() },
"Chime": { ChimeAmpPedal() },
"Chime 15": { ChimeFifteenAmpPedal() },
"Chime Normal": { ChimeNormalAmpPedal() },
"Chime Baby": { ChimeBabyAmpPedal() },
"Fullerton Reverb": { FullertonReverbAmpPedal() },
"Fullerton Deluxe": { FullertonDeluxeAmpPedal() },
"Fullerton Ivy League": { FullertonPrincetonAmpPedal() },
"Fullerton Drive": { FullertonDriveAmpPedal() },
"Jazz Clean": { JazzCleanAmpPedal() },
"Chime Top Boost": { ChimeTopBoostAmpPedal() },
// "Neural Amp" intentionally NOT registered -- hidden from the palette while the neural
// model-loader's security-scoped file access is unresolved. NeuralAmpPedal + its UI are kept
// for later iteration; re-add this entry (and the palette order below) to re-enable.
"Overdrive": { OverdrivePedal() },
"Distortion": { DistortionPedal() },
"Delay": { DelayPedal() },
"Reverb": { ReverbPedal() },
"Modulation": { ModulationPedal() },
"Fuzz": { FuzzPedal() },
"Sustain Fuzz": { SustainFuzzPedal() },
"Tremolo": { TremoloPedal() },
"Phaser": { PhaserPedal() },
"Compressor": { CompressorPedal() },
"Mutant": { MutantPedal() },
"Auto-Wah": { AutoWahPedal() },
"Wah": { WahPedal() },
"Volume": { VolumePedal() },
"Feedback Delay": { FeedbackDelayPedal() },
"Multi-Tap Delay": { MultiTapDelayPedal() },
"Octave Splitter": { OctaveUpSplitterPedal() },
"Bass Octaver": { BassOctaverPedal() },
"Harmonizer": { HarmonizerPedal() },
"Whammy": { WhammyPedal() },
"Poly Octave": { PolyOctavePedal() },
"Ping-Pong": { PingPongDelayPedal() },
"Reverse Delay": { ReverseDelayPedal() },
"Shimmer Reverb": { ShimmerReverbPedal() },
"Plate Reverb": { PlateReverbPedal() },
"Room Reverb": { RoomReverbPedal() },
"Spring Reverb": { SpringReverbPedal() },
"Limiter": { LimiterPedal() },
"Cabinet": { CabinetPedal() },
"Noise Gate": { NoiseGatePedal() },
"Graphic EQ": { GraphicEQPedal() },
"Chorus": { ChorusPedal() },
"Flanger": { FlangerPedal() },
"Vibrato": { VibratoPedal() },
"Vibe": { VibePedal() },
"Rotary": { RotaryPedal() },
"Sustainer": { SustainerPedal() },
"Ring Modulator": { RingModulatorPedal() },
"Diffuser": { DiffuserPedal() },
"Bitcrusher": { BitcrusherPedal() },
"Granulator": { GranulatorPedal() },
"Auto Granulator": { AutoGranulatorPedal() },
"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.
///
/// The returned pedal has default bus indices (`inputBus: 0`, `outputBus: 0`);
/// 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? {
let resolvedType: String = legacyTypeAliases[type] ?? type
guard let constructor: @MainActor () -> Pedal = builtIn[resolvedType] else { return nil }
return constructor()
}
/// Cached `displayName → summary` map for all built-in pedals, built once by instantiating
/// each pedal a single time (no `build()` / engine work). Lets the picker look up
/// descriptions per row without reconstructing a pedal on every render.
@MainActor
public static let summaries: [String: String] = {
var map: [String: String] = [:]
for type: String in builtIn.keys {
map[type] = makePedal(type: type)?.summary ?? ""
}
return map
}()
/// Returns the `summary` description for a built-in pedal type, or `""` if unknown.
///
/// - Parameter type: The `displayName` of the pedal.
/// - Returns: The pedal's one-line `summary`, or `""` for unknown types.
@MainActor
public static func summary(for type: String) -> String {
summaries[type] ?? ""
}
}