tinygroove / example.py
totomanov's picture
tinygroove: weights, ONNX export, samples and model card
ee155af verified
Raw History Blame Contribute Delete
2.88 kB
"""Generate drums with tinygroove using only numpy and onnxruntime.
pip install numpy onnxruntime
hf download totomanov/tinygroove --local-dir tinygroove
python tinygroove/example.py
"""
import json, wave
from pathlib import Path
import numpy as np
import onnxruntime as ort
HERE = Path(__file__).resolve().parent
cfg = json.load(open(HERE / "config.json"))
flow = ort.InferenceSession(str(HERE / "onnx" / "flow.onnx"))
decoder = ort.InferenceSession(str(HERE / "onnx" / "decoder.onnx"))
def drum(category, genre=None, machine=None, decay=None, brightness=None, pitch=None, seed=0, steps=16, guidance=2.2):
"""One second of one drum, as float32 at 44.1 kHz.
category: one of cfg["categories"]. genre / machine: words from cfg["tags"] without their prefix.
decay, brightness, pitch: 0..1 across what is normal for that drum; None lets the model decide.
The same seed with a different request gives a related sound.
"""
no_cat, no_tag = len(cfg["categories"]), len(cfg["tags"])
cat = np.array([cfg["categories"].index(category), no_cat], dtype=np.int64) # row 0: the request; row 1: no request
tags = np.full((2, 4), no_tag, dtype=np.int64)
for i, tag in enumerate(t for t in (genre and "genre:" + genre, machine and "source:" + machine) if t):
tags[0, i] = cfg["tags"].index(tag)
knobs, kmask = np.zeros((2, 3), np.float32), np.zeros((2, 3), np.float32)
for k, v in enumerate((decay, brightness, pitch)):
if v is not None:
lo, mid, hi = (row[k] for row in cfg["knob_range"][category])
knobs[0, k] = lo + (mid - lo) * v * 2 if v < .5 else mid + (hi - mid) * (v - .5) * 2
kmask[0, k] = 1
x = np.random.default_rng(seed).standard_normal((1, cfg["latent_tokens"], cfg["latent_dims"])).astype(np.float32)
for s in range(steps): # Euler steps from noise (t = 0) to sound (t = 1)
v = flow.run(None, {"x": np.repeat(x, 2, 0), "t": np.full(2, s / steps, np.float32),
"cat": cat, "tags": tags, "knobs": knobs, "kmask": kmask})[0]
x = x + (v[1:] + guidance * (v[:1] - v[1:])) / steps
z = np.clip(x * cfg["latent_scale"], -1, 1).astype(np.float32)
audio = decoder.run(None, {"z": z})[0][0]
return audio * 0.89 / np.abs(audio).max() # the decoder can peak above 1.0
def save(path, audio):
with wave.open(str(path), "wb") as f:
f.setnchannels(1); f.setsampwidth(2); f.setframerate(cfg["sample_rate"])
f.writeframes((audio * 32767).astype("<i2").tobytes())
if __name__ == "__main__":
save("kick.wav", drum("kick", genre="deep house", decay=0.75, brightness=0.5))
save("hat.wav", drum("hat_closed", genre="tech house", machine="TR-909", seed=1))
print("wrote kick.wav and hat.wav")