3fe6b8a50a
Review follow-up to the single-convolver cab change. Now that only the selected cabinet voice's convolver is built, any path that changes the selector value without rebuilding leaves audio on the stale convolver. The UI drag already rebuilds; the gap was ShoeGaze.applyState (lightweight scene recall), which applies param snapshots to live pedals without a rebuild. - Pedal.rebuildSelectorValues: values of effectSelectors flagged rebuildsOnChange. - applyState/applySegmentParams/applyBranchParams: detect when a rebuildsOnChange selector value actually changes during recall and rebuild only those boards; boards without such a selector keep the cheap live-apply path. - CabinetVoice.voice(atSelectorValue:): guard non-finite and clamp in Double space so Int() can never trap on a corrupt preset value. - Doc fixes: CabinetPedal.cabinet and AmpVoicePedal chain diagram no longer claim a live Select. MIDI CC is unaffected: selectors are not in ccMappableControls (knobs + sliders only). Tests: recall changing the cab voice rebuilds and applies; recall with an unchanged voice does not rebuild. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
114 lines
4.3 KiB
Swift
114 lines
4.3 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
|
|
|
|
// 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
|
|
|
|
/// 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))
|
|
}
|
|
|
|
// 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),
|
|
]
|
|
}
|
|
|
|
// MARK: - Effect
|
|
|
|
/// Convolves the dry signal against the single selected cabinet IR.
|
|
///
|
|
/// Only the voice named by the live `cabinet` selector is instantiated, so exactly one
|
|
/// partitioned-FFT convolver runs. Changing the selector triggers a board rebuild (the selector
|
|
/// is `rebuildsOnChange`), which re-enters `effect(_:)` with the new voice — so switching cabs
|
|
/// costs a brief rebuild rather than running every voice at once. Synthesizing the short IR
|
|
/// happens off the RT thread in `build()`.
|
|
///
|
|
/// - Parameter dry: Dry mono input signal.
|
|
/// - Returns: The cabinet-convolved mono output.
|
|
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
|
|
let sr: Double = sampleRate
|
|
let voice: CabinetVoice = CabinetVoice.voice(atSelectorValue: cabinet)
|
|
return ConvolutionReverb.Audio(
|
|
input: dry,
|
|
ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr),
|
|
mix: convolutionMix
|
|
)
|
|
}
|
|
}
|