| | import os |
| | import librosa |
| | import numpy as np |
| | import soundfile as sf |
| | from tqdm import tqdm |
| | from concurrent.futures import ProcessPoolExecutor |
| |
|
| | |
| | audio_dir = 'audio' |
| | output_dir = 'processed_audio' |
| |
|
| | |
| | os.makedirs(output_dir, exist_ok=True) |
| |
|
| | |
| | def load_audio(file_path): |
| | audio, sr = librosa.load(file_path, sr=None) |
| | return audio, sr |
| |
|
| | |
| | def normalize_audio(audio): |
| | return audio / np.max(np.abs(audio)) |
| |
|
| | |
| | def save_audio(output_path, audio, sr): |
| | |
| | sf.write(output_path.replace('.wav', '.mp3'), audio, sr, format='MP3') |
| |
|
| | |
| | def process_audio(file_path): |
| | audio, sr = load_audio(file_path) |
| | audio = normalize_audio(audio) |
| | output_path = os.path.join(output_dir, os.path.basename(file_path).replace('.mp3', '.wav')) |
| | save_audio(output_path, audio, sr) |
| | return os.path.basename(file_path) |
| |
|
| | if __name__ == '__main__': |
| | |
| | audio_files = [os.path.join(audio_dir, filename) for filename in os.listdir(audio_dir) if filename.endswith('.mp3')] |
| |
|
| | |
| | with ProcessPoolExecutor() as executor: |
| | for result in tqdm(executor.map(process_audio, audio_files), total=len(audio_files), desc="Processing files", unit="file"): |
| | print(f'Processed {result}') |