Files
ShoeGaze/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift
T
josh 8c701af128 Add stereo Diffuser pedal
A .widener pedal that splits the dry signal into overlapping RBJ band-pass bands
and pans each by frequency (low to left, high to right); the spread knob fans the
spectrum from mono (spread 0) to a wide diffuse image (spread 1), using linear
pan expressed purely as node arithmetic.

Registered, with PedalType.diffuser, enclosure color, palette order, summary,
and tests (non-silent stereo; L == R at spread 0; L != R at full spread).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 19:14:00 -07:00

86 lines
4.1 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] = [
"Overdrive": { OverdrivePedal() },
"Delay": { DelayPedal() },
"Reverb": { ReverbPedal() },
"Modulation": { ModulationPedal() },
"Fuzz": { FuzzPedal() },
"Tremolo": { TremoloPedal() },
"Phaser": { PhaserPedal() },
"Compressor": { CompressorPedal() },
"Mutant": { MutantPedal() },
"Auto-Wah": { AutoWahPedal() },
"Feedback Delay": { FeedbackDelayPedal() },
"Multi-Tap Delay": { MultiTapDelayPedal() },
"Octave Splitter": { OctaveUpSplitterPedal() },
"Bass Octaver": { BassOctaverPedal() },
"Ping-Pong": { PingPongDelayPedal() },
"Limiter": { LimiterPedal() },
"Noise Gate": { NoiseGatePedal() },
"Graphic EQ": { GraphicEQPedal() },
"Chorus": { ChorusPedal() },
"Sustainer": { SustainerPedal() },
"Ring Modulator": { RingModulatorPedal() },
"Diffuser": { DiffuserPedal() },
]
/// 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.
///
/// - 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 }
return constructor()
}
/// Returns the `summary` description for a built-in pedal type, or `""` if unknown.
///
/// Instantiates the pedal to read its `summary` — cheap, since constructing a `Pedal`
/// does not call `build()` or touch the engine. Intended for the pedal picker, which has
/// only the type string; callers that render many rows should cache the result.
///
/// - 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 {
makePedal(type: type)?.summary ?? ""
}
}