File size: 2,879 Bytes
ee155af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""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")