52 lines
2.3 KiB
Swift
52 lines
2.3 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 eight
|
|
/// factory pedals: Overdrive, Delay, Reverb, Modulation, Fuzz, Tremolo, Phaser, and
|
|
/// Compressor.
|
|
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() },
|
|
]
|
|
|
|
/// 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()
|
|
}
|
|
}
|