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