// 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 } }