Spaces:
Running
Running
Update src/vc_infer_pipeline.py
Browse files1234567891011121322 unmodified lines3637383940414243444546474849505152535423 unmodified lines7879808182838485867 unmodified lines94959697989910010110249 unmodified lines15215315415515615715815916016116216316 unmodified lines18018118218318418518618740 unmodified lines2282292302312322332342352362372389 unmodified lines24824925025125225325425525611 unmodified lines268269270271272273274275276277198 unmodified lines47647747847948048148248348448548648748848949049149228 unmodified lines5215225235245255265275286 unmodified lines535536537538539540import numpy as np, parselmouth, torch, pdb, sys, osfrom time import time as ttimeimport torch.nn.functional as Fimport torchcrepefrom torch import Tensorimport scipy.signal as signalimport pyworld, os, traceback, faiss, librosa, torchcrepefrom scipy import signalfrom functools import lru_cacheimport gc, reBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))now_dir = os.path.join(BASE_DIR, 'src')22 unmodified linesdef change_rms(data1, sr1, data2, sr2, rate):rms1 = librosa.feature.rms(y=data1, frame_length=sr1 // 2 * 2, hop_length=sr1 // 2)rms2 = librosa.feature.rms(y=data2, frame_length=sr2 // 2 * 2, hop_length=sr2 // 2)rms1 = torch.from_numpy(rms1)rms1 = F.interpolate(rms1.unsqueeze(0), size=data2.shape[0], mode="linear").squeeze()rms2 = torch.from_numpy(rms2)rms2 = F.interpolate(rms2.unsqueeze(0), size=data2.shape[0], mode="linear").squeeze()rms2 = torch.max(rms2, torch.zeros_like(rms2) + 1e-6)data2 *= (torch.pow(rms1, torch.tensor(1 - rate))* torch.pow(rms2, torch.tensor(rate - 1))23 unmodified linesdef get_optimal_torch_device(self, index: int = 0) -> torch.device:if torch.cuda.is_available():return torch.device(f"cuda:{index % torch.cuda.device_count()}")elif torch.backends.mps.is_available():return torch.device("mps")return torch.device("cpu")7 unmodified lineshop_length=160,model="full",):x = x.astype(np.float32)x /= np.quantile(np.abs(x), 0.999)torch_device = self.get_optimal_torch_device()audio = torch.from_numpy(x).to(torch_device, copy=True)49 unmodified linesf0 = f0[0].cpu().numpy()return f0def get_f0_pyin_computation(self, x, f0_min, f0_max):y, sr = librosa.load("saudio/Sidney.wav", self.sr, mono=True)f0, _, _ = librosa.pyin(y, sr=self.sr, fmin=f0_min, fmax=f0_max)f0 = f0[1:]return f0def get_f0_hybrid_computation(self,methods_str,16 unmodified linesfor method in methods:f0 = Noneif method == "crepe":f0 = self.get_f0_official_crepe_computation(x, f0_min, f0_max)f0 = f0[1:]elif method == "mangio-crepe":f0 = self.get_f0_crepe_computation(x, f0_min, f0_max, p_len, crepe_hop_length40 unmodified linesfilter_radius,crepe_hop_length,inp_f0=None,):global input_audio_path2wavtime_step = self.window / self.sr * 1000f0_min = 50f0_max = 1100f0_mel_min = 1127 * np.log(1 + f0_min / 700)f0_mel_max = 1127 * np.log(1 + f0_max / 700)if f0_method == "pm":9 unmodified lines)pad_size = (p_len - len(f0) + 1) // 2if pad_size > 0 or p_len - len(f0) - pad_size > 0:f0 = np.pad(f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant")elif f0_method == "harvest":input_audio_path2wav[input_audio_path] = x.astype(np.double)11 unmodified lines)f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.sr)f0 = signal.medfilt(f0, 3)elif f0_method == "crepe":f0 = self.get_f0_official_crepe_computation(x, f0_min, f0_max)elif f0_method == "mangio-crepe":f0 = self.get_f0_crepe_computation(x, f0_min, f0_max, p_len, crepe_hop_length)198 unmodified linesprotect,crepe_hop_length,f0_file=None,):if (file_index != ""and os.path.exists(file_index) == Trueand index_rate != 0):try:index = faiss.read_index(file_index)big_npy = index.reconstruct_n(0, index.ntotal)except:traceback.print_exc()index = big_npy = Noneelse:index = big_npy = None28 unmodified linesfor line in lines:inp_f0.append([float(i) for i in line.split(",")])inp_f0 = np.array(inp_f0, dtype="float32")except:traceback.print_exc()sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()pitch, pitchf = None, Noneif if_f0 == 1:6 unmodified linesfilter_radius,crepe_hop_length,inp_f0,)pitch = pitch[:p_len]pitchf = pitchf[:p_len]123456789101112131422 unmodified lines373839404142434445464748495051525354555623 unmodified lines808182838485867 unmodified lines94959697989910049 unmodified lines15015115215315415516 unmodified lines17217317417517617717817918040 unmodified lines2212222232242252262272282292302312322339 unmodified lines24324424524624724824911 unmodified lines261262263264265266267268269270198 unmodified lines46947047147247347447547647747847948048148248328 unmodified lines5125135145155165175185196 unmodified lines526527528529530531532533from functools import lru_cacheimport numpy as np, parselmouth, torch, pdb, sys, osfrom time import time as ttimeimport torch.nn.functional as Fimport torchcrepefrom scipy import signalfrom torch import Tensorimport pyworld, os, faiss, librosa, torchcrepeimport randomimport gcimport reBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))now_dir = os.path.join(BASE_DIR, 'src')22 unmodified linesdef change_rms(data1, sr1, data2, sr2, rate):rms1 = librosa.feature.rms(y=data1, frame_length=sr1 // 2 * 2, hop_length=sr1 // 2)rms2 = librosa.feature.rms(y=data2, frame_length=sr2 // 2 * 2, hop_length=sr2 // 2)rms1 = torch.from_numpy(rms1)rms1 = F.interpolate(rms1.unsqueeze(0), size=data2.shape[0], mode="linear").squeeze()rms2 = torch.from_numpy(rms2)rms2 = F.interpolate(rms2.unsqueeze(0), size=data2.shape[0], mode="linear").squeeze()rms2 = torch.max(rms2, torch.zeros_like(rms2) + 1e-6)data2 *= (torch.pow(rms1, torch.tensor(1 - rate))* torch.pow(rms2, torch.tensor(rate - 1))23 unmodified linesdef get_optimal_torch_device(self, index: int = 0) -> torch.device:if torch.cuda.is_available():return torch.device(f"cuda:{index % torch.cuda.device_count()}")elif torch.backends.mps.is_available():return torch.device("mps")return torch.device("cpu")7 unmodified lineshop_length=160,model="full",):x = x.astype(np.float32)x /= np.quantile(np.abs(x), 0.999)torch_device = self.get_optimal_torch_device()audio = torch.from_numpy(x).to(torch_device, copy=True)49 unmodified linesf0 = f0[0].cpu().numpy()return f0def get_f0_hybrid_computation(self,methods_str,16 unmodified linesfor method in methods:f0 = Noneif method == "crepe":f0 = self.get_f0_crepe_computation(x, f0_min, f0_max, p_len)elif method == "mangio-crepe":f0 = self.get_f0_crepe_computation(x, f0_min, f0_max, p_len, crepe_hop_length40 unmodified linesfilter_radius,crepe_hop_length,inp_f0=None,f0_min=50,f0_max=1100,):global input_audio_path2wavtime_step = self.window / self.sr * 1000#f0_min = 50#f0_max = 1100f0_mel_min = 1127 * np.log(1 + f0_min / 700)f0_mel_max = 1127 * np.log(1 + f0_max / 700)if f0_method == "pm":9 unmodified lines)pad_size = (p_len - len(f0) + 1) // 2if pad_size > 0 or p_len - len(f0) - pad_size > 0:f0 = np.pad(f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant")elif f0_method == "harvest":input_audio_path2wav[input_audio_path] = x.astype(np.double)11 unmodified lines)f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.sr)f0 = signal.medfilt(f0, 3)elif f0_method == "crepe":f0 = self.get_f0_crepe_computation(x, f0_min, f0_max, p_len)elif f0_method == "mangio-crepe":f0 = self.get_f0_crepe_computation(x, f0_min, f0_max, p_len, crepe_hop_length)198 unmodified linesprotect,crepe_hop_length,f0_file=None,f0_min=50,f0_max=1100,):if file_index != "" and os.path.exists(file_index) == True and index_rate != 0:try:index = faiss.read_index(file_index)big_npy = index.reconstruct_n(0, index.ntotal)except Exception as error:print(error)index = big_npy = Noneelse:index = big_npy = None28 unmodified linesfor line in lines:inp_f0.append([float(i) for i in line.split(",")])inp_f0 = np.array(inp_f0, dtype="float32")except Exception as error:print(error)sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()pitch, pitchf = None, Noneif if_f0 == 1:6 unmodified linesfilter_radius,crepe_hop_length,inp_f0,f0_min,f0_max,)pitch = pitch[:p_len]pitchf = pitchf[:p_len]
src/vc_infer_pipeline.py+32-39