Skip to content
OpenSaucePublic

About

Pure-Rust, real-time-safe inference for Neural Amp Modeler (.nam) models.

Topics

Resources

Stars

19 stars

Watchers

1 watching

Forks

Latest commit

 

History

70 Commits

Folders and files

Repository files navigation

nam-rs

crates.io docs.rs CI license

Pure-Rust, real-time-safe inference for Neural Amp Modeler (NAM) .nam models.

NAM is an open-source way to capture guitar amps and pedals as neural networks, and a capture is shared as a .nam file. Load one, hand it blocks of audio, get the amp back. nam-rs is built for the audio thread: a JACK callback, a VST3/CLAP process(), or any real-time audio graph.

  • Pure Rust. No C++ toolchain, no FFI, no build script. Three dependencies: serde, serde_json and thiserror.
  • Real-time safe. Processing allocates nothing, takes no locks and makes no system calls. A test enforces that for every architecture.
  • Matches the reference. Output is checked against the Python and C++ implementations from the first sample. Across 109 real captures it differs from NAM Core by less than −129 dBFS RMS, and by at most 1.3e-5 on any one sample.
  • Fast. A typical amp model uses about 4% of one CPU core on a desktop machine, which leaves the rest for your signal chain. The fastest code path for your CPU is picked automatically, with no compiler flags to set.
  • Covers A2. WaveNet, LSTM and NAM's second-generation A2 models, including the slimmable ones that trade quality for CPU on a dial.

Install

cargo add nam-rs

MSRV: Rust 1.77.

This README tracks main, which can be ahead of the latest release. docs.rs always matches the published crate.

Usage

use nam_rs::{Model, NamModel};

fn main() -> Result<(), nam_rs::Error> {
    // Off the audio thread: parsing, construction and buffers all allocate here.
    // The model comes back settled against silence, so the first real block is
    // clean; `model.reset()` returns it to this state.
    let file = NamModel::from_file("twin_reverb.nam")?;
    let mut model = Model::from_nam(&file)?;
    let mut block = vec![0.0_f32; 512];

    // On the audio thread, once per block: in place, no allocation. State
    // carries across calls, so block-wise output matches one whole-buffer call.
    model.process_buffer(&mut block);
    Ok(())
}

A .nam that is already in memory (a download, an archive entry, one embedded in your binary) loads without touching the filesystem: NamModel::from_slice() takes bytes, from_json_str() takes text, and from_reader() takes any io::Read.

Two runnable examples live in examples/:

cargo run --release --example run_model -- path/to/model.nam   # load it, run it, print stats
cargo run --release --example streaming -- path/to/model.nam   # the full block-wise host loop

Need a .nam to try it with? TONE3000 is a catalogue of captures to download.

Slimmable models

An A2 SlimmableContainer holds several complete submodels of increasing size. One dial picks which of them runs, and turning it is safe on the audio thread:

use nam_rs::{Model, NamModel};

fn main() -> Result<(), nam_rs::Error> {
    let file = NamModel::from_file("slimmable.nam")?;
    let mut model = Model::from_nam(&file)?;

    // `None` for a file that is not a container.
    if let Some(slim) = model.as_slimmable_mut() {
        slim.set_slim_size(0.5); // 0.0 is the smallest submodel, 1.0 the largest
    }
    Ok(())
}

Switching leaves the incoming submodel in the state it last had, where NAM Core resets it. set_slim_size_and_reset() does what NAM Core does; the crate docs explain the difference.

Integration notes

Mono, one per channel. A Model takes mono in and gives mono out, and keeps its own state. For stereo, build one per channel.

Threads. A Model is Send + Sync: build it on a worker thread, where allocating is fine, and move it to the audio thread.

No added latency, any block size. Each output sample depends only on the input up to that sample, so there is no lookahead for a host to compensate. process_buffer takes a block of any length.

Sample rate. A .nam expects audio at the rate it was captured (Model::expected_sample_rate(), 48 kHz if the file omits it). nam-rs does not resample: feed it audio at that rate, or resample in your host first. A mismatch produces silently wrong output, because dilations and recurrence are defined in samples, not seconds.

Startup and reset. Model::from_nam() returns a model already settled against silence, so there is no startup transient and the output matches NAM Core from the first sample. Model::reset() returns to that state without allocating. The crate docs cover opting out.

Levels. Model::loudness(), input_level_dbu() and output_level_dbu() give the calibration numbers for gain-staging. nam-rs runs the forward pass only: the DC blocker and optional loudness normalization that the reference NAM plugin applies belong to your audio graph, not to the model.

Metadata. NamModel::metadata_typed() returns what a model browser shows: name, author, gear, tone type and date. includes_cab() says whether the capture already contains a speaker cabinet, and so whether an impulse response after it would be a second one.

Supported models

  • WaveNet: the original A1 models, and single A2 models with the newer feature set (FiLM, gating, bottleneck and grouped convolutions, conv heads).
  • LSTM.
  • Linear: an impulse response as a model. Mono, convolved directly, at the file's own sample rate.
  • SlimmableContainer (A2): the multi-size models described above.
  • Sequential: complete models in series in one file, such as an amp model followed by an impulse response.

Not supported yet: the ConvNet architecture, the LeakyHardtanh and PReLU activations, and multi-channel input. A file that needs one of these fails at load with an Error that names it. The crate docs list exactly what is accepted and rejected, and #33 tracks the remaining gap to NAM Core.

Performance

From cargo bench, standard-size fixture models, one core of an AMD Ryzen 7 5800X (x86-64 with AVX2), release + LTO:

  • WaveNet: about 0.9 µs/sample through process_buffer, 23× real time at 48 kHz. That is around 16× faster than the per-sample path, so prefer whole blocks.
  • LSTM: recurrent, so one step per sample on either path. On x86-64 with AVX2 process_buffer adds an AVX2 build to the vectorized cell; on aarch64 the two paths run the same code.

On aarch64, enabling the opt-in neon-fma Cargo feature fuses the LSTM, conv and activation multiply-add into NEON fmla. It is off by default and is a no-op on other targets; it changes rounding, so it is not bit-identical to the default build, but stays within the parity bounds.

Numbers vary with CPU and model size, so run cargo bench on your own target. The crate docs describe how the block path gets there.

Versioning

nam-rs is 0.x: a breaking change bumps the minor version. Each release's notes are on the releases page.

Development

cargo test
cargo fmt --check
cargo clippy --all-targets -- -D warnings

Parity fixtures are committed under tests/fixtures/; regenerate them from Python NAM with tests/fixtures/gen_fixtures.py (see tests/fixtures/README.md). The project's vocabulary is in CONTEXT.md.

Attribution & license

nam-rs is MIT-licensed (see LICENSE). It is a derivative work: the algorithm and .nam weight layout are ported from the projects below. Their license texts are reproduced in NOTICE.

Project Role License
neural-amp-modeler Reference trainer + .nam exporter (source of truth for weight/config layout) MIT
NeuralAmpModelerCore Canonical C++ inference library MIT
NeuralAudio High-performance C++ NAM runtime; primary porting reference MIT

.nam files are licensed separately by whoever captured them; nam-rs ships none.

About

Pure-Rust, real-time-safe inference for Neural Amp Modeler (.nam) models.

Topics

Resources

Stars

19 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages