Mirror lj1995/VoiceConversionWebUI @ b2c8cae96e3b — infer.py
Browse files
infer.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch, pdb, os,sys,librosa,warnings,traceback
|
| 2 |
+
warnings.filterwarnings("ignore")
|
| 3 |
+
torch.manual_seed(114514)
|
| 4 |
+
sys.path.append(os.getcwd())
|
| 5 |
+
from config import inp_root,opt_root,f0_up_key,person,is_half,device
|
| 6 |
+
os.makedirs(opt_root,exist_ok=True)
|
| 7 |
+
import soundfile as sf
|
| 8 |
+
from infer_pack.models import SynthesizerTrnMs256NSF as SynthesizerTrn256
|
| 9 |
+
from scipy.io import wavfile
|
| 10 |
+
from fairseq import checkpoint_utils
|
| 11 |
+
import scipy.signal as signal
|
| 12 |
+
from vc_infer_pipeline import VC
|
| 13 |
+
|
| 14 |
+
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(["hubert_base.pt"],suffix="",)
|
| 15 |
+
model = models[0]
|
| 16 |
+
model = model.to(device)
|
| 17 |
+
if(is_half):model = model.half()
|
| 18 |
+
else:model = model.float()
|
| 19 |
+
model.eval()
|
| 20 |
+
|
| 21 |
+
cpt=torch.load(person,map_location="cpu")
|
| 22 |
+
dv=cpt["dv"]
|
| 23 |
+
tgt_sr=cpt["config"][-1]
|
| 24 |
+
net_g = SynthesizerTrn256(*cpt["config"],is_half=is_half)
|
| 25 |
+
net_g.load_state_dict(cpt["weight"],strict=True)
|
| 26 |
+
net_g.eval().to(device)
|
| 27 |
+
if(is_half):net_g = net_g.half()
|
| 28 |
+
else:net_g = net_g.float()
|
| 29 |
+
|
| 30 |
+
vc=VC(tgt_sr,device,is_half)
|
| 31 |
+
|
| 32 |
+
for name in os.listdir(inp_root):
|
| 33 |
+
try:
|
| 34 |
+
wav_path="%s\%s"%(inp_root,name)
|
| 35 |
+
print("processing %s"%wav_path)
|
| 36 |
+
audio, sampling_rate = sf.read(wav_path)
|
| 37 |
+
if len(audio.shape) > 1:
|
| 38 |
+
audio = librosa.to_mono(audio.transpose(1, 0))
|
| 39 |
+
if sampling_rate != vc.sr:
|
| 40 |
+
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=vc.sr)
|
| 41 |
+
|
| 42 |
+
times = [0, 0, 0]
|
| 43 |
+
audio_opt=vc.pipeline(model,net_g,dv,audio,times,f0_up_key,f0_file=None)
|
| 44 |
+
wavfile.write("%s/%s"%(opt_root,name), tgt_sr, audio_opt)
|
| 45 |
+
except:
|
| 46 |
+
traceback.print_exc()
|
| 47 |
+
|
| 48 |
+
print(times)
|