Scripting Custom nn~ Models for Live Rigs
Learning objectives
- learner can wrap an arbitrary PyTorch model as an nn~ object by subclassing and registering methods
- learner can batch multiple audio streams through one model with mc.nn~/mcs.nn~ to save CPU and RAM
- learner can expose control-rate features and wrap a source-separation model to split live audio into stems
Capstone — one whole task that evidences the objectives
Script a custom nn~ external for a live rig: subclass nn_tilde.Module to expose a source-separation model that splits live audio into stems in real time, add a control-rate feature-extraction method via a large out_ratio, and run several channels through it as a batch with mc.nn~/mcs.nn~ to keep CPU usage bounded.
Prerequisite modules
Stock nn~ models get you timbre transfer out of the box, but the moment your set needs something bespoke — live stem-splitting so drums drive one effects chain and vocals another, or a spectral-centroid signal steering your visuals — you have to script the model yourself. This module builds toward exactly that: a hand-rolled nn~ external for a Max-based performance rig, where an audio interface feeds several live channels into one neural model without blowing the CPU budget mid-set.
The arc starts supported: take a trivial PyTorch model and walk the registration recipe — subclass nn_tilde.Module, register methods and attributes, export to TorchScript — leaning on “Any PyTorch model can become a nn~ object by subclassing nn_tilde.Module” as the JIT how-to. Next, internalize how in_ratio and out_ratio trade audio rate for control rate, then reproduce the features.py pattern to emit spectral features one value per hop. Third exercise: adapt the unmix.py pattern to wrap a pretrained separation model into a one-in, four-stems-out object. Finally, the unsupported capstone fuses all three moves and adds batching — packing channels with mc.pack~ into mc.nn~, or declaring batch count up front with mcs.nn~ when you want per-batch latent tricks.
Every required atom gates the capstone: the scripting API and ratio system are the mechanics, the separation and feature-extraction patterns are the two methods you must ship, and the two multichannel externals are how the rig stays glitch-free under load. Supporting atoms enrich rather than gate — TorchScript’s constraints explain why your Python must be typed, the stems concept frames what separation buys a performer, void/lazy init and the RAVE batching example show where these techniques land in real patches.
Runnable examples
Generated from the context/ instrument corpus by concept (redistributable idioms only). Do not edit — regenerate with gen-module-examples.mjs.
live-hot-swap
live_loop :x do; play 60; sleep 1; end
sonicpi-0048 · CC0
Ndef(\a, { SinOsc.ar(220) * 0.1 }).play
supercollider-0037 · CC0
Atoms in this module
Required — these gate the capstone
Supporting — enrichment, not gating
Part of curricula
- Generative & AI AV Artist — real-time machine-driven performance — Deploy neural audio into a live rig required