mirror of
https://github.com/Anjok07/ultimatevocalremovergui.git
synced 2024-11-28 01:10:56 +01:00
Add files via upload
This commit is contained in:
parent
e39c715ca6
commit
33b7900ae2
@ -55,6 +55,7 @@ class ModelParameters(object):
|
||||
else:
|
||||
self.param = default_param
|
||||
|
||||
for k in ['mid_side', 'mid_side_b', 'mid_side_b2', 'stereo_w', 'reverse']:
|
||||
for k in ['mid_side', 'mid_side_b', 'mid_side_b2', 'stereo_w', 'stereo_n', 'reverse']:
|
||||
if not k in self.param:
|
||||
self.param[k] = False
|
||||
|
19
lib/nets.py
19
lib/nets.py
@ -1,9 +1,9 @@
|
||||
import torch
|
||||
import numpy as np
|
||||
from torch import nn
|
||||
import torch.nn.functional as F
|
||||
|
||||
from lib import layers
|
||||
from lib import spec_utils
|
||||
|
||||
|
||||
class BaseASPPNet(nn.Module):
|
||||
@ -60,7 +60,7 @@ class CascadedASPPNet(nn.Module):
|
||||
|
||||
self.offset = 128
|
||||
|
||||
def forward(self, x, aggressiveness=None):
|
||||
def forward(self, x, params={}):
|
||||
mix = x.detach()
|
||||
x = x.clone()
|
||||
|
||||
@ -96,15 +96,14 @@ class CascadedASPPNet(nn.Module):
|
||||
pad=(0, 0, 0, self.output_bin - aux2.size()[2]),
|
||||
mode='replicate')
|
||||
return mask * mix, aux1 * mix, aux2 * mix
|
||||
else:
|
||||
if aggressiveness:
|
||||
mask[:, :, :aggressiveness['split_bin']] = torch.pow(mask[:, :, :aggressiveness['split_bin']], 1 + aggressiveness['value'] / 3)
|
||||
mask[:, :, aggressiveness['split_bin']:] = torch.pow(mask[:, :, aggressiveness['split_bin']:], 1 + aggressiveness['value'])
|
||||
else:
|
||||
if params.get('is_vocal_model'):
|
||||
return (1.0 - spec_utils.adjust_aggr(mask, params)) * mix
|
||||
|
||||
return spec_utils.adjust_aggr(mask, params) * mix
|
||||
|
||||
return mask * mix
|
||||
|
||||
def predict(self, x_mag, aggressiveness=None):
|
||||
h = self.forward(x_mag, aggressiveness)
|
||||
def predict(self, x_mag, params={}):
|
||||
h = self.forward(x_mag, params)
|
||||
|
||||
if self.offset > 0:
|
||||
h = h[:, :, :, self.offset:-self.offset]
|
||||
|
@ -3,6 +3,7 @@ from torch import nn
|
||||
import torch.nn.functional as F
|
||||
|
||||
from lib import layers_123821KB as layers
|
||||
from lib import spec_utils
|
||||
|
||||
|
||||
class BaseASPPNet(nn.Module):
|
||||
@ -59,7 +60,7 @@ class CascadedASPPNet(nn.Module):
|
||||
|
||||
self.offset = 128
|
||||
|
||||
def forward(self, x, aggressiveness=None):
|
||||
def forward(self, x, params={}):
|
||||
mix = x.detach()
|
||||
x = x.clone()
|
||||
|
||||
@ -96,14 +97,13 @@ class CascadedASPPNet(nn.Module):
|
||||
mode='replicate')
|
||||
return mask * mix, aux1 * mix, aux2 * mix
|
||||
else:
|
||||
if aggressiveness:
|
||||
mask[:, :, :aggressiveness['split_bin']] = torch.pow(mask[:, :, :aggressiveness['split_bin']], 1 + aggressiveness['value'] / 3)
|
||||
mask[:, :, aggressiveness['split_bin']:] = torch.pow(mask[:, :, aggressiveness['split_bin']:], 1 + aggressiveness['value'])
|
||||
if params.get('is_vocal_model'):
|
||||
return (1.0 - spec_utils.adjust_aggr(mask, params)) * mix
|
||||
|
||||
return spec_utils.adjust_aggr(mask, params) * mix
|
||||
|
||||
return mask * mix
|
||||
|
||||
def predict(self, x_mag, aggressiveness=None):
|
||||
h = self.forward(x_mag, aggressiveness)
|
||||
def predict(self, x_mag, params={}):
|
||||
h = self.forward(x_mag, params)
|
||||
|
||||
if self.offset > 0:
|
||||
h = h[:, :, :, self.offset:-self.offset]
|
||||
|
@ -3,6 +3,7 @@ from torch import nn
|
||||
import torch.nn.functional as F
|
||||
|
||||
from lib import layers_33966KB as layers
|
||||
from lib import spec_utils
|
||||
|
||||
|
||||
class BaseASPPNet(nn.Module):
|
||||
@ -59,7 +60,7 @@ class CascadedASPPNet(nn.Module):
|
||||
|
||||
self.offset = 128
|
||||
|
||||
def forward(self, x, aggressiveness=None):
|
||||
def forward(self, x, params={}):
|
||||
mix = x.detach()
|
||||
x = x.clone()
|
||||
|
||||
@ -96,14 +97,13 @@ class CascadedASPPNet(nn.Module):
|
||||
mode='replicate')
|
||||
return mask * mix, aux1 * mix, aux2 * mix
|
||||
else:
|
||||
if aggressiveness:
|
||||
mask[:, :, :aggressiveness['split_bin']] = torch.pow(mask[:, :, :aggressiveness['split_bin']], 1 + aggressiveness['value'] / 3)
|
||||
mask[:, :, aggressiveness['split_bin']:] = torch.pow(mask[:, :, aggressiveness['split_bin']:], 1 + aggressiveness['value'])
|
||||
if params.get('is_vocal_model'):
|
||||
return (1.0 - spec_utils.adjust_aggr(mask, params)) * mix
|
||||
|
||||
return mask * mix
|
||||
return spec_utils.adjust_aggr(mask, params) * mix
|
||||
|
||||
def predict(self, x_mag, aggressiveness=None):
|
||||
h = self.forward(x_mag, aggressiveness)
|
||||
def predict(self, x_mag, params={}):
|
||||
h = self.forward(x_mag, params)
|
||||
|
||||
if self.offset > 0:
|
||||
h = h[:, :, :, self.offset:-self.offset]
|
||||
|
@ -1,9 +1,9 @@
|
||||
import torch
|
||||
import numpy as np
|
||||
from torch import nn
|
||||
import torch.nn.functional as F
|
||||
|
||||
from lib import layers_537238KB as layers
|
||||
from lib import spec_utils
|
||||
|
||||
|
||||
class BaseASPPNet(nn.Module):
|
||||
@ -60,7 +60,7 @@ class CascadedASPPNet(nn.Module):
|
||||
|
||||
self.offset = 128
|
||||
|
||||
def forward(self, x, aggressiveness=None):
|
||||
def forward(self, x, params={}):
|
||||
mix = x.detach()
|
||||
x = x.clone()
|
||||
|
||||
@ -97,14 +97,13 @@ class CascadedASPPNet(nn.Module):
|
||||
mode='replicate')
|
||||
return mask * mix, aux1 * mix, aux2 * mix
|
||||
else:
|
||||
if aggressiveness:
|
||||
mask[:, :, :aggressiveness['split_bin']] = torch.pow(mask[:, :, :aggressiveness['split_bin']], 1 + aggressiveness['value'] / 3)
|
||||
mask[:, :, aggressiveness['split_bin']:] = torch.pow(mask[:, :, aggressiveness['split_bin']:], 1 + aggressiveness['value'])
|
||||
if params.get('is_vocal_model'):
|
||||
return (1.0 - spec_utils.adjust_aggr(mask, params)) * mix
|
||||
|
||||
return spec_utils.adjust_aggr(mask, params) * mix
|
||||
|
||||
return mask * mix
|
||||
|
||||
def predict(self, x_mag, aggressiveness=None):
|
||||
h = self.forward(x_mag, aggressiveness)
|
||||
def predict(self, x_mag, params={}):
|
||||
h = self.forward(x_mag, params)
|
||||
|
||||
if self.offset > 0:
|
||||
h = h[:, :, :, self.offset:-self.offset]
|
||||
|
@ -1,5 +1,6 @@
|
||||
import os
|
||||
import librosa
|
||||
import torch
|
||||
import numpy as np
|
||||
import soundfile as sf
|
||||
import math
|
||||
@ -28,26 +29,10 @@ def crop_center(h1, h2):
|
||||
return h1
|
||||
|
||||
|
||||
def wave_to_spectrogram(wave, hop_length, n_fft, mp, multithreading):
|
||||
if mp.param['reverse']:
|
||||
wave_left = np.flip(np.asfortranarray(wave[0]))
|
||||
wave_right = np.flip(np.asfortranarray(wave[1]))
|
||||
elif mp.param['mid_side_b']:
|
||||
wave_left = np.asfortranarray(np.add(wave[0], wave[1] * .5))
|
||||
wave_right = np.asfortranarray(np.subtract(wave[1], wave[0] * .5))
|
||||
elif mp.param['mid_side_b2']:
|
||||
wave_left = np.asfortranarray(np.add(wave[1], wave[0] * .5))
|
||||
wave_right = np.asfortranarray(np.subtract(wave[0], wave[1] * .5))
|
||||
elif mp.param['mid_side']:
|
||||
wave_left = np.asfortranarray(np.add(wave[0], wave[1]) / 2)
|
||||
wave_right = np.asfortranarray(np.subtract(wave[0], wave[1]))
|
||||
elif mp.param['stereo_w']:
|
||||
wave_left = np.asfortranarray(np.subtract(wave[0], wave[1] * .25))
|
||||
wave_right = np.asfortranarray(np.subtract(wave[1], wave[0] * .25))
|
||||
else:
|
||||
wave_left = np.asfortranarray(wave[0])
|
||||
wave_right = np.asfortranarray(wave[1])
|
||||
|
||||
def wave_to_spectrogram(wave, hop_length, n_fft, mp, multithreading):
|
||||
wave_left = np.asfortranarray(wave[0])
|
||||
wave_right = np.asfortranarray(wave[1])
|
||||
|
||||
if multithreading:
|
||||
def run_thread(**kwargs):
|
||||
global spec_left_mt
|
||||
@ -66,6 +51,33 @@ def wave_to_spectrogram(wave, hop_length, n_fft, mp, multithreading):
|
||||
return spec
|
||||
|
||||
|
||||
def convert_channels(spec, mp, band):
|
||||
cc = mp.param['band'][band].get('convert_channels')
|
||||
|
||||
if mp.param['reverse']:
|
||||
spec_left = np.flip(spec[0])
|
||||
spec_right = np.flip(spec[1])
|
||||
elif mp.param['mid_side_b'] or 'mid_side_b' == cc:
|
||||
spec_left = np.add(spec[0], spec[1] * .5)
|
||||
spec_right = np.subtract(spec[1], spec[0] * .5)
|
||||
elif mp.param['mid_side_b2'] or 'mid_side_b2' == cc:
|
||||
spec_left = np.add(spec[1], spec[0] * .5)
|
||||
spec_right = np.subtract(spec[0], spec[1] * .5)
|
||||
elif 'mid_side_c' == cc:
|
||||
spec_left = np.add(spec[0], spec[1] * .25)
|
||||
spec_right = np.subtract(spec[1], spec[0] * .25)
|
||||
elif mp.param['mid_side'] or 'mid_side' == cc:
|
||||
spec_left = np.add(spec[0], spec[1]) / 2
|
||||
spec_right = np.subtract(spec[0], spec[1])
|
||||
#elif mp.param['stereo_w']:
|
||||
# spec_left = np.subtract(spec[0], spec[1] * .25)
|
||||
# spec_right = np.subtract(spec[1], spec[0] * .25)
|
||||
else:
|
||||
return spec
|
||||
|
||||
return np.asfortranarray([spec_left, spec_right])
|
||||
|
||||
|
||||
def combine_spectrograms(specs, mp):
|
||||
l = min([specs[i].shape[2] for i in specs])
|
||||
spec_c = np.zeros(shape=(2, mp.param['bins'] + 1, l), dtype=np.complex64)
|
||||
@ -74,22 +86,25 @@ def combine_spectrograms(specs, mp):
|
||||
|
||||
for d in range(1, bands_n + 1):
|
||||
h = mp.param['band'][d]['crop_stop'] - mp.param['band'][d]['crop_start']
|
||||
spec_c[:, offset:offset+h, :l] = specs[d][:, mp.param['band'][d]['crop_start']:mp.param['band'][d]['crop_stop'], :l]
|
||||
s = specs[d][:, mp.param['band'][d]['crop_start']:mp.param['band'][d]['crop_stop'], :l]
|
||||
#if 'flip' in mp.param['band'][d]:
|
||||
# s = np.flip(s, 1)
|
||||
spec_c[:, offset:offset+h, :l] = s
|
||||
offset += h
|
||||
|
||||
if offset > mp.param['bins']:
|
||||
raise ValueError('Too much bins')
|
||||
|
||||
# lowpass fiter
|
||||
if mp.param['pre_filter_start'] > 0: # and mp.param['band'][bands_n]['res_type'] in ['scipy', 'polyphase']:
|
||||
if bands_n == 1:
|
||||
spec_c = fft_lp_filter(spec_c, mp.param['pre_filter_start'], mp.param['pre_filter_stop'])
|
||||
else:
|
||||
if mp.param['pre_filter_start'] > 0:
|
||||
#if bands_n == 1:
|
||||
spec_c *= get_lp_filter_mask(spec_c.shape[1], mp.param['pre_filter_start'], mp.param['pre_filter_stop'])
|
||||
'''else:
|
||||
gp = 1
|
||||
for b in range(mp.param['pre_filter_start'] + 1, mp.param['pre_filter_stop']):
|
||||
g = math.pow(10, -(b - mp.param['pre_filter_start']) * (3.5 - gp) / 20.0)
|
||||
gp = g
|
||||
spec_c[:, b, :] *= g
|
||||
'''
|
||||
|
||||
return np.asfortranarray(spec_c)
|
||||
|
||||
@ -167,17 +182,24 @@ def mask_silence(mag, ref, thres=0.2, min_range=64, fade_size=32):
|
||||
return mag
|
||||
|
||||
|
||||
def align_wave_head_and_tail(a, b):
|
||||
l = min([a[0].size, b[0].size])
|
||||
|
||||
return a[:l,:l], b[:l,:l]
|
||||
def trim_specs(a, b):
|
||||
l = min([a.shape[2], b.shape[2]])
|
||||
|
||||
return a[:,:,:l], b[:,:,:l]
|
||||
|
||||
|
||||
def cache_or_load(mix_path, inst_path, mp):
|
||||
mix_basename = os.path.splitext(os.path.basename(mix_path))[0]
|
||||
inst_basename = os.path.splitext(os.path.basename(inst_path))[0]
|
||||
|
||||
# the cache will be common for some model types
|
||||
mpp2 = dict(mp.param)
|
||||
mpp2.update(dict.fromkeys(['mid_side', 'mid_side_b', 'mid_side_b2', 'reverse'], False))
|
||||
|
||||
for d in mpp2['band']:
|
||||
mpp2['band'][d]['convert_channels'] = ''
|
||||
|
||||
cache_dir = 'mph{}'.format(hashlib.sha1(json.dumps(mp.param, sort_keys=True).encode('utf-8')).hexdigest())
|
||||
cache_dir = 'mp{}'.format(hashlib.sha1(json.dumps(mpp2, sort_keys=True).encode('utf-8')).hexdigest())
|
||||
mix_cache_dir = os.path.join('cache', cache_dir)
|
||||
inst_cache_dir = os.path.join('cache', cache_dir)
|
||||
|
||||
@ -191,6 +213,7 @@ def cache_or_load(mix_path, inst_path, mp):
|
||||
X_spec_m = np.load(mix_cache_path)
|
||||
y_spec_m = np.load(inst_cache_path)
|
||||
else:
|
||||
'''
|
||||
X_wave, y_wave, X_spec_s, y_spec_s = {}, {}, {}, {}
|
||||
|
||||
for d in range(len(mp.param['band']), 0, -1):
|
||||
@ -214,6 +237,13 @@ def cache_or_load(mix_path, inst_path, mp):
|
||||
|
||||
X_spec_m = combine_spectrograms(X_spec_s, mp)
|
||||
y_spec_m = combine_spectrograms(y_spec_s, mp)
|
||||
'''
|
||||
|
||||
X_spec_m = spec_from_file(mix_path, mp)
|
||||
y_spec_m = spec_from_file(inst_path, mp)
|
||||
|
||||
X_spec_m, y_spec_m = trim_specs(X_spec_m, y_spec_m)
|
||||
|
||||
|
||||
if X_spec_m.shape != y_spec_m.shape:
|
||||
raise ValueError('The combined spectrograms are different: ' + mix_path)
|
||||
@ -226,70 +256,77 @@ def cache_or_load(mix_path, inst_path, mp):
|
||||
return X_spec_m, y_spec_m
|
||||
|
||||
|
||||
def spectrogram_to_wave(spec, hop_length, mp, multithreading):
|
||||
def spectrogram_to_wave(spec, hop_length, mp, band, multithreading):
|
||||
import threading
|
||||
|
||||
spec_left = np.asfortranarray(spec[0])
|
||||
spec_right = np.asfortranarray(spec[1])
|
||||
cc = mp.param['band'][band].get('convert_channels')
|
||||
|
||||
if multithreading:
|
||||
def run_thread(**kwargs):
|
||||
global wave_left
|
||||
wave_left = librosa.istft(**kwargs)
|
||||
global wave_left_mt
|
||||
wave_left_mt = librosa.istft(**kwargs)
|
||||
|
||||
thread = threading.Thread(target=run_thread, kwargs={'stft_matrix': spec_left, 'hop_length': hop_length})
|
||||
thread.start()
|
||||
wave_right = librosa.istft(spec_right, hop_length=hop_length)
|
||||
thread.join()
|
||||
wave_left = wave_left_mt
|
||||
else:
|
||||
wave_left = librosa.istft(spec_left, hop_length=hop_length)
|
||||
wave_right = librosa.istft(spec_right, hop_length=hop_length)
|
||||
|
||||
if mp.param['reverse']:
|
||||
return np.asfortranarray([np.flip(wave_left), np.flip(wave_right)])
|
||||
elif mp.param['mid_side_b']:
|
||||
elif mp.param['mid_side_b'] or 'mid_side_b' == cc:
|
||||
return np.asfortranarray([np.subtract(wave_left / 1.25, .4 * wave_right), np.add(wave_right / 1.25, .4 * wave_left)])
|
||||
elif mp.param['mid_side_b2']:
|
||||
return np.asfortranarray([np.add(wave_right / 1.25, .4 * wave_left), np.subtract(wave_left / 1.25, .4 * wave_right)])
|
||||
elif mp.param['mid_side']:
|
||||
elif mp.param['mid_side_b2'] or 'mid_side_b2' == cc:
|
||||
return np.asfortranarray([np.add(wave_right / 1.25, .4 * wave_left), np.subtract(wave_left / 1.25, .4 * wave_right)])
|
||||
elif 'mid_side_c' == cc:
|
||||
return np.asfortranarray([np.subtract(wave_left / 1.0625, wave_right / 4.25), np.add(wave_right / 1.0625, wave_left / 4.25)])
|
||||
elif mp.param['mid_side'] or 'mid_side' == cc:
|
||||
return np.asfortranarray([np.add(wave_left, wave_right / 2), np.subtract(wave_left, wave_right / 2)])
|
||||
elif mp.param['stereo_w']:
|
||||
return np.asfortranarray([np.add(wave_left, wave_right * .25) / 0.9375, np.add(wave_right, wave_left * .25) / 0.9375])
|
||||
#elif mp.param['stereo_w']:
|
||||
# return np.asfortranarray([np.add(wave_left, wave_right * .25) / 0.9375, np.add(wave_right, wave_left * .25) / 0.9375])
|
||||
else:
|
||||
return np.asfortranarray([wave_left, wave_right])
|
||||
|
||||
|
||||
|
||||
def cmb_spectrogram_to_wave(spec_m, mp, extra_bins_h=None, extra_bins=None):
|
||||
wave_band = {}
|
||||
bands_n = len(mp.param['band'])
|
||||
offset = 0
|
||||
|
||||
|
||||
for d in range(1, bands_n + 1):
|
||||
bp = mp.param['band'][d]
|
||||
spec_s = np.ndarray(shape=(2, bp['n_fft'] // 2 + 1, spec_m.shape[2]), dtype=complex)
|
||||
spec_s = np.zeros(shape=(2, bp['n_fft'] // 2 + 1, spec_m.shape[2]), dtype=complex)
|
||||
h = bp['crop_stop'] - bp['crop_start']
|
||||
#if 'flip' in mp.param['band'][d]:
|
||||
# spec_s[:, bp['crop_start']:bp['crop_stop'], :] = np.flip(spec_m[:, offset:offset+h, :], 1)
|
||||
#else:
|
||||
spec_s[:, bp['crop_start']:bp['crop_stop'], :] = spec_m[:, offset:offset+h, :]
|
||||
|
||||
offset += h
|
||||
if d == bands_n: # higher
|
||||
if extra_bins_h: # if --high_end_process bypass
|
||||
if d == bands_n: # high-end
|
||||
if extra_bins_h:
|
||||
max_bin = bp['n_fft'] // 2
|
||||
spec_s[:, max_bin-extra_bins_h:max_bin, :] = extra_bins[:, :extra_bins_h, :]
|
||||
if bp['hpf_start'] > 0:
|
||||
spec_s = fft_hp_filter(spec_s, bp['hpf_start'], bp['hpf_stop'] - 1)
|
||||
spec_s *= get_hp_filter_mask(spec_s.shape[1], bp['hpf_start'], bp['hpf_stop'] - 1)
|
||||
if bands_n == 1:
|
||||
wave = spectrogram_to_wave(spec_s, bp['hl'], mp, False)
|
||||
wave = spectrogram_to_wave(spec_s, bp['hl'], mp, d, False)
|
||||
else:
|
||||
wave = np.add(wave, spectrogram_to_wave(spec_s, bp['hl'], mp, False))
|
||||
wave = np.add(wave, spectrogram_to_wave(spec_s, bp['hl'], mp, d, False))
|
||||
else:
|
||||
sr = mp.param['band'][d+1]['sr']
|
||||
if d == 1: # lower
|
||||
spec_s = fft_lp_filter(spec_s, bp['lpf_start'], bp['lpf_stop'])
|
||||
wave = librosa.resample(spectrogram_to_wave(spec_s, bp['hl'], mp, False), bp['sr'], sr, res_type="sinc_fastest")
|
||||
if d == 1: # low-end
|
||||
spec_s *= get_lp_filter_mask(spec_s.shape[1], bp['lpf_start'], bp['lpf_stop'])
|
||||
wave = librosa.resample(spectrogram_to_wave(spec_s, bp['hl'], mp, d, False), bp['sr'], sr, res_type="sinc_fastest")
|
||||
else: # mid
|
||||
spec_s = fft_hp_filter(spec_s, bp['hpf_start'], bp['hpf_stop'] - 1)
|
||||
spec_s = fft_lp_filter(spec_s, bp['lpf_start'], bp['lpf_stop'])
|
||||
wave2 = np.add(wave, spectrogram_to_wave(spec_s, bp['hl'], mp, False))
|
||||
spec_s *= get_hp_filter_mask(spec_s.shape[1], bp['hpf_start'], bp['hpf_stop'] - 1)
|
||||
spec_s *= get_lp_filter_mask(spec_s.shape[1], bp['lpf_start'], bp['lpf_stop'])
|
||||
wave2 = np.add(wave, spectrogram_to_wave(spec_s, bp['hl'], mp, d, False))
|
||||
wave = librosa.resample(wave2, bp['sr'], sr, res_type="sinc_fastest")
|
||||
|
||||
return wave.T
|
||||
@ -304,46 +341,46 @@ def cmb_spectrogram_to_wave_ffmpeg(spec_m, mp, tmp_basename, extra_bins_h=None,
|
||||
|
||||
for d in range(1, bands_n + 1):
|
||||
bp = mp.param['band'][d]
|
||||
spec_s = np.ndarray(shape=(2, bp['n_fft'] // 2 + 1, spec_m.shape[2]), dtype=complex)
|
||||
spec_s = np.zeros(shape=(2, bp['n_fft'] // 2 + 1, spec_m.shape[2]), dtype=complex)
|
||||
h = bp['crop_stop'] - bp['crop_start']
|
||||
spec_s[:, bp['crop_start']:bp['crop_stop'], :] = spec_m[:, offset:offset+h, :]
|
||||
tmp_wav = '{}_cmb_spectrogram_to_wave_b{}_sr{}'.format(tmp_basename, d, str(bp['sr']) + '.wav')
|
||||
tmp_wav2 = '{}_cmb_spectrogram_to_wave_b{}_sr{}'.format(tmp_basename, d, str(mp.param['sr']) + '.wav')
|
||||
tmp_wav = os.path.join('tmp', '{}_cstw_b{}_sr{}'.format(tmp_basename, d, str(bp['sr']) + '.wav'))
|
||||
tmp_wav2 = os.path.join('tmp', '{}_cstw_b{}_sr{}'.format(tmp_basename, d, str(mp.param['sr']) + '.wav'))
|
||||
|
||||
offset += h
|
||||
if d == bands_n: # high-end
|
||||
if extra_bins_h: # if --high_end_process bypass
|
||||
if extra_bins_h:
|
||||
max_bin = bp['n_fft'] // 2
|
||||
spec_s[:, max_bin-extra_bins_h:max_bin, :] = extra_bins[:, :extra_bins_h, :]
|
||||
if bp['hpf_start'] > 0:
|
||||
spec_s = fft_hp_filter(spec_s, bp['hpf_start'], bp['hpf_stop'] - 1)
|
||||
spec_s *= get_hp_filter_mask(spec_s.shape[1], bp['hpf_start'], bp['hpf_stop'] - 1)
|
||||
if bands_n == 1:
|
||||
wave = spectrogram_to_wave(spec_s, bp['hl'], mp, False)
|
||||
wave = spectrogram_to_wave(spec_s, bp['hl'], mp, d, True)
|
||||
else:
|
||||
wave = spectrogram_to_wave(spec_s, bp['hl'], mp, False)
|
||||
wave = spectrogram_to_wave(spec_s, bp['hl'], mp, d, True)
|
||||
else:
|
||||
if d == 1: # lower
|
||||
spec_s = fft_lp_filter(spec_s, bp['lpf_start'], bp['lpf_stop'])
|
||||
if d == 1: # low-end
|
||||
spec_s *= get_lp_filter_mask(spec_s.shape[1], bp['lpf_start'], bp['lpf_stop'])
|
||||
else: # mid
|
||||
spec_s = fft_hp_filter(spec_s, bp['hpf_start'], bp['hpf_stop'] - 1)
|
||||
spec_s = fft_lp_filter(spec_s, bp['lpf_start'], bp['lpf_stop'])
|
||||
spec_s *= get_hp_filter_mask(spec_s.shape[1], bp['hpf_start'], bp['hpf_stop'] - 1)
|
||||
spec_s *= get_lp_filter_mask(spec_s.shape[1], bp['lpf_start'], bp['lpf_stop'])
|
||||
|
||||
sf.write(tmp_wav, spectrogram_to_wave(spec_s, bp['hl'], mp, False).T, bp['sr'])
|
||||
sf.write(tmp_wav, spectrogram_to_wave(spec_s, bp['hl'], mp, d, True).T, bp['sr'])
|
||||
ffmprc[d] = subprocess.Popen(['ffmpeg', '-hide_banner', '-loglevel', 'panic', '-y', '-i', tmp_wav, '-ar', str(mp.param['sr']), '-ac', '2', '-c:a', 'pcm_s16le', tmp_wav2])
|
||||
|
||||
for s in ffmprc:
|
||||
ffmprc[s].communicate()
|
||||
|
||||
for d in range(bands_n - 1, 0, -1):
|
||||
os.remove(f'{tmp_basename}_cmb_spectrogram_to_wave_b{d}_sr' + str(mp.param['band'][d]['sr']) + '.wav')
|
||||
tmp_wav2 = f'{tmp_basename}_cmb_spectrogram_to_wave_b{d}_sr' + str(mp.param['sr']) + '.wav'
|
||||
os.remove(os.path.join('tmp', f'{tmp_basename}_cstw_b{d}_sr' + str(mp.param['band'][d]['sr']) + '.wav'))
|
||||
tmp_wav2 = os.path.join('tmp', f'{tmp_basename}_cstw_b{d}_sr' + str(mp.param['sr']) + '.wav')
|
||||
wave2, _ = librosa.load(tmp_wav2, mp.param['sr'], False, dtype=np.float32, res_type="sinc_fastest")
|
||||
os.remove(tmp_wav2)
|
||||
wave = np.add(wave, wave2)
|
||||
|
||||
return wave.T
|
||||
|
||||
|
||||
'''
|
||||
def fft_lp_filter(spec, bin_start, bin_stop):
|
||||
g = 1.0
|
||||
for b in range(bin_start, bin_stop):
|
||||
@ -364,6 +401,26 @@ def fft_hp_filter(spec, bin_start, bin_stop):
|
||||
spec[:, 0:bin_stop+1, :] *= 0
|
||||
|
||||
return spec
|
||||
'''
|
||||
|
||||
def get_lp_filter_mask(bins_n, bin_start, bin_stop):
|
||||
mask = np.concatenate([
|
||||
np.ones((bin_start - 1, 1)),
|
||||
np.linspace(1, 0, bin_stop - bin_start + 1)[:, None],
|
||||
np.zeros((bins_n - bin_stop, 1))
|
||||
], axis=0)
|
||||
|
||||
return mask
|
||||
|
||||
|
||||
def get_hp_filter_mask(bins_n, bin_start, bin_stop):
|
||||
mask = np.concatenate([
|
||||
np.zeros((bin_stop + 1, 1)),
|
||||
np.linspace(0, 1, 1 + bin_start - bin_stop)[:, None],
|
||||
np.ones((bins_n - bin_start - 2, 1))
|
||||
], axis=0)
|
||||
|
||||
return mask
|
||||
|
||||
|
||||
def mirroring(a, spec_m, input_high_end, mp):
|
||||
@ -378,7 +435,29 @@ def mirroring(a, spec_m, input_high_end, mp):
|
||||
mi = np.multiply(mirror, input_high_end * 1.7)
|
||||
|
||||
return np.where(np.abs(input_high_end) <= np.abs(mi), input_high_end, mi)
|
||||
|
||||
|
||||
def adjust_aggr(mask, params):
|
||||
aggr = params.get('aggr_value', 0.0)
|
||||
|
||||
if aggr != 0:
|
||||
if params.get('is_vocal_model'):
|
||||
aggr = 1 - aggr
|
||||
|
||||
aggr_l = aggr_r = aggr
|
||||
|
||||
if params['aggr_correction'] is not None:
|
||||
aggr_l += params['aggr_correction']['left']
|
||||
aggr_r += params['aggr_correction']['right']
|
||||
|
||||
mask[:, 0, :params['aggr_split_bin']] = torch.pow(mask[:, 0, :params['aggr_split_bin']], 1 + aggr_l / 3)
|
||||
mask[:, 0, params['aggr_split_bin']:] = torch.pow(mask[:, 0, params['aggr_split_bin']:], 1 + aggr_l)
|
||||
|
||||
mask[:, 1, :params['aggr_split_bin']] = torch.pow(mask[:, 1, :params['aggr_split_bin']], 1 + aggr_r / 3)
|
||||
mask[:, 1, params['aggr_split_bin']:] = torch.pow(mask[:, 1, params['aggr_split_bin']:], 1 + aggr_r)
|
||||
|
||||
return mask
|
||||
|
||||
|
||||
def ensembling(a, specs):
|
||||
for i in range(1, len(specs)):
|
||||
@ -397,6 +476,27 @@ def ensembling(a, specs):
|
||||
return spec
|
||||
|
||||
|
||||
def spec_from_file(filename, mp):
|
||||
wave, spec = {}, {}
|
||||
|
||||
for d in range(len(mp.param['band']), 0, -1):
|
||||
bp = mp.param['band'][d]
|
||||
|
||||
if d == len(mp.param['band']): # high-end band
|
||||
wave, _ = librosa.load(
|
||||
filename, bp['sr'], False, dtype=np.float32, res_type=bp['res_type'])
|
||||
|
||||
if len(wave.shape) == 1: # mono to stereo
|
||||
wave = np.array([wave, wave])
|
||||
else: # lower bands
|
||||
wave = librosa.resample(wave, mp.param['band'][d+1]['sr'], bp['sr'], res_type=bp['res_type'])
|
||||
|
||||
spec[d] = wave_to_spectrogram(wave, bp['hl'], bp['n_fft'], mp, False)
|
||||
spec[d] = convert_channels(spec[d], mp, d)
|
||||
|
||||
return combine_spectrograms(spec, mp)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import cv2
|
||||
import sys
|
||||
@ -420,40 +520,20 @@ if __name__ == "__main__":
|
||||
if not args.algorithm.startswith('invert') and len(args.input) < 2:
|
||||
raise ValueError('There must be at least two input files.')
|
||||
|
||||
wave, specs = {}, {}
|
||||
specs = {}
|
||||
mp = ModelParameters(args.model_params)
|
||||
|
||||
for i in range(len(args.input)):
|
||||
spec = {}
|
||||
|
||||
for d in range(len(mp.param['band']), 0, -1):
|
||||
bp = mp.param['band'][d]
|
||||
|
||||
if d == len(mp.param['band']): # high-end band
|
||||
wave[d], _ = librosa.load(
|
||||
args.input[i], bp['sr'], False, dtype=np.float32, res_type=bp['res_type'])
|
||||
|
||||
if len(wave[d].shape) == 1: # mono to stereo
|
||||
wave[d] = np.array([wave[d], wave[d]])
|
||||
else: # lower bands
|
||||
wave[d] = librosa.resample(wave[d+1], mp.param['band'][d+1]['sr'], bp['sr'], res_type=bp['res_type'])
|
||||
|
||||
spec[d] = wave_to_spectrogram(wave[d], bp['hl'], bp['n_fft'], mp, False)
|
||||
|
||||
specs[i] = combine_spectrograms(spec, mp)
|
||||
|
||||
del wave
|
||||
|
||||
for i in range(len(args.input)):
|
||||
specs[i] = spec_from_file(args.input[i], mp)
|
||||
|
||||
specs[0], specs[1] = trim_specs(specs[0], specs[1])
|
||||
|
||||
if args.algorithm == 'deep':
|
||||
d_spec = np.where(np.abs(specs[0]) <= np.abs(spec[1]), specs[0], spec[1])
|
||||
v_spec = d_spec - specs[1]
|
||||
sf.write(os.path.join('{}.wav'.format(args.output_name)), cmb_spectrogram_to_wave(v_spec, mp), mp.param['sr'])
|
||||
|
||||
if args.algorithm.startswith('invert'):
|
||||
ln = min([specs[0].shape[2], specs[1].shape[2]])
|
||||
specs[0] = specs[0][:,:,:ln]
|
||||
specs[1] = specs[1][:,:,:ln]
|
||||
|
||||
if args.algorithm.startswith('invert'):
|
||||
if 'invert_p' == args.algorithm:
|
||||
X_mag = np.abs(specs[0])
|
||||
y_mag = np.abs(specs[1])
|
||||
|
Loading…
Reference in New Issue
Block a user