Update spec_utils.py

This commit is contained in:
aufr33 2021-06-04 18:04:51 +03:00 committed by GitHub
parent 88d48c3d0e
commit 2fd50e11f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -346,7 +346,7 @@ def mirroring(a, spec_m, input_high_end, mp):
def ensembling(a, specs): def ensembling(a, specs):
for i in range(1, len(specs)): for i in range(1, len(specs)):
if i == 1: if i == 1:
spec = specs[i-1] spec = specs[0]
ln = min([spec.shape[2], specs[i].shape[2]]) ln = min([spec.shape[2], specs[i].shape[2]])
spec = spec[:,:,:ln] spec = spec[:,:,:ln]
@ -368,7 +368,7 @@ if __name__ == "__main__":
from model_param_init import ModelParameters from model_param_init import ModelParameters
p = argparse.ArgumentParser() p = argparse.ArgumentParser()
p.add_argument('--algorithm', '-a', type=str, choices=['invert', 'min_mag', 'max_mag', 'deep'], default='min_mag') p.add_argument('--algorithm', '-a', type=str, choices=['invert', 'invert_p', 'min_mag', 'max_mag', 'deep'], default='min_mag')
p.add_argument('--model_params', '-m', type=str, default=os.path.join('modelparams', '1band_sr44100_hl512.json')) p.add_argument('--model_params', '-m', type=str, default=os.path.join('modelparams', '1band_sr44100_hl512.json'))
p.add_argument('--output_name', '-o', type=str, default='output') p.add_argument('--output_name', '-o', type=str, default='output')
p.add_argument('--vocals_only', '-v', action='store_true') p.add_argument('--vocals_only', '-v', action='store_true')
@ -377,19 +377,20 @@ if __name__ == "__main__":
start_time = time.time() start_time = time.time()
if args.algorithm == 'invert' and len(args.input) != 2: if args.algorithm.startswith('invert') and len(args.input) != 2:
raise ValueError('There should be two input files.') raise ValueError('There should be two input files.')
if args.algorithm != 'invert' and len(args.input) < 2: if not args.algorithm.startswith('invert') and len(args.input) < 2:
raise ValueError('There must be at least two input files.') raise ValueError('There must be at least two input files.')
wave, specs = {}, {} wave, specs = {}, {}
mp = ModelParameters(args.model_params) mp = ModelParameters(args.model_params)
for i in range(len(args.input)): for i in range(len(args.input)):
spec = {}
for d in range(len(mp.param['band']), 0, -1): for d in range(len(mp.param['band']), 0, -1):
bp = mp.param['band'][d] bp = mp.param['band'][d]
spec = {}
if d == len(mp.param['band']): # high-end band if d == len(mp.param['band']): # high-end band
wave[d], _ = librosa.load( wave[d], _ = librosa.load(
@ -411,7 +412,17 @@ if __name__ == "__main__":
v_spec = d_spec - specs[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']) sf.write(os.path.join('{}.wav'.format(args.output_name)), cmb_spectrogram_to_wave(v_spec, mp), mp.param['sr'])
if args.algorithm == 'invert': 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 'invert_p' == args.algorithm:
X_mag = np.abs(specs[0])
y_mag = np.abs(specs[1])
max_mag = np.where(X_mag >= y_mag, X_mag, y_mag)
v_spec = specs[1] - max_mag * np.exp(1.j * np.angle(specs[0]))
else:
specs[1] = reduce_vocal_aggressively(specs[0], specs[1], 0.2) specs[1] = reduce_vocal_aggressively(specs[0], specs[1], 0.2)
v_spec = specs[0] - specs[1] v_spec = specs[0] - specs[1]