Views
No views yet
transformers-compatible port of google/magenta-realtime-2,
a real-time streaming music generation model. Every component (Depthformer LLM,
SpectroStream neural codec, MusicCoCa style encoder) was reimplemented in torch
and validated bit/token-exact against the original JAX/TFLite reference.trust_remote_code=True — no JAX, no TFLite. Runtime deps: torch,
transformers, sentencepiece (+ soundfile to save audio).1import torch, soundfile as sf
2from transformers import AutoModel
3
4model = AutoModel.from_pretrained(
5 "magenta-community/magenta-realtime-2", trust_remote_code=True, dtype=torch.bfloat16
6).to("cuda").eval()
7
8# Text / audio prompts via the MusicCoCa processor:
9model.load_processor() # magenta-community/magenta-rt-musiccoca-torch
10model.compile_steps() # optional: torch.compile the per-frame step (faster generation)
11audio, state = model.generate(style="lo-fi hip hop, mellow", frames=50, temperature=1.1)
12sf.write("out.wav", audio, 48000) # ~2 s, 48 kHz stereo
13
14# Continuous / live steering — keep passing `state` back; change `style` to morph:
15chunk, state = model.generate(style="drum and bass", frames=25, state=state)
16
17# Or skip the processor and pass explicit style tokens (12 RVQ ids):
18audio, _ = model.generate(style=[100] * 12, frames=50)
19
20# --- Real-time streaming: stateful per-frame (40 ms) decode, low latency ---
21# small chunks are cheap (no overlap-save re-decode); keep passing `state` back,
22# change `style` any time to morph live:
23state = None
24for _ in range(40): # ~8 s, ~0.2 s latency per step
25 chunk, state = model.generate(style="techno", frames=5, state=state)
26 # send `chunk` (48 kHz stereo float32) straight to your audio outputmodel.generate(...) returns (audio, state). Pass state back for seamless
continuation; only the newly-available audio is returned each call (use flush=True
on the final call to emit the tail).| Component | What it is | Validation vs reference |
|---|---|---|
| Depthformer | decoder-only LLM, per-frame RVQ depth-autoregression | token-exact |
| SpectroStream | RVQ neural audio codec (encoder + decoder) | decode 2.7e-6 · encode codes 100% |
| MusicCoCa | text+audio style encoder (separate MusicCoCaProcessor) | tokens 100% exact |
GenerationMixin: the per-frame multi-codebook
depth loop + streaming codec decode don't fit a single-token-stream _sample.generate returns only the newly-available audio and a state; pass state back to
continue seamlessly, and change style between calls to steer the stream live:1import sounddevice as sd, numpy as np
2state = None
3with sd.OutputStream(samplerate=48000, channels=2, dtype="float32") as out:
4 for i in range(20): # ~20 s
5 chunk, state = model.generate(style="techno", frames=25, state=state, flush=(i == 19))
6 out.write(np.ascontiguousarray(chunk, dtype=np.float32))examples/streaming.py.torch.compile the per-frame step for faster-than-real-time generation (one-time warmup,
any CUDA GPU):1model.compile_steps() # torch.compile (dynamic shapes); warms on first call
2audio, state = model.generate(style="techno", frames=25)1model.export_aoti("./aoti") # compile once on your target GPU
2# later / elsewhere on the same GPU arch:
3model.load_aoti("./aoti") # instant load, no torch.compilemagenta-community/magenta-realtime-2 — base (canonical, higher quality)magenta-community/magenta-realtime-2-small — small (real-time)