-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare_saraga.py
59 lines (51 loc) · 2.29 KB
/
prepare_saraga.py
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
import os
import argparse
import torch
import glob
import tqdm
import numpy as np
import torch.nn.functional as F
import torchaudio as T
SR = 22050
def main(args):
concert = glob.glob(os.path.join(args.saraga_dir, '*/'))
for i in tqdm(concert):
songs = glob.glob(os.path.join(args.saraga_dir, i, '*/'))
for j in tqdm.tqdm(songs):
song_name = j.split("/")[-2]
mixture = os.path.join(j, song_name + ".mp3.mp3")
vocals = os.path.join(j, song_name + ".multitrack-vocal.mp3")
if os.path.exists(mixture):
audio_mix, sr = T.load(mixture)
audio_voc, _ = T.load(vocals)
resampling = T.transforms.Resample(sr, SR)
audio_mix = resampling(audio_mix)
audio_voc = resampling(audio_voc)
audio_mix = torch.mean(audio_mix, dim=0).unsqueeze(0)
audio_mix = torch.clamp(audio_mix, -1.0, 1.0)
audio_voc = torch.mean(audio_voc, dim=0).unsqueeze(0)
audio_voc = torch.clamp(audio_voc, -1.0, 1.0)
actual_len = audio_voc.shape
for trim in np.arange(actual_len[1] // (args.sample_len*SR)):
T.save(
os.path.join(
args.output_dir, song_name.lower().replace(" ", "_") + '_' + str(trim) + '_mixture.wav'),
audio_mix[:, trim*args.sample_len*SR:(trim+1)*args.sample_len*SR].cpu(),
sample_rate=sr,
bits_per_sample=16)
T.save(
os.path.join(
args.output_dir, song_name.lower().replace(" ", "_") + '_' + str(trim) + '_vocals.wav'),
audio_voc[:, trim*args.sample_len*SR:(trim+1)*args.sample_len*SR].cpu(),
sample_rate=sr,
bits_per_sample=16)
else:
print("no file...")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--saraga-dir', default=None, type=str)
parser.add_argument('--output-dir', default=None, type=str)
parser.add_argument('--sample-len', default=6)
parser.add_argument('--gpu', default=None)
args = parser.parse_args()
main(args)