Add files via upload

This commit is contained in:
RVC-Boss
2023-05-14 15:05:42 +08:00
committed by GitHub
parent 3909ce4a7b
commit 3b5a2298d7
6 changed files with 305 additions and 218 deletions

View File

@@ -2,18 +2,16 @@ import numpy as np, parselmouth, torch, pdb
from time import time as ttime
import torch.nn.functional as F
import scipy.signal as signal
import pyworld, os, traceback, faiss, librosa
import pyworld, os, traceback, faiss,librosa
from scipy import signal
from functools import lru_cache
bh, ah = signal.butter(N=5, Wn=48, btype="high", fs=16000)
input_audio_path2wav = {}
input_audio_path2wav={}
@lru_cache
def cache_harvest_f0(input_audio_path, fs, f0max, f0min, frame_period):
audio = input_audio_path2wav[input_audio_path]
def cache_harvest_f0(input_audio_path,fs,f0max,f0min,frame_period):
audio=input_audio_path2wav[input_audio_path]
f0, t = pyworld.harvest(
audio,
fs=fs,
@@ -24,6 +22,17 @@ def cache_harvest_f0(input_audio_path, fs, f0max, f0min, frame_period):
f0 = pyworld.stonemask(audio, f0, t, fs)
return f0
def change_rms(data1,sr1,data2,sr2,rate):#1是输入音频2是输出音频,rate是2的占比
# print(data1.max(),data2.max())
rms1 = librosa.feature.rms(y=data1, frame_length=sr1//2*2, hop_length=sr1//2)#每半秒一个点
rms2 = librosa.feature.rms(y=data2, frame_length=sr2//2*2, hop_length=sr2//2)
rms1=torch.from_numpy(rms1)
rms1=F.interpolate(rms1.unsqueeze(0), size=data2.shape[0],mode='linear').squeeze()
rms2=torch.from_numpy(rms2)
rms2=F.interpolate(rms2.unsqueeze(0), size=data2.shape[0],mode='linear').squeeze()
rms2=torch.max(rms2,torch.zeros_like(rms2)+1e-6)
data2*=(torch.pow(rms1,torch.tensor(1-rate))*torch.pow(rms2,torch.tensor(rate-1))).numpy()
return data2
class VC(object):
def __init__(self, tgt_sr, config):
@@ -44,16 +53,7 @@ class VC(object):
self.t_max = self.sr * self.x_max # 免查询时长阈值
self.device = config.device
def get_f0(
self,
input_audio_path,
x,
p_len,
f0_up_key,
f0_method,
filter_radius,
inp_f0=None,
):
def get_f0(self, input_audio_path,x, p_len, f0_up_key, f0_method,filter_radius, inp_f0=None):
global input_audio_path2wav
time_step = self.window / self.sr * 1000
f0_min = 50
@@ -77,9 +77,9 @@ class VC(object):
f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant"
)
elif f0_method == "harvest":
input_audio_path2wav[input_audio_path] = x.astype(np.double)
f0 = cache_harvest_f0(input_audio_path, self.sr, f0_max, f0_min, 10)
if filter_radius > 2:
input_audio_path2wav[input_audio_path]=x.astype(np.double)
f0=cache_harvest_f0(input_audio_path,self.sr,f0_max,f0_min,10)
if(filter_radius>2):
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()]))
@@ -118,6 +118,7 @@ class VC(object):
index,
big_npy,
index_rate,
version,
): # ,file_index,file_big_npy
feats = torch.from_numpy(audio0)
if self.is_half:
@@ -133,12 +134,12 @@ class VC(object):
inputs = {
"source": feats.to(self.device),
"padding_mask": padding_mask,
"output_layer": 9, # layer 9
"output_layer": 9if version=="v1"else 12,
}
t0 = ttime()
with torch.no_grad():
logits = model.extract_features(**inputs)
feats = model.final_proj(logits[0])
feats = model.final_proj(logits[0])if version=="v1"else logits[0]
if (
isinstance(index, type(None)) == False
@@ -176,14 +177,14 @@ class VC(object):
with torch.no_grad():
if pitch != None and pitchf != None:
audio1 = (
(net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0] * 32768)
(net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0])
.data.cpu()
.float()
.numpy()
)
else:
audio1 = (
(net_g.infer(feats, p_len, sid)[0][0, 0] * 32768)
(net_g.infer(feats, p_len, sid)[0][0, 0])
.data.cpu()
.float()
.numpy()
@@ -213,6 +214,8 @@ class VC(object):
filter_radius,
tgt_sr,
resample_sr,
rms_mix_rate,
version,
f0_file=None,
):
if (
@@ -267,15 +270,7 @@ class VC(object):
sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()
pitch, pitchf = None, None
if if_f0 == 1:
pitch, pitchf = self.get_f0(
input_audio_path,
audio_pad,
p_len,
f0_up_key,
f0_method,
filter_radius,
inp_f0,
)
pitch, pitchf = self.get_f0(input_audio_path,audio_pad, p_len, f0_up_key, f0_method,filter_radius, inp_f0)
pitch = pitch[:p_len]
pitchf = pitchf[:p_len]
if self.device == "mps":
@@ -299,6 +294,7 @@ class VC(object):
index,
big_npy,
index_rate,
version,
)[self.t_pad_tgt : -self.t_pad_tgt]
)
else:
@@ -314,6 +310,7 @@ class VC(object):
index,
big_npy,
index_rate,
version,
)[self.t_pad_tgt : -self.t_pad_tgt]
)
s = t
@@ -330,6 +327,7 @@ class VC(object):
index,
big_npy,
index_rate,
version,
)[self.t_pad_tgt : -self.t_pad_tgt]
)
else:
@@ -345,14 +343,20 @@ class VC(object):
index,
big_npy,
index_rate,
version,
)[self.t_pad_tgt : -self.t_pad_tgt]
)
audio_opt = np.concatenate(audio_opt)
if resample_sr >= 16000 and tgt_sr != resample_sr:
if(rms_mix_rate!=1):
audio_opt=change_rms(audio,16000,audio_opt,tgt_sr,rms_mix_rate)
if(resample_sr>=16000 and tgt_sr!=resample_sr):
audio_opt = librosa.resample(
audio_opt, orig_sr=tgt_sr, target_sr=resample_sr
)
audio_opt = audio_opt.astype(np.int16)
audio_max=np.abs(audio_opt).max()/0.99
max_int16=32768
if(audio_max>1):max_int16/=audio_max
audio_opt=(audio_opt * max_int16).astype(np.int16)
del pitch, pitchf, sid
if torch.cuda.is_available():
torch.cuda.empty_cache()