2023-04-15 19:44:24 +08:00
|
|
|
import os, traceback, sys, parselmouth
|
2023-05-14 15:52:36 +08:00
|
|
|
|
2023-05-14 15:05:42 +08:00
|
|
|
now_dir = os.getcwd()
|
|
|
|
sys.path.append(now_dir)
|
|
|
|
from my_utils import load_audio
|
2023-03-31 17:54:38 +08:00
|
|
|
import pyworld
|
2023-04-15 19:44:24 +08:00
|
|
|
import numpy as np, logging
|
|
|
|
|
|
|
|
logging.getLogger("numba").setLevel(logging.WARNING)
|
2023-03-31 17:54:38 +08:00
|
|
|
from multiprocessing import Process
|
|
|
|
|
|
|
|
exp_dir = sys.argv[1]
|
2023-04-15 19:44:24 +08:00
|
|
|
f = open("%s/extract_f0_feature.log" % exp_dir, "a+")
|
|
|
|
|
|
|
|
|
2023-03-31 17:54:38 +08:00
|
|
|
def printt(strr):
|
|
|
|
print(strr)
|
|
|
|
f.write("%s\n" % strr)
|
|
|
|
f.flush()
|
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
|
2023-03-31 17:54:38 +08:00
|
|
|
n_p = int(sys.argv[2])
|
|
|
|
f0method = sys.argv[3]
|
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
|
2023-03-31 17:54:38 +08:00
|
|
|
class FeatureInput(object):
|
|
|
|
def __init__(self, samplerate=16000, hop_size=160):
|
|
|
|
self.fs = samplerate
|
|
|
|
self.hop = hop_size
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
def compute_f0(self, path, f0_method):
|
2023-05-14 15:52:36 +08:00
|
|
|
x = load_audio(path, self.fs)
|
2023-04-15 19:44:24 +08:00
|
|
|
p_len = x.shape[0] // self.hop
|
|
|
|
if f0_method == "pm":
|
2023-03-31 17:54:38 +08:00
|
|
|
time_step = 160 / 16000 * 1000
|
|
|
|
f0_min = 50
|
|
|
|
f0_max = 1100
|
2023-04-15 19:44:24 +08:00
|
|
|
f0 = (
|
2023-05-14 15:05:42 +08:00
|
|
|
parselmouth.Sound(x, self.fs)
|
2023-04-15 19:44:24 +08:00
|
|
|
.to_pitch_ac(
|
|
|
|
time_step=time_step / 1000,
|
|
|
|
voicing_threshold=0.6,
|
|
|
|
pitch_floor=f0_min,
|
|
|
|
pitch_ceiling=f0_max,
|
|
|
|
)
|
|
|
|
.selected_array["frequency"]
|
|
|
|
)
|
|
|
|
pad_size = (p_len - len(f0) + 1) // 2
|
|
|
|
if pad_size > 0 or p_len - len(f0) - pad_size > 0:
|
|
|
|
f0 = np.pad(
|
|
|
|
f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant"
|
|
|
|
)
|
|
|
|
elif f0_method == "harvest":
|
2023-03-31 17:54:38 +08:00
|
|
|
f0, t = pyworld.harvest(
|
|
|
|
x.astype(np.double),
|
2023-05-14 15:05:42 +08:00
|
|
|
fs=self.fs,
|
2023-04-13 23:57:27 +08:00
|
|
|
f0_ceil=self.f0_max,
|
|
|
|
f0_floor=self.f0_min,
|
2023-05-14 15:05:42 +08:00
|
|
|
frame_period=1000 * self.hop / self.fs,
|
2023-03-31 17:54:38 +08:00
|
|
|
)
|
|
|
|
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.fs)
|
2023-04-15 19:44:24 +08:00
|
|
|
elif f0_method == "dio":
|
2023-03-31 17:54:38 +08:00
|
|
|
f0, t = pyworld.dio(
|
|
|
|
x.astype(np.double),
|
2023-05-14 15:05:42 +08:00
|
|
|
fs=self.fs,
|
2023-04-13 23:57:27 +08:00
|
|
|
f0_ceil=self.f0_max,
|
|
|
|
f0_floor=self.f0_min,
|
2023-05-14 15:05:42 +08:00
|
|
|
frame_period=1000 * self.hop / self.fs,
|
2023-03-31 17:54:38 +08:00
|
|
|
)
|
|
|
|
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.fs)
|
|
|
|
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
|
2023-06-10 22:55:34 +08:00
|
|
|
f0_coarse = np.rint(f0_mel).astype(int)
|
2023-03-31 17:54:38 +08:00
|
|
|
assert f0_coarse.max() <= 255 and f0_coarse.min() >= 1, (
|
|
|
|
f0_coarse.max(),
|
|
|
|
f0_coarse.min(),
|
|
|
|
)
|
|
|
|
return f0_coarse
|
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
def go(self, paths, f0_method):
|
|
|
|
if len(paths) == 0:
|
|
|
|
printt("no-f0-todo")
|
2023-03-31 17:54:38 +08:00
|
|
|
else:
|
2023-04-15 19:44:24 +08:00
|
|
|
printt("todo-f0-%s" % len(paths))
|
|
|
|
n = max(len(paths) // 5, 1) # 每个进程最多打印5条
|
|
|
|
for idx, (inp_path, opt_path1, opt_path2) in enumerate(paths):
|
2023-03-31 17:54:38 +08:00
|
|
|
try:
|
2023-04-15 19:44:24 +08:00
|
|
|
if idx % n == 0:
|
|
|
|
printt("f0ing,now-%s,all-%s,-%s" % (idx, len(paths), inp_path))
|
|
|
|
if (
|
|
|
|
os.path.exists(opt_path1 + ".npy") == True
|
|
|
|
and os.path.exists(opt_path2 + ".npy") == True
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
featur_pit = self.compute_f0(inp_path, f0_method)
|
|
|
|
np.save(
|
|
|
|
opt_path2,
|
|
|
|
featur_pit,
|
|
|
|
allow_pickle=False,
|
|
|
|
) # nsf
|
2023-03-31 17:54:38 +08:00
|
|
|
coarse_pit = self.coarse_f0(featur_pit)
|
2023-04-15 19:44:24 +08:00
|
|
|
np.save(
|
|
|
|
opt_path1,
|
|
|
|
coarse_pit,
|
|
|
|
allow_pickle=False,
|
|
|
|
) # ori
|
2023-03-31 17:54:38 +08:00
|
|
|
except:
|
2023-04-15 19:44:24 +08:00
|
|
|
printt("f0fail-%s-%s-%s" % (idx, inp_path, traceback.format_exc()))
|
2023-03-31 17:54:38 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-03-31 17:54:38 +08:00
|
|
|
# exp_dir=r"E:\codes\py39\dataset\mi-test"
|
|
|
|
# n_p=16
|
|
|
|
# f = open("%s/log_extract_f0.log"%exp_dir, "w")
|
|
|
|
printt(sys.argv)
|
|
|
|
featureInput = FeatureInput()
|
2023-04-15 19:44:24 +08:00
|
|
|
paths = []
|
|
|
|
inp_root = "%s/1_16k_wavs" % (exp_dir)
|
|
|
|
opt_root1 = "%s/2a_f0" % (exp_dir)
|
|
|
|
opt_root2 = "%s/2b-f0nsf" % (exp_dir)
|
2023-03-31 17:54:38 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
os.makedirs(opt_root1, exist_ok=True)
|
|
|
|
os.makedirs(opt_root2, exist_ok=True)
|
2023-03-31 17:54:38 +08:00
|
|
|
for name in sorted(list(os.listdir(inp_root))):
|
2023-04-15 19:44:24 +08:00
|
|
|
inp_path = "%s/%s" % (inp_root, name)
|
|
|
|
if "spec" in inp_path:
|
|
|
|
continue
|
|
|
|
opt_path1 = "%s/%s" % (opt_root1, name)
|
|
|
|
opt_path2 = "%s/%s" % (opt_root2, name)
|
|
|
|
paths.append([inp_path, opt_path1, opt_path2])
|
2023-03-31 17:54:38 +08:00
|
|
|
|
2023-04-15 19:44:24 +08:00
|
|
|
ps = []
|
2023-03-31 17:54:38 +08:00
|
|
|
for i in range(n_p):
|
2023-04-15 19:44:24 +08:00
|
|
|
p = Process(
|
|
|
|
target=featureInput.go,
|
|
|
|
args=(
|
|
|
|
paths[i::n_p],
|
|
|
|
f0method,
|
|
|
|
),
|
|
|
|
)
|
2023-03-31 17:54:38 +08:00
|
|
|
ps.append(p)
|
2023-05-19 17:56:06 +08:00
|
|
|
p.start()
|
|
|
|
for i in range(n_p):
|
|
|
|
ps[i].join()
|