mirror of
https://github.com/w-okada/voice-changer.git
synced 2025-02-02 16:23:58 +03:00
sendig sample rate default 48000 to rvc
This commit is contained in:
parent
3fefc528ab
commit
5465f6e1c3
2
client/demo/dist/index.js
vendored
2
client/demo/dist/index.js
vendored
File diff suppressed because one or more lines are too long
@ -445,7 +445,7 @@ export const DefaultWorkletNodeSetting_so_vits_svc_40v2: WorkletNodeSetting = {
|
||||
export const DefaultWorkletNodeSetting_RVC: WorkletNodeSetting = {
|
||||
serverUrl: "",
|
||||
protocol: "sio",
|
||||
sendingSampleRate: 24000,
|
||||
sendingSampleRate: 48000,
|
||||
inputChunkNum: 256,
|
||||
downSamplingMode: "average"
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ from const import HUBERT_ONNX_MODEL_PATH
|
||||
|
||||
import pyworld as pw
|
||||
|
||||
from vc_infer_pipeline import VC
|
||||
from voice_changer.RVC.custom_vc_infer_pipeline import VC
|
||||
from infer_pack.models import SynthesizerTrnMs256NSFsid
|
||||
from fairseq import checkpoint_utils
|
||||
providers = ['OpenVINOExecutionProvider', "CUDAExecutionProvider", "DmlExecutionProvider", "CPUExecutionProvider"]
|
||||
@ -71,7 +71,6 @@ class RVC:
|
||||
self.net_g = None
|
||||
self.onnx_session = None
|
||||
|
||||
self.raw_path = io.BytesIO()
|
||||
self.gpu_num = torch.cuda.device_count()
|
||||
self.prevVol = 0
|
||||
self.params = params
|
||||
@ -103,7 +102,7 @@ class RVC:
|
||||
# PyTorchモデル生成
|
||||
if pyTorch_model_file != None:
|
||||
cpt = torch.load(pyTorch_model_file, map_location="cpu")
|
||||
self.settings.tgt_sr = cpt["config"][-1]
|
||||
self.settings.modelSamplingRate = cpt["config"][-1]
|
||||
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=self.is_half)
|
||||
net_g.eval()
|
||||
net_g.load_state_dict(cpt["weight"], strict=False)
|
||||
@ -163,8 +162,7 @@ class RVC:
|
||||
return data
|
||||
|
||||
def get_processing_sampling_rate(self):
|
||||
return self.settings.tgt_sr
|
||||
# return 24000
|
||||
return self.settings.modelSamplingRate
|
||||
|
||||
def generate_input(self, newData: any, inputSize: int, crossfadeSize: int):
|
||||
newData = newData.astype(np.float32) / 32768.0
|
||||
@ -208,13 +206,13 @@ class RVC:
|
||||
convertSize = data[1]
|
||||
vol = data[2]
|
||||
|
||||
audio = resampy.resample(audio, self.settings.tgt_sr, 16000)
|
||||
audio = resampy.resample(audio, self.settings.modelSamplingRate, 16000)
|
||||
|
||||
if vol < self.settings.silentThreshold:
|
||||
return np.zeros(convertSize).astype(np.int16)
|
||||
|
||||
with torch.no_grad():
|
||||
vc = VC(self.settings.tgt_sr, dev, self.is_half)
|
||||
vc = VC(self.settings.modelSamplingRate, dev, self.is_half)
|
||||
sid = 0
|
||||
times = [0, 0, 0]
|
||||
f0_up_key = self.settings.tran
|
||||
@ -248,14 +246,16 @@ class RVC:
|
||||
convertSize = data[1]
|
||||
vol = data[2]
|
||||
print("audio len 02,", len(audio))
|
||||
audio = resampy.resample(audio, self.settings.tgt_sr, 16000)
|
||||
audio = resampy.resample(audio, self.settings.modelSamplingRate, 16000)
|
||||
print("audio len 03,", len(audio))
|
||||
|
||||
if vol < self.settings.silentThreshold:
|
||||
return np.zeros(convertSize).astype(np.int16)
|
||||
|
||||
with torch.no_grad():
|
||||
vc = VC(self.settings.tgt_sr, dev, self.is_half)
|
||||
repeat = 3 if self.is_half else 1
|
||||
repeat *= self.settings.rvcQuality # 0 or 3
|
||||
vc = VC(self.settings.modelSamplingRate, dev, self.is_half, repeat)
|
||||
sid = 0
|
||||
times = [0, 0, 0]
|
||||
f0_up_key = self.settings.tran
|
||||
|
210
server/voice_changer/RVC/custom_vc_infer_pipeline.py
Normal file
210
server/voice_changer/RVC/custom_vc_infer_pipeline.py
Normal file
@ -0,0 +1,210 @@
|
||||
import numpy as np
|
||||
import parselmouth
|
||||
import torch
|
||||
import pdb
|
||||
from time import time as ttime
|
||||
import torch.nn.functional as F
|
||||
from config import x_pad, x_query, x_center, x_max
|
||||
import scipy.signal as signal
|
||||
import pyworld
|
||||
import os
|
||||
import traceback
|
||||
import faiss
|
||||
|
||||
|
||||
class VC(object):
|
||||
def __init__(self, tgt_sr, device, is_half, x_pad):
|
||||
self.sr = 16000 # hubert输入采样率
|
||||
self.window = 160 # 每帧点数
|
||||
self.t_pad = self.sr * x_pad # 每条前后pad时间
|
||||
print("INITIALIZE", self.sr, x_pad, self.t_pad)
|
||||
self.t_pad_tgt = tgt_sr * x_pad
|
||||
self.t_pad2 = self.t_pad * 2
|
||||
self.t_query = self.sr * x_query # 查询切点前后查询时间
|
||||
self.t_center = self.sr * x_center # 查询切点位置
|
||||
self.t_max = self.sr * x_max # 免查询时长阈值
|
||||
self.device = device
|
||||
self.is_half = is_half
|
||||
|
||||
def get_f0(self, x, p_len, f0_up_key, f0_method, inp_f0=None):
|
||||
time_step = self.window / self.sr * 1000
|
||||
# f0_min = 50
|
||||
# f0_max = 1100
|
||||
f0_min = 70
|
||||
f0_max = 1000
|
||||
f0_mel_min = 1127 * np.log(1 + f0_min / 700)
|
||||
f0_mel_max = 1127 * np.log(1 + f0_max / 700)
|
||||
if (f0_method == "pm"):
|
||||
f0 = parselmouth.Sound(x, self.sr).to_pitch_ac(
|
||||
time_step=time_step / 1000, voicing_threshold=0.6,
|
||||
pitch_floor=f0_min, pitch_ceiling=f0_max).selected_array['frequency']
|
||||
pad_size = (p_len - len(f0) + 1) // 2
|
||||
if (pad_size > 0 or p_len - len(f0) - pad_size > 0):
|
||||
f0 = np.pad(f0, [[pad_size, p_len - len(f0) - pad_size]], mode='constant')
|
||||
elif (f0_method == "harvest"):
|
||||
print("xlen", len(x))
|
||||
f0, t = pyworld.harvest(
|
||||
x.astype(np.double),
|
||||
fs=self.sr,
|
||||
f0_ceil=f0_max,
|
||||
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()
|
||||
f0_mel = 1127 * np.log(1 + f0 / 700)
|
||||
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * 254 / (f0_mel_max - f0_mel_min) + 1
|
||||
f0_mel[f0_mel <= 1] = 1
|
||||
f0_mel[f0_mel > 255] = 255
|
||||
f0_coarse = np.rint(f0_mel).astype(np.int)
|
||||
return f0_coarse, f0bak # 1-0
|
||||
|
||||
def vc(self, model, net_g, sid, audio0, pitch, pitchf, times, index, big_npy, index_rate): # ,file_index,file_big_npy
|
||||
print("vc audio len 1,", len(audio0))
|
||||
feats = torch.from_numpy(audio0)
|
||||
if (self.is_half == True):
|
||||
feats = feats.half()
|
||||
else:
|
||||
feats = feats.float()
|
||||
if feats.dim() == 2: # double channels
|
||||
feats = feats.mean(-1)
|
||||
assert feats.dim() == 1, feats.dim()
|
||||
feats = feats.view(1, -1)
|
||||
padding_mask = torch.BoolTensor(feats.shape).to(self.device).fill_(False)
|
||||
print("padding_mask", padding_mask)
|
||||
|
||||
inputs = {
|
||||
"source": feats.to(self.device),
|
||||
"padding_mask": padding_mask,
|
||||
"output_layer": 9, # layer 9
|
||||
}
|
||||
t0 = ttime()
|
||||
with torch.no_grad():
|
||||
logits = model.extract_features(**inputs)
|
||||
feats = model.final_proj(logits[0])
|
||||
|
||||
if (isinstance(index, type(None)) == False and isinstance(big_npy, type(None)) == False and index_rate != 0):
|
||||
npy = feats[0].cpu().numpy()
|
||||
if (self.is_half == True):
|
||||
npy = npy.astype("float32")
|
||||
D, I = index.search(npy, 1)
|
||||
npy = big_npy[I.squeeze()]
|
||||
if (self.is_half == True):
|
||||
npy = npy.astype("float16")
|
||||
feats = torch.from_numpy(npy).unsqueeze(0).to(self.device) * index_rate + (1 - index_rate) * feats
|
||||
|
||||
print("feats shape1", feats.shape)
|
||||
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
||||
print("feats shape2", feats.shape)
|
||||
t1 = ttime()
|
||||
p_len = audio0.shape[0] // self.window
|
||||
if (feats.shape[1] < p_len):
|
||||
p_len = feats.shape[1]
|
||||
if (pitch != None and pitchf != None):
|
||||
pitch = pitch[:, :p_len]
|
||||
pitchf = pitchf[:, :p_len]
|
||||
p_len = torch.tensor([p_len], device=self.device).long()
|
||||
with torch.no_grad():
|
||||
print("vc audio len feat 1,", feats.shape)
|
||||
if (pitch != None and pitchf != None):
|
||||
print("vc audio len feat use pitch!!!!!!!,", feats.shape)
|
||||
audio1 = (net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0] * 32768).data.cpu().float().numpy().astype(np.int16)
|
||||
else:
|
||||
audio1 = (net_g.infer(feats, p_len, sid)[0][0, 0] * 32768).data.cpu().float().numpy().astype(np.int16)
|
||||
del feats, p_len, padding_mask
|
||||
torch.cuda.empty_cache()
|
||||
t2 = ttime()
|
||||
times[0] += (t1 - t0)
|
||||
times[2] += (t2 - t1)
|
||||
print("vc audio return", len(audio1), audio1)
|
||||
return audio1
|
||||
|
||||
def pipeline(self, model, net_g, sid, audio, times, f0_up_key, f0_method, file_index, file_big_npy, index_rate, if_f0, f0_file=None):
|
||||
print("audio len 1,", len(audio))
|
||||
if (file_big_npy != "" and file_index != "" and os.path.exists(file_big_npy) == True and os.path.exists(file_index) == True and index_rate != 0):
|
||||
try:
|
||||
index = faiss.read_index(file_index)
|
||||
big_npy = np.load(file_big_npy)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
index = big_npy = None
|
||||
else:
|
||||
index = big_npy = None
|
||||
audio_pad = np.pad(audio, (self.window // 2, self.window // 2), mode='reflect')
|
||||
print("audio_pad len 1,", len(audio_pad))
|
||||
opt_ts = []
|
||||
# if (audio_pad.shape[0] > self.t_max):
|
||||
# audio_sum = np.zeros_like(audio)
|
||||
# for i in range(self.window):
|
||||
# audio_sum += audio_pad[i:i - self.window]
|
||||
# for t in range(self.t_center, audio.shape[0], self.t_center):
|
||||
# opt_ts.append(t - self.t_query + np.where(np.abs(audio_sum[t - self.t_query:t + self.t_query])
|
||||
# == np.abs(audio_sum[t - self.t_query:t + self.t_query]).min())[0][0])
|
||||
|
||||
print("audio_pad len 2,", len(audio_pad), opt_ts)
|
||||
|
||||
s = 0
|
||||
audio_opt = []
|
||||
t = None
|
||||
t1 = ttime()
|
||||
audio_pad = np.pad(audio, (self.t_pad, self.t_pad), mode='reflect')
|
||||
p_len = audio_pad.shape[0] // self.window
|
||||
inp_f0 = None
|
||||
print("audio_pad len 3,", len(audio_pad), self.t_pad, len(audio))
|
||||
|
||||
# if (hasattr(f0_file, 'name') == True):
|
||||
# print("load pitch !!!!!!!!!!!!", f0_file.name)
|
||||
# try:
|
||||
# with open(f0_file.name, "r")as f:
|
||||
# lines = f.read().strip("\n").split("\n")
|
||||
# inp_f0 = []
|
||||
# for line in lines:
|
||||
# inp_f0.append([float(i)for i in line.split(",")])
|
||||
# inp_f0 = np.array(inp_f0, dtype="float32")
|
||||
# except:
|
||||
# traceback.print_exc()
|
||||
sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()
|
||||
pitch, pitchf = None, None
|
||||
if (if_f0 == 1):
|
||||
pitch, pitchf = self.get_f0(audio_pad, p_len, f0_up_key, f0_method, inp_f0)
|
||||
print("pitch!", pitch)
|
||||
pitch = pitch[:p_len]
|
||||
pitchf = pitchf[:p_len]
|
||||
pitch = torch.tensor(pitch, device=self.device).unsqueeze(0).long()
|
||||
pitchf = torch.tensor(pitchf, device=self.device).unsqueeze(0).float()
|
||||
t2 = ttime()
|
||||
times[1] += (t2 - t1)
|
||||
print("opt start")
|
||||
# for t in opt_ts:
|
||||
# print("opt exec")
|
||||
# t = t // self.window * self.window
|
||||
# if (if_f0 == 1):
|
||||
# audio_opt.append(self.vc(model, net_g, sid, audio_pad[s:t + self.t_pad2 + self.window], pitch[:, s // self.window:(
|
||||
# t + self.t_pad2) // self.window], pitchf[:, s // self.window:(t + self.t_pad2) // self.window], times, index, big_npy, index_rate)[self.t_pad_tgt:-self.t_pad_tgt])
|
||||
# else:
|
||||
# audio_opt.append(self.vc(model, net_g, sid, audio_pad[s:t + self.t_pad2 + self.window],
|
||||
# None, None, times, index, big_npy, index_rate)[self.t_pad_tgt:-self.t_pad_tgt])
|
||||
# s = t
|
||||
print("opt end")
|
||||
if (if_f0 == 1):
|
||||
print("TTTTT", t, self.t_pad_tgt)
|
||||
# audio_opt.append(self.vc(model, net_g, sid, audio_pad[t:], pitch[:, t // self.window:]if t is not None else pitch, pitchf[:,
|
||||
# t // self.window:]if t is not None else pitchf, times, index, big_npy, index_rate)[self.t_pad_tgt:-self.t_pad_tgt])
|
||||
audio_opt.append(self.vc(model, net_g, sid, audio_pad[t:], pitch[:, t // self.window:]if t is not None else pitch, pitchf[:,
|
||||
t // self.window:]if t is not None else pitchf, times, index, big_npy, index_rate))
|
||||
else:
|
||||
audio_opt.append(self.vc(model, net_g, sid, audio_pad[t:], None, None, times, index, big_npy, index_rate)[self.t_pad_tgt:-self.t_pad_tgt])
|
||||
audio_opt = np.concatenate(audio_opt)
|
||||
del pitch, pitchf, sid
|
||||
torch.cuda.empty_cache()
|
||||
print("result", audio_opt)
|
||||
return audio_opt
|
Loading…
Reference in New Issue
Block a user