-
Notifications
You must be signed in to change notification settings - Fork 7
/
resample.py
37 lines (28 loc) · 1.02 KB
/
resample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import shutil
from glob import glob
import librosa
import soundfile
from tqdm import tqdm
from multiprocessing import Pool
from data_conf import data_root
def process_wav(wavpath):
wav, _ = librosa.load(wavpath, sr=tgt_sr)
soundfile.write(wavpath, wav, tgt_sr)
def get_wav_files(path):
wav_files = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".wav") or file.endswith(".mp3"):
wav_files.append(os.path.join(root, file))
return wav_files
tgt_path = data_root
num_processes = 10 # You can adjust the number of processes as needed
tgt_sr = 32000
print("Note: this script will overwrite the original files!")
print("all the wav files under {} will be resampled to {}Hz".format(tgt_path, tgt_sr))
input("press enter to continue... or press Ctrl+C to cancel")
if __name__ == "__main__":
with Pool(num_processes) as pool:
file_list = get_wav_files(tgt_path)
list(tqdm(pool.imap(process_wav, file_list), total=len(file_list)))