|
import librosa |
|
import soundfile as sf |
|
import os |
|
import concurrent.futures |
|
|
|
|
|
def resample_audio(file_path, target_directory, target_sr=24000): |
|
|
|
audio, sr = librosa.load(file_path, sr=None) |
|
|
|
if sr != target_sr: |
|
audio_resampled = librosa.resample(audio, orig_sr=sr, target_sr=target_sr) |
|
|
|
target_file_path = os.path.join(target_directory, os.path.basename(file_path)) |
|
|
|
sf.write(target_file_path, audio_resampled, target_sr) |
|
|
|
|
|
def resample_audio_multithreaded(source_folder, target_folder, target_sr=24000): |
|
if not os.path.exists(target_folder): |
|
os.makedirs(target_folder) |
|
|
|
|
|
tasks = [] |
|
for root, dirs, files in os.walk(source_folder): |
|
for file in files: |
|
if file.endswith(".wav"): |
|
source_file_path = os.path.join(root, file) |
|
relative_path = os.path.relpath(root, source_folder) |
|
target_directory = os.path.join(target_folder, relative_path) |
|
if not os.path.exists(target_directory): |
|
os.makedirs(target_directory) |
|
|
|
tasks.append((source_file_path, target_directory)) |
|
|
|
|
|
with concurrent.futures.ThreadPoolExecutor() as executor: |
|
futures = [executor.submit(resample_audio, task[0], task[1], target_sr) for task in tasks] |
|
concurrent.futures.wait(futures) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
resample_audio_multithreaded("fsd2018", 'fsd2018_24k') |
|
|
|
|
|
|
|
|