-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
510 lines (437 loc) · 15 KB
/
predict.py
File metadata and controls
510 lines (437 loc) · 15 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import os
import gc
import hashlib
import json
import shlex
import sys
import subprocess
import tempfile
from typing import List
import librosa
import numpy as np
import soundfile as sf
import torch
from cog import BasePredictor, Input, Path
from tqdm import tqdm
import onnxruntime as ort
import warnings
from pedalboard import Pedalboard, Reverb, Compressor, Gain, HighpassFilter
from utils import (
create_directories,
download_manager,
logger,
)
warnings.filterwarnings("ignore")
class MDXModel:
def __init__(
self,
device,
dim_f,
dim_t,
n_fft,
hop=1024,
stem_name=None,
compensation=1.000,
):
self.dim_f = dim_f
self.dim_t = dim_t
self.dim_c = 4
self.n_fft = n_fft
self.hop = hop
self.stem_name = stem_name
self.compensation = compensation
self.n_bins = self.n_fft // 2 + 1
self.chunk_size = hop * (self.dim_t - 1)
self.window = torch.hann_window(
window_length=self.n_fft, periodic=True
).to(device)
out_c = self.dim_c
self.freq_pad = torch.zeros(
[1, out_c, self.n_bins - self.dim_f, self.dim_t]
).to(device)
def stft(self, x):
x = x.reshape([-1, self.chunk_size])
x = torch.stft(
x,
n_fft=self.n_fft,
hop_length=self.hop,
window=self.window,
center=True,
return_complex=True,
)
x = torch.view_as_real(x)
x = x.permute([0, 3, 1, 2])
x = x.reshape([-1, 2, 2, self.n_bins, self.dim_t]).reshape(
[-1, 4, self.n_bins, self.dim_t]
)
return x[:, :, : self.dim_f]
def istft(self, x, freq_pad=None):
freq_pad = (
self.freq_pad.repeat([x.shape[0], 1, 1, 1])
if freq_pad is None
else freq_pad
)
x = torch.cat([x, freq_pad], -2)
x = x.reshape([-1, 2, 2, self.n_bins, self.dim_t]).reshape(
[-1, 2, self.n_bins, self.dim_t]
)
x = x.permute([0, 2, 3, 1])
x = x.contiguous()
x = torch.view_as_complex(x)
x = torch.istft(
x,
n_fft=self.n_fft,
hop_length=self.hop,
window=self.window,
center=True,
)
return x.reshape([-1, 2, self.chunk_size])
class MDX:
DEFAULT_SR = 44100
DEFAULT_CHUNK_SIZE = 0 * DEFAULT_SR
DEFAULT_MARGIN_SIZE = 1 * DEFAULT_SR
def __init__(
self, model_path: str, params: MDXModel, processor=0
):
self.device = (
torch.device(f"cuda:{processor}")
if processor >= 0
else torch.device("cpu")
)
self.provider = (
["CUDAExecutionProvider"]
if processor >= 0
else ["CPUExecutionProvider"]
)
self.model = params
self.ort = ort.InferenceSession(model_path, providers=self.provider)
self.ort.run(
None,
{"input": torch.rand(1, 4, params.dim_f, params.dim_t).numpy()},
)
self.process = lambda spec: self.ort.run(
None, {"input": spec.cpu().numpy()}
)[0]
self.prog = None
@staticmethod
def get_hash(model_path):
try:
with open(model_path, "rb") as f:
f.seek(-10000 * 1024, 2)
model_hash = hashlib.md5(f.read()).hexdigest()
except:
model_hash = hashlib.md5(open(model_path, "rb").read()).hexdigest()
return model_hash
@staticmethod
def segment(
wave,
combine=True,
chunk_size=DEFAULT_CHUNK_SIZE,
margin_size=DEFAULT_MARGIN_SIZE,
):
if combine:
processed_wave = None
for segment_count, segment in enumerate(wave):
start = 0 if segment_count == 0 else margin_size
end = None if segment_count == len(wave) - 1 else -margin_size
if margin_size == 0:
end = None
if processed_wave is None:
processed_wave = segment[:, start:end]
else:
processed_wave = np.concatenate(
(processed_wave, segment[:, start:end]), axis=-1
)
else:
processed_wave = []
sample_count = wave.shape[-1]
if chunk_size <= 0 or chunk_size > sample_count:
chunk_size = sample_count
if margin_size > chunk_size:
margin_size = chunk_size
for segment_count, skip in enumerate(
range(0, sample_count, chunk_size)
):
margin = 0 if segment_count == 0 else margin_size
end = min(skip + chunk_size + margin_size, sample_count)
start = skip - margin
cut = wave[:, start:end].copy()
processed_wave.append(cut)
if end == sample_count:
break
return processed_wave
def pad_wave(self, wave):
n_sample = wave.shape[1]
trim = self.model.n_fft // 2
gen_size = self.model.chunk_size - 2 * trim
pad = gen_size - n_sample % gen_size
wave_p = np.concatenate(
(
np.zeros((2, trim)),
wave,
np.zeros((2, pad)),
np.zeros((2, trim)),
),
1,
)
mix_waves = []
for i in range(0, n_sample + pad, gen_size):
waves = np.array(wave_p[:, i:i + self.model.chunk_size])
mix_waves.append(waves)
mix_waves = torch.tensor(mix_waves, dtype=torch.float32).to(
self.device
)
return mix_waves, pad, trim
def _process_wave(self, mix_waves, trim, pad):
mix_waves = mix_waves.split(1)
with torch.no_grad():
pw = []
for mix_wave in mix_waves:
if self.prog:
self.prog.update()
spec = self.model.stft(mix_wave)
processed_spec = torch.tensor(self.process(spec))
processed_wav = self.model.istft(
processed_spec.to(self.device)
)
processed_wav = (
processed_wav[:, :, trim:-trim]
.transpose(0, 1)
.reshape(2, -1)
.cpu()
.numpy()
)
pw.append(processed_wav)
processed_signal = np.concatenate(pw, axis=-1)[:, :-pad]
return processed_signal
def process_wave(self, wave: np.array):
self.prog = tqdm(total=0)
mix_waves, pad, trim = self.pad_wave(wave)
self.prog.total = len(mix_waves)
processed_signal = self._process_wave(mix_waves, trim, pad)
self.prog.close()
return processed_signal
def run_mdx(
model_params,
output_dir,
model_path,
filename,
suffix=None,
invert_suffix=None,
denoise=False,
device_base="cuda",
):
if device_base == "cuda":
device = torch.device("cuda:0")
processor_num = 0
else:
device = torch.device("cpu")
processor_num = -1
model_hash = MDX.get_hash(model_path)
mp = model_params.get(model_hash)
model = MDXModel(
device,
dim_f=mp["mdx_dim_f_set"],
dim_t=2 ** mp["mdx_dim_t_set"],
n_fft=mp["mdx_n_fft_scale_set"],
stem_name=mp["primary_stem"],
compensation=mp["compensate"],
)
mdx_sess = MDX(model_path, model, processor=processor_num)
wave, sr = librosa.load(filename, mono=False, sr=44100)
# Normalize input
peak = max(np.max(wave), abs(np.min(wave)))
wave /= peak
if denoise:
wave_processed = -(mdx_sess.process_wave(-wave)) + (
mdx_sess.process_wave(wave)
)
wave_processed *= 0.5
else:
wave_processed = mdx_sess.process_wave(wave)
# Return to previous peak
wave_processed *= peak
stem_name = model.stem_name if suffix is None else suffix
main_filepath = os.path.join(
output_dir,
f"{os.path.basename(os.path.splitext(filename)[0])}_{stem_name}.wav",
)
sf.write(main_filepath, wave_processed.T, sr)
del mdx_sess, wave_processed, wave
gc.collect()
torch.cuda.empty_cache()
return main_filepath
def convert_to_stereo_and_wav(audio_path, output_dir):
wave, sr = librosa.load(audio_path, mono=False, sr=44100)
if type(wave[0]) != np.ndarray or audio_path[-4:].lower() != ".wav":
stereo_path = f"{os.path.splitext(os.path.basename(audio_path))[0]}_stereo.wav"
stereo_path = os.path.join(output_dir, stereo_path)
command = shlex.split(
f'ffmpeg -y -loglevel error -i "{audio_path}" -ac 2 -f wav "{stereo_path}"'
)
sub_params = {
"stdout": subprocess.PIPE,
"stderr": subprocess.PIPE,
"creationflags": subprocess.CREATE_NO_WINDOW
if sys.platform == "win32"
else 0,
}
process_wav = subprocess.Popen(command, **sub_params)
output, errors = process_wav.communicate()
if process_wav.returncode != 0 or not os.path.exists(stereo_path):
raise Exception("Error processing audio to stereo wav")
return stereo_path
else:
return audio_path
def add_vocal_effects(
input_file,
output_file,
reverb_room_size=0.15,
reverb_damping=0.7,
reverb_wet_level=0.2,
compressor_threshold_db=-15,
compressor_ratio=4.0,
compressor_attack_ms=1.0,
compressor_release_ms=100,
gain_db=0,
):
effects = [HighpassFilter()]
effects.append(
Reverb(
room_size=reverb_room_size,
damping=reverb_damping,
wet_level=reverb_wet_level,
dry_level=0.8
)
)
effects.append(
Compressor(
threshold_db=compressor_threshold_db,
ratio=compressor_ratio,
attack_ms=compressor_attack_ms,
release_ms=compressor_release_ms
)
)
if gain_db:
effects.append(Gain(gain_db=gain_db))
board = Pedalboard(effects)
with sf.SoundFile(input_file) as f:
audio = f.read(always_2d=True).T
samplerate = f.samplerate
effected = board(audio, samplerate)
sf.write(output_file, effected.T, samplerate)
MDX_DOWNLOAD_LINK = "https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/"
UVR_MODELS = ["UVR-MDX-NET-Voc_FT.onnx"]
class Predictor(BasePredictor):
def setup(self):
"""Load the models into memory"""
self.mdxnet_models_dir = "mdx_models"
os.makedirs(self.mdxnet_models_dir, exist_ok=True)
# Download required model
for id_model in UVR_MODELS:
download_manager(
os.path.join(MDX_DOWNLOAD_LINK, id_model),
self.mdxnet_models_dir
)
# Load model parameters
with open(os.path.join(self.mdxnet_models_dir, "data.json")) as infile:
self.mdx_model_params = json.load(infile)
# Set device
self.device_base = "cuda" if torch.cuda.is_available() else "cpu"
logger.info(f"Device: {self.device_base}")
logger.info(f"ONNX Runtime device: {ort.get_device()}")
def predict(
self,
audio: Path = Input(description="Input audio file URL or path"),
extract_vocals: bool = Input(
description="Extract vocals (if False, extracts instrumental)",
default=True
),
output_format: str = Input(
description="Output audio format",
choices=["wav", "mp3"],
default="wav"
),
) -> Path:
"""Run audio separation"""
# Create temp directory for processing
temp_dir = tempfile.mkdtemp()
output_dir = os.path.join(temp_dir, "output")
create_directories(output_dir)
try:
# Convert to stereo WAV if needed
audio_path = str(audio)
logger.info(f"Processing audio: {audio_path}")
stereo_path = convert_to_stereo_and_wav(audio_path, temp_dir)
# Vocal separation
logger.info("Starting vocal separation...")
vocals_path = run_mdx(
self.mdx_model_params,
output_dir,
os.path.join(self.mdxnet_models_dir, "UVR-MDX-NET-Voc_FT.onnx"),
stereo_path,
suffix="Vocals" if extract_vocals else "Instrumental",
denoise=True,
device_base=self.device_base,
)
# Apply effects with defaults from app.py
if extract_vocals:
logger.info("Applying vocal effects...")
effects_path = vocals_path.replace(".wav", "_effects.wav")
add_vocal_effects(
vocals_path,
effects_path,
reverb_room_size=0.15,
reverb_damping=0.7,
reverb_wet_level=0.2,
compressor_threshold_db=-15,
compressor_ratio=4.0,
compressor_attack_ms=1.0,
compressor_release_ms=100,
gain_db=0,
)
output_path = effects_path
else:
output_path = vocals_path
# Convert format if needed
if output_format == "mp3":
wav_root, _ = os.path.splitext(output_path)
mp3_path = f"{wav_root}.mp3"
ffmpeg_cmd = [
"ffmpeg",
"-y",
"-i",
output_path,
"-codec:a",
"libmp3lame",
"-b:a",
"192k",
mp3_path,
]
try:
subprocess.run(
ffmpeg_cmd,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except FileNotFoundError as exc:
raise RuntimeError(
"FFmpeg is required for MP3 export but was not found in the runtime."
) from exc
except subprocess.CalledProcessError as exc:
raise RuntimeError(
"FFmpeg failed while converting audio to MP3: "
f"{exc.stderr.decode(errors='ignore')}"
) from exc
output_path = mp3_path
# Move to final output location
final_output = f"/tmp/output.{output_format}"
os.rename(output_path, final_output)
logger.info(f"Processing complete: {final_output}")
return Path(final_output)
finally:
# Cleanup temp directory
import shutil
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)