File size: 1,469 Bytes
2e1dc7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import lightning as L

import config as cfg
from loader import load_model
from utils.audio import load_audio, save_audio
"""
REQUIREMENTS:

weights/
 - encodec_32khz.pt
 - lm-small-weights.pt in

checkpoints/
 - stage-drums-ckp1.pt
 - stage-bass-ckp1.pt
"""

#%% Load model
INSTRUMENT = "drums"
checkpoint_path = cfg.CKP_DIR / f"stage-{INSTRUMENT}.safetensors"
model = load_model(checkpoint_path)

#%% Load conditioning, generate and save

# load context audio, description
SAMPLE = "sample2"
SEED = 42

# load description if present
desc_path = cfg.AUDIO_DIR / INSTRUMENT / f"{SAMPLE}-desc.txt"
desc = desc_path.read_text().strip() if desc_path.exists() else None

# load audio context
wav = load_audio(cfg.AUDIO_DIR / INSTRUMENT / f"{SAMPLE}.wav").to(model.device)

# generate
L.seed_everything(SEED)
out = model.generate(n_samples=1,
                     gen_seconds=10,
                     prompt=None,
                     context=wav,
                     style=None,
                     beat=None,
                     description=[desc],
                     prog_bar=True)

# save output and mix
save_audio(out, cfg.AUDIO_DIR / "gen" / f"{SAMPLE}_{INSTRUMENT}_{SEED}.wav")
padded_wav = torch.nn.functional.pad(wav,
                                     tuple((0, out.shape[-1] - wav.shape[-1])),
                                     value=0)
mix = out + padded_wav
save_audio(mix, cfg.AUDIO_DIR / "gen" / f"{SAMPLE}_{INSTRUMENT}_{SEED}_mix.wav")