2023-03-24 02:56:15 +03:00
|
|
|
import sys
|
|
|
|
import os
|
2023-04-28 08:49:17 +03:00
|
|
|
from voice_changer.utils.LoadModelParams import LoadModelParams
|
|
|
|
|
|
|
|
from voice_changer.utils.VoiceChangerModel import AudioInOut
|
|
|
|
from voice_changer.utils.VoiceChangerParams import VoiceChangerParams
|
|
|
|
|
|
|
|
if sys.platform.startswith("darwin"):
|
2023-03-24 02:56:15 +03:00
|
|
|
baseDir = [x for x in sys.path if x.endswith("Contents/MacOS")]
|
|
|
|
if len(baseDir) != 1:
|
|
|
|
print("baseDir should be only one ", baseDir)
|
|
|
|
sys.exit()
|
|
|
|
modulePath = os.path.join(baseDir[0], "DDSP-SVC")
|
|
|
|
sys.path.append(modulePath)
|
|
|
|
else:
|
|
|
|
sys.path.append("DDSP-SVC")
|
|
|
|
|
|
|
|
from dataclasses import dataclass, asdict, field
|
|
|
|
import numpy as np
|
|
|
|
import torch
|
2023-04-28 08:49:17 +03:00
|
|
|
import ddsp.vocoder as vo # type:ignore
|
|
|
|
from ddsp.core import upsample # type:ignore
|
|
|
|
from enhancer import Enhancer # type:ignore
|
2023-04-17 03:45:12 +03:00
|
|
|
|
|
|
|
from Exceptions import NoModeLoadedException
|
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
providers = [
|
|
|
|
"OpenVINOExecutionProvider",
|
|
|
|
"CUDAExecutionProvider",
|
|
|
|
"DmlExecutionProvider",
|
|
|
|
"CPUExecutionProvider",
|
|
|
|
]
|
2023-03-24 02:56:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2023-04-28 08:49:17 +03:00
|
|
|
class DDSP_SVCSettings:
|
2023-03-24 02:56:15 +03:00
|
|
|
gpu: int = 0
|
|
|
|
dstId: int = 0
|
|
|
|
|
2023-04-16 15:34:00 +03:00
|
|
|
f0Detector: str = "dio" # dio or harvest # parselmouth
|
2023-03-24 02:56:15 +03:00
|
|
|
tran: int = 20
|
|
|
|
predictF0: int = 0 # 0:False, 1:True
|
|
|
|
silentThreshold: float = 0.00001
|
|
|
|
extraConvertSize: int = 1024 * 32
|
2023-04-16 15:34:00 +03:00
|
|
|
|
|
|
|
enableEnhancer: int = 0
|
|
|
|
enhancerTune: int = 0
|
2023-03-24 02:56:15 +03:00
|
|
|
|
|
|
|
framework: str = "PyTorch" # PyTorch or ONNX
|
|
|
|
pyTorchModelFile: str = ""
|
|
|
|
onnxModelFile: str = ""
|
|
|
|
configFile: str = ""
|
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
speakers: dict[str, int] = field(default_factory=lambda: {})
|
2023-03-24 02:56:15 +03:00
|
|
|
|
|
|
|
# ↓mutableな物だけ列挙
|
2023-04-28 08:49:17 +03:00
|
|
|
intData = [
|
|
|
|
"gpu",
|
|
|
|
"dstId",
|
|
|
|
"tran",
|
|
|
|
"predictF0",
|
|
|
|
"extraConvertSize",
|
|
|
|
"enableEnhancer",
|
|
|
|
"enhancerTune",
|
|
|
|
]
|
2023-04-20 11:17:43 +03:00
|
|
|
floatData = ["silentThreshold", "clusterInferRatio"]
|
2023-03-24 02:56:15 +03:00
|
|
|
strData = ["framework", "f0Detector"]
|
|
|
|
|
|
|
|
|
|
|
|
class DDSP_SVC:
|
2023-04-28 08:49:17 +03:00
|
|
|
audio_buffer: AudioInOut | None = None
|
|
|
|
|
|
|
|
def __init__(self, params: VoiceChangerParams):
|
2023-03-24 02:56:15 +03:00
|
|
|
self.settings = DDSP_SVCSettings()
|
|
|
|
self.net_g = None
|
|
|
|
self.onnx_session = None
|
|
|
|
|
|
|
|
self.gpu_num = torch.cuda.device_count()
|
|
|
|
self.prevVol = 0
|
|
|
|
self.params = params
|
|
|
|
print("DDSP-SVC initialization:", params)
|
|
|
|
|
2023-04-16 22:37:22 +03:00
|
|
|
def useDevice(self):
|
|
|
|
if self.settings.gpu >= 0 and torch.cuda.is_available():
|
|
|
|
return torch.device("cuda", index=self.settings.gpu)
|
|
|
|
else:
|
|
|
|
return torch.device("cpu")
|
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
def loadModel(self, props: LoadModelParams):
|
|
|
|
self.settings.pyTorchModelFile = props.files.pyTorchModelFilename
|
2023-03-24 02:56:15 +03:00
|
|
|
# model
|
2023-04-28 08:49:17 +03:00
|
|
|
model, args = vo.load_model(
|
|
|
|
self.settings.pyTorchModelFile, device=self.useDevice()
|
|
|
|
)
|
2023-03-24 02:56:15 +03:00
|
|
|
self.model = model
|
|
|
|
self.args = args
|
2023-04-16 22:37:22 +03:00
|
|
|
self.sampling_rate = args.data.sampling_rate
|
2023-04-28 08:49:17 +03:00
|
|
|
self.hop_size = int(
|
|
|
|
self.args.data.block_size
|
|
|
|
* self.sampling_rate
|
|
|
|
/ self.args.data.sampling_rate
|
|
|
|
)
|
2023-03-24 02:56:15 +03:00
|
|
|
|
2023-03-24 03:47:14 +03:00
|
|
|
# hubert
|
2023-04-28 08:49:17 +03:00
|
|
|
self.vec_path = self.params.hubert_soft
|
2023-03-24 02:56:15 +03:00
|
|
|
self.encoder = vo.Units_Encoder(
|
2023-04-16 22:37:22 +03:00
|
|
|
self.args.data.encoder,
|
|
|
|
self.vec_path,
|
|
|
|
self.args.data.encoder_sample_rate,
|
|
|
|
self.args.data.encoder_hop_size,
|
2023-04-28 08:49:17 +03:00
|
|
|
device=self.useDevice(),
|
|
|
|
)
|
2023-03-29 17:11:03 +03:00
|
|
|
|
|
|
|
# ort_options = onnxruntime.SessionOptions()
|
|
|
|
# ort_options.intra_op_num_threads = 8
|
|
|
|
# self.onnx_session = onnxruntime.InferenceSession(
|
|
|
|
# "model_DDSP-SVC/hubert4.0.onnx",
|
|
|
|
# providers=providers
|
|
|
|
# )
|
|
|
|
# inputs = self.onnx_session.get_inputs()
|
|
|
|
# outputs = self.onnx_session.get_outputs()
|
|
|
|
# for input in inputs:
|
|
|
|
# print("input::::", input)
|
|
|
|
# for output in outputs:
|
|
|
|
# print("output::::", output)
|
|
|
|
|
2023-03-24 02:56:15 +03:00
|
|
|
# f0dec
|
|
|
|
self.f0_detector = vo.F0_Extractor(
|
2023-03-24 04:27:45 +03:00
|
|
|
# "crepe",
|
2023-03-24 02:56:15 +03:00
|
|
|
self.settings.f0Detector,
|
2023-04-16 22:37:22 +03:00
|
|
|
self.sampling_rate,
|
2023-03-24 04:27:45 +03:00
|
|
|
self.hop_size,
|
2023-03-24 02:56:15 +03:00
|
|
|
float(50),
|
2023-04-28 08:49:17 +03:00
|
|
|
float(1100),
|
|
|
|
)
|
2023-03-24 02:56:15 +03:00
|
|
|
|
2023-03-24 03:47:14 +03:00
|
|
|
self.volume_extractor = vo.Volume_Extractor(self.hop_size)
|
2023-04-28 08:49:17 +03:00
|
|
|
self.enhancer_path = self.params.nsf_hifigan
|
|
|
|
self.enhancer = Enhancer(
|
|
|
|
self.args.enhancer.type, self.enhancer_path, device=self.useDevice()
|
|
|
|
)
|
2023-03-24 02:56:15 +03:00
|
|
|
return self.get_info()
|
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
def update_settings(self, key: str, val: int | float | str):
|
|
|
|
if key == "onnxExecutionProvider" and self.onnx_session is not None:
|
2023-03-24 02:56:15 +03:00
|
|
|
if val == "CUDAExecutionProvider":
|
|
|
|
if self.settings.gpu < 0 or self.settings.gpu >= self.gpu_num:
|
|
|
|
self.settings.gpu = 0
|
2023-04-28 08:49:17 +03:00
|
|
|
provider_options = [{"device_id": self.settings.gpu}]
|
|
|
|
self.onnx_session.set_providers(
|
|
|
|
providers=[val], provider_options=provider_options
|
|
|
|
)
|
2023-03-24 02:56:15 +03:00
|
|
|
else:
|
|
|
|
self.onnx_session.set_providers(providers=[val])
|
|
|
|
elif key in self.settings.intData:
|
2023-04-28 08:49:17 +03:00
|
|
|
val = int(val)
|
|
|
|
setattr(self.settings, key, val)
|
|
|
|
if (
|
|
|
|
key == "gpu"
|
|
|
|
and val >= 0
|
|
|
|
and val < self.gpu_num
|
|
|
|
and self.onnx_session is not None
|
|
|
|
):
|
2023-03-24 02:56:15 +03:00
|
|
|
providers = self.onnx_session.get_providers()
|
|
|
|
print("Providers:", providers)
|
|
|
|
if "CUDAExecutionProvider" in providers:
|
2023-04-28 08:49:17 +03:00
|
|
|
provider_options = [{"device_id": self.settings.gpu}]
|
|
|
|
self.onnx_session.set_providers(
|
|
|
|
providers=["CUDAExecutionProvider"],
|
|
|
|
provider_options=provider_options,
|
|
|
|
)
|
2023-04-17 04:35:16 +03:00
|
|
|
if key == "gpu" and len(self.settings.pyTorchModelFile) > 0:
|
2023-04-28 08:49:17 +03:00
|
|
|
model, _args = vo.load_model(
|
|
|
|
self.settings.pyTorchModelFile, device=self.useDevice()
|
|
|
|
)
|
2023-04-16 22:37:22 +03:00
|
|
|
self.model = model
|
2023-04-28 08:49:17 +03:00
|
|
|
self.enhancer = Enhancer(
|
|
|
|
self.args.enhancer.type, self.enhancer_path, device=self.useDevice()
|
|
|
|
)
|
|
|
|
self.encoder = vo.Units_Encoder(
|
|
|
|
self.args.data.encoder,
|
|
|
|
self.vec_path,
|
|
|
|
self.args.data.encoder_sample_rate,
|
|
|
|
self.args.data.encoder_hop_size,
|
|
|
|
device=self.useDevice(),
|
|
|
|
)
|
2023-04-16 22:37:22 +03:00
|
|
|
|
2023-03-24 02:56:15 +03:00
|
|
|
elif key in self.settings.floatData:
|
|
|
|
setattr(self.settings, key, float(val))
|
|
|
|
elif key in self.settings.strData:
|
|
|
|
setattr(self.settings, key, str(val))
|
2023-04-16 15:34:00 +03:00
|
|
|
if key == "f0Detector":
|
|
|
|
print("f0Detector update", val)
|
2023-04-28 11:18:33 +03:00
|
|
|
# if val == "dio":
|
|
|
|
# val = "parselmouth"
|
2023-04-16 22:37:22 +03:00
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
if hasattr(self, "sampling_rate") is False:
|
2023-04-16 22:37:22 +03:00
|
|
|
self.sampling_rate = 44100
|
|
|
|
self.hop_size = 512
|
|
|
|
|
2023-04-16 15:34:00 +03:00
|
|
|
self.f0_detector = vo.F0_Extractor(
|
2023-04-28 08:49:17 +03:00
|
|
|
val, self.sampling_rate, self.hop_size, float(50), float(1100)
|
|
|
|
)
|
2023-03-24 02:56:15 +03:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_info(self):
|
|
|
|
data = asdict(self.settings)
|
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
data["onnxExecutionProviders"] = (
|
|
|
|
self.onnx_session.get_providers() if self.onnx_session is not None else []
|
|
|
|
)
|
2023-03-24 02:56:15 +03:00
|
|
|
files = ["configFile", "pyTorchModelFile", "onnxModelFile"]
|
|
|
|
for f in files:
|
2023-04-28 08:49:17 +03:00
|
|
|
if data[f] is not None and os.path.exists(data[f]):
|
2023-03-24 02:56:15 +03:00
|
|
|
data[f] = os.path.basename(data[f])
|
|
|
|
else:
|
|
|
|
data[f] = ""
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def get_processing_sampling_rate(self):
|
2023-04-16 22:37:22 +03:00
|
|
|
return self.sampling_rate
|
2023-03-24 02:56:15 +03:00
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
def generate_input(
|
|
|
|
self,
|
|
|
|
newData: AudioInOut,
|
|
|
|
inputSize: int,
|
|
|
|
crossfadeSize: int,
|
|
|
|
solaSearchFrame: int = 0,
|
|
|
|
):
|
2023-03-24 03:38:23 +03:00
|
|
|
newData = newData.astype(np.float32) / 32768.0
|
2023-03-24 02:56:15 +03:00
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
if self.audio_buffer is not None:
|
|
|
|
self.audio_buffer = np.concatenate(
|
|
|
|
[self.audio_buffer, newData], 0
|
|
|
|
) # 過去のデータに連結
|
2023-03-24 02:56:15 +03:00
|
|
|
else:
|
|
|
|
self.audio_buffer = newData
|
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
convertSize = (
|
|
|
|
inputSize + crossfadeSize + solaSearchFrame + self.settings.extraConvertSize
|
|
|
|
)
|
2023-04-16 15:34:00 +03:00
|
|
|
|
2023-03-24 03:47:14 +03:00
|
|
|
if convertSize % self.hop_size != 0: # モデルの出力のホップサイズで切り捨てが発生するので補う。
|
|
|
|
convertSize = convertSize + (self.hop_size - (convertSize % self.hop_size))
|
2023-03-24 02:56:15 +03:00
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
convertOffset = -1 * convertSize
|
|
|
|
self.audio_buffer = self.audio_buffer[convertOffset:] # 変換対象の部分だけ抽出
|
2023-03-24 02:56:15 +03:00
|
|
|
|
2023-03-24 04:27:45 +03:00
|
|
|
# f0
|
2023-04-28 08:49:17 +03:00
|
|
|
f0 = self.f0_detector.extract(
|
|
|
|
self.audio_buffer * 32768.0,
|
|
|
|
uv_interp=True,
|
|
|
|
silence_front=self.settings.extraConvertSize / self.sampling_rate,
|
|
|
|
)
|
2023-03-24 03:42:21 +03:00
|
|
|
f0 = torch.from_numpy(f0).float().unsqueeze(-1).unsqueeze(0)
|
2023-03-24 04:27:45 +03:00
|
|
|
f0 = f0 * 2 ** (float(self.settings.tran) / 12)
|
2023-03-24 03:42:21 +03:00
|
|
|
|
2023-03-24 04:27:45 +03:00
|
|
|
# volume, mask
|
2023-03-24 03:47:14 +03:00
|
|
|
volume = self.volume_extractor.extract(self.audio_buffer)
|
2023-04-28 08:49:17 +03:00
|
|
|
mask = (volume > 10 ** (float(-60) / 20)).astype("float")
|
2023-03-24 04:27:45 +03:00
|
|
|
mask = np.pad(mask, (4, 4), constant_values=(mask[0], mask[-1]))
|
2023-04-28 08:49:17 +03:00
|
|
|
mask = np.array(
|
|
|
|
[np.max(mask[n : n + 9]) for n in range(len(mask) - 8)] # noqa: E203
|
|
|
|
)
|
2023-03-24 04:27:45 +03:00
|
|
|
mask = torch.from_numpy(mask).float().unsqueeze(-1).unsqueeze(0)
|
|
|
|
mask = upsample(mask, self.args.data.block_size).squeeze(-1)
|
|
|
|
volume = torch.from_numpy(volume).float().unsqueeze(-1).unsqueeze(0)
|
2023-03-24 03:47:14 +03:00
|
|
|
|
2023-03-24 04:27:45 +03:00
|
|
|
# embed
|
2023-04-28 08:49:17 +03:00
|
|
|
audio = (
|
|
|
|
torch.from_numpy(self.audio_buffer)
|
|
|
|
.float()
|
|
|
|
.to(self.useDevice())
|
|
|
|
.unsqueeze(0)
|
|
|
|
)
|
2023-04-16 22:37:22 +03:00
|
|
|
seg_units = self.encoder.encode(audio, self.sampling_rate, self.hop_size)
|
2023-03-24 04:27:45 +03:00
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
cropOffset = -1 * (inputSize + crossfadeSize)
|
|
|
|
cropEnd = -1 * (crossfadeSize)
|
|
|
|
crop = self.audio_buffer[cropOffset:cropEnd]
|
2023-03-24 02:56:15 +03:00
|
|
|
|
2023-03-24 04:27:45 +03:00
|
|
|
rms = np.sqrt(np.square(crop).mean(axis=0))
|
|
|
|
vol = max(rms, self.prevVol * 0.0)
|
|
|
|
self.prevVol = vol
|
2023-03-24 02:56:15 +03:00
|
|
|
|
2023-03-24 04:27:45 +03:00
|
|
|
return (seg_units, f0, volume, mask, convertSize, vol)
|
2023-03-24 02:56:15 +03:00
|
|
|
|
|
|
|
def _onnx_inference(self, data):
|
2023-04-28 08:49:17 +03:00
|
|
|
if hasattr(self, "onnx_session") is False or self.onnx_session is None:
|
2023-03-24 02:56:15 +03:00
|
|
|
print("[Voice Changer] No onnx session.")
|
2023-04-17 03:45:12 +03:00
|
|
|
raise NoModeLoadedException("ONNX")
|
2023-03-24 02:56:15 +03:00
|
|
|
|
2023-04-20 11:17:43 +03:00
|
|
|
raise NoModeLoadedException("ONNX")
|
2023-03-24 02:56:15 +03:00
|
|
|
|
|
|
|
def _pyTorch_inference(self, data):
|
2023-04-28 08:49:17 +03:00
|
|
|
if hasattr(self, "model") is False or self.model is None:
|
2023-03-24 02:56:15 +03:00
|
|
|
print("[Voice Changer] No pyTorch session.")
|
2023-04-17 03:45:12 +03:00
|
|
|
raise NoModeLoadedException("pytorch")
|
2023-03-24 02:56:15 +03:00
|
|
|
|
2023-04-16 22:37:22 +03:00
|
|
|
c = data[0].to(self.useDevice())
|
|
|
|
f0 = data[1].to(self.useDevice())
|
|
|
|
volume = data[2].to(self.useDevice())
|
|
|
|
mask = data[3].to(self.useDevice())
|
2023-03-24 03:38:23 +03:00
|
|
|
|
2023-04-28 08:49:17 +03:00
|
|
|
# convertSize = data[4]
|
|
|
|
# vol = data[5]
|
2023-03-29 17:11:03 +03:00
|
|
|
# if vol < self.settings.silentThreshold:
|
|
|
|
# print("threshold")
|
|
|
|
# return np.zeros(convertSize).astype(np.int16)
|
2023-03-24 02:56:15 +03:00
|
|
|
|
|
|
|
with torch.no_grad():
|
2023-04-28 08:49:17 +03:00
|
|
|
spk_id = torch.LongTensor(np.array([[self.settings.dstId]])).to(
|
|
|
|
self.useDevice()
|
|
|
|
)
|
|
|
|
seg_output, _, (s_h, s_n) = self.model(
|
|
|
|
c, f0, volume, spk_id=spk_id, spk_mix_dict=None
|
|
|
|
)
|
2023-03-24 04:27:45 +03:00
|
|
|
seg_output *= mask
|
2023-03-24 04:52:36 +03:00
|
|
|
|
2023-04-16 15:34:00 +03:00
|
|
|
if self.settings.enableEnhancer:
|
|
|
|
seg_output, output_sample_rate = self.enhancer.enhance(
|
|
|
|
seg_output,
|
|
|
|
self.args.data.sampling_rate,
|
|
|
|
f0,
|
|
|
|
self.args.data.block_size,
|
2023-04-18 22:30:56 +03:00
|
|
|
# adaptive_key=float(self.settings.enhancerTune),
|
2023-04-28 08:49:17 +03:00
|
|
|
adaptive_key="auto",
|
|
|
|
silence_front=self.settings.extraConvertSize / self.sampling_rate,
|
|
|
|
)
|
2023-04-16 15:34:00 +03:00
|
|
|
|
2023-03-24 04:27:45 +03:00
|
|
|
result = seg_output.squeeze().cpu().numpy() * 32768.0
|
|
|
|
return np.array(result).astype(np.int16)
|
2023-03-24 02:56:15 +03:00
|
|
|
|
|
|
|
def inference(self, data):
|
|
|
|
if self.settings.framework == "ONNX":
|
|
|
|
audio = self._onnx_inference(data)
|
|
|
|
else:
|
|
|
|
audio = self._pyTorch_inference(data)
|
|
|
|
return audio
|
|
|
|
|
|
|
|
def destroy(self):
|
|
|
|
del self.net_g
|
|
|
|
del self.onnx_session
|
|
|
|
|
2023-04-10 18:21:17 +03:00
|
|
|
def __del__(self):
|
|
|
|
del self.net_g
|
|
|
|
del self.onnx_session
|
|
|
|
|
2023-04-16 15:34:00 +03:00
|
|
|
remove_path = os.path.join("DDSP-SVC")
|
2023-04-28 08:49:17 +03:00
|
|
|
sys.path = [x for x in sys.path if x.endswith(remove_path) is False]
|
2023-04-16 15:34:00 +03:00
|
|
|
|
|
|
|
for key in list(sys.modules):
|
|
|
|
val = sys.modules.get(key)
|
|
|
|
try:
|
|
|
|
file_path = val.__file__
|
|
|
|
if file_path.find("DDSP-SVC" + os.path.sep) >= 0:
|
|
|
|
print("remove", key, file_path)
|
|
|
|
sys.modules.pop(key)
|
2023-04-28 08:49:17 +03:00
|
|
|
except: # type:ignore
|
2023-04-16 15:34:00 +03:00
|
|
|
pass
|