vc modules
This commit is contained in:
parent
6396af8249
commit
2e56c5c600
0
infer/modules/vc/__init__.py
Normal file
0
infer/modules/vc/__init__.py
Normal file
219
infer/modules/vc/modules.py
Normal file
219
infer/modules/vc/modules.py
Normal file
@ -0,0 +1,219 @@
|
||||
import traceback
|
||||
|
||||
import torch
|
||||
import soundfile as sf
|
||||
|
||||
from infer.lib.infer_pack.models import (
|
||||
SynthesizerTrnMs256NSFsid,
|
||||
SynthesizerTrnMs256NSFsid_nono,
|
||||
SynthesizerTrnMs768NSFsid,
|
||||
SynthesizerTrnMs768NSFsid_nono,
|
||||
)
|
||||
from infer.modules.vc.pipeline import Pipeline
|
||||
from infer.modules.vc.utils import *
|
||||
|
||||
|
||||
class VC:
|
||||
def __init__(self, config):
|
||||
self.n_spk = None
|
||||
self.tgt_sr = None
|
||||
self.net_g = None
|
||||
self.pipeline = None
|
||||
self.cpt = None
|
||||
self.version = None
|
||||
self.if_f0 = None
|
||||
self.version = None
|
||||
self.hubert_model = None
|
||||
|
||||
self.config = config
|
||||
|
||||
def get_vc(self, sid, to_return_protect0, to_return_protect1):
|
||||
person = f'{os.getenv("weight_root")}/{sid}'
|
||||
print(f'loading {person}')
|
||||
|
||||
self.cpt = torch.load(person, map_location="cpu")
|
||||
self.tgt_sr = self.cpt["config"][-1]
|
||||
self.cpt["config"][-3] = self.cpt["weight"]["emb_g.weight"].shape[0] # n_spk
|
||||
self.if_f0 = self.cpt.get("f0", 1)
|
||||
self.version = self.cpt.get("version", "v1")
|
||||
|
||||
to_return_protect0 = {
|
||||
"visible": self.if_f0 != 0,
|
||||
"value": to_return_protect0 if self.if_f0 != 0 else 0.5,
|
||||
"__type__": "update",
|
||||
}
|
||||
to_return_protect1 = {
|
||||
"visible": self.if_f0 != 0,
|
||||
"value": to_return_protect1 if self.if_f0 != 0 else 0.33,
|
||||
"__type__": "update",
|
||||
}
|
||||
|
||||
synthesizer_class = {
|
||||
("v1", 1): SynthesizerTrnMs256NSFsid,
|
||||
("v1", 0): SynthesizerTrnMs256NSFsid_nono,
|
||||
("v2", 1): SynthesizerTrnMs768NSFsid,
|
||||
("v2", 0): SynthesizerTrnMs768NSFsid_nono
|
||||
}
|
||||
|
||||
self.net_g = synthesizer_class.get((self.version, self.if_f0), SynthesizerTrnMs256NSFsid)(*self.cpt["config"], is_half=self.config.is_half)
|
||||
|
||||
del self.net_g.enc_q
|
||||
|
||||
self.net_g.load_state_dict(self.cpt["weight"], strict=False)
|
||||
self.net_g.eval().to(self.config.device)
|
||||
if self.config.is_half:
|
||||
self.net_g = self.net_g.half()
|
||||
else:
|
||||
self.net_g = self.net_g.float()
|
||||
|
||||
self.pipeline = Pipeline(self.tgt_sr, self.config)
|
||||
n_spk = self.cpt["config"][-3]
|
||||
index = {
|
||||
"value": get_index_path_from_model(sid),
|
||||
"__type__": "update"
|
||||
}
|
||||
|
||||
return (
|
||||
{"visible": True, "maximum": n_spk, "__type__": "update"},
|
||||
to_return_protect0,
|
||||
to_return_protect1,
|
||||
index,
|
||||
index
|
||||
)
|
||||
|
||||
def vc_single(self, sid, input_audio_path, f0_up_key, f0_file, f0_method, file_index, file_index2, index_rate, filter_radius, resample_sr, rms_mix_rate, protect):
|
||||
if input_audio_path is None:
|
||||
return "You need to upload an audio", None
|
||||
f0_up_key = int(f0_up_key)
|
||||
try:
|
||||
audio = load_audio(input_audio_path, 16000)
|
||||
audio_max = np.abs(audio).max() / 0.95
|
||||
if audio_max > 1:
|
||||
audio /= audio_max
|
||||
times = [0, 0, 0]
|
||||
|
||||
if self.hubert_model is None:
|
||||
self.hubert_model = load_hubert(self.config)
|
||||
|
||||
file_index = (
|
||||
(
|
||||
file_index.strip(" ")
|
||||
.strip('"')
|
||||
.strip("\n")
|
||||
.strip('"')
|
||||
.strip(" ")
|
||||
.replace("trained", "added")
|
||||
)
|
||||
if file_index != ""
|
||||
else file_index2
|
||||
) # 防止小白写错,自动帮他替换掉
|
||||
|
||||
audio_opt = Pipeline.pipeline(
|
||||
self.hubert_model,
|
||||
self.net_g,
|
||||
sid,
|
||||
audio,
|
||||
input_audio_path,
|
||||
times,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
file_index,
|
||||
index_rate,
|
||||
self.if_f0,
|
||||
filter_radius,
|
||||
self.tgt_sr,
|
||||
resample_sr,
|
||||
rms_mix_rate,
|
||||
self.version,
|
||||
protect,
|
||||
f0_file,
|
||||
)
|
||||
if self.tgt_sr != resample_sr >= 16000:
|
||||
self.tgt_sr = resample_sr
|
||||
index_info = (
|
||||
"Using index:%s." % file_index
|
||||
if os.path.exists(file_index)
|
||||
else "Index not used."
|
||||
)
|
||||
return f"Success.\n {index_info}\nTime:\n npy:{times[0]}s, f0:{times[1]}s, infer:{times[2]}s", (self.tgt_sr, audio_opt)
|
||||
except:
|
||||
info = traceback.format_exc()
|
||||
print(info)
|
||||
return info, (None, None)
|
||||
|
||||
def vc_multi(
|
||||
self,
|
||||
sid,
|
||||
dir_path,
|
||||
opt_root,
|
||||
paths,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
file_index,
|
||||
file_index2,
|
||||
index_rate,
|
||||
filter_radius,
|
||||
resample_sr,
|
||||
rms_mix_rate,
|
||||
protect,
|
||||
format1):
|
||||
try:
|
||||
dir_path = (
|
||||
dir_path.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
||||
) # 防止小白拷路径头尾带了空格和"和回车
|
||||
opt_root = opt_root.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
||||
os.makedirs(opt_root, exist_ok=True)
|
||||
try:
|
||||
if dir_path != "":
|
||||
paths = [os.path.join(dir_path, name) for name in os.listdir(dir_path)]
|
||||
else:
|
||||
paths = [path.name for path in paths]
|
||||
except:
|
||||
traceback.print_exc()
|
||||
paths = [path.name for path in paths]
|
||||
infos = []
|
||||
for path in paths:
|
||||
info, opt = self.vc_single(
|
||||
sid,
|
||||
path,
|
||||
f0_up_key,
|
||||
None,
|
||||
f0_method,
|
||||
file_index,
|
||||
file_index2,
|
||||
# file_big_npy,
|
||||
index_rate,
|
||||
filter_radius,
|
||||
resample_sr,
|
||||
rms_mix_rate,
|
||||
protect,
|
||||
)
|
||||
if "Success" in info:
|
||||
try:
|
||||
tgt_sr, audio_opt = opt
|
||||
if format1 in ["wav", "flac"]:
|
||||
sf.write(
|
||||
"%s/%s.%s" % (opt_root, os.path.basename(path), format1),
|
||||
audio_opt,
|
||||
tgt_sr,
|
||||
)
|
||||
else:
|
||||
path = "%s/%s.wav" % (opt_root, os.path.basename(path))
|
||||
sf.write(
|
||||
path,
|
||||
audio_opt,
|
||||
tgt_sr,
|
||||
)
|
||||
if os.path.exists(path):
|
||||
os.system(
|
||||
"ffmpeg -i %s -vn %s -q:a 2 -y"
|
||||
% (path, path[:-4] + ".%s" % format1)
|
||||
)
|
||||
except:
|
||||
info += traceback.format_exc()
|
||||
infos.append("%s->%s" % (os.path.basename(path), info))
|
||||
yield "\n".join(infos)
|
||||
yield "\n".join(infos)
|
||||
except:
|
||||
yield traceback.format_exc()
|
||||
|
456
infer/modules/vc/pipeline.py
Normal file
456
infer/modules/vc/pipeline.py
Normal file
@ -0,0 +1,456 @@
|
||||
import sys
|
||||
from time import time as ttime
|
||||
|
||||
import numpy as np
|
||||
import parselmouth
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
import pyworld, os, traceback, faiss, librosa, torchcrepe
|
||||
from scipy import signal
|
||||
from functools import lru_cache
|
||||
|
||||
now_dir = os.getcwd()
|
||||
sys.path.append(now_dir)
|
||||
|
||||
bh, ah = signal.butter(N=5, Wn=48, btype="high", fs=16000)
|
||||
|
||||
input_audio_path2wav = {}
|
||||
|
||||
|
||||
@lru_cache
|
||||
def cache_harvest_f0(input_audio_path, fs, f0max, f0min, frame_period):
|
||||
audio = input_audio_path2wav[input_audio_path]
|
||||
f0, t = pyworld.harvest(
|
||||
audio,
|
||||
fs=fs,
|
||||
f0_ceil=f0max,
|
||||
f0_floor=f0min,
|
||||
frame_period=frame_period,
|
||||
)
|
||||
f0 = pyworld.stonemask(audio, f0, t, fs)
|
||||
return f0
|
||||
|
||||
|
||||
def change_rms(data1, sr1, data2, sr2, rate): # 1是输入音频,2是输出音频,rate是2的占比
|
||||
# print(data1.max(),data2.max())
|
||||
rms1 = librosa.feature.rms(
|
||||
y=data1, frame_length=sr1 // 2 * 2, hop_length=sr1 // 2
|
||||
) # 每半秒一个点
|
||||
rms2 = librosa.feature.rms(y=data2, frame_length=sr2 // 2 * 2, hop_length=sr2 // 2)
|
||||
rms1 = torch.from_numpy(rms1)
|
||||
rms1 = F.interpolate(
|
||||
rms1.unsqueeze(0), size=data2.shape[0], mode="linear"
|
||||
).squeeze()
|
||||
rms2 = torch.from_numpy(rms2)
|
||||
rms2 = F.interpolate(
|
||||
rms2.unsqueeze(0), size=data2.shape[0], mode="linear"
|
||||
).squeeze()
|
||||
rms2 = torch.max(rms2, torch.zeros_like(rms2) + 1e-6)
|
||||
data2 *= (
|
||||
torch.pow(rms1, torch.tensor(1 - rate))
|
||||
* torch.pow(rms2, torch.tensor(rate - 1))
|
||||
).numpy()
|
||||
return data2
|
||||
|
||||
|
||||
class Pipeline(object):
|
||||
def __init__(self, tgt_sr, config):
|
||||
self.x_pad, self.x_query, self.x_center, self.x_max, self.is_half = (
|
||||
config.x_pad,
|
||||
config.x_query,
|
||||
config.x_center,
|
||||
config.x_max,
|
||||
config.is_half,
|
||||
)
|
||||
self.sr = 16000 # hubert输入采样率
|
||||
self.window = 160 # 每帧点数
|
||||
self.t_pad = self.sr * self.x_pad # 每条前后pad时间
|
||||
self.t_pad_tgt = tgt_sr * self.x_pad
|
||||
self.t_pad2 = self.t_pad * 2
|
||||
self.t_query = self.sr * self.x_query # 查询切点前后查询时间
|
||||
self.t_center = self.sr * self.x_center # 查询切点位置
|
||||
self.t_max = self.sr * self.x_max # 免查询时长阈值
|
||||
self.device = config.device
|
||||
|
||||
self.model_rmvpe = None
|
||||
|
||||
def get_f0(
|
||||
self,
|
||||
input_audio_path,
|
||||
x,
|
||||
p_len,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
filter_radius,
|
||||
inp_f0=None,
|
||||
):
|
||||
global input_audio_path2wav
|
||||
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(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":
|
||||
input_audio_path2wav[input_audio_path] = x.astype(np.double)
|
||||
f0 = cache_harvest_f0(input_audio_path, self.sr, f0_max, f0_min, 10)
|
||||
if filter_radius > 2:
|
||||
f0 = signal.medfilt(f0, 3)
|
||||
elif f0_method == "crepe":
|
||||
model = "full"
|
||||
# Pick a batch size that doesn't cause memory errors on your gpu
|
||||
batch_size = 512
|
||||
# Compute pitch using first gpu
|
||||
audio = torch.tensor(np.copy(x))[None].float()
|
||||
f0, pd = torchcrepe.predict(
|
||||
audio,
|
||||
self.sr,
|
||||
self.window,
|
||||
f0_min,
|
||||
f0_max,
|
||||
model,
|
||||
batch_size=batch_size,
|
||||
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()
|
||||
elif f0_method == "rmvpe":
|
||||
if not hasattr(self, "model_rmvpe"):
|
||||
from infer.lib.rmvpe import RMVPE
|
||||
|
||||
print("loading rmvpe model")
|
||||
self.model_rmvpe = RMVPE(
|
||||
"rmvpe.pt", is_half=self.is_half, device=self.device
|
||||
)
|
||||
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
|
||||
|
||||
if "privateuseone" in str(self.device): # clean ortruntime memory
|
||||
del self.model_rmvpe.model
|
||||
del self.model_rmvpe
|
||||
print("cleaning ortruntime memory")
|
||||
|
||||
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[self.x_pad * tf0: self.x_pad * tf0 + len(replace_f0)].shape[0]
|
||||
f0[self.x_pad * tf0: self.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.int32)
|
||||
return f0_coarse, f0bak # 1-0
|
||||
|
||||
def vc(
|
||||
self,
|
||||
model,
|
||||
net_g,
|
||||
sid,
|
||||
audio0,
|
||||
pitch,
|
||||
pitchf,
|
||||
times,
|
||||
index,
|
||||
big_npy,
|
||||
index_rate,
|
||||
version,
|
||||
protect,
|
||||
): # ,file_index,file_big_npy
|
||||
feats = torch.from_numpy(audio0)
|
||||
if self.is_half:
|
||||
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)
|
||||
|
||||
inputs = {
|
||||
"source": feats.to(self.device),
|
||||
"padding_mask": padding_mask,
|
||||
"output_layer": 9 if version == "v1" else 12,
|
||||
}
|
||||
t0 = ttime()
|
||||
with torch.no_grad():
|
||||
logits = model.extract_features(**inputs)
|
||||
feats = model.final_proj(logits[0]) if version == "v1" else logits[0]
|
||||
if protect < 0.5 and pitch is not None and pitchf is not None:
|
||||
feats0 = feats.clone()
|
||||
if (
|
||||
not isinstance(index, type(None))
|
||||
and not isinstance(big_npy, type(None))
|
||||
and index_rate != 0
|
||||
):
|
||||
npy = feats[0].cpu().numpy()
|
||||
if self.is_half:
|
||||
npy = npy.astype("float32")
|
||||
|
||||
# _, I = index.search(npy, 1)
|
||||
# npy = big_npy[I.squeeze()]
|
||||
|
||||
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)
|
||||
|
||||
if self.is_half:
|
||||
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)
|
||||
if protect < 0.5 and pitch is not None and pitchf is not None:
|
||||
feats0 = F.interpolate(feats0.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 is not None and pitchf is not None:
|
||||
pitch = pitch[:, :p_len]
|
||||
pitchf = pitchf[:, :p_len]
|
||||
|
||||
if protect < 0.5 and pitch is not None and pitchf is not None:
|
||||
pitchff = pitchf.clone()
|
||||
pitchff[pitchf > 0] = 1
|
||||
pitchff[pitchf < 1] = protect
|
||||
pitchff = pitchff.unsqueeze(-1)
|
||||
feats = feats * pitchff + feats0 * (1 - pitchff)
|
||||
feats = feats.to(feats0.dtype)
|
||||
p_len = torch.tensor([p_len], device=self.device).long()
|
||||
with torch.no_grad():
|
||||
if pitch is not None and pitchf is not None:
|
||||
audio1 = (
|
||||
(net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0])
|
||||
.data.cpu()
|
||||
.float()
|
||||
.numpy()
|
||||
)
|
||||
else:
|
||||
audio1 = (
|
||||
(net_g.infer(feats, p_len, sid)[0][0, 0]).data.cpu().float().numpy()
|
||||
)
|
||||
del feats, p_len, padding_mask
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.empty_cache()
|
||||
t2 = ttime()
|
||||
times[0] += t1 - t0
|
||||
times[2] += t2 - t1
|
||||
return audio1
|
||||
|
||||
def pipeline(
|
||||
self,
|
||||
model,
|
||||
net_g,
|
||||
sid,
|
||||
audio,
|
||||
input_audio_path,
|
||||
times,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
file_index,
|
||||
# file_big_npy,
|
||||
index_rate,
|
||||
if_f0,
|
||||
filter_radius,
|
||||
tgt_sr,
|
||||
resample_sr,
|
||||
rms_mix_rate,
|
||||
version,
|
||||
protect,
|
||||
f0_file=None,
|
||||
):
|
||||
print(file_index)
|
||||
if (
|
||||
file_index != ""
|
||||
# and file_big_npy != ""
|
||||
# and os.path.exists(file_big_npy) == True
|
||||
and os.path.exists(file_index)
|
||||
and index_rate != 0
|
||||
):
|
||||
try:
|
||||
index = faiss.read_index(file_index)
|
||||
# big_npy = np.load(file_big_npy)
|
||||
big_npy = index.reconstruct_n(0, index.ntotal)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
index = big_npy = None
|
||||
else:
|
||||
index = big_npy = None
|
||||
audio = signal.filtfilt(bh, ah, audio)
|
||||
audio_pad = np.pad(audio, (self.window // 2, self.window // 2), mode="reflect")
|
||||
opt_ts = []
|
||||
if audio_pad.shape[0] > self.t_max:
|
||||
audio_sum = np.zeros_like(audio)
|
||||
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]
|
||||
)
|
||||
s = 0
|
||||
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"):
|
||||
try:
|
||||
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")
|
||||
except:
|
||||
traceback.print_exc()
|
||||
sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()
|
||||
pitch, pitchf = None, None
|
||||
if if_f0 == 1:
|
||||
pitch, pitchf = self.get_f0(
|
||||
input_audio_path,
|
||||
audio_pad,
|
||||
p_len,
|
||||
f0_up_key,
|
||||
f0_method,
|
||||
filter_radius,
|
||||
inp_f0,
|
||||
)
|
||||
pitch = pitch[:p_len]
|
||||
pitchf = pitchf[:p_len]
|
||||
if self.device == "mps":
|
||||
pitchf = pitchf.astype(np.float32)
|
||||
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
|
||||
for t in opt_ts:
|
||||
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,
|
||||
version,
|
||||
protect,
|
||||
)[self.t_pad_tgt: -self.t_pad_tgt]
|
||||
)
|
||||
else:
|
||||
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,
|
||||
version,
|
||||
protect,
|
||||
)[self.t_pad_tgt: -self.t_pad_tgt]
|
||||
)
|
||||
s = t
|
||||
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,
|
||||
version,
|
||||
protect,
|
||||
)[self.t_pad_tgt: -self.t_pad_tgt]
|
||||
)
|
||||
else:
|
||||
audio_opt.append(
|
||||
self.vc(
|
||||
model,
|
||||
net_g,
|
||||
sid,
|
||||
audio_pad[t:],
|
||||
None,
|
||||
None,
|
||||
times,
|
||||
index,
|
||||
big_npy,
|
||||
index_rate,
|
||||
version,
|
||||
protect,
|
||||
)[self.t_pad_tgt: -self.t_pad_tgt]
|
||||
)
|
||||
audio_opt = np.concatenate(audio_opt)
|
||||
if rms_mix_rate != 1:
|
||||
audio_opt = change_rms(audio, 16000, audio_opt, tgt_sr, rms_mix_rate)
|
||||
if tgt_sr != resample_sr >= 16000:
|
||||
audio_opt = librosa.resample(
|
||||
audio_opt, orig_sr=tgt_sr, target_sr=resample_sr
|
||||
)
|
||||
audio_max = np.abs(audio_opt).max() / 0.99
|
||||
max_int16 = 32768
|
||||
if audio_max > 1:
|
||||
max_int16 /= audio_max
|
||||
audio_opt = (audio_opt * max_int16).astype(np.int16)
|
||||
del pitch, pitchf, sid
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.empty_cache()
|
||||
return audio_opt
|
||||
|
42
infer/modules/vc/utils.py
Normal file
42
infer/modules/vc/utils.py
Normal file
@ -0,0 +1,42 @@
|
||||
import os
|
||||
|
||||
import numpy as np
|
||||
import ffmpeg
|
||||
from fairseq import checkpoint_utils
|
||||
|
||||
|
||||
def get_index_path_from_model(sid):
|
||||
return next((f for f in [os.path.join(root, name) for root, dirs, files in os.walk(os.getenv("index_root"), topdown=False) for name in files if name.endswith(".index") and "trained" not in name] if sid.split(".")[0] in f), "")
|
||||
|
||||
|
||||
def load_hubert(config):
|
||||
models, _, _ = checkpoint_utils.load_model_ensemble_and_task(
|
||||
["assets/hubert/hubert_base.pt"],
|
||||
suffix="",
|
||||
)
|
||||
hubert_model = models[0]
|
||||
hubert_model = hubert_model.to(config.device)
|
||||
if config.is_half:
|
||||
hubert_model = hubert_model.half()
|
||||
else:
|
||||
hubert_model = hubert_model.float()
|
||||
return hubert_model.eval()
|
||||
|
||||
|
||||
def load_audio(file, sr):
|
||||
try:
|
||||
# https://github.com/openai/whisper/blob/main/whisper/audio.py#L26
|
||||
# This launches a subprocess to decode audio while down-mixing and resampling as necessary.
|
||||
# Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
|
||||
file = (
|
||||
file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
||||
) # 防止小白拷路径头尾带了空格和"和回车
|
||||
out, _ = (
|
||||
ffmpeg.input(file, threads=0)
|
||||
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
|
||||
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
||||
)
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Failed to load audio: {e}")
|
||||
|
||||
return np.frombuffer(out, np.float32).flatten()
|
Loading…
Reference in New Issue
Block a user