2023-04-15 19:44:24 +08:00
|
|
|
import numpy as np, parselmouth, torch, pdb
|
2023-03-31 17:49:09 +08:00
|
|
|
from time import time as ttime
|
|
|
|
import torch.nn.functional as F
|
2023-04-15 19:44:24 +08:00
|
|
|
from config import x_pad, x_query, x_center, x_max
|
2023-03-31 17:49:09 +08:00
|
|
|
import scipy.signal as signal
|
2023-04-15 19:44:24 +08:00
|
|
|
import pyworld, os, traceback, faiss
|
2023-04-16 18:56:20 +08:00
|
|
|
from scipy import signal
|
2023-04-17 20:49:29 +08:00
|
|
|
|
|
|
|
bh, ah = signal.butter(N=5, Wn=48, btype="high", fs=16000)
|
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
|
2023-03-31 17:49:09 +08:00
|
|
|
class VC(object):
|
2023-04-15 19:44:24 +08:00
|
|
|
def __init__(self, tgt_sr, device, is_half):
|
|
|
|
self.sr = 16000 # hubert输入采样率
|
|
|
|
self.window = 160 # 每帧点数
|
|
|
|
self.t_pad = self.sr * x_pad # 每条前后pad时间
|
|
|
|
self.t_pad_tgt = tgt_sr * x_pad
|
|
|
|
self.t_pad2 = self.t_pad * 2
|
|
|
|
self.t_query = self.sr * x_query # 查询切点前后查询时间
|
|
|
|
self.t_center = self.sr * x_center # 查询切点位置
|
|
|
|
self.t_max = self.sr * x_max # 免查询时长阈值
|
|
|
|
self.device = device
|
|
|
|
self.is_half = is_half
|
2023-03-31 17:49:09 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
def get_f0(self, x, p_len, f0_up_key, f0_method, inp_f0=None):
|
2023-03-31 17:49:09 +08:00
|
|
|
time_step = self.window / self.sr * 1000
|
|
|
|
f0_min = 50
|
|
|
|
f0_max = 1100
|
|
|
|
f0_mel_min = 1127 * np.log(1 + f0_min / 700)
|
|
|
|
f0_mel_max = 1127 * np.log(1 + f0_max / 700)
|
2023-04-15 19:44:24 +08:00
|
|
|
if f0_method == "pm":
|
|
|
|
f0 = (
|
|
|
|
parselmouth.Sound(x, self.sr)
|
|
|
|
.to_pitch_ac(
|
|
|
|
time_step=time_step / 1000,
|
|
|
|
voicing_threshold=0.6,
|
|
|
|
pitch_floor=f0_min,
|
|
|
|
pitch_ceiling=f0_max,
|
|
|
|
)
|
|
|
|
.selected_array["frequency"]
|
|
|
|
)
|
|
|
|
pad_size = (p_len - len(f0) + 1) // 2
|
|
|
|
if 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":
|
2023-03-31 17:49:09 +08:00
|
|
|
f0, t = pyworld.harvest(
|
|
|
|
x.astype(np.double),
|
|
|
|
fs=self.sr,
|
|
|
|
f0_ceil=f0_max,
|
2023-04-13 23:57:27 +08:00
|
|
|
f0_floor=f0_min,
|
2023-03-31 17:49:09 +08:00
|
|
|
frame_period=10,
|
|
|
|
)
|
|
|
|
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.sr)
|
|
|
|
f0 = signal.medfilt(f0, 3)
|
|
|
|
f0 *= pow(2, f0_up_key / 12)
|
|
|
|
# with open("test.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()]))
|
2023-04-15 19:44:24 +08:00
|
|
|
tf0 = self.sr // self.window # 每秒f0点数
|
|
|
|
if inp_f0 is not None:
|
|
|
|
delta_t = np.round(
|
|
|
|
(inp_f0[:, 0].max() - inp_f0[:, 0].min()) * tf0 + 1
|
|
|
|
).astype("int16")
|
|
|
|
replace_f0 = np.interp(
|
|
|
|
list(range(delta_t)), inp_f0[:, 0] * 100, inp_f0[:, 1]
|
|
|
|
)
|
|
|
|
shape = f0[x_pad * tf0 : x_pad * tf0 + len(replace_f0)].shape[0]
|
|
|
|
f0[x_pad * tf0 : x_pad * tf0 + len(replace_f0)] = replace_f0[:shape]
|
2023-03-31 17:49:09 +08:00
|
|
|
# with open("test_opt.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()]))
|
|
|
|
f0bak = f0.copy()
|
|
|
|
f0_mel = 1127 * np.log(1 + f0 / 700)
|
2023-04-15 19:44:24 +08:00
|
|
|
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * 254 / (
|
|
|
|
f0_mel_max - f0_mel_min
|
|
|
|
) + 1
|
2023-03-31 17:49:09 +08:00
|
|
|
f0_mel[f0_mel <= 1] = 1
|
|
|
|
f0_mel[f0_mel > 255] = 255
|
|
|
|
f0_coarse = np.rint(f0_mel).astype(np.int)
|
2023-04-15 19:44:24 +08:00
|
|
|
return f0_coarse, f0bak # 1-0
|
2023-03-31 17:49:09 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
def vc(
|
|
|
|
self,
|
|
|
|
model,
|
|
|
|
net_g,
|
|
|
|
sid,
|
|
|
|
audio0,
|
|
|
|
pitch,
|
|
|
|
pitchf,
|
|
|
|
times,
|
|
|
|
index,
|
|
|
|
big_npy,
|
|
|
|
index_rate,
|
|
|
|
): # ,file_index,file_big_npy
|
2023-03-31 17:49:09 +08:00
|
|
|
feats = torch.from_numpy(audio0)
|
2023-04-15 19:44:24 +08:00
|
|
|
if self.is_half:
|
|
|
|
feats = feats.half()
|
|
|
|
else:
|
|
|
|
feats = feats.float()
|
2023-03-31 17:49:09 +08:00
|
|
|
if feats.dim() == 2: # double channels
|
|
|
|
feats = feats.mean(-1)
|
|
|
|
assert feats.dim() == 1, feats.dim()
|
|
|
|
feats = feats.view(1, -1)
|
|
|
|
padding_mask = torch.BoolTensor(feats.shape).to(self.device).fill_(False)
|
|
|
|
|
|
|
|
inputs = {
|
|
|
|
"source": feats.to(self.device),
|
|
|
|
"padding_mask": padding_mask,
|
|
|
|
"output_layer": 9, # layer 9
|
|
|
|
}
|
|
|
|
t0 = ttime()
|
|
|
|
with torch.no_grad():
|
|
|
|
logits = model.extract_features(**inputs)
|
2023-04-15 19:44:24 +08:00
|
|
|
feats = model.final_proj(logits[0])
|
2023-03-31 17:49:09 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
if (
|
|
|
|
isinstance(index, type(None)) == False
|
|
|
|
and isinstance(big_npy, type(None)) == False
|
|
|
|
and index_rate != 0
|
|
|
|
):
|
2023-03-31 17:49:09 +08:00
|
|
|
npy = feats[0].cpu().numpy()
|
2023-04-15 19:44:24 +08:00
|
|
|
if self.is_half:
|
|
|
|
npy = npy.astype("float32")
|
2023-04-26 19:13:54 +08:00
|
|
|
|
|
|
|
# _, I = index.search(npy, 1)
|
|
|
|
# npy = big_npy[I.squeeze()]
|
|
|
|
|
2023-04-26 19:17:48 +08:00
|
|
|
#by github @nadare881
|
2023-04-26 19:13:54 +08:00
|
|
|
score, ix = index.search(npy, k=8)
|
|
|
|
weight = np.square(1 / score)
|
|
|
|
weight /= weight.sum(axis=1, keepdims=True)
|
|
|
|
npy = np.sum(big_npy[ix] * np.expand_dims(weight, axis=2), axis=1)
|
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
if self.is_half:
|
|
|
|
npy = npy.astype("float16")
|
|
|
|
feats = (
|
|
|
|
torch.from_numpy(npy).unsqueeze(0).to(self.device) * index_rate
|
|
|
|
+ (1 - index_rate) * feats
|
|
|
|
)
|
2023-03-31 17:49:09 +08:00
|
|
|
|
|
|
|
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
|
|
|
t1 = ttime()
|
2023-04-15 19:44:24 +08:00
|
|
|
p_len = audio0.shape[0] // self.window
|
|
|
|
if feats.shape[1] < p_len:
|
|
|
|
p_len = feats.shape[1]
|
|
|
|
if pitch != None and pitchf != None:
|
|
|
|
pitch = pitch[:, :p_len]
|
|
|
|
pitchf = pitchf[:, :p_len]
|
|
|
|
p_len = torch.tensor([p_len], device=self.device).long()
|
2023-03-31 17:49:09 +08:00
|
|
|
with torch.no_grad():
|
2023-04-15 19:44:24 +08:00
|
|
|
if pitch != None and pitchf != None:
|
|
|
|
audio1 = (
|
|
|
|
(net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0] * 32768)
|
|
|
|
.data.cpu()
|
|
|
|
.float()
|
|
|
|
.numpy()
|
|
|
|
.astype(np.int16)
|
|
|
|
)
|
2023-03-31 17:49:09 +08:00
|
|
|
else:
|
2023-04-15 19:44:24 +08:00
|
|
|
audio1 = (
|
|
|
|
(net_g.infer(feats, p_len, sid)[0][0, 0] * 32768)
|
|
|
|
.data.cpu()
|
|
|
|
.float()
|
|
|
|
.numpy()
|
|
|
|
.astype(np.int16)
|
|
|
|
)
|
|
|
|
del feats, p_len, padding_mask
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
torch.cuda.empty_cache()
|
2023-03-31 17:49:09 +08:00
|
|
|
t2 = ttime()
|
2023-04-15 19:44:24 +08:00
|
|
|
times[0] += t1 - t0
|
|
|
|
times[2] += t2 - t1
|
2023-03-31 17:49:09 +08:00
|
|
|
return audio1
|
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
def pipeline(
|
|
|
|
self,
|
|
|
|
model,
|
|
|
|
net_g,
|
|
|
|
sid,
|
|
|
|
audio,
|
|
|
|
times,
|
|
|
|
f0_up_key,
|
|
|
|
f0_method,
|
|
|
|
file_index,
|
2023-04-26 19:13:54 +08:00
|
|
|
# file_big_npy,
|
2023-04-15 19:44:24 +08:00
|
|
|
index_rate,
|
|
|
|
if_f0,
|
|
|
|
f0_file=None,
|
|
|
|
):
|
2023-04-26 19:13:54 +08:00
|
|
|
print(file_index!= "",os.path.exists(file_index) == True,index_rate != 0)
|
2023-04-15 19:44:24 +08:00
|
|
|
if (
|
2023-04-26 19:13:54 +08:00
|
|
|
file_index != ""
|
|
|
|
# and file_big_npy != ""
|
|
|
|
# and os.path.exists(file_big_npy) == True
|
2023-04-15 19:44:24 +08:00
|
|
|
and os.path.exists(file_index) == True
|
|
|
|
and index_rate != 0
|
|
|
|
):
|
2023-03-31 17:49:09 +08:00
|
|
|
try:
|
|
|
|
index = faiss.read_index(file_index)
|
2023-04-26 19:13:54 +08:00
|
|
|
# big_npy = np.load(file_big_npy)
|
|
|
|
big_npy = index.reconstruct_n(0, index.ntotal)
|
2023-03-31 17:49:09 +08:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2023-04-15 19:44:24 +08:00
|
|
|
index = big_npy = None
|
2023-03-31 17:49:09 +08:00
|
|
|
else:
|
2023-04-15 19:44:24 +08:00
|
|
|
index = big_npy = None
|
2023-04-16 18:56:20 +08:00
|
|
|
audio = signal.filtfilt(bh, ah, audio)
|
2023-04-15 19:44:24 +08:00
|
|
|
audio_pad = np.pad(audio, (self.window // 2, self.window // 2), mode="reflect")
|
2023-03-31 17:49:09 +08:00
|
|
|
opt_ts = []
|
2023-04-15 19:44:24 +08:00
|
|
|
if audio_pad.shape[0] > self.t_max:
|
2023-03-31 17:49:09 +08:00
|
|
|
audio_sum = np.zeros_like(audio)
|
2023-04-15 19:44:24 +08:00
|
|
|
for i in range(self.window):
|
|
|
|
audio_sum += audio_pad[i : i - self.window]
|
|
|
|
for t in range(self.t_center, audio.shape[0], self.t_center):
|
|
|
|
opt_ts.append(
|
|
|
|
t
|
|
|
|
- self.t_query
|
|
|
|
+ np.where(
|
|
|
|
np.abs(audio_sum[t - self.t_query : t + self.t_query])
|
|
|
|
== np.abs(audio_sum[t - self.t_query : t + self.t_query]).min()
|
|
|
|
)[0][0]
|
|
|
|
)
|
2023-03-31 17:49:09 +08:00
|
|
|
s = 0
|
2023-04-15 19:44:24 +08:00
|
|
|
audio_opt = []
|
|
|
|
t = None
|
|
|
|
t1 = ttime()
|
|
|
|
audio_pad = np.pad(audio, (self.t_pad, self.t_pad), mode="reflect")
|
|
|
|
p_len = audio_pad.shape[0] // self.window
|
|
|
|
inp_f0 = None
|
|
|
|
if hasattr(f0_file, "name") == True:
|
2023-03-31 17:49:09 +08:00
|
|
|
try:
|
2023-04-15 19:44:24 +08:00
|
|
|
with open(f0_file.name, "r") as f:
|
|
|
|
lines = f.read().strip("\n").split("\n")
|
|
|
|
inp_f0 = []
|
|
|
|
for line in lines:
|
|
|
|
inp_f0.append([float(i) for i in line.split(",")])
|
|
|
|
inp_f0 = np.array(inp_f0, dtype="float32")
|
2023-03-31 17:49:09 +08:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2023-04-15 19:44:24 +08:00
|
|
|
sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()
|
|
|
|
pitch, pitchf = None, None
|
|
|
|
if if_f0 == 1:
|
|
|
|
pitch, pitchf = self.get_f0(audio_pad, p_len, f0_up_key, f0_method, inp_f0)
|
2023-03-31 17:49:09 +08:00
|
|
|
pitch = pitch[:p_len]
|
|
|
|
pitchf = pitchf[:p_len]
|
2023-04-15 19:44:24 +08:00
|
|
|
pitch = torch.tensor(pitch, device=self.device).unsqueeze(0).long()
|
|
|
|
pitchf = torch.tensor(pitchf, device=self.device).unsqueeze(0).float()
|
|
|
|
t2 = ttime()
|
|
|
|
times[1] += t2 - t1
|
2023-03-31 17:49:09 +08:00
|
|
|
for t in opt_ts:
|
2023-04-15 19:44:24 +08:00
|
|
|
t = t // self.window * self.window
|
|
|
|
if if_f0 == 1:
|
|
|
|
audio_opt.append(
|
|
|
|
self.vc(
|
|
|
|
model,
|
|
|
|
net_g,
|
|
|
|
sid,
|
|
|
|
audio_pad[s : t + self.t_pad2 + self.window],
|
|
|
|
pitch[:, s // self.window : (t + self.t_pad2) // self.window],
|
|
|
|
pitchf[:, s // self.window : (t + self.t_pad2) // self.window],
|
|
|
|
times,
|
|
|
|
index,
|
|
|
|
big_npy,
|
|
|
|
index_rate,
|
|
|
|
)[self.t_pad_tgt : -self.t_pad_tgt]
|
|
|
|
)
|
2023-03-31 17:49:09 +08:00
|
|
|
else:
|
2023-04-15 19:44:24 +08:00
|
|
|
audio_opt.append(
|
|
|
|
self.vc(
|
|
|
|
model,
|
|
|
|
net_g,
|
|
|
|
sid,
|
|
|
|
audio_pad[s : t + self.t_pad2 + self.window],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
times,
|
|
|
|
index,
|
|
|
|
big_npy,
|
|
|
|
index_rate,
|
|
|
|
)[self.t_pad_tgt : -self.t_pad_tgt]
|
|
|
|
)
|
2023-03-31 17:49:09 +08:00
|
|
|
s = t
|
2023-04-15 19:44:24 +08:00
|
|
|
if if_f0 == 1:
|
|
|
|
audio_opt.append(
|
|
|
|
self.vc(
|
|
|
|
model,
|
|
|
|
net_g,
|
|
|
|
sid,
|
|
|
|
audio_pad[t:],
|
|
|
|
pitch[:, t // self.window :] if t is not None else pitch,
|
|
|
|
pitchf[:, t // self.window :] if t is not None else pitchf,
|
|
|
|
times,
|
|
|
|
index,
|
|
|
|
big_npy,
|
|
|
|
index_rate,
|
|
|
|
)[self.t_pad_tgt : -self.t_pad_tgt]
|
|
|
|
)
|
2023-03-31 17:49:09 +08:00
|
|
|
else:
|
2023-04-15 19:44:24 +08:00
|
|
|
audio_opt.append(
|
|
|
|
self.vc(
|
|
|
|
model,
|
|
|
|
net_g,
|
|
|
|
sid,
|
|
|
|
audio_pad[t:],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
times,
|
|
|
|
index,
|
|
|
|
big_npy,
|
|
|
|
index_rate,
|
|
|
|
)[self.t_pad_tgt : -self.t_pad_tgt]
|
|
|
|
)
|
|
|
|
audio_opt = np.concatenate(audio_opt)
|
|
|
|
del pitch, pitchf, sid
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
torch.cuda.empty_cache()
|
2023-03-31 17:49:09 +08:00
|
|
|
return audio_opt
|