WIP: refactoring

This commit is contained in:
wataru 2023-04-28 09:47:39 +09:00
parent 4fc57153e7
commit 83083a68ac
3 changed files with 496 additions and 84 deletions

View File

@ -15,6 +15,8 @@ import numpy as np
import torch import torch
from fairseq import checkpoint_utils from fairseq import checkpoint_utils
import traceback
import faiss
from const import TMP_DIR # type:ignore from const import TMP_DIR # type:ignore
@ -169,12 +171,15 @@ class RVC:
self.settings.modelSlots[slot].deprecated = tmp_onnx_session.getDeprecated() self.settings.modelSlots[slot].deprecated = tmp_onnx_session.getDeprecated()
def prepareModel(self, slot: int): def prepareModel(self, slot: int):
if slot < 0:
return self.get_info()
print("[Voice Changer] Prepare Model of slot:", slot) print("[Voice Changer] Prepare Model of slot:", slot)
onnxModelFile = self.settings.modelSlots[slot].onnxModelFile onnxModelFile = self.settings.modelSlots[slot].onnxModelFile
isONNX = ( isONNX = (
True if self.settings.modelSlots[slot].onnxModelFile is not None else False True if self.settings.modelSlots[slot].onnxModelFile is not None else False
) )
# モデルのロード
if isONNX: if isONNX:
print("[Voice Changer] Loading ONNX Model...") print("[Voice Changer] Loading ONNX Model...")
self.next_onnx_session = ModelWrapper(onnxModelFile) self.next_onnx_session = ModelWrapper(onnxModelFile)
@ -214,8 +219,36 @@ class RVC:
self.next_net_g = net_g self.next_net_g = net_g
self.next_onnx_session = None self.next_onnx_session = None
# Indexのロード
print("[Voice Changer] Loading index...")
self.next_feature_file = self.settings.modelSlots[slot].featureFile self.next_feature_file = self.settings.modelSlots[slot].featureFile
self.next_index_file = self.settings.modelSlots[slot].indexFile self.next_index_file = self.settings.modelSlots[slot].indexFile
if (
self.settings.modelSlots[slot].featureFile is not None
and self.settings.modelSlots[slot].indexFile is not None
):
if (
os.path.exists(self.settings.modelSlots[slot].featureFile) is True
and os.path.exists(self.settings.modelSlots[slot].indexFile) is True
):
try:
self.next_index = faiss.read_index(
self.settings.modelSlots[slot].indexFile
)
self.next_feature = np.load(
self.settings.modelSlots[slot].featureFile
)
except:
print("[Voice Changer] load index failed. Use no index.")
traceback.print_exc()
self.next_index = self.next_feature = None
else:
print("[Voice Changer] Index file is not found. Use no index.")
self.next_index = self.next_feature = None
else:
self.next_index = self.next_feature = None
self.next_trans = self.settings.modelSlots[slot].defaultTrans self.next_trans = self.settings.modelSlots[slot].defaultTrans
self.next_samplingRate = self.settings.modelSlots[slot].samplingRate self.next_samplingRate = self.settings.modelSlots[slot].samplingRate
self.next_framework = ( self.next_framework = (
@ -232,6 +265,8 @@ class RVC:
self.onnx_session = self.next_onnx_session self.onnx_session = self.next_onnx_session
self.feature_file = self.next_feature_file self.feature_file = self.next_feature_file
self.index_file = self.next_index_file self.index_file = self.next_index_file
self.feature = self.next_feature
self.index = self.next_index
self.settings.tran = self.next_trans self.settings.tran = self.next_trans
self.settings.framework = self.next_framework self.settings.framework = self.next_framework
self.settings.modelSamplingRate = self.next_samplingRate self.settings.modelSamplingRate = self.next_samplingRate
@ -436,14 +471,10 @@ class RVC:
repeat *= self.settings.rvcQuality # 0 or 3 repeat *= self.settings.rvcQuality # 0 or 3
vc = VC(self.settings.modelSamplingRate, dev, self.is_half, repeat) vc = VC(self.settings.modelSamplingRate, dev, self.is_half, repeat)
sid = 0 sid = 0
times = [0, 0, 0]
f0_up_key = self.settings.tran f0_up_key = self.settings.tran
f0_method = self.settings.f0Detector f0_method = self.settings.f0Detector
file_index = self.index_file if self.index_file is not None else ""
file_big_npy = self.feature_file if self.feature_file is not None else ""
index_rate = self.settings.indexRatio index_rate = self.settings.indexRatio
if_f0 = 1 if self.settings.modelSlots[self.currentSlot].f0 else 0 if_f0 = 1 if self.settings.modelSlots[self.currentSlot].f0 else 0
f0_file = None
embChannels = self.settings.modelSlots[self.currentSlot].embChannels embChannels = self.settings.modelSlots[self.currentSlot].embChannels
audio_out = vc.pipeline( audio_out = vc.pipeline(
@ -451,14 +482,12 @@ class RVC:
self.net_g, self.net_g,
sid, sid,
audio, audio,
times,
f0_up_key, f0_up_key,
f0_method, f0_method,
file_index, self.index,
file_big_npy, self.feature,
index_rate, index_rate,
if_f0, if_f0,
f0_file=f0_file,
silence_front=self.settings.extraConvertSize silence_front=self.settings.extraConvertSize
/ self.settings.modelSamplingRate, / self.settings.modelSamplingRate,
embChannels=embChannels, embChannels=embChannels,

View File

@ -1,16 +1,10 @@
import numpy as np import numpy as np
import parselmouth import parselmouth
import torch import torch
import pdb
from time import time as ttime
import torch.nn.functional as F import torch.nn.functional as F
from config import x_pad, x_query, x_center, x_max from config import x_pad, x_query, x_center, x_max
import scipy.signal as signal import scipy.signal as signal
import pyworld import pyworld
import os
import traceback
import faiss
# from .const import RVC_MODEL_TYPE_NORMAL, RVC_MODEL_TYPE_PITCHLESS, RVC_MODEL_TYPE_WEBUI_256_NORMAL, RVC_MODEL_TYPE_WEBUI_768_NORMAL, RVC_MODEL_TYPE_WEBUI_256_PITCHLESS, RVC_MODEL_TYPE_WEBUI_768_PITCHLESS
from .const import RVC_MODEL_TYPE_RVC, RVC_MODEL_TYPE_WEBUI from .const import RVC_MODEL_TYPE_RVC, RVC_MODEL_TYPE_WEBUI
@ -20,7 +14,6 @@ class VC(object):
self.window = 160 # 每帧点数 self.window = 160 # 每帧点数
self.t_pad = self.sr * x_pad # 每条前后pad时间 self.t_pad = self.sr * x_pad # 每条前后pad时间
self.t_pad_tgt = tgt_sr * x_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_query = self.sr * x_query # 查询切点前后查询时间
self.t_center = self.sr * x_center # 查询切点位置 self.t_center = self.sr * x_center # 查询切点位置
self.t_max = self.sr * x_max # 免查询时长阈值 self.t_max = self.sr * x_max # 免查询时长阈值
@ -28,7 +21,6 @@ class VC(object):
self.is_half = is_half self.is_half = is_half
def get_f0(self, audio, p_len, f0_up_key, f0_method, inp_f0=None, silence_front=0): def get_f0(self, audio, p_len, f0_up_key, f0_method, inp_f0=None, silence_front=0):
n_frames = int(len(audio) // self.window) + 1 n_frames = int(len(audio) // self.window) + 1
start_frame = int(silence_front * self.sr / self.window) start_frame = int(silence_front * self.sr / self.window)
real_silence_front = start_frame * self.window / self.sr real_silence_front = start_frame * self.window / self.sr
@ -40,14 +32,23 @@ class VC(object):
f0_max = 1100 f0_max = 1100
f0_mel_min = 1127 * np.log(1 + f0_min / 700) f0_mel_min = 1127 * np.log(1 + f0_min / 700)
f0_mel_max = 1127 * np.log(1 + f0_max / 700) f0_mel_max = 1127 * np.log(1 + f0_max / 700)
if (f0_method == "pm"): if f0_method == "pm":
f0 = parselmouth.Sound(audio, self.sr).to_pitch_ac( f0 = (
time_step=time_step / 1000, voicing_threshold=0.6, parselmouth.Sound(audio, self.sr)
pitch_floor=f0_min, pitch_ceiling=f0_max).selected_array['frequency'] .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 pad_size = (p_len - len(f0) + 1) // 2
if (pad_size > 0 or p_len - len(f0) - pad_size > 0): 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') f0 = np.pad(
elif (f0_method == "harvest"): f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant"
)
elif f0_method == "harvest":
f0, t = pyworld.harvest( f0, t = pyworld.harvest(
audio.astype(np.double), audio.astype(np.double),
fs=self.sr, fs=self.sr,
@ -57,36 +58,65 @@ class VC(object):
f0 = pyworld.stonemask(audio.astype(np.double), f0, t, self.sr) f0 = pyworld.stonemask(audio.astype(np.double), f0, t, self.sr)
f0 = signal.medfilt(f0, 3) f0 = signal.medfilt(f0, 3)
f0 = np.pad(f0.astype('float'), (start_frame, n_frames - len(f0) - start_frame)) f0 = np.pad(
f0.astype("float"), (start_frame, n_frames - len(f0) - start_frame)
)
else: else:
print("[Voice Changer] invalid f0 detector, use pm.", f0_method) print("[Voice Changer] invalid f0 detector, use pm.", f0_method)
f0 = parselmouth.Sound(audio, self.sr).to_pitch_ac( f0 = (
time_step=time_step / 1000, voicing_threshold=0.6, parselmouth.Sound(audio, self.sr)
pitch_floor=f0_min, pitch_ceiling=f0_max).selected_array['frequency'] .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 pad_size = (p_len - len(f0) + 1) // 2
if (pad_size > 0 or p_len - len(f0) - pad_size > 0): 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') f0 = np.pad(
f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant"
)
f0 *= pow(2, f0_up_key / 12) 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()])) # with open("test.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()]))
tf0 = self.sr // self.window # 每秒f0点数 tf0 = self.sr // self.window # 每秒f0点数
if (inp_f0 is not None): if inp_f0 is not None:
delta_t = np.round((inp_f0[:, 0].max() - inp_f0[:, 0].min()) * tf0 + 1).astype("int16") delta_t = np.round(
replace_f0 = np.interp(list(range(delta_t)), inp_f0[:, 0] * 100, inp_f0[:, 1]) (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] 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] f0[x_pad * tf0 : x_pad * tf0 + len(replace_f0)] = replace_f0[:shape]
# with open("test_opt.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()])) # with open("test_opt.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()]))
f0bak = f0.copy() f0bak = f0.copy()
f0_mel = 1127 * np.log(1 + f0 / 700) 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 > 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 <= 1] = 1
f0_mel[f0_mel > 255] = 255 f0_mel[f0_mel > 255] = 255
f0_coarse = np.rint(f0_mel).astype(np.int) f0_coarse = np.rint(f0_mel).astype(np.int)
return f0_coarse, f0bak # 1-0 return f0_coarse, f0bak # 1-0
def vc(self, model, net_g, sid, audio0, pitch, pitchf, times, index, big_npy, index_rate, embChannels=256): # ,file_index,file_big_npy def vc(
self,
model,
net_g,
sid,
audio0,
pitch,
pitchf,
index,
big_npy,
index_rate,
embChannels=256,
): # ,file_index,file_big_npy
feats = torch.from_numpy(audio0) feats = torch.from_numpy(audio0)
if (self.is_half == True): if self.is_half == True:
feats = feats.half() feats = feats.half()
else: else:
feats = feats.float() feats = feats.float()
@ -107,7 +137,6 @@ class VC(object):
"padding_mask": padding_mask, "padding_mask": padding_mask,
} }
t0 = ttime()
with torch.no_grad(): with torch.no_grad():
logits = model.extract_features(**inputs) logits = model.extract_features(**inputs)
if embChannels == 256: if embChannels == 256:
@ -115,82 +144,121 @@ class VC(object):
else: else:
feats = logits[0] feats = logits[0]
if (isinstance(index, type(None)) == False and isinstance(big_npy, type(None)) == False and index_rate != 0): if (
isinstance(index, type(None)) is False
and isinstance(big_npy, type(None)) is False
and index_rate != 0
):
npy = feats[0].cpu().numpy() npy = feats[0].cpu().numpy()
if (self.is_half == True): if self.is_half is True:
npy = npy.astype("float32") npy = npy.astype("float32")
D, I = index.search(npy, 1) D, I = index.search(npy, 1)
npy = big_npy[I.squeeze()] npy = big_npy[I.squeeze()]
if (self.is_half == True): if self.is_half is True:
npy = npy.astype("float16") npy = npy.astype("float16")
feats = torch.from_numpy(npy).unsqueeze(0).to(self.device) * index_rate + (1 - index_rate) * feats
feats = (
torch.from_numpy(npy).unsqueeze(0).to(self.device) * index_rate
+ (1 - index_rate) * feats
)
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1) feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
t1 = ttime()
p_len = audio0.shape[0] // self.window p_len = audio0.shape[0] // self.window
if (feats.shape[1] < p_len): if feats.shape[1] < p_len:
p_len = feats.shape[1] p_len = feats.shape[1]
if (pitch != None and pitchf != None): if pitch is not None and pitchf is not None:
pitch = pitch[:, :p_len] pitch = pitch[:, :p_len]
pitchf = pitchf[:, :p_len] pitchf = pitchf[:, :p_len]
p_len = torch.tensor([p_len], device=self.device).long() p_len = torch.tensor([p_len], device=self.device).long()
with torch.no_grad(): with torch.no_grad():
if pitch != None: if pitch is not None:
audio1 = (net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0] * 32768).data.cpu().float().numpy().astype(np.int16) audio1 = (
(net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0] * 32768)
.data.cpu()
.float()
.numpy()
.astype(np.int16)
)
else: else:
if hasattr(net_g, "infer_pitchless"): if hasattr(net_g, "infer_pitchless"):
audio1 = (net_g.infer_pitchless(feats, p_len, sid)[0][0, 0] * 32768).data.cpu().float().numpy().astype(np.int16) audio1 = (
(net_g.infer_pitchless(feats, p_len, sid)[0][0, 0] * 32768)
.data.cpu()
.float()
.numpy()
.astype(np.int16)
)
else: else:
audio1 = (net_g.infer(feats, p_len, sid)[0][0, 0] * 32768).data.cpu().float().numpy().astype(np.int16) audio1 = (
(net_g.infer(feats, p_len, sid)[0][0, 0] * 32768)
# audio1 = (net_g.infer(feats, p_len, None, pitchf, sid)[0][0, 0] * 32768).data.cpu().float().numpy().astype(np.int16) .data.cpu()
.float()
.numpy()
.astype(np.int16)
)
del feats, p_len, padding_mask del feats, p_len, padding_mask
torch.cuda.empty_cache() torch.cuda.empty_cache()
t2 = ttime()
times[0] += (t1 - t0)
times[2] += (t2 - t1)
return audio1 return audio1
def pipeline(self, model, net_g, sid, audio, times, f0_up_key, f0_method, file_index, file_big_npy, index_rate, if_f0, f0_file=None, silence_front=0, embChannels=256): def pipeline(
if (file_big_npy != "" and file_index != "" and os.path.exists(file_big_npy) == True and os.path.exists(file_index) == True and index_rate != 0): self,
try: embedder,
index = faiss.read_index(file_index) model,
big_npy = np.load(file_big_npy) sid,
except: audio,
traceback.print_exc() f0_up_key,
index = big_npy = None f0_method,
else: index,
index = big_npy = None big_npy,
index_rate,
audio_opt = [] if_f0,
t = None silence_front=0,
t1 = ttime() embChannels=256,
audio_pad = np.pad(audio, (self.t_pad, self.t_pad), mode='reflect') ):
audio_pad = np.pad(audio, (self.t_pad, self.t_pad), mode="reflect")
p_len = audio_pad.shape[0] // self.window p_len = audio_pad.shape[0] // self.window
inp_f0 = None inp_f0 = None
sid = torch.tensor(sid, device=self.device).unsqueeze(0).long() sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()
# ピッチ検出
pitch, pitchf = None, None pitch, pitchf = None, None
if (if_f0 == 1): if if_f0 == 1:
pitch, pitchf = self.get_f0(audio_pad, p_len, f0_up_key, f0_method, inp_f0, silence_front=silence_front) pitch, pitchf = self.get_f0(
audio_pad,
p_len,
f0_up_key,
f0_method,
inp_f0,
silence_front=silence_front,
)
pitch = pitch[:p_len] pitch = pitch[:p_len]
pitchf = pitchf[:p_len] pitchf = pitchf[:p_len]
pitch = torch.tensor(pitch, device=self.device).unsqueeze(0).long() pitch = torch.tensor(pitch, device=self.device).unsqueeze(0).long()
pitchf = torch.tensor(pitchf, device=self.device, dtype=torch.float).unsqueeze(0) pitchf = torch.tensor(
pitchf, device=self.device, dtype=torch.float
).unsqueeze(0)
t2 = ttime() output = self.vc(
times[1] += (t2 - t1) embedder,
if self.t_pad_tgt == 0: model,
audio_opt.append(self.vc(model, net_g, sid, audio_pad[t:], pitch[:, t // self.window:]if t is not None else pitch, sid,
pitchf[:, t // self.window:]if t is not None else pitchf, times, index, big_npy, index_rate, embChannels)) audio_pad,
else: pitch,
audio_opt.append(self.vc(model, net_g, sid, audio_pad[t:], pitch[:, t // self.window:]if t is not None else pitch, pitchf,
pitchf[:, t // self.window:]if t is not None else pitchf, times, index, big_npy, index_rate, embChannels)[self.t_pad_tgt:-self.t_pad_tgt]) index,
big_npy,
index_rate,
embChannels,
)
if self.t_pad_tgt != 0:
offset = self.t_pad_tgt
end = -1 * self.t_pad_tgt
output = output[offset:end]
audio_opt = np.concatenate(audio_opt)
del pitch, pitchf, sid del pitch, pitchf, sid
torch.cuda.empty_cache() torch.cuda.empty_cache()
return audio_opt return output

View File

@ -0,0 +1,315 @@
import numpy as np
import parselmouth
import torch
import pdb
from time import time as ttime
import torch.nn.functional as F
from config import x_pad, x_query, x_center, x_max
import scipy.signal as signal
import pyworld
import os
import traceback
import faiss
from .const import RVC_MODEL_TYPE_RVC, RVC_MODEL_TYPE_WEBUI
class VC(object):
def __init__(self, tgt_sr, device, is_half, x_pad):
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
def get_f0(self, audio, p_len, f0_up_key, f0_method, inp_f0=None, silence_front=0):
n_frames = int(len(audio) // self.window) + 1
start_frame = int(silence_front * self.sr / self.window)
real_silence_front = start_frame * self.window / self.sr
audio = audio[int(np.round(real_silence_front * self.sr)) :]
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)
if f0_method == "pm":
f0 = (
parselmouth.Sound(audio, 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":
f0, t = pyworld.harvest(
audio.astype(np.double),
fs=self.sr,
f0_ceil=f0_max,
frame_period=10,
)
f0 = pyworld.stonemask(audio.astype(np.double), f0, t, self.sr)
f0 = signal.medfilt(f0, 3)
f0 = np.pad(
f0.astype("float"), (start_frame, n_frames - len(f0) - start_frame)
)
else:
print("[Voice Changer] invalid f0 detector, use pm.", f0_method)
f0 = (
parselmouth.Sound(audio, 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"
)
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()]))
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]
# 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)
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
f0_coarse = np.rint(f0_mel).astype(np.int)
return f0_coarse, f0bak # 1-0
def vc(
self,
model,
net_g,
sid,
audio0,
pitch,
pitchf,
times,
index,
big_npy,
index_rate,
embChannels=256,
): # ,file_index,file_big_npy
feats = torch.from_numpy(audio0)
if self.is_half == True:
feats = feats.half()
else:
feats = feats.float()
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)
if embChannels == 256:
inputs = {
"source": feats.to(self.device),
"padding_mask": padding_mask,
"output_layer": 9, # layer 9
}
else:
inputs = {
"source": feats.to(self.device),
"padding_mask": padding_mask,
}
t0 = ttime()
with torch.no_grad():
logits = model.extract_features(**inputs)
if embChannels == 256:
feats = model.final_proj(logits[0])
else:
feats = logits[0]
if (
isinstance(index, type(None)) == False
and isinstance(big_npy, type(None)) == False
and index_rate != 0
):
npy = feats[0].cpu().numpy()
if self.is_half == True:
npy = npy.astype("float32")
D, I = index.search(npy, 1)
npy = big_npy[I.squeeze()]
if self.is_half == True:
npy = npy.astype("float16")
feats = (
torch.from_numpy(npy).unsqueeze(0).to(self.device) * index_rate
+ (1 - index_rate) * feats
)
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
t1 = ttime()
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()
with torch.no_grad():
if pitch != None:
audio1 = (
(net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0] * 32768)
.data.cpu()
.float()
.numpy()
.astype(np.int16)
)
else:
if hasattr(net_g, "infer_pitchless"):
audio1 = (
(net_g.infer_pitchless(feats, p_len, sid)[0][0, 0] * 32768)
.data.cpu()
.float()
.numpy()
.astype(np.int16)
)
else:
audio1 = (
(net_g.infer(feats, p_len, sid)[0][0, 0] * 32768)
.data.cpu()
.float()
.numpy()
.astype(np.int16)
)
# audio1 = (net_g.infer(feats, p_len, None, pitchf, sid)[0][0, 0] * 32768).data.cpu().float().numpy().astype(np.int16)
del feats, p_len, padding_mask
torch.cuda.empty_cache()
t2 = ttime()
times[0] += t1 - t0
times[2] += t2 - t1
return audio1
def pipeline(
self,
model,
net_g,
sid,
audio,
times,
f0_up_key,
f0_method,
file_index,
file_big_npy,
index_rate,
if_f0,
f0_file=None,
silence_front=0,
embChannels=256,
):
if (
file_big_npy != ""
and file_index != ""
and os.path.exists(file_big_npy) == True
and os.path.exists(file_index) == True
and index_rate != 0
):
try:
index = faiss.read_index(file_index)
big_npy = np.load(file_big_npy)
except:
traceback.print_exc()
index = big_npy = None
else:
index = big_npy = None
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
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,
silence_front=silence_front,
)
pitch = pitch[:p_len]
pitchf = pitchf[:p_len]
pitch = torch.tensor(pitch, device=self.device).unsqueeze(0).long()
pitchf = torch.tensor(
pitchf, device=self.device, dtype=torch.float
).unsqueeze(0)
t2 = ttime()
times[1] += t2 - t1
if self.t_pad_tgt == 0:
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,
embChannels,
)
)
else:
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,
embChannels,
)[self.t_pad_tgt : -self.t_pad_tgt]
)
audio_opt = np.concatenate(audio_opt)
del pitch, pitchf, sid
torch.cuda.empty_cache()
return audio_opt