so-vits-svc/inference_main.py

142 lines
8.3 KiB
Python
Raw Normal View History

2023-03-10 10:11:04 +00:00
import io
import logging
import time
from pathlib import Path
import librosa
import matplotlib.pyplot as plt
import numpy as np
import soundfile
from inference import infer_tool
from inference import slicer
from inference.infer_tool import Svc
logging.getLogger('numba').setLevel(logging.WARNING)
chunks_dict = infer_tool.read_temp("inference/chunks_temp.json")
def main():
import argparse
parser = argparse.ArgumentParser(description='sovits4 inference')
# 一定要设置的部分
parser.add_argument('-m', '--model_path', type=str, default="logs/44k/G_0.pth", help='模型路径')
parser.add_argument('-c', '--config_path', type=str, default="configs/config.json", help='配置文件路径')
2023-04-04 14:44:55 +00:00
parser.add_argument('-cl', '--clip', type=float, default=0, help='音频强制切片默认0为自动切片单位为秒/s')
2023-03-10 10:11:04 +00:00
parser.add_argument('-n', '--clean_names', type=str, nargs='+', default=["君の知らない物語-src.wav"], help='wav文件名列表放在raw文件夹下')
parser.add_argument('-t', '--trans', type=int, nargs='+', default=[0], help='音高调整,支持正负(半音)')
parser.add_argument('-s', '--spk_list', type=str, nargs='+', default=['nen'], help='合成目标说话人名称')
# 可选项部分
2023-04-14 05:29:19 +00:00
parser.add_argument('-a', '--auto_predict_f0', type=bool, default=False, help='语音转换自动预测音高,转换歌声时不要打开这个会严重跑调')
2023-03-10 10:11:04 +00:00
parser.add_argument('-cm', '--cluster_model_path', type=str, default="logs/44k/kmeans_10000.pt", help='聚类模型路径,如果没有训练聚类则随便填')
2023-04-04 14:44:55 +00:00
parser.add_argument('-cr', '--cluster_infer_ratio', type=float, default=0, help='聚类方案占比范围0-1若没有训练聚类模型则默认0即可')
parser.add_argument('-lg', '--linear_gradient', type=float, default=0, help='两段音频切片的交叉淡入长度如果强制切片后出现人声不连贯可调整该数值如果连贯建议采用默认值0单位为秒')
2023-04-05 12:02:32 +00:00
parser.add_argument('-fmp', '--f0_mean_pooling', type=bool, default=False, help='是否对F0使用均值滤波器(池化),对部分哑音有改善。注意,启动该选项会导致推理速度下降,默认关闭')
2023-04-08 17:01:48 +00:00
parser.add_argument('-eh', '--enhance', type=bool, default=False, help='是否使用NSF_HIFIGAN增强器,该选项对部分训练集少的模型有一定的音质增强效果,但是对训练好的模型有反面效果,默认关闭')
2023-04-14 05:29:19 +00:00
2023-03-10 10:11:04 +00:00
# 不用动的部分
parser.add_argument('-sd', '--slice_db', type=int, default=-40, help='默认-40嘈杂的音频可以-30干声保留呼吸可以-50')
parser.add_argument('-d', '--device', type=str, default=None, help='推理设备None则为自动选择cpu和gpu')
parser.add_argument('-ns', '--noice_scale', type=float, default=0.4, help='噪音级别,会影响咬字和音质,较为玄学')
parser.add_argument('-p', '--pad_seconds', type=float, default=0.5, help='推理音频pad秒数由于未知原因开头结尾会有异响pad一小段静音段后就不会出现')
parser.add_argument('-wf', '--wav_format', type=str, default='flac', help='音频输出格式')
2023-03-20 08:26:40 +00:00
parser.add_argument('-lgr', '--linear_gradient_retain', type=float, default=0.75, help='自动音频切片后需要舍弃每段切片的头尾。该参数设置交叉长度保留的比例范围0-1,左开右闭')
2023-04-08 17:01:48 +00:00
parser.add_argument('-eak', '--enhancer_adaptive_key', type=int, default=0, help='使增强器适应更高的音域(单位为半音数)|默认为0')
2023-04-17 10:31:15 +00:00
parser.add_argument('-ft', '--F0_filter_threshold', type=float, default=0.05,help='F0过滤阈值只有启动f0_mean_pooling时有效. 数值范围从0-1. 降低该值可减少跑调概率,但会增加哑音')
2023-03-10 10:11:04 +00:00
args = parser.parse_args()
clean_names = args.clean_names
trans = args.trans
spk_list = args.spk_list
slice_db = args.slice_db
wav_format = args.wav_format
auto_predict_f0 = args.auto_predict_f0
cluster_infer_ratio = args.cluster_infer_ratio
noice_scale = args.noice_scale
pad_seconds = args.pad_seconds
2023-03-20 06:08:55 +00:00
clip = args.clip
lg = args.linear_gradient
2023-03-20 08:26:40 +00:00
lgr = args.linear_gradient_retain
2023-04-04 07:47:05 +00:00
F0_mean_pooling = args.f0_mean_pooling
2023-04-08 17:01:48 +00:00
enhance = args.enhance
enhancer_adaptive_key = args.enhancer_adaptive_key
2023-04-17 10:31:15 +00:00
cr_threshold = args.F0_filter_threshold
2023-04-08 17:01:48 +00:00
svc_model = Svc(args.model_path, args.config_path, args.device, args.cluster_model_path,enhance)
infer_tool.mkdir(["raw", "results"])
2023-03-10 10:11:04 +00:00
infer_tool.fill_a_to_b(trans, clean_names)
for clean_name, tran in zip(clean_names, trans):
raw_audio_path = f"raw/{clean_name}"
if "." not in raw_audio_path:
raw_audio_path += ".wav"
infer_tool.format_wav(raw_audio_path)
wav_path = Path(raw_audio_path).with_suffix('.wav')
chunks = slicer.cut(wav_path, db_thresh=slice_db)
audio_data, audio_sr = slicer.chunks2audio(wav_path, chunks)
2023-03-20 06:08:55 +00:00
per_size = int(clip*audio_sr)
lg_size = int(lg*audio_sr)
2023-03-20 08:26:40 +00:00
lg_size_r = int(lg_size*lgr)
lg_size_c_l = (lg_size-lg_size_r)//2
lg_size_c_r = lg_size-lg_size_r-lg_size_c_l
lg = np.linspace(0,1,lg_size_r) if lg_size!=0 else 0
2023-03-10 10:11:04 +00:00
for spk in spk_list:
audio = []
for (slice_tag, data) in audio_data:
print(f'#=====segment start, {round(len(data) / audio_sr, 3)}s======')
2023-03-20 08:26:40 +00:00
2023-03-10 10:11:04 +00:00
length = int(np.ceil(len(data) / audio_sr * svc_model.target_sample))
if slice_tag:
print('jump empty segment')
_audio = np.zeros(length)
2023-03-20 06:08:55 +00:00
audio.extend(list(infer_tool.pad_array(_audio, length)))
continue
if per_size != 0:
datas = infer_tool.split_list_by_n(data, per_size,lg_size)
2023-03-10 10:11:04 +00:00
else:
2023-03-20 06:08:55 +00:00
datas = [data]
for k,dat in enumerate(datas):
per_length = int(np.ceil(len(dat) / audio_sr * svc_model.target_sample)) if clip!=0 else length
if clip!=0: print(f'###=====segment clip start, {round(len(dat) / audio_sr, 3)}s======')
2023-03-10 10:11:04 +00:00
# padd
pad_len = int(audio_sr * pad_seconds)
2023-03-20 06:08:55 +00:00
dat = np.concatenate([np.zeros([pad_len]), dat, np.zeros([pad_len])])
2023-03-10 10:11:04 +00:00
raw_path = io.BytesIO()
2023-03-20 06:08:55 +00:00
soundfile.write(raw_path, dat, audio_sr, format="wav")
2023-03-10 10:11:04 +00:00
raw_path.seek(0)
out_audio, out_sr = svc_model.infer(spk, tran, raw_path,
cluster_infer_ratio=cluster_infer_ratio,
auto_predict_f0=auto_predict_f0,
2023-04-04 07:47:05 +00:00
noice_scale=noice_scale,
2023-04-08 17:01:48 +00:00
F0_mean_pooling = F0_mean_pooling,
2023-04-17 10:31:15 +00:00
enhancer_adaptive_key = enhancer_adaptive_key,
cr_threshold = cr_threshold
2023-03-10 10:11:04 +00:00
)
_audio = out_audio.cpu().numpy()
pad_len = int(svc_model.target_sample * pad_seconds)
_audio = _audio[pad_len:-pad_len]
2023-03-20 06:08:55 +00:00
_audio = infer_tool.pad_array(_audio, per_length)
if lg_size!=0 and k!=0:
2023-03-20 08:26:40 +00:00
lg1 = audio[-(lg_size_r+lg_size_c_r):-lg_size_c_r] if lgr != 1 else audio[-lg_size:]
lg2 = _audio[lg_size_c_l:lg_size_c_l+lg_size_r] if lgr != 1 else _audio[0:lg_size]
2023-03-20 06:08:55 +00:00
lg_pre = lg1*(1-lg)+lg2*lg
2023-03-20 08:26:40 +00:00
audio = audio[0:-(lg_size_r+lg_size_c_r)] if lgr != 1 else audio[0:-lg_size]
2023-03-20 06:08:55 +00:00
audio.extend(lg_pre)
2023-03-20 08:26:40 +00:00
_audio = _audio[lg_size_c_l+lg_size_r:] if lgr != 1 else _audio[lg_size:]
2023-03-20 06:08:55 +00:00
audio.extend(list(_audio))
2023-03-10 10:11:04 +00:00
key = "auto" if auto_predict_f0 else f"{tran}key"
cluster_name = "" if cluster_infer_ratio == 0 else f"_{cluster_infer_ratio}"
res_path = f'./results/{clean_name}_{key}_{spk}{cluster_name}.{wav_format}'
soundfile.write(res_path, audio, svc_model.target_sample, format=wav_format)
2023-04-08 17:01:48 +00:00
svc_model.clear_empty()
2023-03-10 10:11:04 +00:00
if __name__ == '__main__':
main()