Spaces:
Sleeping
Sleeping
issue
Browse files- vc_infer_pipeline.py +0 -647
vc_infer_pipeline.py
DELETED
|
@@ -1,647 +0,0 @@
|
|
| 1 |
-
import numpy as np, parselmouth, torch, pdb, sys, os
|
| 2 |
-
from time import time as ttime
|
| 3 |
-
import torch.nn.functional as F
|
| 4 |
-
import torchcrepe # Fork feature. Use the crepe f0 algorithm. New dependency (pip install torchcrepe)
|
| 5 |
-
from torch import Tensor
|
| 6 |
-
import scipy.signal as signal
|
| 7 |
-
import pyworld, os, traceback, faiss, librosa, torchcrepe
|
| 8 |
-
from scipy import signal
|
| 9 |
-
from functools import lru_cache
|
| 10 |
-
|
| 11 |
-
now_dir = os.getcwd()
|
| 12 |
-
sys.path.append(now_dir)
|
| 13 |
-
|
| 14 |
-
bh, ah = signal.butter(N=5, Wn=48, btype="high", fs=16000)
|
| 15 |
-
|
| 16 |
-
input_audio_path2wav = {}
|
| 17 |
-
|
| 18 |
-
#A fun little addition from my personal RVC branch.
|
| 19 |
-
#You don't have to implement it if you don't have to
|
| 20 |
-
from config import Config
|
| 21 |
-
config=Config()
|
| 22 |
-
from rmvpe import RMVPE
|
| 23 |
-
print("Preloading RMVPE model...")
|
| 24 |
-
model_rmvpe = RMVPE("rmvpe.pt", is_half=config.is_half, device=config.device)
|
| 25 |
-
del config
|
| 26 |
-
|
| 27 |
-
@lru_cache
|
| 28 |
-
def cache_harvest_f0(input_audio_path, fs, f0max, f0min, frame_period):
|
| 29 |
-
audio = input_audio_path2wav[input_audio_path]
|
| 30 |
-
f0, t = pyworld.harvest(
|
| 31 |
-
audio,
|
| 32 |
-
fs=fs,
|
| 33 |
-
f0_ceil=f0max,
|
| 34 |
-
f0_floor=f0min,
|
| 35 |
-
frame_period=frame_period,
|
| 36 |
-
)
|
| 37 |
-
f0 = pyworld.stonemask(audio, f0, t, fs)
|
| 38 |
-
return f0
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def change_rms(data1, sr1, data2, sr2, rate): # 1是输入音频,2是输出音频,rate是2的占比
|
| 42 |
-
# print(data1.max(),data2.max())
|
| 43 |
-
rms1 = librosa.feature.rms(
|
| 44 |
-
y=data1, frame_length=sr1 // 2 * 2, hop_length=sr1 // 2
|
| 45 |
-
) # 每半秒一个点
|
| 46 |
-
rms2 = librosa.feature.rms(y=data2, frame_length=sr2 // 2 * 2, hop_length=sr2 // 2)
|
| 47 |
-
rms1 = torch.from_numpy(rms1)
|
| 48 |
-
rms1 = F.interpolate(
|
| 49 |
-
rms1.unsqueeze(0), size=data2.shape[0], mode="linear"
|
| 50 |
-
).squeeze()
|
| 51 |
-
rms2 = torch.from_numpy(rms2)
|
| 52 |
-
rms2 = F.interpolate(
|
| 53 |
-
rms2.unsqueeze(0), size=data2.shape[0], mode="linear"
|
| 54 |
-
).squeeze()
|
| 55 |
-
rms2 = torch.max(rms2, torch.zeros_like(rms2) + 1e-6)
|
| 56 |
-
data2 *= (
|
| 57 |
-
torch.pow(rms1, torch.tensor(1 - rate))
|
| 58 |
-
* torch.pow(rms2, torch.tensor(rate - 1))
|
| 59 |
-
).numpy()
|
| 60 |
-
return data2
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
class VC(object):
|
| 64 |
-
def __init__(self, tgt_sr, config):
|
| 65 |
-
self.x_pad, self.x_query, self.x_center, self.x_max, self.is_half = (
|
| 66 |
-
config.x_pad,
|
| 67 |
-
config.x_query,
|
| 68 |
-
config.x_center,
|
| 69 |
-
config.x_max,
|
| 70 |
-
config.is_half,
|
| 71 |
-
)
|
| 72 |
-
self.sr = 16000 # hubert输入采样率
|
| 73 |
-
self.window = 160 # 每帧点数
|
| 74 |
-
self.t_pad = self.sr * self.x_pad # 每条前后pad时间
|
| 75 |
-
self.t_pad_tgt = tgt_sr * self.x_pad
|
| 76 |
-
self.t_pad2 = self.t_pad * 2
|
| 77 |
-
self.t_query = self.sr * self.x_query # 查询切点前后查询时间
|
| 78 |
-
self.t_center = self.sr * self.x_center # 查询切点位置
|
| 79 |
-
self.t_max = self.sr * self.x_max # 免查询时长阈值
|
| 80 |
-
self.device = config.device
|
| 81 |
-
|
| 82 |
-
# Fork Feature: Get the best torch device to use for f0 algorithms that require a torch device. Will return the type (torch.device)
|
| 83 |
-
def get_optimal_torch_device(self, index: int = 0) -> torch.device:
|
| 84 |
-
# Get cuda device
|
| 85 |
-
if torch.cuda.is_available():
|
| 86 |
-
return torch.device(
|
| 87 |
-
f"cuda:{index % torch.cuda.device_count()}"
|
| 88 |
-
) # Very fast
|
| 89 |
-
elif torch.backends.mps.is_available():
|
| 90 |
-
return torch.device("mps")
|
| 91 |
-
# Insert an else here to grab "xla" devices if available. TO DO later. Requires the torch_xla.core.xla_model library
|
| 92 |
-
# Else wise return the "cpu" as a torch device,
|
| 93 |
-
return torch.device("cpu")
|
| 94 |
-
|
| 95 |
-
# Fork Feature: Compute f0 with the crepe method
|
| 96 |
-
def get_f0_crepe_computation(
|
| 97 |
-
self,
|
| 98 |
-
x,
|
| 99 |
-
f0_min,
|
| 100 |
-
f0_max,
|
| 101 |
-
p_len,
|
| 102 |
-
hop_length=160, # 512 before. Hop length changes the speed that the voice jumps to a different dramatic pitch. Lower hop lengths means more pitch accuracy but longer inference time.
|
| 103 |
-
model="full", # Either use crepe-tiny "tiny" or crepe "full". Default is full
|
| 104 |
-
):
|
| 105 |
-
x = x.astype(
|
| 106 |
-
np.float32
|
| 107 |
-
) # fixes the F.conv2D exception. We needed to convert double to float.
|
| 108 |
-
x /= np.quantile(np.abs(x), 0.999)
|
| 109 |
-
torch_device = self.get_optimal_torch_device()
|
| 110 |
-
audio = torch.from_numpy(x).to(torch_device, copy=True)
|
| 111 |
-
audio = torch.unsqueeze(audio, dim=0)
|
| 112 |
-
if audio.ndim == 2 and audio.shape[0] > 1:
|
| 113 |
-
audio = torch.mean(audio, dim=0, keepdim=True).detach()
|
| 114 |
-
audio = audio.detach()
|
| 115 |
-
print("Initiating prediction with a crepe_hop_length of: " + str(hop_length))
|
| 116 |
-
pitch: Tensor = torchcrepe.predict(
|
| 117 |
-
audio,
|
| 118 |
-
self.sr,
|
| 119 |
-
hop_length,
|
| 120 |
-
f0_min,
|
| 121 |
-
f0_max,
|
| 122 |
-
model,
|
| 123 |
-
batch_size=hop_length * 2,
|
| 124 |
-
device=torch_device,
|
| 125 |
-
pad=True,
|
| 126 |
-
)
|
| 127 |
-
p_len = p_len or x.shape[0] // hop_length
|
| 128 |
-
# Resize the pitch for final f0
|
| 129 |
-
source = np.array(pitch.squeeze(0).cpu().float().numpy())
|
| 130 |
-
source[source < 0.001] = np.nan
|
| 131 |
-
target = np.interp(
|
| 132 |
-
np.arange(0, len(source) * p_len, len(source)) / p_len,
|
| 133 |
-
np.arange(0, len(source)),
|
| 134 |
-
source,
|
| 135 |
-
)
|
| 136 |
-
f0 = np.nan_to_num(target)
|
| 137 |
-
return f0 # Resized f0
|
| 138 |
-
|
| 139 |
-
def get_f0_official_crepe_computation(
|
| 140 |
-
self,
|
| 141 |
-
x,
|
| 142 |
-
f0_min,
|
| 143 |
-
f0_max,
|
| 144 |
-
model="full",
|
| 145 |
-
):
|
| 146 |
-
# Pick a batch size that doesn't cause memory errors on your gpu
|
| 147 |
-
batch_size = 512
|
| 148 |
-
# Compute pitch using first gpu
|
| 149 |
-
audio = torch.tensor(np.copy(x))[None].float()
|
| 150 |
-
f0, pd = torchcrepe.predict(
|
| 151 |
-
audio,
|
| 152 |
-
self.sr,
|
| 153 |
-
self.window,
|
| 154 |
-
f0_min,
|
| 155 |
-
f0_max,
|
| 156 |
-
model,
|
| 157 |
-
batch_size=batch_size,
|
| 158 |
-
device=self.device,
|
| 159 |
-
return_periodicity=True,
|
| 160 |
-
)
|
| 161 |
-
pd = torchcrepe.filter.median(pd, 3)
|
| 162 |
-
f0 = torchcrepe.filter.mean(f0, 3)
|
| 163 |
-
f0[pd < 0.1] = 0
|
| 164 |
-
f0 = f0[0].cpu().numpy()
|
| 165 |
-
return f0
|
| 166 |
-
|
| 167 |
-
# Fork Feature: Compute pYIN f0 method
|
| 168 |
-
def get_f0_pyin_computation(self, x, f0_min, f0_max):
|
| 169 |
-
y, sr = librosa.load("saudio/Sidney.wav", self.sr, mono=True)
|
| 170 |
-
f0, _, _ = librosa.pyin(y, sr=self.sr, fmin=f0_min, fmax=f0_max)
|
| 171 |
-
f0 = f0[1:] # Get rid of extra first frame
|
| 172 |
-
return f0
|
| 173 |
-
|
| 174 |
-
# Fork Feature: Acquire median hybrid f0 estimation calculation
|
| 175 |
-
def get_f0_hybrid_computation(
|
| 176 |
-
self,
|
| 177 |
-
methods_str,
|
| 178 |
-
input_audio_path,
|
| 179 |
-
x,
|
| 180 |
-
f0_min,
|
| 181 |
-
f0_max,
|
| 182 |
-
p_len,
|
| 183 |
-
filter_radius,
|
| 184 |
-
crepe_hop_length,
|
| 185 |
-
time_step,
|
| 186 |
-
):
|
| 187 |
-
# Get various f0 methods from input to use in the computation stack
|
| 188 |
-
s = methods_str
|
| 189 |
-
s = s.split("hybrid")[1]
|
| 190 |
-
s = s.replace("[", "").replace("]", "")
|
| 191 |
-
methods = s.split("+")
|
| 192 |
-
f0_computation_stack = []
|
| 193 |
-
|
| 194 |
-
print("Calculating f0 pitch estimations for methods: %s" % str(methods))
|
| 195 |
-
x = x.astype(np.float32)
|
| 196 |
-
x /= np.quantile(np.abs(x), 0.999)
|
| 197 |
-
# Get f0 calculations for all methods specified
|
| 198 |
-
for method in methods:
|
| 199 |
-
f0 = None
|
| 200 |
-
if method == "pm":
|
| 201 |
-
f0 = (
|
| 202 |
-
parselmouth.Sound(x, self.sr)
|
| 203 |
-
.to_pitch_ac(
|
| 204 |
-
time_step=time_step / 1000,
|
| 205 |
-
voicing_threshold=0.6,
|
| 206 |
-
pitch_floor=f0_min,
|
| 207 |
-
pitch_ceiling=f0_max,
|
| 208 |
-
)
|
| 209 |
-
.selected_array["frequency"]
|
| 210 |
-
)
|
| 211 |
-
pad_size = (p_len - len(f0) + 1) // 2
|
| 212 |
-
if pad_size > 0 or p_len - len(f0) - pad_size > 0:
|
| 213 |
-
f0 = np.pad(
|
| 214 |
-
f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant"
|
| 215 |
-
)
|
| 216 |
-
elif method == "crepe":
|
| 217 |
-
f0 = self.get_f0_official_crepe_computation(x, f0_min, f0_max)
|
| 218 |
-
f0 = f0[1:] # Get rid of extra first frame
|
| 219 |
-
elif method == "crepe-tiny":
|
| 220 |
-
f0 = self.get_f0_official_crepe_computation(x, f0_min, f0_max, "tiny")
|
| 221 |
-
f0 = f0[1:] # Get rid of extra first frame
|
| 222 |
-
elif method == "mangio-crepe":
|
| 223 |
-
f0 = self.get_f0_crepe_computation(
|
| 224 |
-
x, f0_min, f0_max, p_len, crepe_hop_length
|
| 225 |
-
)
|
| 226 |
-
elif method == "mangio-crepe-tiny":
|
| 227 |
-
f0 = self.get_f0_crepe_computation(
|
| 228 |
-
x, f0_min, f0_max, p_len, crepe_hop_length, "tiny"
|
| 229 |
-
)
|
| 230 |
-
elif method == "harvest":
|
| 231 |
-
f0 = cache_harvest_f0(input_audio_path, self.sr, f0_max, f0_min, 10)
|
| 232 |
-
if filter_radius > 2:
|
| 233 |
-
f0 = signal.medfilt(f0, 3)
|
| 234 |
-
f0 = f0[1:] # Get rid of first frame.
|
| 235 |
-
elif method == "dio": # Potentially buggy?
|
| 236 |
-
f0, t = pyworld.dio(
|
| 237 |
-
x.astype(np.double),
|
| 238 |
-
fs=self.sr,
|
| 239 |
-
f0_ceil=f0_max,
|
| 240 |
-
f0_floor=f0_min,
|
| 241 |
-
frame_period=10,
|
| 242 |
-
)
|
| 243 |
-
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.sr)
|
| 244 |
-
f0 = signal.medfilt(f0, 3)
|
| 245 |
-
f0 = f0[1:]
|
| 246 |
-
# elif method == "pyin": Not Working just yet
|
| 247 |
-
# f0 = self.get_f0_pyin_computation(x, f0_min, f0_max)
|
| 248 |
-
# Push method to the stack
|
| 249 |
-
f0_computation_stack.append(f0)
|
| 250 |
-
|
| 251 |
-
for fc in f0_computation_stack:
|
| 252 |
-
print(len(fc))
|
| 253 |
-
|
| 254 |
-
print("Calculating hybrid median f0 from the stack of: %s" % str(methods))
|
| 255 |
-
f0_median_hybrid = None
|
| 256 |
-
if len(f0_computation_stack) == 1:
|
| 257 |
-
f0_median_hybrid = f0_computation_stack[0]
|
| 258 |
-
else:
|
| 259 |
-
f0_median_hybrid = np.nanmedian(f0_computation_stack, axis=0)
|
| 260 |
-
return f0_median_hybrid
|
| 261 |
-
|
| 262 |
-
def get_f0(
|
| 263 |
-
self,
|
| 264 |
-
input_audio_path,
|
| 265 |
-
x,
|
| 266 |
-
p_len,
|
| 267 |
-
f0_up_key,
|
| 268 |
-
f0_method,
|
| 269 |
-
filter_radius,
|
| 270 |
-
crepe_hop_length,
|
| 271 |
-
inp_f0=None,
|
| 272 |
-
):
|
| 273 |
-
global input_audio_path2wav
|
| 274 |
-
time_step = self.window / self.sr * 1000
|
| 275 |
-
f0_min = 50
|
| 276 |
-
f0_max = 1100
|
| 277 |
-
f0_mel_min = 1127 * np.log(1 + f0_min / 700)
|
| 278 |
-
f0_mel_max = 1127 * np.log(1 + f0_max / 700)
|
| 279 |
-
if f0_method == "pm":
|
| 280 |
-
f0 = (
|
| 281 |
-
parselmouth.Sound(x, self.sr)
|
| 282 |
-
.to_pitch_ac(
|
| 283 |
-
time_step=time_step / 1000,
|
| 284 |
-
voicing_threshold=0.6,
|
| 285 |
-
pitch_floor=f0_min,
|
| 286 |
-
pitch_ceiling=f0_max,
|
| 287 |
-
)
|
| 288 |
-
.selected_array["frequency"]
|
| 289 |
-
)
|
| 290 |
-
pad_size = (p_len - len(f0) + 1) // 2
|
| 291 |
-
if pad_size > 0 or p_len - len(f0) - pad_size > 0:
|
| 292 |
-
f0 = np.pad(
|
| 293 |
-
f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant"
|
| 294 |
-
)
|
| 295 |
-
elif f0_method == "harvest":
|
| 296 |
-
input_audio_path2wav[input_audio_path] = x.astype(np.double)
|
| 297 |
-
f0 = cache_harvest_f0(input_audio_path, self.sr, f0_max, f0_min, 10)
|
| 298 |
-
if filter_radius > 2:
|
| 299 |
-
f0 = signal.medfilt(f0, 3)
|
| 300 |
-
elif f0_method == "dio": # Potentially Buggy?
|
| 301 |
-
f0, t = pyworld.dio(
|
| 302 |
-
x.astype(np.double),
|
| 303 |
-
fs=self.sr,
|
| 304 |
-
f0_ceil=f0_max,
|
| 305 |
-
f0_floor=f0_min,
|
| 306 |
-
frame_period=10,
|
| 307 |
-
)
|
| 308 |
-
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.sr)
|
| 309 |
-
f0 = signal.medfilt(f0, 3)
|
| 310 |
-
elif f0_method == "crepe":
|
| 311 |
-
f0 = self.get_f0_official_crepe_computation(x, f0_min, f0_max)
|
| 312 |
-
elif f0_method == "crepe-tiny":
|
| 313 |
-
f0 = self.get_f0_official_crepe_computation(x, f0_min, f0_max, "tiny")
|
| 314 |
-
elif f0_method == "mangio-crepe":
|
| 315 |
-
f0 = self.get_f0_crepe_computation(
|
| 316 |
-
x, f0_min, f0_max, p_len, crepe_hop_length
|
| 317 |
-
)
|
| 318 |
-
elif f0_method == "mangio-crepe-tiny":
|
| 319 |
-
f0 = self.get_f0_crepe_computation(
|
| 320 |
-
x, f0_min, f0_max, p_len, crepe_hop_length, "tiny"
|
| 321 |
-
)
|
| 322 |
-
elif f0_method == "rmvpe":
|
| 323 |
-
f0 = model_rmvpe.infer_from_audio(x, thred=0.03)
|
| 324 |
-
|
| 325 |
-
elif "hybrid" in f0_method:
|
| 326 |
-
# Perform hybrid median pitch estimation
|
| 327 |
-
input_audio_path2wav[input_audio_path] = x.astype(np.double)
|
| 328 |
-
f0 = self.get_f0_hybrid_computation(
|
| 329 |
-
f0_method,
|
| 330 |
-
input_audio_path,
|
| 331 |
-
x,
|
| 332 |
-
f0_min,
|
| 333 |
-
f0_max,
|
| 334 |
-
p_len,
|
| 335 |
-
filter_radius,
|
| 336 |
-
crepe_hop_length,
|
| 337 |
-
time_step,
|
| 338 |
-
)
|
| 339 |
-
|
| 340 |
-
f0 *= pow(2, f0_up_key / 12)
|
| 341 |
-
# with open("test.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()]))
|
| 342 |
-
tf0 = self.sr // self.window # 每秒f0点数
|
| 343 |
-
if inp_f0 is not None:
|
| 344 |
-
delta_t = np.round(
|
| 345 |
-
(inp_f0[:, 0].max() - inp_f0[:, 0].min()) * tf0 + 1
|
| 346 |
-
).astype("int16")
|
| 347 |
-
replace_f0 = np.interp(
|
| 348 |
-
list(range(delta_t)), inp_f0[:, 0] * 100, inp_f0[:, 1]
|
| 349 |
-
)
|
| 350 |
-
shape = f0[self.x_pad * tf0 : self.x_pad * tf0 + len(replace_f0)].shape[0]
|
| 351 |
-
f0[self.x_pad * tf0 : self.x_pad * tf0 + len(replace_f0)] = replace_f0[
|
| 352 |
-
:shape
|
| 353 |
-
]
|
| 354 |
-
# with open("test_opt.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()]))
|
| 355 |
-
f0bak = f0.copy()
|
| 356 |
-
f0_mel = 1127 * np.log(1 + f0 / 700)
|
| 357 |
-
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * 254 / (
|
| 358 |
-
f0_mel_max - f0_mel_min
|
| 359 |
-
) + 1
|
| 360 |
-
f0_mel[f0_mel <= 1] = 1
|
| 361 |
-
f0_mel[f0_mel > 255] = 255
|
| 362 |
-
f0_coarse = np.rint(f0_mel).astype(int)
|
| 363 |
-
|
| 364 |
-
return f0_coarse, f0bak # 1-0
|
| 365 |
-
|
| 366 |
-
def vc(
|
| 367 |
-
self,
|
| 368 |
-
model,
|
| 369 |
-
net_g,
|
| 370 |
-
sid,
|
| 371 |
-
audio0,
|
| 372 |
-
pitch,
|
| 373 |
-
pitchf,
|
| 374 |
-
times,
|
| 375 |
-
index,
|
| 376 |
-
big_npy,
|
| 377 |
-
index_rate,
|
| 378 |
-
version,
|
| 379 |
-
protect,
|
| 380 |
-
): # ,file_index,file_big_npy
|
| 381 |
-
feats = torch.from_numpy(audio0)
|
| 382 |
-
if self.is_half:
|
| 383 |
-
feats = feats.half()
|
| 384 |
-
else:
|
| 385 |
-
feats = feats.float()
|
| 386 |
-
if feats.dim() == 2: # double channels
|
| 387 |
-
feats = feats.mean(-1)
|
| 388 |
-
assert feats.dim() == 1, feats.dim()
|
| 389 |
-
feats = feats.view(1, -1)
|
| 390 |
-
padding_mask = torch.BoolTensor(feats.shape).to(self.device).fill_(False)
|
| 391 |
-
|
| 392 |
-
inputs = {
|
| 393 |
-
"source": feats.to(self.device),
|
| 394 |
-
"padding_mask": padding_mask,
|
| 395 |
-
"output_layer": 9 if version == "v1" else 12,
|
| 396 |
-
}
|
| 397 |
-
t0 = ttime()
|
| 398 |
-
with torch.no_grad():
|
| 399 |
-
logits = model.extract_features(**inputs)
|
| 400 |
-
feats = model.final_proj(logits[0]) if version == "v1" else logits[0]
|
| 401 |
-
if protect < 0.5 and pitch != None and pitchf != None:
|
| 402 |
-
feats0 = feats.clone()
|
| 403 |
-
if (
|
| 404 |
-
isinstance(index, type(None)) == False
|
| 405 |
-
and isinstance(big_npy, type(None)) == False
|
| 406 |
-
and index_rate != 0
|
| 407 |
-
):
|
| 408 |
-
npy = feats[0].cpu().numpy()
|
| 409 |
-
if self.is_half:
|
| 410 |
-
npy = npy.astype("float32")
|
| 411 |
-
|
| 412 |
-
# _, I = index.search(npy, 1)
|
| 413 |
-
# npy = big_npy[I.squeeze()]
|
| 414 |
-
|
| 415 |
-
score, ix = index.search(npy, k=8)
|
| 416 |
-
weight = np.square(1 / score)
|
| 417 |
-
weight /= weight.sum(axis=1, keepdims=True)
|
| 418 |
-
npy = np.sum(big_npy[ix] * np.expand_dims(weight, axis=2), axis=1)
|
| 419 |
-
|
| 420 |
-
if self.is_half:
|
| 421 |
-
npy = npy.astype("float16")
|
| 422 |
-
feats = (
|
| 423 |
-
torch.from_numpy(npy).unsqueeze(0).to(self.device) * index_rate
|
| 424 |
-
+ (1 - index_rate) * feats
|
| 425 |
-
)
|
| 426 |
-
|
| 427 |
-
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
| 428 |
-
if protect < 0.5 and pitch != None and pitchf != None:
|
| 429 |
-
feats0 = F.interpolate(feats0.permute(0, 2, 1), scale_factor=2).permute(
|
| 430 |
-
0, 2, 1
|
| 431 |
-
)
|
| 432 |
-
t1 = ttime()
|
| 433 |
-
p_len = audio0.shape[0] // self.window
|
| 434 |
-
if feats.shape[1] < p_len:
|
| 435 |
-
p_len = feats.shape[1]
|
| 436 |
-
if pitch != None and pitchf != None:
|
| 437 |
-
pitch = pitch[:, :p_len]
|
| 438 |
-
pitchf = pitchf[:, :p_len]
|
| 439 |
-
|
| 440 |
-
if protect < 0.5 and pitch != None and pitchf != None:
|
| 441 |
-
pitchff = pitchf.clone()
|
| 442 |
-
pitchff[pitchf > 0] = 1
|
| 443 |
-
pitchff[pitchf < 1] = protect
|
| 444 |
-
pitchff = pitchff.unsqueeze(-1)
|
| 445 |
-
feats = feats * pitchff + feats0 * (1 - pitchff)
|
| 446 |
-
feats = feats.to(feats0.dtype)
|
| 447 |
-
p_len = torch.tensor([p_len], device=self.device).long()
|
| 448 |
-
with torch.no_grad():
|
| 449 |
-
if pitch != None and pitchf != None:
|
| 450 |
-
audio1 = (
|
| 451 |
-
(net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0])
|
| 452 |
-
.data.cpu()
|
| 453 |
-
.float()
|
| 454 |
-
.numpy()
|
| 455 |
-
)
|
| 456 |
-
else:
|
| 457 |
-
audio1 = (
|
| 458 |
-
(net_g.infer(feats, p_len, sid)[0][0, 0]).data.cpu().float().numpy()
|
| 459 |
-
)
|
| 460 |
-
del feats, p_len, padding_mask
|
| 461 |
-
if torch.cuda.is_available():
|
| 462 |
-
torch.cuda.empty_cache()
|
| 463 |
-
t2 = ttime()
|
| 464 |
-
times[0] += t1 - t0
|
| 465 |
-
times[2] += t2 - t1
|
| 466 |
-
return audio1
|
| 467 |
-
|
| 468 |
-
def pipeline(
|
| 469 |
-
self,
|
| 470 |
-
model,
|
| 471 |
-
net_g,
|
| 472 |
-
sid,
|
| 473 |
-
audio,
|
| 474 |
-
input_audio_path,
|
| 475 |
-
times,
|
| 476 |
-
f0_up_key,
|
| 477 |
-
f0_method,
|
| 478 |
-
file_index,
|
| 479 |
-
# file_big_npy,
|
| 480 |
-
index_rate,
|
| 481 |
-
if_f0,
|
| 482 |
-
filter_radius,
|
| 483 |
-
tgt_sr,
|
| 484 |
-
resample_sr,
|
| 485 |
-
rms_mix_rate,
|
| 486 |
-
version,
|
| 487 |
-
protect,
|
| 488 |
-
crepe_hop_length,
|
| 489 |
-
f0_file=None,
|
| 490 |
-
):
|
| 491 |
-
if (
|
| 492 |
-
file_index != ""
|
| 493 |
-
# and file_big_npy != ""
|
| 494 |
-
# and os.path.exists(file_big_npy) == True
|
| 495 |
-
and os.path.exists(file_index) == True
|
| 496 |
-
and index_rate != 0
|
| 497 |
-
):
|
| 498 |
-
try:
|
| 499 |
-
index = faiss.read_index(file_index)
|
| 500 |
-
# big_npy = np.load(file_big_npy)
|
| 501 |
-
big_npy = index.reconstruct_n(0, index.ntotal)
|
| 502 |
-
except:
|
| 503 |
-
traceback.print_exc()
|
| 504 |
-
index = big_npy = None
|
| 505 |
-
else:
|
| 506 |
-
index = big_npy = None
|
| 507 |
-
audio = signal.filtfilt(bh, ah, audio)
|
| 508 |
-
audio_pad = np.pad(audio, (self.window // 2, self.window // 2), mode="reflect")
|
| 509 |
-
opt_ts = []
|
| 510 |
-
if audio_pad.shape[0] > self.t_max:
|
| 511 |
-
audio_sum = np.zeros_like(audio)
|
| 512 |
-
for i in range(self.window):
|
| 513 |
-
audio_sum += audio_pad[i : i - self.window]
|
| 514 |
-
for t in range(self.t_center, audio.shape[0], self.t_center):
|
| 515 |
-
opt_ts.append(
|
| 516 |
-
t
|
| 517 |
-
- self.t_query
|
| 518 |
-
+ np.where(
|
| 519 |
-
np.abs(audio_sum[t - self.t_query : t + self.t_query])
|
| 520 |
-
== np.abs(audio_sum[t - self.t_query : t + self.t_query]).min()
|
| 521 |
-
)[0][0]
|
| 522 |
-
)
|
| 523 |
-
s = 0
|
| 524 |
-
audio_opt = []
|
| 525 |
-
t = None
|
| 526 |
-
t1 = ttime()
|
| 527 |
-
audio_pad = np.pad(audio, (self.t_pad, self.t_pad), mode="reflect")
|
| 528 |
-
p_len = audio_pad.shape[0] // self.window
|
| 529 |
-
inp_f0 = None
|
| 530 |
-
if hasattr(f0_file, "name") == True:
|
| 531 |
-
try:
|
| 532 |
-
with open(f0_file.name, "r") as f:
|
| 533 |
-
lines = f.read().strip("\n").split("\n")
|
| 534 |
-
inp_f0 = []
|
| 535 |
-
for line in lines:
|
| 536 |
-
inp_f0.append([float(i) for i in line.split(",")])
|
| 537 |
-
inp_f0 = np.array(inp_f0, dtype="float32")
|
| 538 |
-
except:
|
| 539 |
-
traceback.print_exc()
|
| 540 |
-
sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()
|
| 541 |
-
pitch, pitchf = None, None
|
| 542 |
-
if if_f0 == 1:
|
| 543 |
-
pitch, pitchf = self.get_f0(
|
| 544 |
-
input_audio_path,
|
| 545 |
-
audio_pad,
|
| 546 |
-
p_len,
|
| 547 |
-
f0_up_key,
|
| 548 |
-
f0_method,
|
| 549 |
-
filter_radius,
|
| 550 |
-
crepe_hop_length,
|
| 551 |
-
inp_f0,
|
| 552 |
-
)
|
| 553 |
-
pitch = pitch[:p_len]
|
| 554 |
-
pitchf = pitchf[:p_len]
|
| 555 |
-
if self.device == "mps":
|
| 556 |
-
pitchf = pitchf.astype(np.float32)
|
| 557 |
-
pitch = torch.tensor(pitch, device=self.device).unsqueeze(0).long()
|
| 558 |
-
pitchf = torch.tensor(pitchf, device=self.device).unsqueeze(0).float()
|
| 559 |
-
t2 = ttime()
|
| 560 |
-
times[1] += t2 - t1
|
| 561 |
-
for t in opt_ts:
|
| 562 |
-
t = t // self.window * self.window
|
| 563 |
-
if if_f0 == 1:
|
| 564 |
-
audio_opt.append(
|
| 565 |
-
self.vc(
|
| 566 |
-
model,
|
| 567 |
-
net_g,
|
| 568 |
-
sid,
|
| 569 |
-
audio_pad[s : t + self.t_pad2 + self.window],
|
| 570 |
-
pitch[:, s // self.window : (t + self.t_pad2) // self.window],
|
| 571 |
-
pitchf[:, s // self.window : (t + self.t_pad2) // self.window],
|
| 572 |
-
times,
|
| 573 |
-
index,
|
| 574 |
-
big_npy,
|
| 575 |
-
index_rate,
|
| 576 |
-
version,
|
| 577 |
-
protect,
|
| 578 |
-
)[self.t_pad_tgt : -self.t_pad_tgt]
|
| 579 |
-
)
|
| 580 |
-
else:
|
| 581 |
-
audio_opt.append(
|
| 582 |
-
self.vc(
|
| 583 |
-
model,
|
| 584 |
-
net_g,
|
| 585 |
-
sid,
|
| 586 |
-
audio_pad[s : t + self.t_pad2 + self.window],
|
| 587 |
-
None,
|
| 588 |
-
None,
|
| 589 |
-
times,
|
| 590 |
-
index,
|
| 591 |
-
big_npy,
|
| 592 |
-
index_rate,
|
| 593 |
-
version,
|
| 594 |
-
protect,
|
| 595 |
-
)[self.t_pad_tgt : -self.t_pad_tgt]
|
| 596 |
-
)
|
| 597 |
-
s = t
|
| 598 |
-
if if_f0 == 1:
|
| 599 |
-
audio_opt.append(
|
| 600 |
-
self.vc(
|
| 601 |
-
model,
|
| 602 |
-
net_g,
|
| 603 |
-
sid,
|
| 604 |
-
audio_pad[t:],
|
| 605 |
-
pitch[:, t // self.window :] if t is not None else pitch,
|
| 606 |
-
pitchf[:, t // self.window :] if t is not None else pitchf,
|
| 607 |
-
times,
|
| 608 |
-
index,
|
| 609 |
-
big_npy,
|
| 610 |
-
index_rate,
|
| 611 |
-
version,
|
| 612 |
-
protect,
|
| 613 |
-
)[self.t_pad_tgt : -self.t_pad_tgt]
|
| 614 |
-
)
|
| 615 |
-
else:
|
| 616 |
-
audio_opt.append(
|
| 617 |
-
self.vc(
|
| 618 |
-
model,
|
| 619 |
-
net_g,
|
| 620 |
-
sid,
|
| 621 |
-
audio_pad[t:],
|
| 622 |
-
None,
|
| 623 |
-
None,
|
| 624 |
-
times,
|
| 625 |
-
index,
|
| 626 |
-
big_npy,
|
| 627 |
-
index_rate,
|
| 628 |
-
version,
|
| 629 |
-
protect,
|
| 630 |
-
)[self.t_pad_tgt : -self.t_pad_tgt]
|
| 631 |
-
)
|
| 632 |
-
audio_opt = np.concatenate(audio_opt)
|
| 633 |
-
if rms_mix_rate != 1:
|
| 634 |
-
audio_opt = change_rms(audio, 16000, audio_opt, tgt_sr, rms_mix_rate)
|
| 635 |
-
if resample_sr >= 16000 and tgt_sr != resample_sr:
|
| 636 |
-
audio_opt = librosa.resample(
|
| 637 |
-
audio_opt, orig_sr=tgt_sr, target_sr=resample_sr
|
| 638 |
-
)
|
| 639 |
-
audio_max = np.abs(audio_opt).max() / 0.99
|
| 640 |
-
max_int16 = 32768
|
| 641 |
-
if audio_max > 1:
|
| 642 |
-
max_int16 /= audio_max
|
| 643 |
-
audio_opt = (audio_opt * max_int16).astype(np.int16)
|
| 644 |
-
del pitch, pitchf, sid
|
| 645 |
-
if torch.cuda.is_available():
|
| 646 |
-
torch.cuda.empty_cache()
|
| 647 |
-
return audio_opt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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