Files
ShoeGaze/Sources/LyrebirdShoeGaze/Pedals/AmpPedal.swift
T
josh 23d607b9fc feat(ui): amp-head layout + Neural Amp model loader (S4)
- AmpPedal: double-width (widthUnits 2) amp-head card so the four tone knobs +
  Amp/Cab selectors + master aren't cramped. Uses the generic single-column
  content (no greedy Spacers → no flow-canvas height issue).
- Neural Amp model loader: PedalView renders, for a NeuralAmpPedal, a "Load
  Model…" picker (.fileImporter for .json) plus the loaded model's filename.
  Picking sets Pedal.resourcePath and asks the host to rebuild the board so the
  model applies live. Picker + AppKit stay in the UI layer (core imports none).
- New `pedalRebuildAction` environment value (@MainActor @Sendable), installed by
  ShoeGazeBuilderView; it resolves the pedal's board via a new branch-aware
  `ShoeGaze.board(containing:)` (finds pedals inside parallel / stereo-split
  branches, not just the main chain) and rebuilds it via the live-safe
  `rebuildBoard(_:from:)`.

Tests: AmpPedal widthUnits == 2; board(containing:) finds main + branch pedals
and returns nil for a stranger. Full suite 502. (SwiftUI loader flow needs
on-device visual QA.)

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

173 lines
6.7 KiB
Swift

