2 Commits

Author SHA1 Message Date
josh 9b23f99a58 docs(cabinet): fix doc attachment + class-level surface for dual-mic
Review follow-ups (all doc-only; DSP/RT/preset verified correct):
- The blendSecondMic insertion had detached applyPlacement's summary; give each
  helper its own complete doc block again.
- Update the class-level signal-path diagram + ## Parameters list for micB /
  micBlend / micOffset and the optional parallel second-convolver branch.
- micOffset: note it is a set-and-leave spacing control (live-sweeping it moves
  the comb, a flanger-like effect) — keeps .linear interp so micOffset=0 is a
  true zero-delay alignment.
- Correct the maxMicOffsetSeconds comment (1.5 ms ~ 0.5 m spacing, not "mic
  diameters"; first notch near 330 Hz at full offset).

669 tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-04 09:40:09 -07:00
josh d7eb0690ff feat(cabinet): dual-mic blend with comb (14c)
Phase 14c — the classic two-mic technique. When "Mic B" (a second mic-voice
selector, default No Mic) is engaged, a parallel convolver (same cab voice,
mic B's IR) is delayed by up to 1.5 ms (Mic Offset) and summed with the
aligned primary at the Mic Blend balance — the time offset between the two
coherent copies produces comb filtering (e.g. a 57 + 121 blend).

- Mic B: rebuildsOnChange selector; No Mic (default) builds NO second
  convolver, so the single-mic path is bit-identical to before and costs one
  convolution. Mic Blend / Mic Offset are live smooth knobs (advanced pop-over,
  appended after Position/Proximity — append-only for advancedValues).
- Coherent (same-source) mics sum linearly: A*(1-blend) + Bdelayed*blend.
- The look-ahead limiter still bounds the summed output to ±1.

Tests: second mic changes output; with Mic B Off the blend/offset are inert
(single-mic path untouched); blend + comb offset both change output when
engaged; dual-mic round-trips through state; dual-mic stays within ±1. 669
tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-04 09:31:14 -07:00
2 changed files with 144 additions and 10 deletions
@@ -19,6 +19,8 @@ import LyrebirdUI
/// Signal path (inside `effect(_:)`): /// Signal path (inside `effect(_:)`):
/// ``` /// ```
/// dry → ConvolutionReverb(ir: cab voice + mic voice) (one IR — mic colour baked in) /// dry → ConvolutionReverb(ir: cab voice + mic voice) (one IR — mic colour baked in)
/// ├─ (optional) ConvolutionReverb(ir: cab + mic B) → DelayLine(Mic Offset) (parallel 2nd mic)
/// │ blended A·(1−Mic Blend) + Bdelayed·(Mic Blend) (comb of two mics)
/// → RBJHighShelf (Position) → RBJLowShelf (Proximity) (live mic-placement shelves) /// → RBJHighShelf (Position) → RBJLowShelf (Proximity) (live mic-placement shelves)
/// → LookAheadLimiter(±1) (transparent peak safety) /// → LookAheadLimiter(±1) (transparent peak safety)
/// → mono out /// → mono out
@@ -36,8 +38,12 @@ import LyrebirdUI
/// ///
/// - `cabinet`: Selects the built-in cabinet voicing (stepped selector over ``CabinetVoice``). /// - `cabinet`: Selects the built-in cabinet voicing (stepped selector over ``CabinetVoice``).
/// - `mic`: Selects the microphone voicing baked into the IR (stepped selector over ``MicVoice``). /// - `mic`: Selects the microphone voicing baked into the IR (stepped selector over ``MicVoice``).
/// - `micB`: Optional second mic (stepped selector over ``MicVoice``; No Mic = single mic). When set,
/// a parallel convolver blends in for the classic two-mic technique.
/// - `position` / `proximity`: Live mic-placement shelves in the advanced pop-over (on-axis ↔ edge, /// - `position` / `proximity`: Live mic-placement shelves in the advanced pop-over (on-axis ↔ edge,
/// distant ↔ close). /// distant ↔ close).
/// - `micBlend` / `micOffset`: Advanced pop-over — the A/B balance and the mic-B time offset (comb
/// spacing) when a second mic is engaged.
public final class CabinetPedal: Pedal { public final class CabinetPedal: Pedal {
/// One-line description shown in the picker and info panel. /// One-line description shown in the picker and info panel.
@@ -62,6 +68,21 @@ public final class CabinetPedal: Pedal {
/// low-end boost). A live low-shelf boost, so it is a smooth knob (no rebake); `0` is a bit-exact /// 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. /// 0 dB identity. Surfaced in the advanced pop-over.
@UIParam public var proximity: Double = 0.0 @UIParam public var proximity: Double = 0.0
/// Second microphone voicing as an index into ``MicVoice/allCases`` (`0` = No Mic → single mic,
/// the default). When engaged, a **second** convolver runs in parallel and blends with the first
/// (the classic two-mic technique). `rebuildsOnChange` — toggling it adds/removes the convolver.
@UIParam public var micB: Double = 0.0
/// **Mic Blend** in `[0, 1]` between mic A (`0`) and mic B (`1`); `0.5` = equal. Only active when
/// ``micB`` ≠ No Mic. A live blend, so it is a smooth knob. Surfaced in the advanced pop-over.
@UIParam public var micBlend: Double = 0.5
/// **Mic Offset** in `[0, 1]`: the relative time offset of mic B, `0` = aligned, `1` = the maximum
/// spacing. A short live delay on mic B; summing it with the aligned mic A produces the comb
/// filtering of two spaced mics. Only active when ``micB`` ≠ No Mic. Surfaced in the pop-over.
///
/// > Note: intended as a **set-and-leave** spacing control (dial in the comb, then play). It is a
/// > live delay, so *sweeping* it during playback moves the comb — a flanger-like effect rather
/// > than a static mic position.
@UIParam public var micOffset: Double = 0.0
// MARK: - Constants // MARK: - Constants
@@ -91,8 +112,12 @@ public final class CabinetPedal: Pedal {
private let proximityMaxBoostDb: Double = 6.0 private let proximityMaxBoostDb: Double = 6.0
/// Shelf slope for the position / proximity live shelves (gentle, like the amp shelves). /// Shelf slope for the position / proximity live shelves (gentle, like the amp shelves).
private let placementShelfSlope: Double = 0.7 private let placementShelfSlope: Double = 0.7
/// `[0, 1]` range for the Position / Proximity pop-over knobs. /// `[0, 1]` range for the Position / Proximity / Mic Blend / Mic Offset pop-over knobs.
private static let placementRange: ClosedRange<Double> = 0.0 ... 1.0 private static let placementRange: ClosedRange<Double> = 0.0 ... 1.0
/// Maximum mic-B time offset (seconds) at `micOffset = 1` — a spaced-mic offset (~0.5 m of path
/// difference). Sets both the delay-line size and the comb range: the first notch is at
/// `1 / (2·offset)` Hz, so full offset places it near 330 Hz (`micOffset = 0` = aligned, no comb).
private let maxMicOffsetSeconds: Double = 0.0015
/// Selector stops, one per ``CabinetVoice`` in `allCases` order (value == index). /// Selector stops, one per ``CabinetVoice`` in `allCases` order (value == index).
private static let voiceStops: [PedalSelectorKnob.Stop] = CabinetVoice.allCases private static let voiceStops: [PedalSelectorKnob.Stop] = CabinetVoice.allCases
@@ -135,16 +160,22 @@ public final class CabinetPedal: Pedal {
label: "Cab", node: $cabinet, stops: CabinetPedal.voiceStops, rebuildsOnChange: true), label: "Cab", node: $cabinet, stops: CabinetPedal.voiceStops, rebuildsOnChange: true),
PedalSelectorKnob( PedalSelectorKnob(
label: "Mic", node: $mic, stops: CabinetPedal.micStops, rebuildsOnChange: true), label: "Mic", node: $mic, stops: CabinetPedal.micStops, rebuildsOnChange: true),
PedalSelectorKnob(
label: "Mic B", node: $micB, stops: CabinetPedal.micStops, rebuildsOnChange: true),
] ]
} }
/// Mic-placement deep-cut controls, tucked behind the pedal card's advanced pop-over: **Position** /// Mic-placement + dual-mic deep-cut controls, tucked behind the pedal card's advanced pop-over:
/// (on-axis ↔ cone edge) and **Proximity** (distance / bass lift). Live shelves (not baked), so /// **Position** (on-axis ↔ cone edge), **Proximity** (distance / bass lift), and — when a second
/// they are smooth knobs; neither is a topology control. Persisted positionally in `advancedValues`. /// mic is engaged — **Mic Blend** (A/B) and **Mic Offset** (comb spacing). All live (not baked),
/// so they are smooth knobs; none is a topology control. Persisted positionally in
/// `advancedValues` (append-only order).
public override var advancedKnobs: [PedalKnob] { public override var advancedKnobs: [PedalKnob] {
[ [
PedalKnob(label: "Position", node: $position, range: CabinetPedal.placementRange), PedalKnob(label: "Position", node: $position, range: CabinetPedal.placementRange),
PedalKnob(label: "Proximity", node: $proximity, range: CabinetPedal.placementRange), PedalKnob(label: "Proximity", node: $proximity, range: CabinetPedal.placementRange),
PedalKnob(label: "Mic Blend", node: $micBlend, range: CabinetPedal.placementRange),
PedalKnob(label: "Mic Offset", node: $micOffset, range: CabinetPedal.placementRange),
] ]
} }
@@ -179,7 +210,8 @@ public final class CabinetPedal: Pedal {
ir: CabinetIRFactory.makeIR(voice: voice, mic: micVoice, sampleRate: sr), ir: CabinetIRFactory.makeIR(voice: voice, mic: micVoice, sampleRate: sr),
mix: convolutionMix mix: convolutionMix
) )
let placed: any ValidGeneratorInput = applyPlacement(convolved) let mixed: any ValidGeneratorInput = blendSecondMic(dry: dry, primary: convolved, voice: voice, sr: sr)
let placed: any ValidGeneratorInput = applyPlacement(mixed)
return LookAheadLimiter( return LookAheadLimiter(
in: placed, in: placed,
maxAmplitude: outputCeiling, maxAmplitude: outputCeiling,
@@ -189,13 +221,47 @@ public final class CabinetPedal: Pedal {
) )
} }
/// Blends a second microphone into the primary convolution when ``micB`` ≠ No Mic — the classic
/// two-mic technique. A parallel convolver (same cab voice, mic B's IR) is delayed by up to
/// ``maxMicOffsetSeconds`` (``micOffset``) and summed with the aligned primary at the ``micBlend``
/// balance; the time offset between the two coherent copies produces comb filtering. When ``micB``
/// is No Mic (the default) the primary is returned unchanged — no second convolver is built, so
/// the single-mic path costs exactly one convolution and is bit-identical to before this feature.
///
/// - Parameters:
/// - dry: The dry input (fed to the second convolver).
/// - primary: The primary (mic A) convolution output.
/// - voice: The cabinet voice (shared by both mics).
/// - sr: Engine sample rate.
/// - Returns: The single- or dual-mic blended signal.
private func blendSecondMic(
dry: any ValidGeneratorInput,
primary: any ValidGeneratorInput,
voice: CabinetVoice,
sr: Double
) -> any ValidGeneratorInput {
let micBVoice: MicVoice = MicVoice.voice(atSelectorValue: micB)
guard micBVoice != .off else { return primary }
let secondary: ConvolutionReverb.Audio = ConvolutionReverb.Audio(
input: dry,
ir: CabinetIRFactory.makeIR(voice: voice, mic: micBVoice, sampleRate: sr),
mix: convolutionMix
)
let delaySeconds: LinLin.Audio = LinLin.Audio(
$micOffset, inLo: 0.0, inHi: 1.0, outLo: 0.0, outHi: maxMicOffsetSeconds)
let delayedSecondary: DelayLine = DelayLine(
input: secondary, maxDelay: maxMicOffsetSeconds, delay: delaySeconds, sampleRate: sr)
// Coherent (same-source) mics sum linearly: A·(1−blend) + Bdelayed·blend.
return primary * (1.0 - $micBlend) + delayedSecondary * $micBlend
}
/// Applies the live mic-placement shelves to the convolved signal: a **Position** high-shelf that /// 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** /// 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 /// 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 /// 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. /// identity and the cabinet sounds exactly as it did before placement existed.
/// ///
/// - Parameter signal: The cab-convolved signal. /// - Parameter signal: The (single- or dual-mic) convolved signal.
/// - Returns: The placement-shaped signal. /// - Returns: The placement-shaped signal.
private func applyPlacement(_ signal: any ValidGeneratorInput) -> any ValidGeneratorInput { private func applyPlacement(_ signal: any ValidGeneratorInput) -> any ValidGeneratorInput {
let positionCutDb: LinLin.Audio = LinLin.Audio( let positionCutDb: LinLin.Audio = LinLin.Audio(
@@ -62,12 +62,14 @@ struct CabinetPedalTests {
@Test func exposesCabinetAndMicSelectors() throws { @Test func exposesCabinetAndMicSelectors() throws {
let pedal: CabinetPedal = CabinetPedal() let pedal: CabinetPedal = CabinetPedal()
let labels: [String] = pedal.effectSelectors.map { $0.label } let labels: [String] = pedal.effectSelectors.map { $0.label }
#expect(labels == ["Cab", "Mic"]) #expect(labels == ["Cab", "Mic", "Mic B"])
// One Cab stop per voice, one Mic stop per mic voice, both in allCases order. // One Cab stop per voice, and one stop per mic voice for both Mic and Mic B, in allCases order.
let cabStops: [String] = pedal.effectSelectors.first { $0.label == "Cab" }?.stops.map { $0.title } ?? [] let cabStops: [String] = pedal.effectSelectors.first { $0.label == "Cab" }?.stops.map { $0.title } ?? []
#expect(cabStops == CabinetVoice.allCases.map { $0.displayName }) #expect(cabStops == CabinetVoice.allCases.map { $0.displayName })
let micStops: [String] = pedal.effectSelectors.first { $0.label == "Mic" }?.stops.map { $0.title } ?? [] let micStops: [String] = pedal.effectSelectors.first { $0.label == "Mic" }?.stops.map { $0.title } ?? []
#expect(micStops == MicVoice.allCases.map { $0.displayName }) #expect(micStops == MicVoice.allCases.map { $0.displayName })
let micBStops: [String] = pedal.effectSelectors.first { $0.label == "Mic B" }?.stops.map { $0.title } ?? []
#expect(micBStops == MicVoice.allCases.map { $0.displayName })
} }
@Test func micSelectionChangesOutput() throws { @Test func micSelectionChangesOutput() throws {
@@ -134,9 +136,75 @@ struct CabinetPedalTests {
#expect(ssd == 0, "explicit default placement did not match the default render") #expect(ssd == 0, "explicit default placement did not match the default render")
} }
@Test func advancedKnobsExposePlacement() throws { @Test func advancedKnobsExposePlacementAndDualMic() throws {
let pedal: CabinetPedal = CabinetPedal() let pedal: CabinetPedal = CabinetPedal()
#expect(pedal.advancedKnobs.map { $0.label } == ["Position", "Proximity"]) #expect(pedal.advancedKnobs.map { $0.label } == ["Position", "Proximity", "Mic Blend", "Mic Offset"])
}
// MARK: - Dual mic
@Test func secondMicChangesOutput() throws {
// Engaging Mic B (a second parallel convolver) must change the output vs the single mic.
let single: [Float] = try render { $0.micB = 0.0 }
let dual: [Float] = try render { $0.micB = 2.0 } // Ribbon as the second mic
let skip: Int = 4096, window: Int = 4096
#expect(single.count >= skip + window && dual.count >= skip + window, "render too short")
guard single.count >= skip + window, dual.count >= skip + window else { return }
let ssd: Double = zip(single[skip..<(skip + window)], dual[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
#expect(ssd > 0, "engaging Mic B had no effect")
}
@Test func micBOffMakesBlendAndOffsetInert() throws {
// With Mic B = No Mic, the second convolver is not built, so Mic Blend / Mic Offset must have
// no effect — proving the single-mic default path is untouched by the dual-mic controls.
let a: [Float] = try render { $0.micB = 0.0; $0.micBlend = 0.0; $0.micOffset = 0.0 }
let b: [Float] = try render { $0.micB = 0.0; $0.micBlend = 1.0; $0.micOffset = 1.0 }
let skip: Int = 4096, window: Int = 4096
#expect(a.count >= skip + window && b.count >= skip + window, "render too short")
guard a.count >= skip + window, b.count >= skip + window else { return }
let ssd: Double = zip(a[skip..<(skip + window)], b[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
#expect(ssd == 0, "Mic Blend/Offset changed output while Mic B was Off")
}
@Test func micBlendAndOffsetChangeOutputWhenEngaged() throws {
// With Mic B engaged, both the blend balance and the comb offset must change the output.
let blendA: [Float] = try render { $0.micB = 2.0; $0.micBlend = 0.0 }
let blendB: [Float] = try render { $0.micB = 2.0; $0.micBlend = 1.0 }
let offset0: [Float] = try render { $0.micB = 2.0; $0.micBlend = 0.5; $0.micOffset = 0.0 }
let offset1: [Float] = try render { $0.micB = 2.0; $0.micBlend = 0.5; $0.micOffset = 1.0 }
let skip: Int = 4096, window: Int = 4096
func ssd(_ x: [Float], _ y: [Float]) -> Double {
guard x.count >= skip + window, y.count >= skip + window else { return 0.0 }
return zip(x[skip..<(skip + window)], y[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
}
#expect(ssd(blendA, blendB) > 0, "Mic Blend had no effect when engaged")
#expect(ssd(offset0, offset1) > 0, "Mic Offset (comb) had no effect when engaged")
}
@Test func dualMicRoundTripsThroughState() throws {
let pedal: CabinetPedal = CabinetPedal()
pedal.micB = 3.0
pedal.micBlend = 0.3
pedal.micOffset = 0.6
let restored: CabinetPedal = CabinetPedal()
restored.applyState(pedal.captureState())
#expect(restored.micB == 3.0 && restored.micBlend == 0.3 && restored.micOffset == 0.6,
"dual-mic controls did not round-trip")
}
@Test func dualMicStaysWithinCeiling() throws {
// Two convolvers summed can exceed ±1 pre-limiter; the look-ahead limiter must still bound it.
let ceilingEpsilon: Double = 1.0e-4
let out: [Float] = try render { $0.micB = 1.0; $0.micBlend = 0.5; $0.micOffset = 0.5 }
#expect(out.allSatisfy { $0.isFinite })
let peak: Double = out.reduce(0.0) { Swift.max($0, abs(Double($1))) }
#expect(peak <= 1.0 + ceilingEpsilon, "dual-mic peak \(peak) exceeds ±1")
} }
@Test func placementRoundTripsThroughState() throws { @Test func placementRoundTripsThroughState() throws {