diff --git a/gui.py b/gui.py index 054c188..f2b3e2a 100644 --- a/gui.py +++ b/gui.py @@ -3,8 +3,10 @@ import sounddevice as sd import noisereduce as nr import numpy as np from fairseq import checkpoint_utils -import librosa,torch,parselmouth,faiss,time,threading +import librosa,torch,parselmouth,faiss,time,threading,math import torch.nn.functional as F +import torchaudio.transforms as tat + #import matplotlib.pyplot as plt from infer_pack.models import SynthesizerTrnMs256NSFsid, SynthesizerTrnMs256NSFsid_nono from webui_locale import I18nAuto @@ -85,7 +87,7 @@ class RVC: audio = librosa.to_mono(audio.transpose(1, 0)) if sampling_rate != 16000: audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000) - print('test:audio:'+str(audio.shape)) + #print('test:audio:'+str(audio.shape)) '''padding''' @@ -147,7 +149,8 @@ class Config: self.threhold:int=-30 self.crossfade_time:float=0.08 self.extra_time:float=0.04 - self.noise_reduce=False + self.I_noise_reduce=False + self.O_noise_reduce=False class GUI: def __init__(self) -> None: @@ -162,6 +165,7 @@ class GUI: layout=[ [ sg.Frame(title=i18n('加载模型'),layout=[ + [sg.Input(default_text='TEMP\\hubert_base.pt',key='hubert_path'),sg.FileBrowse(i18n('Hubert File'))], [sg.Input(default_text='TEMP\\atri.pth',key='pth_path'),sg.FileBrowse(i18n('选择.pth文件'))], [sg.Input(default_text='TEMP\\added_IVF512_Flat_atri_baseline_src_feat.index',key='index_path'),sg.FileBrowse(i18n('选择.index文件'))], [sg.Input(default_text='TEMP\\big_src_feature_atri.npy',key='npy_path'),sg.FileBrowse(i18n('选择.npy文件'))] @@ -183,10 +187,10 @@ class GUI: [sg.Text(i18n("采样长度")),sg.Slider(range=(0.1,3.0),key='block_time',resolution=0.1,orientation='h',default_value=1.0)], [sg.Text(i18n("淡入淡出长度")),sg.Slider(range=(0.01,0.15),key='crossfade_length',resolution=0.01,orientation='h',default_value=0.08)], [sg.Text(i18n("额外推理时长")),sg.Slider(range=(0.05,3.00),key='extra_time',resolution=0.01,orientation='h',default_value=0.05)], - [sg.Checkbox(i18n('输出降噪/Output Noisereduce'),key='noise_reduce')] + [sg.Checkbox(i18n('Input Noisereduce'),key='I_noise_reduce'),sg.Checkbox(i18n('Output Noisereduce'),key='O_noise_reduce')] ],title=i18n("性能设置")) ], - [sg.Button(i18n("开始音频转换"),key='start_vc'),sg.Button(i18n("停止音频转换"),key='stop_vc')] + [sg.Button(i18n("开始音频转换"),key='start_vc'),sg.Button(i18n("停止音频转换"),key='stop_vc'),sg.Text(i18n("Infer Time(ms):")),sg.Text("0",key='infer_time')] ] self.window=sg.Window("RVC - GUI",layout=layout) @@ -219,11 +223,13 @@ class GUI: self.config.block_time=values['block_time'] self.config.crossfade_time=values['crossfade_length'] self.config.extra_time=values['extra_time'] - self.config.noise_reduce=values['noise_reduce'] + self.config.I_noise_reduce=values['I_noise_reduce'] + self.config.O_noise_reduce=values['O_noise_reduce'] def start_vc(self): torch.cuda.empty_cache() self.flag_vc=True + self.RMS_threhold=math.e**(float(self.config.threhold)/10) 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) @@ -231,11 +237,15 @@ class GUI: self.extra_frame=int(self.config.extra_time*self.config.samplerate)#往后预留0.04s self.rvc=None self.rvc=RVC(self.config.pitch,self.config.pth_path,self.config.index_path,self.config.npy_path) - self.input_wav:np.ndarray=np.zeros(self.extra_frame+self.crossfade_frame+self.sola_search_frame+self.block_frame) - self.output_wav:np.ndarray=np.zeros(self.block_frame) - self.sola_buffer:np.ndarray=np.zeros(self.crossfade_frame,dtype='float32') - self.fade_in_window:np.ndarray = np.linspace(0, 1, self.crossfade_frame) - self.fade_out_window:np.ndarray = 1 - self.fade_in_window + 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:np.ndarray=np.zeros(self.crossfade_frame,dtype='float32') + self.sola_buffer:torch.Tensor=torch.zeros(self.crossfade_frame,device=device,dtype=torch.float32) + #self.fade_in_window:np.ndarray = np.linspace(0, 1, self.crossfade_frame) + 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.resampler=tat.Resample(orig_freq=40000,new_freq=self.config.samplerate,dtype=torch.float32) + self.RMS=lambda y:torch.sqrt(torch.mean(torch.square(y))).item()#RMS calculator thread_vc=threading.Thread(target=self.soundinput) thread_vc.start() @@ -257,46 +267,48 @@ class GUI: ''' start_time=time.perf_counter() indata=librosa.to_mono(indata.T) - self.input_wav[:]=np.roll(self.input_wav,-self.block_frame) - - #TODO:Convert all numpy calculation to torch + if self.config.I_noise_reduce: + indata[:]=nr.reduce_noise(y=indata,sr=self.config.samplerate) + '''noise gate''' - frame_length=1024 - hop_length=512 + 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]