// AmpPedal.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// A full component-style guitar amp: preamp tube stage → tone stack → power-amp tube stage →
/// speaker cabinet, all white-box (no neural capture).
///
/// Signal chain (inside `effect(_:)`):
/// ```
/// dry → TriodeStage(drive: gain) (preamp)
/// → ToneStack(bass, mid, treble, model) (tone stack — Fender/Marshall/Vox, live-selectable)
/// → TriodeStage(power-amp drive) (power-amp warmth)
/// → ConvolutionReverb(cab IR) (speaker cabinet — live-selectable voice)
/// → mono out
/// ```
///
/// The tone-stack model and cabinet voice are each built once per option at `build()` and picked
/// live with a `Select` (so both round-trip through the rig save format). The amp is fully wet;
/// `Level` is the master volume. Built on Lyrebird 1.7's `TriodeStage` / `ToneStack` plus
/// ShoeGaze's synthesized cabinet IRs.
///
/// > Note: `Select` evaluates *all* its inputs every block (SC parity — keeps state in sync), so
/// > every tone-stack model **and** every cabinet convolver runs continuously, not just the
/// > selected one. That is a few extra IIRs plus one partitioned-FFT convolver per unselected
/// > cabinet voice — a real (but bounded) CPU cost that scales with the number of voices. The
/// > cabinet IRs are synthesized at `build()` for the injected `sampleRate`; a live engine
/// > sample-rate change takes effect on the next rebuild.
///
/// ## Parameters
///
/// Inherits `bypass` and `level` (master) from `Pedal`, plus:
///
/// - `gain`: Preamp drive. Range `[1, 30]`.
/// - `bass` / `mid` / `treble`: Tone-stack controls in `[0, 1]`.
/// - `amp`: Tone-stack model (Fender / Marshall / Vox).
/// - `cab`: Speaker-cabinet voice.
public final class AmpPedal: Pedal {
/// One-line description shown in the picker and info panel.
public override var summary: String {
"Full tube amp — preamp, tone stack, power amp, and speaker cabinet in one."
}
/// The amp is fully wet (it replaces the dry signal); hide the Mix control.
public override var showsMixControl: Bool { false }
/// Amp-head-style double-width card so the four tone knobs plus the Amp / Cab selectors and
/// master lay out with room, rather than cramping into a single-width stompbox.
public override var widthUnits: Int { 2 }
// MARK: - Parameters
/// Preamp drive into the first tube stage. Range [1, 30].
@UIParam public var gain: Double = 6.0
/// Tone-stack bass control in [0, 1].
@UIParam public var bass: Double = 0.5
/// Tone-stack mid control in [0, 1].
@UIParam public var mid: Double = 0.5
/// Tone-stack treble control in [0, 1].
@UIParam public var treble: Double = 0.6
/// Selected tone-stack model as an index into `ToneStackModel.allCases`.
@UIParam public var amp: Double = 0.0
/// Selected cabinet voice as an index into `CabinetVoice.allCases`.
@UIParam public var cab: Double = 0.0
// MARK: - Constants
private static let defaultMix: Double = 1.0
/// Preamp grid bias — a little asymmetry for even-harmonic warmth.
private let preampBias: Double = 0.2
/// Fixed power-amp stage drive and bias (gentler than the preamp).
private let powerAmpDrive: Double = 1.5
private let powerAmpBias: Double = 0.0
/// Fully-wet convolution; the base class provides bypass and master level.
private let cabinetMix: Double = 1.0
/// Gain knob range.
private let gainRange: ClosedRange<Double> = 1.0 ... 30.0
private let toneRange: ClosedRange<Double> = 0.0 ... 1.0
private static let ampStops: [PedalSelectorKnob.Stop] = ToneStackModel.allCases
.enumerated()
.map { (index: Int, model: ToneStackModel) in
PedalSelectorKnob.Stop(title: model.displayName, value: Double(index))
}
private static let cabStops: [PedalSelectorKnob.Stop] = CabinetVoice.allCases
.enumerated()
.map { (index: Int, voice: CabinetVoice) in
PedalSelectorKnob.Stop(title: voice.displayName, value: Double(index))
}
// MARK: - Init
/// Creates an amp pedal.
///
/// - Parameters:
/// - inputBus: Audio bus index to read the dry input from. Defaults to `0`.
/// - outputBus: Audio bus index to write the processed output to. Defaults to `0`.
public init(
inputBus: Int = 0,
outputBus: Int = 0
) {
super.init(
name: "Amp",
inputBus: inputBus,
outputBus: outputBus,
initialMix: AmpPedal.defaultMix
)
}
// MARK: - UI
public override var effectKnobs: [PedalKnob] {
[
PedalKnob(label: "Gain", node: $gain, range: gainRange),
PedalKnob(label: "Bass", node: $bass, range: toneRange),
PedalKnob(label: "Mid", node: $mid, range: toneRange),
PedalKnob(label: "Treble", node: $treble, range: toneRange),
]
}
public override var effectSelectors: [PedalSelectorKnob] {
[
PedalSelectorKnob(label: "Amp", node: $amp, stops: AmpPedal.ampStops),
PedalSelectorKnob(label: "Cab", node: $cab, stops: AmpPedal.cabStops),
]
}
// MARK: - Effect
/// Preamp → tone stack (model-selected) → power amp → cabinet (voice-selected).
///
/// - Parameter dry: Dry mono input signal.
/// - Returns: The amped, cab-convolved mono output.
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let sr: Double = sampleRate
// Preamp tube stage.
let preamp: TriodeStage = TriodeStage(input: dry, drive: $gain, bias: preampBias, oversample: 4)
// Tone stack — one per model, picked live.
let toneStacks: [any ValidGeneratorInput] = ToneStackModel.allCases.map { (model: ToneStackModel) in
ToneStack(input: preamp, bass: $bass, mid: $mid, treble: $treble, model: model)
}
let toned: Select.Audio = Select.Audio(which: $amp, inputs: toneStacks)
// Power-amp tube stage (fixed, gentle).
let powerAmp: TriodeStage = TriodeStage(input: toned, drive: powerAmpDrive, bias: powerAmpBias, oversample: 2)
// Speaker cabinet — one convolver per voice, picked live.
let cabinets: [any ValidGeneratorInput] = CabinetVoice.allCases.map { (voice: CabinetVoice) in
ConvolutionReverb.Audio(
input: powerAmp,
ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr),
mix: cabinetMix
)
}
return Select.Audio(which: $cab, inputs: cabinets)
}
}