Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/ModernHighGainAmpPedalTests.swift
T
josh 781577b062 fix(amp): preserve legacy rigs across the Sag/Bias rename + move
Addresses two blocking findings from the external review, both legacy
preset-compat (no RT/protocol issues were found):

- Rename compat: "Fullerton Princeton" → display "Fullerton Ivy League"
  would silently drop the pedal from any rig saved under the old name
  (displayName is the serialization key). Add PedalRegistry.legacyTypeAliases
  resolved in makePedal (self-healing: loads, then re-saves under the new
  name). Regression test asserts the old name still resolves.
- Knob-shift compat: moving Modern High-Gain's Sag off the face shrank its
  effectKnobs by one, so an old 11-value knobValues array would misassign
  Sag→Mix and Mix→Level on load. Add a targeted PedalState migration
  (Pedal.legacyMigrated) that lifts Sag into advancedValues and keeps
  Mix/Level in place. Regression test decodes a hand-built legacy preset
  and asserts correct placement.

Also: drop the not-yet-built "Bias X" from a doc comment; de-brand "Vox"
from the Chime variants test names (ToneStackModel .vox enum refs kept).

643 tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-03 21:20:16 -07:00

147 lines
7.3 KiB
Swift

// ModernHighGainAmpPedalTests.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("ModernHighGainAmpPedal")
struct ModernHighGainAmpPedalTests {
private let sampleRate: Double = 48000.0
private func render(configure: (ModernHighGainAmpPedal) -> 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: ModernHighGainAmpPedal = ModernHighGainAmpPedal(inputBus: srcBus.index, 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
}
}
@Test func buildsNonSilentFiniteWithinCeiling() throws {
let hot: [Float] = try render { (p: ModernHighGainAmpPedal) in
p.gain = 30.0; p.bass = 1.0; p.treble = 1.0; p.master = 1.0; p.channel = 1.0
p.drive = 30.0; p.sag = 1.0; p.tight = 1.0
}
#expect(hasNonSilentOutput(hot) && hot.allSatisfy { $0.isFinite })
let peak: Double = hot.reduce(0.0) { Swift.max($0, abs(Double($1))) }
#expect(peak <= 1.0 + 1.0e-4, "Modern High-Gain exceeds the ±1 ceiling: \(peak)")
}
@Test func sagChangesOutput() throws {
let stiff: [Float] = try render { $0.gain = 20.0; $0.channel = 1.0; $0.sag = 0.0 }
let saggy: [Float] = try render { $0.gain = 20.0; $0.channel = 1.0; $0.sag = 1.0 }
#expect(windowedSSD(stiff, saggy) > 0, "Sag had no effect")
}
@Test func sagAttenuatesWithoutPhaseInversion() throws {
// With Presence/Depth maxed the shelf boosts push the sag envelope above 1; the sag gain
// must stay positive (attenuation), never go negative (phase inversion). Assert the sag'd
// output correlates positively with the stiff one (not an inverted copy).
let cfg: (ModernHighGainAmpPedal) -> Void = { (p: ModernHighGainAmpPedal) in
p.gain = 20.0; p.channel = 1.0; p.presence = 1.0; p.depth = 1.0
}
let stiff: [Float] = try render { cfg($0); $0.sag = 0.0 }
let saggy: [Float] = try render { cfg($0); $0.sag = 1.0 }
let skip: Int = 4096, window: Int = 4096
guard stiff.count >= skip + window, saggy.count >= skip + window else {
Issue.record("render too short"); return
}
// Unnormalized dot product — its sign is what matters: negative ⇒ phase-inverted copy.
let dotProduct: Double = zip(stiff[skip..<(skip + window)], saggy[skip..<(skip + window)])
.reduce(0.0) { acc, pair in acc + Double(pair.0) * Double(pair.1) }
#expect(dotProduct > 0, "sag phase-inverted the signal (dot product \(dotProduct) < 0)")
}
@Test func tightSwitchChangesOutput() throws {
let loose: [Float] = try render { $0.tight = 0.0 }
let tight: [Float] = try render { $0.tight = 1.0 }
#expect(windowedSSD(loose, tight) > 0, "Tight switch had no effect")
}
@Test func channelSwitchInherited() throws {
let clean: [Float] = try render { $0.channel = 0.0 }
let lead: [Float] = try render { $0.channel = 1.0 }
#expect(windowedSSD(clean, lead) > 0, "channel switch had no effect")
let channel: PedalSwitch? = ModernHighGainAmpPedal().effectSwitches.first { $0.label == "Channel" }
#expect(channel?.rebuildsOnChange == true)
}
@Test func exposesControlSurface() throws {
let pedal: ModernHighGainAmpPedal = ModernHighGainAmpPedal()
// Sag now lives in the shared advanced pop-over (with Bias), not on the pedal face.
#expect(pedal.effectKnobs.map { $0.label }
== ["Gain", "Bass", "Mid", "Treble", "Presence", "Depth", "Master", "Drive"])
#expect(pedal.advancedKnobs.map { $0.label } == ["Sag", "Bias"])
#expect(pedal.effectSwitches.map { $0.label } == ["Channel", "Tight"])
#expect(pedal.widthUnits == 4)
#expect(pedal.toneStackModel == .mesa)
#expect(pedal.sag == 0.3, "Modern High-Gain keeps rectifier sag on by default")
}
@Test func controlsRoundTripThroughState() throws {
let pedal: ModernHighGainAmpPedal = ModernHighGainAmpPedal()
pedal.sag = 0.8; pedal.tight = 1.0; pedal.channel = 1.0
pedal.drive = 20.0; pedal.master = 0.7; pedal.cab = 2.0
let restored: ModernHighGainAmpPedal = ModernHighGainAmpPedal()
restored.applyState(pedal.captureState())
#expect(restored.sag == 0.8 && restored.tight == 1.0 && restored.channel == 1.0)
#expect(restored.drive == 20.0 && restored.master == 0.7 && restored.cab == 2.0)
}
@Test func registeredInBuiltInRegistry() throws {
#expect(PedalRegistry.makePedal(type: "Modern High-Gain") is ModernHighGainAmpPedal)
}
@Test func legacyPresetWithSagOnFaceMigratesWithoutShiftingMixLevel() throws {
// A rig saved when Sag was the 9th face knob has 11 knobValues (Gain…Drive, Sag, Mix, Level)
// and no advancedValues. Sag has since moved to the advanced pop-over, so the current shape
// has 10 knobValues (Gain…Drive, Mix, Level) + 2 advancedValues (Sag, Bias). Loading the
// legacy preset must lift Sag into advancedValues and keep Mix/Level in place — never shift
// the old Sag onto Mix or the old Mix onto Level.
let legacy: PedalState = PedalState(
id: UUID(),
type: "Modern High-Gain",
knobValues: [10.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 15.0, 0.7, 0.8, 0.9],
bypass: 0.0
)
let pedal: ModernHighGainAmpPedal = ModernHighGainAmpPedal()
pedal.applyState(legacy)
let restored: PedalState = pedal.captureState()
// Gain…Drive, then Mix (0.8), Level (0.9) — the old Sag (0.7) is gone from knobValues.
#expect(restored.knobValues == [10.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 15.0, 0.8, 0.9],
"legacy Sag shifted Mix/Level: \(restored.knobValues)")
// Sag lifted into advanced slot 0; Bias had no legacy value → default 0.5.
#expect(restored.advancedValues == [0.7, 0.5], "Sag/Bias advanced values wrong: \(restored.advancedValues)")
}
}