home/ modules/ scripting-custom-nn-tilde-models

Scripting Custom nn~ Models for Live Rigs

  • 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

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.

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

Any PyTorch model can become a nn~ object by subclassing nn_tilde.Module and registering methods and attributes
Procedure L3 Craft KN
A deep source separation model can be wrapped in nn~ to split live audio into stems in real time
Concept L3 Craft KC
nn~ can output audio-analysis features as control-rate signals by using a large out_ratio
Concept L3 Craft KJ
in_ratio and out_ratio in nn~ method registration define the temporal compression between audio and model outputs
Concept L3 Craft KN
mc.nn~ runs several audio streams through one model as a batch, saving CPU and RAM versus duplicating nn~
Concept L3 Craft KN
mcs.nn~ packs all of one instance's inlets/outlets into a single multi-channel connection, enabling per-batch latent operations
Concept L3 Craft KN

Supporting — enrichment, not gating

mc.nn~ processes multiple audio channels through one RAVE model instance to cut CPU and RAM
Fact L3 Craft KN
nn~'s void/lazy mode lets you fix inlet/outlet count before attaching a model
Concept L3 Craft KN
A nn~ model's methods each define a distinct processing pipeline with its own inlet/outlet count
Concept L2 First instrument KN
TorchScript (.ts) files are the portable, runtime-independent format for deploying PyTorch models without Python
Concept L2 First instrument KN
Music source separation splits a stereo mix into isolated stems (drums, bass, vocals, other)
Concept L2 First instrument KC