import librosa import soundfile as sf import os import concurrent.futures def resample_audio(file_path, target_directory, target_sr=24000): # Load the audio file audio, sr = librosa.load(file_path, sr=None) # Resample the audio if sr != target_sr: audio_resampled = librosa.resample(audio, orig_sr=sr, target_sr=target_sr) # Determine the new file path target_file_path = os.path.join(target_directory, os.path.basename(file_path)) # Export the resampled audio 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) # Create a list of tasks 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)) # Process the tasks in parallel using multiple threads 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__': # Example usage resample_audio_multithreaded("fsd2018", 'fsd2018_24k') # Note: The actual execution of this code is not possible here due to the lack of access to the user's filesystem. # The user should run this script on their local machine.