f67a5370ff
Phase 14b of cabinet mic modeling. Two live, continuous placement controls in the CabinetPedal advanced pop-over: - Position [0,1]: on-axis (0, bright) -> cone edge / off-axis (1, dark), a live RBJ high-shelf cut (0..-9 dB @ 3.5 kHz). - Proximity [0,1]: distant (0, flat) -> close-mic'd (1), a live RBJ low-shelf boost (0..+6 dB @ 150 Hz) modelling the proximity effect. Both are LIVE post-convolution shelves (not baked), so they are smooth knobs with no rebuild-on-change, and each maps its knob to 0 dB at 0 -> a bit-exact identity at the defaults (existing cab presets sound unchanged). Placed in advancedKnobs (advancedValues), append-only, so old CabinetPedal presets don't shift Mix/Level positions. The look-ahead limiter still bounds output. Tests: Position/Proximity change output; neutral (0,0) is a bit-exact identity; advanced knobs exposed; round-trip through state; placement extremes stay within the ±1 ceiling. 664 tests pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
205 lines
10 KiB
Swift
205 lines
10 KiB
Swift
// CabinetPedal.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
|
|
/// Speaker-cabinet simulation via impulse-response convolution.
|
|
///
|
|
/// Convolves the input against a cabinet impulse response using Lyrebird's partitioned FFT
|
|
/// convolver (`ConvolutionReverb`). A single convolver is built at `build()` time for the voice the
|
|
/// ``cabinet`` selector currently names (each voicing's IR is synthesized in code — our own MIT
|
|
/// content, see ``CabinetIRFactory``). Changing the selector asks the host to rebuild the board so
|
|
/// only the selected voice's convolver ever runs — one partitioned-FFT convolver, not one per
|
|
/// voice. The selection round-trips through the rig save format like every other pedal.
|
|
///
|
|
/// Signal path (inside `effect(_:)`):
|
|
/// ```
|
|
/// dry → ConvolutionReverb(ir: selectedVoice) → mono out
|
|
/// ```
|
|
///
|
|
/// The wet/dry mix and bypass are handled by the `Pedal` base class.
|
|
///
|
|
/// > Note: Loading a user-supplied captured `.wav` cab IR is a planned follow-up (it needs a
|
|
/// > persisted file-path field the current `PedalState` doesn't carry); this first version ships
|
|
/// > the built-in voicings only.
|
|
///
|
|
/// ## Parameters
|
|
///
|
|
/// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus:
|
|
///
|
|
/// - `cabinet`: Selects the built-in cabinet voicing (stepped selector over ``CabinetVoice``).
|
|
public final class CabinetPedal: Pedal {
|
|
|
|
/// One-line description shown in the picker and info panel.
|
|
public override var summary: String {
|
|
"Speaker-cabinet impulse-response convolution with selectable built-in voicings."
|
|
}
|
|
|
|
// MARK: - Parameters
|
|
|
|
/// Selected cabinet voicing as an index into ``CabinetVoice/allCases``. Snaps to integer
|
|
/// stops via the ``cabinet`` selector; read at board-build time by `effect(_:)`, which
|
|
/// instantiates only that voice's convolver (the selector is `rebuildsOnChange`).
|
|
@UIParam public var cabinet: Double = 0.0
|
|
/// Selected microphone voicing as an index into ``MicVoice/allCases`` (`0` = No Mic). Baked into
|
|
/// the same IR as the cab voice, so it costs nothing extra at render time. `rebuildsOnChange`.
|
|
@UIParam public var mic: Double = 0.0
|
|
/// Mic **Position** on the speaker in `[0, 1]`: `0` = on-axis (bright, centred on the dust cap),
|
|
/// `1` = cone edge / off-axis (progressively darker). A live high-shelf cut, so it is a smooth
|
|
/// knob (no rebake); `0` is a bit-exact 0 dB identity. Surfaced in the advanced pop-over.
|
|
@UIParam public var position: Double = 0.0
|
|
/// Mic **Proximity** in `[0, 1]`: `0` = distant (flat lows), `1` = close-mic'd (proximity-effect
|
|
/// low-end boost). A live low-shelf boost, so it is a smooth knob (no rebake); `0` is a bit-exact
|
|
/// 0 dB identity. Surfaced in the advanced pop-over.
|
|
@UIParam public var proximity: Double = 0.0
|
|
|
|
// MARK: - Constants
|
|
|
|
/// Default wet/dry blend — fully wet; the cabinet sits in-line.
|
|
private static let defaultMix: Double = 1.0
|
|
|
|
/// Convolution is taken fully wet here; the `Pedal` base class provides the user wet/dry mix.
|
|
private let convolutionMix: Double = 1.0
|
|
|
|
/// Output peak ceiling. A true look-ahead limiter holds `|output| ≤` this, taming the peaks a
|
|
/// speaker convolution can push past ±1 (an analog cab can't digital-clip) while leaving signals
|
|
/// below the ceiling **untouched** — no always-on saturation of clean, below-ceiling material.
|
|
private let outputCeiling: Double = 1.0
|
|
/// Limiter look-ahead / output delay. Short (2 ms) so the added latency is negligible in-line
|
|
/// while still catching the brief convolution-overshoot transients.
|
|
private let limiterLookAheadSeconds: Double = 0.002
|
|
/// Limiter gain-release time constant — slow enough not to modulate sustained low frequencies.
|
|
private let limiterReleaseSeconds: Double = 0.050
|
|
|
|
/// Mic-position high-shelf corner (Hz) and its maximum cut (dB) at the cone edge (`position = 1`).
|
|
/// Moving off-axis toward the cone rolls the top off; on-axis (`0`) is a 0 dB identity.
|
|
private let positionShelfFreqHz: Double = 3500.0
|
|
private let positionMaxCutDb: Double = 9.0
|
|
/// Mic-proximity low-shelf corner (Hz) and its maximum boost (dB) up close (`proximity = 1`) —
|
|
/// the proximity-effect bass lift; distant (`0`) is a 0 dB identity.
|
|
private let proximityShelfFreqHz: Double = 150.0
|
|
private let proximityMaxBoostDb: Double = 6.0
|
|
/// Shelf slope for the position / proximity live shelves (gentle, like the amp shelves).
|
|
private let placementShelfSlope: Double = 0.7
|
|
/// `[0, 1]` range for the Position / Proximity pop-over knobs.
|
|
private static let placementRange: ClosedRange<Double> = 0.0 ... 1.0
|
|
|
|
/// Selector stops, one per ``CabinetVoice`` in `allCases` order (value == index).
|
|
private static let voiceStops: [PedalSelectorKnob.Stop] = CabinetVoice.allCases
|
|
.enumerated()
|
|
.map { (index: Int, voice: CabinetVoice) in
|
|
PedalSelectorKnob.Stop(title: voice.displayName, value: Double(index))
|
|
}
|
|
|
|
/// Selector stops, one per ``MicVoice`` in `allCases` order (value == index).
|
|
private static let micStops: [PedalSelectorKnob.Stop] = MicVoice.allCases
|
|
.enumerated()
|
|
.map { (index: Int, mic: MicVoice) in
|
|
PedalSelectorKnob.Stop(title: mic.displayName, value: Double(index))
|
|
}
|
|
|
|
// MARK: - Init
|
|
|
|
/// Creates a cabinet 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: "Cabinet",
|
|
inputBus: inputBus,
|
|
outputBus: outputBus,
|
|
initialMix: CabinetPedal.defaultMix
|
|
)
|
|
}
|
|
|
|
// MARK: - UI
|
|
|
|
public override var effectSelectors: [PedalSelectorKnob] {
|
|
[
|
|
PedalSelectorKnob(
|
|
label: "Cab", node: $cabinet, stops: CabinetPedal.voiceStops, rebuildsOnChange: true),
|
|
PedalSelectorKnob(
|
|
label: "Mic", node: $mic, stops: CabinetPedal.micStops, rebuildsOnChange: true),
|
|
]
|
|
}
|
|
|
|
/// Mic-placement deep-cut controls, tucked behind the pedal card's advanced pop-over: **Position**
|
|
/// (on-axis ↔ cone edge) and **Proximity** (distance / bass lift). Live shelves (not baked), so
|
|
/// they are smooth knobs; neither is a topology control. Persisted positionally in `advancedValues`.
|
|
public override var advancedKnobs: [PedalKnob] {
|
|
[
|
|
PedalKnob(label: "Position", node: $position, range: CabinetPedal.placementRange),
|
|
PedalKnob(label: "Proximity", node: $proximity, range: CabinetPedal.placementRange),
|
|
]
|
|
}
|
|
|
|
// MARK: - Effect
|
|
|
|
/// Convolves the dry signal against the single selected cabinet + mic IR, then soft-limits.
|
|
///
|
|
/// Only the voice/mic named by the live selectors is baked, so exactly one partitioned-FFT
|
|
/// convolver runs; the mic colour is folded into that same IR at no extra render cost. Changing
|
|
/// either selector triggers a board rebuild (both are `rebuildsOnChange`). Synthesizing the short
|
|
/// IR happens off the RT thread in `build()`.
|
|
///
|
|
/// The closing `LookAheadLimiter` is a transparent peak limiter on the **wet** convolution
|
|
/// output: a speaker cab is analog and can't digital-clip, and the convolution can push peaks
|
|
/// past ±1. Unlike a `tanh` saturator, a look-ahead limiter leaves signals **below** the ceiling
|
|
/// bit-for-bit untouched (no coloration of clean, below-ceiling material) and only pulls gain
|
|
/// down for the brief peaks that would exceed ±1. It adds a short (2 ms) output delay. At the
|
|
/// default fully-wet `mix` this bounds the pedal output to ±1 (e.g. a decoupled amp → cabinet
|
|
/// chain); a partial `mix` re-introduces the dry signal via the base class's equal-power
|
|
/// crossfade, so at intermediate settings final level is the board's output-stage concern. The
|
|
/// 2 ms wet delay is uniform at the default fully-wet mix; at a partial mix it sits against the
|
|
/// undelayed dry path, so a slight comb is expected there (a cab is normally run fully wet).
|
|
///
|
|
/// - Parameter dry: Dry mono input signal.
|
|
/// - Returns: The cabinet-convolved, mic-voiced, peak-limited mono output.
|
|
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
|
|
let sr: Double = sampleRate
|
|
let voice: CabinetVoice = CabinetVoice.voice(atSelectorValue: cabinet)
|
|
let micVoice: MicVoice = MicVoice.voice(atSelectorValue: mic)
|
|
let convolved: ConvolutionReverb.Audio = ConvolutionReverb.Audio(
|
|
input: dry,
|
|
ir: CabinetIRFactory.makeIR(voice: voice, mic: micVoice, sampleRate: sr),
|
|
mix: convolutionMix
|
|
)
|
|
let placed: any ValidGeneratorInput = applyPlacement(convolved)
|
|
return LookAheadLimiter(
|
|
in: placed,
|
|
maxAmplitude: outputCeiling,
|
|
lookAheadTime: limiterLookAheadSeconds,
|
|
releaseTime: limiterReleaseSeconds,
|
|
sampleRate: sr
|
|
)
|
|
}
|
|
|
|
/// Applies the live mic-placement shelves to the convolved signal: a **Position** high-shelf that
|
|
/// rolls the top off as the mic moves off-axis toward the cone edge, then a **Proximity**
|
|
/// low-shelf that lifts the low end as the mic moves closer. Both map their knob `[0, 1]` to a
|
|
/// shelf gain in dB with `0` → `0` dB, so at the default (`0, 0`) each shelf is a bit-exact
|
|
/// identity and the cabinet sounds exactly as it did before placement existed.
|
|
///
|
|
/// - Parameter signal: The cab-convolved signal.
|
|
/// - Returns: The placement-shaped signal.
|
|
private func applyPlacement(_ signal: any ValidGeneratorInput) -> any ValidGeneratorInput {
|
|
let positionCutDb: LinLin.Audio = LinLin.Audio(
|
|
$position, inLo: 0.0, inHi: 1.0, outLo: 0.0, outHi: -positionMaxCutDb)
|
|
let positioned: RBJHighShelf.Audio = RBJHighShelf.Audio(
|
|
input: signal, freq: positionShelfFreqHz, gain: positionCutDb, slope: placementShelfSlope)
|
|
let proximityBoostDb: LinLin.Audio = LinLin.Audio(
|
|
$proximity, inLo: 0.0, inHi: 1.0, outLo: 0.0, outHi: proximityMaxBoostDb)
|
|
return RBJLowShelf.Audio(
|
|
input: positioned, freq: proximityShelfFreqHz, gain: proximityBoostDb, slope: placementShelfSlope)
|
|
}
|
|
}
|