initial upload attempt
initial upload attempt
This commit is contained in:
1 parent
84c19ed5a9
commit
9fd928f6e7
527 files changed
+37359
No files matched your search
@@ -0,0 +1,237 @@
|
||||
import os
|
||||
import sys
|
||||
import numpy as np
|
||||
import pyworld
|
||||
import torchcrepe
|
||||
import torch
|
||||
import parselmouth
|
||||
import tqdm
|
||||
from multiprocessing import Process, cpu_count
|
||||
|
||||
current_directory = os.getcwd()
|
||||
sys.path.append(current_directory)
|
||||
|
||||
|
||||
from rvc.lib.utils import load_audio
|
||||
|
||||
|
||||
exp_dir = sys.argv[1]
|
||||
f0_method = sys.argv[2]
|
||||
num_processes = cpu_count()
|
||||
|
||||
try:
|
||||
hop_length = int(sys.argv[3])
|
||||
except ValueError:
|
||||
hop_length = 128
|
||||
|
||||
DoFormant = False
|
||||
Quefrency = 1.0
|
||||
Timbre = 1.0
|
||||
|
||||
|
||||
class FeatureInput:
|
||||
def __init__(self, sample_rate=16000, hop_size=160):
|
||||
self.fs = sample_rate
|
||||
self.hop = hop_size
|
||||
|
||||
self.f0_method_dict = self.get_f0_method_dict()
|
||||
|
||||
self.f0_bin = 256
|
||||
self.f0_max = 1100.0
|
||||
self.f0_min = 50.0
|
||||
self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700)
|
||||
self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700)
|
||||
|
||||
def mncrepe(self, method, x, p_len, hop_length):
|
||||
f0 = None
|
||||
torch_device_index = 0
|
||||
torch_device = (
|
||||
torch.device(f"cuda:{torch_device_index % torch.cuda.device_count()}")
|
||||
if torch.cuda.is_available()
|
||||
else (
|
||||
torch.device("mps")
|
||||
if torch.backends.mps.is_available()
|
||||
else torch.device("cpu")
|
||||
)
|
||||
)
|
||||
|
||||
audio = torch.from_numpy(x.astype(np.float32)).to(torch_device, copy=True)
|
||||
audio /= torch.quantile(torch.abs(audio), 0.999)
|
||||
audio = torch.unsqueeze(audio, dim=0)
|
||||
if audio.ndim == 2 and audio.shape[0] > 1:
|
||||
audio = torch.mean(audio, dim=0, keepdim=True).detach()
|
||||
audio = audio.detach()
|
||||
|
||||
if method == "crepe":
|
||||
pitch = torchcrepe.predict(
|
||||
audio,
|
||||
self.fs,
|
||||
hop_length,
|
||||
self.f0_min,
|
||||
self.f0_max,
|
||||
"full",
|
||||
batch_size=hop_length * 2,
|
||||
device=torch_device,
|
||||
pad=True,
|
||||
)
|
||||
p_len = p_len or x.shape[0] // hop_length
|
||||
source = np.array(pitch.squeeze(0).cpu().float().numpy())
|
||||
source[source < 0.001] = np.nan
|
||||
target = np.interp(
|
||||
np.arange(0, len(source) * p_len, len(source)) / p_len,
|
||||
np.arange(0, len(source)),
|
||||
source,
|
||||
)
|
||||
f0 = np.nan_to_num(target)
|
||||
|
||||
return f0
|
||||
|
||||
def get_pm(self, x, p_len):
|
||||
f0 = (
|
||||
parselmouth.Sound(x, self.fs)
|
||||
.to_pitch_ac(
|
||||
time_step=160 / 16000,
|
||||
voicing_threshold=0.6,
|
||||
pitch_floor=self.f0_min,
|
||||
pitch_ceiling=self.f0_max,
|
||||
)
|
||||
.selected_array["frequency"]
|
||||
)
|
||||
|
||||
return np.pad(
|
||||
f0,
|
||||
[
|
||||
[
|
||||
max(0, (p_len - len(f0) + 1) // 2),
|
||||
max(0, p_len - len(f0) - (p_len - len(f0) + 1) // 2),
|
||||
]
|
||||
],
|
||||
mode="constant",
|
||||
)
|
||||
|
||||
def get_harvest(self, x):
|
||||
f0_spectral = pyworld.harvest(
|
||||
x.astype(np.double),
|
||||
fs=self.fs,
|
||||
f0_ceil=self.f0_max,
|
||||
f0_floor=self.f0_min,
|
||||
frame_period=1000 * self.hop / self.fs,
|
||||
)
|
||||
return pyworld.stonemask(x.astype(np.double), *f0_spectral, self.fs)
|
||||
|
||||
def get_dio(self, x):
|
||||
f0_spectral = pyworld.dio(
|
||||
x.astype(np.double),
|
||||
fs=self.fs,
|
||||
f0_ceil=self.f0_max,
|
||||
f0_floor=self.f0_min,
|
||||
frame_period=1000 * self.hop / self.fs,
|
||||
)
|
||||
return pyworld.stonemask(x.astype(np.double), *f0_spectral, self.fs)
|
||||
|
||||
def get_rmvpe(self, x):
|
||||
if not hasattr(self, "model_rmvpe"):
|
||||
from rvc.lib.rmvpe import RMVPE
|
||||
|
||||
self.model_rmvpe = RMVPE(current_directory / "models" / "rvc_base" / "rmvpe.pt", is_half=False, device="cpu")
|
||||
return self.model_rmvpe.infer_from_audio(x, thred=0.03)
|
||||
|
||||
def get_f0_method_dict(self):
|
||||
return {
|
||||
"pm": self.get_pm,
|
||||
"harvest": self.get_harvest,
|
||||
"dio": self.get_dio,
|
||||
"rmvpe": self.get_rmvpe,
|
||||
}
|
||||
|
||||
def compute_f0(self, path, f0_method, hop_length):
|
||||
x = load_audio(path, self.fs)
|
||||
p_len = x.shape[0] // self.hop
|
||||
|
||||
if f0_method in self.f0_method_dict:
|
||||
f0 = (
|
||||
self.f0_method_dict[f0_method](x, p_len)
|
||||
if f0_method == "pm"
|
||||
else self.f0_method_dict[f0_method](x)
|
||||
)
|
||||
elif f0_method == "crepe":
|
||||
f0 = self.mncrepe(f0_method, x, p_len, hop_length)
|
||||
return f0
|
||||
|
||||
def coarse_f0(self, f0):
|
||||
f0_mel = 1127 * np.log(1 + f0 / 700)
|
||||
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - self.f0_mel_min) * (
|
||||
self.f0_bin - 2
|
||||
) / (self.f0_mel_max - self.f0_mel_min) + 1
|
||||
|
||||
# use 0 or 1
|
||||
f0_mel[f0_mel <= 1] = 1
|
||||
f0_mel[f0_mel > self.f0_bin - 1] = self.f0_bin - 1
|
||||
f0_coarse = np.rint(f0_mel).astype(int)
|
||||
assert f0_coarse.max() <= 255 and f0_coarse.min() >= 1, (
|
||||
f0_coarse.max(),
|
||||
f0_coarse.min(),
|
||||
)
|
||||
return f0_coarse
|
||||
|
||||
def process_paths(self, paths, f0_method, hop_length, thread_n):
|
||||
if len(paths) == 0:
|
||||
print("There are no paths to process.")
|
||||
return
|
||||
with tqdm.tqdm(total=len(paths), leave=True, position=thread_n) as pbar:
|
||||
description = f"Thread {thread_n} | Hop-Length {hop_length}"
|
||||
pbar.set_description(description)
|
||||
|
||||
for idx, (inp_path, opt_path1, opt_path2) in enumerate(paths):
|
||||
try:
|
||||
if os.path.exists(opt_path1 + ".npy") and os.path.exists(
|
||||
opt_path2 + ".npy"
|
||||
):
|
||||
pbar.update(1)
|
||||
continue
|
||||
|
||||
feature_pit = self.compute_f0(inp_path, f0_method, hop_length)
|
||||
np.save(
|
||||
opt_path2,
|
||||
feature_pit,
|
||||
allow_pickle=False,
|
||||
) # nsf
|
||||
coarse_pit = self.coarse_f0(feature_pit)
|
||||
np.save(
|
||||
opt_path1,
|
||||
coarse_pit,
|
||||
allow_pickle=False,
|
||||
) # ori
|
||||
pbar.update(1)
|
||||
except Exception as error:
|
||||
print(f"f0fail-{idx}-{inp_path}-{error}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
feature_input = FeatureInput()
|
||||
paths = []
|
||||
input_root = f"{exp_dir}/1_16k_wavs"
|
||||
output_root1 = f"{exp_dir}/2a_f0"
|
||||
output_root2 = f"{exp_dir}/2b-f0nsf"
|
||||
|
||||
os.makedirs(output_root1, exist_ok=True)
|
||||
os.makedirs(output_root2, exist_ok=True)
|
||||
for name in sorted(list(os.listdir(input_root))):
|
||||
input_path = f"{input_root}/{name}"
|
||||
if "spec" in input_path:
|
||||
continue
|
||||
output_path1 = f"{output_root1}/{name}"
|
||||
output_path2 = f"{output_root2}/{name}"
|
||||
paths.append([input_path, output_path1, output_path2])
|
||||
|
||||
processes = []
|
||||
print("Using f0 method: " + f0_method)
|
||||
for i in range(num_processes):
|
||||
p = Process(
|
||||
target=feature_input.process_paths,
|
||||
args=(paths[i::num_processes], f0_method, hop_length, i),
|
||||
)
|
||||
processes.append(p)
|
||||
p.start()
|
||||
for i in range(num_processes):
|
||||
processes[i].join()
|
||||
@@ -0,0 +1,94 @@
|
||||
import os
|
||||
import sys
|
||||
import tqdm
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
import soundfile as sf
|
||||
import numpy as np
|
||||
|
||||
now_dir = os.getcwd()
|
||||
sys.path.append(now_dir)
|
||||
from rvc.lib.utils import load_embedding
|
||||
|
||||
device = sys.argv[1]
|
||||
n_parts = int(sys.argv[2])
|
||||
i_part = int(sys.argv[3])
|
||||
i_gpu = sys.argv[4]
|
||||
exp_dir = sys.argv[5]
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = str(i_gpu)
|
||||
version = sys.argv[6]
|
||||
is_half = bool(sys.argv[7])
|
||||
embedder_model = sys.argv[8]
|
||||
|
||||
|
||||
wav_path = f"{exp_dir}/1_16k_wavs"
|
||||
out_path = f"{exp_dir}/3_feature256" if version == "v1" else f"{exp_dir}/3_feature768"
|
||||
os.makedirs(out_path, exist_ok=True)
|
||||
|
||||
|
||||
def read_wave(wav_path, normalize=False):
|
||||
wav, sr = sf.read(wav_path)
|
||||
assert sr == 16000
|
||||
feats = torch.from_numpy(wav)
|
||||
feats = feats.half() if is_half else feats.float()
|
||||
feats = feats.mean(-1) if feats.dim() == 2 else feats
|
||||
feats = feats.view(1, -1)
|
||||
if normalize:
|
||||
with torch.no_grad():
|
||||
feats = F.layer_norm(feats, feats.shape)
|
||||
return feats
|
||||
|
||||
|
||||
print("Starting feature extraction...")
|
||||
models, saved_cfg, task = load_embedding(embedder_model)
|
||||
model = models[0]
|
||||
model = model.to(device)
|
||||
if device not in ["mps", "cpu"]:
|
||||
model = model.half()
|
||||
model.eval()
|
||||
|
||||
todo = sorted(os.listdir(wav_path))[i_part::n_parts]
|
||||
n = max(1, len(todo) // 10)
|
||||
|
||||
if len(todo) == 0:
|
||||
print(
|
||||
"An error occurred in the feature extraction, make sure you have provided the audios correctly."
|
||||
)
|
||||
else:
|
||||
print(f"{len(todo)}")
|
||||
with tqdm.tqdm(total=len(todo)) as pbar:
|
||||
for idx, file in enumerate(todo):
|
||||
try:
|
||||
if file.endswith(".wav"):
|
||||
wav_file_path = os.path.join(wav_path, file)
|
||||
out_file_path = os.path.join(out_path, file.replace("wav", "npy"))
|
||||
|
||||
if os.path.exists(out_file_path):
|
||||
continue
|
||||
|
||||
feats = read_wave(wav_file_path, normalize=saved_cfg.task.normalize)
|
||||
padding_mask = torch.BoolTensor(feats.shape).fill_(False)
|
||||
inputs = {
|
||||
"source": feats.to(device),
|
||||
"padding_mask": padding_mask.to(device),
|
||||
"output_layer": 9 if version == "v1" else 12,
|
||||
}
|
||||
with torch.no_grad():
|
||||
logits = model.extract_features(**inputs)
|
||||
feats = (
|
||||
model.final_proj(logits[0])
|
||||
if version == "v1"
|
||||
else logits[0]
|
||||
)
|
||||
|
||||
feats = feats.squeeze(0).float().cpu().numpy()
|
||||
if np.isnan(feats).sum() == 0:
|
||||
np.save(out_file_path, feats, allow_pickle=False)
|
||||
else:
|
||||
print(f"{file} - contains nan")
|
||||
pbar.set_description(f"Processing {file} {feats.shape}")
|
||||
except Exception as error:
|
||||
print(error)
|
||||
pbar.update(1)
|
||||
|
||||
print("Feature extraction completed successfully!")
|
||||
@@ -0,0 +1,71 @@
|
||||
import os
|
||||
import json
|
||||
import pathlib
|
||||
from random import shuffle
|
||||
|
||||
from rvc.configs.config import Config
|
||||
|
||||
config = Config()
|
||||
current_directory = os.getcwd()
|
||||
|
||||
|
||||
def generate_config(rvc_version, sampling_rate, model_path):
|
||||
if rvc_version == "v1" or sampling_rate == "40000":
|
||||
config_path = f"v1/{sampling_rate}.json"
|
||||
else:
|
||||
config_path = f"v2/{sampling_rate}.json"
|
||||
config_save_path = os.path.join(model_path, "config.json")
|
||||
if not pathlib.Path(config_save_path).exists():
|
||||
with open(config_save_path, "w", encoding="utf-8") as f:
|
||||
json.dump(
|
||||
config.json_config[config_path],
|
||||
f,
|
||||
ensure_ascii=False,
|
||||
indent=4,
|
||||
sort_keys=True,
|
||||
)
|
||||
f.write("\n")
|
||||
|
||||
|
||||
def generate_filelist(f0_method, model_path, rvc_version, sampling_rate):
|
||||
gt_wavs_dir = f"{model_path}/0_gt_wavs"
|
||||
feature_dir = (
|
||||
f"{model_path}/3_feature256"
|
||||
if rvc_version == "v1"
|
||||
else f"{model_path}/3_feature768"
|
||||
)
|
||||
if f0_method:
|
||||
f0_dir = f"{model_path}/2a_f0"
|
||||
f0nsf_dir = f"{model_path}/2b-f0nsf"
|
||||
names = (
|
||||
set([name.split(".")[0] for name in os.listdir(gt_wavs_dir)])
|
||||
& set([name.split(".")[0] for name in os.listdir(feature_dir)])
|
||||
& set([name.split(".")[0] for name in os.listdir(f0_dir)])
|
||||
& set([name.split(".")[0] for name in os.listdir(f0nsf_dir)])
|
||||
)
|
||||
else:
|
||||
names = set([name.split(".")[0] for name in os.listdir(gt_wavs_dir)]) & set(
|
||||
[name.split(".")[0] for name in os.listdir(feature_dir)]
|
||||
)
|
||||
options = []
|
||||
for name in names:
|
||||
if f0_method:
|
||||
options.append(
|
||||
f"{gt_wavs_dir}/{name}.wav|{feature_dir}/{name}.npy|{f0_dir}/{name}.wav.npy|{f0nsf_dir}/{name}.wav.npy|0"
|
||||
)
|
||||
else:
|
||||
options.append(f"{gt_wavs_dir}/{name}.wav|{feature_dir}/{name}.npy|0")
|
||||
fea_dim = 256 if rvc_version == "v1" else 768
|
||||
if f0_method:
|
||||
for _ in range(2):
|
||||
options.append(
|
||||
f"{current_directory}/logs/mute/0_gt_wavs/mute{sampling_rate}.wav|{current_directory}/logs/mute/3_feature{fea_dim}/mute.npy|{current_directory}/logs/mute/2a_f0/mute.wav.npy|{current_directory}/logs/mute/2b-f0nsf/mute.wav.npy|0"
|
||||
)
|
||||
else:
|
||||
for _ in range(2):
|
||||
options.append(
|
||||
f"{current_directory}/logs/mute/0_gt_wavs/mute{sampling_rate}.wav|{current_directory}/logs/mute/3_feature{fea_dim}/mute.npy|0"
|
||||
)
|
||||
shuffle(options)
|
||||
with open(f"{model_path}/filelist.txt", "w") as f:
|
||||
f.write("\n".join(options))
|
||||
Reference in new issue
Block a user