Spaces:
Running
Running
jhj0517
commited on
Commit
·
d317550
1
Parent(s):
899737d
Add base separator
Browse files
modules/uvr/music_separator.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Credit to Team UVR : https://github.com/Anjok07/ultimatevocalremovergui
|
| 2 |
+
from typing import Optional
|
| 3 |
+
import soundfile as sf
|
| 4 |
+
import os
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
from uvr.models import MDX, Demucs, VrNetwork, MDXC
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class MusicSeparator:
|
| 11 |
+
def __init__(self,
|
| 12 |
+
model_dir: Optional[str] = None,
|
| 13 |
+
output_dir: Optional[str] = None):
|
| 14 |
+
self.model = None
|
| 15 |
+
self.device = self.get_device()
|
| 16 |
+
self.model_dir = model_dir
|
| 17 |
+
self.output_dir = output_dir
|
| 18 |
+
|
| 19 |
+
def update_model(self,
|
| 20 |
+
model_name: str = "UVR-MDX-NET-Inst_1",
|
| 21 |
+
segment_size: int = 256):
|
| 22 |
+
self.model = MDX(name="UVR-MDX-NET-Inst_1",
|
| 23 |
+
other_metadata={"segment": segment_size, "split": True},
|
| 24 |
+
device=self.device,
|
| 25 |
+
logger=None,
|
| 26 |
+
model_path="models\UVR\MDX_Net_Models\UVR-MDX-NET-Inst_HQ_1.onnx")
|
| 27 |
+
|
| 28 |
+
def separate(self,
|
| 29 |
+
audio_file_path: str,
|
| 30 |
+
sample_rate: int = 44100):
|
| 31 |
+
if self.model is None:
|
| 32 |
+
self.model = self.update_model()
|
| 33 |
+
|
| 34 |
+
filename = audio_file_path
|
| 35 |
+
instrumental_output_path = os.path.join(self.output_dir, "instrumental", filename)
|
| 36 |
+
vocals_output_path = os.path.join(self.output_dir, "vocals", filename)
|
| 37 |
+
|
| 38 |
+
result = self.model(audio_file_path)
|
| 39 |
+
instrumental, vocals = result["instrumental"], result["vocals"]
|
| 40 |
+
|
| 41 |
+
sf.write('instrumental.wav', instrumental.T, sample_rate, format="WAV")
|
| 42 |
+
sf.write('vocals.wav', vocals.T, sample_rate, format="WAV")
|
| 43 |
+
|
| 44 |
+
return instrumental_output_path, vocals_output_path
|
| 45 |
+
|
| 46 |
+
@staticmethod
|
| 47 |
+
def get_device():
|
| 48 |
+
return "cuda" if torch.cuda.is_available() else "cpu"
|