|
| 1 | +import os, traceback, sys, parselmouth |
| 2 | + |
| 3 | +now_dir = os.getcwd() |
| 4 | +sys.path.append(now_dir) |
| 5 | +from lib.audio import load_audio |
| 6 | +import pyworld |
| 7 | +import numpy as np, logging |
| 8 | + |
| 9 | +logging.getLogger("numba").setLevel(logging.WARNING) |
| 10 | + |
| 11 | +n_part = int(sys.argv[1]) |
| 12 | +i_part = int(sys.argv[2]) |
| 13 | +i_gpu = sys.argv[3] |
| 14 | +os.environ["CUDA_VISIBLE_DEVICES"] = str(i_gpu) |
| 15 | +exp_dir = sys.argv[4] |
| 16 | +is_half = sys.argv[5] |
| 17 | +f = open("%s/extract_f0_feature.log" % exp_dir, "a+") |
| 18 | + |
| 19 | + |
| 20 | +def printt(strr): |
| 21 | + print(strr) |
| 22 | + f.write("%s\n" % strr) |
| 23 | + f.flush() |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +class FeatureInput(object): |
| 28 | + def __init__(self, samplerate=16000, hop_size=160): |
| 29 | + self.fs = samplerate |
| 30 | + self.hop = hop_size |
| 31 | + |
| 32 | + self.f0_bin = 256 |
| 33 | + self.f0_max = 1100.0 |
| 34 | + self.f0_min = 50.0 |
| 35 | + self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700) |
| 36 | + self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700) |
| 37 | + |
| 38 | + def compute_f0(self, path, f0_method): |
| 39 | + x = load_audio(path, self.fs) |
| 40 | + p_len = x.shape[0] // self.hop |
| 41 | + if(f0_method=="rmvpe"): |
| 42 | + if hasattr(self, "model_rmvpe") == False: |
| 43 | + from lib.rmvpe import RMVPE |
| 44 | + |
| 45 | + print("loading rmvpe model") |
| 46 | + self.model_rmvpe = RMVPE( |
| 47 | + "rmvpe.pt", is_half=True, device="cuda" |
| 48 | + ) |
| 49 | + f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03) |
| 50 | + return f0 |
| 51 | + |
| 52 | + def coarse_f0(self, f0): |
| 53 | + f0_mel = 1127 * np.log(1 + f0 / 700) |
| 54 | + f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - self.f0_mel_min) * ( |
| 55 | + self.f0_bin - 2 |
| 56 | + ) / (self.f0_mel_max - self.f0_mel_min) + 1 |
| 57 | + |
| 58 | + # use 0 or 1 |
| 59 | + f0_mel[f0_mel <= 1] = 1 |
| 60 | + f0_mel[f0_mel > self.f0_bin - 1] = self.f0_bin - 1 |
| 61 | + f0_coarse = np.rint(f0_mel).astype(int) |
| 62 | + assert f0_coarse.max() <= 255 and f0_coarse.min() >= 1, ( |
| 63 | + f0_coarse.max(), |
| 64 | + f0_coarse.min(), |
| 65 | + ) |
| 66 | + return f0_coarse |
| 67 | + |
| 68 | + def go(self, paths, f0_method): |
| 69 | + if len(paths) == 0: |
| 70 | + printt("no-f0-todo") |
| 71 | + else: |
| 72 | + printt("todo-f0-%s" % len(paths)) |
| 73 | + n = max(len(paths) // 5, 1) # 每个进程最多打印5条 |
| 74 | + for idx, (inp_path, opt_path1, opt_path2) in enumerate(paths): |
| 75 | + try: |
| 76 | + if idx % n == 0: |
| 77 | + printt("f0ing,now-%s,all-%s,-%s" % (idx, len(paths), inp_path)) |
| 78 | + if ( |
| 79 | + os.path.exists(opt_path1 + ".npy") == True |
| 80 | + and os.path.exists(opt_path2 + ".npy") == True |
| 81 | + ): |
| 82 | + continue |
| 83 | + featur_pit = self.compute_f0(inp_path, f0_method) |
| 84 | + np.save( |
| 85 | + opt_path2, |
| 86 | + featur_pit, |
| 87 | + allow_pickle=False, |
| 88 | + ) # nsf |
| 89 | + coarse_pit = self.coarse_f0(featur_pit) |
| 90 | + np.save( |
| 91 | + opt_path1, |
| 92 | + coarse_pit, |
| 93 | + allow_pickle=False, |
| 94 | + ) # ori |
| 95 | + except: |
| 96 | + printt("f0fail-%s-%s-%s" % (idx, inp_path, traceback.format_exc())) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + # exp_dir=r"E:\codes\py39\dataset\mi-test" |
| 101 | + # n_p=16 |
| 102 | + # f = open("%s/log_extract_f0.log"%exp_dir, "w") |
| 103 | + printt(sys.argv) |
| 104 | + featureInput = FeatureInput() |
| 105 | + paths = [] |
| 106 | + inp_root = "%s/1_16k_wavs" % (exp_dir) |
| 107 | + opt_root1 = "%s/2a_f0" % (exp_dir) |
| 108 | + opt_root2 = "%s/2b-f0nsf" % (exp_dir) |
| 109 | + |
| 110 | + os.makedirs(opt_root1, exist_ok=True) |
| 111 | + os.makedirs(opt_root2, exist_ok=True) |
| 112 | + for name in sorted(list(os.listdir(inp_root))): |
| 113 | + inp_path = "%s/%s" % (inp_root, name) |
| 114 | + if "spec" in inp_path: |
| 115 | + continue |
| 116 | + opt_path1 = "%s/%s" % (opt_root1, name) |
| 117 | + opt_path2 = "%s/%s" % (opt_root2, name) |
| 118 | + paths.append([inp_path, opt_path1, opt_path2]) |
| 119 | + try: |
| 120 | + featureInput.go(paths[i_part::n_part],"rmvpe") |
| 121 | + except: |
| 122 | + printt("f0_all_fail-%s" % (traceback.format_exc())) |
| 123 | + # ps = [] |
| 124 | + # for i in range(n_p): |
| 125 | + # p = Process( |
| 126 | + # target=featureInput.go, |
| 127 | + # args=( |
| 128 | + # paths[i::n_p], |
| 129 | + # f0method, |
| 130 | + # ), |
| 131 | + # ) |
| 132 | + # ps.append(p) |
| 133 | + # p.start() |
| 134 | + # for i in range(n_p): |
| 135 | + # ps[i].join() |
0 commit comments