2023-10-06 11:14:33 +02:00
|
|
|
|
from io import BytesIO
|
2023-08-28 09:07:41 +02:00
|
|
|
|
import os
|
2023-10-06 11:14:33 +02:00
|
|
|
|
import pickle
|
2023-08-28 09:07:41 +02:00
|
|
|
|
import sys
|
|
|
|
|
import traceback
|
2023-10-06 11:14:33 +02:00
|
|
|
|
from infer.lib import jit
|
|
|
|
|
from infer.lib.jit.get_synthesizer import get_synthesizer
|
2023-08-28 09:07:41 +02:00
|
|
|
|
from time import time as ttime
|
2023-08-12 19:05:58 +02:00
|
|
|
|
import fairseq
|
2023-08-28 09:07:41 +02:00
|
|
|
|
import faiss
|
|
|
|
|
import numpy as np
|
|
|
|
|
import parselmouth
|
|
|
|
|
import pyworld
|
|
|
|
|
import scipy.signal as signal
|
|
|
|
|
import torch
|
|
|
|
|
import torch.nn as nn
|
|
|
|
|
import torch.nn.functional as F
|
|
|
|
|
import torchcrepe
|
|
|
|
|
|
|
|
|
|
from infer.lib.infer_pack.models import (
|
2023-07-11 16:09:41 +02:00
|
|
|
|
SynthesizerTrnMs256NSFsid,
|
|
|
|
|
SynthesizerTrnMs256NSFsid_nono,
|
|
|
|
|
SynthesizerTrnMs768NSFsid,
|
|
|
|
|
SynthesizerTrnMs768NSFsid_nono,
|
|
|
|
|
)
|
2023-07-13 08:35:24 +02:00
|
|
|
|
|
2023-07-11 16:09:41 +02:00
|
|
|
|
now_dir = os.getcwd()
|
|
|
|
|
sys.path.append(now_dir)
|
|
|
|
|
from multiprocessing import Manager as M
|
2023-07-13 08:35:24 +02:00
|
|
|
|
|
2023-08-28 09:07:41 +02:00
|
|
|
|
from configs.config import Config
|
|
|
|
|
|
2023-10-06 11:14:33 +02:00
|
|
|
|
# config = Config()
|
2023-08-28 09:07:41 +02:00
|
|
|
|
|
2023-07-11 16:09:41 +02:00
|
|
|
|
mm = M()
|
2023-08-13 05:52:51 +02:00
|
|
|
|
|
|
|
|
|
|
2023-10-06 11:14:33 +02:00
|
|
|
|
def printt(strr, *args):
|
|
|
|
|
if len(args) == 0:
|
|
|
|
|
print(strr)
|
|
|
|
|
else:
|
|
|
|
|
print(strr % args)
|
2023-08-13 05:52:51 +02:00
|
|
|
|
|
|
|
|
|
|
2023-08-12 19:05:58 +02:00
|
|
|
|
# config.device=torch.device("cpu")########强制cpu测试
|
|
|
|
|
# config.is_half=False########强制cpu测试
|
2023-07-11 16:09:41 +02:00
|
|
|
|
class RVC:
|
|
|
|
|
def __init__(
|
2023-09-02 10:14:32 +02:00
|
|
|
|
self,
|
|
|
|
|
key,
|
|
|
|
|
pth_path,
|
|
|
|
|
index_path,
|
|
|
|
|
index_rate,
|
|
|
|
|
n_cpu,
|
|
|
|
|
inp_q,
|
|
|
|
|
opt_q,
|
2023-10-06 11:14:33 +02:00
|
|
|
|
config: Config,
|
2023-09-02 10:14:32 +02:00
|
|
|
|
last_rvc=None,
|
2023-07-11 16:09:41 +02:00
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
初始化
|
|
|
|
|
"""
|
|
|
|
|
try:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
if config.dml == True:
|
|
|
|
|
|
|
|
|
|
def forward_dml(ctx, x, scale):
|
|
|
|
|
ctx.scale = scale
|
|
|
|
|
res = x.clone().detach()
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
fairseq.modules.grad_multiply.GradMultiply.forward = forward_dml
|
|
|
|
|
# global config
|
|
|
|
|
self.config = config
|
2023-07-13 08:35:24 +02:00
|
|
|
|
self.inp_q = inp_q
|
|
|
|
|
self.opt_q = opt_q
|
2023-08-12 19:05:58 +02:00
|
|
|
|
# device="cpu"########强制cpu测试
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.device = config.device
|
2023-07-11 16:09:41 +02:00
|
|
|
|
self.f0_up_key = key
|
|
|
|
|
self.time_step = 160 / 16000 * 1000
|
|
|
|
|
self.f0_min = 50
|
|
|
|
|
self.f0_max = 1100
|
|
|
|
|
self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700)
|
|
|
|
|
self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700)
|
|
|
|
|
self.sr = 16000
|
|
|
|
|
self.window = 160
|
|
|
|
|
self.n_cpu = n_cpu
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.use_jit = self.config.use_jit
|
|
|
|
|
self.is_half = config.is_half
|
|
|
|
|
|
2023-07-11 16:09:41 +02:00
|
|
|
|
if index_rate != 0:
|
|
|
|
|
self.index = faiss.read_index(index_path)
|
|
|
|
|
self.big_npy = self.index.reconstruct_n(0, self.index.ntotal)
|
2023-10-06 11:14:33 +02:00
|
|
|
|
printt("Index search enabled")
|
|
|
|
|
self.pth_path: str = pth_path
|
2023-09-01 04:16:49 +02:00
|
|
|
|
self.index_path = index_path
|
2023-07-11 16:09:41 +02:00
|
|
|
|
self.index_rate = index_rate
|
2023-09-02 10:14:32 +02:00
|
|
|
|
|
2023-09-02 09:45:50 +02:00
|
|
|
|
if last_rvc is None:
|
|
|
|
|
models, _, _ = fairseq.checkpoint_utils.load_model_ensemble_and_task(
|
|
|
|
|
["assets/hubert/hubert_base.pt"],
|
|
|
|
|
suffix="",
|
|
|
|
|
)
|
|
|
|
|
hubert_model = models[0]
|
2023-10-06 11:14:33 +02:00
|
|
|
|
hubert_model = hubert_model.to(self.device)
|
|
|
|
|
if self.is_half:
|
2023-09-02 09:45:50 +02:00
|
|
|
|
hubert_model = hubert_model.half()
|
2023-07-11 16:09:41 +02:00
|
|
|
|
else:
|
2023-09-02 09:45:50 +02:00
|
|
|
|
hubert_model = hubert_model.float()
|
|
|
|
|
hubert_model.eval()
|
|
|
|
|
self.model = hubert_model
|
|
|
|
|
else:
|
|
|
|
|
self.model = last_rvc.model
|
2023-09-02 10:14:32 +02:00
|
|
|
|
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.net_g: nn.Module = None
|
|
|
|
|
|
|
|
|
|
def set_default_model():
|
|
|
|
|
self.net_g, cpt = get_synthesizer(self.pth_path, self.device)
|
2023-09-02 09:45:50 +02:00
|
|
|
|
self.tgt_sr = cpt["config"][-1]
|
|
|
|
|
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0]
|
|
|
|
|
self.if_f0 = cpt.get("f0", 1)
|
|
|
|
|
self.version = cpt.get("version", "v1")
|
2023-10-06 11:14:33 +02:00
|
|
|
|
if self.is_half:
|
2023-09-02 09:45:50 +02:00
|
|
|
|
self.net_g = self.net_g.half()
|
2023-07-11 16:09:41 +02:00
|
|
|
|
else:
|
2023-09-02 09:45:50 +02:00
|
|
|
|
self.net_g = self.net_g.float()
|
2023-10-06 11:14:33 +02:00
|
|
|
|
|
|
|
|
|
def set_jit_model():
|
|
|
|
|
jit_pth_path = self.pth_path.rstrip(".pth")
|
|
|
|
|
jit_pth_path += ".half.jit" if self.is_half else ".jit"
|
|
|
|
|
reload = False
|
|
|
|
|
if str(self.device) == "cuda":
|
|
|
|
|
self.device = torch.device("cuda:0")
|
|
|
|
|
if os.path.exists(jit_pth_path):
|
|
|
|
|
cpt = jit.load(jit_pth_path)
|
|
|
|
|
model_device = cpt["device"]
|
|
|
|
|
if model_device != str(self.device):
|
|
|
|
|
reload = True
|
|
|
|
|
else:
|
|
|
|
|
reload = True
|
|
|
|
|
|
|
|
|
|
if reload:
|
|
|
|
|
cpt = jit.synthesizer_jit_export(
|
|
|
|
|
self.pth_path,
|
|
|
|
|
"script",
|
|
|
|
|
None,
|
|
|
|
|
device=self.device,
|
|
|
|
|
is_half=self.is_half,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.tgt_sr = cpt["config"][-1]
|
|
|
|
|
self.if_f0 = cpt.get("f0", 1)
|
|
|
|
|
self.version = cpt.get("version", "v1")
|
|
|
|
|
self.net_g = torch.jit.load(
|
|
|
|
|
BytesIO(cpt["model"]), map_location=self.device
|
|
|
|
|
)
|
|
|
|
|
self.net_g.infer = self.net_g.forward
|
|
|
|
|
self.net_g.eval().to(self.device)
|
|
|
|
|
|
|
|
|
|
def set_synthesizer():
|
|
|
|
|
if self.use_jit and not config.dml:
|
|
|
|
|
if self.is_half and "cpu" in str(self.device):
|
|
|
|
|
printt(
|
|
|
|
|
"Use default Synthesizer model. \
|
|
|
|
|
Jit is not supported on the CPU for half floating point"
|
|
|
|
|
)
|
|
|
|
|
set_default_model()
|
|
|
|
|
else:
|
|
|
|
|
set_jit_model()
|
|
|
|
|
else:
|
|
|
|
|
set_default_model()
|
|
|
|
|
|
|
|
|
|
if last_rvc is None or last_rvc.pth_path != self.pth_path:
|
|
|
|
|
set_synthesizer()
|
2023-07-11 16:09:41 +02:00
|
|
|
|
else:
|
2023-09-02 09:45:50 +02:00
|
|
|
|
self.tgt_sr = last_rvc.tgt_sr
|
|
|
|
|
self.if_f0 = last_rvc.if_f0
|
|
|
|
|
self.version = last_rvc.version
|
|
|
|
|
self.is_half = last_rvc.is_half
|
2023-10-06 11:14:33 +02:00
|
|
|
|
if last_rvc.use_jit != self.use_jit:
|
|
|
|
|
set_synthesizer()
|
|
|
|
|
else:
|
|
|
|
|
self.net_g = last_rvc.net_g
|
2023-09-02 10:14:32 +02:00
|
|
|
|
|
2023-09-02 09:45:50 +02:00
|
|
|
|
if last_rvc is not None and hasattr(last_rvc, "model_rmvpe"):
|
|
|
|
|
self.model_rmvpe = last_rvc.model_rmvpe
|
2023-07-11 16:09:41 +02:00
|
|
|
|
except:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
printt(traceback.format_exc())
|
2023-09-01 06:03:35 +02:00
|
|
|
|
|
2023-09-01 04:16:49 +02:00
|
|
|
|
def change_key(self, new_key):
|
|
|
|
|
self.f0_up_key = new_key
|
2023-09-01 06:03:35 +02:00
|
|
|
|
|
2023-09-01 04:16:49 +02:00
|
|
|
|
def change_index_rate(self, new_index_rate):
|
|
|
|
|
if new_index_rate != 0 and self.index_rate == 0:
|
2023-09-01 06:03:35 +02:00
|
|
|
|
self.index = faiss.read_index(self.index_path)
|
|
|
|
|
self.big_npy = self.index.reconstruct_n(0, self.index.ntotal)
|
2023-10-06 11:14:33 +02:00
|
|
|
|
printt("Index search enabled")
|
2023-09-01 04:16:49 +02:00
|
|
|
|
self.index_rate = new_index_rate
|
2023-09-01 06:03:35 +02:00
|
|
|
|
|
2023-07-11 16:09:41 +02:00
|
|
|
|
def get_f0_post(self, f0):
|
|
|
|
|
f0_min = self.f0_min
|
|
|
|
|
f0_max = self.f0_max
|
|
|
|
|
f0_mel_min = 1127 * np.log(1 + f0_min / 700)
|
|
|
|
|
f0_mel_max = 1127 * np.log(1 + f0_max / 700)
|
|
|
|
|
f0bak = f0.copy()
|
|
|
|
|
f0_mel = 1127 * np.log(1 + f0 / 700)
|
|
|
|
|
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * 254 / (
|
|
|
|
|
f0_mel_max - f0_mel_min
|
|
|
|
|
) + 1
|
|
|
|
|
f0_mel[f0_mel <= 1] = 1
|
|
|
|
|
f0_mel[f0_mel > 255] = 255
|
2023-08-10 04:28:30 +02:00
|
|
|
|
f0_coarse = np.rint(f0_mel).astype(np.int32)
|
2023-07-11 16:09:41 +02:00
|
|
|
|
return f0_coarse, f0bak
|
|
|
|
|
|
|
|
|
|
def get_f0(self, x, f0_up_key, n_cpu, method="harvest"):
|
|
|
|
|
n_cpu = int(n_cpu)
|
2023-07-13 08:35:24 +02:00
|
|
|
|
if method == "crepe":
|
|
|
|
|
return self.get_f0_crepe(x, f0_up_key)
|
|
|
|
|
if method == "rmvpe":
|
|
|
|
|
return self.get_f0_rmvpe(x, f0_up_key)
|
|
|
|
|
if method == "pm":
|
2023-09-02 09:45:50 +02:00
|
|
|
|
p_len = x.shape[0] // 160 + 1
|
2023-11-12 10:21:25 +01:00
|
|
|
|
f0_min = 65
|
|
|
|
|
l_pad = int(np.ceil(1.5 / f0_min * 16000))
|
|
|
|
|
r_pad = l_pad + 1
|
|
|
|
|
s = parselmouth.Sound(np.pad(x, (l_pad, r_pad)), 16000).to_pitch_ac(
|
2023-11-12 10:40:59 +01:00
|
|
|
|
time_step=0.01,
|
|
|
|
|
voicing_threshold=0.6,
|
|
|
|
|
pitch_floor=f0_min,
|
|
|
|
|
pitch_ceiling=1100,
|
|
|
|
|
)
|
2023-11-12 10:21:25 +01:00
|
|
|
|
assert np.abs(s.t1 - 1.5 / f0_min) < 0.001
|
2023-11-12 10:40:59 +01:00
|
|
|
|
f0 = s.selected_array["frequency"]
|
2023-11-12 10:21:25 +01:00
|
|
|
|
if len(f0) < p_len:
|
|
|
|
|
f0 = np.pad(f0, (0, p_len - len(f0)))
|
2023-11-12 10:40:59 +01:00
|
|
|
|
f0 = f0[:p_len]
|
2023-07-11 16:09:41 +02:00
|
|
|
|
f0 *= pow(2, f0_up_key / 12)
|
|
|
|
|
return self.get_f0_post(f0)
|
2023-07-13 08:35:24 +02:00
|
|
|
|
if n_cpu == 1:
|
2023-07-11 16:09:41 +02:00
|
|
|
|
f0, t = pyworld.harvest(
|
|
|
|
|
x.astype(np.double),
|
|
|
|
|
fs=16000,
|
|
|
|
|
f0_ceil=1100,
|
|
|
|
|
f0_floor=50,
|
|
|
|
|
frame_period=10,
|
|
|
|
|
)
|
|
|
|
|
f0 = signal.medfilt(f0, 3)
|
|
|
|
|
f0 *= pow(2, f0_up_key / 12)
|
|
|
|
|
return self.get_f0_post(f0)
|
2023-09-02 09:45:50 +02:00
|
|
|
|
f0bak = np.zeros(x.shape[0] // 160 + 1, dtype=np.float64)
|
2023-07-11 16:09:41 +02:00
|
|
|
|
length = len(x)
|
2023-09-02 09:45:50 +02:00
|
|
|
|
part_length = 160 * ((length // 160 - 1) // n_cpu + 1)
|
|
|
|
|
n_cpu = (length // 160 - 1) // (part_length // 160) + 1
|
2023-07-11 16:09:41 +02:00
|
|
|
|
ts = ttime()
|
|
|
|
|
res_f0 = mm.dict()
|
|
|
|
|
for idx in range(n_cpu):
|
|
|
|
|
tail = part_length * (idx + 1) + 320
|
2023-07-13 08:35:24 +02:00
|
|
|
|
if idx == 0:
|
2023-07-11 16:09:41 +02:00
|
|
|
|
self.inp_q.put((idx, x[:tail], res_f0, n_cpu, ts))
|
|
|
|
|
else:
|
2023-07-13 08:35:24 +02:00
|
|
|
|
self.inp_q.put(
|
|
|
|
|
(idx, x[part_length * idx - 320 : tail], res_f0, n_cpu, ts)
|
|
|
|
|
)
|
|
|
|
|
while 1:
|
2023-07-11 16:09:41 +02:00
|
|
|
|
res_ts = self.opt_q.get()
|
2023-07-13 08:35:24 +02:00
|
|
|
|
if res_ts == ts:
|
2023-07-11 16:09:41 +02:00
|
|
|
|
break
|
|
|
|
|
f0s = [i[1] for i in sorted(res_f0.items(), key=lambda x: x[0])]
|
|
|
|
|
for idx, f0 in enumerate(f0s):
|
2023-07-13 08:35:24 +02:00
|
|
|
|
if idx == 0:
|
2023-07-11 16:09:41 +02:00
|
|
|
|
f0 = f0[:-3]
|
2023-07-13 08:35:24 +02:00
|
|
|
|
elif idx != n_cpu - 1:
|
2023-07-11 16:09:41 +02:00
|
|
|
|
f0 = f0[2:-3]
|
|
|
|
|
else:
|
2023-09-02 09:45:50 +02:00
|
|
|
|
f0 = f0[2:]
|
2023-07-13 08:35:24 +02:00
|
|
|
|
f0bak[
|
|
|
|
|
part_length * idx // 160 : part_length * idx // 160 + f0.shape[0]
|
|
|
|
|
] = f0
|
2023-07-11 16:09:41 +02:00
|
|
|
|
f0bak = signal.medfilt(f0bak, 3)
|
|
|
|
|
f0bak *= pow(2, f0_up_key / 12)
|
|
|
|
|
return self.get_f0_post(f0bak)
|
|
|
|
|
|
|
|
|
|
def get_f0_crepe(self, x, f0_up_key):
|
2023-08-29 12:53:33 +02:00
|
|
|
|
if "privateuseone" in str(self.device): ###不支持dml,cpu又太慢用不成,拿pm顶替
|
2023-08-13 05:52:51 +02:00
|
|
|
|
return self.get_f0(x, f0_up_key, 1, "pm")
|
2023-07-11 16:09:41 +02:00
|
|
|
|
audio = torch.tensor(np.copy(x))[None].float()
|
2023-10-06 11:14:33 +02:00
|
|
|
|
# printt("using crepe,device:%s"%self.device)
|
2023-07-11 16:09:41 +02:00
|
|
|
|
f0, pd = torchcrepe.predict(
|
|
|
|
|
audio,
|
|
|
|
|
self.sr,
|
|
|
|
|
160,
|
|
|
|
|
self.f0_min,
|
|
|
|
|
self.f0_max,
|
|
|
|
|
"full",
|
|
|
|
|
batch_size=512,
|
2023-08-12 19:05:58 +02:00
|
|
|
|
# device=self.device if self.device.type!="privateuseone" else "cpu",###crepe不用半精度全部是全精度所以不愁###cpu延迟高到没法用
|
2023-07-11 16:09:41 +02:00
|
|
|
|
device=self.device,
|
|
|
|
|
return_periodicity=True,
|
|
|
|
|
)
|
|
|
|
|
pd = torchcrepe.filter.median(pd, 3)
|
|
|
|
|
f0 = torchcrepe.filter.mean(f0, 3)
|
|
|
|
|
f0[pd < 0.1] = 0
|
|
|
|
|
f0 = f0[0].cpu().numpy()
|
|
|
|
|
f0 *= pow(2, f0_up_key / 12)
|
|
|
|
|
return self.get_f0_post(f0)
|
|
|
|
|
|
|
|
|
|
def get_f0_rmvpe(self, x, f0_up_key):
|
2023-07-13 08:35:24 +02:00
|
|
|
|
if hasattr(self, "model_rmvpe") == False:
|
2023-08-29 09:00:24 +02:00
|
|
|
|
from infer.lib.rmvpe import RMVPE
|
2023-08-13 05:52:51 +02:00
|
|
|
|
|
2023-10-06 11:14:33 +02:00
|
|
|
|
printt("Loading rmvpe model")
|
2023-07-13 08:35:24 +02:00
|
|
|
|
self.model_rmvpe = RMVPE(
|
2023-08-12 19:05:58 +02:00
|
|
|
|
# "rmvpe.pt", is_half=self.is_half if self.device.type!="privateuseone" else False, device=self.device if self.device.type!="privateuseone"else "cpu"####dml时强制对rmvpe用cpu跑
|
|
|
|
|
# "rmvpe.pt", is_half=False, device=self.device####dml配置
|
2023-08-13 05:52:51 +02:00
|
|
|
|
# "rmvpe.pt", is_half=False, device="cpu"####锁定cpu配置
|
2023-08-29 09:00:24 +02:00
|
|
|
|
"assets/rmvpe/rmvpe.pt",
|
2023-08-13 05:52:51 +02:00
|
|
|
|
is_half=self.is_half,
|
|
|
|
|
device=self.device, ####正常逻辑
|
2023-10-06 11:14:33 +02:00
|
|
|
|
use_jit=self.config.use_jit,
|
2023-07-13 08:35:24 +02:00
|
|
|
|
)
|
2023-07-11 16:09:41 +02:00
|
|
|
|
# self.model_rmvpe = RMVPE("aug2_58000_half.pt", is_half=self.is_half, device=self.device)
|
|
|
|
|
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
|
|
|
|
|
f0 *= pow(2, f0_up_key / 12)
|
|
|
|
|
return self.get_f0_post(f0)
|
|
|
|
|
|
2023-07-13 08:35:24 +02:00
|
|
|
|
def infer(
|
|
|
|
|
self,
|
|
|
|
|
feats: torch.Tensor,
|
|
|
|
|
indata: np.ndarray,
|
2023-09-02 09:45:50 +02:00
|
|
|
|
block_frame_16k,
|
|
|
|
|
rate,
|
2023-07-13 08:35:24 +02:00
|
|
|
|
cache_pitch,
|
|
|
|
|
cache_pitchf,
|
|
|
|
|
f0method,
|
|
|
|
|
) -> np.ndarray:
|
2023-07-11 16:09:41 +02:00
|
|
|
|
feats = feats.view(1, -1)
|
2023-10-06 11:14:33 +02:00
|
|
|
|
if self.config.is_half:
|
2023-07-11 16:09:41 +02:00
|
|
|
|
feats = feats.half()
|
|
|
|
|
else:
|
|
|
|
|
feats = feats.float()
|
|
|
|
|
feats = feats.to(self.device)
|
|
|
|
|
t1 = ttime()
|
|
|
|
|
with torch.no_grad():
|
|
|
|
|
padding_mask = torch.BoolTensor(feats.shape).to(self.device).fill_(False)
|
|
|
|
|
inputs = {
|
|
|
|
|
"source": feats,
|
|
|
|
|
"padding_mask": padding_mask,
|
|
|
|
|
"output_layer": 9 if self.version == "v1" else 12,
|
|
|
|
|
}
|
|
|
|
|
logits = self.model.extract_features(**inputs)
|
2023-07-13 08:35:24 +02:00
|
|
|
|
feats = (
|
|
|
|
|
self.model.final_proj(logits[0]) if self.version == "v1" else logits[0]
|
|
|
|
|
)
|
2023-11-12 10:40:59 +01:00
|
|
|
|
feats = torch.cat((feats, feats[:, -1:, :]), 1)
|
2023-07-11 16:09:41 +02:00
|
|
|
|
t2 = ttime()
|
|
|
|
|
try:
|
2023-07-13 08:35:24 +02:00
|
|
|
|
if hasattr(self, "index") and self.index_rate != 0:
|
2023-09-02 09:45:50 +02:00
|
|
|
|
leng_replace_head = int(rate * feats[0].shape[0])
|
2023-07-11 16:09:41 +02:00
|
|
|
|
npy = feats[0][-leng_replace_head:].cpu().numpy().astype("float32")
|
|
|
|
|
score, ix = self.index.search(npy, k=8)
|
|
|
|
|
weight = np.square(1 / score)
|
|
|
|
|
weight /= weight.sum(axis=1, keepdims=True)
|
|
|
|
|
npy = np.sum(self.big_npy[ix] * np.expand_dims(weight, axis=2), axis=1)
|
2023-10-06 11:14:33 +02:00
|
|
|
|
if self.config.is_half:
|
2023-07-11 16:09:41 +02:00
|
|
|
|
npy = npy.astype("float16")
|
|
|
|
|
feats[0][-leng_replace_head:] = (
|
|
|
|
|
torch.from_numpy(npy).unsqueeze(0).to(self.device) * self.index_rate
|
|
|
|
|
+ (1 - self.index_rate) * feats[0][-leng_replace_head:]
|
|
|
|
|
)
|
|
|
|
|
else:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
printt("Index search FAILED or disabled")
|
2023-07-11 16:09:41 +02:00
|
|
|
|
except:
|
2023-10-09 15:21:35 +02:00
|
|
|
|
traceback.print_exc()
|
2023-10-06 11:14:33 +02:00
|
|
|
|
printt("Index search FAILED")
|
2023-07-11 16:09:41 +02:00
|
|
|
|
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
|
|
|
|
t3 = ttime()
|
|
|
|
|
if self.if_f0 == 1:
|
|
|
|
|
pitch, pitchf = self.get_f0(indata, self.f0_up_key, self.n_cpu, f0method)
|
2023-09-02 09:45:50 +02:00
|
|
|
|
start_frame = block_frame_16k // 160
|
2023-09-02 10:14:32 +02:00
|
|
|
|
end_frame = len(cache_pitch) - (pitch.shape[0] - 4) + start_frame
|
|
|
|
|
cache_pitch[:] = np.append(cache_pitch[start_frame:end_frame], pitch[3:-1])
|
2023-07-13 08:35:24 +02:00
|
|
|
|
cache_pitchf[:] = np.append(
|
2023-09-02 10:14:32 +02:00
|
|
|
|
cache_pitchf[start_frame:end_frame], pitchf[3:-1]
|
2023-07-13 08:35:24 +02:00
|
|
|
|
)
|
2023-07-11 16:09:41 +02:00
|
|
|
|
p_len = min(feats.shape[1], 13000, cache_pitch.shape[0])
|
|
|
|
|
else:
|
|
|
|
|
cache_pitch, cache_pitchf = None, None
|
|
|
|
|
p_len = min(feats.shape[1], 13000)
|
|
|
|
|
t4 = ttime()
|
|
|
|
|
feats = feats[:, :p_len, :]
|
|
|
|
|
if self.if_f0 == 1:
|
|
|
|
|
cache_pitch = cache_pitch[:p_len]
|
|
|
|
|
cache_pitchf = cache_pitchf[:p_len]
|
|
|
|
|
cache_pitch = torch.LongTensor(cache_pitch).unsqueeze(0).to(self.device)
|
|
|
|
|
cache_pitchf = torch.FloatTensor(cache_pitchf).unsqueeze(0).to(self.device)
|
|
|
|
|
p_len = torch.LongTensor([p_len]).to(self.device)
|
|
|
|
|
ii = 0 # sid
|
|
|
|
|
sid = torch.LongTensor([ii]).to(self.device)
|
|
|
|
|
with torch.no_grad():
|
|
|
|
|
if self.if_f0 == 1:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
# printt(12222222222,feats.device,p_len.device,cache_pitch.device,cache_pitchf.device,sid.device,rate2)
|
2023-09-14 02:34:30 +02:00
|
|
|
|
infered_audio = self.net_g.infer(
|
2023-10-06 11:14:33 +02:00
|
|
|
|
feats,
|
|
|
|
|
p_len,
|
|
|
|
|
cache_pitch,
|
|
|
|
|
cache_pitchf,
|
|
|
|
|
sid,
|
|
|
|
|
torch.FloatTensor([rate]),
|
2023-09-14 02:34:30 +02:00
|
|
|
|
)[0][0, 0].data.float()
|
2023-07-11 16:09:41 +02:00
|
|
|
|
else:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
infered_audio = self.net_g.infer(
|
|
|
|
|
feats, p_len, sid, torch.FloatTensor([rate])
|
|
|
|
|
)[0][0, 0].data.float()
|
2023-07-11 16:09:41 +02:00
|
|
|
|
t5 = ttime()
|
2023-10-06 11:14:33 +02:00
|
|
|
|
printt(
|
2023-09-02 06:18:56 +02:00
|
|
|
|
"Spent time: fea = %.2fs, index = %.2fs, f0 = %.2fs, model = %.2fs",
|
2023-09-02 05:50:52 +02:00
|
|
|
|
t2 - t1,
|
|
|
|
|
t3 - t2,
|
|
|
|
|
t4 - t3,
|
|
|
|
|
t5 - t4,
|
|
|
|
|
)
|
2023-07-11 16:09:41 +02:00
|
|
|
|
return infered_audio
|