2023-04-14 23:10:04 +08:00
|
|
|
import sys, os, multiprocessing
|
|
|
|
|
|
|
|
now_dir = os.getcwd()
|
2023-03-31 17:49:09 +08:00
|
|
|
sys.path.append(now_dir)
|
|
|
|
|
|
|
|
inp_root = sys.argv[1]
|
|
|
|
sr = int(sys.argv[2])
|
|
|
|
n_p = int(sys.argv[3])
|
|
|
|
exp_dir = sys.argv[4]
|
2023-04-01 16:42:19 +08:00
|
|
|
noparallel = sys.argv[5] == "True"
|
2023-04-14 23:10:04 +08:00
|
|
|
import numpy as np, os, traceback
|
2023-03-31 17:49:09 +08:00
|
|
|
from slicer2 import Slicer
|
2023-04-14 23:10:04 +08:00
|
|
|
import librosa, traceback
|
|
|
|
from scipy.io import wavfile
|
2023-03-31 17:49:09 +08:00
|
|
|
import multiprocessing
|
|
|
|
from my_utils import load_audio
|
|
|
|
|
2023-04-01 16:42:19 +08:00
|
|
|
mutex = multiprocessing.Lock()
|
2023-04-14 23:10:04 +08:00
|
|
|
f = open("%s/preprocess.log" % exp_dir, "a+")
|
|
|
|
|
|
|
|
|
2023-04-13 23:57:27 +08:00
|
|
|
def println(strr):
|
|
|
|
mutex.acquire()
|
|
|
|
print(strr)
|
|
|
|
f.write("%s\n" % strr)
|
|
|
|
f.flush()
|
|
|
|
mutex.release()
|
2023-03-31 17:49:09 +08:00
|
|
|
|
2023-04-14 23:10:04 +08:00
|
|
|
|
|
|
|
class PreProcess:
|
|
|
|
def __init__(self, sr, exp_dir):
|
2023-03-31 17:49:09 +08:00
|
|
|
self.slicer = Slicer(
|
|
|
|
sr=sr,
|
|
|
|
threshold=-32,
|
|
|
|
min_length=800,
|
|
|
|
min_interval=400,
|
|
|
|
hop_size=15,
|
2023-04-14 23:10:04 +08:00
|
|
|
max_sil_kept=150,
|
2023-03-31 17:49:09 +08:00
|
|
|
)
|
2023-04-14 23:10:04 +08:00
|
|
|
self.sr = sr
|
|
|
|
self.per = 3.7
|
|
|
|
self.overlap = 0.3
|
|
|
|
self.tail = self.per + self.overlap
|
|
|
|
self.max = 0.95
|
|
|
|
self.alpha = 0.8
|
|
|
|
self.exp_dir = exp_dir
|
|
|
|
self.gt_wavs_dir = "%s/0_gt_wavs" % exp_dir
|
|
|
|
self.wavs16k_dir = "%s/1_16k_wavs" % exp_dir
|
|
|
|
os.makedirs(self.exp_dir, exist_ok=True)
|
|
|
|
os.makedirs(self.gt_wavs_dir, exist_ok=True)
|
|
|
|
os.makedirs(self.wavs16k_dir, exist_ok=True)
|
2023-03-31 17:49:09 +08:00
|
|
|
|
2023-04-14 23:10:04 +08:00
|
|
|
def norm_write(self, tmp_audio, idx0, idx1):
|
|
|
|
tmp_audio = (tmp_audio / np.abs(tmp_audio).max() * (self.max * self.alpha)) + (
|
|
|
|
1 - self.alpha
|
|
|
|
) * tmp_audio
|
|
|
|
wavfile.write(
|
|
|
|
"%s/%s_%s.wav" % (self.gt_wavs_dir, idx0, idx1),
|
|
|
|
self.sr,
|
|
|
|
(tmp_audio * 32768).astype(np.int16),
|
|
|
|
)
|
2023-03-31 17:49:09 +08:00
|
|
|
tmp_audio = librosa.resample(tmp_audio, orig_sr=self.sr, target_sr=16000)
|
2023-04-14 23:10:04 +08:00
|
|
|
wavfile.write(
|
|
|
|
"%s/%s_%s.wav" % (self.wavs16k_dir, idx0, idx1),
|
|
|
|
16000,
|
|
|
|
(tmp_audio * 32768).astype(np.int16),
|
|
|
|
)
|
2023-03-31 17:49:09 +08:00
|
|
|
|
2023-04-14 23:10:04 +08:00
|
|
|
def pipeline(self, path, idx0):
|
2023-03-31 17:49:09 +08:00
|
|
|
try:
|
2023-04-14 23:10:04 +08:00
|
|
|
audio = load_audio(path, self.sr)
|
|
|
|
idx1 = 0
|
2023-03-31 17:49:09 +08:00
|
|
|
for audio in self.slicer.slice(audio):
|
|
|
|
i = 0
|
2023-04-14 23:10:04 +08:00
|
|
|
while 1:
|
2023-03-31 17:49:09 +08:00
|
|
|
start = int(self.sr * (self.per - self.overlap) * i)
|
|
|
|
i += 1
|
2023-04-14 23:10:04 +08:00
|
|
|
if len(audio[start:]) > self.tail * self.sr:
|
|
|
|
tmp_audio = audio[start : start + int(self.per * self.sr)]
|
|
|
|
self.norm_write(tmp_audio, idx0, idx1)
|
2023-03-31 17:49:09 +08:00
|
|
|
idx1 += 1
|
|
|
|
else:
|
|
|
|
tmp_audio = audio[start:]
|
|
|
|
break
|
|
|
|
self.norm_write(tmp_audio, idx0, idx1)
|
2023-04-14 23:10:04 +08:00
|
|
|
println("%s->Suc." % path)
|
2023-03-31 17:49:09 +08:00
|
|
|
except:
|
2023-04-14 23:10:04 +08:00
|
|
|
println("%s->%s" % (path, traceback.format_exc()))
|
2023-03-31 17:49:09 +08:00
|
|
|
|
2023-04-14 23:10:04 +08:00
|
|
|
def pipeline_mp(self, infos):
|
2023-03-31 17:49:09 +08:00
|
|
|
for path, idx0 in infos:
|
2023-04-14 23:10:04 +08:00
|
|
|
self.pipeline(path, idx0)
|
2023-03-31 17:49:09 +08:00
|
|
|
|
2023-04-14 23:10:04 +08:00
|
|
|
def pipeline_mp_inp_dir(self, inp_root, n_p):
|
2023-03-31 17:49:09 +08:00
|
|
|
try:
|
2023-04-14 23:10:04 +08:00
|
|
|
infos = [
|
|
|
|
("%s/%s" % (inp_root, name), idx)
|
|
|
|
for idx, name in enumerate(sorted(list(os.listdir(inp_root))))
|
|
|
|
]
|
2023-04-01 16:42:19 +08:00
|
|
|
if noparallel:
|
2023-04-14 23:10:04 +08:00
|
|
|
for i in range(n_p):
|
|
|
|
self.pipeline_mp(infos[i::n_p])
|
2023-04-01 16:42:19 +08:00
|
|
|
else:
|
2023-04-14 23:10:04 +08:00
|
|
|
ps = []
|
2023-04-01 16:42:19 +08:00
|
|
|
for i in range(n_p):
|
2023-04-14 23:10:04 +08:00
|
|
|
p = multiprocessing.Process(
|
|
|
|
target=self.pipeline_mp, args=(infos[i::n_p],)
|
|
|
|
)
|
2023-04-01 16:42:19 +08:00
|
|
|
p.start()
|
|
|
|
ps.append(p)
|
2023-04-14 23:10:04 +08:00
|
|
|
for p in ps:
|
|
|
|
p.join()
|
2023-03-31 17:49:09 +08:00
|
|
|
except:
|
2023-04-14 23:10:04 +08:00
|
|
|
println("Fail. %s" % traceback.format_exc())
|
|
|
|
|
2023-03-31 17:49:09 +08:00
|
|
|
|
2023-04-01 16:42:19 +08:00
|
|
|
def preprocess_trainset(inp_root, sr, n_p, exp_dir):
|
2023-04-14 23:10:04 +08:00
|
|
|
pp = PreProcess(sr, exp_dir)
|
2023-04-13 23:57:27 +08:00
|
|
|
println("start preprocess")
|
|
|
|
println(sys.argv)
|
2023-04-14 23:10:04 +08:00
|
|
|
pp.pipeline_mp_inp_dir(inp_root, n_p)
|
2023-04-13 23:57:27 +08:00
|
|
|
println("end preprocess")
|
2023-04-01 16:42:19 +08:00
|
|
|
|
2023-04-14 23:10:04 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-04-01 16:42:19 +08:00
|
|
|
preprocess_trainset(inp_root, sr, n_p, exp_dir)
|