1
0
mirror of synced 2024-11-28 01:10:56 +01:00
This commit is contained in:
Ftps 2023-08-19 19:57:41 +09:00
commit c25bb6c5d5
3 changed files with 137 additions and 107 deletions

View File

@ -24,19 +24,19 @@ class VC:
self.if_f0 = None self.if_f0 = None
self.version = None self.version = None
self.hubert_model = None self.hubert_model = None
self.config = config self.config = config
def get_vc(self, sid, to_return_protect0, to_return_protect1): def get_vc(self, sid, to_return_protect0, to_return_protect1):
person = f'{os.getenv("weight_root")}/{sid}' person = f'{os.getenv("weight_root")}/{sid}'
print(f'loading {person}') print(f"loading {person}")
self.cpt = torch.load(person, map_location="cpu") self.cpt = torch.load(person, map_location="cpu")
self.tgt_sr = self.cpt["config"][-1] self.tgt_sr = self.cpt["config"][-1]
self.cpt["config"][-3] = self.cpt["weight"]["emb_g.weight"].shape[0] # n_spk self.cpt["config"][-3] = self.cpt["weight"]["emb_g.weight"].shape[0] # n_spk
self.if_f0 = self.cpt.get("f0", 1) self.if_f0 = self.cpt.get("f0", 1)
self.version = self.cpt.get("version", "v1") self.version = self.cpt.get("version", "v1")
to_return_protect0 = { to_return_protect0 = {
"visible": self.if_f0 != 0, "visible": self.if_f0 != 0,
"value": to_return_protect0 if self.if_f0 != 0 else 0.5, "value": to_return_protect0 if self.if_f0 != 0 else 0.5,
@ -47,16 +47,18 @@ class VC:
"value": to_return_protect1 if self.if_f0 != 0 else 0.33, "value": to_return_protect1 if self.if_f0 != 0 else 0.33,
"__type__": "update", "__type__": "update",
} }
synthesizer_class = { synthesizer_class = {
("v1", 1): SynthesizerTrnMs256NSFsid, ("v1", 1): SynthesizerTrnMs256NSFsid,
("v1", 0): SynthesizerTrnMs256NSFsid_nono, ("v1", 0): SynthesizerTrnMs256NSFsid_nono,
("v2", 1): SynthesizerTrnMs768NSFsid, ("v2", 1): SynthesizerTrnMs768NSFsid,
("v2", 0): SynthesizerTrnMs768NSFsid_nono ("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) 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 del self.net_g.enc_q
self.net_g.load_state_dict(self.cpt["weight"], strict=False) self.net_g.load_state_dict(self.cpt["weight"], strict=False)
@ -65,23 +67,34 @@ class VC:
self.net_g = self.net_g.half() self.net_g = self.net_g.half()
else: else:
self.net_g = self.net_g.float() self.net_g = self.net_g.float()
self.pipeline = Pipeline(self.tgt_sr, self.config) self.pipeline = Pipeline(self.tgt_sr, self.config)
n_spk = self.cpt["config"][-3] n_spk = self.cpt["config"][-3]
index = { index = {"value": get_index_path_from_model(sid), "__type__": "update"}
"value": get_index_path_from_model(sid),
"__type__": "update"
}
return ( return (
{"visible": True, "maximum": n_spk, "__type__": "update"}, {"visible": True, "maximum": n_spk, "__type__": "update"},
to_return_protect0, to_return_protect0,
to_return_protect1, to_return_protect1,
index, index,
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): 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: if input_audio_path is None:
return "You need to upload an audio", None return "You need to upload an audio", None
f0_up_key = int(f0_up_key) f0_up_key = int(f0_up_key)
@ -91,10 +104,10 @@ class VC:
if audio_max > 1: if audio_max > 1:
audio /= audio_max audio /= audio_max
times = [0, 0, 0] times = [0, 0, 0]
if self.hubert_model is None: if self.hubert_model is None:
self.hubert_model = load_hubert(self.config) self.hubert_model = load_hubert(self.config)
file_index = ( file_index = (
( (
file_index.strip(" ") file_index.strip(" ")
@ -107,7 +120,7 @@ class VC:
if file_index != "" if file_index != ""
else file_index2 else file_index2
) # 防止小白写错,自动帮他替换掉 ) # 防止小白写错,自动帮他替换掉
audio_opt = Pipeline.pipeline( audio_opt = Pipeline.pipeline(
self.hubert_model, self.hubert_model,
self.net_g, self.net_g,
@ -135,28 +148,32 @@ class VC:
if os.path.exists(file_index) if os.path.exists(file_index)
else "Index not used." 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) 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: except:
info = traceback.format_exc() info = traceback.format_exc()
print(info) print(info)
return info, (None, None) return info, (None, None)
def vc_multi( def vc_multi(
self, self,
sid, sid,
dir_path, dir_path,
opt_root, opt_root,
paths, paths,
f0_up_key, f0_up_key,
f0_method, f0_method,
file_index, file_index,
file_index2, file_index2,
index_rate, index_rate,
filter_radius, filter_radius,
resample_sr, resample_sr,
rms_mix_rate, rms_mix_rate,
protect, protect,
format1): format1,
):
try: try:
dir_path = ( dir_path = (
dir_path.strip(" ").strip('"').strip("\n").strip('"').strip(" ") dir_path.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
@ -165,7 +182,9 @@ class VC:
os.makedirs(opt_root, exist_ok=True) os.makedirs(opt_root, exist_ok=True)
try: try:
if dir_path != "": if dir_path != "":
paths = [os.path.join(dir_path, name) for name in os.listdir(dir_path)] paths = [
os.path.join(dir_path, name) for name in os.listdir(dir_path)
]
else: else:
paths = [path.name for path in paths] paths = [path.name for path in paths]
except: except:
@ -193,7 +212,8 @@ class VC:
tgt_sr, audio_opt = opt tgt_sr, audio_opt = opt
if format1 in ["wav", "flac"]: if format1 in ["wav", "flac"]:
sf.write( sf.write(
"%s/%s.%s" % (opt_root, os.path.basename(path), format1), "%s/%s.%s"
% (opt_root, os.path.basename(path), format1),
audio_opt, audio_opt,
tgt_sr, tgt_sr,
) )
@ -216,4 +236,3 @@ class VC:
yield "\n".join(infos) yield "\n".join(infos)
except: except:
yield traceback.format_exc() yield traceback.format_exc()

View File

@ -11,7 +11,7 @@ from functools import lru_cache
now_dir = os.getcwd() now_dir = os.getcwd()
sys.path.append(now_dir) sys.path.append(now_dir)
bh, ah = signal.butter(N=5, Wn=48, btype="high", fs=16000) bh, ah = signal.butter(N=5, Wn=48, btype="high", fs=16000)
input_audio_path2wav = {} input_audio_path2wav = {}
@ -71,18 +71,18 @@ class Pipeline(object):
self.t_center = self.sr * self.x_center # 查询切点位置 self.t_center = self.sr * self.x_center # 查询切点位置
self.t_max = self.sr * self.x_max # 免查询时长阈值 self.t_max = self.sr * self.x_max # 免查询时长阈值
self.device = config.device self.device = config.device
self.model_rmvpe = None self.model_rmvpe = None
def get_f0( def get_f0(
self, self,
input_audio_path, input_audio_path,
x, x,
p_len, p_len,
f0_up_key, f0_up_key,
f0_method, f0_method,
filter_radius, filter_radius,
inp_f0=None, inp_f0=None,
): ):
global input_audio_path2wav global input_audio_path2wav
time_step = self.window / self.sr * 1000 time_step = self.window / self.sr * 1000
@ -141,12 +141,12 @@ class Pipeline(object):
"rmvpe.pt", is_half=self.is_half, device=self.device "rmvpe.pt", is_half=self.is_half, device=self.device
) )
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03) f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
if "privateuseone" in str(self.device): # clean ortruntime memory if "privateuseone" in str(self.device): # clean ortruntime memory
del self.model_rmvpe.model del self.model_rmvpe.model
del self.model_rmvpe del self.model_rmvpe
print("cleaning ortruntime memory") print("cleaning ortruntime memory")
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点数
@ -157,8 +157,8 @@ class Pipeline(object):
replace_f0 = np.interp( replace_f0 = np.interp(
list(range(delta_t)), inp_f0[:, 0] * 100, inp_f0[:, 1] 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] 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[ f0[self.x_pad * tf0 : self.x_pad * tf0 + len(replace_f0)] = replace_f0[
:shape :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()]))
@ -173,19 +173,19 @@ class Pipeline(object):
return f0_coarse, f0bak # 1-0 return f0_coarse, f0bak # 1-0
def vc( def vc(
self, self,
model, model,
net_g, net_g,
sid, sid,
audio0, audio0,
pitch, pitch,
pitchf, pitchf,
times, times,
index, index,
big_npy, big_npy,
index_rate, index_rate,
version, version,
protect, protect,
): # ,file_index,file_big_npy ): # ,file_index,file_big_npy
feats = torch.from_numpy(audio0) feats = torch.from_numpy(audio0)
if self.is_half: if self.is_half:
@ -275,26 +275,26 @@ class Pipeline(object):
return audio1 return audio1
def pipeline( def pipeline(
self, self,
model, model,
net_g, net_g,
sid, sid,
audio, audio,
input_audio_path, input_audio_path,
times, times,
f0_up_key, f0_up_key,
f0_method, f0_method,
file_index, file_index,
# file_big_npy, # file_big_npy,
index_rate, index_rate,
if_f0, if_f0,
filter_radius, filter_radius,
tgt_sr, tgt_sr,
resample_sr, resample_sr,
rms_mix_rate, rms_mix_rate,
version, version,
protect, protect,
f0_file=None, f0_file=None,
): ):
print(file_index) print(file_index)
if ( if (
@ -319,14 +319,14 @@ class Pipeline(object):
if audio_pad.shape[0] > self.t_max: if audio_pad.shape[0] > self.t_max:
audio_sum = np.zeros_like(audio) audio_sum = np.zeros_like(audio)
for i in range(self.window): for i in range(self.window):
audio_sum += audio_pad[i: i - self.window] audio_sum += audio_pad[i : i - self.window]
for t in range(self.t_center, audio.shape[0], self.t_center): for t in range(self.t_center, audio.shape[0], self.t_center):
opt_ts.append( opt_ts.append(
t t
- self.t_query - self.t_query
+ np.where( + 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])
== np.abs(audio_sum[t - self.t_query: t + self.t_query]).min() == np.abs(audio_sum[t - self.t_query : t + self.t_query]).min()
)[0][0] )[0][0]
) )
s = 0 s = 0
@ -374,16 +374,16 @@ class Pipeline(object):
model, model,
net_g, net_g,
sid, sid,
audio_pad[s: t + self.t_pad2 + self.window], audio_pad[s : t + self.t_pad2 + self.window],
pitch[:, s // self.window: (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], pitchf[:, s // self.window : (t + self.t_pad2) // self.window],
times, times,
index, index,
big_npy, big_npy,
index_rate, index_rate,
version, version,
protect, protect,
)[self.t_pad_tgt: -self.t_pad_tgt] )[self.t_pad_tgt : -self.t_pad_tgt]
) )
else: else:
audio_opt.append( audio_opt.append(
@ -391,7 +391,7 @@ class Pipeline(object):
model, model,
net_g, net_g,
sid, sid,
audio_pad[s: t + self.t_pad2 + self.window], audio_pad[s : t + self.t_pad2 + self.window],
None, None,
None, None,
times, times,
@ -400,7 +400,7 @@ class Pipeline(object):
index_rate, index_rate,
version, version,
protect, protect,
)[self.t_pad_tgt: -self.t_pad_tgt] )[self.t_pad_tgt : -self.t_pad_tgt]
) )
s = t s = t
if if_f0 == 1: if if_f0 == 1:
@ -410,15 +410,15 @@ class Pipeline(object):
net_g, net_g,
sid, sid,
audio_pad[t:], audio_pad[t:],
pitch[:, t // self.window:] if t is not None else pitch, pitch[:, t // self.window :] if t is not None else pitch,
pitchf[:, t // self.window:] if t is not None else pitchf, pitchf[:, t // self.window :] if t is not None else pitchf,
times, times,
index, index,
big_npy, big_npy,
index_rate, index_rate,
version, version,
protect, protect,
)[self.t_pad_tgt: -self.t_pad_tgt] )[self.t_pad_tgt : -self.t_pad_tgt]
) )
else: else:
audio_opt.append( audio_opt.append(
@ -435,7 +435,7 @@ class Pipeline(object):
index_rate, index_rate,
version, version,
protect, protect,
)[self.t_pad_tgt: -self.t_pad_tgt] )[self.t_pad_tgt : -self.t_pad_tgt]
) )
audio_opt = np.concatenate(audio_opt) audio_opt = np.concatenate(audio_opt)
if rms_mix_rate != 1: if rms_mix_rate != 1:
@ -453,4 +453,3 @@ class Pipeline(object):
if torch.cuda.is_available(): if torch.cuda.is_available():
torch.cuda.empty_cache() torch.cuda.empty_cache()
return audio_opt return audio_opt

View File

@ -6,7 +6,19 @@ from fairseq import checkpoint_utils
def get_index_path_from_model(sid): 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), "") 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): def load_hubert(config):
@ -21,8 +33,8 @@ def load_hubert(config):
else: else:
hubert_model = hubert_model.float() hubert_model = hubert_model.float()
return hubert_model.eval() return hubert_model.eval()
def load_audio(file, sr): def load_audio(file, sr):
try: try:
# https://github.com/openai/whisper/blob/main/whisper/audio.py#L26 # https://github.com/openai/whisper/blob/main/whisper/audio.py#L26
@ -39,4 +51,4 @@ def load_audio(file, sr):
except Exception as e: except Exception as e:
raise RuntimeError(f"Failed to load audio: {e}") raise RuntimeError(f"Failed to load audio: {e}")
return np.frombuffer(out, np.float32).flatten() return np.frombuffer(out, np.float32).flatten()