mirror of
https://github.com/fumiama/Retrieval-based-Voice-Conversion-WebUI.git
synced 2026-06-05 01:10:22 +08:00
chore(format): run black on dev (#94)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
a8783c6639
commit
d3add81469
@@ -28,6 +28,7 @@ def float_to_int16(audio: np.ndarray) -> np.ndarray:
|
|||||||
am = 32767 * 32768 // am
|
am = 32767 * 32768 // am
|
||||||
return np.multiply(audio, am).astype(np.int16)
|
return np.multiply(audio, am).astype(np.int16)
|
||||||
|
|
||||||
|
|
||||||
def float_np_array_to_wav_buf(wav: np.ndarray, sr: int, f32=False) -> BytesIO:
|
def float_np_array_to_wav_buf(wav: np.ndarray, sr: int, f32=False) -> BytesIO:
|
||||||
buf = BytesIO()
|
buf = BytesIO()
|
||||||
if f32:
|
if f32:
|
||||||
@@ -41,10 +42,12 @@ def float_np_array_to_wav_buf(wav: np.ndarray, sr: int, f32=False) -> BytesIO:
|
|||||||
buf.seek(0, 0)
|
buf.seek(0, 0)
|
||||||
return buf
|
return buf
|
||||||
|
|
||||||
|
|
||||||
def save_audio(path: str, audio: np.ndarray, sr: int, f32=False):
|
def save_audio(path: str, audio: np.ndarray, sr: int, f32=False):
|
||||||
with open(path, "wb") as f:
|
with open(path, "wb") as f:
|
||||||
f.write(float_np_array_to_wav_buf(audio, sr, f32).getbuffer())
|
f.write(float_np_array_to_wav_buf(audio, sr, f32).getbuffer())
|
||||||
|
|
||||||
|
|
||||||
def wav2(i: BytesIO, o: BufferedWriter, format: str):
|
def wav2(i: BytesIO, o: BufferedWriter, format: str):
|
||||||
inp = av.open(i, "r")
|
inp = av.open(i, "r")
|
||||||
format = video_format_dict.get(format, format)
|
format = video_format_dict.get(format, format)
|
||||||
@@ -65,12 +68,14 @@ def wav2(i: BytesIO, o: BufferedWriter, format: str):
|
|||||||
|
|
||||||
|
|
||||||
def load_audio(
|
def load_audio(
|
||||||
file: Union[str, BytesIO, Path],
|
file: Union[str, BytesIO, Path],
|
||||||
sr: Optional[int]=None,
|
sr: Optional[int] = None,
|
||||||
format: Optional[str]=None,
|
format: Optional[str] = None,
|
||||||
mono=True
|
mono=True,
|
||||||
) -> Union[np.ndarray, Tuple[np.ndarray, int]]:
|
) -> Union[np.ndarray, Tuple[np.ndarray, int]]:
|
||||||
if (isinstance(file, str) and not Path(file).exists()) or (isinstance(file, Path) and not file.exists()):
|
if (isinstance(file, str) and not Path(file).exists()) or (
|
||||||
|
isinstance(file, Path) and not file.exists()
|
||||||
|
):
|
||||||
raise FileNotFoundError(f"File not found: {file}")
|
raise FileNotFoundError(f"File not found: {file}")
|
||||||
rate = 0
|
rate = 0
|
||||||
|
|
||||||
@@ -78,12 +83,25 @@ def load_audio(
|
|||||||
audio_stream = next(s for s in container.streams if s.type == "audio")
|
audio_stream = next(s for s in container.streams if s.type == "audio")
|
||||||
channels = 1 if audio_stream.layout == "mono" else 2
|
channels = 1 if audio_stream.layout == "mono" else 2
|
||||||
container.seek(0)
|
container.seek(0)
|
||||||
resampler = AudioResampler(format="fltp", layout=audio_stream.layout, rate=sr) if sr is not None else None
|
resampler = (
|
||||||
|
AudioResampler(format="fltp", layout=audio_stream.layout, rate=sr)
|
||||||
|
if sr is not None
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
|
||||||
# Estimated maximum total number of samples to pre-allocate the array
|
# Estimated maximum total number of samples to pre-allocate the array
|
||||||
# AV stores length in microseconds by default
|
# AV stores length in microseconds by default
|
||||||
estimated_total_samples = int(container.duration * sr // 1_000_000) if sr is not None else 48000
|
estimated_total_samples = (
|
||||||
decoded_audio = np.zeros(estimated_total_samples + 1 if channels == 1 else (channels, estimated_total_samples + 1), dtype=np.float32)
|
int(container.duration * sr // 1_000_000) if sr is not None else 48000
|
||||||
|
)
|
||||||
|
decoded_audio = np.zeros(
|
||||||
|
(
|
||||||
|
estimated_total_samples + 1
|
||||||
|
if channels == 1
|
||||||
|
else (channels, estimated_total_samples + 1)
|
||||||
|
),
|
||||||
|
dtype=np.float32,
|
||||||
|
)
|
||||||
|
|
||||||
offset = 0
|
offset = 0
|
||||||
|
|
||||||
@@ -92,7 +110,9 @@ def load_audio(
|
|||||||
rate = 0
|
rate = 0
|
||||||
for frame in packet:
|
for frame in packet:
|
||||||
frame.pts = None # 清除时间戳,避免重新采样问题
|
frame.pts = None # 清除时间戳,避免重新采样问题
|
||||||
resampled_frames = resampler.resample(frame) if resampler is not None else [frame]
|
resampled_frames = (
|
||||||
|
resampler.resample(frame) if resampler is not None else [frame]
|
||||||
|
)
|
||||||
for resampled_frame in resampled_frames:
|
for resampled_frame in resampled_frames:
|
||||||
frame_data = resampled_frame.to_ndarray()
|
frame_data = resampled_frame.to_ndarray()
|
||||||
rate = resampled_frame.rate
|
rate = resampled_frame.rate
|
||||||
@@ -104,13 +124,16 @@ def load_audio(
|
|||||||
yield p.decode()
|
yield p.decode()
|
||||||
|
|
||||||
for r, frames_data in map(process_packet, frame_iter(container)):
|
for r, frames_data in map(process_packet, frame_iter(container)):
|
||||||
if not rate: rate = r
|
if not rate:
|
||||||
|
rate = r
|
||||||
for frame_data in frames_data:
|
for frame_data in frames_data:
|
||||||
end_index = offset + len(frame_data[0])
|
end_index = offset + len(frame_data[0])
|
||||||
|
|
||||||
# 检查 decoded_audio 是否有足够的空间,并在必要时调整大小
|
# 检查 decoded_audio 是否有足够的空间,并在必要时调整大小
|
||||||
if end_index > decoded_audio.shape[1]:
|
if end_index > decoded_audio.shape[1]:
|
||||||
decoded_audio = np.resize(decoded_audio, (decoded_audio.shape[0], end_index*4))
|
decoded_audio = np.resize(
|
||||||
|
decoded_audio, (decoded_audio.shape[0], end_index * 4)
|
||||||
|
)
|
||||||
|
|
||||||
np.copyto(decoded_audio[..., offset:end_index], frame_data)
|
np.copyto(decoded_audio[..., offset:end_index], frame_data)
|
||||||
offset += len(frame_data[0])
|
offset += len(frame_data[0])
|
||||||
@@ -126,7 +149,9 @@ def load_audio(
|
|||||||
return decoded_audio, rate
|
return decoded_audio, rate
|
||||||
|
|
||||||
|
|
||||||
def downsample_audio(input_path: str, output_path: str, format: str, br=128_000) -> None:
|
def downsample_audio(
|
||||||
|
input_path: str, output_path: str, format: str, br=128_000
|
||||||
|
) -> None:
|
||||||
"""
|
"""
|
||||||
default to 128kb/s (equivalent to -q:a 2)
|
default to 128kb/s (equivalent to -q:a 2)
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -244,11 +244,15 @@ def main():
|
|||||||
for i, chunk in enumerate(chunks):
|
for i, chunk in enumerate(chunks):
|
||||||
if len(chunk.shape) > 1:
|
if len(chunk.shape) > 1:
|
||||||
chunk = chunk.T
|
chunk = chunk.T
|
||||||
save_audio(os.path.join(
|
save_audio(
|
||||||
out,
|
os.path.join(
|
||||||
f"%s_%d.wav"
|
out,
|
||||||
% (os.path.basename(args.audio).rsplit(".", maxsplit=1)[0], i),
|
f"%s_%d.wav"
|
||||||
), chunk, sr)
|
% (os.path.basename(args.audio).rsplit(".", maxsplit=1)[0], i),
|
||||||
|
),
|
||||||
|
chunk,
|
||||||
|
sr,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -62,15 +62,24 @@ class PreProcess:
|
|||||||
tmp_audio = (tmp_audio / tmp_max * (self.max * self.alpha)) + (
|
tmp_audio = (tmp_audio / tmp_max * (self.max * self.alpha)) + (
|
||||||
1 - self.alpha
|
1 - self.alpha
|
||||||
) * tmp_audio
|
) * tmp_audio
|
||||||
save_audio("%s/%s_%s.wav" % (self.gt_wavs_dir, idx0, idx1), tmp_audio, self.sr, f32=True)
|
save_audio(
|
||||||
|
"%s/%s_%s.wav" % (self.gt_wavs_dir, idx0, idx1),
|
||||||
|
tmp_audio,
|
||||||
|
self.sr,
|
||||||
|
f32=True,
|
||||||
|
)
|
||||||
with open("%s/%s_%s.wav" % (self.wavs16k_dir, idx0, idx1), "wb") as f:
|
with open("%s/%s_%s.wav" % (self.wavs16k_dir, idx0, idx1), "wb") as f:
|
||||||
f.write(float_np_array_to_wav_buf(
|
f.write(
|
||||||
load_audio(
|
float_np_array_to_wav_buf(
|
||||||
float_np_array_to_wav_buf(tmp_audio, self.sr, f32=True),
|
load_audio(
|
||||||
sr=16000,
|
float_np_array_to_wav_buf(tmp_audio, self.sr, f32=True),
|
||||||
format="wav",
|
sr=16000,
|
||||||
)
|
format="wav",
|
||||||
, 16000, True).getbuffer())
|
),
|
||||||
|
16000,
|
||||||
|
True,
|
||||||
|
).getbuffer()
|
||||||
|
)
|
||||||
|
|
||||||
def pipeline(self, path, idx0):
|
def pipeline(self, path, idx0):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -133,11 +133,21 @@ def run(rank, n_gpus, hps: utils.HParams, logger: logging.Logger):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
dist.init_process_group(
|
dist.init_process_group(
|
||||||
backend="gloo" if os.name == "nt" or not torch.cuda.is_available() else "nccl", init_method="env://", world_size=n_gpus, rank=rank
|
backend=(
|
||||||
|
"gloo" if os.name == "nt" or not torch.cuda.is_available() else "nccl"
|
||||||
|
),
|
||||||
|
init_method="env://",
|
||||||
|
world_size=n_gpus,
|
||||||
|
rank=rank,
|
||||||
)
|
)
|
||||||
except:
|
except:
|
||||||
dist.init_process_group(
|
dist.init_process_group(
|
||||||
backend="gloo" if os.name == "nt" or not torch.cuda.is_available() else "nccl", init_method="env://?use_libuv=False", world_size=n_gpus, rank=rank
|
backend=(
|
||||||
|
"gloo" if os.name == "nt" or not torch.cuda.is_available() else "nccl"
|
||||||
|
),
|
||||||
|
init_method="env://?use_libuv=False",
|
||||||
|
world_size=n_gpus,
|
||||||
|
rank=rank,
|
||||||
)
|
)
|
||||||
torch.manual_seed(hps.train.seed)
|
torch.manual_seed(hps.train.seed)
|
||||||
if torch.cuda.is_available():
|
if torch.cuda.is_available():
|
||||||
@@ -243,13 +253,17 @@ def run(rank, n_gpus, hps: utils.HParams, logger: logging.Logger):
|
|||||||
if hasattr(net_g, "module"):
|
if hasattr(net_g, "module"):
|
||||||
logger.info(
|
logger.info(
|
||||||
net_g.module.load_state_dict(
|
net_g.module.load_state_dict(
|
||||||
torch.load(hps.pretrainG, map_location="cpu", weights_only=True)["model"]
|
torch.load(
|
||||||
|
hps.pretrainG, map_location="cpu", weights_only=True
|
||||||
|
)["model"]
|
||||||
)
|
)
|
||||||
) ##测试不加载优化器
|
) ##测试不加载优化器
|
||||||
else:
|
else:
|
||||||
logger.info(
|
logger.info(
|
||||||
net_g.load_state_dict(
|
net_g.load_state_dict(
|
||||||
torch.load(hps.pretrainG, map_location="cpu", weights_only=True)["model"]
|
torch.load(
|
||||||
|
hps.pretrainG, map_location="cpu", weights_only=True
|
||||||
|
)["model"]
|
||||||
)
|
)
|
||||||
) ##测试不加载优化器
|
) ##测试不加载优化器
|
||||||
if hps.pretrainD != "":
|
if hps.pretrainD != "":
|
||||||
@@ -258,13 +272,17 @@ def run(rank, n_gpus, hps: utils.HParams, logger: logging.Logger):
|
|||||||
if hasattr(net_d, "module"):
|
if hasattr(net_d, "module"):
|
||||||
logger.info(
|
logger.info(
|
||||||
net_d.module.load_state_dict(
|
net_d.module.load_state_dict(
|
||||||
torch.load(hps.pretrainD, map_location="cpu", weights_only=True)["model"]
|
torch.load(
|
||||||
|
hps.pretrainD, map_location="cpu", weights_only=True
|
||||||
|
)["model"]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
logger.info(
|
logger.info(
|
||||||
net_d.load_state_dict(
|
net_d.load_state_dict(
|
||||||
torch.load(hps.pretrainD, map_location="cpu", weights_only=True)["model"]
|
torch.load(
|
||||||
|
hps.pretrainD, map_location="cpu", weights_only=True
|
||||||
|
)["model"]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -208,8 +208,12 @@ class Predictor:
|
|||||||
sources = self.demix(mix.T)
|
sources = self.demix(mix.T)
|
||||||
opt = sources[0].T
|
opt = sources[0].T
|
||||||
if format in ["wav", "flac"]:
|
if format in ["wav", "flac"]:
|
||||||
save_audio("%s/vocal_%s.%s" % (vocal_root, basename, format), mix - opt, rate)
|
save_audio(
|
||||||
save_audio("%s/instrument_%s.%s" % (others_root, basename, format), opt, rate)
|
"%s/vocal_%s.%s" % (vocal_root, basename, format), mix - opt, rate
|
||||||
|
)
|
||||||
|
save_audio(
|
||||||
|
"%s/instrument_%s.%s" % (others_root, basename, format), opt, rate
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
path_vocal = "%s/vocal_%s.wav" % (vocal_root, basename)
|
path_vocal = "%s/vocal_%s.wav" % (vocal_root, basename)
|
||||||
path_other = "%s/instrument_%s.wav" % (others_root, basename)
|
path_other = "%s/instrument_%s.wav" % (others_root, basename)
|
||||||
|
|||||||
@@ -48,9 +48,7 @@ class AudioPre:
|
|||||||
self.mp = mp
|
self.mp = mp
|
||||||
self.model = model
|
self.model = model
|
||||||
|
|
||||||
def _path_audio_(
|
def _path_audio_(self, music_file, ins_root=None, vocal_root=None, format="flac"):
|
||||||
self, music_file, ins_root=None, vocal_root=None, format="flac"
|
|
||||||
):
|
|
||||||
if ins_root is None and vocal_root is None:
|
if ins_root is None and vocal_root is None:
|
||||||
return "No save root."
|
return "No save root."
|
||||||
name = os.path.basename(music_file)
|
name = os.path.basename(music_file)
|
||||||
@@ -134,10 +132,14 @@ class AudioPre:
|
|||||||
else:
|
else:
|
||||||
head = "instrument_"
|
head = "instrument_"
|
||||||
if format in ["wav", "flac"]:
|
if format in ["wav", "flac"]:
|
||||||
save_audio(os.path.join(
|
save_audio(
|
||||||
|
os.path.join(
|
||||||
ins_root,
|
ins_root,
|
||||||
head + "{}_{}.{}".format(name, self.data["agg"], format),
|
head + "{}_{}.{}".format(name, self.data["agg"], format),
|
||||||
), wav_instrument, self.mp.param["sr"])
|
),
|
||||||
|
wav_instrument,
|
||||||
|
self.mp.param["sr"],
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
path = os.path.join(
|
path = os.path.join(
|
||||||
ins_root, head + "{}_{}.wav".format(name, self.data["agg"])
|
ins_root, head + "{}_{}.wav".format(name, self.data["agg"])
|
||||||
@@ -162,10 +164,14 @@ class AudioPre:
|
|||||||
wav_vocals = spec_utils.cmb_spectrogram_to_wave(v_spec_m, self.mp)
|
wav_vocals = spec_utils.cmb_spectrogram_to_wave(v_spec_m, self.mp)
|
||||||
logger.info("%s vocals done" % name)
|
logger.info("%s vocals done" % name)
|
||||||
if format in ["wav", "flac"]:
|
if format in ["wav", "flac"]:
|
||||||
save_audio(os.path.join(
|
save_audio(
|
||||||
|
os.path.join(
|
||||||
vocal_root,
|
vocal_root,
|
||||||
head + "{}_{}.{}".format(name, self.data["agg"], format),
|
head + "{}_{}.{}".format(name, self.data["agg"], format),
|
||||||
), wav_vocals, self.mp.param["sr"])
|
),
|
||||||
|
wav_vocals,
|
||||||
|
self.mp.param["sr"],
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
path = os.path.join(
|
path = os.path.join(
|
||||||
vocal_root, head + "{}_{}.wav".format(name, self.data["agg"])
|
vocal_root, head + "{}_{}.wav".format(name, self.data["agg"])
|
||||||
|
|||||||
@@ -252,8 +252,12 @@ class VC:
|
|||||||
try:
|
try:
|
||||||
tgt_sr, audio_opt = opt
|
tgt_sr, audio_opt = opt
|
||||||
if format1 in ["wav", "flac"]:
|
if format1 in ["wav", "flac"]:
|
||||||
save_audio("%s/%s.%s"
|
save_audio(
|
||||||
% (opt_root, os.path.basename(path), format1), audio_opt, tgt_sr)
|
"%s/%s.%s"
|
||||||
|
% (opt_root, os.path.basename(path), format1),
|
||||||
|
audio_opt,
|
||||||
|
tgt_sr,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
path = "%s/%s.%s" % (
|
path = "%s/%s.%s" % (
|
||||||
opt_root,
|
opt_root,
|
||||||
@@ -261,7 +265,11 @@ class VC:
|
|||||||
format1,
|
format1,
|
||||||
)
|
)
|
||||||
with open(path, "wb") as outf:
|
with open(path, "wb") as outf:
|
||||||
wav2(float_np_array_to_wav_buf(audio_opt, tgt_sr), outf, format1)
|
wav2(
|
||||||
|
float_np_array_to_wav_buf(audio_opt, tgt_sr),
|
||||||
|
outf,
|
||||||
|
format1,
|
||||||
|
)
|
||||||
except:
|
except:
|
||||||
info += traceback.format_exc()
|
info += traceback.format_exc()
|
||||||
infos.append("%s->%s" % (os.path.basename(path), info))
|
infos.append("%s->%s" % (os.path.basename(path), info))
|
||||||
|
|||||||
@@ -166,9 +166,11 @@ class SineGenerator(torch.nn.Module):
|
|||||||
rad = f0 / self.sampling_rate * a
|
rad = f0 / self.sampling_rate * a
|
||||||
rad2 = torch.fmod(rad[:, :-1, -1:].float() + 0.5, 1.0) - 0.5
|
rad2 = torch.fmod(rad[:, :-1, -1:].float() + 0.5, 1.0) - 0.5
|
||||||
rad_acc = rad2.cumsum(dim=1).fmod(1.0).to(f0)
|
rad_acc = rad2.cumsum(dim=1).fmod(1.0).to(f0)
|
||||||
rad += F.pad(rad_acc, (0, 0, 1, 0), mode='constant')
|
rad += F.pad(rad_acc, (0, 0, 1, 0), mode="constant")
|
||||||
rad = rad.reshape(f0.shape[0], -1, 1)
|
rad = rad.reshape(f0.shape[0], -1, 1)
|
||||||
b = torch.arange(1, self.dim + 1, dtype=f0.dtype, device=f0.device).reshape(1, 1, -1)
|
b = torch.arange(1, self.dim + 1, dtype=f0.dtype, device=f0.device).reshape(
|
||||||
|
1, 1, -1
|
||||||
|
)
|
||||||
rad *= b
|
rad *= b
|
||||||
rand_ini = torch.rand(1, 1, self.dim, device=f0.device)
|
rand_ini = torch.rand(1, 1, self.dim, device=f0.device)
|
||||||
rand_ini[..., 0] = 0
|
rand_ini[..., 0] = 0
|
||||||
|
|||||||
@@ -20,4 +20,4 @@ wav, sr = librosa.load(wav_path, sr=sampling_rate)
|
|||||||
|
|
||||||
audio = model.infer(wav, sr, sampling_rate, sid, f0_method, f0_up_key)
|
audio = model.infer(wav, sr, sampling_rate, sid, f0_method, f0_up_key)
|
||||||
|
|
||||||
save_audio(out_path, audio, sampling_rate)
|
save_audio(out_path, audio, sampling_rate)
|
||||||
|
|||||||
3
web.py
3
web.py
@@ -144,12 +144,14 @@ outside_index_root = os.getenv("outside_index_root")
|
|||||||
names = [""]
|
names = [""]
|
||||||
index_paths = [""]
|
index_paths = [""]
|
||||||
|
|
||||||
|
|
||||||
def lookup_names(weight_root):
|
def lookup_names(weight_root):
|
||||||
global names
|
global names
|
||||||
for name in os.listdir(weight_root):
|
for name in os.listdir(weight_root):
|
||||||
if name.endswith(".pth"):
|
if name.endswith(".pth"):
|
||||||
names.append(name)
|
names.append(name)
|
||||||
|
|
||||||
|
|
||||||
def lookup_indices(index_root):
|
def lookup_indices(index_root):
|
||||||
global index_paths
|
global index_paths
|
||||||
for root, _, files in os.walk(index_root, topdown=False):
|
for root, _, files in os.walk(index_root, topdown=False):
|
||||||
@@ -157,6 +159,7 @@ def lookup_indices(index_root):
|
|||||||
if name.endswith(".index") and "trained" not in name:
|
if name.endswith(".index") and "trained" not in name:
|
||||||
index_paths.append(str(pathlib.Path(root, name)))
|
index_paths.append(str(pathlib.Path(root, name)))
|
||||||
|
|
||||||
|
|
||||||
lookup_names(weight_root)
|
lookup_names(weight_root)
|
||||||
lookup_indices(index_root)
|
lookup_indices(index_root)
|
||||||
lookup_indices(outside_index_root)
|
lookup_indices(outside_index_root)
|
||||||
|
|||||||
Reference in New Issue
Block a user