feat(amps): master-volume Marshalls — British Master / Dual (Phase 5)

Add a shared AmpVoicePedal.masterDrive(_:minDrive:maxDrive:) helper (Master knob
[0,1] → power-amp drive range) and route British's powerAmpDriveInput through it.
Two new BritishComboAmpPedal subclasses (Presence + Bright inherited):

- British Master — 800-series single-channel lead: preampStage cascades a second
  gain stage (both driven by Gain) + Master + 4× power-amp oversample.
- British Dual — 900/DSL: rebuildsOnChange Clean/Lead Channel switch (Lead
  cascades a stage set by Drive), Master, spring reverb; widthUnits 4.

Registered across 6 sites as .amp. 11 tests (Master drives power amp via
RMS-normalized SSD, cascaded-vs-Plexi, channel switch + rebuildsOnChange, drive/
reverb, ceiling on lead channel at max, control surfaces, round-trips, registry).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-08-03 13:46:08 -07:00
parent a7534a29db
commit cda596b9d5
10 changed files with 347 additions and 3 deletions
@@ -0,0 +1,169 @@
// BritishMasterAmpPedalTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
import LyrebirdUI
@testable import LyrebirdShoeGaze
@MainActor
@Suite("British master-volume amps")
struct BritishMasterAmpPedalTests {
private let sampleRate: Double = 48000.0
private func render<P: AmpVoicePedal>(_ make: () -> P, configure: (P) -> Void = { _ in }) throws -> [Float] {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: 0.3, sampleRate: sampleRate, numAudioBuses: 64)
defer { try? FileManager.default.removeItem(at: url) }
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
defer {
engine.busAllocator.releaseAudioBus(srcBus)
engine.busAllocator.releaseAudioBus(dstBus)
}
let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index)
let pedal: P = make()
pedal.inputBus = srcBus.index
pedal.outputBus = dstBus.index
pedal.sampleRate = sampleRate
configure(pedal)
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
try engine.add(source)
try engine.add(pedal, position: NodePosition.after(source))
try engine.add(master, position: NodePosition.after(pedal))
try engine.start()
return readStereoCAF(url: url, sampleRate: sampleRate)
}
private func windowedSSD(_ a: [Float], _ b: [Float], skip: Int = 4096, window: Int = 4096) -> Double {
guard a.count >= skip + window, b.count >= skip + window else { return 0.0 }
return 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
}
}
/// RMS-normalized SSD — isolates a waveshape (saturation) change from a pure level change.
private func rmsNormalizedSSD(_ a: [Float], _ b: [Float], skip: Int = 4096, window: Int = 4096) -> Double {
guard a.count >= skip + window, b.count >= skip + window else { return 0.0 }
let aw: ArraySlice<Float> = a[skip..<(skip + window)]
let bw: ArraySlice<Float> = b[skip..<(skip + window)]
func rms(_ x: ArraySlice<Float>) -> Double {
let sumSq: Double = x.reduce(0.0) { $0 + Double($1) * Double($1) }
return (sumSq / Double(x.count)).squareRoot()
}
let ra: Double = rms(aw)
let rb: Double = rms(bw)
guard ra > 0.0, rb > 0.0 else { return 0.0 }
return zip(aw, bw).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0) / ra - Double(pair.1) / rb
return acc + d * d
}
}
private func peak(_ s: [Float]) -> Double { s.reduce(0.0) { Swift.max($0, abs(Double($1))) } }
// MARK: - British Master
@Test func masterBuildsAndDrivesPowerAmp() throws {
let samples: [Float] = try render({ BritishMasterAmpPedal() })
#expect(hasNonSilentOutput(samples) && samples.allSatisfy { $0.isFinite })
// Master drives the power-amp stage → a waveshape change, not just level.
let low: [Float] = try render({ BritishMasterAmpPedal() }) { $0.gain = 10.0; $0.master = 0.1 }
let high: [Float] = try render({ BritishMasterAmpPedal() }) { $0.gain = 10.0; $0.master = 1.0 }
#expect(rmsNormalizedSSD(low, high) > 1.0e-3, "Master changed level but not tone")
}
@Test func masterHasCascadedGainVsPlexi() throws {
// The cascaded two-stage preamp makes British Master hotter/different from the single-stage
// non-master Plexi at the same Gain.
let masterAmp: [Float] = try render({ BritishMasterAmpPedal() }) { $0.gain = 6.0 }
let plexi: [Float] = try render({ BritishPlexiAmpPedal() }) { $0.gain = 6.0 }
#expect(windowedSSD(masterAmp, plexi) > 0, "cascaded lead should differ from the Plexi")
}
@Test func masterExposesControlSurface() throws {
let pedal: BritishMasterAmpPedal = BritishMasterAmpPedal()
#expect(pedal.effectKnobs.map { $0.label } == ["Gain", "Bass", "Mid", "Treble", "Presence", "Master"])
#expect(pedal.effectSwitches.map { $0.label } == ["Bright"])
#expect(pedal.widthUnits == 3)
#expect(pedal.toneStackModel == .marshall)
#expect(pedal.powerAmpOversample == 4)
}
@Test func masterStaysWithinCeiling() throws {
let hot: [Float] = try render({ BritishMasterAmpPedal() }) { (p: BritishMasterAmpPedal) in
p.gain = 30.0; p.bass = 1.0; p.treble = 1.0; p.presence = 1.0; p.bright = 1.0; p.master = 1.0
}
#expect(peak(hot) <= 1.0 + 1.0e-4, "British Master exceeds the ±1 ceiling")
}
@Test func masterRoundTrips() throws {
let pedal: BritishMasterAmpPedal = BritishMasterAmpPedal()
pedal.master = 0.8; pedal.presence = 0.7; pedal.bright = 1.0; pedal.cab = 2.0
let restored: BritishMasterAmpPedal = BritishMasterAmpPedal()
restored.applyState(pedal.captureState())
#expect(restored.master == 0.8 && restored.presence == 0.7 && restored.bright == 1.0 && restored.cab == 2.0)
}
// MARK: - British Dual
@Test func dualChannelSwitchChangesOutput() throws {
let clean: [Float] = try render({ BritishDualAmpPedal() }) { $0.channel = 0.0 }
let lead: [Float] = try render({ BritishDualAmpPedal() }) { $0.channel = 1.0; $0.drive = 15.0 }
#expect(hasNonSilentOutput(clean) && hasNonSilentOutput(lead))
#expect(windowedSSD(clean, lead) > 0, "channel switch had no effect")
let channel: PedalSwitch? = BritishDualAmpPedal().effectSwitches.first { $0.label == "Channel" }
#expect(channel?.rebuildsOnChange == true, "Channel switch should rebuild on change")
}
@Test func dualDriveAndReverbAndMasterWork() throws {
let low: [Float] = try render({ BritishDualAmpPedal() }) { $0.channel = 1.0; $0.drive = 2.0 }
let high: [Float] = try render({ BritishDualAmpPedal() }) { $0.channel = 1.0; $0.drive = 25.0 }
#expect(windowedSSD(low, high) > 0, "Drive had no effect on the lead channel")
let dry: [Float] = try render({ BritishDualAmpPedal() }) { $0.reverb = 0.0 }
let wet: [Float] = try render({ BritishDualAmpPedal() }) { $0.reverb = 0.9 }
#expect(windowedSSD(dry, wet) > 0, "reverb had no effect")
let mLow: [Float] = try render({ BritishDualAmpPedal() }) { $0.gain = 10.0; $0.master = 0.1 }
let mHigh: [Float] = try render({ BritishDualAmpPedal() }) { $0.gain = 10.0; $0.master = 1.0 }
#expect(rmsNormalizedSSD(mLow, mHigh) > 1.0e-3, "Master changed level but not tone")
}
@Test func dualExposesControlSurface() throws {
let pedal: BritishDualAmpPedal = BritishDualAmpPedal()
#expect(pedal.effectKnobs.map { $0.label }
== ["Gain", "Bass", "Mid", "Treble", "Presence", "Master", "Drive", "Reverb"])
#expect(pedal.effectSwitches.map { $0.label } == ["Bright", "Channel"])
#expect(pedal.widthUnits == 4)
#expect(pedal.toneStackModel == .marshall)
}
@Test func dualStaysWithinCeilingOnLeadChannel() throws {
let hot: [Float] = try render({ BritishDualAmpPedal() }) { (p: BritishDualAmpPedal) in
p.gain = 30.0; p.bass = 1.0; p.treble = 1.0; p.presence = 1.0; p.bright = 1.0
p.master = 1.0; p.channel = 1.0; p.drive = 30.0; p.reverb = 1.0
}
#expect(peak(hot) <= 1.0 + 1.0e-4, "British Dual exceeds the ±1 ceiling on the lead channel")
}
@Test func dualRoundTrips() throws {
let pedal: BritishDualAmpPedal = BritishDualAmpPedal()
pedal.master = 0.7; pedal.drive = 12.0; pedal.reverb = 0.5; pedal.channel = 1.0
pedal.presence = 0.6; pedal.bright = 1.0; pedal.cab = 2.0
let restored: BritishDualAmpPedal = BritishDualAmpPedal()
restored.applyState(pedal.captureState())
#expect(restored.master == 0.7 && restored.drive == 12.0 && restored.reverb == 0.5)
#expect(restored.channel == 1.0 && restored.presence == 0.6 && restored.bright == 1.0 && restored.cab == 2.0)
}
@Test func registeredInBuiltInRegistry() throws {
#expect(PedalRegistry.makePedal(type: "British Master") is BritishMasterAmpPedal)
#expect(PedalRegistry.makePedal(type: "British Dual") is BritishDualAmpPedal)
}
}