Fix the scope of resample.py
This commit is contained in:
parent
8938204851
commit
577c416c5e
43
resample.py
43
resample.py
|
@ -12,18 +12,22 @@ from tqdm import tqdm
|
|||
def load_wav(wav_path):
|
||||
return librosa.load(wav_path, sr=None)
|
||||
|
||||
|
||||
def trim_wav(wav, top_db=40):
|
||||
return librosa.effects.trim(wav, top_db=top_db)
|
||||
|
||||
|
||||
def normalize_peak(wav, threshold=1.0):
|
||||
peak = np.abs(wav).max()
|
||||
if peak > threshold:
|
||||
wav = 0.98 * wav / peak
|
||||
return wav
|
||||
|
||||
|
||||
def resample_wav(wav, sr, target_sr):
|
||||
return librosa.resample(wav, orig_sr=sr, target_sr=target_sr)
|
||||
|
||||
|
||||
def save_wav_to_path(wav, save_path, sr):
|
||||
wavfile.write(
|
||||
save_path,
|
||||
|
@ -31,8 +35,9 @@ def save_wav_to_path(wav, save_path, sr):
|
|||
(wav * np.iinfo(np.int16).max).astype(np.int16)
|
||||
)
|
||||
|
||||
|
||||
def process(item):
|
||||
spkdir, wav_name, args = item
|
||||
spkdir, wav_name = item
|
||||
speaker = spkdir.replace("\\", "/").split("/")[-1]
|
||||
|
||||
wav_path = os.path.join(args.in_dir, speaker, wav_name)
|
||||
|
@ -47,25 +52,16 @@ def process(item):
|
|||
if not args.skip_loudnorm:
|
||||
resampled_wav /= max(resampled_wav.max(), -resampled_wav.min())
|
||||
|
||||
print(type(resampled_wav))
|
||||
save_path2 = os.path.join(args.out_dir2, speaker, wav_name)
|
||||
save_wav_to_path(resampled_wav, save_path2, args.sr2)
|
||||
|
||||
# def process_all_speakers(speakers, args):
|
||||
# process_count = 30 if os.cpu_count() > 60 else (os.cpu_count() - 2 if os.cpu_count() > 4 else 1)
|
||||
|
||||
# with ThreadPoolExecutor(max_workers=process_count) as executor:
|
||||
# for speaker in speakers:
|
||||
# spk_dir = os.path.join(args.in_dir, speaker)
|
||||
# if os.path.isdir(spk_dir):
|
||||
# print(spk_dir)
|
||||
# futures = [executor.submit(process, (spk_dir, i, args)) for i in os.listdir(spk_dir) if i.endswith("wav")]
|
||||
# for _ in tqdm(concurrent.futures.as_completed(futures), total=len(futures)):
|
||||
# pass
|
||||
|
||||
# multi process
|
||||
def process_all_speakers(speakers, args):
|
||||
"""
|
||||
def process_all_speakers():
|
||||
process_count = 30 if os.cpu_count() > 60 else (os.cpu_count() - 2 if os.cpu_count() > 4 else 1)
|
||||
with ProcessPoolExecutor(max_workers=process_count) as executor:
|
||||
|
||||
with ThreadPoolExecutor(max_workers=process_count) as executor:
|
||||
for speaker in speakers:
|
||||
spk_dir = os.path.join(args.in_dir, speaker)
|
||||
if os.path.isdir(spk_dir):
|
||||
|
@ -73,6 +69,21 @@ def process_all_speakers(speakers, args):
|
|||
futures = [executor.submit(process, (spk_dir, i, args)) for i in os.listdir(spk_dir) if i.endswith("wav")]
|
||||
for _ in tqdm(concurrent.futures.as_completed(futures), total=len(futures)):
|
||||
pass
|
||||
"""
|
||||
# multi process
|
||||
|
||||
|
||||
def process_all_speakers():
|
||||
process_count = 30 if os.cpu_count() > 60 else (os.cpu_count() - 2 if os.cpu_count() > 4 else 1)
|
||||
with ProcessPoolExecutor(max_workers=process_count) as executor:
|
||||
for speaker in speakers:
|
||||
spk_dir = os.path.join(args.in_dir, speaker)
|
||||
if os.path.isdir(spk_dir):
|
||||
print(spk_dir)
|
||||
futures = [executor.submit(process, (spk_dir, i)) for i in os.listdir(spk_dir) if i.endswith("wav")]
|
||||
for _ in tqdm(concurrent.futures.as_completed(futures), total=len(futures)):
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
|
@ -84,4 +95,4 @@ if __name__ == "__main__":
|
|||
|
||||
print(f"CPU count: {cpu_count()}")
|
||||
speakers = os.listdir(args.in_dir)
|
||||
process_all_speakers(speakers, args)
|
||||
process_all_speakers()
|
||||
|
|
Loading…
Reference in New Issue