Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/BritishMasterAmpPedalTests.swift
T
josh f9ddd6b160 feat(amp): Gain/Drive become a 1–11 dial with a musical taper
Every amp's Gain (and the boutique/drive-channel Drive) is now a 1–11 dial —
yes, it goes to eleven — instead of the raw 1–30 tube-drive number.

- New AmpVoicePedal.taperedDrive(_:) maps the 1–11 dial to the 1–30 tube drive
  through an exponential LinExp taper, so 1 is nearly clean, 11 is full
  saturation, and the useful grind spreads across the dial (fine at the bottom,
  steep at the top) rather than bunching near the low end.
- Wired into the base preampStage and every override that feeds a drive:
  Boutique / Fullerton Drive / Jazz Clean / British Dual (Drive channel),
  Anaheim (SS preamp), British Master (cascaded stage).
- gainRange / driveRange → 1...11; every amp's default gain/drive reconverted
  from drive-space to the equivalent dial value (dial = 1 + 10·ln(drive)/ln(30)),
  so each amp's shipped voicing is preserved.
- No preset migration (nothing shipped — per product owner); all knob work
  stays within ShoeGaze (no core Lyrebird change).
- Tests updated to the 1–11 scale; added range + exponential-taper assertions.

671 tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-04 10:42:35 -07:00

171 lines
8.4 KiB
Swift

// 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 = 11.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 = 9.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 = 10.5 }
#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)
#expect(pedal.powerAmpOversample == 4)
}
@Test func dualStaysWithinCeilingOnLeadChannel() throws {
let hot: [Float] = try render({ BritishDualAmpPedal() }) { (p: BritishDualAmpPedal) in
p.gain = 11.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 = 11.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 = 8.3; 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 == 8.3 && 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)
}
}