Sucial commited on
Commit
d0c5fc0
1 Parent(s): 7496225

Upload create_reverb_delay_multi_threads.py

Browse files
scripts/create_reverb_delay_multi_threads.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import argparse
3
+ import librosa
4
+ import numpy as np
5
+ import soundfile as sf
6
+ from pedalboard import Pedalboard, Reverb, Delay, HighpassFilter, LowpassFilter
7
+ from random import uniform
8
+ from tqdm import tqdm
9
+ from concurrent.futures import ThreadPoolExecutor
10
+
11
+ def random_effect(audio, sr):
12
+ reverb = Pedalboard([
13
+ Delay(
14
+ delay_seconds=uniform(0.001, 0.100),
15
+ feedback=0.0,
16
+ mix=1.0
17
+ ),
18
+ Reverb(
19
+ room_size=uniform(0.7, 1.0),
20
+ damping=uniform(0.7, 1.0),
21
+ wet_level=1.0,
22
+ dry_level=0.0,
23
+ width=uniform(0.7, 1.0)
24
+ ),
25
+ HighpassFilter(cutoff_frequency_hz=uniform(100, 800)),
26
+ LowpassFilter(cutoff_frequency_hz=uniform(4000, 15000))
27
+ ])
28
+
29
+ effect = uniform(0.3, 0.6) * reverb(audio, sr)
30
+ mix = effect + audio
31
+
32
+ return mix, effect
33
+
34
+ def process_file(file, input_folder, output_folder, index, sr):
35
+ try:
36
+ audio, _ = librosa.load(os.path.join(input_folder, file), sr=sr)
37
+ if len(audio.shape) == 1:
38
+ audio = np.stack([audio, audio], axis=1)
39
+ effect = random_effect(audio.T, sr)
40
+ except Exception as e:
41
+ print(f"Failed to process file: {file}. Error: {e}")
42
+ return False
43
+
44
+ output_path = os.path.join(output_folder, str(index))
45
+ os.makedirs(output_path, exist_ok=True)
46
+
47
+ try:
48
+ sf.write(os.path.join(output_path, "mixture.wav"), effect[0].T, sr, subtype='PCM_16')
49
+ sf.write(os.path.join(output_path, "other.wav"), effect[1].T, sr, subtype='PCM_16')
50
+ sf.write(os.path.join(output_path, "dry.wav"), audio, sr, subtype='PCM_16')
51
+ os.remove(os.path.join(input_folder, file))
52
+ except Exception as e:
53
+ print(f"Failed to save file for {file}. Error: {e}")
54
+ return False
55
+
56
+ return True
57
+
58
+ if __name__ == '__main__':
59
+ argparser = argparse.ArgumentParser(description='Add random reverb and delay effects to audio files using multithreading.')
60
+ argparser.add_argument('-i', '--input_folder', type=str, default="trainset", help='Path to the input folder containing audio files.')
61
+ argparser.add_argument('-o', '--output_folder', type=str, default="train2", help='Path to the output folder for processed audio files.')
62
+ argparser.add_argument('-t', '--threads', type=int, default=32, help='Number of threads to use for processing.')
63
+ args = argparser.parse_args()
64
+
65
+ sr = 44100
66
+ input_files = os.listdir(args.input_folder)
67
+
68
+ with ThreadPoolExecutor(max_workers=args.threads) as executor:
69
+ futures = {executor.submit(process_file, file, args.input_folder, args.output_folder, index, sr): file for index, file in enumerate(input_files, start=1)}
70
+ for future in tqdm(futures, total=len(input_files)):
71
+ future.result()