2023-06-18 12:38:35 +08:00
|
|
|
import os, sys, traceback, re
|
2023-05-21 19:19:53 +08:00
|
|
|
|
2023-06-08 16:53:51 +08:00
|
|
|
import json
|
|
|
|
|
2023-04-16 18:56:20 +08:00
|
|
|
now_dir = os.getcwd()
|
|
|
|
sys.path.append(now_dir)
|
2023-05-28 20:52:05 +08:00
|
|
|
from config import Config
|
2023-05-21 19:19:53 +08:00
|
|
|
|
2023-05-26 19:32:19 +08:00
|
|
|
Config = Config()
|
2023-04-11 00:11:14 +08:00
|
|
|
import PySimpleGUI as sg
|
|
|
|
import sounddevice as sd
|
2023-04-12 14:28:28 +08:00
|
|
|
import noisereduce as nr
|
2023-04-11 00:11:14 +08:00
|
|
|
import numpy as np
|
|
|
|
from fairseq import checkpoint_utils
|
2023-04-22 19:32:49 +08:00
|
|
|
import librosa, torch, pyworld, faiss, time, threading
|
2023-04-11 00:11:14 +08:00
|
|
|
import torch.nn.functional as F
|
2023-04-13 10:15:11 +08:00
|
|
|
import torchaudio.transforms as tat
|
2023-04-22 19:32:49 +08:00
|
|
|
import scipy.signal as signal
|
2023-04-13 10:15:11 +08:00
|
|
|
|
2023-06-08 16:53:51 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
# import matplotlib.pyplot as plt
|
2023-06-24 15:26:14 +08:00
|
|
|
from lib.infer_pack.models import (
|
2023-05-21 19:19:53 +08:00
|
|
|
SynthesizerTrnMs256NSFsid,
|
|
|
|
SynthesizerTrnMs256NSFsid_nono,
|
|
|
|
SynthesizerTrnMs768NSFsid,
|
|
|
|
SynthesizerTrnMs768NSFsid_nono,
|
|
|
|
)
|
2023-04-16 14:29:01 +08:00
|
|
|
from i18n import I18nAuto
|
2023-04-15 19:44:24 +08:00
|
|
|
|
2023-04-12 10:48:39 +08:00
|
|
|
i18n = I18nAuto()
|
2023-04-11 00:11:14 +08:00
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
2023-06-08 16:53:51 +08:00
|
|
|
current_dir = os.getcwd()
|
2023-04-11 00:11:14 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
|
2023-04-11 00:11:14 +08:00
|
|
|
class RVC:
|
2023-04-15 19:44:24 +08:00
|
|
|
def __init__(
|
|
|
|
self, key, hubert_path, pth_path, index_path, npy_path, index_rate
|
|
|
|
) -> None:
|
|
|
|
"""
|
2023-04-11 00:11:14 +08:00
|
|
|
初始化
|
2023-04-15 19:44:24 +08:00
|
|
|
"""
|
2023-04-22 19:32:49 +08:00
|
|
|
try:
|
|
|
|
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
|
|
|
|
if index_rate != 0:
|
|
|
|
self.index = faiss.read_index(index_path)
|
2023-05-04 22:22:46 +08:00
|
|
|
# self.big_npy = np.load(npy_path)
|
2023-05-08 01:40:09 +08:00
|
|
|
self.big_npy = self.index.reconstruct_n(0, self.index.ntotal)
|
2023-04-22 19:32:49 +08:00
|
|
|
print("index search enabled")
|
|
|
|
self.index_rate = index_rate
|
|
|
|
model_path = hubert_path
|
|
|
|
print("load model(s) from {}".format(model_path))
|
|
|
|
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(
|
|
|
|
[model_path],
|
|
|
|
suffix="",
|
|
|
|
)
|
|
|
|
self.model = models[0]
|
|
|
|
self.model = self.model.to(device)
|
2023-05-26 19:32:19 +08:00
|
|
|
if Config.is_half:
|
2023-05-21 14:57:16 +08:00
|
|
|
self.model = self.model.half()
|
|
|
|
else:
|
|
|
|
self.model = self.model.float()
|
2023-04-22 19:32:49 +08:00
|
|
|
self.model.eval()
|
|
|
|
cpt = torch.load(pth_path, map_location="cpu")
|
2023-04-27 18:52:01 +08:00
|
|
|
self.tgt_sr = cpt["config"][-1]
|
2023-04-22 19:32:49 +08:00
|
|
|
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
|
2023-04-22 19:38:00 +08:00
|
|
|
self.if_f0 = cpt.get("f0", 1)
|
2023-05-21 14:57:16 +08:00
|
|
|
self.version = cpt.get("version", "v1")
|
2023-05-25 08:27:40 +08:00
|
|
|
if self.version == "v1":
|
|
|
|
if self.if_f0 == 1:
|
2023-05-21 19:19:53 +08:00
|
|
|
self.net_g = SynthesizerTrnMs256NSFsid(
|
2023-05-26 19:32:19 +08:00
|
|
|
*cpt["config"], is_half=Config.is_half
|
2023-05-21 19:19:53 +08:00
|
|
|
)
|
2023-05-21 14:57:16 +08:00
|
|
|
else:
|
2023-05-21 15:01:34 +08:00
|
|
|
self.net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
|
2023-05-25 08:27:40 +08:00
|
|
|
elif self.version == "v2":
|
|
|
|
if self.if_f0 == 1:
|
2023-05-21 19:19:53 +08:00
|
|
|
self.net_g = SynthesizerTrnMs768NSFsid(
|
2023-05-26 19:32:19 +08:00
|
|
|
*cpt["config"], is_half=Config.is_half
|
2023-05-21 19:19:53 +08:00
|
|
|
)
|
2023-05-21 14:57:16 +08:00
|
|
|
else:
|
2023-05-21 15:01:34 +08:00
|
|
|
self.net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
|
2023-04-22 19:32:49 +08:00
|
|
|
del self.net_g.enc_q
|
|
|
|
print(self.net_g.load_state_dict(cpt["weight"], strict=False))
|
|
|
|
self.net_g.eval().to(device)
|
2023-05-26 19:32:19 +08:00
|
|
|
if Config.is_half:
|
2023-05-21 19:19:53 +08:00
|
|
|
self.net_g = self.net_g.half()
|
2023-05-21 14:57:16 +08:00
|
|
|
else:
|
2023-05-21 19:19:53 +08:00
|
|
|
self.net_g = self.net_g.float()
|
2023-05-08 01:40:09 +08:00
|
|
|
except:
|
|
|
|
print(traceback.format_exc())
|
2023-04-11 00:11:14 +08:00
|
|
|
|
2023-04-22 19:32:49 +08:00
|
|
|
def get_f0(self, x, f0_up_key, inp_f0=None):
|
2023-04-24 20:35:56 +08:00
|
|
|
x_pad = 1
|
2023-04-22 19:32:49 +08:00
|
|
|
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)
|
|
|
|
f0, t = pyworld.harvest(
|
|
|
|
x.astype(np.double),
|
|
|
|
fs=self.sr,
|
|
|
|
f0_ceil=f0_max,
|
|
|
|
f0_floor=f0_min,
|
|
|
|
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()]))
|
|
|
|
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()
|
2023-04-11 00:11:14 +08:00
|
|
|
f0_mel = 1127 * np.log(1 + f0 / 700)
|
2023-04-22 19:32:49 +08:00
|
|
|
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * 254 / (
|
|
|
|
f0_mel_max - f0_mel_min
|
2023-04-15 19:44:24 +08:00
|
|
|
) + 1
|
2023-04-11 00:11:14 +08:00
|
|
|
f0_mel[f0_mel <= 1] = 1
|
|
|
|
f0_mel[f0_mel > 255] = 255
|
2023-08-10 10:28:30 +08:00
|
|
|
f0_coarse = np.rint(f0_mel).astype(np.int32)
|
2023-04-22 19:32:49 +08:00
|
|
|
return f0_coarse, f0bak # 1-0
|
2023-04-11 00:11:14 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
def infer(self, feats: torch.Tensor) -> np.ndarray:
|
|
|
|
"""
|
2023-04-14 21:00:31 +08:00
|
|
|
推理函数
|
2023-04-15 19:44:24 +08:00
|
|
|
"""
|
|
|
|
audio = feats.clone().cpu().numpy()
|
2023-04-11 00:11:14 +08:00
|
|
|
assert feats.dim() == 1, feats.dim()
|
|
|
|
feats = feats.view(1, -1)
|
|
|
|
padding_mask = torch.BoolTensor(feats.shape).fill_(False)
|
2023-05-30 21:17:10 +08:00
|
|
|
if Config.is_half:
|
|
|
|
feats = feats.half()
|
|
|
|
else:
|
|
|
|
feats = feats.float()
|
2023-04-11 00:11:14 +08:00
|
|
|
inputs = {
|
2023-05-30 21:17:10 +08:00
|
|
|
"source": feats.to(device),
|
2023-04-11 00:11:14 +08:00
|
|
|
"padding_mask": padding_mask.to(device),
|
2023-05-21 14:57:16 +08:00
|
|
|
"output_layer": 9 if self.version == "v1" else 12,
|
2023-04-11 00:11:14 +08:00
|
|
|
}
|
|
|
|
torch.cuda.synchronize()
|
|
|
|
with torch.no_grad():
|
|
|
|
logits = self.model.extract_features(**inputs)
|
2023-05-27 23:36:12 +08:00
|
|
|
feats = (
|
|
|
|
self.model.final_proj(logits[0]) if self.version == "v1" else logits[0]
|
|
|
|
)
|
2023-04-11 00:11:14 +08:00
|
|
|
|
|
|
|
####索引优化
|
2023-05-21 14:57:16 +08:00
|
|
|
try:
|
2023-05-21 19:19:53 +08:00
|
|
|
if (
|
|
|
|
hasattr(self, "index")
|
|
|
|
and hasattr(self, "big_npy")
|
|
|
|
and self.index_rate != 0
|
|
|
|
):
|
2023-05-21 14:57:16 +08:00
|
|
|
npy = feats[0].cpu().numpy().astype("float32")
|
|
|
|
score, ix = self.index.search(npy, k=8)
|
|
|
|
weight = np.square(1 / score)
|
|
|
|
weight /= weight.sum(axis=1, keepdims=True)
|
2023-05-21 19:19:53 +08:00
|
|
|
npy = np.sum(self.big_npy[ix] * np.expand_dims(weight, axis=2), axis=1)
|
2023-05-26 19:32:19 +08:00
|
|
|
if Config.is_half:
|
2023-05-21 19:19:53 +08:00
|
|
|
npy = npy.astype("float16")
|
2023-05-21 14:57:16 +08:00
|
|
|
feats = (
|
|
|
|
torch.from_numpy(npy).unsqueeze(0).to(device) * self.index_rate
|
|
|
|
+ (1 - self.index_rate) * feats
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
print("index search FAIL or disabled")
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
print("index search FAIL")
|
2023-04-15 19:44:24 +08:00
|
|
|
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
2023-04-11 00:11:14 +08:00
|
|
|
torch.cuda.synchronize()
|
2023-04-14 21:00:31 +08:00
|
|
|
print(feats.shape)
|
2023-04-24 20:35:56 +08:00
|
|
|
if self.if_f0 == 1:
|
2023-04-22 19:38:00 +08:00
|
|
|
pitch, pitchf = self.get_f0(audio, self.f0_up_key)
|
|
|
|
p_len = min(feats.shape[1], 13000, pitch.shape[0]) # 太大了爆显存
|
|
|
|
else:
|
|
|
|
pitch, pitchf = None, None
|
|
|
|
p_len = min(feats.shape[1], 13000) # 太大了爆显存
|
2023-04-11 00:11:14 +08:00
|
|
|
torch.cuda.synchronize()
|
|
|
|
# print(feats.shape,pitch.shape)
|
2023-04-15 19:44:24 +08:00
|
|
|
feats = feats[:, :p_len, :]
|
2023-04-24 20:35:56 +08:00
|
|
|
if self.if_f0 == 1:
|
2023-04-22 19:38:00 +08:00
|
|
|
pitch = pitch[:p_len]
|
|
|
|
pitchf = pitchf[:p_len]
|
|
|
|
pitch = torch.LongTensor(pitch).unsqueeze(0).to(device)
|
|
|
|
pitchf = torch.FloatTensor(pitchf).unsqueeze(0).to(device)
|
2023-04-11 00:11:14 +08:00
|
|
|
p_len = torch.LongTensor([p_len]).to(device)
|
2023-04-15 19:44:24 +08:00
|
|
|
ii = 0 # sid
|
|
|
|
sid = torch.LongTensor([ii]).to(device)
|
2023-04-11 00:11:14 +08:00
|
|
|
with torch.no_grad():
|
2023-04-24 20:35:56 +08:00
|
|
|
if self.if_f0 == 1:
|
2023-04-22 19:38:00 +08:00
|
|
|
infered_audio = (
|
|
|
|
self.net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0]
|
|
|
|
.data.cpu()
|
|
|
|
.float()
|
|
|
|
)
|
|
|
|
else:
|
2023-04-24 20:35:56 +08:00
|
|
|
infered_audio = (
|
|
|
|
self.net_g.infer(feats, p_len, sid)[0][0, 0].data.cpu().float()
|
2023-04-22 19:38:00 +08:00
|
|
|
)
|
2023-04-11 00:11:14 +08:00
|
|
|
torch.cuda.synchronize()
|
2023-04-14 21:00:31 +08:00
|
|
|
return infered_audio
|
2023-04-11 00:11:14 +08:00
|
|
|
|
|
|
|
|
2023-05-26 19:32:19 +08:00
|
|
|
class GUIConfig:
|
2023-04-11 00:11:14 +08:00
|
|
|
def __init__(self) -> None:
|
2023-04-15 19:44:24 +08:00
|
|
|
self.hubert_path: str = ""
|
|
|
|
self.pth_path: str = ""
|
|
|
|
self.index_path: str = ""
|
|
|
|
self.npy_path: str = ""
|
|
|
|
self.pitch: int = 12
|
|
|
|
self.samplerate: int = 44100
|
|
|
|
self.block_time: float = 1.0 # s
|
|
|
|
self.buffer_num: int = 1
|
|
|
|
self.threhold: int = -30
|
|
|
|
self.crossfade_time: float = 0.08
|
|
|
|
self.extra_time: float = 0.04
|
|
|
|
self.I_noise_reduce = False
|
|
|
|
self.O_noise_reduce = False
|
|
|
|
self.index_rate = 0.3
|
|
|
|
|
2023-04-11 00:11:14 +08:00
|
|
|
|
|
|
|
class GUI:
|
|
|
|
def __init__(self) -> None:
|
2023-05-26 19:32:19 +08:00
|
|
|
self.config = GUIConfig()
|
2023-04-15 19:44:24 +08:00
|
|
|
self.flag_vc = False
|
|
|
|
|
2023-04-11 00:11:14 +08:00
|
|
|
self.launcher()
|
2023-04-15 19:44:24 +08:00
|
|
|
|
2023-06-08 16:53:51 +08:00
|
|
|
def load(self):
|
2023-06-28 13:54:31 +08:00
|
|
|
(
|
|
|
|
input_devices,
|
|
|
|
output_devices,
|
|
|
|
input_devices_indices,
|
|
|
|
output_devices_indices,
|
|
|
|
) = self.get_devices()
|
2023-06-08 16:53:51 +08:00
|
|
|
try:
|
2023-06-08 16:55:18 +08:00
|
|
|
with open("values1.json", "r") as j:
|
2023-06-08 16:53:51 +08:00
|
|
|
data = json.load(j)
|
|
|
|
except:
|
2023-06-08 16:55:18 +08:00
|
|
|
with open("values1.json", "w") as j:
|
2023-06-08 16:53:51 +08:00
|
|
|
data = {
|
2023-06-28 13:53:52 +08:00
|
|
|
"pth_path": "",
|
|
|
|
"index_path": "",
|
2023-06-28 13:54:31 +08:00
|
|
|
"sg_input_device": input_devices[
|
|
|
|
input_devices_indices.index(sd.default.device[0])
|
|
|
|
],
|
|
|
|
"sg_output_device": output_devices[
|
|
|
|
output_devices_indices.index(sd.default.device[1])
|
|
|
|
],
|
2023-06-08 21:29:34 +08:00
|
|
|
"threhold": "-45",
|
|
|
|
"pitch": "0",
|
|
|
|
"index_rate": "0",
|
2023-06-08 16:55:18 +08:00
|
|
|
"block_time": "1",
|
2023-06-08 21:29:34 +08:00
|
|
|
"crossfade_length": "0.04",
|
|
|
|
"extra_time": "1",
|
2023-06-08 16:55:18 +08:00
|
|
|
}
|
2023-06-08 16:53:51 +08:00
|
|
|
return data
|
2023-06-08 16:55:18 +08:00
|
|
|
|
2023-04-11 00:11:14 +08:00
|
|
|
def launcher(self):
|
2023-06-08 16:53:51 +08:00
|
|
|
data = self.load()
|
2023-04-15 19:44:24 +08:00
|
|
|
sg.theme("LightBlue3")
|
|
|
|
input_devices, output_devices, _, _ = self.get_devices()
|
|
|
|
layout = [
|
|
|
|
[
|
|
|
|
sg.Frame(
|
|
|
|
title=i18n("加载模型"),
|
|
|
|
layout=[
|
|
|
|
[
|
2023-06-08 16:55:18 +08:00
|
|
|
sg.Input(
|
|
|
|
default_text="hubert_base.pt",
|
|
|
|
key="hubert_path",
|
|
|
|
disabled=True,
|
|
|
|
),
|
|
|
|
sg.FileBrowse(
|
|
|
|
i18n("Hubert模型"),
|
|
|
|
initial_folder=os.path.join(os.getcwd()),
|
2023-06-28 13:53:52 +08:00
|
|
|
file_types=(("pt files", "*.pt"),),
|
2023-06-08 16:55:18 +08:00
|
|
|
),
|
2023-04-15 19:44:24 +08:00
|
|
|
],
|
|
|
|
[
|
2023-06-08 16:55:18 +08:00
|
|
|
sg.Input(
|
|
|
|
default_text=data.get("pth_path", ""),
|
|
|
|
key="pth_path",
|
|
|
|
),
|
|
|
|
sg.FileBrowse(
|
|
|
|
i18n("选择.pth文件"),
|
|
|
|
initial_folder=os.path.join(os.getcwd(), "weights"),
|
2023-06-28 13:53:52 +08:00
|
|
|
file_types=(("weight files", "*.pth"),),
|
2023-06-08 16:55:18 +08:00
|
|
|
),
|
2023-04-15 19:44:24 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
sg.Input(
|
2023-06-08 16:55:18 +08:00
|
|
|
default_text=data.get("index_path", ""),
|
2023-04-15 19:44:24 +08:00
|
|
|
key="index_path",
|
|
|
|
),
|
2023-06-08 16:55:18 +08:00
|
|
|
sg.FileBrowse(
|
|
|
|
i18n("选择.index文件"),
|
|
|
|
initial_folder=os.path.join(os.getcwd(), "logs"),
|
2023-06-28 13:53:52 +08:00
|
|
|
file_types=(("index files", "*.index"),),
|
2023-06-08 16:55:18 +08:00
|
|
|
),
|
2023-04-15 19:44:24 +08:00
|
|
|
],
|
|
|
|
[
|
|
|
|
sg.Input(
|
2023-05-04 22:22:46 +08:00
|
|
|
default_text="你不需要填写这个You don't need write this.",
|
2023-04-15 19:44:24 +08:00
|
|
|
key="npy_path",
|
2023-06-08 16:55:18 +08:00
|
|
|
disabled=True,
|
|
|
|
),
|
|
|
|
sg.FileBrowse(
|
|
|
|
i18n("选择.npy文件"),
|
|
|
|
initial_folder=os.path.join(os.getcwd(), "logs"),
|
2023-06-28 13:53:52 +08:00
|
|
|
file_types=(("feature files", "*.npy"),),
|
2023-04-15 19:44:24 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
],
|
2023-04-11 00:11:14 +08:00
|
|
|
[
|
2023-04-15 19:44:24 +08:00
|
|
|
sg.Frame(
|
|
|
|
layout=[
|
|
|
|
[
|
|
|
|
sg.Text(i18n("输入设备")),
|
|
|
|
sg.Combo(
|
|
|
|
input_devices,
|
|
|
|
key="sg_input_device",
|
2023-06-08 16:55:18 +08:00
|
|
|
default_value=data.get("sg_input_device", ""),
|
2023-04-15 19:44:24 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
sg.Text(i18n("输出设备")),
|
|
|
|
sg.Combo(
|
|
|
|
output_devices,
|
|
|
|
key="sg_output_device",
|
2023-06-08 16:55:18 +08:00
|
|
|
default_value=data.get("sg_output_device", ""),
|
2023-04-15 19:44:24 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
title=i18n("音频设备(请使用同种类驱动)"),
|
|
|
|
)
|
2023-04-11 00:11:14 +08:00
|
|
|
],
|
|
|
|
[
|
2023-04-15 19:44:24 +08:00
|
|
|
sg.Frame(
|
|
|
|
layout=[
|
|
|
|
[
|
|
|
|
sg.Text(i18n("响应阈值")),
|
|
|
|
sg.Slider(
|
|
|
|
range=(-60, 0),
|
|
|
|
key="threhold",
|
|
|
|
resolution=1,
|
|
|
|
orientation="h",
|
2023-06-08 16:55:18 +08:00
|
|
|
default_value=data.get("threhold", ""),
|
2023-04-15 19:44:24 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
sg.Text(i18n("音调设置")),
|
|
|
|
sg.Slider(
|
|
|
|
range=(-24, 24),
|
|
|
|
key="pitch",
|
|
|
|
resolution=1,
|
|
|
|
orientation="h",
|
2023-06-08 16:55:18 +08:00
|
|
|
default_value=data.get("pitch", ""),
|
2023-04-15 19:44:24 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
sg.Text(i18n("Index Rate")),
|
|
|
|
sg.Slider(
|
|
|
|
range=(0.0, 1.0),
|
|
|
|
key="index_rate",
|
|
|
|
resolution=0.01,
|
|
|
|
orientation="h",
|
2023-06-08 16:55:18 +08:00
|
|
|
default_value=data.get("index_rate", ""),
|
2023-04-15 19:44:24 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
title=i18n("常规设置"),
|
|
|
|
),
|
|
|
|
sg.Frame(
|
|
|
|
layout=[
|
|
|
|
[
|
|
|
|
sg.Text(i18n("采样长度")),
|
|
|
|
sg.Slider(
|
|
|
|
range=(0.1, 3.0),
|
|
|
|
key="block_time",
|
|
|
|
resolution=0.1,
|
|
|
|
orientation="h",
|
2023-06-08 16:55:18 +08:00
|
|
|
default_value=data.get("block_time", ""),
|
2023-04-15 19:44:24 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
sg.Text(i18n("淡入淡出长度")),
|
|
|
|
sg.Slider(
|
|
|
|
range=(0.01, 0.15),
|
|
|
|
key="crossfade_length",
|
|
|
|
resolution=0.01,
|
|
|
|
orientation="h",
|
2023-06-08 16:55:18 +08:00
|
|
|
default_value=data.get("crossfade_length", ""),
|
2023-04-15 19:44:24 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
sg.Text(i18n("额外推理时长")),
|
|
|
|
sg.Slider(
|
|
|
|
range=(0.05, 3.00),
|
|
|
|
key="extra_time",
|
|
|
|
resolution=0.01,
|
|
|
|
orientation="h",
|
2023-06-08 16:55:18 +08:00
|
|
|
default_value=data.get("extra_time", ""),
|
2023-04-15 19:44:24 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
sg.Checkbox(i18n("输入降噪"), key="I_noise_reduce"),
|
|
|
|
sg.Checkbox(i18n("输出降噪"), key="O_noise_reduce"),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
title=i18n("性能设置"),
|
|
|
|
),
|
2023-04-11 00:11:14 +08:00
|
|
|
],
|
|
|
|
[
|
2023-04-15 19:44:24 +08:00
|
|
|
sg.Button(i18n("开始音频转换"), key="start_vc"),
|
|
|
|
sg.Button(i18n("停止音频转换"), key="stop_vc"),
|
|
|
|
sg.Text(i18n("推理时间(ms):")),
|
|
|
|
sg.Text("0", key="infer_time"),
|
2023-04-11 00:11:14 +08:00
|
|
|
],
|
|
|
|
]
|
2023-04-15 19:44:24 +08:00
|
|
|
self.window = sg.Window("RVC - GUI", layout=layout)
|
2023-04-11 00:11:14 +08:00
|
|
|
self.event_handler()
|
2023-04-15 19:44:24 +08:00
|
|
|
|
2023-04-11 00:11:14 +08:00
|
|
|
def event_handler(self):
|
|
|
|
while True:
|
|
|
|
event, values = self.window.read()
|
2023-04-15 19:44:24 +08:00
|
|
|
if event == sg.WINDOW_CLOSED:
|
|
|
|
self.flag_vc = False
|
2023-04-11 00:11:14 +08:00
|
|
|
exit()
|
2023-04-15 19:44:24 +08:00
|
|
|
if event == "start_vc" and self.flag_vc == False:
|
2023-06-18 12:38:35 +08:00
|
|
|
if self.set_values(values) == True:
|
|
|
|
print("using_cuda:" + str(torch.cuda.is_available()))
|
|
|
|
self.start_vc()
|
|
|
|
settings = {
|
|
|
|
"pth_path": values["pth_path"],
|
|
|
|
"index_path": values["index_path"],
|
|
|
|
"sg_input_device": values["sg_input_device"],
|
|
|
|
"sg_output_device": values["sg_output_device"],
|
|
|
|
"threhold": values["threhold"],
|
|
|
|
"pitch": values["pitch"],
|
|
|
|
"index_rate": values["index_rate"],
|
|
|
|
"block_time": values["block_time"],
|
|
|
|
"crossfade_length": values["crossfade_length"],
|
|
|
|
"extra_time": values["extra_time"],
|
|
|
|
}
|
|
|
|
with open("values1.json", "w") as j:
|
|
|
|
json.dump(settings, j)
|
2023-04-15 19:44:24 +08:00
|
|
|
if event == "stop_vc" and self.flag_vc == True:
|
2023-04-11 00:11:14 +08:00
|
|
|
self.flag_vc = False
|
2023-06-08 16:55:18 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
def set_values(self, values):
|
2023-06-18 18:39:56 +08:00
|
|
|
if len(values["pth_path"].strip()) == 0:
|
|
|
|
sg.popup(i18n("请选择pth文件"))
|
2023-06-18 12:38:35 +08:00
|
|
|
return False
|
2023-06-18 18:39:56 +08:00
|
|
|
if len(values["index_path"].strip()) == 0:
|
|
|
|
sg.popup(i18n("请选择index文件"))
|
2023-06-18 12:38:35 +08:00
|
|
|
return False
|
|
|
|
pattern = re.compile("[^\x00-\x7F]+")
|
2023-06-18 18:39:56 +08:00
|
|
|
if pattern.findall(values["hubert_path"]):
|
|
|
|
sg.popup(i18n("hubert模型路径不可包含中文"))
|
2023-06-18 12:38:35 +08:00
|
|
|
return False
|
2023-06-18 18:39:56 +08:00
|
|
|
if pattern.findall(values["pth_path"]):
|
|
|
|
sg.popup(i18n("pth文件路径不可包含中文"))
|
2023-06-18 12:38:35 +08:00
|
|
|
return False
|
2023-06-18 18:39:56 +08:00
|
|
|
if pattern.findall(values["index_path"]):
|
|
|
|
sg.popup(i18n("index文件路径不可包含中文"))
|
2023-06-18 12:38:35 +08:00
|
|
|
return False
|
2023-04-15 19:44:24 +08:00
|
|
|
self.set_devices(values["sg_input_device"], values["sg_output_device"])
|
2023-06-08 16:55:18 +08:00
|
|
|
self.config.hubert_path = os.path.join(current_dir, "hubert_base.pt")
|
2023-04-15 19:44:24 +08:00
|
|
|
self.config.pth_path = values["pth_path"]
|
|
|
|
self.config.index_path = values["index_path"]
|
|
|
|
self.config.npy_path = values["npy_path"]
|
|
|
|
self.config.threhold = values["threhold"]
|
|
|
|
self.config.pitch = values["pitch"]
|
|
|
|
self.config.block_time = values["block_time"]
|
|
|
|
self.config.crossfade_time = values["crossfade_length"]
|
|
|
|
self.config.extra_time = values["extra_time"]
|
|
|
|
self.config.I_noise_reduce = values["I_noise_reduce"]
|
|
|
|
self.config.O_noise_reduce = values["O_noise_reduce"]
|
|
|
|
self.config.index_rate = values["index_rate"]
|
2023-06-18 12:38:35 +08:00
|
|
|
return True
|
2023-04-11 00:11:14 +08:00
|
|
|
|
|
|
|
def start_vc(self):
|
|
|
|
torch.cuda.empty_cache()
|
2023-04-15 19:44:24 +08:00
|
|
|
self.flag_vc = True
|
|
|
|
self.block_frame = int(self.config.block_time * self.config.samplerate)
|
|
|
|
self.crossfade_frame = int(self.config.crossfade_time * self.config.samplerate)
|
|
|
|
self.sola_search_frame = int(0.012 * self.config.samplerate)
|
2023-04-27 18:52:01 +08:00
|
|
|
self.delay_frame = int(0.01 * self.config.samplerate) # 往前预留0.02s
|
2023-04-28 11:25:20 +08:00
|
|
|
self.extra_frame = int(self.config.extra_time * self.config.samplerate)
|
2023-04-15 19:44:24 +08:00
|
|
|
self.rvc = None
|
|
|
|
self.rvc = RVC(
|
|
|
|
self.config.pitch,
|
|
|
|
self.config.hubert_path,
|
|
|
|
self.config.pth_path,
|
|
|
|
self.config.index_path,
|
|
|
|
self.config.npy_path,
|
2023-04-24 20:35:56 +08:00
|
|
|
self.config.index_rate,
|
2023-04-15 19:44:24 +08:00
|
|
|
)
|
|
|
|
self.input_wav: np.ndarray = np.zeros(
|
|
|
|
self.extra_frame
|
|
|
|
+ self.crossfade_frame
|
|
|
|
+ self.sola_search_frame
|
|
|
|
+ self.block_frame,
|
|
|
|
dtype="float32",
|
|
|
|
)
|
|
|
|
self.output_wav: torch.Tensor = torch.zeros(
|
|
|
|
self.block_frame, device=device, dtype=torch.float32
|
|
|
|
)
|
|
|
|
self.sola_buffer: torch.Tensor = torch.zeros(
|
|
|
|
self.crossfade_frame, device=device, dtype=torch.float32
|
|
|
|
)
|
|
|
|
self.fade_in_window: torch.Tensor = torch.linspace(
|
|
|
|
0.0, 1.0, steps=self.crossfade_frame, device=device, dtype=torch.float32
|
|
|
|
)
|
|
|
|
self.fade_out_window: torch.Tensor = 1 - self.fade_in_window
|
|
|
|
self.resampler1 = tat.Resample(
|
|
|
|
orig_freq=self.config.samplerate, new_freq=16000, dtype=torch.float32
|
|
|
|
)
|
|
|
|
self.resampler2 = tat.Resample(
|
2023-04-28 11:25:20 +08:00
|
|
|
orig_freq=self.rvc.tgt_sr,
|
|
|
|
new_freq=self.config.samplerate,
|
|
|
|
dtype=torch.float32,
|
2023-04-15 19:44:24 +08:00
|
|
|
)
|
|
|
|
thread_vc = threading.Thread(target=self.soundinput)
|
2023-04-11 00:11:14 +08:00
|
|
|
thread_vc.start()
|
|
|
|
|
|
|
|
def soundinput(self):
|
2023-04-15 19:44:24 +08:00
|
|
|
"""
|
2023-04-11 00:11:14 +08:00
|
|
|
接受音频输入
|
2023-04-15 19:44:24 +08:00
|
|
|
"""
|
|
|
|
with sd.Stream(
|
2023-06-28 13:53:52 +08:00
|
|
|
channels=2,
|
2023-04-15 19:44:24 +08:00
|
|
|
callback=self.audio_callback,
|
|
|
|
blocksize=self.block_frame,
|
|
|
|
samplerate=self.config.samplerate,
|
|
|
|
dtype="float32",
|
|
|
|
):
|
2023-04-11 00:11:14 +08:00
|
|
|
while self.flag_vc:
|
|
|
|
time.sleep(self.config.block_time)
|
2023-04-15 19:44:24 +08:00
|
|
|
print("Audio block passed.")
|
|
|
|
print("ENDing VC")
|
2023-04-11 00:11:14 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
def audio_callback(
|
|
|
|
self, indata: np.ndarray, outdata: np.ndarray, frames, times, status
|
|
|
|
):
|
|
|
|
"""
|
2023-04-11 00:11:14 +08:00
|
|
|
音频处理
|
2023-04-15 19:44:24 +08:00
|
|
|
"""
|
|
|
|
start_time = time.perf_counter()
|
|
|
|
indata = librosa.to_mono(indata.T)
|
2023-04-13 10:15:11 +08:00
|
|
|
if self.config.I_noise_reduce:
|
2023-04-15 19:44:24 +08:00
|
|
|
indata[:] = nr.reduce_noise(y=indata, sr=self.config.samplerate)
|
|
|
|
|
|
|
|
"""noise gate"""
|
|
|
|
frame_length = 2048
|
|
|
|
hop_length = 1024
|
|
|
|
rms = librosa.feature.rms(
|
|
|
|
y=indata, frame_length=frame_length, hop_length=hop_length
|
|
|
|
)
|
|
|
|
db_threhold = librosa.amplitude_to_db(rms, ref=1.0)[0] < self.config.threhold
|
|
|
|
# print(rms.shape,db.shape,db)
|
2023-04-11 00:11:14 +08:00
|
|
|
for i in range(db_threhold.shape[0]):
|
|
|
|
if db_threhold[i]:
|
2023-04-15 19:44:24 +08:00
|
|
|
indata[i * hop_length : (i + 1) * hop_length] = 0
|
|
|
|
self.input_wav[:] = np.append(self.input_wav[self.block_frame :], indata)
|
|
|
|
|
|
|
|
# infer
|
|
|
|
print("input_wav:" + str(self.input_wav.shape))
|
|
|
|
# print('infered_wav:'+str(infer_wav.shape))
|
|
|
|
infer_wav: torch.Tensor = self.resampler2(
|
|
|
|
self.rvc.infer(self.resampler1(torch.from_numpy(self.input_wav)))
|
|
|
|
)[-self.crossfade_frame - self.sola_search_frame - self.block_frame :].to(
|
|
|
|
device
|
|
|
|
)
|
|
|
|
print("infer_wav:" + str(infer_wav.shape))
|
|
|
|
|
2023-04-11 00:11:14 +08:00
|
|
|
# SOLA algorithm from https://github.com/yxlllc/DDSP-SVC
|
2023-04-15 19:44:24 +08:00
|
|
|
cor_nom = F.conv1d(
|
|
|
|
infer_wav[None, None, : self.crossfade_frame + self.sola_search_frame],
|
|
|
|
self.sola_buffer[None, None, :],
|
|
|
|
)
|
|
|
|
cor_den = torch.sqrt(
|
|
|
|
F.conv1d(
|
|
|
|
infer_wav[None, None, : self.crossfade_frame + self.sola_search_frame]
|
|
|
|
** 2,
|
|
|
|
torch.ones(1, 1, self.crossfade_frame, device=device),
|
|
|
|
)
|
|
|
|
+ 1e-8
|
|
|
|
)
|
|
|
|
sola_offset = torch.argmax(cor_nom[0, 0] / cor_den[0, 0])
|
|
|
|
print("sola offset: " + str(int(sola_offset)))
|
|
|
|
|
2023-04-11 00:11:14 +08:00
|
|
|
# crossfade
|
2023-04-15 19:44:24 +08:00
|
|
|
self.output_wav[:] = infer_wav[sola_offset : sola_offset + self.block_frame]
|
|
|
|
self.output_wav[: self.crossfade_frame] *= self.fade_in_window
|
|
|
|
self.output_wav[: self.crossfade_frame] += self.sola_buffer[:]
|
2023-04-11 00:11:14 +08:00
|
|
|
if sola_offset < self.sola_search_frame:
|
2023-04-15 19:44:24 +08:00
|
|
|
self.sola_buffer[:] = (
|
|
|
|
infer_wav[
|
|
|
|
-self.sola_search_frame
|
|
|
|
- self.crossfade_frame
|
|
|
|
+ sola_offset : -self.sola_search_frame
|
|
|
|
+ sola_offset
|
|
|
|
]
|
|
|
|
* self.fade_out_window
|
|
|
|
)
|
2023-04-11 00:11:14 +08:00
|
|
|
else:
|
2023-04-15 19:44:24 +08:00
|
|
|
self.sola_buffer[:] = (
|
|
|
|
infer_wav[-self.crossfade_frame :] * self.fade_out_window
|
|
|
|
)
|
|
|
|
|
2023-04-13 10:15:11 +08:00
|
|
|
if self.config.O_noise_reduce:
|
2023-04-15 19:44:24 +08:00
|
|
|
outdata[:] = np.tile(
|
|
|
|
nr.reduce_noise(
|
|
|
|
y=self.output_wav[:].cpu().numpy(), sr=self.config.samplerate
|
|
|
|
),
|
|
|
|
(2, 1),
|
|
|
|
).T
|
2023-04-13 10:15:11 +08:00
|
|
|
else:
|
2023-04-15 19:44:24 +08:00
|
|
|
outdata[:] = self.output_wav[:].repeat(2, 1).t().cpu().numpy()
|
|
|
|
total_time = time.perf_counter() - start_time
|
|
|
|
self.window["infer_time"].update(int(total_time * 1000))
|
2023-04-22 19:32:49 +08:00
|
|
|
print("infer time:" + str(total_time))
|
2023-04-15 19:44:24 +08:00
|
|
|
|
|
|
|
def get_devices(self, update: bool = True):
|
|
|
|
"""获取设备列表"""
|
2023-04-11 00:11:14 +08:00
|
|
|
if update:
|
|
|
|
sd._terminate()
|
|
|
|
sd._initialize()
|
|
|
|
devices = sd.query_devices()
|
|
|
|
hostapis = sd.query_hostapis()
|
|
|
|
for hostapi in hostapis:
|
|
|
|
for device_idx in hostapi["devices"]:
|
|
|
|
devices[device_idx]["hostapi_name"] = hostapi["name"]
|
|
|
|
input_devices = [
|
|
|
|
f"{d['name']} ({d['hostapi_name']})"
|
|
|
|
for d in devices
|
|
|
|
if d["max_input_channels"] > 0
|
|
|
|
]
|
|
|
|
output_devices = [
|
|
|
|
f"{d['name']} ({d['hostapi_name']})"
|
|
|
|
for d in devices
|
|
|
|
if d["max_output_channels"] > 0
|
|
|
|
]
|
2023-04-15 19:44:24 +08:00
|
|
|
input_devices_indices = [
|
2023-06-06 22:35:35 +08:00
|
|
|
d["index"] if "index" in d else d["name"]
|
|
|
|
for d in devices
|
|
|
|
if d["max_input_channels"] > 0
|
2023-04-15 19:44:24 +08:00
|
|
|
]
|
2023-04-11 00:11:14 +08:00
|
|
|
output_devices_indices = [
|
2023-06-06 22:35:35 +08:00
|
|
|
d["index"] if "index" in d else d["name"]
|
|
|
|
for d in devices
|
|
|
|
if d["max_output_channels"] > 0
|
2023-04-11 00:11:14 +08:00
|
|
|
]
|
2023-04-15 19:44:24 +08:00
|
|
|
return (
|
|
|
|
input_devices,
|
|
|
|
output_devices,
|
|
|
|
input_devices_indices,
|
|
|
|
output_devices_indices,
|
|
|
|
)
|
|
|
|
|
|
|
|
def set_devices(self, input_device, output_device):
|
|
|
|
"""设置输出设备"""
|
|
|
|
(
|
|
|
|
input_devices,
|
|
|
|
output_devices,
|
|
|
|
input_device_indices,
|
|
|
|
output_device_indices,
|
|
|
|
) = self.get_devices()
|
|
|
|
sd.default.device[0] = input_device_indices[input_devices.index(input_device)]
|
|
|
|
sd.default.device[1] = output_device_indices[
|
|
|
|
output_devices.index(output_device)
|
|
|
|
]
|
|
|
|
print("input device:" + str(sd.default.device[0]) + ":" + str(input_device))
|
|
|
|
print("output device:" + str(sd.default.device[1]) + ":" + str(output_device))
|
|
|
|
|
2023-06-08 16:55:18 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
gui = GUI()
|