Delete lib_v5
Browse filesFiles changed (38)
- lib_v5/mdxnet.pyDeleted-136
- lib_v5/mixer.ckptDeleted-3
- lib_v5/modules.pyDeleted-74
- lib_v5/pyrb.pyDeleted-92
- lib_v5/results.pyDeleted-48
- lib_v5/spec_utils.pyDeleted-1241
- lib_v5/tfc_tdf_v3.pyDeleted-253
- lib_v5/vr_network/__init__.pyDeleted-1
- lib_v5/vr_network/layers.pyDeleted-143
- lib_v5/vr_network/layers_new.pyDeleted-126
- lib_v5/vr_network/model_param_init.pyDeleted-32
- lib_v5/vr_network/modelparams/1band_sr16000_hl512.jsonDeleted-19
- lib_v5/vr_network/modelparams/1band_sr32000_hl512.jsonDeleted-19
- lib_v5/vr_network/modelparams/1band_sr33075_hl384.jsonDeleted-19
- lib_v5/vr_network/modelparams/1band_sr44100_hl1024.jsonDeleted-19
- lib_v5/vr_network/modelparams/1band_sr44100_hl256.jsonDeleted-19
- lib_v5/vr_network/modelparams/1band_sr44100_hl512.jsonDeleted-19
- lib_v5/vr_network/modelparams/1band_sr44100_hl512_cut.jsonDeleted-19
- lib_v5/vr_network/modelparams/1band_sr44100_hl512_nf1024.jsonDeleted-19
- lib_v5/vr_network/modelparams/2band_32000.jsonDeleted-30
- lib_v5/vr_network/modelparams/2band_44100_lofi.jsonDeleted-30
- lib_v5/vr_network/modelparams/2band_48000.jsonDeleted-30
- lib_v5/vr_network/modelparams/3band_44100.jsonDeleted-42
- lib_v5/vr_network/modelparams/3band_44100_mid.jsonDeleted-43
- lib_v5/vr_network/modelparams/3band_44100_msb2.jsonDeleted-43
- lib_v5/vr_network/modelparams/4band_44100.jsonDeleted-54
- lib_v5/vr_network/modelparams/4band_44100_mid.jsonDeleted-55
- lib_v5/vr_network/modelparams/4band_44100_msb.jsonDeleted-55
- lib_v5/vr_network/modelparams/4band_44100_msb2.jsonDeleted-55
- lib_v5/vr_network/modelparams/4band_44100_reverse.jsonDeleted-55
- lib_v5/vr_network/modelparams/4band_44100_sw.jsonDeleted-55
- lib_v5/vr_network/modelparams/4band_v2.jsonDeleted-54
- lib_v5/vr_network/modelparams/4band_v2_sn.jsonDeleted-55
- lib_v5/vr_network/modelparams/4band_v3.jsonDeleted-54
- lib_v5/vr_network/modelparams/4band_v3_sn.jsonDeleted-55
- lib_v5/vr_network/modelparams/ensemble.jsonDeleted-43
- lib_v5/vr_network/nets.pyDeleted-166
- lib_v5/vr_network/nets_new.pyDeleted-125
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136import torchimport torch.nn as nnfrom .modules import TFC_TDFfrom pytorch_lightning import LightningModule
dim_s = 4
class AbstractMDXNet(LightningModule): def __init__(self, target_name, lr, optimizer, dim_c, dim_f, dim_t, n_fft, hop_length, overlap): super().__init__() self.target_name = target_name self.lr = lr self.optimizer = optimizer self.dim_c = dim_c self.dim_f = dim_f self.dim_t = dim_t self.n_fft = n_fft self.n_bins = n_fft // 2 + 1 self.hop_length = hop_length self.window = nn.Parameter(torch.hann_window(window_length=self.n_fft, periodic=True), requires_grad=False) self.freq_pad = nn.Parameter(torch.zeros([1, dim_c, self.n_bins - self.dim_f, self.dim_t]), requires_grad=False)
def get_optimizer(self): if self.optimizer == 'rmsprop': return torch.optim.RMSprop(self.parameters(), self.lr) if self.optimizer == 'adamw': return torch.optim.AdamW(self.parameters(), self.lr)
class ConvTDFNet(AbstractMDXNet): def __init__(self, target_name, lr, optimizer, dim_c, dim_f, dim_t, n_fft, hop_length, num_blocks, l, g, k, bn, bias, overlap):
super(ConvTDFNet, self).__init__( target_name, lr, optimizer, dim_c, dim_f, dim_t, n_fft, hop_length, overlap) #self.save_hyperparameters()
self.num_blocks = num_blocks self.l = l self.g = g self.k = k self.bn = bn self.bias = bias
if optimizer == 'rmsprop': norm = nn.BatchNorm2d if optimizer == 'adamw': norm = lambda input:nn.GroupNorm(2, input) self.n = num_blocks // 2 scale = (2, 2)
self.first_conv = nn.Sequential( nn.Conv2d(in_channels=self.dim_c, out_channels=g, kernel_size=(1, 1)), norm(g), nn.ReLU(), )
f = self.dim_f c = g self.encoding_blocks = nn.ModuleList() self.ds = nn.ModuleList() for i in range(self.n): self.encoding_blocks.append(TFC_TDF(c, l, f, k, bn, bias=bias, norm=norm)) self.ds.append( nn.Sequential( nn.Conv2d(in_channels=c, out_channels=c + g, kernel_size=scale, stride=scale), norm(c + g), nn.ReLU() ) ) f = f // 2 c += g
self.bottleneck_block = TFC_TDF(c, l, f, k, bn, bias=bias, norm=norm)
self.decoding_blocks = nn.ModuleList() self.us = nn.ModuleList() for i in range(self.n): self.us.append( nn.Sequential( nn.ConvTranspose2d(in_channels=c, out_channels=c - g, kernel_size=scale, stride=scale), norm(c - g), nn.ReLU() ) ) f = f * 2 c -= g
self.decoding_blocks.append(TFC_TDF(c, l, f, k, bn, bias=bias, norm=norm))
self.final_conv = nn.Sequential( nn.Conv2d(in_channels=c, out_channels=self.dim_c, kernel_size=(1, 1)), )
def forward(self, x):
x = self.first_conv(x)
x = x.transpose(-1, -2)
ds_outputs = [] for i in range(self.n): x = self.encoding_blocks[i](x) ds_outputs.append(x) x = self.ds[i](x)
x = self.bottleneck_block(x)
for i in range(self.n): x = self.us[i](x) x *= ds_outputs[-i - 1] x = self.decoding_blocks[i](x)
x = x.transpose(-1, -2)
x = self.final_conv(x)
return x class Mixer(nn.Module): def __init__(self, device, mixer_path): super(Mixer, self).__init__() self.linear = nn.Linear((dim_s+1)*2, dim_s*2, bias=False) self.load_state_dict( torch.load(mixer_path, map_location=device) )
def forward(self, x): x = x.reshape(1,(dim_s+1)*2,-1).transpose(-1,-2) x = self.linear(x) return x.transpose(-1,-2).reshape(dim_s,2,-1)123version https://git-lfs.github.com/spec/v1oid sha256:ea781bd52c6a523b825fa6cdbb6189f52e318edd8b17e6fe404f76f7af8caa9csize 12081234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import torchimport torch.nn as nn
class TFC(nn.Module): def __init__(self, c, l, k, norm): super(TFC, self).__init__()
self.H = nn.ModuleList() for i in range(l): self.H.append( nn.Sequential( nn.Conv2d(in_channels=c, out_channels=c, kernel_size=k, stride=1, padding=k // 2), norm(c), nn.ReLU(), ) )
def forward(self, x): for h in self.H: x = h(x) return x
class DenseTFC(nn.Module): def __init__(self, c, l, k, norm): super(DenseTFC, self).__init__()
self.conv = nn.ModuleList() for i in range(l): self.conv.append( nn.Sequential( nn.Conv2d(in_channels=c, out_channels=c, kernel_size=k, stride=1, padding=k // 2), norm(c), nn.ReLU(), ) )
def forward(self, x): for layer in self.conv[:-1]: x = torch.cat([layer(x), x], 1) return self.conv[-1](x)
class TFC_TDF(nn.Module): def __init__(self, c, l, f, k, bn, dense=False, bias=True, norm=nn.BatchNorm2d):
super(TFC_TDF, self).__init__()
self.use_tdf = bn is not None
self.tfc = DenseTFC(c, l, k, norm) if dense else TFC(c, l, k, norm)
if self.use_tdf: if bn == 0: self.tdf = nn.Sequential( nn.Linear(f, f, bias=bias), norm(c), nn.ReLU() ) else: self.tdf = nn.Sequential( nn.Linear(f, f // bn, bias=bias), norm(c), nn.ReLU(), nn.Linear(f // bn, f, bias=bias), norm(c), nn.ReLU() )
def forward(self, x): x = self.tfc(x) return x + self.tdf(x) if self.use_tdf else x
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192import osimport subprocessimport tempfileimport siximport numpy as npimport soundfile as sfimport sys
if getattr(sys, 'frozen', False): BASE_PATH_RUB = sys._MEIPASSelse: BASE_PATH_RUB = os.path.dirname(os.path.abspath(__file__))
__all__ = ['time_stretch', 'pitch_shift']
__RUBBERBAND_UTIL = os.path.join(BASE_PATH_RUB, 'rubberband')
if six.PY2: DEVNULL = open(os.devnull, 'w')else: DEVNULL = subprocess.DEVNULL
def __rubberband(y, sr, **kwargs):
assert sr > 0
# Get the input and output tempfile fd, infile = tempfile.mkstemp(suffix='.wav') os.close(fd) fd, outfile = tempfile.mkstemp(suffix='.wav') os.close(fd)
# dump the audio sf.write(infile, y, sr)
try: # Execute rubberband arguments = [__RUBBERBAND_UTIL, '-q']
for key, value in six.iteritems(kwargs): arguments.append(str(key)) arguments.append(str(value))
arguments.extend([infile, outfile])
subprocess.check_call(arguments, stdout=DEVNULL, stderr=DEVNULL)
# Load the processed audio. y_out, _ = sf.read(outfile, always_2d=True)
# make sure that output dimensions matches input if y.ndim == 1: y_out = np.squeeze(y_out)
except OSError as exc: six.raise_from(RuntimeError('Failed to execute rubberband. ' 'Please verify that rubberband-cli ' 'is installed.'), exc)
finally: # Remove temp files os.unlink(infile) os.unlink(outfile)
return y_out
def time_stretch(y, sr, rate, rbargs=None): if rate <= 0: raise ValueError('rate must be strictly positive')
if rate == 1.0: return y
if rbargs is None: rbargs = dict()
rbargs.setdefault('--tempo', rate)
return __rubberband(y, sr, **rbargs)
def pitch_shift(y, sr, n_steps, rbargs=None):
if n_steps == 0: return y
if rbargs is None: rbargs = dict()
rbargs.setdefault('--pitch', n_steps)
return __rubberband(y, sr, **rbargs)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748# -*- coding: utf-8 -*-
"""Matchering - Audio Matching and Mastering Python LibraryCopyright (C) 2016-2022 Sergree
This program is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.
This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.
You should have received a copy of the GNU General Public Licensealong with this program. If not, see <https://www.gnu.org/licenses/>."""
import osimport soundfile as sf
class Result: def __init__( self, file: str, subtype: str, use_limiter: bool = True, normalize: bool = True ): _, file_ext = os.path.splitext(file) file_ext = file_ext[1:].upper() if not sf.check_format(file_ext): raise TypeError(f"{file_ext} format is not supported") if not sf.check_format(file_ext, subtype): raise TypeError(f"{file_ext} format does not have {subtype} subtype") self.file = file self.subtype = subtype self.use_limiter = use_limiter self.normalize = normalize
def pcm16(file: str) -> Result: return Result(file, "PCM_16")
def pcm24(file: str) -> Result: return Result(file, "FLOAT")
def save_audiofile(file: str, wav_set="PCM_16") -> Result: return Result(file, wav_set)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241import audioreadimport librosaimport numpy as npimport soundfile as sfimport mathimport platformimport tracebackfrom . import pyrbfrom scipy.signal import correlate, hilbertimport io
OPERATING_SYSTEM = platform.system()SYSTEM_ARCH = platform.platform()SYSTEM_PROC = platform.processor()ARM = 'arm'
AUTO_PHASE = "Automatic"POSITIVE_PHASE = "Positive Phase"NEGATIVE_PHASE = "Negative Phase"NONE_P = "None",LOW_P = "Shifts: Low",MED_P = "Shifts: Medium",HIGH_P = "Shifts: High",VHIGH_P = "Shifts: Very High"MAXIMUM_P = "Shifts: Maximum"
progress_value = 0last_update_time = 0is_macos = False
if OPERATING_SYSTEM == 'Windows': from pyrubberband import pyrbelse: from . import pyrb
if OPERATING_SYSTEM == 'Darwin': wav_resolution = "polyphase" if SYSTEM_PROC == ARM or ARM in SYSTEM_ARCH else "sinc_fastest" wav_resolution_float_resampling = "kaiser_best" if SYSTEM_PROC == ARM or ARM in SYSTEM_ARCH else wav_resolution is_macos = Trueelse: wav_resolution = "sinc_fastest" wav_resolution_float_resampling = wav_resolution
MAX_SPEC = 'Max Spec'MIN_SPEC = 'Min Spec'LIN_ENSE = 'Linear Ensemble'
MAX_WAV = MAX_SPECMIN_WAV = MIN_SPEC
AVERAGE = 'Average'
def crop_center(h1, h2): h1_shape = h1.size() h2_shape = h2.size()
if h1_shape[3] == h2_shape[3]: return h1 elif h1_shape[3] < h2_shape[3]: raise ValueError('h1_shape[3] must be greater than h2_shape[3]')
s_time = (h1_shape[3] - h2_shape[3]) // 2 e_time = s_time + h2_shape[3] h1 = h1[:, :, :, s_time:e_time]
return h1
def preprocess(X_spec): X_mag = np.abs(X_spec) X_phase = np.angle(X_spec)
return X_mag, X_phase
def make_padding(width, cropsize, offset): left = offset roi_size = cropsize - offset * 2 if roi_size == 0: roi_size = cropsize right = roi_size - (width % roi_size) + left
return left, right, roi_size
def normalize(wave, is_normalize=False): """Normalize audio"""
maxv = np.abs(wave).max() if maxv > 1.0: if is_normalize: print("Above clipping threshold.") wave /= maxv return wave def auto_transpose(audio_array:np.ndarray): """ Ensure that the audio array is in the (channels, samples) format.
Parameters: audio_array (ndarray): Input audio array.
Returns: ndarray: Transposed audio array if necessary. """ # If the second dimension is 2 (indicating stereo channels), transpose the array if audio_array.shape[1] == 2: return audio_array.T return audio_array
def write_array_to_mem(audio_data, subtype): if isinstance(audio_data, np.ndarray): audio_buffer = io.BytesIO() sf.write(audio_buffer, audio_data, 44100, subtype=subtype, format='WAV') audio_buffer.seek(0) return audio_buffer else: return audio_data
def spectrogram_to_image(spec, mode='magnitude'): if mode == 'magnitude': if np.iscomplexobj(spec): y = np.abs(spec) else: y = spec y = np.log10(y ** 2 + 1e-8) elif mode == 'phase': if np.iscomplexobj(spec): y = np.angle(spec) else: y = spec
y -= y.min() y *= 255 / y.max() img = np.uint8(y)
if y.ndim == 3: img = img.transpose(1, 2, 0) img = np.concatenate([ np.max(img, axis=2, keepdims=True), img ], axis=2)
return img
def reduce_vocal_aggressively(X, y, softmask): v = X - y y_mag_tmp = np.abs(y) v_mag_tmp = np.abs(v)
v_mask = v_mag_tmp > y_mag_tmp y_mag = np.clip(y_mag_tmp - v_mag_tmp * v_mask * softmask, 0, np.inf)
return y_mag * np.exp(1.j * np.angle(y))
def merge_artifacts(y_mask, thres=0.01, min_range=64, fade_size=32): mask = y_mask try: if min_range < fade_size * 2: raise ValueError('min_range must be >= fade_size * 2')
idx = np.where(y_mask.min(axis=(0, 1)) > thres)[0] start_idx = np.insert(idx[np.where(np.diff(idx) != 1)[0] + 1], 0, idx[0]) end_idx = np.append(idx[np.where(np.diff(idx) != 1)[0]], idx[-1]) artifact_idx = np.where(end_idx - start_idx > min_range)[0] weight = np.zeros_like(y_mask) if len(artifact_idx) > 0: start_idx = start_idx[artifact_idx] end_idx = end_idx[artifact_idx] old_e = None for s, e in zip(start_idx, end_idx): if old_e is not None and s - old_e < fade_size: s = old_e - fade_size * 2
if s != 0: weight[:, :, s:s + fade_size] = np.linspace(0, 1, fade_size) else: s -= fade_size
if e != y_mask.shape[2]: weight[:, :, e - fade_size:e] = np.linspace(1, 0, fade_size) else: e += fade_size
weight[:, :, s + fade_size:e - fade_size] = 1 old_e = e
v_mask = 1 - y_mask y_mask += weight * v_mask mask = y_mask except Exception as e: error_name = f'{type(e).__name__}' traceback_text = ''.join(traceback.format_tb(e.__traceback__)) message = f'{error_name}: "{e}"\n{traceback_text}"' print('Post Process Failed: ', message) return mask
def align_wave_head_and_tail(a, b): l = min([a[0].size, b[0].size]) return a[:l,:l], b[:l,:l] def convert_channels(spec, mp, band): cc = mp.param['band'][band].get('convert_channels')
if 'mid_side_c' == cc: spec_left = np.add(spec[0], spec[1] * .25) spec_right = np.subtract(spec[1], spec[0] * .25) elif 'mid_side' == cc: spec_left = np.add(spec[0], spec[1]) / 2 spec_right = np.subtract(spec[0], spec[1]) elif 'stereo_n' == cc: spec_left = np.add(spec[0], spec[1] * .25) / 0.9375 spec_right = np.add(spec[1], spec[0] * .25) / 0.9375 else: return spec return np.asfortranarray([spec_left, spec_right]) def combine_spectrograms(specs, mp, is_v51_model=False): l = min([specs[i].shape[2] for i in specs]) spec_c = np.zeros(shape=(2, mp.param['bins'] + 1, l), dtype=np.complex64) offset = 0 bands_n = len(mp.param['band']) 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] offset += h if offset > mp.param['bins']: raise ValueError('Too much bins') # lowpass fiter if mp.param['pre_filter_start'] > 0: if is_v51_model: spec_c *= get_lp_filter_mask(spec_c.shape[1], mp.param['pre_filter_start'], mp.param['pre_filter_stop']) else: if bands_n == 1: spec_c = fft_lp_filter(spec_c, 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) def wave_to_spectrogram(wave, hop_length, n_fft, mp, band, is_v51_model=False):
if wave.ndim == 1: wave = np.asfortranarray([wave,wave])
if not is_v51_model: 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']: wave_left = np.asfortranarray(np.add(wave[0], wave[1]) / 2) wave_right = np.asfortranarray(np.subtract(wave[0], wave[1])) 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)) else: wave_left = np.asfortranarray(wave[0]) wave_right = np.asfortranarray(wave[1]) else: wave_left = np.asfortranarray(wave[0]) wave_right = np.asfortranarray(wave[1])
spec_left = librosa.stft(wave_left, n_fft, hop_length=hop_length) spec_right = librosa.stft(wave_right, n_fft, hop_length=hop_length) spec = np.asfortranarray([spec_left, spec_right])
if is_v51_model: spec = convert_channels(spec, mp, band)
return spec
def spectrogram_to_wave(spec, hop_length=1024, mp={}, band=0, is_v51_model=True): spec_left = np.asfortranarray(spec[0]) spec_right = np.asfortranarray(spec[1]) wave_left = librosa.istft(spec_left, hop_length=hop_length) wave_right = librosa.istft(spec_right, hop_length=hop_length) if is_v51_model: cc = mp.param['band'][band].get('convert_channels') if '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 'mid_side' == cc: return np.asfortranarray([np.add(wave_left, wave_right / 2), np.subtract(wave_left, wave_right / 2)]) elif 'stereo_n' == cc: return np.asfortranarray([np.subtract(wave_left, wave_right * .25), np.subtract(wave_right, wave_left * .25)]) else: if mp.param['reverse']: return np.asfortranarray([np.flip(wave_left), np.flip(wave_right)]) elif mp.param['mid_side']: return np.asfortranarray([np.add(wave_left, wave_right / 2), np.subtract(wave_left, wave_right / 2)]) 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)]) return np.asfortranarray([wave_left, wave_right]) def cmb_spectrogram_to_wave(spec_m, mp, extra_bins_h=None, extra_bins=None, is_v51_model=False): 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) h = bp['crop_stop'] - bp['crop_start'] 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 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: if is_v51_model: spec_s *= get_hp_filter_mask(spec_s.shape[1], bp['hpf_start'], bp['hpf_stop'] - 1) else: spec_s = fft_hp_filter(spec_s, bp['hpf_start'], bp['hpf_stop'] - 1) if bands_n == 1: wave = spectrogram_to_wave(spec_s, bp['hl'], mp, d, is_v51_model) else: wave = np.add(wave, spectrogram_to_wave(spec_s, bp['hl'], mp, d, is_v51_model)) else: sr = mp.param['band'][d+1]['sr'] if d == 1: # lower if is_v51_model: spec_s *= get_lp_filter_mask(spec_s.shape[1], bp['lpf_start'], bp['lpf_stop']) else: spec_s = fft_lp_filter(spec_s, bp['lpf_start'], bp['lpf_stop']) wave = librosa.resample(spectrogram_to_wave(spec_s, bp['hl'], mp, d, is_v51_model), bp['sr'], sr, res_type=wav_resolution) else: # mid if is_v51_model: 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']) else: 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, d, is_v51_model)) wave = librosa.resample(wave2, bp['sr'], sr, res_type=wav_resolution) return wave
def get_lp_filter_mask(n_bins, 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((n_bins - bin_stop, 1)) ], axis=0)
return mask def get_hp_filter_mask(n_bins, 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((n_bins - bin_start - 2, 1)) ], axis=0)
return mask
def fft_lp_filter(spec, bin_start, bin_stop): g = 1.0 for b in range(bin_start, bin_stop): g -= 1 / (bin_stop - bin_start) spec[:, b, :] = g * spec[:, b, :] spec[:, bin_stop:, :] *= 0
return spec
def fft_hp_filter(spec, bin_start, bin_stop): g = 1.0 for b in range(bin_start, bin_stop, -1): g -= 1 / (bin_start - bin_stop) spec[:, b, :] = g * spec[:, b, :] spec[:, 0:bin_stop+1, :] *= 0
return spec
def spectrogram_to_wave_old(spec, hop_length=1024): if spec.ndim == 2: wave = librosa.istft(spec, hop_length=hop_length) elif spec.ndim == 3: spec_left = np.asfortranarray(spec[0]) spec_right = np.asfortranarray(spec[1])
wave_left = librosa.istft(spec_left, hop_length=hop_length) wave_right = librosa.istft(spec_right, hop_length=hop_length) wave = np.asfortranarray([wave_left, wave_right])
return wave def wave_to_spectrogram_old(wave, hop_length, n_fft): wave_left = np.asfortranarray(wave[0]) wave_right = np.asfortranarray(wave[1])
spec_left = librosa.stft(wave_left, n_fft, hop_length=hop_length) spec_right = librosa.stft(wave_right, n_fft, hop_length=hop_length) spec = np.asfortranarray([spec_left, spec_right])
return spec
def mirroring(a, spec_m, input_high_end, mp): if 'mirroring' == a: mirror = np.flip(np.abs(spec_m[:, mp.param['pre_filter_start']-10-input_high_end.shape[1]:mp.param['pre_filter_start']-10, :]), 1) mirror = mirror * np.exp(1.j * np.angle(input_high_end)) return np.where(np.abs(input_high_end) <= np.abs(mirror), input_high_end, mirror) if 'mirroring2' == a: mirror = np.flip(np.abs(spec_m[:, mp.param['pre_filter_start']-10-input_high_end.shape[1]:mp.param['pre_filter_start']-10, :]), 1) 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, is_non_accom_stem, aggressiveness): aggr = aggressiveness['value'] * 2
if aggr != 0: if is_non_accom_stem: aggr = 1 - aggr aggr = [aggr, aggr] if aggressiveness['aggr_correction'] is not None: aggr[0] += aggressiveness['aggr_correction']['left'] aggr[1] += aggressiveness['aggr_correction']['right']
for ch in range(2): mask[ch, :aggressiveness['split_bin']] = np.power(mask[ch, :aggressiveness['split_bin']], 1 + aggr[ch] / 3) mask[ch, aggressiveness['split_bin']:] = np.power(mask[ch, aggressiveness['split_bin']:], 1 + aggr[ch])
return mask
def stft(wave, nfft, hl): wave_left = np.asfortranarray(wave[0]) wave_right = np.asfortranarray(wave[1]) spec_left = librosa.stft(wave_left, nfft, hop_length=hl) spec_right = librosa.stft(wave_right, nfft, hop_length=hl) spec = np.asfortranarray([spec_left, spec_right])
return spec
def istft(spec, hl): spec_left = np.asfortranarray(spec[0]) spec_right = np.asfortranarray(spec[1]) wave_left = librosa.istft(spec_left, hop_length=hl) wave_right = librosa.istft(spec_right, hop_length=hl) wave = np.asfortranarray([wave_left, wave_right])
return wave
def spec_effects(wave, algorithm='Default', value=None): spec = [stft(wave[0],2048,1024), stft(wave[1],2048,1024)] if algorithm == 'Min_Mag': v_spec_m = np.where(np.abs(spec[1]) <= np.abs(spec[0]), spec[1], spec[0]) wave = istft(v_spec_m,1024) elif algorithm == 'Max_Mag': v_spec_m = np.where(np.abs(spec[1]) >= np.abs(spec[0]), spec[1], spec[0]) wave = istft(v_spec_m,1024) elif algorithm == 'Default': wave = (wave[1] * value) + (wave[0] * (1-value)) elif algorithm == 'Invert_p': X_mag = np.abs(spec[0]) y_mag = np.abs(spec[1]) max_mag = np.where(X_mag >= y_mag, X_mag, y_mag) v_spec = spec[1] - max_mag * np.exp(1.j * np.angle(spec[0])) wave = istft(v_spec,1024) return wave
def spectrogram_to_wave_no_mp(spec, n_fft=2048, hop_length=1024): wave = librosa.istft(spec, n_fft=n_fft, hop_length=hop_length) if wave.ndim == 1: wave = np.asfortranarray([wave,wave])
return wave
def wave_to_spectrogram_no_mp(wave): spec = librosa.stft(wave, n_fft=2048, hop_length=1024) if spec.ndim == 1: spec = np.asfortranarray([spec,spec])
return spec
def invert_audio(specs, invert_p=True): ln = min([specs[0].shape[2], specs[1].shape[2]]) specs[0] = specs[0][:,:,:ln] specs[1] = specs[1][:,:,:ln] if invert_p: 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) v_spec = specs[0] - specs[1]
return v_spec
def invert_stem(mixture, stem): mixture = wave_to_spectrogram_no_mp(mixture) stem = wave_to_spectrogram_no_mp(stem) output = spectrogram_to_wave_no_mp(invert_audio([mixture, stem]))
return -output.T
def ensembling(a, inputs, is_wavs=False):
for i in range(1, len(inputs)): if i == 1: input = inputs[0]
if is_wavs: ln = min([input.shape[1], inputs[i].shape[1]]) input = input[:,:ln] inputs[i] = inputs[i][:,:ln] else: ln = min([input.shape[2], inputs[i].shape[2]]) input = input[:,:,:ln] inputs[i] = inputs[i][:,:,:ln] if MIN_SPEC == a: input = np.where(np.abs(inputs[i]) <= np.abs(input), inputs[i], input) if MAX_SPEC == a: input = np.where(np.abs(inputs[i]) >= np.abs(input), inputs[i], input)
#linear_ensemble #input = ensemble_wav(inputs, split_size=1)
return input
def ensemble_for_align(waves): specs = [] for wav in waves: spec = wave_to_spectrogram_no_mp(wav.T) specs.append(spec) wav_aligned = spectrogram_to_wave_no_mp(ensembling(MIN_SPEC, specs)).T wav_aligned = match_array_shapes(wav_aligned, waves[1], is_swap=True) return wav_aligned def ensemble_inputs(audio_input, algorithm, is_normalization, wav_type_set, save_path, is_wave=False, is_array=False):
wavs_ = [] if algorithm == AVERAGE: output = average_audio(audio_input) samplerate = 44100 else: specs = [] for i in range(len(audio_input)): wave, samplerate = librosa.load(audio_input[i], mono=False, sr=44100) wavs_.append(wave) spec = wave if is_wave else wave_to_spectrogram_no_mp(wave) specs.append(spec) wave_shapes = [w.shape[1] for w in wavs_] target_shape = wavs_[wave_shapes.index(max(wave_shapes))] if is_wave: output = ensembling(algorithm, specs, is_wavs=True) else: output = spectrogram_to_wave_no_mp(ensembling(algorithm, specs)) output = to_shape(output, target_shape.shape)
sf.write(save_path, normalize(output.T, is_normalization), samplerate, subtype=wav_type_set)
def to_shape(x, target_shape): padding_list = [] for x_dim, target_dim in zip(x.shape, target_shape): pad_value = (target_dim - x_dim) pad_tuple = ((0, pad_value)) padding_list.append(pad_tuple) return np.pad(x, tuple(padding_list), mode='constant')
def to_shape_minimize(x: np.ndarray, target_shape): padding_list = [] for x_dim, target_dim in zip(x.shape, target_shape): pad_value = (target_dim - x_dim) pad_tuple = ((0, pad_value)) padding_list.append(pad_tuple) return np.pad(x, tuple(padding_list), mode='constant')
def detect_leading_silence(audio, sr, silence_threshold=0.007, frame_length=1024): """ Detect silence at the beginning of an audio signal.
:param audio: np.array, audio signal :param sr: int, sample rate :param silence_threshold: float, magnitude threshold below which is considered silence :param frame_length: int, the number of samples to consider for each check
:return: float, duration of the leading silence in milliseconds """ if len(audio.shape) == 2: # If stereo, pick the channel with more energy to determine the silence channel = np.argmax(np.sum(np.abs(audio), axis=1)) audio = audio[channel] for i in range(0, len(audio), frame_length): if np.max(np.abs(audio[i:i+frame_length])) > silence_threshold: return (i / sr) * 1000
return (len(audio) / sr) * 1000
def adjust_leading_silence(target_audio, reference_audio, silence_threshold=0.01, frame_length=1024): """ Adjust the leading silence of the target_audio to match the leading silence of the reference_audio.
:param target_audio: np.array, audio signal that will have its silence adjusted :param reference_audio: np.array, audio signal used as a reference :param sr: int, sample rate :param silence_threshold: float, magnitude threshold below which is considered silence :param frame_length: int, the number of samples to consider for each check
:return: np.array, target_audio adjusted to have the same leading silence as reference_audio """ def find_silence_end(audio): if len(audio.shape) == 2: # If stereo, pick the channel with more energy to determine the silence channel = np.argmax(np.sum(np.abs(audio), axis=1)) audio_mono = audio[channel] else: audio_mono = audio
for i in range(0, len(audio_mono), frame_length): if np.max(np.abs(audio_mono[i:i+frame_length])) > silence_threshold: return i return len(audio_mono)
ref_silence_end = find_silence_end(reference_audio) target_silence_end = find_silence_end(target_audio) silence_difference = ref_silence_end - target_silence_end
try: ref_silence_end_p = (ref_silence_end / 44100) * 1000 target_silence_end_p = (target_silence_end / 44100) * 1000 silence_difference_p = ref_silence_end_p - target_silence_end_p print("silence_difference: ", silence_difference_p) except Exception as e: pass
if silence_difference > 0: # Add silence to target_audio if len(target_audio.shape) == 2: # stereo silence_to_add = np.zeros((target_audio.shape[0], silence_difference)) else: # mono silence_to_add = np.zeros(silence_difference) return np.hstack((silence_to_add, target_audio)) elif silence_difference < 0: # Remove silence from target_audio if len(target_audio.shape) == 2: # stereo return target_audio[:, -silence_difference:] else: # mono return target_audio[-silence_difference:] else: # No adjustment needed return target_audio
def match_array_shapes(array_1:np.ndarray, array_2:np.ndarray, is_swap=False): if is_swap: array_1, array_2 = array_1.T, array_2.T #print("before", array_1.shape, array_2.shape) if array_1.shape[1] > array_2.shape[1]: array_1 = array_1[:,:array_2.shape[1]] elif array_1.shape[1] < array_2.shape[1]: padding = array_2.shape[1] - array_1.shape[1] array_1 = np.pad(array_1, ((0,0), (0,padding)), 'constant', constant_values=0) #print("after", array_1.shape, array_2.shape) if is_swap: array_1, array_2 = array_1.T, array_2.T return array_1
def match_mono_array_shapes(array_1: np.ndarray, array_2: np.ndarray): if len(array_1) > len(array_2): array_1 = array_1[:len(array_2)] elif len(array_1) < len(array_2): padding = len(array_2) - len(array_1) array_1 = np.pad(array_1, (0, padding), 'constant', constant_values=0) return array_1
def change_pitch_semitones(y, sr, semitone_shift): factor = 2 ** (semitone_shift / 12) # Convert semitone shift to factor for resampling y_pitch_tuned = [] for y_channel in y: y_pitch_tuned.append(librosa.resample(y_channel, sr, sr*factor, res_type=wav_resolution_float_resampling)) y_pitch_tuned = np.array(y_pitch_tuned) new_sr = sr * factor return y_pitch_tuned, new_sr
def augment_audio(export_path, audio_file, rate, is_normalization, wav_type_set, save_format=None, is_pitch=False, is_time_correction=True):
wav, sr = librosa.load(audio_file, sr=44100, mono=False)
if wav.ndim == 1: wav = np.asfortranarray([wav,wav])
if not is_time_correction: wav_mix = change_pitch_semitones(wav, 44100, semitone_shift=-rate)[0] else: if is_pitch: wav_1 = pyrb.pitch_shift(wav[0], sr, rate, rbargs=None) wav_2 = pyrb.pitch_shift(wav[1], sr, rate, rbargs=None) else: wav_1 = pyrb.time_stretch(wav[0], sr, rate, rbargs=None) wav_2 = pyrb.time_stretch(wav[1], sr, rate, rbargs=None)
if wav_1.shape > wav_2.shape: wav_2 = to_shape(wav_2, wav_1.shape) if wav_1.shape < wav_2.shape: wav_1 = to_shape(wav_1, wav_2.shape) wav_mix = np.asfortranarray([wav_1, wav_2]) sf.write(export_path, normalize(wav_mix.T, is_normalization), sr, subtype=wav_type_set) save_format(export_path) def average_audio(audio): waves = [] wave_shapes = [] final_waves = []
for i in range(len(audio)): wave = librosa.load(audio[i], sr=44100, mono=False) waves.append(wave[0]) wave_shapes.append(wave[0].shape[1])
wave_shapes_index = wave_shapes.index(max(wave_shapes)) target_shape = waves[wave_shapes_index] waves.pop(wave_shapes_index) final_waves.append(target_shape)
for n_array in waves: wav_target = to_shape(n_array, target_shape.shape) final_waves.append(wav_target)
waves = sum(final_waves) waves = waves/len(audio)
return waves def average_dual_sources(wav_1, wav_2, value): if wav_1.shape > wav_2.shape: wav_2 = to_shape(wav_2, wav_1.shape) if wav_1.shape < wav_2.shape: wav_1 = to_shape(wav_1, wav_2.shape)
wave = (wav_1 * value) + (wav_2 * (1-value))
return wave def reshape_sources(wav_1: np.ndarray, wav_2: np.ndarray): if wav_1.shape > wav_2.shape: wav_2 = to_shape(wav_2, wav_1.shape) if wav_1.shape < wav_2.shape: ln = min([wav_1.shape[1], wav_2.shape[1]]) wav_2 = wav_2[:,:ln]
ln = min([wav_1.shape[1], wav_2.shape[1]]) wav_1 = wav_1[:,:ln] wav_2 = wav_2[:,:ln]
return wav_2 def reshape_sources_ref(wav_1_shape, wav_2: np.ndarray): if wav_1_shape > wav_2.shape: wav_2 = to_shape(wav_2, wav_1_shape)
return wav_2 def combine_arrarys(audio_sources, is_swap=False): source = np.zeros_like(max(audio_sources, key=np.size)) for v in audio_sources: v = match_array_shapes(v, source, is_swap=is_swap) source += v return source def combine_audio(paths: list, audio_file_base=None, wav_type_set='FLOAT', save_format=None): source = combine_arrarys([load_audio(i) for i in paths]) save_path = f"{audio_file_base}_combined.wav" sf.write(save_path, source.T, 44100, subtype=wav_type_set) save_format(save_path) def reduce_mix_bv(inst_source, voc_source, reduction_rate=0.9): # Reduce the volume inst_source = inst_source * (1 - reduction_rate)
mix_reduced = combine_arrarys([inst_source, voc_source], is_swap=True)
return mix_reduced def organize_inputs(inputs): input_list = { "target":None, "reference":None, "reverb":None, "inst":None } for i in inputs: if i.endswith("_(Vocals).wav"): input_list["reference"] = i elif "_RVC_" in i: input_list["target"] = i elif i.endswith("reverbed_stem.wav"): input_list["reverb"] = i elif i.endswith("_(Instrumental).wav"): input_list["inst"] = i return input_list def check_if_phase_inverted(wav1, wav2, is_mono=False): # Load the audio files if not is_mono: wav1 = np.mean(wav1, axis=0) wav2 = np.mean(wav2, axis=0) # Compute the correlation correlation = np.corrcoef(wav1[:1000], wav2[:1000]) return correlation[0,1] < 0 def align_audio(file1, file2, file2_aligned, file_subtracted, wav_type_set, is_save_aligned, command_Text, save_format, align_window:list, align_intro_val:list, db_analysis:tuple, set_progress_bar, phase_option, phase_shifts, is_match_silence, is_spec_match): global progress_value progress_value = 0 is_mono = False def get_diff(a, b): corr = np.correlate(a, b, "full") diff = corr.argmax() - (b.shape[0] - 1)
return diff
def progress_bar(length): global progress_value progress_value += 1
if (0.90/length*progress_value) >= 0.9: length = progress_value + 1
set_progress_bar(0.1, (0.9/length*progress_value)) # read tracks if file1.endswith(".mp3") and is_macos: length1 = rerun_mp3(file1) wav1, sr1 = librosa.load(file1, duration=length1, sr=44100, mono=False) else: wav1, sr1 = librosa.load(file1, sr=44100, mono=False)
if file2.endswith(".mp3") and is_macos: length2 = rerun_mp3(file2) wav2, sr2 = librosa.load(file2, duration=length2, sr=44100, mono=False) else: wav2, sr2 = librosa.load(file2, sr=44100, mono=False)
if wav1.ndim == 1 and wav2.ndim == 1: is_mono = True elif wav1.ndim == 1: wav1 = np.asfortranarray([wav1,wav1]) elif wav2.ndim == 1: wav2 = np.asfortranarray([wav2,wav2]) # Check if phase is inverted if phase_option == AUTO_PHASE: if check_if_phase_inverted(wav1, wav2, is_mono=is_mono): wav2 = -wav2 elif phase_option == POSITIVE_PHASE: wav2 = +wav2 elif phase_option == NEGATIVE_PHASE: wav2 = -wav2 if is_match_silence: wav2 = adjust_leading_silence(wav2, wav1) wav1_length = int(librosa.get_duration(y=wav1, sr=44100)) wav2_length = int(librosa.get_duration(y=wav2, sr=44100)) if not is_mono: wav1 = wav1.transpose() wav2 = wav2.transpose()
wav2_org = wav2.copy() command_Text("Processing files... \n") seconds_length = min(wav1_length, wav2_length) wav2_aligned_sources = [] for sec_len in align_intro_val: # pick a position at 1 second in and get diff sec_seg = 1 if sec_len == 1 else int(seconds_length // sec_len) index = sr1*sec_seg # 1 second in, assuming sr1 = sr2 = 44100
if is_mono: samp1, samp2 = wav1[index : index + sr1], wav2[index : index + sr1] diff = get_diff(samp1, samp2) #print(f"Estimated difference: {diff}\n") else: index = sr1*sec_seg # 1 second in, assuming sr1 = sr2 = 44100 samp1, samp2 = wav1[index : index + sr1, 0], wav2[index : index + sr1, 0] samp1_r, samp2_r = wav1[index : index + sr1, 1], wav2[index : index + sr1, 1] diff, diff_r = get_diff(samp1, samp2), get_diff(samp1_r, samp2_r) #print(f"Estimated difference Left Channel: {diff}\nEstimated difference Right Channel: {diff_r}\n") # make aligned track 2 if diff > 0: zeros_to_append = np.zeros(diff) if is_mono else np.zeros((diff, 2)) wav2_aligned = np.append(zeros_to_append, wav2_org, axis=0) elif diff < 0: wav2_aligned = wav2_org[-diff:] else: wav2_aligned = wav2_org #command_Text(f"Audio files already aligned.\n") if not any(np.array_equal(wav2_aligned, source) for source in wav2_aligned_sources): wav2_aligned_sources.append(wav2_aligned)
#print("Unique Sources: ", len(wav2_aligned_sources)) unique_sources = len(wav2_aligned_sources) sub_mapper_big_mapper = {}
for s in wav2_aligned_sources: wav2_aligned = match_mono_array_shapes(s, wav1) if is_mono else match_array_shapes(s, wav1, is_swap=True) if align_window: wav_sub = time_correction(wav1, wav2_aligned, seconds_length, align_window=align_window, db_analysis=db_analysis, progress_bar=progress_bar, unique_sources=unique_sources, phase_shifts=phase_shifts) wav_sub_size = np.abs(wav_sub).mean() sub_mapper_big_mapper = {**sub_mapper_big_mapper, **{wav_sub_size:wav_sub}} else: wav2_aligned = wav2_aligned * np.power(10, db_analysis[0] / 20) db_range = db_analysis[1] for db_adjustment in db_range: # Adjust the dB of track2 s_adjusted = wav2_aligned * (10 ** (db_adjustment / 20)) wav_sub = wav1 - s_adjusted wav_sub_size = np.abs(wav_sub).mean() sub_mapper_big_mapper = {**sub_mapper_big_mapper, **{wav_sub_size:wav_sub}} #print(sub_mapper_big_mapper.keys(), min(sub_mapper_big_mapper.keys())) sub_mapper_value_list = list(sub_mapper_big_mapper.values()) if is_spec_match and len(sub_mapper_value_list) >= 2: #print("using spec ensemble with align") wav_sub = ensemble_for_align(list(sub_mapper_big_mapper.values())) else: #print("using linear ensemble with align") wav_sub = ensemble_wav(list(sub_mapper_big_mapper.values())) #print(f"Mix Mean: {np.abs(wav1).mean()}\nInst Mean: {np.abs(wav2).mean()}") #print('Final: ', np.abs(wav_sub).mean()) wav_sub = np.clip(wav_sub, -1, +1) command_Text(f"Saving inverted track... ")
if is_save_aligned or is_spec_match: wav1 = match_mono_array_shapes(wav1, wav_sub) if is_mono else match_array_shapes(wav1, wav_sub, is_swap=True) wav2_aligned = wav1 - wav_sub
if is_spec_match: if wav1.ndim == 1 and wav2.ndim == 1: wav2_aligned = np.asfortranarray([wav2_aligned, wav2_aligned]).T wav1 = np.asfortranarray([wav1, wav1]).T wav2_aligned = ensemble_for_align([wav2_aligned, wav1]) wav_sub = wav1 - wav2_aligned if is_save_aligned: sf.write(file2_aligned, wav2_aligned, sr1, subtype=wav_type_set) save_format(file2_aligned)
sf.write(file_subtracted, wav_sub, sr1, subtype=wav_type_set) save_format(file_subtracted)
def phase_shift_hilbert(signal, degree): analytic_signal = hilbert(signal) return np.cos(np.radians(degree)) * analytic_signal.real - np.sin(np.radians(degree)) * analytic_signal.imag
def get_phase_shifted_tracks(track, phase_shift): if phase_shift == 180: return [track, -track]
step = phase_shift end = 180 - (180 % step) if 180 % step == 0 else 181 phase_range = range(step, end, step) flipped_list = [track, -track] for i in phase_range: flipped_list.extend([phase_shift_hilbert(track, i), phase_shift_hilbert(track, -i)])
return flipped_list
def time_correction(mix:np.ndarray, instrumental:np.ndarray, seconds_length, align_window, db_analysis, sr=44100, progress_bar=None, unique_sources=None, phase_shifts=NONE_P): # Function to align two tracks using cross-correlation
def align_tracks(track1, track2): # A dictionary to store each version of track2_shifted and its mean absolute value shifted_tracks = {}
# Loop to adjust dB of track2 track2 = track2 * np.power(10, db_analysis[0] / 20) db_range = db_analysis[1] if phase_shifts == 190: track2_flipped = [track2] else: track2_flipped = get_phase_shifted_tracks(track2, phase_shifts) for db_adjustment in db_range: for t in track2_flipped: # Adjust the dB of track2 track2_adjusted = t * (10 ** (db_adjustment / 20)) corr = correlate(track1, track2_adjusted) delay = np.argmax(np.abs(corr)) - (len(track1) - 1) track2_shifted = np.roll(track2_adjusted, shift=delay)
# Compute the mean absolute value of track2_shifted track2_shifted_sub = track1 - track2_shifted mean_abs_value = np.abs(track2_shifted_sub).mean()
# Store track2_shifted and its mean absolute value in the dictionary shifted_tracks[mean_abs_value] = track2_shifted
# Return the version of track2_shifted with the smallest mean absolute value return shifted_tracks[min(shifted_tracks.keys())]
# Make sure the audio files have the same shape assert mix.shape == instrumental.shape, f"Audio files must have the same shape - Mix: {mix.shape}, Inst: {instrumental.shape}" seconds_length = seconds_length // 2
sub_mapper = {} progress_update_interval = 120 total_iterations = 0 if len(align_window) > 2: progress_update_interval = 320 for secs in align_window: step = secs / 2 window_size = int(sr * secs) step_size = int(sr * step) if len(mix.shape) == 1: total_mono = (len(range(0, len(mix) - window_size, step_size))//progress_update_interval)*unique_sources total_iterations += total_mono else: total_stereo_ = len(range(0, len(mix[:, 0]) - window_size, step_size))*2 total_stereo = (total_stereo_//progress_update_interval) * unique_sources total_iterations += total_stereo #print(total_iterations) for secs in align_window: sub = np.zeros_like(mix) divider = np.zeros_like(mix) step = secs / 2 window_size = int(sr * secs) step_size = int(sr * step) window = np.hanning(window_size)
# For the mono case: if len(mix.shape) == 1: # The files are mono counter = 0 for i in range(0, len(mix) - window_size, step_size): counter += 1 if counter % progress_update_interval == 0: progress_bar(total_iterations) window_mix = mix[i:i+window_size] * window window_instrumental = instrumental[i:i+window_size] * window window_instrumental_aligned = align_tracks(window_mix, window_instrumental) sub[i:i+window_size] += window_mix - window_instrumental_aligned divider[i:i+window_size] += window else: # The files are stereo counter = 0 for ch in range(mix.shape[1]): for i in range(0, len(mix[:, ch]) - window_size, step_size): counter += 1 if counter % progress_update_interval == 0: progress_bar(total_iterations) window_mix = mix[i:i+window_size, ch] * window window_instrumental = instrumental[i:i+window_size, ch] * window window_instrumental_aligned = align_tracks(window_mix, window_instrumental) sub[i:i+window_size, ch] += window_mix - window_instrumental_aligned divider[i:i+window_size, ch] += window
# Normalize the result by the overlap count sub = np.where(divider > 1e-6, sub / divider, sub) sub_size = np.abs(sub).mean() sub_mapper = {**sub_mapper, **{sub_size: sub}}
#print("SUB_LEN", len(list(sub_mapper.values())))
sub = ensemble_wav(list(sub_mapper.values()), split_size=12) return sub
def ensemble_wav(waveforms, split_size=240): # Create a dictionary to hold the thirds of each waveform and their mean absolute values waveform_thirds = {i: np.array_split(waveform, split_size) for i, waveform in enumerate(waveforms)}
# Initialize the final waveform final_waveform = []
# For chunk for third_idx in range(split_size): # Compute the mean absolute value of each third from each waveform means = [np.abs(waveform_thirds[i][third_idx]).mean() for i in range(len(waveforms))]
# Find the index of the waveform with the lowest mean absolute value for this third min_index = np.argmin(means)
# Add the least noisy third to the final waveform final_waveform.append(waveform_thirds[min_index][third_idx])
# Concatenate all the thirds to create the final waveform final_waveform = np.concatenate(final_waveform)
return final_waveform
def ensemble_wav_min(waveforms): for i in range(1, len(waveforms)): if i == 1: wave = waveforms[0]
ln = min(len(wave), len(waveforms[i])) wave = wave[:ln] waveforms[i] = waveforms[i][:ln]
wave = np.where(np.abs(waveforms[i]) <= np.abs(wave), waveforms[i], wave) return wave
def align_audio_test(wav1, wav2, sr1=44100): def get_diff(a, b): corr = np.correlate(a, b, "full") diff = corr.argmax() - (b.shape[0] - 1) return diff # read tracks wav1 = wav1.transpose() wav2 = wav2.transpose()
#print(f"Audio file shapes: {wav1.shape} / {wav2.shape}\n") wav2_org = wav2.copy() # pick a position at 1 second in and get diff index = sr1#*seconds_length # 1 second in, assuming sr1 = sr2 = 44100 samp1 = wav1[index : index + sr1, 0] # currently use left channel samp2 = wav2[index : index + sr1, 0] diff = get_diff(samp1, samp2) # make aligned track 2 if diff > 0: wav2_aligned = np.append(np.zeros((diff, 1)), wav2_org, axis=0) elif diff < 0: wav2_aligned = wav2_org[-diff:] else: wav2_aligned = wav2_org return wav2_aligned
def load_audio(audio_file): wav, sr = librosa.load(audio_file, sr=44100, mono=False)
if wav.ndim == 1: wav = np.asfortranarray([wav,wav]) return wav
def rerun_mp3(audio_file): with audioread.audio_open(audio_file) as f: track_length = int(f.duration)
return track_length123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253import torchimport torch.nn as nnfrom functools import partial
class STFT: def __init__(self, n_fft, hop_length, dim_f, device): self.n_fft = n_fft self.hop_length = hop_length self.window = torch.hann_window(window_length=self.n_fft, periodic=True) self.dim_f = dim_f self.device = device
def __call__(self, x): x_is_mps = not x.device.type in ["cuda", "cpu"] if x_is_mps: x = x.cpu()
window = self.window.to(x.device) batch_dims = x.shape[:-2] c, t = x.shape[-2:] x = x.reshape([-1, t]) x = torch.stft(x, n_fft=self.n_fft, hop_length=self.hop_length, window=window, center=True,return_complex=False) x = x.permute([0, 3, 1, 2]) x = x.reshape([*batch_dims, c, 2, -1, x.shape[-1]]).reshape([*batch_dims, c * 2, -1, x.shape[-1]])
if x_is_mps: x = x.to(self.device)
return x[..., :self.dim_f, :]
def inverse(self, x): x_is_mps = not x.device.type in ["cuda", "cpu"] if x_is_mps: x = x.cpu()
window = self.window.to(x.device) batch_dims = x.shape[:-3] c, f, t = x.shape[-3:] n = self.n_fft // 2 + 1 f_pad = torch.zeros([*batch_dims, c, n - f, t]).to(x.device) x = torch.cat([x, f_pad], -2) x = x.reshape([*batch_dims, c // 2, 2, n, t]).reshape([-1, 2, n, t]) x = x.permute([0, 2, 3, 1]) x = x[..., 0] + x[..., 1] * 1.j x = torch.istft(x, n_fft=self.n_fft, hop_length=self.hop_length, window=window, center=True) x = x.reshape([*batch_dims, 2, -1])
if x_is_mps: x = x.to(self.device)
return x
def get_norm(norm_type): def norm(c, norm_type): if norm_type == 'BatchNorm': return nn.BatchNorm2d(c) elif norm_type == 'InstanceNorm': return nn.InstanceNorm2d(c, affine=True) elif 'GroupNorm' in norm_type: g = int(norm_type.replace('GroupNorm', '')) return nn.GroupNorm(num_groups=g, num_channels=c) else: return nn.Identity()
return partial(norm, norm_type=norm_type)
def get_act(act_type): if act_type == 'gelu': return nn.GELU() elif act_type == 'relu': return nn.ReLU() elif act_type[:3] == 'elu': alpha = float(act_type.replace('elu', '')) return nn.ELU(alpha) else: raise Exception
class Upscale(nn.Module): def __init__(self, in_c, out_c, scale, norm, act): super().__init__() self.conv = nn.Sequential( norm(in_c), act, nn.ConvTranspose2d(in_channels=in_c, out_channels=out_c, kernel_size=scale, stride=scale, bias=False) )
def forward(self, x): return self.conv(x)
class Downscale(nn.Module): def __init__(self, in_c, out_c, scale, norm, act): super().__init__() self.conv = nn.Sequential( norm(in_c), act, nn.Conv2d(in_channels=in_c, out_channels=out_c, kernel_size=scale, stride=scale, bias=False) )
def forward(self, x): return self.conv(x)
class TFC_TDF(nn.Module): def __init__(self, in_c, c, l, f, bn, norm, act): super().__init__()
self.blocks = nn.ModuleList() for i in range(l): block = nn.Module()
block.tfc1 = nn.Sequential( norm(in_c), act, nn.Conv2d(in_c, c, 3, 1, 1, bias=False), ) block.tdf = nn.Sequential( norm(c), act, nn.Linear(f, f // bn, bias=False), norm(c), act, nn.Linear(f // bn, f, bias=False), ) block.tfc2 = nn.Sequential( norm(c), act, nn.Conv2d(c, c, 3, 1, 1, bias=False), ) block.shortcut = nn.Conv2d(in_c, c, 1, 1, 0, bias=False)
self.blocks.append(block) in_c = c
def forward(self, x): for block in self.blocks: s = block.shortcut(x) x = block.tfc1(x) x = x + block.tdf(x) x = block.tfc2(x) x = x + s return x
class TFC_TDF_net(nn.Module): def __init__(self, config, device): super().__init__() self.config = config self.device = device
norm = get_norm(norm_type=config.model.norm) act = get_act(act_type=config.model.act)
self.num_target_instruments = 1 if config.training.target_instrument else len(config.training.instruments) self.num_subbands = config.model.num_subbands
dim_c = self.num_subbands * config.audio.num_channels * 2 n = config.model.num_scales scale = config.model.scale l = config.model.num_blocks_per_scale c = config.model.num_channels g = config.model.growth bn = config.model.bottleneck_factor f = config.audio.dim_f // self.num_subbands
self.first_conv = nn.Conv2d(dim_c, c, 1, 1, 0, bias=False)
self.encoder_blocks = nn.ModuleList() for i in range(n): block = nn.Module() block.tfc_tdf = TFC_TDF(c, c, l, f, bn, norm, act) block.downscale = Downscale(c, c + g, scale, norm, act) f = f // scale[1] c += g self.encoder_blocks.append(block)
self.bottleneck_block = TFC_TDF(c, c, l, f, bn, norm, act)
self.decoder_blocks = nn.ModuleList() for i in range(n): block = nn.Module() block.upscale = Upscale(c, c - g, scale, norm, act) f = f * scale[1] c -= g block.tfc_tdf = TFC_TDF(2 * c, c, l, f, bn, norm, act) self.decoder_blocks.append(block)
self.final_conv = nn.Sequential( nn.Conv2d(c + dim_c, c, 1, 1, 0, bias=False), act, nn.Conv2d(c, self.num_target_instruments * dim_c, 1, 1, 0, bias=False) )
self.stft = STFT(config.audio.n_fft, config.audio.hop_length, config.audio.dim_f, self.device)
def cac2cws(self, x): k = self.num_subbands b, c, f, t = x.shape x = x.reshape(b, c, k, f // k, t) x = x.reshape(b, c * k, f // k, t) return x
def cws2cac(self, x): k = self.num_subbands b, c, f, t = x.shape x = x.reshape(b, c // k, k, f, t) x = x.reshape(b, c // k, f * k, t) return x
def forward(self, x):
x = self.stft(x)
mix = x = self.cac2cws(x)
first_conv_out = x = self.first_conv(x)
x = x.transpose(-1, -2)
encoder_outputs = [] for block in self.encoder_blocks: x = block.tfc_tdf(x) encoder_outputs.append(x) x = block.downscale(x)
x = self.bottleneck_block(x)
for block in self.decoder_blocks: x = block.upscale(x) x = torch.cat([x, encoder_outputs.pop()], 1) x = block.tfc_tdf(x)
x = x.transpose(-1, -2)
x = x * first_conv_out # reduce artifacts
x = self.final_conv(torch.cat([mix, x], 1))
x = self.cws2cac(x)
if self.num_target_instruments > 1: b, c, f, t = x.shape x = x.reshape(b, self.num_target_instruments, -1, f, t)
x = self.stft.inverse(x)
return x
1# VR init.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143import torchfrom torch import nnimport torch.nn.functional as F
from lib_v5 import spec_utils
class Conv2DBNActiv(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU): super(Conv2DBNActiv, self).__init__() self.conv = nn.Sequential( nn.Conv2d( nin, nout, kernel_size=ksize, stride=stride, padding=pad, dilation=dilation, bias=False), nn.BatchNorm2d(nout), activ() )
def __call__(self, x): return self.conv(x)
class SeperableConv2DBNActiv(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU): super(SeperableConv2DBNActiv, self).__init__() self.conv = nn.Sequential( nn.Conv2d( nin, nin, kernel_size=ksize, stride=stride, padding=pad, dilation=dilation, groups=nin, bias=False), nn.Conv2d( nin, nout, kernel_size=1, bias=False), nn.BatchNorm2d(nout), activ() )
def __call__(self, x): return self.conv(x)
class Encoder(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU): super(Encoder, self).__init__() self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ) self.conv2 = Conv2DBNActiv(nout, nout, ksize, stride, pad, activ=activ)
def __call__(self, x): skip = self.conv1(x) h = self.conv2(skip)
return h, skip
class Decoder(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False): super(Decoder, self).__init__() self.conv = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ) self.dropout = nn.Dropout2d(0.1) if dropout else None
def __call__(self, x, skip=None): x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True) if skip is not None: skip = spec_utils.crop_center(skip, x) x = torch.cat([x, skip], dim=1) h = self.conv(x)
if self.dropout is not None: h = self.dropout(h)
return h
class ASPPModule(nn.Module):
def __init__(self, nn_architecture, nin, nout, dilations=(4, 8, 16), activ=nn.ReLU): super(ASPPModule, self).__init__() self.conv1 = nn.Sequential( nn.AdaptiveAvgPool2d((1, None)), Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ) ) self.nn_architecture = nn_architecture self.six_layer = [129605] self.seven_layer = [537238, 537227, 33966] extra_conv = SeperableConv2DBNActiv( nin, nin, 3, 1, dilations[2], dilations[2], activ=activ) self.conv2 = Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ) self.conv3 = SeperableConv2DBNActiv( nin, nin, 3, 1, dilations[0], dilations[0], activ=activ) self.conv4 = SeperableConv2DBNActiv( nin, nin, 3, 1, dilations[1], dilations[1], activ=activ) self.conv5 = SeperableConv2DBNActiv( nin, nin, 3, 1, dilations[2], dilations[2], activ=activ) if self.nn_architecture in self.six_layer: self.conv6 = extra_conv nin_x = 6 elif self.nn_architecture in self.seven_layer: self.conv6 = extra_conv self.conv7 = extra_conv nin_x = 7 else: nin_x = 5 self.bottleneck = nn.Sequential( Conv2DBNActiv(nin * nin_x, nout, 1, 1, 0, activ=activ), nn.Dropout2d(0.1) )
def forward(self, x): _, _, h, w = x.size() feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True) feat2 = self.conv2(x) feat3 = self.conv3(x) feat4 = self.conv4(x) feat5 = self.conv5(x) if self.nn_architecture in self.six_layer: feat6 = self.conv6(x) out = torch.cat((feat1, feat2, feat3, feat4, feat5, feat6), dim=1) elif self.nn_architecture in self.seven_layer: feat6 = self.conv6(x) feat7 = self.conv7(x) out = torch.cat((feat1, feat2, feat3, feat4, feat5, feat6, feat7), dim=1) else: out = torch.cat((feat1, feat2, feat3, feat4, feat5), dim=1) bottle = self.bottleneck(out) return bottle123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126import torchfrom torch import nnimport torch.nn.functional as F
from lib_v5 import spec_utils
class Conv2DBNActiv(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU): super(Conv2DBNActiv, self).__init__() self.conv = nn.Sequential( nn.Conv2d( nin, nout, kernel_size=ksize, stride=stride, padding=pad, dilation=dilation, bias=False), nn.BatchNorm2d(nout), activ() )
def __call__(self, x): return self.conv(x)
class Encoder(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU): super(Encoder, self).__init__() self.conv1 = Conv2DBNActiv(nin, nout, ksize, stride, pad, activ=activ) self.conv2 = Conv2DBNActiv(nout, nout, ksize, 1, pad, activ=activ)
def __call__(self, x): h = self.conv1(x) h = self.conv2(h)
return h
class Decoder(nn.Module):
def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False): super(Decoder, self).__init__() self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ) # self.conv2 = Conv2DBNActiv(nout, nout, ksize, 1, pad, activ=activ) self.dropout = nn.Dropout2d(0.1) if dropout else None
def __call__(self, x, skip=None): x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
if skip is not None: skip = spec_utils.crop_center(skip, x) x = torch.cat([x, skip], dim=1)
h = self.conv1(x) # h = self.conv2(h)
if self.dropout is not None: h = self.dropout(h)
return h
class ASPPModule(nn.Module):
def __init__(self, nin, nout, dilations=(4, 8, 12), activ=nn.ReLU, dropout=False): super(ASPPModule, self).__init__() self.conv1 = nn.Sequential( nn.AdaptiveAvgPool2d((1, None)), Conv2DBNActiv(nin, nout, 1, 1, 0, activ=activ) ) self.conv2 = Conv2DBNActiv(nin, nout, 1, 1, 0, activ=activ) self.conv3 = Conv2DBNActiv( nin, nout, 3, 1, dilations[0], dilations[0], activ=activ ) self.conv4 = Conv2DBNActiv( nin, nout, 3, 1, dilations[1], dilations[1], activ=activ ) self.conv5 = Conv2DBNActiv( nin, nout, 3, 1, dilations[2], dilations[2], activ=activ ) self.bottleneck = Conv2DBNActiv(nout * 5, nout, 1, 1, 0, activ=activ) self.dropout = nn.Dropout2d(0.1) if dropout else None
def forward(self, x): _, _, h, w = x.size() feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True) feat2 = self.conv2(x) feat3 = self.conv3(x) feat4 = self.conv4(x) feat5 = self.conv5(x) out = torch.cat((feat1, feat2, feat3, feat4, feat5), dim=1) out = self.bottleneck(out)
if self.dropout is not None: out = self.dropout(out)
return out
class LSTMModule(nn.Module):
def __init__(self, nin_conv, nin_lstm, nout_lstm): super(LSTMModule, self).__init__() self.conv = Conv2DBNActiv(nin_conv, 1, 1, 1, 0) self.lstm = nn.LSTM( input_size=nin_lstm, hidden_size=nout_lstm // 2, bidirectional=True ) self.dense = nn.Sequential( nn.Linear(nout_lstm, nin_lstm), nn.BatchNorm1d(nin_lstm), nn.ReLU() )
def forward(self, x): N, _, nbins, nframes = x.size() h = self.conv(x)[:, 0] # N, nbins, nframes h = h.permute(2, 0, 1) # nframes, N, nbins h, _ = self.lstm(h) h = self.dense(h.reshape(-1, h.size()[-1])) # nframes * N, nbins h = h.reshape(nframes, N, 1, nbins) h = h.permute(1, 2, 3, 0)
return h1234567891011121314151617181920212223242526272829303132import json
default_param = {}default_param['bins'] = -1default_param['unstable_bins'] = -1 # training onlydefault_param['stable_bins'] = -1 # training onlydefault_param['sr'] = 44100default_param['pre_filter_start'] = -1default_param['pre_filter_stop'] = -1default_param['band'] = {}
N_BINS = 'n_bins'
def int_keys(d): r = {} for k, v in d: if k.isdigit(): k = int(k) r[k] = v return r class ModelParameters(object): def __init__(self, config_path=''): with open(config_path, 'r') as f: self.param = json.loads(f.read(), object_pairs_hook=int_keys) 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 if N_BINS in self.param: self.param['bins'] = self.param[N_BINS]12345678910111213141516171819{ "bins": 1024, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 16000, "hl": 512, "n_fft": 2048, "crop_start": 0, "crop_stop": 1024, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 16000, "pre_filter_start": 1023, "pre_filter_stop": 1024}12345678910111213141516171819{ "bins": 1024, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 32000, "hl": 512, "n_fft": 2048, "crop_start": 0, "crop_stop": 1024, "hpf_start": -1, "res_type": "kaiser_fast" } }, "sr": 32000, "pre_filter_start": 1000, "pre_filter_stop": 1021}12345678910111213141516171819{ "bins": 1024, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 33075, "hl": 384, "n_fft": 2048, "crop_start": 0, "crop_stop": 1024, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 33075, "pre_filter_start": 1000, "pre_filter_stop": 1021}12345678910111213141516171819{ "bins": 1024, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 44100, "hl": 1024, "n_fft": 2048, "crop_start": 0, "crop_stop": 1024, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 44100, "pre_filter_start": 1023, "pre_filter_stop": 1024}12345678910111213141516171819{ "bins": 256, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 44100, "hl": 256, "n_fft": 512, "crop_start": 0, "crop_stop": 256, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 44100, "pre_filter_start": 256, "pre_filter_stop": 256}12345678910111213141516171819{ "bins": 1024, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 44100, "hl": 512, "n_fft": 2048, "crop_start": 0, "crop_stop": 1024, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 44100, "pre_filter_start": 1023, "pre_filter_stop": 1024}12345678910111213141516171819{ "bins": 1024, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 44100, "hl": 512, "n_fft": 2048, "crop_start": 0, "crop_stop": 700, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 44100, "pre_filter_start": 1023, "pre_filter_stop": 700}12345678910111213141516171819{ "bins": 512, "unstable_bins": 0, "reduction_bins": 0, "band": { "1": { "sr": 44100, "hl": 512, "n_fft": 1024, "crop_start": 0, "crop_stop": 512, "hpf_start": -1, "res_type": "sinc_best" } }, "sr": 44100, "pre_filter_start": 511, "pre_filter_stop": 512}123456789101112131415161718192021222324252627282930{ "bins": 768, "unstable_bins": 7, "reduction_bins": 705, "band": { "1": { "sr": 6000, "hl": 66, "n_fft": 512, "crop_start": 0, "crop_stop": 240, "lpf_start": 60, "lpf_stop": 118, "res_type": "sinc_fastest" }, "2": { "sr": 32000, "hl": 352, "n_fft": 1024, "crop_start": 22, "crop_stop": 505, "hpf_start": 44, "hpf_stop": 23, "res_type": "sinc_medium" } }, "sr": 32000, "pre_filter_start": 710, "pre_filter_stop": 731}123456789101112131415161718192021222324252627282930{ "bins": 512, "unstable_bins": 7, "reduction_bins": 510, "band": { "1": { "sr": 11025, "hl": 160, "n_fft": 768, "crop_start": 0, "crop_stop": 192, "lpf_start": 41, "lpf_stop": 139, "res_type": "sinc_fastest" }, "2": { "sr": 44100, "hl": 640, "n_fft": 1024, "crop_start": 10, "crop_stop": 320, "hpf_start": 47, "hpf_stop": 15, "res_type": "sinc_medium" } }, "sr": 44100, "pre_filter_start": 510, "pre_filter_stop": 512}123456789101112131415161718192021222324252627282930{ "bins": 768, "unstable_bins": 7, "reduction_bins": 705, "band": { "1": { "sr": 6000, "hl": 66, "n_fft": 512, "crop_start": 0, "crop_stop": 240, "lpf_start": 60, "lpf_stop": 240, "res_type": "sinc_fastest" }, "2": { "sr": 48000, "hl": 528, "n_fft": 1536, "crop_start": 22, "crop_stop": 505, "hpf_start": 82, "hpf_stop": 22, "res_type": "sinc_medium" } }, "sr": 48000, "pre_filter_start": 710, "pre_filter_stop": 731}123456789101112131415161718192021222324252627282930313233343536373839404142{ "bins": 768, "unstable_bins": 5, "reduction_bins": 733, "band": { "1": { "sr": 11025, "hl": 128, "n_fft": 768, "crop_start": 0, "crop_stop": 278, "lpf_start": 28, "lpf_stop": 140, "res_type": "polyphase" }, "2": { "sr": 22050, "hl": 256, "n_fft": 768, "crop_start": 14, "crop_stop": 322, "hpf_start": 70, "hpf_stop": 14, "lpf_start": 283, "lpf_stop": 314, "res_type": "polyphase" }, "3": { "sr": 44100, "hl": 512, "n_fft": 768, "crop_start": 131, "crop_stop": 313, "hpf_start": 154, "hpf_stop": 141, "res_type": "sinc_medium" } }, "sr": 44100, "pre_filter_start": 757, "pre_filter_stop": 768}12345678910111213141516171819202122232425262728293031323334353637383940414243{ "mid_side": true, "bins": 768, "unstable_bins": 5, "reduction_bins": 733, "band": { "1": { "sr": 11025, "hl": 128, "n_fft": 768, "crop_start": 0, "crop_stop": 278, "lpf_start": 28, "lpf_stop": 140, "res_type": "polyphase" }, "2": { "sr": 22050, "hl": 256, "n_fft": 768, "crop_start": 14, "crop_stop": 322, "hpf_start": 70, "hpf_stop": 14, "lpf_start": 283, "lpf_stop": 314, "res_type": "polyphase" }, "3": { "sr": 44100, "hl": 512, "n_fft": 768, "crop_start": 131, "crop_stop": 313, "hpf_start": 154, "hpf_stop": 141, "res_type": "sinc_medium" } }, "sr": 44100, "pre_filter_start": 757, "pre_filter_stop": 768}12345678910111213141516171819202122232425262728293031323334353637383940414243{ "mid_side_b2": true, "bins": 640, "unstable_bins": 7, "reduction_bins": 565, "band": { "1": { "sr": 11025, "hl": 108, "n_fft": 1024, "crop_start": 0, "crop_stop": 187, "lpf_start": 92, "lpf_stop": 186, "res_type": "polyphase" }, "2": { "sr": 22050, "hl": 216, "n_fft": 768, "crop_start": 0, "crop_stop": 212, "hpf_start": 68, "hpf_stop": 34, "lpf_start": 174, "lpf_stop": 209, "res_type": "polyphase" }, "3": { "sr": 44100, "hl": 432, "n_fft": 640, "crop_start": 66, "crop_stop": 307, "hpf_start": 86, "hpf_stop": 72, "res_type": "kaiser_fast" } }, "sr": 44100, "pre_filter_start": 639, "pre_filter_stop": 640}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354{ "bins": 768, "unstable_bins": 7, "reduction_bins": 668, "band": { "1": { "sr": 11025, "hl": 128, "n_fft": 1024, "crop_start": 0, "crop_stop": 186, "lpf_start": 37, "lpf_stop": 73, "res_type": "polyphase" }, "2": { "sr": 11025, "hl": 128, "n_fft": 512, "crop_start": 4, "crop_stop": 185, "hpf_start": 36, "hpf_stop": 18, "lpf_start": 93, "lpf_stop": 185, "res_type": "polyphase" }, "3": { "sr": 22050, "hl": 256, "n_fft": 512, "crop_start": 46, "crop_stop": 186, "hpf_start": 93, "hpf_stop": 46, "lpf_start": 164, "lpf_stop": 186, "res_type": "polyphase" }, "4": { "sr": 44100, "hl": 512, "n_fft": 768, "crop_start": 121, "crop_stop": 382, "hpf_start": 138, "hpf_stop": 123, "res_type": "sinc_medium" } }, "sr": 44100, "pre_filter_start": 740, "pre_filter_stop": 768}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455{ "bins": 768, "unstable_bins": 7, "mid_side": true, "reduction_bins": 668, "band": { "1": { "sr": 11025, "hl": 128, "n_fft": 1024, "crop_start": 0, "crop_stop": 186, "lpf_start": 37, "lpf_stop": 73, "res_type": "polyphase" }, "2": { "sr": 11025, "hl": 128, "n_fft": 512, "crop_start": 4, "crop_stop": 185, "hpf_start": 36, "hpf_stop": 18, "lpf_start": 93, "lpf_stop": 185, "res_type": "polyphase" }, "3": { "sr": 22050, "hl": 256, "n_fft": 512, "crop_start": 46, "crop_stop": 186, "hpf_start": 93, "hpf_stop": 46, "lpf_start": 164, "lpf_stop": 186, "res_type": "polyphase" }, "4": { "sr": 44100, "hl": 512, "n_fft": 768, "crop_start": 121, "crop_stop": 382, "hpf_start": 138, "hpf_stop": 123, "res_type": "sinc_medium" } }, "sr": 44100, "pre_filter_start": 740, "pre_filter_stop": 768}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455{ "mid_side_b": true, "bins": 768, "unstable_bins": 7, "reduction_bins": 668, "band": { "1": { "sr": 11025, "hl": 128, "n_fft": 1024, "crop_start": 0, "crop_stop": 186, "lpf_start": 37, "lpf_stop": 73, "res_type": "polyphase" }, "2": { "sr": 11025, "hl": 128, "n_fft": 512, "crop_start": 4, "crop_stop": 185, "hpf_start": 36, "hpf_stop": 18, "lpf_start": 93, "lpf_stop": 185, "res_type": "polyphase" }, "3": { "sr": 22050, "hl": 256, "n_fft": 512, "crop_start": 46, "crop_stop": 186, "hpf_start": 93, "hpf_stop": 46, "lpf_start": 164, "lpf_stop": 186, "res_type": "polyphase" }, "4": { "sr": 44100, "hl": 512, "n_fft": 768, "crop_start": 121, "crop_stop": 382, "hpf_start": 138, "hpf_stop": 123, "res_type": "sinc_medium" } }, "sr": 44100, "pre_filter_start": 740, "pre_filter_stop": 768}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455{ "mid_side_b": true, "bins": 768, "unstable_bins": 7, "reduction_bins": 668, "band": { "1": { "sr": 11025, "hl": 128, "n_fft": 1024, "crop_start": 0, "crop_stop": 186, "lpf_start": 37, "lpf_stop": 73, "res_type": "polyphase" }, "2": { "sr": 11025, "hl": 128, "n_fft": 512, "crop_start": 4, "crop_stop": 185, "hpf_start": 36, "hpf_stop": 18, "lpf_start": 93, "lpf_stop": 185, "res_type": "polyphase" }, "3": { "sr": 22050, "hl": 256, "n_fft": 512, "crop_start": 46, "crop_stop": 186, "hpf_start": 93, "hpf_stop": 46, "lpf_start": 164, "lpf_stop": 186, "res_type": "polyphase" }, "4": { "sr": 44100, "hl": 512, "n_fft": 768, "crop_start": 121, "crop_stop": 382, "hpf_start": 138, "hpf_stop": 123, "res_type": "sinc_medium" } }, "sr": 44100, "pre_filter_start": 740, "pre_filter_stop": 768}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455{ "reverse": true, "bins": 768, "unstable_bins": 7, "reduction_bins": 668, "band": { "1": { "sr": 11025, "hl": 128, "n_fft": 1024, "crop_start": 0, "crop_stop": 186, "lpf_start": 37, "lpf_stop": 73, "res_type": "polyphase" }, "2": { "sr": 11025, "hl": 128, "n_fft": 512, "crop_start": 4, "crop_stop": 185, "hpf_start": 36, "hpf_stop": 18, "lpf_start": 93, "lpf_stop": 185, "res_type": "polyphase" }, "3": { "sr": 22050, "hl": 256, "n_fft": 512, "crop_start": 46, "crop_stop": 186, "hpf_start": 93, "hpf_stop": 46, "lpf_start": 164, "lpf_stop": 186, "res_type": "polyphase" }, "4": { "sr": 44100, "hl": 512, "n_fft": 768, "crop_start": 121, "crop_stop": 382, "hpf_start": 138, "hpf_stop": 123, "res_type": "sinc_medium" } }, "sr": 44100, "pre_filter_start": 740, "pre_filter_stop": 768}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455{ "stereo_w": true, "bins": 768, "unstable_bins": 7, "reduction_bins": 668, "band": { "1": { "sr": 11025, "hl": 128, "n_fft": 1024, "crop_start": 0, "crop_stop": 186, "lpf_start": 37, "lpf_stop": 73, "res_type": "polyphase" }, "2": { "sr": 11025, "hl": 128, "n_fft": 512, "crop_start": 4, "crop_stop": 185, "hpf_start": 36, "hpf_stop": 18, "lpf_start": 93, "lpf_stop": 185, "res_type": "polyphase" }, "3": { "sr": 22050, "hl": 256, "n_fft": 512, "crop_start": 46, "crop_stop": 186, "hpf_start": 93, "hpf_stop": 46, "lpf_start": 164, "lpf_stop": 186, "res_type": "polyphase" }, "4": { "sr": 44100, "hl": 512, "n_fft": 768, "crop_start": 121, "crop_stop": 382, "hpf_start": 138, "hpf_stop": 123, "res_type": "sinc_medium" } }, "sr": 44100, "pre_filter_start": 740, "pre_filter_stop": 768}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354{ "bins": 672, "unstable_bins": 8, "reduction_bins": 637, "band": { "1": { "sr": 7350, "hl": 80, "n_fft": 640, "crop_start": 0, "crop_stop": 85, "lpf_start": 25, "lpf_stop": 53, "res_type": "polyphase" }, "2": { "sr": 7350, "hl": 80, "n_fft": 320, "crop_start": 4, "crop_stop": 87, "hpf_start": 25, "hpf_stop": 12, "lpf_start": 31, "lpf_stop": 62, "res_type": "polyphase" }, "3": { "sr": 14700, "hl": 160, "n_fft": 512, "crop_start": 17, "crop_stop": 216, "hpf_start": 48, "hpf_stop": 24, "lpf_start": 139, "lpf_stop": 210, "res_type": "polyphase" }, "4": { "sr": 44100, "hl": 480, "n_fft": 960, "crop_start": 78, "crop_stop": 383, "hpf_start": 130, "hpf_stop": 86, "res_type": "kaiser_fast" } }, "sr": 44100, "pre_filter_start": 668, "pre_filter_stop": 672}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455{ "bins": 672, "unstable_bins": 8, "reduction_bins": 637, "band": { "1": { "sr": 7350, "hl": 80, "n_fft": 640, "crop_start": 0, "crop_stop": 85, "lpf_start": 25, "lpf_stop": 53, "res_type": "polyphase" }, "2": { "sr": 7350, "hl": 80, "n_fft": 320, "crop_start": 4, "crop_stop": 87, "hpf_start": 25, "hpf_stop": 12, "lpf_start": 31, "lpf_stop": 62, "res_type": "polyphase" }, "3": { "sr": 14700, "hl": 160, "n_fft": 512, "crop_start": 17, "crop_stop": 216, "hpf_start": 48, "hpf_stop": 24, "lpf_start": 139, "lpf_stop": 210, "res_type": "polyphase" }, "4": { "sr": 44100, "hl": 480, "n_fft": 960, "crop_start": 78, "crop_stop": 383, "hpf_start": 130, "hpf_stop": 86, "convert_channels": "stereo_n", "res_type": "kaiser_fast" } }, "sr": 44100, "pre_filter_start": 668, "pre_filter_stop": 672}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354{ "bins": 672, "unstable_bins": 8, "reduction_bins": 530, "band": { "1": { "sr": 7350, "hl": 80, "n_fft": 640, "crop_start": 0, "crop_stop": 85, "lpf_start": 25, "lpf_stop": 53, "res_type": "polyphase" }, "2": { "sr": 7350, "hl": 80, "n_fft": 320, "crop_start": 4, "crop_stop": 87, "hpf_start": 25, "hpf_stop": 12, "lpf_start": 31, "lpf_stop": 62, "res_type": "polyphase" }, "3": { "sr": 14700, "hl": 160, "n_fft": 512, "crop_start": 17, "crop_stop": 216, "hpf_start": 48, "hpf_stop": 24, "lpf_start": 139, "lpf_stop": 210, "res_type": "polyphase" }, "4": { "sr": 44100, "hl": 480, "n_fft": 960, "crop_start": 78, "crop_stop": 383, "hpf_start": 130, "hpf_stop": 86, "res_type": "kaiser_fast" } }, "sr": 44100, "pre_filter_start": 668, "pre_filter_stop": 672}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455{ "n_bins": 672, "unstable_bins": 8, "stable_bins": 530, "band": { "1": { "sr": 7350, "hl": 80, "n_fft": 640, "crop_start": 0, "crop_stop": 85, "lpf_start": 25, "lpf_stop": 53, "res_type": "polyphase" }, "2": { "sr": 7350, "hl": 80, "n_fft": 320, "crop_start": 4, "crop_stop": 87, "hpf_start": 25, "hpf_stop": 12, "lpf_start": 31, "lpf_stop": 62, "res_type": "polyphase" }, "3": { "sr": 14700, "hl": 160, "n_fft": 512, "crop_start": 17, "crop_stop": 216, "hpf_start": 48, "hpf_stop": 24, "lpf_start": 139, "lpf_stop": 210, "res_type": "polyphase" }, "4": { "sr": 44100, "hl": 480, "n_fft": 960, "crop_start": 78, "crop_stop": 383, "hpf_start": 130, "hpf_stop": 86, "convert_channels": "stereo_n", "res_type": "kaiser_fast" } }, "sr": 44100, "pre_filter_start": 668, "pre_filter_stop": 672}12345678910111213141516171819202122232425262728293031323334353637383940414243{ "mid_side_b2": true, "bins": 1280, "unstable_bins": 7, "reduction_bins": 565, "band": { "1": { "sr": 11025, "hl": 108, "n_fft": 2048, "crop_start": 0, "crop_stop": 374, "lpf_start": 92, "lpf_stop": 186, "res_type": "polyphase" }, "2": { "sr": 22050, "hl": 216, "n_fft": 1536, "crop_start": 0, "crop_stop": 424, "hpf_start": 68, "hpf_stop": 34, "lpf_start": 348, "lpf_stop": 418, "res_type": "polyphase" }, "3": { "sr": 44100, "hl": 432, "n_fft": 1280, "crop_start": 132, "crop_stop": 614, "hpf_start": 172, "hpf_stop": 144, "res_type": "polyphase" } }, "sr": 44100, "pre_filter_start": 1280, "pre_filter_stop": 1280}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166import torchfrom torch import nnimport torch.nn.functional as F
from . import layers
class BaseASPPNet(nn.Module):
def __init__(self, nn_architecture, nin, ch, dilations=(4, 8, 16)): super(BaseASPPNet, self).__init__() self.nn_architecture = nn_architecture self.enc1 = layers.Encoder(nin, ch, 3, 2, 1) self.enc2 = layers.Encoder(ch, ch * 2, 3, 2, 1) self.enc3 = layers.Encoder(ch * 2, ch * 4, 3, 2, 1) self.enc4 = layers.Encoder(ch * 4, ch * 8, 3, 2, 1) if self.nn_architecture == 129605: self.enc5 = layers.Encoder(ch * 8, ch * 16, 3, 2, 1) self.aspp = layers.ASPPModule(nn_architecture, ch * 16, ch * 32, dilations) self.dec5 = layers.Decoder(ch * (16 + 32), ch * 16, 3, 1, 1) else: self.aspp = layers.ASPPModule(nn_architecture, ch * 8, ch * 16, dilations) self.dec4 = layers.Decoder(ch * (8 + 16), ch * 8, 3, 1, 1) self.dec3 = layers.Decoder(ch * (4 + 8), ch * 4, 3, 1, 1) self.dec2 = layers.Decoder(ch * (2 + 4), ch * 2, 3, 1, 1) self.dec1 = layers.Decoder(ch * (1 + 2), ch, 3, 1, 1)
def __call__(self, x): h, e1 = self.enc1(x) h, e2 = self.enc2(h) h, e3 = self.enc3(h) h, e4 = self.enc4(h) if self.nn_architecture == 129605: h, e5 = self.enc5(h) h = self.aspp(h) h = self.dec5(h, e5) else: h = self.aspp(h) h = self.dec4(h, e4) h = self.dec3(h, e3) h = self.dec2(h, e2) h = self.dec1(h, e1)
return h
def determine_model_capacity(n_fft_bins, nn_architecture): sp_model_arch = [31191, 33966, 129605] hp_model_arch = [123821, 123812] hp2_model_arch = [537238, 537227] if nn_architecture in sp_model_arch: model_capacity_data = [ (2, 16), (2, 16), (18, 8, 1, 1, 0), (8, 16), (34, 16, 1, 1, 0), (16, 32), (32, 2, 1), (16, 2, 1), (16, 2, 1), ] if nn_architecture in hp_model_arch: model_capacity_data = [ (2, 32), (2, 32), (34, 16, 1, 1, 0), (16, 32), (66, 32, 1, 1, 0), (32, 64), (64, 2, 1), (32, 2, 1), (32, 2, 1), ] if nn_architecture in hp2_model_arch: model_capacity_data = [ (2, 64), (2, 64), (66, 32, 1, 1, 0), (32, 64), (130, 64, 1, 1, 0), (64, 128), (128, 2, 1), (64, 2, 1), (64, 2, 1), ]
cascaded = CascadedASPPNet model = cascaded(n_fft_bins, model_capacity_data, nn_architecture) return model
class CascadedASPPNet(nn.Module):
def __init__(self, n_fft, model_capacity_data, nn_architecture): super(CascadedASPPNet, self).__init__() self.stg1_low_band_net = BaseASPPNet(nn_architecture, *model_capacity_data[0]) self.stg1_high_band_net = BaseASPPNet(nn_architecture, *model_capacity_data[1])
self.stg2_bridge = layers.Conv2DBNActiv(*model_capacity_data[2]) self.stg2_full_band_net = BaseASPPNet(nn_architecture, *model_capacity_data[3])
self.stg3_bridge = layers.Conv2DBNActiv(*model_capacity_data[4]) self.stg3_full_band_net = BaseASPPNet(nn_architecture, *model_capacity_data[5])
self.out = nn.Conv2d(*model_capacity_data[6], bias=False) self.aux1_out = nn.Conv2d(*model_capacity_data[7], bias=False) self.aux2_out = nn.Conv2d(*model_capacity_data[8], bias=False)
self.max_bin = n_fft // 2 self.output_bin = n_fft // 2 + 1
self.offset = 128
def forward(self, x): mix = x.detach() x = x.clone()
x = x[:, :, :self.max_bin]
bandw = x.size()[2] // 2 aux1 = torch.cat([ self.stg1_low_band_net(x[:, :, :bandw]), self.stg1_high_band_net(x[:, :, bandw:]) ], dim=2)
h = torch.cat([x, aux1], dim=1) aux2 = self.stg2_full_band_net(self.stg2_bridge(h))
h = torch.cat([x, aux1, aux2], dim=1) h = self.stg3_full_band_net(self.stg3_bridge(h))
mask = torch.sigmoid(self.out(h)) mask = F.pad( input=mask, pad=(0, 0, 0, self.output_bin - mask.size()[2]), mode='replicate') if self.training: aux1 = torch.sigmoid(self.aux1_out(aux1)) aux1 = F.pad( input=aux1, pad=(0, 0, 0, self.output_bin - aux1.size()[2]), mode='replicate') aux2 = torch.sigmoid(self.aux2_out(aux2)) aux2 = F.pad( input=aux2, pad=(0, 0, 0, self.output_bin - aux2.size()[2]), mode='replicate') return mask * mix, aux1 * mix, aux2 * mix else: return mask# * mix
def predict_mask(self, x): mask = self.forward(x)
if self.offset > 0: mask = mask[:, :, :, self.offset:-self.offset]
return mask123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125import torchfrom torch import nnimport torch.nn.functional as Ffrom . import layers_new as layers
class BaseNet(nn.Module):
def __init__(self, nin, nout, nin_lstm, nout_lstm, dilations=((4, 2), (8, 4), (12, 6))): super(BaseNet, self).__init__() self.enc1 = layers.Conv2DBNActiv(nin, nout, 3, 1, 1) self.enc2 = layers.Encoder(nout, nout * 2, 3, 2, 1) self.enc3 = layers.Encoder(nout * 2, nout * 4, 3, 2, 1) self.enc4 = layers.Encoder(nout * 4, nout * 6, 3, 2, 1) self.enc5 = layers.Encoder(nout * 6, nout * 8, 3, 2, 1)
self.aspp = layers.ASPPModule(nout * 8, nout * 8, dilations, dropout=True)
self.dec4 = layers.Decoder(nout * (6 + 8), nout * 6, 3, 1, 1) self.dec3 = layers.Decoder(nout * (4 + 6), nout * 4, 3, 1, 1) self.dec2 = layers.Decoder(nout * (2 + 4), nout * 2, 3, 1, 1) self.lstm_dec2 = layers.LSTMModule(nout * 2, nin_lstm, nout_lstm) self.dec1 = layers.Decoder(nout * (1 + 2) + 1, nout * 1, 3, 1, 1)
def __call__(self, x): e1 = self.enc1(x) e2 = self.enc2(e1) e3 = self.enc3(e2) e4 = self.enc4(e3) e5 = self.enc5(e4)
h = self.aspp(e5)
h = self.dec4(h, e4) h = self.dec3(h, e3) h = self.dec2(h, e2) h = torch.cat([h, self.lstm_dec2(h)], dim=1) h = self.dec1(h, e1)
return h
class CascadedNet(nn.Module):
def __init__(self, n_fft, nn_arch_size=51000, nout=32, nout_lstm=128): super(CascadedNet, self).__init__() self.max_bin = n_fft // 2 self.output_bin = n_fft // 2 + 1 self.nin_lstm = self.max_bin // 2 self.offset = 64 nout = 64 if nn_arch_size == 218409 else nout
#print(nout, nout_lstm, n_fft)
self.stg1_low_band_net = nn.Sequential( BaseNet(2, nout // 2, self.nin_lstm // 2, nout_lstm), layers.Conv2DBNActiv(nout // 2, nout // 4, 1, 1, 0) ) self.stg1_high_band_net = BaseNet(2, nout // 4, self.nin_lstm // 2, nout_lstm // 2)
self.stg2_low_band_net = nn.Sequential( BaseNet(nout // 4 + 2, nout, self.nin_lstm // 2, nout_lstm), layers.Conv2DBNActiv(nout, nout // 2, 1, 1, 0) ) self.stg2_high_band_net = BaseNet(nout // 4 + 2, nout // 2, self.nin_lstm // 2, nout_lstm // 2)
self.stg3_full_band_net = BaseNet(3 * nout // 4 + 2, nout, self.nin_lstm, nout_lstm)
self.out = nn.Conv2d(nout, 2, 1, bias=False) self.aux_out = nn.Conv2d(3 * nout // 4, 2, 1, bias=False)
def forward(self, x): x = x[:, :, :self.max_bin]
bandw = x.size()[2] // 2 l1_in = x[:, :, :bandw] h1_in = x[:, :, bandw:] l1 = self.stg1_low_band_net(l1_in) h1 = self.stg1_high_band_net(h1_in) aux1 = torch.cat([l1, h1], dim=2)
l2_in = torch.cat([l1_in, l1], dim=1) h2_in = torch.cat([h1_in, h1], dim=1) l2 = self.stg2_low_band_net(l2_in) h2 = self.stg2_high_band_net(h2_in) aux2 = torch.cat([l2, h2], dim=2)
f3_in = torch.cat([x, aux1, aux2], dim=1) f3 = self.stg3_full_band_net(f3_in)
mask = torch.sigmoid(self.out(f3)) mask = F.pad( input=mask, pad=(0, 0, 0, self.output_bin - mask.size()[2]), mode='replicate' )
if self.training: aux = torch.cat([aux1, aux2], dim=1) aux = torch.sigmoid(self.aux_out(aux)) aux = F.pad( input=aux, pad=(0, 0, 0, self.output_bin - aux.size()[2]), mode='replicate' ) return mask, aux else: return mask
def predict_mask(self, x): mask = self.forward(x)
if self.offset > 0: mask = mask[:, :, :, self.offset:-self.offset] assert mask.size()[3] > 0
return mask
def predict(self, x): mask = self.forward(x) pred_mag = x * mask
if self.offset > 0: pred_mag = pred_mag[:, :, :, self.offset:-self.offset] assert pred_mag.size()[3] > 0
return pred_maglib_v5/mdxnet.pyDeleted-136
lib_v5/mixer.ckptDeleted-3
lib_v5/modules.pyDeleted-74
lib_v5/pyrb.pyDeleted-92
lib_v5/results.pyDeleted-48
lib_v5/spec_utils.pyDeleted-1241
lib_v5/tfc_tdf_v3.pyDeleted-253
lib_v5/vr_network/__init__.pyDeleted-1
lib_v5/vr_network/layers.pyDeleted-143
lib_v5/vr_network/layers_new.pyDeleted-126
lib_v5/vr_network/model_param_init.pyDeleted-32
lib_v5/vr_network/modelparams/1band_sr16000_hl512.jsonDeleted-19
lib_v5/vr_network/modelparams/1band_sr32000_hl512.jsonDeleted-19
lib_v5/vr_network/modelparams/1band_sr33075_hl384.jsonDeleted-19
lib_v5/vr_network/modelparams/1band_sr44100_hl1024.jsonDeleted-19
lib_v5/vr_network/modelparams/1band_sr44100_hl256.jsonDeleted-19
lib_v5/vr_network/modelparams/1band_sr44100_hl512.jsonDeleted-19
lib_v5/vr_network/modelparams/1band_sr44100_hl512_cut.jsonDeleted-19
lib_v5/vr_network/modelparams/1band_sr44100_hl512_nf1024.jsonDeleted-19
lib_v5/vr_network/modelparams/2band_32000.jsonDeleted-30
lib_v5/vr_network/modelparams/2band_44100_lofi.jsonDeleted-30
lib_v5/vr_network/modelparams/2band_48000.jsonDeleted-30
lib_v5/vr_network/modelparams/3band_44100.jsonDeleted-42
lib_v5/vr_network/modelparams/3band_44100_mid.jsonDeleted-43
lib_v5/vr_network/modelparams/3band_44100_msb2.jsonDeleted-43
lib_v5/vr_network/modelparams/4band_44100.jsonDeleted-54
lib_v5/vr_network/modelparams/4band_44100_mid.jsonDeleted-55
lib_v5/vr_network/modelparams/4band_44100_msb.jsonDeleted-55
lib_v5/vr_network/modelparams/4band_44100_msb2.jsonDeleted-55
lib_v5/vr_network/modelparams/4band_44100_reverse.jsonDeleted-55
lib_v5/vr_network/modelparams/4band_44100_sw.jsonDeleted-55
lib_v5/vr_network/modelparams/4band_v2.jsonDeleted-54
lib_v5/vr_network/modelparams/4band_v2_sn.jsonDeleted-55
lib_v5/vr_network/modelparams/4band_v3.jsonDeleted-54
lib_v5/vr_network/modelparams/4band_v3_sn.jsonDeleted-55
lib_v5/vr_network/modelparams/ensemble.jsonDeleted-43
lib_v5/vr_network/nets.pyDeleted-166
lib_v5/vr_network/nets_new.pyDeleted-125