feat(pedals): Neural Amp pedal (S3) + PedalState resource path; Lyrebird 1.7.1
Bump Lyrebird to 1.7.1 (ProteusModelImporter) and add a neural amp-capture pedal. - NeuralAmpPedal: loads a GuitarML Proteus/NeuralPi model (via ProteusModelImporter) and runs it with LSTMAmp on the gain-trimmed input; passes through clean when no model is loaded. Model files are user-supplied, not bundled (GuitarML = GPL-3.0). - PedalState gains an optional `resourcePath` (backward-compatible, decodeIfPresent) and Pedal a matching `resourcePath` property, wired through captureState/applyState — so the loaded model file persists across rig save/load. The neural pedal reads it at build() (applied on rebuild). This foundation also unblocks a future custom-cabinet-IR path. - Registered in PedalRegistry / PedalType / PedalCategory (.gain) / palette / enclosure color; Help acknowledges the GuitarML model format. Tests: no-model passthrough; loaded model transforms output & differs from passthrough; gain affects modeled output; resourcePath round-trips through PedalState; gain knob exposed; registry resolves "Neural Amp". Full suite 501. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
// 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 registeredInBuiltInRegistry() throws {
|
||||
let pedal: Pedal? = PedalRegistry.makePedal(type: "Neural Amp")
|
||||
#expect(pedal is NeuralAmpPedal)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user