59370ee5dd
The cab selector was a Select over one ConvolutionReverb per CabinetVoice, and Select evaluates every input each block — so all 8 voices' partitioned-FFT convolvers ran continuously (7/8 discarded), per CabinetPedal and per amp voice. Build only the selected voice's convolver. The cab selector is now rebuildsOnChange: committing a new stop asks the host to rebuild the board (same mechanism the Neural Amp model loader uses), which re-enters effect() with the new voice. One convolver runs instead of N; cost moves to a brief rebuild on cab switch (cabs are set-and-forget). - PedalSelectorKnob: add rebuildsOnChange flag. - PedalSelectorKnobView: fire onCommit on drag-end when the stop changed. - PedalView: wire onCommit → pedalRebuildAction for rebuildsOnChange selectors. - CabinetVoice.voice(atSelectorValue:): rounded/clamped index → voice. - CabinetPedal + AmpVoicePedal: single ConvolutionReverb for the selected voice. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
155 lines
6.1 KiB
Swift
155 lines
6.1 KiB
Swift
// AmpVoicePedal.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
|
|
/// Shared base for the fixed-voicing amp pedals (California / British / Chime).
|
|
///
|
|
/// Each concrete amp fixes one tone-stack ``ToneStackModel`` and adds one signature control, but
|
|
/// shares the common chain built here:
|
|
/// ```
|
|
/// dry → TriodeStage(drive: gain) (preamp)
|
|
/// → ToneStack(bass, mid, treble, model) (fixed per amp)
|
|
/// → TriodeStage(power-amp drive) (power-amp warmth)
|
|
/// → shapeVoice(_:) (subclass signature filter)
|
|
/// → ConvolutionReverb(cab IR) (speaker cabinet — live-selectable voice)
|
|
/// ```
|
|
/// Fully wet (the amp replaces the dry signal); `Level` is the master. Double-width amp-head card.
|
|
///
|
|
/// Subclasses override ``toneStackModel``, ``summaryText``, ``signatureKnobs``, and
|
|
/// ``shapeVoice(_:)``, and may set a voice-appropriate default `gain` in their `init`.
|
|
open class AmpVoicePedal: Pedal {
|
|
|
|
// MARK: - Common 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 cabinet voice as an index into `CabinetVoice.allCases`.
|
|
@UIParam public var cab: Double = 0.0
|
|
|
|
// MARK: - Subclass configuration
|
|
|
|
/// The fixed tone-stack model for this amp voice. Subclasses override.
|
|
open var toneStackModel: ToneStackModel { .fender }
|
|
/// One-line description. Subclasses override.
|
|
open var summaryText: String { "Tube amp." }
|
|
/// The amp's signature knob(s), appended after Gain/Bass/Mid/Treble. Default none.
|
|
open var signatureKnobs: [PedalKnob] { [] }
|
|
|
|
/// Applies the amp's signature post-power-amp shaping (e.g. a bright/presence shelf or a cut
|
|
/// low-pass). Default: identity. Subclasses override.
|
|
///
|
|
/// - Parameter signal: The post-power-amp signal.
|
|
/// - Returns: The shaped signal fed to the cabinet.
|
|
open func shapeVoice(_ signal: any ValidGeneratorInput) -> any ValidGeneratorInput {
|
|
signal
|
|
}
|
|
|
|
// 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/bias (gentler than the preamp).
|
|
private let powerAmpDrive: Double = 1.5
|
|
private let powerAmpBias: Double = 0.0
|
|
private let cabinetMix: Double = 1.0
|
|
|
|
let gainRange: ClosedRange<Double> = 1.0 ... 30.0
|
|
let toneRange: ClosedRange<Double> = 0.0 ... 1.0
|
|
/// Range for a signature control that maps [0, 1] (Bright / Presence / Cut amount).
|
|
let signatureRange: ClosedRange<Double> = 0.0 ... 1.0
|
|
|
|
private static let cabStops: [PedalSelectorKnob.Stop] = CabinetVoice.allCases
|
|
.enumerated()
|
|
.map { (index: Int, voice: CabinetVoice) in
|
|
PedalSelectorKnob.Stop(title: voice.displayName, value: Double(index))
|
|
}
|
|
|
|
// MARK: - Chrome
|
|
|
|
/// 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.
|
|
public override var widthUnits: Int { 2 }
|
|
|
|
public override var summary: String { summaryText }
|
|
|
|
// MARK: - Init
|
|
|
|
/// Creates an amp voice pedal.
|
|
///
|
|
/// - Parameters:
|
|
/// - name: Display name (the amp voice, e.g. "California").
|
|
/// - 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(
|
|
name: String,
|
|
inputBus: Int = 0,
|
|
outputBus: Int = 0
|
|
) {
|
|
super.init(
|
|
name: name,
|
|
inputBus: inputBus,
|
|
outputBus: outputBus,
|
|
initialMix: AmpVoicePedal.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),
|
|
] + signatureKnobs
|
|
}
|
|
|
|
public override var effectSelectors: [PedalSelectorKnob] {
|
|
[
|
|
PedalSelectorKnob(
|
|
label: "Cab", node: $cab, stops: AmpVoicePedal.cabStops, rebuildsOnChange: true),
|
|
]
|
|
}
|
|
|
|
// MARK: - Effect
|
|
|
|
/// Preamp → fixed tone stack → power amp → signature shaping → cabinet (voice-selected).
|
|
///
|
|
/// > Note: only the voice named by the live `cab` selector is instantiated, so exactly one
|
|
/// > `ConvolutionReverb` runs. The `cab` selector is `rebuildsOnChange`, so switching cabs
|
|
/// > rebuilds the board (re-entering `effect(_:)` with the new voice) rather than running every
|
|
/// > voice at once.
|
|
///
|
|
/// - 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
|
|
let preamp: TriodeStage = TriodeStage(input: dry, drive: $gain, bias: preampBias, oversample: 4)
|
|
let toned: ToneStack = ToneStack(
|
|
input: preamp, bass: $bass, mid: $mid, treble: $treble, model: toneStackModel)
|
|
let powerAmp: TriodeStage = TriodeStage(
|
|
input: toned, drive: powerAmpDrive, bias: powerAmpBias, oversample: 2)
|
|
let shaped: any ValidGeneratorInput = shapeVoice(powerAmp)
|
|
let voice: CabinetVoice = CabinetVoice.voice(atSelectorValue: cab)
|
|
return ConvolutionReverb.Audio(
|
|
input: shaped,
|
|
ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr),
|
|
mix: cabinetMix
|
|
)
|
|
}
|
|
}
|