Spaces:
No application file
No application file
_Noxty
commited on
Update libs/rtrvc.py
Browse files- libs/rtrvc.py +461 -461
libs/rtrvc.py
CHANGED
|
@@ -1,461 +1,461 @@
|
|
| 1 |
-
from io import BytesIO
|
| 2 |
-
import os
|
| 3 |
-
import sys
|
| 4 |
-
import traceback
|
| 5 |
-
from infer.lib import jit
|
| 6 |
-
from infer.lib.jit.get_synthesizer import get_synthesizer
|
| 7 |
-
from time import time as ttime
|
| 8 |
-
import fairseq
|
| 9 |
-
import faiss
|
| 10 |
-
import numpy as np
|
| 11 |
-
import parselmouth
|
| 12 |
-
import pyworld
|
| 13 |
-
import scipy.signal as signal
|
| 14 |
-
import torch
|
| 15 |
-
import torch.nn as nn
|
| 16 |
-
import torch.nn.functional as F
|
| 17 |
-
import torchcrepe
|
| 18 |
-
from torchaudio.transforms import Resample
|
| 19 |
-
|
| 20 |
-
now_dir = os.getcwd()
|
| 21 |
-
sys.path.append(now_dir)
|
| 22 |
-
from multiprocessing import Manager as M
|
| 23 |
-
|
| 24 |
-
from
|
| 25 |
-
|
| 26 |
-
# config = Config()
|
| 27 |
-
|
| 28 |
-
mm = M()
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def printt(strr, *args):
|
| 32 |
-
if len(args) == 0:
|
| 33 |
-
print(strr)
|
| 34 |
-
else:
|
| 35 |
-
print(strr % args)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# config.device=torch.device("cpu")########强制cpu测试
|
| 39 |
-
# config.is_half=False########强制cpu测试
|
| 40 |
-
class RVC:
|
| 41 |
-
def __init__(
|
| 42 |
-
self,
|
| 43 |
-
key,
|
| 44 |
-
formant,
|
| 45 |
-
pth_path,
|
| 46 |
-
index_path,
|
| 47 |
-
index_rate,
|
| 48 |
-
n_cpu,
|
| 49 |
-
inp_q,
|
| 50 |
-
opt_q,
|
| 51 |
-
config: Config,
|
| 52 |
-
last_rvc=None,
|
| 53 |
-
) -> None:
|
| 54 |
-
"""
|
| 55 |
-
初始化
|
| 56 |
-
"""
|
| 57 |
-
try:
|
| 58 |
-
if config.dml == True:
|
| 59 |
-
|
| 60 |
-
def forward_dml(ctx, x, scale):
|
| 61 |
-
ctx.scale = scale
|
| 62 |
-
res = x.clone().detach()
|
| 63 |
-
return res
|
| 64 |
-
|
| 65 |
-
fairseq.modules.grad_multiply.GradMultiply.forward = forward_dml
|
| 66 |
-
# global config
|
| 67 |
-
self.config = config
|
| 68 |
-
self.inp_q = inp_q
|
| 69 |
-
self.opt_q = opt_q
|
| 70 |
-
# device="cpu"########强制cpu测试
|
| 71 |
-
self.device = config.device
|
| 72 |
-
self.f0_up_key = key
|
| 73 |
-
self.formant_shift = formant
|
| 74 |
-
self.f0_min = 50
|
| 75 |
-
self.f0_max = 1100
|
| 76 |
-
self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700)
|
| 77 |
-
self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700)
|
| 78 |
-
self.n_cpu = n_cpu
|
| 79 |
-
self.use_jit = self.config.use_jit
|
| 80 |
-
self.is_half = config.is_half
|
| 81 |
-
|
| 82 |
-
if index_rate != 0:
|
| 83 |
-
self.index = faiss.read_index(index_path)
|
| 84 |
-
self.big_npy = self.index.reconstruct_n(0, self.index.ntotal)
|
| 85 |
-
printt("Index search enabled")
|
| 86 |
-
self.pth_path: str = pth_path
|
| 87 |
-
self.index_path = index_path
|
| 88 |
-
self.index_rate = index_rate
|
| 89 |
-
self.cache_pitch: torch.Tensor = torch.zeros(
|
| 90 |
-
1024, device=self.device, dtype=torch.long
|
| 91 |
-
)
|
| 92 |
-
self.cache_pitchf = torch.zeros(
|
| 93 |
-
1024, device=self.device, dtype=torch.float32
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
self.resample_kernel = {}
|
| 97 |
-
|
| 98 |
-
if last_rvc is None:
|
| 99 |
-
models, _, _ = fairseq.checkpoint_utils.load_model_ensemble_and_task(
|
| 100 |
-
["assets/hubert/hubert_base.pt"],
|
| 101 |
-
suffix="",
|
| 102 |
-
)
|
| 103 |
-
hubert_model = models[0]
|
| 104 |
-
hubert_model = hubert_model.to(self.device)
|
| 105 |
-
if self.is_half:
|
| 106 |
-
hubert_model = hubert_model.half()
|
| 107 |
-
else:
|
| 108 |
-
hubert_model = hubert_model.float()
|
| 109 |
-
hubert_model.eval()
|
| 110 |
-
self.model = hubert_model
|
| 111 |
-
else:
|
| 112 |
-
self.model = last_rvc.model
|
| 113 |
-
|
| 114 |
-
self.net_g: nn.Module = None
|
| 115 |
-
|
| 116 |
-
def set_default_model():
|
| 117 |
-
self.net_g, cpt = get_synthesizer(self.pth_path, self.device)
|
| 118 |
-
self.tgt_sr = cpt["config"][-1]
|
| 119 |
-
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0]
|
| 120 |
-
self.if_f0 = cpt.get("f0", 1)
|
| 121 |
-
self.version = cpt.get("version", "v1")
|
| 122 |
-
if self.is_half:
|
| 123 |
-
self.net_g = self.net_g.half()
|
| 124 |
-
else:
|
| 125 |
-
self.net_g = self.net_g.float()
|
| 126 |
-
|
| 127 |
-
def set_jit_model():
|
| 128 |
-
jit_pth_path = self.pth_path.rstrip(".pth")
|
| 129 |
-
jit_pth_path += ".half.jit" if self.is_half else ".jit"
|
| 130 |
-
reload = False
|
| 131 |
-
if str(self.device) == "cuda":
|
| 132 |
-
self.device = torch.device("cuda:0")
|
| 133 |
-
if os.path.exists(jit_pth_path):
|
| 134 |
-
cpt = jit.load(jit_pth_path)
|
| 135 |
-
model_device = cpt["device"]
|
| 136 |
-
if model_device != str(self.device):
|
| 137 |
-
reload = True
|
| 138 |
-
else:
|
| 139 |
-
reload = True
|
| 140 |
-
|
| 141 |
-
if reload:
|
| 142 |
-
cpt = jit.synthesizer_jit_export(
|
| 143 |
-
self.pth_path,
|
| 144 |
-
"script",
|
| 145 |
-
None,
|
| 146 |
-
device=self.device,
|
| 147 |
-
is_half=self.is_half,
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
self.tgt_sr = cpt["config"][-1]
|
| 151 |
-
self.if_f0 = cpt.get("f0", 1)
|
| 152 |
-
self.version = cpt.get("version", "v1")
|
| 153 |
-
self.net_g = torch.jit.load(
|
| 154 |
-
BytesIO(cpt["model"]), map_location=self.device
|
| 155 |
-
)
|
| 156 |
-
self.net_g.infer = self.net_g.forward
|
| 157 |
-
self.net_g.eval().to(self.device)
|
| 158 |
-
|
| 159 |
-
def set_synthesizer():
|
| 160 |
-
if self.use_jit and not config.dml:
|
| 161 |
-
if self.is_half and "cpu" in str(self.device):
|
| 162 |
-
printt(
|
| 163 |
-
"Use default Synthesizer model. \
|
| 164 |
-
Jit is not supported on the CPU for half floating point"
|
| 165 |
-
)
|
| 166 |
-
set_default_model()
|
| 167 |
-
else:
|
| 168 |
-
set_jit_model()
|
| 169 |
-
else:
|
| 170 |
-
set_default_model()
|
| 171 |
-
|
| 172 |
-
if last_rvc is None or last_rvc.pth_path != self.pth_path:
|
| 173 |
-
set_synthesizer()
|
| 174 |
-
else:
|
| 175 |
-
self.tgt_sr = last_rvc.tgt_sr
|
| 176 |
-
self.if_f0 = last_rvc.if_f0
|
| 177 |
-
self.version = last_rvc.version
|
| 178 |
-
self.is_half = last_rvc.is_half
|
| 179 |
-
if last_rvc.use_jit != self.use_jit:
|
| 180 |
-
set_synthesizer()
|
| 181 |
-
else:
|
| 182 |
-
self.net_g = last_rvc.net_g
|
| 183 |
-
|
| 184 |
-
if last_rvc is not None and hasattr(last_rvc, "model_rmvpe"):
|
| 185 |
-
self.model_rmvpe = last_rvc.model_rmvpe
|
| 186 |
-
if last_rvc is not None and hasattr(last_rvc, "model_fcpe"):
|
| 187 |
-
self.device_fcpe = last_rvc.device_fcpe
|
| 188 |
-
self.model_fcpe = last_rvc.model_fcpe
|
| 189 |
-
except:
|
| 190 |
-
printt(traceback.format_exc())
|
| 191 |
-
|
| 192 |
-
def change_key(self, new_key):
|
| 193 |
-
self.f0_up_key = new_key
|
| 194 |
-
|
| 195 |
-
def change_formant(self, new_formant):
|
| 196 |
-
self.formant_shift = new_formant
|
| 197 |
-
|
| 198 |
-
def change_index_rate(self, new_index_rate):
|
| 199 |
-
if new_index_rate != 0 and self.index_rate == 0:
|
| 200 |
-
self.index = faiss.read_index(self.index_path)
|
| 201 |
-
self.big_npy = self.index.reconstruct_n(0, self.index.ntotal)
|
| 202 |
-
printt("Index search enabled")
|
| 203 |
-
self.index_rate = new_index_rate
|
| 204 |
-
|
| 205 |
-
def get_f0_post(self, f0):
|
| 206 |
-
if not torch.is_tensor(f0):
|
| 207 |
-
f0 = torch.from_numpy(f0)
|
| 208 |
-
f0 = f0.float().to(self.device).squeeze()
|
| 209 |
-
f0_mel = 1127 * torch.log(1 + f0 / 700)
|
| 210 |
-
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - self.f0_mel_min) * 254 / (
|
| 211 |
-
self.f0_mel_max - self.f0_mel_min
|
| 212 |
-
) + 1
|
| 213 |
-
f0_mel[f0_mel <= 1] = 1
|
| 214 |
-
f0_mel[f0_mel > 255] = 255
|
| 215 |
-
f0_coarse = torch.round(f0_mel).long()
|
| 216 |
-
return f0_coarse, f0
|
| 217 |
-
|
| 218 |
-
def get_f0(self, x, f0_up_key, n_cpu, method="harvest"):
|
| 219 |
-
n_cpu = int(n_cpu)
|
| 220 |
-
if method == "crepe":
|
| 221 |
-
return self.get_f0_crepe(x, f0_up_key)
|
| 222 |
-
if method == "rmvpe":
|
| 223 |
-
return self.get_f0_rmvpe(x, f0_up_key)
|
| 224 |
-
if method == "fcpe":
|
| 225 |
-
return self.get_f0_fcpe(x, f0_up_key)
|
| 226 |
-
x = x.cpu().numpy()
|
| 227 |
-
if method == "pm":
|
| 228 |
-
p_len = x.shape[0] // 160 + 1
|
| 229 |
-
f0_min = 65
|
| 230 |
-
l_pad = int(np.ceil(1.5 / f0_min * 16000))
|
| 231 |
-
r_pad = l_pad + 1
|
| 232 |
-
s = parselmouth.Sound(np.pad(x, (l_pad, r_pad)), 16000).to_pitch_ac(
|
| 233 |
-
time_step=0.01,
|
| 234 |
-
voicing_threshold=0.6,
|
| 235 |
-
pitch_floor=f0_min,
|
| 236 |
-
pitch_ceiling=1100,
|
| 237 |
-
)
|
| 238 |
-
assert np.abs(s.t1 - 1.5 / f0_min) < 0.001
|
| 239 |
-
f0 = s.selected_array["frequency"]
|
| 240 |
-
if len(f0) < p_len:
|
| 241 |
-
f0 = np.pad(f0, (0, p_len - len(f0)))
|
| 242 |
-
f0 = f0[:p_len]
|
| 243 |
-
f0 *= pow(2, f0_up_key / 12)
|
| 244 |
-
return self.get_f0_post(f0)
|
| 245 |
-
if n_cpu == 1:
|
| 246 |
-
f0, t = pyworld.harvest(
|
| 247 |
-
x.astype(np.double),
|
| 248 |
-
fs=16000,
|
| 249 |
-
f0_ceil=1100,
|
| 250 |
-
f0_floor=50,
|
| 251 |
-
frame_period=10,
|
| 252 |
-
)
|
| 253 |
-
f0 = signal.medfilt(f0, 3)
|
| 254 |
-
f0 *= pow(2, f0_up_key / 12)
|
| 255 |
-
return self.get_f0_post(f0)
|
| 256 |
-
f0bak = np.zeros(x.shape[0] // 160 + 1, dtype=np.float64)
|
| 257 |
-
length = len(x)
|
| 258 |
-
part_length = 160 * ((length // 160 - 1) // n_cpu + 1)
|
| 259 |
-
n_cpu = (length // 160 - 1) // (part_length // 160) + 1
|
| 260 |
-
ts = ttime()
|
| 261 |
-
res_f0 = mm.dict()
|
| 262 |
-
for idx in range(n_cpu):
|
| 263 |
-
tail = part_length * (idx + 1) + 320
|
| 264 |
-
if idx == 0:
|
| 265 |
-
self.inp_q.put((idx, x[:tail], res_f0, n_cpu, ts))
|
| 266 |
-
else:
|
| 267 |
-
self.inp_q.put(
|
| 268 |
-
(idx, x[part_length * idx - 320 : tail], res_f0, n_cpu, ts)
|
| 269 |
-
)
|
| 270 |
-
while 1:
|
| 271 |
-
res_ts = self.opt_q.get()
|
| 272 |
-
if res_ts == ts:
|
| 273 |
-
break
|
| 274 |
-
f0s = [i[1] for i in sorted(res_f0.items(), key=lambda x: x[0])]
|
| 275 |
-
for idx, f0 in enumerate(f0s):
|
| 276 |
-
if idx == 0:
|
| 277 |
-
f0 = f0[:-3]
|
| 278 |
-
elif idx != n_cpu - 1:
|
| 279 |
-
f0 = f0[2:-3]
|
| 280 |
-
else:
|
| 281 |
-
f0 = f0[2:]
|
| 282 |
-
f0bak[part_length * idx // 160 : part_length * idx // 160 + f0.shape[0]] = (
|
| 283 |
-
f0
|
| 284 |
-
)
|
| 285 |
-
f0bak = signal.medfilt(f0bak, 3)
|
| 286 |
-
f0bak *= pow(2, f0_up_key / 12)
|
| 287 |
-
return self.get_f0_post(f0bak)
|
| 288 |
-
|
| 289 |
-
def get_f0_crepe(self, x, f0_up_key):
|
| 290 |
-
if "privateuseone" in str(
|
| 291 |
-
self.device
|
| 292 |
-
): ###不支持dml,cpu又太慢用不成,拿fcpe顶替
|
| 293 |
-
return self.get_f0(x, f0_up_key, 1, "fcpe")
|
| 294 |
-
# printt("using crepe,device:%s"%self.device)
|
| 295 |
-
f0, pd = torchcrepe.predict(
|
| 296 |
-
x.unsqueeze(0).float(),
|
| 297 |
-
16000,
|
| 298 |
-
160,
|
| 299 |
-
self.f0_min,
|
| 300 |
-
self.f0_max,
|
| 301 |
-
"full",
|
| 302 |
-
batch_size=512,
|
| 303 |
-
# device=self.device if self.device.type!="privateuseone" else "cpu",###crepe不用半精度全部是全精度所以不愁###cpu延迟高到没法用
|
| 304 |
-
device=self.device,
|
| 305 |
-
return_periodicity=True,
|
| 306 |
-
)
|
| 307 |
-
pd = torchcrepe.filter.median(pd, 3)
|
| 308 |
-
f0 = torchcrepe.filter.mean(f0, 3)
|
| 309 |
-
f0[pd < 0.1] = 0
|
| 310 |
-
f0 *= pow(2, f0_up_key / 12)
|
| 311 |
-
return self.get_f0_post(f0)
|
| 312 |
-
|
| 313 |
-
def get_f0_rmvpe(self, x, f0_up_key):
|
| 314 |
-
if hasattr(self, "model_rmvpe") == False:
|
| 315 |
-
from infer.lib.rmvpe import RMVPE
|
| 316 |
-
|
| 317 |
-
printt("Loading rmvpe model")
|
| 318 |
-
self.model_rmvpe = RMVPE(
|
| 319 |
-
"assets/
|
| 320 |
-
is_half=self.is_half,
|
| 321 |
-
device=self.device,
|
| 322 |
-
use_jit=self.config.use_jit,
|
| 323 |
-
)
|
| 324 |
-
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
|
| 325 |
-
f0 *= pow(2, f0_up_key / 12)
|
| 326 |
-
return self.get_f0_post(f0)
|
| 327 |
-
|
| 328 |
-
def get_f0_fcpe(self, x, f0_up_key):
|
| 329 |
-
if hasattr(self, "model_fcpe") == False:
|
| 330 |
-
from torchfcpe import spawn_bundled_infer_model
|
| 331 |
-
|
| 332 |
-
printt("Loading fcpe model")
|
| 333 |
-
if "privateuseone" in str(self.device):
|
| 334 |
-
self.device_fcpe = "cpu"
|
| 335 |
-
else:
|
| 336 |
-
self.device_fcpe = self.device
|
| 337 |
-
self.model_fcpe = spawn_bundled_infer_model(self.device_fcpe)
|
| 338 |
-
f0 = self.model_fcpe.infer(
|
| 339 |
-
x.to(self.device_fcpe).unsqueeze(0).float(),
|
| 340 |
-
sr=16000,
|
| 341 |
-
decoder_mode="local_argmax",
|
| 342 |
-
threshold=0.006,
|
| 343 |
-
)
|
| 344 |
-
f0 *= pow(2, f0_up_key / 12)
|
| 345 |
-
return self.get_f0_post(f0)
|
| 346 |
-
|
| 347 |
-
def infer(
|
| 348 |
-
self,
|
| 349 |
-
input_wav: torch.Tensor,
|
| 350 |
-
block_frame_16k,
|
| 351 |
-
skip_head,
|
| 352 |
-
return_length,
|
| 353 |
-
f0method,
|
| 354 |
-
) -> np.ndarray:
|
| 355 |
-
t1 = ttime()
|
| 356 |
-
with torch.no_grad():
|
| 357 |
-
if self.config.is_half:
|
| 358 |
-
feats = input_wav.half().view(1, -1)
|
| 359 |
-
else:
|
| 360 |
-
feats = input_wav.float().view(1, -1)
|
| 361 |
-
padding_mask = torch.BoolTensor(feats.shape).to(self.device).fill_(False)
|
| 362 |
-
inputs = {
|
| 363 |
-
"source": feats,
|
| 364 |
-
"padding_mask": padding_mask,
|
| 365 |
-
"output_layer": 9 if self.version == "v1" else 12,
|
| 366 |
-
}
|
| 367 |
-
logits = self.model.extract_features(**inputs)
|
| 368 |
-
feats = (
|
| 369 |
-
self.model.final_proj(logits[0]) if self.version == "v1" else logits[0]
|
| 370 |
-
)
|
| 371 |
-
feats = torch.cat((feats, feats[:, -1:, :]), 1)
|
| 372 |
-
t2 = ttime()
|
| 373 |
-
try:
|
| 374 |
-
if hasattr(self, "index") and self.index_rate != 0:
|
| 375 |
-
npy = feats[0][skip_head // 2 :].cpu().numpy().astype("float32")
|
| 376 |
-
score, ix = self.index.search(npy, k=8)
|
| 377 |
-
if (ix >= 0).all():
|
| 378 |
-
weight = np.square(1 / score)
|
| 379 |
-
weight /= weight.sum(axis=1, keepdims=True)
|
| 380 |
-
npy = np.sum(
|
| 381 |
-
self.big_npy[ix] * np.expand_dims(weight, axis=2), axis=1
|
| 382 |
-
)
|
| 383 |
-
if self.config.is_half:
|
| 384 |
-
npy = npy.astype("float16")
|
| 385 |
-
feats[0][skip_head // 2 :] = (
|
| 386 |
-
torch.from_numpy(npy).unsqueeze(0).to(self.device)
|
| 387 |
-
* self.index_rate
|
| 388 |
-
+ (1 - self.index_rate) * feats[0][skip_head // 2 :]
|
| 389 |
-
)
|
| 390 |
-
else:
|
| 391 |
-
printt(
|
| 392 |
-
"Invalid index. You MUST use added_xxxx.index but not trained_xxxx.index!"
|
| 393 |
-
)
|
| 394 |
-
else:
|
| 395 |
-
printt("Index search FAILED or disabled")
|
| 396 |
-
except:
|
| 397 |
-
traceback.print_exc()
|
| 398 |
-
printt("Index search FAILED")
|
| 399 |
-
t3 = ttime()
|
| 400 |
-
p_len = input_wav.shape[0] // 160
|
| 401 |
-
factor = pow(2, self.formant_shift / 12)
|
| 402 |
-
return_length2 = int(np.ceil(return_length * factor))
|
| 403 |
-
if self.if_f0 == 1:
|
| 404 |
-
f0_extractor_frame = block_frame_16k + 800
|
| 405 |
-
if f0method == "rmvpe":
|
| 406 |
-
f0_extractor_frame = 5120 * ((f0_extractor_frame - 1) // 5120 + 1) - 160
|
| 407 |
-
pitch, pitchf = self.get_f0(
|
| 408 |
-
input_wav[-f0_extractor_frame:], self.f0_up_key - self.formant_shift, self.n_cpu, f0method
|
| 409 |
-
)
|
| 410 |
-
shift = block_frame_16k // 160
|
| 411 |
-
self.cache_pitch[:-shift] = self.cache_pitch[shift:].clone()
|
| 412 |
-
self.cache_pitchf[:-shift] = self.cache_pitchf[shift:].clone()
|
| 413 |
-
self.cache_pitch[4 - pitch.shape[0] :] = pitch[3:-1]
|
| 414 |
-
self.cache_pitchf[4 - pitch.shape[0] :] = pitchf[3:-1]
|
| 415 |
-
cache_pitch = self.cache_pitch[None, -p_len:]
|
| 416 |
-
cache_pitchf = self.cache_pitchf[None, -p_len:] * return_length2 / return_length
|
| 417 |
-
t4 = ttime()
|
| 418 |
-
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
| 419 |
-
feats = feats[:, :p_len, :]
|
| 420 |
-
p_len = torch.LongTensor([p_len]).to(self.device)
|
| 421 |
-
sid = torch.LongTensor([0]).to(self.device)
|
| 422 |
-
skip_head = torch.LongTensor([skip_head])
|
| 423 |
-
return_length2 = torch.LongTensor([return_length2])
|
| 424 |
-
return_length = torch.LongTensor([return_length])
|
| 425 |
-
with torch.no_grad():
|
| 426 |
-
if self.if_f0 == 1:
|
| 427 |
-
infered_audio, _, _ = self.net_g.infer(
|
| 428 |
-
feats,
|
| 429 |
-
p_len,
|
| 430 |
-
cache_pitch,
|
| 431 |
-
cache_pitchf,
|
| 432 |
-
sid,
|
| 433 |
-
skip_head,
|
| 434 |
-
return_length,
|
| 435 |
-
return_length2,
|
| 436 |
-
)
|
| 437 |
-
else:
|
| 438 |
-
infered_audio, _, _ = self.net_g.infer(
|
| 439 |
-
feats, p_len, sid, skip_head, return_length, return_length2
|
| 440 |
-
)
|
| 441 |
-
infered_audio = infered_audio.squeeze(1).float()
|
| 442 |
-
upp_res = int(np.floor(factor * self.tgt_sr // 100))
|
| 443 |
-
if upp_res != self.tgt_sr // 100:
|
| 444 |
-
if upp_res not in self.resample_kernel:
|
| 445 |
-
self.resample_kernel[upp_res] = Resample(
|
| 446 |
-
orig_freq=upp_res,
|
| 447 |
-
new_freq=self.tgt_sr // 100,
|
| 448 |
-
dtype=torch.float32,
|
| 449 |
-
).to(self.device)
|
| 450 |
-
infered_audio = self.resample_kernel[upp_res](
|
| 451 |
-
infered_audio[:, : return_length * upp_res]
|
| 452 |
-
)
|
| 453 |
-
t5 = ttime()
|
| 454 |
-
printt(
|
| 455 |
-
"Spent time: fea = %.3fs, index = %.3fs, f0 = %.3fs, model = %.3fs",
|
| 456 |
-
t2 - t1,
|
| 457 |
-
t3 - t2,
|
| 458 |
-
t4 - t3,
|
| 459 |
-
t5 - t4,
|
| 460 |
-
)
|
| 461 |
-
return infered_audio.squeeze()
|
|
|
|
| 1 |
+
from io import BytesIO
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import traceback
|
| 5 |
+
from infer.lib import jit
|
| 6 |
+
from infer.lib.jit.get_synthesizer import get_synthesizer
|
| 7 |
+
from time import time as ttime
|
| 8 |
+
import fairseq
|
| 9 |
+
import faiss
|
| 10 |
+
import numpy as np
|
| 11 |
+
import parselmouth
|
| 12 |
+
import pyworld
|
| 13 |
+
import scipy.signal as signal
|
| 14 |
+
import torch
|
| 15 |
+
import torch.nn as nn
|
| 16 |
+
import torch.nn.functional as F
|
| 17 |
+
import torchcrepe
|
| 18 |
+
from torchaudio.transforms import Resample
|
| 19 |
+
|
| 20 |
+
now_dir = os.getcwd()
|
| 21 |
+
sys.path.append(now_dir)
|
| 22 |
+
from multiprocessing import Manager as M
|
| 23 |
+
|
| 24 |
+
from config import Config
|
| 25 |
+
|
| 26 |
+
# config = Config()
|
| 27 |
+
|
| 28 |
+
mm = M()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def printt(strr, *args):
|
| 32 |
+
if len(args) == 0:
|
| 33 |
+
print(strr)
|
| 34 |
+
else:
|
| 35 |
+
print(strr % args)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# config.device=torch.device("cpu")########强制cpu测试
|
| 39 |
+
# config.is_half=False########强制cpu测试
|
| 40 |
+
class RVC:
|
| 41 |
+
def __init__(
|
| 42 |
+
self,
|
| 43 |
+
key,
|
| 44 |
+
formant,
|
| 45 |
+
pth_path,
|
| 46 |
+
index_path,
|
| 47 |
+
index_rate,
|
| 48 |
+
n_cpu,
|
| 49 |
+
inp_q,
|
| 50 |
+
opt_q,
|
| 51 |
+
config: Config,
|
| 52 |
+
last_rvc=None,
|
| 53 |
+
) -> None:
|
| 54 |
+
"""
|
| 55 |
+
初始化
|
| 56 |
+
"""
|
| 57 |
+
try:
|
| 58 |
+
if config.dml == True:
|
| 59 |
+
|
| 60 |
+
def forward_dml(ctx, x, scale):
|
| 61 |
+
ctx.scale = scale
|
| 62 |
+
res = x.clone().detach()
|
| 63 |
+
return res
|
| 64 |
+
|
| 65 |
+
fairseq.modules.grad_multiply.GradMultiply.forward = forward_dml
|
| 66 |
+
# global config
|
| 67 |
+
self.config = config
|
| 68 |
+
self.inp_q = inp_q
|
| 69 |
+
self.opt_q = opt_q
|
| 70 |
+
# device="cpu"########强制cpu测试
|
| 71 |
+
self.device = config.device
|
| 72 |
+
self.f0_up_key = key
|
| 73 |
+
self.formant_shift = formant
|
| 74 |
+
self.f0_min = 50
|
| 75 |
+
self.f0_max = 1100
|
| 76 |
+
self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700)
|
| 77 |
+
self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700)
|
| 78 |
+
self.n_cpu = n_cpu
|
| 79 |
+
self.use_jit = self.config.use_jit
|
| 80 |
+
self.is_half = config.is_half
|
| 81 |
+
|
| 82 |
+
if index_rate != 0:
|
| 83 |
+
self.index = faiss.read_index(index_path)
|
| 84 |
+
self.big_npy = self.index.reconstruct_n(0, self.index.ntotal)
|
| 85 |
+
printt("Index search enabled")
|
| 86 |
+
self.pth_path: str = pth_path
|
| 87 |
+
self.index_path = index_path
|
| 88 |
+
self.index_rate = index_rate
|
| 89 |
+
self.cache_pitch: torch.Tensor = torch.zeros(
|
| 90 |
+
1024, device=self.device, dtype=torch.long
|
| 91 |
+
)
|
| 92 |
+
self.cache_pitchf = torch.zeros(
|
| 93 |
+
1024, device=self.device, dtype=torch.float32
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
self.resample_kernel = {}
|
| 97 |
+
|
| 98 |
+
if last_rvc is None:
|
| 99 |
+
models, _, _ = fairseq.checkpoint_utils.load_model_ensemble_and_task(
|
| 100 |
+
["assets/hubert/hubert_base.pt"],
|
| 101 |
+
suffix="",
|
| 102 |
+
)
|
| 103 |
+
hubert_model = models[0]
|
| 104 |
+
hubert_model = hubert_model.to(self.device)
|
| 105 |
+
if self.is_half:
|
| 106 |
+
hubert_model = hubert_model.half()
|
| 107 |
+
else:
|
| 108 |
+
hubert_model = hubert_model.float()
|
| 109 |
+
hubert_model.eval()
|
| 110 |
+
self.model = hubert_model
|
| 111 |
+
else:
|
| 112 |
+
self.model = last_rvc.model
|
| 113 |
+
|
| 114 |
+
self.net_g: nn.Module = None
|
| 115 |
+
|
| 116 |
+
def set_default_model():
|
| 117 |
+
self.net_g, cpt = get_synthesizer(self.pth_path, self.device)
|
| 118 |
+
self.tgt_sr = cpt["config"][-1]
|
| 119 |
+
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0]
|
| 120 |
+
self.if_f0 = cpt.get("f0", 1)
|
| 121 |
+
self.version = cpt.get("version", "v1")
|
| 122 |
+
if self.is_half:
|
| 123 |
+
self.net_g = self.net_g.half()
|
| 124 |
+
else:
|
| 125 |
+
self.net_g = self.net_g.float()
|
| 126 |
+
|
| 127 |
+
def set_jit_model():
|
| 128 |
+
jit_pth_path = self.pth_path.rstrip(".pth")
|
| 129 |
+
jit_pth_path += ".half.jit" if self.is_half else ".jit"
|
| 130 |
+
reload = False
|
| 131 |
+
if str(self.device) == "cuda":
|
| 132 |
+
self.device = torch.device("cuda:0")
|
| 133 |
+
if os.path.exists(jit_pth_path):
|
| 134 |
+
cpt = jit.load(jit_pth_path)
|
| 135 |
+
model_device = cpt["device"]
|
| 136 |
+
if model_device != str(self.device):
|
| 137 |
+
reload = True
|
| 138 |
+
else:
|
| 139 |
+
reload = True
|
| 140 |
+
|
| 141 |
+
if reload:
|
| 142 |
+
cpt = jit.synthesizer_jit_export(
|
| 143 |
+
self.pth_path,
|
| 144 |
+
"script",
|
| 145 |
+
None,
|
| 146 |
+
device=self.device,
|
| 147 |
+
is_half=self.is_half,
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
self.tgt_sr = cpt["config"][-1]
|
| 151 |
+
self.if_f0 = cpt.get("f0", 1)
|
| 152 |
+
self.version = cpt.get("version", "v1")
|
| 153 |
+
self.net_g = torch.jit.load(
|
| 154 |
+
BytesIO(cpt["model"]), map_location=self.device
|
| 155 |
+
)
|
| 156 |
+
self.net_g.infer = self.net_g.forward
|
| 157 |
+
self.net_g.eval().to(self.device)
|
| 158 |
+
|
| 159 |
+
def set_synthesizer():
|
| 160 |
+
if self.use_jit and not config.dml:
|
| 161 |
+
if self.is_half and "cpu" in str(self.device):
|
| 162 |
+
printt(
|
| 163 |
+
"Use default Synthesizer model. \
|
| 164 |
+
Jit is not supported on the CPU for half floating point"
|
| 165 |
+
)
|
| 166 |
+
set_default_model()
|
| 167 |
+
else:
|
| 168 |
+
set_jit_model()
|
| 169 |
+
else:
|
| 170 |
+
set_default_model()
|
| 171 |
+
|
| 172 |
+
if last_rvc is None or last_rvc.pth_path != self.pth_path:
|
| 173 |
+
set_synthesizer()
|
| 174 |
+
else:
|
| 175 |
+
self.tgt_sr = last_rvc.tgt_sr
|
| 176 |
+
self.if_f0 = last_rvc.if_f0
|
| 177 |
+
self.version = last_rvc.version
|
| 178 |
+
self.is_half = last_rvc.is_half
|
| 179 |
+
if last_rvc.use_jit != self.use_jit:
|
| 180 |
+
set_synthesizer()
|
| 181 |
+
else:
|
| 182 |
+
self.net_g = last_rvc.net_g
|
| 183 |
+
|
| 184 |
+
if last_rvc is not None and hasattr(last_rvc, "model_rmvpe"):
|
| 185 |
+
self.model_rmvpe = last_rvc.model_rmvpe
|
| 186 |
+
if last_rvc is not None and hasattr(last_rvc, "model_fcpe"):
|
| 187 |
+
self.device_fcpe = last_rvc.device_fcpe
|
| 188 |
+
self.model_fcpe = last_rvc.model_fcpe
|
| 189 |
+
except:
|
| 190 |
+
printt(traceback.format_exc())
|
| 191 |
+
|
| 192 |
+
def change_key(self, new_key):
|
| 193 |
+
self.f0_up_key = new_key
|
| 194 |
+
|
| 195 |
+
def change_formant(self, new_formant):
|
| 196 |
+
self.formant_shift = new_formant
|
| 197 |
+
|
| 198 |
+
def change_index_rate(self, new_index_rate):
|
| 199 |
+
if new_index_rate != 0 and self.index_rate == 0:
|
| 200 |
+
self.index = faiss.read_index(self.index_path)
|
| 201 |
+
self.big_npy = self.index.reconstruct_n(0, self.index.ntotal)
|
| 202 |
+
printt("Index search enabled")
|
| 203 |
+
self.index_rate = new_index_rate
|
| 204 |
+
|
| 205 |
+
def get_f0_post(self, f0):
|
| 206 |
+
if not torch.is_tensor(f0):
|
| 207 |
+
f0 = torch.from_numpy(f0)
|
| 208 |
+
f0 = f0.float().to(self.device).squeeze()
|
| 209 |
+
f0_mel = 1127 * torch.log(1 + f0 / 700)
|
| 210 |
+
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - self.f0_mel_min) * 254 / (
|
| 211 |
+
self.f0_mel_max - self.f0_mel_min
|
| 212 |
+
) + 1
|
| 213 |
+
f0_mel[f0_mel <= 1] = 1
|
| 214 |
+
f0_mel[f0_mel > 255] = 255
|
| 215 |
+
f0_coarse = torch.round(f0_mel).long()
|
| 216 |
+
return f0_coarse, f0
|
| 217 |
+
|
| 218 |
+
def get_f0(self, x, f0_up_key, n_cpu, method="harvest"):
|
| 219 |
+
n_cpu = int(n_cpu)
|
| 220 |
+
if method == "crepe":
|
| 221 |
+
return self.get_f0_crepe(x, f0_up_key)
|
| 222 |
+
if method == "rmvpe":
|
| 223 |
+
return self.get_f0_rmvpe(x, f0_up_key)
|
| 224 |
+
if method == "fcpe":
|
| 225 |
+
return self.get_f0_fcpe(x, f0_up_key)
|
| 226 |
+
x = x.cpu().numpy()
|
| 227 |
+
if method == "pm":
|
| 228 |
+
p_len = x.shape[0] // 160 + 1
|
| 229 |
+
f0_min = 65
|
| 230 |
+
l_pad = int(np.ceil(1.5 / f0_min * 16000))
|
| 231 |
+
r_pad = l_pad + 1
|
| 232 |
+
s = parselmouth.Sound(np.pad(x, (l_pad, r_pad)), 16000).to_pitch_ac(
|
| 233 |
+
time_step=0.01,
|
| 234 |
+
voicing_threshold=0.6,
|
| 235 |
+
pitch_floor=f0_min,
|
| 236 |
+
pitch_ceiling=1100,
|
| 237 |
+
)
|
| 238 |
+
assert np.abs(s.t1 - 1.5 / f0_min) < 0.001
|
| 239 |
+
f0 = s.selected_array["frequency"]
|
| 240 |
+
if len(f0) < p_len:
|
| 241 |
+
f0 = np.pad(f0, (0, p_len - len(f0)))
|
| 242 |
+
f0 = f0[:p_len]
|
| 243 |
+
f0 *= pow(2, f0_up_key / 12)
|
| 244 |
+
return self.get_f0_post(f0)
|
| 245 |
+
if n_cpu == 1:
|
| 246 |
+
f0, t = pyworld.harvest(
|
| 247 |
+
x.astype(np.double),
|
| 248 |
+
fs=16000,
|
| 249 |
+
f0_ceil=1100,
|
| 250 |
+
f0_floor=50,
|
| 251 |
+
frame_period=10,
|
| 252 |
+
)
|
| 253 |
+
f0 = signal.medfilt(f0, 3)
|
| 254 |
+
f0 *= pow(2, f0_up_key / 12)
|
| 255 |
+
return self.get_f0_post(f0)
|
| 256 |
+
f0bak = np.zeros(x.shape[0] // 160 + 1, dtype=np.float64)
|
| 257 |
+
length = len(x)
|
| 258 |
+
part_length = 160 * ((length // 160 - 1) // n_cpu + 1)
|
| 259 |
+
n_cpu = (length // 160 - 1) // (part_length // 160) + 1
|
| 260 |
+
ts = ttime()
|
| 261 |
+
res_f0 = mm.dict()
|
| 262 |
+
for idx in range(n_cpu):
|
| 263 |
+
tail = part_length * (idx + 1) + 320
|
| 264 |
+
if idx == 0:
|
| 265 |
+
self.inp_q.put((idx, x[:tail], res_f0, n_cpu, ts))
|
| 266 |
+
else:
|
| 267 |
+
self.inp_q.put(
|
| 268 |
+
(idx, x[part_length * idx - 320 : tail], res_f0, n_cpu, ts)
|
| 269 |
+
)
|
| 270 |
+
while 1:
|
| 271 |
+
res_ts = self.opt_q.get()
|
| 272 |
+
if res_ts == ts:
|
| 273 |
+
break
|
| 274 |
+
f0s = [i[1] for i in sorted(res_f0.items(), key=lambda x: x[0])]
|
| 275 |
+
for idx, f0 in enumerate(f0s):
|
| 276 |
+
if idx == 0:
|
| 277 |
+
f0 = f0[:-3]
|
| 278 |
+
elif idx != n_cpu - 1:
|
| 279 |
+
f0 = f0[2:-3]
|
| 280 |
+
else:
|
| 281 |
+
f0 = f0[2:]
|
| 282 |
+
f0bak[part_length * idx // 160 : part_length * idx // 160 + f0.shape[0]] = (
|
| 283 |
+
f0
|
| 284 |
+
)
|
| 285 |
+
f0bak = signal.medfilt(f0bak, 3)
|
| 286 |
+
f0bak *= pow(2, f0_up_key / 12)
|
| 287 |
+
return self.get_f0_post(f0bak)
|
| 288 |
+
|
| 289 |
+
def get_f0_crepe(self, x, f0_up_key):
|
| 290 |
+
if "privateuseone" in str(
|
| 291 |
+
self.device
|
| 292 |
+
): ###不支持dml,cpu又太慢用不成,拿fcpe顶替
|
| 293 |
+
return self.get_f0(x, f0_up_key, 1, "fcpe")
|
| 294 |
+
# printt("using crepe,device:%s"%self.device)
|
| 295 |
+
f0, pd = torchcrepe.predict(
|
| 296 |
+
x.unsqueeze(0).float(),
|
| 297 |
+
16000,
|
| 298 |
+
160,
|
| 299 |
+
self.f0_min,
|
| 300 |
+
self.f0_max,
|
| 301 |
+
"full",
|
| 302 |
+
batch_size=512,
|
| 303 |
+
# device=self.device if self.device.type!="privateuseone" else "cpu",###crepe不用半精度全部是全精度所以不愁###cpu延迟高到没法用
|
| 304 |
+
device=self.device,
|
| 305 |
+
return_periodicity=True,
|
| 306 |
+
)
|
| 307 |
+
pd = torchcrepe.filter.median(pd, 3)
|
| 308 |
+
f0 = torchcrepe.filter.mean(f0, 3)
|
| 309 |
+
f0[pd < 0.1] = 0
|
| 310 |
+
f0 *= pow(2, f0_up_key / 12)
|
| 311 |
+
return self.get_f0_post(f0)
|
| 312 |
+
|
| 313 |
+
def get_f0_rmvpe(self, x, f0_up_key):
|
| 314 |
+
if hasattr(self, "model_rmvpe") == False:
|
| 315 |
+
from infer.lib.rmvpe import RMVPE
|
| 316 |
+
|
| 317 |
+
printt("Loading rmvpe model")
|
| 318 |
+
self.model_rmvpe = RMVPE(
|
| 319 |
+
"assets/rvc/rmvpe.pt",
|
| 320 |
+
is_half=self.is_half,
|
| 321 |
+
device=self.device,
|
| 322 |
+
use_jit=self.config.use_jit,
|
| 323 |
+
)
|
| 324 |
+
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
|
| 325 |
+
f0 *= pow(2, f0_up_key / 12)
|
| 326 |
+
return self.get_f0_post(f0)
|
| 327 |
+
|
| 328 |
+
def get_f0_fcpe(self, x, f0_up_key):
|
| 329 |
+
if hasattr(self, "model_fcpe") == False:
|
| 330 |
+
from torchfcpe import spawn_bundled_infer_model
|
| 331 |
+
|
| 332 |
+
printt("Loading fcpe model")
|
| 333 |
+
if "privateuseone" in str(self.device):
|
| 334 |
+
self.device_fcpe = "cpu"
|
| 335 |
+
else:
|
| 336 |
+
self.device_fcpe = self.device
|
| 337 |
+
self.model_fcpe = spawn_bundled_infer_model(self.device_fcpe)
|
| 338 |
+
f0 = self.model_fcpe.infer(
|
| 339 |
+
x.to(self.device_fcpe).unsqueeze(0).float(),
|
| 340 |
+
sr=16000,
|
| 341 |
+
decoder_mode="local_argmax",
|
| 342 |
+
threshold=0.006,
|
| 343 |
+
)
|
| 344 |
+
f0 *= pow(2, f0_up_key / 12)
|
| 345 |
+
return self.get_f0_post(f0)
|
| 346 |
+
|
| 347 |
+
def infer(
|
| 348 |
+
self,
|
| 349 |
+
input_wav: torch.Tensor,
|
| 350 |
+
block_frame_16k,
|
| 351 |
+
skip_head,
|
| 352 |
+
return_length,
|
| 353 |
+
f0method,
|
| 354 |
+
) -> np.ndarray:
|
| 355 |
+
t1 = ttime()
|
| 356 |
+
with torch.no_grad():
|
| 357 |
+
if self.config.is_half:
|
| 358 |
+
feats = input_wav.half().view(1, -1)
|
| 359 |
+
else:
|
| 360 |
+
feats = input_wav.float().view(1, -1)
|
| 361 |
+
padding_mask = torch.BoolTensor(feats.shape).to(self.device).fill_(False)
|
| 362 |
+
inputs = {
|
| 363 |
+
"source": feats,
|
| 364 |
+
"padding_mask": padding_mask,
|
| 365 |
+
"output_layer": 9 if self.version == "v1" else 12,
|
| 366 |
+
}
|
| 367 |
+
logits = self.model.extract_features(**inputs)
|
| 368 |
+
feats = (
|
| 369 |
+
self.model.final_proj(logits[0]) if self.version == "v1" else logits[0]
|
| 370 |
+
)
|
| 371 |
+
feats = torch.cat((feats, feats[:, -1:, :]), 1)
|
| 372 |
+
t2 = ttime()
|
| 373 |
+
try:
|
| 374 |
+
if hasattr(self, "index") and self.index_rate != 0:
|
| 375 |
+
npy = feats[0][skip_head // 2 :].cpu().numpy().astype("float32")
|
| 376 |
+
score, ix = self.index.search(npy, k=8)
|
| 377 |
+
if (ix >= 0).all():
|
| 378 |
+
weight = np.square(1 / score)
|
| 379 |
+
weight /= weight.sum(axis=1, keepdims=True)
|
| 380 |
+
npy = np.sum(
|
| 381 |
+
self.big_npy[ix] * np.expand_dims(weight, axis=2), axis=1
|
| 382 |
+
)
|
| 383 |
+
if self.config.is_half:
|
| 384 |
+
npy = npy.astype("float16")
|
| 385 |
+
feats[0][skip_head // 2 :] = (
|
| 386 |
+
torch.from_numpy(npy).unsqueeze(0).to(self.device)
|
| 387 |
+
* self.index_rate
|
| 388 |
+
+ (1 - self.index_rate) * feats[0][skip_head // 2 :]
|
| 389 |
+
)
|
| 390 |
+
else:
|
| 391 |
+
printt(
|
| 392 |
+
"Invalid index. You MUST use added_xxxx.index but not trained_xxxx.index!"
|
| 393 |
+
)
|
| 394 |
+
else:
|
| 395 |
+
printt("Index search FAILED or disabled")
|
| 396 |
+
except:
|
| 397 |
+
traceback.print_exc()
|
| 398 |
+
printt("Index search FAILED")
|
| 399 |
+
t3 = ttime()
|
| 400 |
+
p_len = input_wav.shape[0] // 160
|
| 401 |
+
factor = pow(2, self.formant_shift / 12)
|
| 402 |
+
return_length2 = int(np.ceil(return_length * factor))
|
| 403 |
+
if self.if_f0 == 1:
|
| 404 |
+
f0_extractor_frame = block_frame_16k + 800
|
| 405 |
+
if f0method == "rmvpe":
|
| 406 |
+
f0_extractor_frame = 5120 * ((f0_extractor_frame - 1) // 5120 + 1) - 160
|
| 407 |
+
pitch, pitchf = self.get_f0(
|
| 408 |
+
input_wav[-f0_extractor_frame:], self.f0_up_key - self.formant_shift, self.n_cpu, f0method
|
| 409 |
+
)
|
| 410 |
+
shift = block_frame_16k // 160
|
| 411 |
+
self.cache_pitch[:-shift] = self.cache_pitch[shift:].clone()
|
| 412 |
+
self.cache_pitchf[:-shift] = self.cache_pitchf[shift:].clone()
|
| 413 |
+
self.cache_pitch[4 - pitch.shape[0] :] = pitch[3:-1]
|
| 414 |
+
self.cache_pitchf[4 - pitch.shape[0] :] = pitchf[3:-1]
|
| 415 |
+
cache_pitch = self.cache_pitch[None, -p_len:]
|
| 416 |
+
cache_pitchf = self.cache_pitchf[None, -p_len:] * return_length2 / return_length
|
| 417 |
+
t4 = ttime()
|
| 418 |
+
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
| 419 |
+
feats = feats[:, :p_len, :]
|
| 420 |
+
p_len = torch.LongTensor([p_len]).to(self.device)
|
| 421 |
+
sid = torch.LongTensor([0]).to(self.device)
|
| 422 |
+
skip_head = torch.LongTensor([skip_head])
|
| 423 |
+
return_length2 = torch.LongTensor([return_length2])
|
| 424 |
+
return_length = torch.LongTensor([return_length])
|
| 425 |
+
with torch.no_grad():
|
| 426 |
+
if self.if_f0 == 1:
|
| 427 |
+
infered_audio, _, _ = self.net_g.infer(
|
| 428 |
+
feats,
|
| 429 |
+
p_len,
|
| 430 |
+
cache_pitch,
|
| 431 |
+
cache_pitchf,
|
| 432 |
+
sid,
|
| 433 |
+
skip_head,
|
| 434 |
+
return_length,
|
| 435 |
+
return_length2,
|
| 436 |
+
)
|
| 437 |
+
else:
|
| 438 |
+
infered_audio, _, _ = self.net_g.infer(
|
| 439 |
+
feats, p_len, sid, skip_head, return_length, return_length2
|
| 440 |
+
)
|
| 441 |
+
infered_audio = infered_audio.squeeze(1).float()
|
| 442 |
+
upp_res = int(np.floor(factor * self.tgt_sr // 100))
|
| 443 |
+
if upp_res != self.tgt_sr // 100:
|
| 444 |
+
if upp_res not in self.resample_kernel:
|
| 445 |
+
self.resample_kernel[upp_res] = Resample(
|
| 446 |
+
orig_freq=upp_res,
|
| 447 |
+
new_freq=self.tgt_sr // 100,
|
| 448 |
+
dtype=torch.float32,
|
| 449 |
+
).to(self.device)
|
| 450 |
+
infered_audio = self.resample_kernel[upp_res](
|
| 451 |
+
infered_audio[:, : return_length * upp_res]
|
| 452 |
+
)
|
| 453 |
+
t5 = ttime()
|
| 454 |
+
printt(
|
| 455 |
+
"Spent time: fea = %.3fs, index = %.3fs, f0 = %.3fs, model = %.3fs",
|
| 456 |
+
t2 - t1,
|
| 457 |
+
t3 - t2,
|
| 458 |
+
t4 - t3,
|
| 459 |
+
t5 - t4,
|
| 460 |
+
)
|
| 461 |
+
return infered_audio.squeeze()
|