Add files via upload

This commit is contained in:
RVC-Boss 2024-01-18 00:31:02 +08:00 committed by GitHub
parent 30ce1f2e69
commit d86c86557e

View File

@ -1,7 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys,os import sys,os
inp_text= os.environ.get("inp_text") inp_text= os.environ.get("inp_text")
inp_wav_dir= os.environ.get("inp_wav_dir") inp_wav_dir= os.environ.get("inp_wav_dir")
exp_name= os.environ.get("exp_name") exp_name= os.environ.get("exp_name")
@ -9,7 +8,6 @@ i_part = os.environ.get("i_part")
all_parts= os.environ.get("all_parts") all_parts= os.environ.get("all_parts")
os.environ["CUDA_VISIBLE_DEVICES"]= os.environ.get("_CUDA_VISIBLE_DEVICES") os.environ["CUDA_VISIBLE_DEVICES"]= os.environ.get("_CUDA_VISIBLE_DEVICES")
from feature_extractor import cnhubert from feature_extractor import cnhubert
opt_dir= os.environ.get("opt_dir") opt_dir= os.environ.get("opt_dir")
cnhubert.cnhubert_base_path= os.environ.get("cnhubert_base_dir") cnhubert.cnhubert_base_path= os.environ.get("cnhubert_base_dir")
is_half=eval(os.environ.get("is_half","True")) is_half=eval(os.environ.get("is_half","True"))
@ -17,7 +15,6 @@ is_half = eval(os.environ.get("is_half", "True"))
import pdb,traceback,numpy as np,logging import pdb,traceback,numpy as np,logging
from scipy.io import wavfile from scipy.io import wavfile
import librosa,torch import librosa,torch
now_dir = os.getcwd() now_dir = os.getcwd()
sys.path.append(now_dir) sys.path.append(now_dir)
from my_utils import load_audio from my_utils import load_audio
@ -35,8 +32,6 @@ from my_utils import load_audio
from time import time as ttime from time import time as ttime
import shutil import shutil
def my_save(fea,path):#####fix issue: torch.save doesn't support chinese path def my_save(fea,path):#####fix issue: torch.save doesn't support chinese path
dir=os.path.dirname(path) dir=os.path.dirname(path)
name=os.path.basename(path) name=os.path.basename(path)
@ -44,7 +39,6 @@ def my_save(fea, path): #####fix issue: torch.save doesn't support chinese path
torch.save(fea,tmp_path) torch.save(fea,tmp_path)
shutil.move(tmp_path,"%s/%s"%(dir,name)) shutil.move(tmp_path,"%s/%s"%(dir,name))
hubert_dir="%s/4-cnhubert"%(opt_dir) hubert_dir="%s/4-cnhubert"%(opt_dir)
wav32dir="%s/5-wav32k"%(opt_dir) wav32dir="%s/5-wav32k"%(opt_dir)
os.makedirs(opt_dir,exist_ok=True) os.makedirs(opt_dir,exist_ok=True)
@ -55,38 +49,31 @@ maxx = 0.95
alpha=0.5 alpha=0.5
device="cuda:0" device="cuda:0"
model=cnhubert.get_model() model=cnhubert.get_model()
if is_half == True: if(is_half==True):
model=model.half().to(device) model=model.half().to(device)
else: else:
model = model.to(device) model = model.to(device)
def name2go(wav_name): def name2go(wav_name):
hubert_path="%s/%s.pt"%(hubert_dir,wav_name) hubert_path="%s/%s.pt"%(hubert_dir,wav_name)
if os.path.exists(hubert_path): if(os.path.exists(hubert_path)):return
return if(inp_wav_dir!=""):
wav_path="%s/%s"%(inp_wav_dir,wav_name) wav_path="%s/%s"%(inp_wav_dir,wav_name)
tmp_audio = load_audio(wav_path, 32000) tmp_audio = load_audio(wav_path, 32000)
tmp_max = np.abs(tmp_audio).max() tmp_max = np.abs(tmp_audio).max()
if tmp_max > 2.2: if tmp_max > 2.2:
print("%s-%s-%s-filtered" % (idx0, idx1, tmp_max)) print("%s-%s-%s-filtered" % (idx0, idx1, tmp_max))
return return
tmp_audio32 = (tmp_audio / tmp_max * (maxx * alpha * 32768)) + ( tmp_audio32 = (tmp_audio / tmp_max * (maxx * alpha*32768)) + ((1 - alpha)*32768) * tmp_audio
(1 - alpha) * 32768 tmp_audio = librosa.resample(
) * tmp_audio tmp_audio32, orig_sr=32000, target_sr=16000
tmp_audio = librosa.resample(tmp_audio32, orig_sr=32000, target_sr=16000) )
tensor_wav16 = torch.from_numpy(tmp_audio) tensor_wav16 = torch.from_numpy(tmp_audio)
if is_half == True: if (is_half == True):
tensor_wav16=tensor_wav16.half().to(device) tensor_wav16=tensor_wav16.half().to(device)
else: else:
tensor_wav16 = tensor_wav16.to(device) tensor_wav16 = tensor_wav16.to(device)
ssl = ( ssl=model.model(tensor_wav16.unsqueeze(0))["last_hidden_state"].transpose(1,2).cpu()#torch.Size([1, 768, 215])
model.model(tensor_wav16.unsqueeze(0))["last_hidden_state"] if np.isnan(ssl.detach().numpy()).sum()!= 0:return
.transpose(1, 2)
.cpu()
) # torch.Size([1, 768, 215])
if np.isnan(ssl.detach().numpy()).sum() != 0:
return
wavfile.write( wavfile.write(
"%s/%s"%(wav32dir,wav_name), "%s/%s"%(wav32dir,wav_name),
32000, 32000,
@ -95,7 +82,6 @@ def name2go(wav_name):
# torch.save(ssl,hubert_path ) # torch.save(ssl,hubert_path )
my_save(ssl,hubert_path ) my_save(ssl,hubert_path )
with open(inp_text,"r",encoding="utf8")as f: with open(inp_text,"r",encoding="utf8")as f:
lines=f.read().strip("\n").split("\n") lines=f.read().strip("\n").split("\n")