File size: 2,691 Bytes
f116a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import argparse
import librosa
import numpy as np
import soundfile as sf
from pedalboard import Pedalboard, Reverb, Delay, HighpassFilter, LowpassFilter
from random import uniform
from tqdm import tqdm


def random_effect(audio, sr):
    reverb = Pedalboard([
        Delay(
            delay_seconds=uniform(0.001, 0.100), 
            feedback=0.0, 
            mix=1.0
        ),
        Reverb(
            room_size=uniform(0.1, 0.8),
            damping=uniform(0.1, 0.8),
            wet_level=1.0, 
            dry_level=0.0, 
            width=uniform(0.6, 1.0)
        ),
        HighpassFilter(cutoff_frequency_hz=uniform(100, 1000)),
        LowpassFilter(cutoff_frequency_hz=uniform(4000, 12000))
    ])

    delay = Pedalboard([
        Delay(
            delay_seconds=uniform(0.05, 0.500),
            feedback=uniform(0.1, 0.5),
            mix=1.0
        ),
        Reverb(
            room_size=uniform(0.05, 0.3),
            damping=uniform(0.1, 0.8),
            wet_level=0.2,
            dry_level=0.8,
            width=uniform(0.6, 1.0)
        ),
        HighpassFilter(cutoff_frequency_hz=uniform(100, 1000)),
        LowpassFilter(cutoff_frequency_hz=uniform(3000, 10000))
    ])

    effect = uniform(0.1, 0.4) * reverb(audio, sr) + uniform(0.1, 0.4) * delay(audio, sr)
    mix = effect + audio

    return mix, effect


if __name__ == '__main__':
    argparser = argparse.ArgumentParser(description='Add random reverb and delay effects to an audio file.')
    argparser.add_argument('-i', '--input_folder', type=str, default="train", help='Path to the input audio file.')
    argparser.add_argument('-o', '--output_folder', type=str, default="dataset_train", help='Path to the output audio file.')
    args = argparser.parse_args()

    index = 1
    sr = 44100
    for file in tqdm(os.listdir(args.input_folder)):
        try:
            audio, _ = librosa.load(os.path.join(args.input_folder, file), sr=sr)
            if len(audio.shape) == 1:
                audio = np.stack([audio, audio], axis=1)
            effect = random_effect(audio.T, sr)
        except:
            print(f"Failed to process file: {file}")
            continue

        os.makedirs(os.path.join(args.output_folder, str(index)), exist_ok=True)

        sf.write(os.path.join(args.output_folder, str(index), "mixture.wav"), effect[0].T, sr, subtype='PCM_16')
        sf.write(os.path.join(args.output_folder, str(index), "other.wav"), effect[1].T, sr, subtype='PCM_16')
        sf.write(os.path.join(args.output_folder, str(index), "dry.wav"), audio, sr, subtype='PCM_16')

        index += 1