diff --git a/Package.resolved b/Package.resolved index b185d36..fb4d3a9 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "7d48b3cb04ded2905154feb906405cef4e190bcef981bdf1cacf3b92271d0960", + "originHash" : "bc2a61a4b15bd882ede79ecb295b7878eca7ce7b55f4832eaf9bd7e7f557dd4f", "pins" : [ { "identity" : "linenoise-swift", @@ -15,8 +15,8 @@ "kind" : "remoteSourceControl", "location" : "https://realizedsound.mooo.com/josh/Lyrebird", "state" : { - "revision" : "3e33f09e0719dad95750de7c2b604ae97a826584", - "version" : "1.7.0" + "revision" : "ea0aa84ca796d9a21667e4b5a7d76a1ac2c280b6", + "version" : "1.7.1" } }, { diff --git a/Package.swift b/Package.swift index ca55a87..d94b1b5 100644 --- a/Package.swift +++ b/Package.swift @@ -24,7 +24,7 @@ let package = Package( .library(name: "LyrebirdShoeGazeUI", targets: ["LyrebirdShoeGazeUI"]), ], dependencies: [ - .package(url: "https://realizedsound.mooo.com/josh/Lyrebird", from: "1.7.0"), + .package(url: "https://realizedsound.mooo.com/josh/Lyrebird", from: "1.7.1"), ], targets: [ // The core target depends on LyrebirdUI as well as the synthesis core: diff --git a/Sources/LyrebirdShoeGaze/Pedal.swift b/Sources/LyrebirdShoeGaze/Pedal.swift index 942329d..a50b9f1 100644 --- a/Sources/LyrebirdShoeGaze/Pedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedal.swift @@ -53,6 +53,11 @@ open class Pedal: SynthesizerBase { /// Output gain as a linear multiplier. `1.0` = unity gain. @UIParam public var level: Double = 1.0 + /// Optional path to an external resource this pedal references (e.g. a neural-amp model file). + /// Not a `@UIParam` — it's a `String`, applied at `build()`, and persisted separately in + /// ``PedalState/resourcePath``. Default `nil`. Pedals that don't use an external file ignore it. + public var resourcePath: String? + // MARK: - Sample rate /// Engine sample rate, injected by `PedalBoard.add(_:)` before the pedal is added diff --git a/Sources/LyrebirdShoeGaze/PedalType.swift b/Sources/LyrebirdShoeGaze/PedalType.swift index 86b03a9..95eb472 100644 --- a/Sources/LyrebirdShoeGaze/PedalType.swift +++ b/Sources/LyrebirdShoeGaze/PedalType.swift @@ -45,6 +45,7 @@ public extension PedalType { // Drive & dynamics static let amp = PedalType("Amp") + static let neuralAmp = PedalType("Neural Amp") static let overdrive = PedalType("Overdrive") static let distortion = PedalType("Distortion") static let fuzz = PedalType("Fuzz") diff --git a/Sources/LyrebirdShoeGaze/Pedals/NeuralAmpPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/NeuralAmpPedal.swift new file mode 100644 index 0000000..dcc325f --- /dev/null +++ b/Sources/LyrebirdShoeGaze/Pedals/NeuralAmpPedal.swift @@ -0,0 +1,120 @@ +// NeuralAmpPedal.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import Foundation +import LyrebirdSynth +import LyrebirdUI + +/// Neural amp/pedal capture player: runs a GuitarML Proteus / NeuralPi LSTM model on the input. +/// +/// Load a model with ``loadModel(path:)`` (or set ``Pedal/resourcePath``); the file path is +/// persisted in the rig, so a saved rig reloads the same capture. With no model loaded the pedal +/// passes the signal through clean. +/// +/// Signal chain (inside `effect(_:)`): +/// ``` +/// dry → × gain → LSTMAmp(model) → mono out (model loaded) +/// dry → mono out (no model) +/// ``` +/// +/// The model is read and built at `build()` (off the RT thread); changing it takes effect on the +/// next rebuild. Fully wet when a model is loaded; `Level` is the master. +/// +/// > Model files are **user-supplied and not bundled** — GuitarML models are GPL-3.0. Download a +/// > capture (e.g. from the GuitarML Tone Library or Tone3000) and point the pedal at it. +/// +/// ## Parameters +/// +/// Inherits `bypass` and `level` (master) from `Pedal`, plus: +/// - `gain`: Input drive into the model (linear). Range `[0.25, 4]`. +public final class NeuralAmpPedal: Pedal { + + /// One-line description shown in the picker and info panel. + public override var summary: String { + "Plays a neural amp/pedal capture (GuitarML Proteus / NeuralPi model)." + } + + /// Fully wet when a model is loaded; hide the Mix control. + public override var showsMixControl: Bool { false } + + // MARK: - Parameters + + /// Input drive into the model (linear multiplier). Range [0.25, 4]. + @UIParam public var gain: Double = 1.0 + + // MARK: - Constants + + private static let defaultMix: Double = 1.0 + private let gainRange: ClosedRange = 0.25 ... 4.0 + + /// Caches the parsed model per path so a stereo-region build (which calls `effect(_:)` once + /// per channel) reads and decodes the file only once. Keyed on the path so a model change + /// re-parses. Touched only at `build()` on the main thread. + private var loadedModel: (path: String, model: NeuralAmpModel)? + + // MARK: - Init + + /// Creates a neural amp pedal. + /// + /// - Parameters: + /// - inputBus: Audio bus index to read the dry input from. Defaults to `0`. + /// - outputBus: Audio bus index to write the processed output to. Defaults to `0`. + public init( + inputBus: Int = 0, + outputBus: Int = 0 + ) { + super.init( + name: "Neural Amp", + inputBus: inputBus, + outputBus: outputBus, + initialMix: NeuralAmpPedal.defaultMix + ) + } + + /// Points the pedal at a Proteus/NeuralPi model file. Takes effect on the next rebuild. + /// + /// - Parameter path: Filesystem path to the model `.json`. + public func loadModel(path: String) { + resourcePath = path + } + + // MARK: - UI + + public override var effectKnobs: [PedalKnob] { + [ + PedalKnob(label: "Gain", node: $gain, range: gainRange), + ] + } + + // MARK: - Effect + + /// Runs the loaded model on the (gain-trimmed) input; passes through clean if no model is set + /// or the file can't be read/parsed. + /// + /// - Parameter dry: Dry mono input signal. + /// - Returns: The model output, or the dry signal when no model is loaded. + public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { + guard let path: String = resourcePath, let model: NeuralAmpModel = resolveModel(path: path) else { + return dry + } + let driven: any ValidGeneratorInput = dry * $gain + return LSTMAmp(input: driven, model: model) + } + + /// Loads (and caches) the model at `path`, or returns `nil` if the file is missing/unparsable. + private func resolveModel(path: String) -> NeuralAmpModel? { + if let cached: (path: String, model: NeuralAmpModel) = loadedModel, cached.path == path { + return cached.model + } + guard let data: Data = try? Data(contentsOf: URL(fileURLWithPath: path)), + let model: NeuralAmpModel = try? ProteusModelImporter.model(fromProteusJSON: data) + else { + return nil + } + loadedModel = (path, model) + return model + } +} diff --git a/Sources/LyrebirdShoeGaze/Presets/Pedal+Preset.swift b/Sources/LyrebirdShoeGaze/Presets/Pedal+Preset.swift index fe91e53..aec809f 100644 --- a/Sources/LyrebirdShoeGaze/Presets/Pedal+Preset.swift +++ b/Sources/LyrebirdShoeGaze/Presets/Pedal+Preset.swift @@ -28,7 +28,8 @@ extension Pedal { sliderValues: sliderValues, selectorValues: selectorValues, bypass: bypass, - tempoSync: tempoSync.isEmpty ? nil : tempoSync + tempoSync: tempoSync.isEmpty ? nil : tempoSync, + resourcePath: resourcePath ) } @@ -66,5 +67,6 @@ extension Pedal { // tempo by `ShoeGaze` after the rebuild, so no clock is required here. tempoSync = state.tempoSync ?? [:] tempoSyncRevision.bump() + resourcePath = state.resourcePath } } diff --git a/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift b/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift index f27251d..8fae858 100644 --- a/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift +++ b/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift @@ -32,6 +32,7 @@ public enum PedalRegistry { /// to a board via `board.add(_:)`. public static let builtIn: [String: @MainActor () -> Pedal] = [ "Amp": { AmpPedal() }, + "Neural Amp": { NeuralAmpPedal() }, "Overdrive": { OverdrivePedal() }, "Distortion": { DistortionPedal() }, "Delay": { DelayPedal() }, diff --git a/Sources/LyrebirdShoeGaze/Presets/PedalState.swift b/Sources/LyrebirdShoeGaze/Presets/PedalState.swift index 3b967b1..5ee2f2b 100644 --- a/Sources/LyrebirdShoeGaze/Presets/PedalState.swift +++ b/Sources/LyrebirdShoeGaze/Presets/PedalState.swift @@ -50,6 +50,10 @@ public struct PedalState: Codable, Equatable { /// decode to "all parameters free". public let tempoSync: [String: TempoSync]? + /// Optional path to an external resource the pedal references (e.g. a neural-amp model file). + /// `nil` for pedals that use no external file, and for presets saved before this existed. + public let resourcePath: String? + /// Creates a pedal state snapshot. /// /// - Parameters: @@ -60,6 +64,8 @@ public struct PedalState: Codable, Equatable { /// - selectorValues: Current selector values in `Pedal.effectSelectors` order. Defaults to empty. /// - bypass: Bypass parameter value (`0.0` = engaged, `1.0` = bypassed). /// - tempoSync: Tempo-sync state per syncable parameter, or `nil` for none. Defaults to `nil`. + /// - resourcePath: Path to an external resource the pedal references (e.g. a neural-amp + /// model file), or `nil`. Defaults to `nil`. public init( id: UUID = UUID(), type: String, @@ -68,7 +74,8 @@ public struct PedalState: Codable, Equatable { sliderValues: [Double] = [], selectorValues: [Double] = [], bypass: Double, - tempoSync: [String: TempoSync]? = nil + tempoSync: [String: TempoSync]? = nil, + resourcePath: String? = nil ) { self.id = id self.type = type @@ -78,6 +85,7 @@ public struct PedalState: Codable, Equatable { self.selectorValues = selectorValues self.bypass = bypass self.tempoSync = tempoSync + self.resourcePath = resourcePath } private enum CodingKeys: String, CodingKey { @@ -89,6 +97,7 @@ public struct PedalState: Codable, Equatable { case selectorValues case bypass case tempoSync + case resourcePath } /// Decodes a snapshot, tolerating presets saved before `switchValues` / `sliderValues` / @@ -103,5 +112,6 @@ public struct PedalState: Codable, Equatable { self.selectorValues = try container.decodeIfPresent([Double].self, forKey: .selectorValues) ?? [] self.bypass = try container.decode(Double.self, forKey: .bypass) self.tempoSync = try container.decodeIfPresent([String: TempoSync].self, forKey: .tempoSync) + self.resourcePath = try container.decodeIfPresent(String.self, forKey: .resourcePath) } } diff --git a/Sources/LyrebirdShoeGazeUI/HelpView.swift b/Sources/LyrebirdShoeGazeUI/HelpView.swift index a3f5996..915bb8a 100644 --- a/Sources/LyrebirdShoeGazeUI/HelpView.swift +++ b/Sources/LyrebirdShoeGazeUI/HelpView.swift @@ -181,6 +181,9 @@ public struct HelpView: View { "The Amp pedal's tone stack follows the Yeh & Smith analysis of the Fender/Marshall/Vox " + "tone-stack circuit (\"Discretization of the '59 Fender Bassman Tone Stack\", DAFx-06); " + "its models are original code, not captures of specific hardware.", + "The Neural Amp pedal loads GuitarML Proteus / NeuralPi capture models (the open LSTM " + + "format from github.com/GuitarML). You supply the model file; none are bundled, and " + + "those models are licensed by their respective authors.", ] ) } diff --git a/Sources/LyrebirdShoeGazeUI/PedalCategory.swift b/Sources/LyrebirdShoeGazeUI/PedalCategory.swift index 8bc30e8..2276dd9 100644 --- a/Sources/LyrebirdShoeGazeUI/PedalCategory.swift +++ b/Sources/LyrebirdShoeGazeUI/PedalCategory.swift @@ -42,7 +42,7 @@ public enum PedalCategory: String, CaseIterable, Identifiable, Sendable { } private static let mapping: [String: PedalCategory] = [ - "Amp": .gain, "Overdrive": .gain, "Distortion": .gain, "Fuzz": .gain, "Volume": .gain, + "Amp": .gain, "Neural Amp": .gain, "Overdrive": .gain, "Distortion": .gain, "Fuzz": .gain, "Volume": .gain, "Compressor": .dynamics, "Sustainer": .dynamics, "Limiter": .dynamics, "Noise Gate": .dynamics, "Graphic EQ": .filter, "Auto-Wah": .filter, "Wah": .filter, "Mutant": .filter, "Cabinet": .filter, diff --git a/Sources/LyrebirdShoeGazeUI/PedalPaletteView.swift b/Sources/LyrebirdShoeGazeUI/PedalPaletteView.swift index b83b7c0..f340ae5 100644 --- a/Sources/LyrebirdShoeGazeUI/PedalPaletteView.swift +++ b/Sources/LyrebirdShoeGazeUI/PedalPaletteView.swift @@ -54,7 +54,7 @@ public struct PedalPaletteView: View { /// Preferred display order for the built-in pedals. Any registered pedal not listed here is /// appended afterwards (sorted) so the palette never silently drops a newly added pedal. private static let preferredOrder: [String] = [ - "Amp", "Overdrive", "Distortion", "Compressor", "Sustainer", "Limiter", "Noise Gate", "Fuzz", + "Amp", "Neural Amp", "Overdrive", "Distortion", "Compressor", "Sustainer", "Limiter", "Noise Gate", "Fuzz", "Volume", "Graphic EQ", "Tremolo", "Phaser", "Modulation", "Chorus", "Flanger", "Vibrato", "Rotary", "Auto-Wah", "Wah", "Mutant", "Cabinet", "Ring Modulator", "Diffuser", "Bitcrusher", "Granulator", diff --git a/Sources/LyrebirdShoeGazeUI/PedalView.swift b/Sources/LyrebirdShoeGazeUI/PedalView.swift index 275a039..06214c3 100644 --- a/Sources/LyrebirdShoeGazeUI/PedalView.swift +++ b/Sources/LyrebirdShoeGazeUI/PedalView.swift @@ -17,6 +17,7 @@ import LyrebirdShoeGaze private func enclosureColor(for displayName: String) -> Color { switch displayName { case "Amp": return Color(red: 0.18, green: 0.17, blue: 0.20) // amp-head charcoal + case "Neural Amp": return Color(red: 0.22, green: 0.16, blue: 0.30) // deep neural violet case "Overdrive": return Color(red: 0.85, green: 0.42, blue: 0.08) // warm orange case "Distortion": return Color(red: 0.80, green: 0.26, blue: 0.12) // hot rust case "Delay": return Color(red: 0.10, green: 0.55, blue: 0.55) // teal diff --git a/Tests/LyrebirdShoeGazeTests/NeuralAmpPedalTests.swift b/Tests/LyrebirdShoeGazeTests/NeuralAmpPedalTests.swift new file mode 100644 index 0000000..cf0e655 --- /dev/null +++ b/Tests/LyrebirdShoeGazeTests/NeuralAmpPedalTests.swift @@ -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.. 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) + } +}