2023-08-28 09:08:31 +02:00
|
|
|
|
import os
|
|
|
|
|
import sys
|
2023-09-02 20:07:49 +02:00
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
load_dotenv()
|
2023-08-13 05:52:51 +02:00
|
|
|
|
|
2023-09-04 11:01:11 +02:00
|
|
|
|
os.environ["OMP_NUM_THREADS"] = "4"
|
2023-07-17 16:54:15 +02:00
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
|
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
|
|
|
|
|
2023-07-11 08:45:34 +02:00
|
|
|
|
now_dir = os.getcwd()
|
|
|
|
|
sys.path.append(now_dir)
|
|
|
|
|
import multiprocessing
|
2023-07-13 08:35:24 +02:00
|
|
|
|
|
2023-09-21 16:17:06 +02:00
|
|
|
|
stream_latency = -1
|
2023-07-13 08:35:24 +02:00
|
|
|
|
|
2023-09-21 16:18:12 +02:00
|
|
|
|
|
2023-10-06 11:14:33 +02:00
|
|
|
|
def printt(strr, *args):
|
|
|
|
|
if len(args) == 0:
|
|
|
|
|
print(strr)
|
|
|
|
|
else:
|
|
|
|
|
print(strr % args)
|
|
|
|
|
|
|
|
|
|
|
2023-07-11 08:45:34 +02:00
|
|
|
|
class Harvest(multiprocessing.Process):
|
2023-07-13 08:35:24 +02:00
|
|
|
|
def __init__(self, inp_q, opt_q):
|
2023-07-11 08:45:34 +02:00
|
|
|
|
multiprocessing.Process.__init__(self)
|
2023-07-13 08:35:24 +02:00
|
|
|
|
self.inp_q = inp_q
|
|
|
|
|
self.opt_q = opt_q
|
2023-07-11 08:45:34 +02:00
|
|
|
|
|
|
|
|
|
def run(self):
|
2023-08-28 09:08:31 +02:00
|
|
|
|
import numpy as np
|
|
|
|
|
import pyworld
|
2023-07-13 08:35:24 +02:00
|
|
|
|
|
|
|
|
|
while 1:
|
|
|
|
|
idx, x, res_f0, n_cpu, ts = self.inp_q.get()
|
|
|
|
|
f0, t = pyworld.harvest(
|
2023-07-11 08:45:34 +02:00
|
|
|
|
x.astype(np.double),
|
|
|
|
|
fs=16000,
|
|
|
|
|
f0_ceil=1100,
|
|
|
|
|
f0_floor=50,
|
|
|
|
|
frame_period=10,
|
|
|
|
|
)
|
2023-07-13 08:35:24 +02:00
|
|
|
|
res_f0[idx] = f0
|
|
|
|
|
if len(res_f0.keys()) >= n_cpu:
|
2023-07-11 08:45:34 +02:00
|
|
|
|
self.opt_q.put(ts)
|
|
|
|
|
|
2023-07-13 08:35:24 +02:00
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-08-28 09:08:31 +02:00
|
|
|
|
import json
|
|
|
|
|
import multiprocessing
|
|
|
|
|
import re
|
|
|
|
|
import threading
|
|
|
|
|
import time
|
|
|
|
|
import traceback
|
|
|
|
|
from multiprocessing import Queue, cpu_count
|
2023-07-11 08:45:34 +02:00
|
|
|
|
from queue import Empty
|
2023-08-28 09:08:31 +02:00
|
|
|
|
|
|
|
|
|
import librosa
|
2023-09-03 07:57:31 +02:00
|
|
|
|
from tools.torchgate import TorchGate
|
2023-07-11 08:45:34 +02:00
|
|
|
|
import numpy as np
|
|
|
|
|
import PySimpleGUI as sg
|
|
|
|
|
import sounddevice as sd
|
2023-08-28 09:08:31 +02:00
|
|
|
|
import torch
|
2023-07-11 08:45:34 +02:00
|
|
|
|
import torch.nn.functional as F
|
|
|
|
|
import torchaudio.transforms as tat
|
2023-08-28 09:08:31 +02:00
|
|
|
|
|
2023-08-28 16:57:32 +02:00
|
|
|
|
import tools.rvc_for_realtime as rvc_for_realtime
|
2023-08-29 08:47:17 +02:00
|
|
|
|
from i18n.i18n import I18nAuto
|
2023-10-06 11:14:33 +02:00
|
|
|
|
from configs.config import Config
|
2023-08-13 05:52:51 +02:00
|
|
|
|
|
2023-07-11 08:45:34 +02:00
|
|
|
|
i18n = I18nAuto()
|
2023-10-06 11:14:33 +02:00
|
|
|
|
|
|
|
|
|
# device = rvc_for_realtime.config.device
|
2023-08-12 19:05:58 +02:00
|
|
|
|
# device = torch.device(
|
|
|
|
|
# "cuda"
|
|
|
|
|
# if torch.cuda.is_available()
|
|
|
|
|
# else ("mps" if torch.backends.mps.is_available() else "cpu")
|
|
|
|
|
# )
|
2023-07-11 08:45:34 +02:00
|
|
|
|
current_dir = os.getcwd()
|
|
|
|
|
inp_q = Queue()
|
2023-07-13 08:35:24 +02:00
|
|
|
|
opt_q = Queue()
|
|
|
|
|
n_cpu = min(cpu_count(), 8)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
for _ in range(n_cpu):
|
2023-07-13 08:35:24 +02:00
|
|
|
|
Harvest(inp_q, opt_q).start()
|
2023-08-12 19:05:58 +02:00
|
|
|
|
|
2023-07-11 08:45:34 +02:00
|
|
|
|
class GUIConfig:
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
|
self.pth_path: str = ""
|
|
|
|
|
self.index_path: str = ""
|
2023-09-03 07:57:31 +02:00
|
|
|
|
self.pitch: int = 0
|
2023-07-11 08:45:34 +02:00
|
|
|
|
self.samplerate: int = 40000
|
|
|
|
|
self.block_time: float = 1.0 # s
|
|
|
|
|
self.buffer_num: int = 1
|
2023-09-03 07:57:31 +02:00
|
|
|
|
self.threhold: int = -60
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.crossfade_time: float = 0.05
|
|
|
|
|
self.extra_time: float = 2.5
|
2023-07-11 08:45:34 +02:00
|
|
|
|
self.I_noise_reduce = False
|
|
|
|
|
self.O_noise_reduce = False
|
2023-09-03 07:57:31 +02:00
|
|
|
|
self.rms_mix_rate = 0.0
|
2023-07-11 08:45:34 +02:00
|
|
|
|
self.index_rate = 0.3
|
2023-08-12 19:05:58 +02:00
|
|
|
|
self.n_cpu = min(n_cpu, 6)
|
2023-07-13 08:35:24 +02:00
|
|
|
|
self.f0method = "harvest"
|
2023-07-22 19:24:51 +02:00
|
|
|
|
self.sg_input_device = ""
|
|
|
|
|
self.sg_output_device = ""
|
2023-07-11 08:45:34 +02:00
|
|
|
|
|
|
|
|
|
class GUI:
|
|
|
|
|
def __init__(self) -> None:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config = GUIConfig()
|
|
|
|
|
self.config = Config()
|
2023-07-11 08:45:34 +02:00
|
|
|
|
self.flag_vc = False
|
2023-09-21 16:18:12 +02:00
|
|
|
|
self.function = "vc"
|
2023-09-21 16:17:06 +02:00
|
|
|
|
self.delay_time = 0
|
2023-07-11 08:45:34 +02:00
|
|
|
|
self.launcher()
|
|
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
|
input_devices, output_devices, _, _ = self.get_devices()
|
|
|
|
|
try:
|
2023-08-26 18:46:03 +02:00
|
|
|
|
with open("configs/config.json", "r") as j:
|
2023-07-11 08:45:34 +02:00
|
|
|
|
data = json.load(j)
|
2023-07-13 08:35:24 +02:00
|
|
|
|
data["pm"] = data["f0method"] == "pm"
|
|
|
|
|
data["harvest"] = data["f0method"] == "harvest"
|
|
|
|
|
data["crepe"] = data["f0method"] == "crepe"
|
|
|
|
|
data["rmvpe"] = data["f0method"] == "rmvpe"
|
2023-09-21 16:17:06 +02:00
|
|
|
|
if data["sg_input_device"] not in input_devices:
|
|
|
|
|
data["sg_input_device"] = input_devices[sd.default.device[0]]
|
|
|
|
|
if data["sg_output_device"] not in output_devices:
|
2023-09-21 16:18:12 +02:00
|
|
|
|
data["sg_output_device"] = output_devices[sd.default.device[1]]
|
2023-07-11 08:45:34 +02:00
|
|
|
|
except:
|
2023-08-26 18:46:03 +02:00
|
|
|
|
with open("configs/config.json", "w") as j:
|
2023-07-11 08:45:34 +02:00
|
|
|
|
data = {
|
|
|
|
|
"pth_path": " ",
|
|
|
|
|
"index_path": " ",
|
|
|
|
|
"sg_input_device": input_devices[sd.default.device[0]],
|
|
|
|
|
"sg_output_device": output_devices[sd.default.device[1]],
|
2023-09-03 07:57:31 +02:00
|
|
|
|
"threhold": "-60",
|
2023-07-11 08:45:34 +02:00
|
|
|
|
"pitch": "0",
|
|
|
|
|
"index_rate": "0",
|
2023-09-03 07:57:31 +02:00
|
|
|
|
"rms_mix_rate": "0",
|
|
|
|
|
"block_time": "0.25",
|
2023-10-06 11:14:33 +02:00
|
|
|
|
"crossfade_length": "0.05",
|
|
|
|
|
"extra_time": "2.5",
|
2023-07-11 10:27:18 +02:00
|
|
|
|
"f0method": "rmvpe",
|
2023-10-06 11:14:33 +02:00
|
|
|
|
"use_jit": False,
|
2023-07-11 08:45:34 +02:00
|
|
|
|
}
|
2023-09-03 07:57:31 +02:00
|
|
|
|
data["pm"] = data["f0method"] == "pm"
|
|
|
|
|
data["harvest"] = data["f0method"] == "harvest"
|
|
|
|
|
data["crepe"] = data["f0method"] == "crepe"
|
|
|
|
|
data["rmvpe"] = data["f0method"] == "rmvpe"
|
2023-07-11 08:45:34 +02:00
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
def launcher(self):
|
|
|
|
|
data = self.load()
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.config.use_jit = False # data.get("use_jit", self.config.use_jit)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
sg.theme("LightBlue3")
|
|
|
|
|
input_devices, output_devices, _, _ = self.get_devices()
|
|
|
|
|
layout = [
|
|
|
|
|
[
|
|
|
|
|
sg.Frame(
|
|
|
|
|
title=i18n("加载模型"),
|
|
|
|
|
layout=[
|
|
|
|
|
[
|
|
|
|
|
sg.Input(
|
|
|
|
|
default_text=data.get("pth_path", ""),
|
|
|
|
|
key="pth_path",
|
|
|
|
|
),
|
|
|
|
|
sg.FileBrowse(
|
|
|
|
|
i18n("选择.pth文件"),
|
2023-08-29 09:01:02 +02:00
|
|
|
|
initial_folder=os.path.join(
|
|
|
|
|
os.getcwd(), "assets/weights"
|
|
|
|
|
),
|
2023-07-11 08:45:34 +02:00
|
|
|
|
file_types=((". pth"),),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
sg.Input(
|
|
|
|
|
default_text=data.get("index_path", ""),
|
|
|
|
|
key="index_path",
|
|
|
|
|
),
|
|
|
|
|
sg.FileBrowse(
|
|
|
|
|
i18n("选择.index文件"),
|
|
|
|
|
initial_folder=os.path.join(os.getcwd(), "logs"),
|
|
|
|
|
file_types=((". index"),),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
sg.Frame(
|
|
|
|
|
layout=[
|
|
|
|
|
[
|
|
|
|
|
sg.Text(i18n("输入设备")),
|
|
|
|
|
sg.Combo(
|
|
|
|
|
input_devices,
|
|
|
|
|
key="sg_input_device",
|
|
|
|
|
default_value=data.get("sg_input_device", ""),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
sg.Text(i18n("输出设备")),
|
|
|
|
|
sg.Combo(
|
|
|
|
|
output_devices,
|
|
|
|
|
key="sg_output_device",
|
|
|
|
|
default_value=data.get("sg_output_device", ""),
|
|
|
|
|
),
|
|
|
|
|
],
|
2023-07-22 19:24:51 +02:00
|
|
|
|
[sg.Button(i18n("重载设备列表"), key="reload_devices")],
|
2023-07-11 08:45:34 +02:00
|
|
|
|
],
|
|
|
|
|
title=i18n("音频设备(请使用同种类驱动)"),
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
sg.Frame(
|
|
|
|
|
layout=[
|
|
|
|
|
[
|
|
|
|
|
sg.Text(i18n("响应阈值")),
|
|
|
|
|
sg.Slider(
|
|
|
|
|
range=(-60, 0),
|
|
|
|
|
key="threhold",
|
|
|
|
|
resolution=1,
|
|
|
|
|
orientation="h",
|
2023-09-03 07:57:31 +02:00
|
|
|
|
default_value=data.get("threhold", "-60"),
|
2023-09-01 04:16:49 +02:00
|
|
|
|
enable_events=True,
|
2023-07-11 08:45:34 +02:00
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
sg.Text(i18n("音调设置")),
|
|
|
|
|
sg.Slider(
|
|
|
|
|
range=(-24, 24),
|
|
|
|
|
key="pitch",
|
|
|
|
|
resolution=1,
|
|
|
|
|
orientation="h",
|
2023-09-03 07:57:31 +02:00
|
|
|
|
default_value=data.get("pitch", "0"),
|
2023-09-01 04:16:49 +02:00
|
|
|
|
enable_events=True,
|
2023-07-11 08:45:34 +02:00
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
sg.Text(i18n("Index Rate")),
|
|
|
|
|
sg.Slider(
|
|
|
|
|
range=(0.0, 1.0),
|
|
|
|
|
key="index_rate",
|
|
|
|
|
resolution=0.01,
|
|
|
|
|
orientation="h",
|
2023-09-03 07:57:31 +02:00
|
|
|
|
default_value=data.get("index_rate", "0"),
|
|
|
|
|
enable_events=True,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
sg.Text(i18n("响度因子")),
|
|
|
|
|
sg.Slider(
|
|
|
|
|
range=(0.0, 1.0),
|
|
|
|
|
key="rms_mix_rate",
|
|
|
|
|
resolution=0.01,
|
|
|
|
|
orientation="h",
|
|
|
|
|
default_value=data.get("rms_mix_rate", "0"),
|
2023-09-01 04:16:49 +02:00
|
|
|
|
enable_events=True,
|
2023-07-11 08:45:34 +02:00
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
sg.Text(i18n("音高算法")),
|
2023-07-13 08:35:24 +02:00
|
|
|
|
sg.Radio(
|
|
|
|
|
"pm",
|
|
|
|
|
"f0method",
|
|
|
|
|
key="pm",
|
|
|
|
|
default=data.get("pm", "") == True,
|
2023-09-01 04:16:49 +02:00
|
|
|
|
enable_events=True,
|
2023-07-13 08:35:24 +02:00
|
|
|
|
),
|
|
|
|
|
sg.Radio(
|
|
|
|
|
"harvest",
|
|
|
|
|
"f0method",
|
|
|
|
|
key="harvest",
|
|
|
|
|
default=data.get("harvest", "") == True,
|
2023-09-01 04:16:49 +02:00
|
|
|
|
enable_events=True,
|
2023-07-13 08:35:24 +02:00
|
|
|
|
),
|
|
|
|
|
sg.Radio(
|
|
|
|
|
"crepe",
|
|
|
|
|
"f0method",
|
|
|
|
|
key="crepe",
|
|
|
|
|
default=data.get("crepe", "") == True,
|
2023-09-01 04:16:49 +02:00
|
|
|
|
enable_events=True,
|
2023-07-13 08:35:24 +02:00
|
|
|
|
),
|
|
|
|
|
sg.Radio(
|
|
|
|
|
"rmvpe",
|
|
|
|
|
"f0method",
|
|
|
|
|
key="rmvpe",
|
|
|
|
|
default=data.get("rmvpe", "") == True,
|
2023-09-01 04:16:49 +02:00
|
|
|
|
enable_events=True,
|
2023-07-13 08:35:24 +02:00
|
|
|
|
),
|
2023-07-11 08:45:34 +02:00
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
title=i18n("常规设置"),
|
|
|
|
|
),
|
|
|
|
|
sg.Frame(
|
|
|
|
|
layout=[
|
|
|
|
|
[
|
|
|
|
|
sg.Text(i18n("采样长度")),
|
|
|
|
|
sg.Slider(
|
2023-09-02 09:45:50 +02:00
|
|
|
|
range=(0.05, 2.4),
|
2023-07-11 08:45:34 +02:00
|
|
|
|
key="block_time",
|
2023-09-02 09:45:50 +02:00
|
|
|
|
resolution=0.01,
|
2023-07-11 08:45:34 +02:00
|
|
|
|
orientation="h",
|
2023-09-03 07:57:31 +02:00
|
|
|
|
default_value=data.get("block_time", "0.25"),
|
2023-09-01 04:16:49 +02:00
|
|
|
|
enable_events=True,
|
2023-07-11 08:45:34 +02:00
|
|
|
|
),
|
|
|
|
|
],
|
2023-10-06 11:14:33 +02:00
|
|
|
|
# [
|
|
|
|
|
# sg.Text("设备延迟"),
|
|
|
|
|
# sg.Slider(
|
|
|
|
|
# range=(0, 1),
|
|
|
|
|
# key="device_latency",
|
|
|
|
|
# resolution=0.001,
|
|
|
|
|
# orientation="h",
|
|
|
|
|
# default_value=data.get("device_latency", "0.1"),
|
|
|
|
|
# enable_events=True,
|
|
|
|
|
# ),
|
|
|
|
|
# ],
|
2023-07-11 08:45:34 +02:00
|
|
|
|
[
|
|
|
|
|
sg.Text(i18n("harvest进程数")),
|
|
|
|
|
sg.Slider(
|
|
|
|
|
range=(1, n_cpu),
|
|
|
|
|
key="n_cpu",
|
|
|
|
|
resolution=1,
|
|
|
|
|
orientation="h",
|
2023-07-13 08:35:24 +02:00
|
|
|
|
default_value=data.get(
|
2023-10-06 11:14:33 +02:00
|
|
|
|
"n_cpu", min(self.gui_config.n_cpu, n_cpu)
|
2023-07-13 08:35:24 +02:00
|
|
|
|
),
|
2023-09-01 04:16:49 +02:00
|
|
|
|
enable_events=True,
|
2023-07-11 08:45:34 +02:00
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
sg.Text(i18n("淡入淡出长度")),
|
|
|
|
|
sg.Slider(
|
|
|
|
|
range=(0.01, 0.15),
|
|
|
|
|
key="crossfade_length",
|
|
|
|
|
resolution=0.01,
|
|
|
|
|
orientation="h",
|
2023-10-06 11:14:33 +02:00
|
|
|
|
default_value=data.get("crossfade_length", "0.05"),
|
2023-09-01 04:16:49 +02:00
|
|
|
|
enable_events=True,
|
2023-07-11 08:45:34 +02:00
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
sg.Text(i18n("额外推理时长")),
|
|
|
|
|
sg.Slider(
|
2023-08-12 19:05:58 +02:00
|
|
|
|
range=(0.05, 5.00),
|
2023-07-11 08:45:34 +02:00
|
|
|
|
key="extra_time",
|
|
|
|
|
resolution=0.01,
|
|
|
|
|
orientation="h",
|
2023-10-06 11:14:33 +02:00
|
|
|
|
default_value=data.get("extra_time", "2.5"),
|
2023-09-01 04:16:49 +02:00
|
|
|
|
enable_events=True,
|
2023-07-11 08:45:34 +02:00
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
2023-09-01 06:03:35 +02:00
|
|
|
|
sg.Checkbox(
|
|
|
|
|
i18n("输入降噪"),
|
|
|
|
|
key="I_noise_reduce",
|
|
|
|
|
enable_events=True,
|
|
|
|
|
),
|
|
|
|
|
sg.Checkbox(
|
|
|
|
|
i18n("输出降噪"),
|
|
|
|
|
key="O_noise_reduce",
|
|
|
|
|
enable_events=True,
|
|
|
|
|
),
|
2023-10-06 11:14:33 +02:00
|
|
|
|
# sg.Checkbox(
|
|
|
|
|
# "JIT加速",
|
|
|
|
|
# default=self.config.use_jit,
|
|
|
|
|
# key="use_jit",
|
|
|
|
|
# enable_events=False,
|
|
|
|
|
# ),
|
2023-07-11 08:45:34 +02:00
|
|
|
|
],
|
2023-10-06 11:14:33 +02:00
|
|
|
|
# [sg.Text("注:首次使用JIT加速时,会出现卡顿,\n 并伴随一些噪音,但这是正常现象!")],
|
2023-07-11 08:45:34 +02:00
|
|
|
|
],
|
|
|
|
|
title=i18n("性能设置"),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
sg.Button(i18n("开始音频转换"), key="start_vc"),
|
|
|
|
|
sg.Button(i18n("停止音频转换"), key="stop_vc"),
|
2023-09-21 16:17:06 +02:00
|
|
|
|
sg.Radio(
|
|
|
|
|
i18n("输入监听"),
|
|
|
|
|
"function",
|
|
|
|
|
key="im",
|
|
|
|
|
default=False,
|
|
|
|
|
enable_events=True,
|
2023-09-21 16:18:12 +02:00
|
|
|
|
),
|
2023-09-21 16:17:06 +02:00
|
|
|
|
sg.Radio(
|
|
|
|
|
i18n("输出变声"),
|
|
|
|
|
"function",
|
|
|
|
|
key="vc",
|
|
|
|
|
default=True,
|
|
|
|
|
enable_events=True,
|
|
|
|
|
),
|
|
|
|
|
sg.Text(i18n("算法延迟(ms):")),
|
|
|
|
|
sg.Text("0", key="delay_time"),
|
2023-07-11 08:45:34 +02:00
|
|
|
|
sg.Text(i18n("推理时间(ms):")),
|
|
|
|
|
sg.Text("0", key="infer_time"),
|
|
|
|
|
],
|
|
|
|
|
]
|
2023-09-01 04:16:49 +02:00
|
|
|
|
self.window = sg.Window("RVC - GUI", layout=layout, finalize=True)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
self.event_handler()
|
|
|
|
|
|
|
|
|
|
def event_handler(self):
|
|
|
|
|
while True:
|
|
|
|
|
event, values = self.window.read()
|
|
|
|
|
if event == sg.WINDOW_CLOSED:
|
|
|
|
|
self.flag_vc = False
|
|
|
|
|
exit()
|
2023-07-22 19:56:43 +02:00
|
|
|
|
if event == "reload_devices":
|
|
|
|
|
prev_input = self.window["sg_input_device"].get()
|
|
|
|
|
prev_output = self.window["sg_output_device"].get()
|
2023-07-22 19:24:51 +02:00
|
|
|
|
input_devices, output_devices, _, _ = self.get_devices(update=True)
|
|
|
|
|
if prev_input not in input_devices:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.sg_input_device = input_devices[0]
|
2023-07-22 19:24:51 +02:00
|
|
|
|
else:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.sg_input_device = prev_input
|
2023-07-22 19:56:43 +02:00
|
|
|
|
self.window["sg_input_device"].Update(values=input_devices)
|
|
|
|
|
self.window["sg_input_device"].Update(
|
2023-10-06 11:14:33 +02:00
|
|
|
|
value=self.gui_config.sg_input_device
|
2023-07-22 19:56:43 +02:00
|
|
|
|
)
|
2023-07-22 19:24:51 +02:00
|
|
|
|
if prev_output not in output_devices:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.sg_output_device = output_devices[0]
|
2023-07-22 19:24:51 +02:00
|
|
|
|
else:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.sg_output_device = prev_output
|
2023-07-22 19:56:43 +02:00
|
|
|
|
self.window["sg_output_device"].Update(values=output_devices)
|
|
|
|
|
self.window["sg_output_device"].Update(
|
2023-10-06 11:14:33 +02:00
|
|
|
|
value=self.gui_config.sg_output_device
|
2023-07-22 19:56:43 +02:00
|
|
|
|
)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
if event == "start_vc" and self.flag_vc == False:
|
|
|
|
|
if self.set_values(values) == True:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
printt("cuda_is_available: %s", torch.cuda.is_available())
|
2023-07-11 08:45:34 +02:00
|
|
|
|
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"],
|
2023-09-03 07:57:31 +02:00
|
|
|
|
"rms_mix_rate": values["rms_mix_rate"],
|
2023-07-11 08:45:34 +02:00
|
|
|
|
"index_rate": values["index_rate"],
|
2023-10-06 11:14:33 +02:00
|
|
|
|
# "device_latency": values["device_latency"],
|
2023-07-11 08:45:34 +02:00
|
|
|
|
"block_time": values["block_time"],
|
|
|
|
|
"crossfade_length": values["crossfade_length"],
|
|
|
|
|
"extra_time": values["extra_time"],
|
|
|
|
|
"n_cpu": values["n_cpu"],
|
2023-10-06 11:14:33 +02:00
|
|
|
|
# "use_jit": values["use_jit"],
|
|
|
|
|
"use_jit": False,
|
2023-07-13 08:35:24 +02:00
|
|
|
|
"f0method": ["pm", "harvest", "crepe", "rmvpe"][
|
|
|
|
|
[
|
|
|
|
|
values["pm"],
|
|
|
|
|
values["harvest"],
|
|
|
|
|
values["crepe"],
|
|
|
|
|
values["rmvpe"],
|
|
|
|
|
].index(True)
|
|
|
|
|
],
|
2023-07-11 08:45:34 +02:00
|
|
|
|
}
|
2023-08-26 18:46:03 +02:00
|
|
|
|
with open("configs/config.json", "w") as j:
|
2023-07-11 08:45:34 +02:00
|
|
|
|
json.dump(settings, j)
|
2023-09-21 16:17:06 +02:00
|
|
|
|
global stream_latency
|
|
|
|
|
while stream_latency < 0:
|
|
|
|
|
time.sleep(0.01)
|
2023-09-21 16:18:12 +02:00
|
|
|
|
self.delay_time = (
|
|
|
|
|
stream_latency
|
|
|
|
|
+ values["block_time"]
|
|
|
|
|
+ values["crossfade_length"]
|
|
|
|
|
+ 0.01
|
|
|
|
|
)
|
2023-09-21 16:17:06 +02:00
|
|
|
|
if values["I_noise_reduce"]:
|
|
|
|
|
self.delay_time += values["crossfade_length"]
|
|
|
|
|
self.window["delay_time"].update(int(self.delay_time * 1000))
|
2023-07-11 08:45:34 +02:00
|
|
|
|
if event == "stop_vc" and self.flag_vc == True:
|
|
|
|
|
self.flag_vc = False
|
2023-09-21 16:17:06 +02:00
|
|
|
|
stream_latency = -1
|
2023-09-01 04:16:49 +02:00
|
|
|
|
# Parameter hot update
|
2023-09-01 06:03:35 +02:00
|
|
|
|
if event == "threhold":
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.threhold = values["threhold"]
|
2023-09-01 04:16:49 +02:00
|
|
|
|
elif event == "pitch":
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.pitch = values["pitch"]
|
2023-09-01 04:16:49 +02:00
|
|
|
|
if hasattr(self, "rvc"):
|
|
|
|
|
self.rvc.change_key(values["pitch"])
|
|
|
|
|
elif event == "index_rate":
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.index_rate = values["index_rate"]
|
2023-09-01 04:16:49 +02:00
|
|
|
|
if hasattr(self, "rvc"):
|
|
|
|
|
self.rvc.change_index_rate(values["index_rate"])
|
2023-09-03 07:57:31 +02:00
|
|
|
|
elif event == "rms_mix_rate":
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.rms_mix_rate = values["rms_mix_rate"]
|
2023-09-01 04:16:49 +02:00
|
|
|
|
elif event in ["pm", "harvest", "crepe", "rmvpe"]:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.f0method = event
|
2023-09-01 06:03:35 +02:00
|
|
|
|
elif event == "I_noise_reduce":
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.I_noise_reduce = values["I_noise_reduce"]
|
2023-09-21 16:17:06 +02:00
|
|
|
|
if stream_latency > 0:
|
2023-09-21 16:18:12 +02:00
|
|
|
|
self.delay_time += (
|
|
|
|
|
1 if values["I_noise_reduce"] else -1
|
|
|
|
|
) * values["crossfade_length"]
|
2023-09-21 16:17:06 +02:00
|
|
|
|
self.window["delay_time"].update(int(self.delay_time * 1000))
|
2023-09-01 06:03:35 +02:00
|
|
|
|
elif event == "O_noise_reduce":
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.O_noise_reduce = values["O_noise_reduce"]
|
2023-09-21 16:17:06 +02:00
|
|
|
|
elif event in ["vc", "im"]:
|
|
|
|
|
self.function = event
|
2023-09-01 04:16:49 +02:00
|
|
|
|
elif event != "start_vc" and self.flag_vc == True:
|
|
|
|
|
# Other parameters do not support hot update
|
|
|
|
|
self.flag_vc = False
|
2023-09-21 16:17:06 +02:00
|
|
|
|
stream_latency = -1
|
2023-09-01 06:03:35 +02:00
|
|
|
|
|
2023-07-11 08:45:34 +02:00
|
|
|
|
def set_values(self, values):
|
|
|
|
|
if len(values["pth_path"].strip()) == 0:
|
|
|
|
|
sg.popup(i18n("请选择pth文件"))
|
|
|
|
|
return False
|
|
|
|
|
if len(values["index_path"].strip()) == 0:
|
|
|
|
|
sg.popup(i18n("请选择index文件"))
|
|
|
|
|
return False
|
|
|
|
|
pattern = re.compile("[^\x00-\x7F]+")
|
|
|
|
|
if pattern.findall(values["pth_path"]):
|
|
|
|
|
sg.popup(i18n("pth文件路径不可包含中文"))
|
|
|
|
|
return False
|
|
|
|
|
if pattern.findall(values["index_path"]):
|
|
|
|
|
sg.popup(i18n("index文件路径不可包含中文"))
|
|
|
|
|
return False
|
|
|
|
|
self.set_devices(values["sg_input_device"], values["sg_output_device"])
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.config.use_jit = False # values["use_jit"]
|
|
|
|
|
# self.device_latency = values["device_latency"]
|
|
|
|
|
self.gui_config.pth_path = values["pth_path"]
|
|
|
|
|
self.gui_config.index_path = values["index_path"]
|
|
|
|
|
self.gui_config.threhold = values["threhold"]
|
|
|
|
|
self.gui_config.pitch = values["pitch"]
|
|
|
|
|
self.gui_config.block_time = values["block_time"]
|
|
|
|
|
self.gui_config.crossfade_time = values["crossfade_length"]
|
|
|
|
|
self.gui_config.extra_time = values["extra_time"]
|
|
|
|
|
self.gui_config.I_noise_reduce = values["I_noise_reduce"]
|
|
|
|
|
self.gui_config.O_noise_reduce = values["O_noise_reduce"]
|
|
|
|
|
self.gui_config.rms_mix_rate = values["rms_mix_rate"]
|
|
|
|
|
self.gui_config.index_rate = values["index_rate"]
|
|
|
|
|
self.gui_config.n_cpu = values["n_cpu"]
|
|
|
|
|
self.gui_config.f0method = ["pm", "harvest", "crepe", "rmvpe"][
|
2023-07-13 08:35:24 +02:00
|
|
|
|
[
|
|
|
|
|
values["pm"],
|
|
|
|
|
values["harvest"],
|
|
|
|
|
values["crepe"],
|
|
|
|
|
values["rmvpe"],
|
|
|
|
|
].index(True)
|
|
|
|
|
]
|
2023-07-11 08:45:34 +02:00
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def start_vc(self):
|
|
|
|
|
torch.cuda.empty_cache()
|
|
|
|
|
self.flag_vc = True
|
2023-08-12 19:05:58 +02:00
|
|
|
|
self.rvc = rvc_for_realtime.RVC(
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.pitch,
|
|
|
|
|
self.gui_config.pth_path,
|
|
|
|
|
self.gui_config.index_path,
|
|
|
|
|
self.gui_config.index_rate,
|
|
|
|
|
self.gui_config.n_cpu,
|
2023-07-13 08:35:24 +02:00
|
|
|
|
inp_q,
|
|
|
|
|
opt_q,
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.config,
|
2023-09-14 02:34:30 +02:00
|
|
|
|
self.rvc if hasattr(self, "rvc") else None,
|
2023-07-13 08:35:24 +02:00
|
|
|
|
)
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.samplerate = self.rvc.tgt_sr
|
2023-09-02 09:45:50 +02:00
|
|
|
|
self.zc = self.rvc.tgt_sr // 100
|
2023-09-14 02:34:30 +02:00
|
|
|
|
self.block_frame = (
|
2023-10-06 11:14:33 +02:00
|
|
|
|
int(
|
|
|
|
|
np.round(
|
|
|
|
|
self.gui_config.block_time
|
|
|
|
|
* self.gui_config.samplerate
|
|
|
|
|
/ self.zc
|
|
|
|
|
)
|
|
|
|
|
)
|
2023-09-14 02:34:30 +02:00
|
|
|
|
* self.zc
|
|
|
|
|
)
|
2023-09-02 09:45:50 +02:00
|
|
|
|
self.block_frame_16k = 160 * self.block_frame // self.zc
|
2023-09-14 02:34:30 +02:00
|
|
|
|
self.crossfade_frame = (
|
|
|
|
|
int(
|
|
|
|
|
np.round(
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.crossfade_time
|
|
|
|
|
* self.gui_config.samplerate
|
|
|
|
|
/ self.zc
|
2023-09-14 02:34:30 +02:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
* self.zc
|
|
|
|
|
)
|
2023-09-04 11:01:11 +02:00
|
|
|
|
self.sola_search_frame = self.zc
|
2023-09-14 02:34:30 +02:00
|
|
|
|
self.extra_frame = (
|
2023-10-06 11:14:33 +02:00
|
|
|
|
int(
|
|
|
|
|
np.round(
|
|
|
|
|
self.gui_config.extra_time
|
|
|
|
|
* self.gui_config.samplerate
|
|
|
|
|
/ self.zc
|
|
|
|
|
)
|
|
|
|
|
)
|
2023-09-14 02:34:30 +02:00
|
|
|
|
* self.zc
|
|
|
|
|
)
|
2023-09-04 11:01:11 +02:00
|
|
|
|
self.input_wav: torch.Tensor = torch.zeros(
|
|
|
|
|
self.extra_frame
|
|
|
|
|
+ self.crossfade_frame
|
|
|
|
|
+ self.sola_search_frame
|
|
|
|
|
+ self.block_frame,
|
2023-10-06 11:14:33 +02:00
|
|
|
|
device=self.config.device,
|
2023-07-13 08:35:24 +02:00
|
|
|
|
dtype=torch.float32,
|
|
|
|
|
)
|
2023-09-14 02:34:30 +02:00
|
|
|
|
self.input_wav_res: torch.Tensor = torch.zeros(
|
|
|
|
|
160 * self.input_wav.shape[0] // self.zc,
|
2023-10-06 11:14:33 +02:00
|
|
|
|
device=self.config.device,
|
2023-09-14 02:34:30 +02:00
|
|
|
|
dtype=torch.float32,
|
|
|
|
|
)
|
2023-07-13 08:35:24 +02:00
|
|
|
|
self.pitch: np.ndarray = np.zeros(
|
|
|
|
|
self.input_wav.shape[0] // self.zc,
|
|
|
|
|
dtype="int32",
|
|
|
|
|
)
|
|
|
|
|
self.pitchf: np.ndarray = np.zeros(
|
|
|
|
|
self.input_wav.shape[0] // self.zc,
|
|
|
|
|
dtype="float64",
|
|
|
|
|
)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
self.sola_buffer: torch.Tensor = torch.zeros(
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.crossfade_frame, device=self.config.device, dtype=torch.float32
|
2023-07-11 08:45:34 +02:00
|
|
|
|
)
|
2023-09-04 11:01:11 +02:00
|
|
|
|
self.nr_buffer: torch.Tensor = self.sola_buffer.clone()
|
|
|
|
|
self.output_buffer: torch.Tensor = self.input_wav.clone()
|
2023-09-14 02:34:30 +02:00
|
|
|
|
self.res_buffer: torch.Tensor = torch.zeros(
|
2023-10-06 11:14:33 +02:00
|
|
|
|
2 * self.zc, device=self.config.device, dtype=torch.float32
|
2023-09-14 02:34:30 +02:00
|
|
|
|
)
|
2023-09-04 11:01:11 +02:00
|
|
|
|
self.valid_rate = 1 - (self.extra_frame - 1) / self.input_wav.shape[0]
|
2023-09-01 06:03:35 +02:00
|
|
|
|
self.fade_in_window: torch.Tensor = (
|
|
|
|
|
torch.sin(
|
|
|
|
|
0.5
|
|
|
|
|
* np.pi
|
|
|
|
|
* torch.linspace(
|
|
|
|
|
0.0,
|
|
|
|
|
1.0,
|
|
|
|
|
steps=self.crossfade_frame,
|
2023-10-06 11:14:33 +02:00
|
|
|
|
device=self.config.device,
|
2023-09-01 06:03:35 +02:00
|
|
|
|
dtype=torch.float32,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
** 2
|
|
|
|
|
)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
self.fade_out_window: torch.Tensor = 1 - self.fade_in_window
|
|
|
|
|
self.resampler = tat.Resample(
|
2023-10-06 11:14:33 +02:00
|
|
|
|
orig_freq=self.gui_config.samplerate,
|
|
|
|
|
new_freq=16000,
|
|
|
|
|
dtype=torch.float32,
|
|
|
|
|
).to(self.config.device)
|
2023-09-14 02:34:30 +02:00
|
|
|
|
self.tg = TorchGate(
|
2023-10-06 11:14:33 +02:00
|
|
|
|
sr=self.gui_config.samplerate, n_fft=4 * self.zc, prop_decrease=0.9
|
|
|
|
|
).to(self.config.device)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
thread_vc = threading.Thread(target=self.soundinput)
|
|
|
|
|
thread_vc.start()
|
|
|
|
|
|
|
|
|
|
def soundinput(self):
|
|
|
|
|
"""
|
|
|
|
|
接受音频输入
|
|
|
|
|
"""
|
2023-07-17 16:54:15 +02:00
|
|
|
|
channels = 1 if sys.platform == "darwin" else 2
|
2023-07-11 08:45:34 +02:00
|
|
|
|
with sd.Stream(
|
2023-07-17 16:54:15 +02:00
|
|
|
|
channels=channels,
|
2023-07-11 08:45:34 +02:00
|
|
|
|
callback=self.audio_callback,
|
|
|
|
|
blocksize=self.block_frame,
|
2023-10-06 11:14:33 +02:00
|
|
|
|
samplerate=self.gui_config.samplerate,
|
2023-07-11 08:45:34 +02:00
|
|
|
|
dtype="float32",
|
2023-09-21 16:17:06 +02:00
|
|
|
|
) as stream:
|
|
|
|
|
global stream_latency
|
|
|
|
|
stream_latency = stream.latency[-1]
|
2023-07-11 08:45:34 +02:00
|
|
|
|
while self.flag_vc:
|
2023-10-06 11:14:33 +02:00
|
|
|
|
time.sleep(self.gui_config.block_time)
|
|
|
|
|
printt("Audio block passed.")
|
|
|
|
|
printt("ENDing VC")
|
2023-07-11 08:45:34 +02:00
|
|
|
|
|
|
|
|
|
def audio_callback(
|
|
|
|
|
self, indata: np.ndarray, outdata: np.ndarray, frames, times, status
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
音频处理
|
|
|
|
|
"""
|
|
|
|
|
start_time = time.perf_counter()
|
|
|
|
|
indata = librosa.to_mono(indata.T)
|
2023-10-06 11:14:33 +02:00
|
|
|
|
if self.gui_config.threhold > -60:
|
2023-09-04 11:01:11 +02:00
|
|
|
|
rms = librosa.feature.rms(
|
2023-09-14 02:34:30 +02:00
|
|
|
|
y=indata, frame_length=4 * self.zc, hop_length=self.zc
|
2023-09-04 11:01:11 +02:00
|
|
|
|
)
|
2023-07-13 08:35:24 +02:00
|
|
|
|
db_threhold = (
|
2023-10-06 11:14:33 +02:00
|
|
|
|
librosa.amplitude_to_db(rms, ref=1.0)[0] < self.gui_config.threhold
|
2023-07-13 08:35:24 +02:00
|
|
|
|
)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
for i in range(db_threhold.shape[0]):
|
|
|
|
|
if db_threhold[i]:
|
2023-09-04 11:01:11 +02:00
|
|
|
|
indata[i * self.zc : (i + 1) * self.zc] = 0
|
2023-09-14 02:34:30 +02:00
|
|
|
|
self.input_wav[: -self.block_frame] = self.input_wav[
|
|
|
|
|
self.block_frame :
|
|
|
|
|
].clone()
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.input_wav[-self.block_frame :] = torch.from_numpy(indata).to(
|
|
|
|
|
self.config.device
|
|
|
|
|
)
|
2023-09-14 02:34:30 +02:00
|
|
|
|
self.input_wav_res[: -self.block_frame_16k] = self.input_wav_res[
|
|
|
|
|
self.block_frame_16k :
|
|
|
|
|
].clone()
|
2023-09-04 11:01:11 +02:00
|
|
|
|
# input noise reduction and resampling
|
2023-10-06 11:14:33 +02:00
|
|
|
|
if self.gui_config.I_noise_reduce and self.function == "vc":
|
2023-09-14 02:34:30 +02:00
|
|
|
|
input_wav = self.input_wav[
|
|
|
|
|
-self.crossfade_frame - self.block_frame - 2 * self.zc :
|
|
|
|
|
]
|
|
|
|
|
input_wav = self.tg(
|
|
|
|
|
input_wav.unsqueeze(0), self.input_wav.unsqueeze(0)
|
|
|
|
|
)[0, 2 * self.zc :]
|
2023-09-04 11:01:11 +02:00
|
|
|
|
input_wav[: self.crossfade_frame] *= self.fade_in_window
|
2023-09-14 02:34:30 +02:00
|
|
|
|
input_wav[: self.crossfade_frame] += (
|
|
|
|
|
self.nr_buffer * self.fade_out_window
|
|
|
|
|
)
|
|
|
|
|
self.nr_buffer[:] = input_wav[-self.crossfade_frame :]
|
|
|
|
|
input_wav = torch.cat(
|
|
|
|
|
(self.res_buffer[:], input_wav[: self.block_frame])
|
|
|
|
|
)
|
|
|
|
|
self.res_buffer[:] = input_wav[-2 * self.zc :]
|
|
|
|
|
self.input_wav_res[-self.block_frame_16k - 160 :] = self.resampler(
|
|
|
|
|
input_wav
|
|
|
|
|
)[160:]
|
2023-09-04 11:01:11 +02:00
|
|
|
|
else:
|
2023-09-14 02:34:30 +02:00
|
|
|
|
self.input_wav_res[-self.block_frame_16k - 160 :] = self.resampler(
|
|
|
|
|
self.input_wav[-self.block_frame - 2 * self.zc :]
|
|
|
|
|
)[160:]
|
2023-09-04 11:01:11 +02:00
|
|
|
|
# infer
|
2023-09-21 16:18:12 +02:00
|
|
|
|
if self.function == "vc":
|
2023-09-21 16:17:06 +02:00
|
|
|
|
f0_extractor_frame = self.block_frame_16k + 800
|
2023-10-06 11:14:33 +02:00
|
|
|
|
if self.gui_config.f0method == "rmvpe":
|
2023-09-21 16:18:12 +02:00
|
|
|
|
f0_extractor_frame = (
|
|
|
|
|
5120 * ((f0_extractor_frame - 1) // 5120 + 1) - 160
|
|
|
|
|
)
|
2023-09-21 16:17:06 +02:00
|
|
|
|
infer_wav = self.rvc.infer(
|
|
|
|
|
self.input_wav_res,
|
|
|
|
|
self.input_wav_res[-f0_extractor_frame:].cpu().numpy(),
|
|
|
|
|
self.block_frame_16k,
|
|
|
|
|
self.valid_rate,
|
|
|
|
|
self.pitch,
|
|
|
|
|
self.pitchf,
|
2023-10-06 11:14:33 +02:00
|
|
|
|
self.gui_config.f0method,
|
2023-09-21 16:17:06 +02:00
|
|
|
|
)
|
|
|
|
|
infer_wav = infer_wav[
|
|
|
|
|
-self.crossfade_frame - self.sola_search_frame - self.block_frame :
|
|
|
|
|
]
|
|
|
|
|
else:
|
|
|
|
|
infer_wav = self.input_wav[
|
|
|
|
|
-self.crossfade_frame - self.sola_search_frame - self.block_frame :
|
|
|
|
|
].clone()
|
2023-09-04 11:01:11 +02:00
|
|
|
|
# output noise reduction
|
2023-10-06 11:14:33 +02:00
|
|
|
|
if (self.gui_config.O_noise_reduce and self.function == "vc") or (
|
|
|
|
|
self.gui_config.I_noise_reduce and self.function == "im"
|
2023-09-21 16:18:12 +02:00
|
|
|
|
):
|
2023-09-14 02:34:30 +02:00
|
|
|
|
self.output_buffer[: -self.block_frame] = self.output_buffer[
|
|
|
|
|
self.block_frame :
|
|
|
|
|
].clone()
|
|
|
|
|
self.output_buffer[-self.block_frame :] = infer_wav[-self.block_frame :]
|
|
|
|
|
infer_wav = self.tg(
|
|
|
|
|
infer_wav.unsqueeze(0), self.output_buffer.unsqueeze(0)
|
|
|
|
|
).squeeze(0)
|
2023-09-04 11:01:11 +02:00
|
|
|
|
# volume envelop mixing
|
2023-10-06 11:14:33 +02:00
|
|
|
|
if self.gui_config.rms_mix_rate < 1 and self.function == "vc":
|
2023-09-03 07:57:31 +02:00
|
|
|
|
rms1 = librosa.feature.rms(
|
2023-09-14 02:34:30 +02:00
|
|
|
|
y=self.input_wav_res[-160 * infer_wav.shape[0] // self.zc :]
|
|
|
|
|
.cpu()
|
|
|
|
|
.numpy(),
|
|
|
|
|
frame_length=640,
|
|
|
|
|
hop_length=160,
|
2023-09-03 07:57:31 +02:00
|
|
|
|
)
|
2023-10-06 11:14:33 +02:00
|
|
|
|
rms1 = torch.from_numpy(rms1).to(self.config.device)
|
2023-09-03 07:57:31 +02:00
|
|
|
|
rms1 = F.interpolate(
|
2023-09-14 02:34:30 +02:00
|
|
|
|
rms1.unsqueeze(0),
|
|
|
|
|
size=infer_wav.shape[0] + 1,
|
|
|
|
|
mode="linear",
|
|
|
|
|
align_corners=True,
|
|
|
|
|
)[0, 0, :-1]
|
2023-09-03 07:57:31 +02:00
|
|
|
|
rms2 = librosa.feature.rms(
|
2023-09-14 02:34:30 +02:00
|
|
|
|
y=infer_wav[:].cpu().numpy(),
|
|
|
|
|
frame_length=4 * self.zc,
|
|
|
|
|
hop_length=self.zc,
|
2023-09-03 07:57:31 +02:00
|
|
|
|
)
|
2023-10-06 11:14:33 +02:00
|
|
|
|
rms2 = torch.from_numpy(rms2).to(self.config.device)
|
2023-09-03 07:57:31 +02:00
|
|
|
|
rms2 = F.interpolate(
|
2023-09-14 02:34:30 +02:00
|
|
|
|
rms2.unsqueeze(0),
|
|
|
|
|
size=infer_wav.shape[0] + 1,
|
|
|
|
|
mode="linear",
|
|
|
|
|
align_corners=True,
|
|
|
|
|
)[0, 0, :-1]
|
2023-09-03 07:57:31 +02:00
|
|
|
|
rms2 = torch.max(rms2, torch.zeros_like(rms2) + 1e-3)
|
2023-09-14 02:34:30 +02:00
|
|
|
|
infer_wav *= torch.pow(
|
2023-10-06 11:14:33 +02:00
|
|
|
|
rms1 / rms2, torch.tensor(1 - self.gui_config.rms_mix_rate)
|
2023-09-14 02:34:30 +02:00
|
|
|
|
)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
# SOLA algorithm from https://github.com/yxlllc/DDSP-SVC
|
2023-09-14 02:34:30 +02:00
|
|
|
|
conv_input = infer_wav[
|
|
|
|
|
None, None, : self.crossfade_frame + self.sola_search_frame
|
|
|
|
|
]
|
2023-09-04 11:01:11 +02:00
|
|
|
|
cor_nom = F.conv1d(conv_input, self.sola_buffer[None, None, :])
|
2023-07-11 08:45:34 +02:00
|
|
|
|
cor_den = torch.sqrt(
|
2023-09-14 02:34:30 +02:00
|
|
|
|
F.conv1d(
|
|
|
|
|
conv_input**2,
|
2023-10-06 11:14:33 +02:00
|
|
|
|
torch.ones(1, 1, self.crossfade_frame, device=self.config.device),
|
2023-09-14 02:34:30 +02:00
|
|
|
|
)
|
|
|
|
|
+ 1e-8
|
|
|
|
|
)
|
2023-07-17 16:54:15 +02:00
|
|
|
|
if sys.platform == "darwin":
|
2023-07-26 05:54:37 +02:00
|
|
|
|
_, sola_offset = torch.max(cor_nom[0, 0] / cor_den[0, 0])
|
|
|
|
|
sola_offset = sola_offset.item()
|
|
|
|
|
else:
|
|
|
|
|
sola_offset = torch.argmax(cor_nom[0, 0] / cor_den[0, 0])
|
2023-10-06 11:14:33 +02:00
|
|
|
|
printt("sola_offset = %d", int(sola_offset))
|
2023-09-14 02:34:30 +02:00
|
|
|
|
infer_wav = infer_wav[
|
|
|
|
|
sola_offset : sola_offset + self.block_frame + self.crossfade_frame
|
|
|
|
|
]
|
2023-09-04 11:01:11 +02:00
|
|
|
|
infer_wav[: self.crossfade_frame] *= self.fade_in_window
|
2023-09-14 02:34:30 +02:00
|
|
|
|
infer_wav[: self.crossfade_frame] += self.sola_buffer * self.fade_out_window
|
|
|
|
|
self.sola_buffer[:] = infer_wav[-self.crossfade_frame :]
|
2023-09-03 07:57:31 +02:00
|
|
|
|
if sys.platform == "darwin":
|
2023-09-14 02:34:30 +02:00
|
|
|
|
outdata[:] = (
|
|
|
|
|
infer_wav[: -self.crossfade_frame].cpu().numpy()[:, np.newaxis]
|
|
|
|
|
)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
else:
|
2023-09-14 02:34:30 +02:00
|
|
|
|
outdata[:] = (
|
|
|
|
|
infer_wav[: -self.crossfade_frame].repeat(2, 1).t().cpu().numpy()
|
|
|
|
|
)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
total_time = time.perf_counter() - start_time
|
|
|
|
|
self.window["infer_time"].update(int(total_time * 1000))
|
2023-10-06 11:14:33 +02:00
|
|
|
|
printt("Infer time: %.2f", total_time)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
|
|
|
|
|
def get_devices(self, update: bool = True):
|
|
|
|
|
"""获取设备列表"""
|
|
|
|
|
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
|
|
|
|
|
]
|
|
|
|
|
input_devices_indices = [
|
|
|
|
|
d["index"] if "index" in d else d["name"]
|
|
|
|
|
for d in devices
|
|
|
|
|
if d["max_input_channels"] > 0
|
|
|
|
|
]
|
|
|
|
|
output_devices_indices = [
|
|
|
|
|
d["index"] if "index" in d else d["name"]
|
|
|
|
|
for d in devices
|
|
|
|
|
if d["max_output_channels"] > 0
|
|
|
|
|
]
|
|
|
|
|
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()
|
2023-07-13 08:35:24 +02:00
|
|
|
|
sd.default.device[0] = input_device_indices[
|
|
|
|
|
input_devices.index(input_device)
|
|
|
|
|
]
|
2023-07-11 08:45:34 +02:00
|
|
|
|
sd.default.device[1] = output_device_indices[
|
|
|
|
|
output_devices.index(output_device)
|
|
|
|
|
]
|
2023-10-06 11:14:33 +02:00
|
|
|
|
printt("Input device: %s:%s", str(sd.default.device[0]), input_device)
|
|
|
|
|
printt("Output device: %s:%s", str(sd.default.device[1]), output_device)
|
2023-07-11 08:45:34 +02:00
|
|
|
|
|
|
|
|
|
gui = GUI()
|