-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
77 lines (59 loc) · 1.72 KB
/
run.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from pathlib import Path
import torch
import typer
app = typer.Typer(pretty_exceptions_show_locals=False)
@app.command()
def model_summary() -> None:
from unet import UNet
net = UNet()
print(net)
@app.command()
def test() -> None:
from unet import UNet
batch_size = 5
n_channels = 2
x = torch.randn(batch_size, n_channels, 512, 128)
print(x.shape)
net = UNet(in_channels=n_channels)
y = net.forward(x)
print(y.shape)
@app.command()
def split(
model_path: str = "models/2stems/model",
input: str = "data/audio_example.mp3",
output_dir: str = "output",
offset: float = 0,
duration: float = 30,
write_src: bool = False,
) -> None:
import librosa
import soundfile
from splitter import Splitter
sr = 44100
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
splitter = Splitter.from_pretrained(model_path).to(device).eval()
# load wav audio
fpath_src = Path(input)
wav, _ = librosa.load(
fpath_src,
mono=False,
res_type="kaiser_fast",
sr=sr,
duration=duration,
offset=offset,
)
wav = torch.Tensor(wav).to(device)
# normalize audio
# wav_torch = wav / (wav.max() + 1e-8)
with torch.no_grad():
stems = splitter.separate(wav)
if write_src:
stems["input"] = wav
for name, stem in stems.items():
fpath_dst = Path(output_dir) / f"{fpath_src.stem}_{name}.wav"
print(f"Writing {fpath_dst}")
fpath_dst.parent.mkdir(exist_ok=True)
soundfile.write(fpath_dst, stem.cpu().detach().numpy().T, sr, "PCM_16")
# write_wav(fname, np.asfortranarray(stem.squeeze().numpy()), sr)
if __name__ == "__main__":
app()