0f6d69819d
Remove Neural Amp from PedalRegistry.builtIn and PedalPaletteView.preferredOrder so it can't be added — the model loader's security-scoped file access is unresolved (fails silently under sandboxing; flagged in external review). NeuralAmpPedal, its neuralModelRow UI, PedalType/category/color mappings, and tests are all kept for later iteration — re-add the two entries to re-enable. Help acknowledgment for the GuitarML loader dropped (feature not user-reachable); registry test now asserts it is not registered. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
123 lines
5.5 KiB
Swift
123 lines
5.5 KiB
Swift
// NeuralAmpPedalTests.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("NeuralAmpPedal")
|
|
struct NeuralAmpPedalTests {
|
|
|
|
private let sampleRate: Double = 48000.0
|
|
|
|
/// Writes a small, deterministic Proteus/NeuralPi-format model JSON to a temp file and returns
|
|
/// its path. Weights are arbitrary-but-fixed (a real capture is not needed to exercise the
|
|
/// load → import → LSTMAmp path).
|
|
private func writeTempModel(hidden h: Int) throws -> String {
|
|
let gated: Int = 4 * h
|
|
let weightIH: [[Double]] = (0..<gated).map { [0.1 * sin(Double($0) + 1.0)] }
|
|
let weightHH: [[Double]] = (0..<gated).map { r in (0..<h).map { 0.05 * cos(Double(r * h + $0) + 1.0) } }
|
|
let biasIH: [Double] = (0..<gated).map { 0.01 * Double($0) }
|
|
let biasHH: [Double] = [Double](repeating: 0.0, count: gated)
|
|
let linWeight: [[Double]] = [(0..<h).map { 0.3 * sin(Double($0) + 0.5) }]
|
|
let object: [String: Any] = [
|
|
"model_data": [
|
|
"unit_type": "LSTM", "input_size": 1, "hidden_size": h,
|
|
"num_layers": 1, "output_size": 1, "skip": 1,
|
|
],
|
|
"state_dict": [
|
|
"rec.weight_ih_l0": weightIH,
|
|
"rec.weight_hh_l0": weightHH,
|
|
"rec.bias_ih_l0": biasIH,
|
|
"rec.bias_hh_l0": biasHH,
|
|
"lin.weight": linWeight,
|
|
"lin.bias": [0.05],
|
|
],
|
|
]
|
|
let data: Data = try JSONSerialization.data(withJSONObject: object)
|
|
let url: URL = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("neuralamp-test-\(h)-\(UUID().uuidString).json")
|
|
try data.write(to: url)
|
|
return url.path
|
|
}
|
|
|
|
private func render(configure: (NeuralAmpPedal) -> Void = { _ in }) throws -> [Float] {
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
|
|
duration: 0.15, 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: NeuralAmpPedal = NeuralAmpPedal(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 = 2048, window: Int = 2048) -> 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 noModelPassesThroughNonSilent() throws {
|
|
let out: [Float] = try render()
|
|
#expect(hasNonSilentOutput(out), "no-model passthrough was silent")
|
|
#expect(out.allSatisfy { $0.isFinite })
|
|
}
|
|
|
|
@Test func loadedModelTransformsAndDiffersFromPassthrough() throws {
|
|
let path: String = try writeTempModel(hidden: 8)
|
|
defer { try? FileManager.default.removeItem(atPath: path) }
|
|
let passthrough: [Float] = try render()
|
|
let modeled: [Float] = try render { $0.resourcePath = path }
|
|
#expect(hasNonSilentOutput(modeled), "modeled output was silent")
|
|
#expect(modeled.allSatisfy { $0.isFinite })
|
|
#expect(windowedSSD(passthrough, modeled) > 0, "loaded model did not change the output")
|
|
}
|
|
|
|
@Test func gainChangesModeledOutput() throws {
|
|
let path: String = try writeTempModel(hidden: 8)
|
|
defer { try? FileManager.default.removeItem(atPath: path) }
|
|
let low: [Float] = try render { $0.resourcePath = path; $0.gain = 0.5 }
|
|
let high: [Float] = try render { $0.resourcePath = path; $0.gain = 3.0 }
|
|
#expect(windowedSSD(low, high) > 0, "gain had no effect on modeled output")
|
|
}
|
|
|
|
@Test func resourcePathRoundTripsThroughState() throws {
|
|
let pedal: NeuralAmpPedal = NeuralAmpPedal()
|
|
pedal.resourcePath = "/some/model.json"
|
|
let restored: NeuralAmpPedal = NeuralAmpPedal()
|
|
restored.applyState(pedal.captureState())
|
|
#expect(restored.resourcePath == "/some/model.json", "resourcePath did not round-trip")
|
|
}
|
|
|
|
@Test func exposesGainKnob() throws {
|
|
let pedal: NeuralAmpPedal = NeuralAmpPedal()
|
|
#expect(pedal.effectKnobs.map { $0.label } == ["Gain"])
|
|
}
|
|
|
|
@Test func notRegisteredWhileHidden() throws {
|
|
// Neural Amp is intentionally not in the palette/registry for now (unresolved security-scoped
|
|
// model-file access); the class + UI are kept for later iteration. Re-add to
|
|
// PedalRegistry.builtIn + PedalPaletteView.preferredOrder to re-enable.
|
|
#expect(PedalRegistry.makePedal(type: "Neural Amp") == nil)
|
|
}
|
|
}
|