Any PyTorch model can become a nn~ object by subclassing nn_tilde.Module and registering methods and attributes
The nn_tilde Python library provides a Module base class. To make any PyTorch model playable in Max/Pd, you: (1) subclass nn_tilde.Module; (2) call register_method() for each processing pipeline, specifying in_channels, in_ratio, out_channels, out_ratio, and labels; (3) call register_attribute() for each live-controllable parameter with a default value; (4) implement the methods decorated with @torch.jit.export; (5) call model.export_to_ts(‘model.ts’) to script and save. The ratios define temporal downsampling — out_ratio=1024 means each 1024 input samples produces 1 output sample (useful for feature extractors). Registration validates the method’s tensor shapes automatically.
Examples
See scripting/effects.py: AudioUtils subclasses nn_tilde.Module, registers methods like ‘saturate’ (1 in, 1 out, ratio 1:1) and ‘rms’ (out_ratio=1024), then calls export_to_ts(‘effects.ts’). The resulting .ts file is loadable in any nn~ object.
Assessment
Write the Python code skeleton to register a method named ‘pitch_shift’ with 1 input channel, 1 output channel, and a 1:1 ratio. Explain what export_to_ts() does and why @torch.jit.export is required.