-
Notifications
You must be signed in to change notification settings - Fork 0
/
medley_vox.py
153 lines (137 loc) · 5.92 KB
/
medley_vox.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
import glob
import json
import numpy as np
import torch
from torch.utils.data import Dataset
import librosa
exclude_duet = {
"CatMartino_IPromise",
"TleilaxEnsemble_Late",
"TleilaxEnsemble_MelancholyFlowers",
}
class MedleyVox(Dataset):
"""Dataset class for MedleyVox source separation tasks.
Args:
task (str): One of ``'unison'``, ``'duet'``, ``'main_vs_rest'`` or
``'total'`` :
* ``'unison'`` for unison vocal separation.
* ``'duet'`` for duet vocal separation.
* ``'main_vs_rest'`` for main vs. rest vocal separation (main vs rest).
* ``'n_singing'`` for N-singing separation. We will use all of the duet, unison, and main vs. rest data.
sample_rate (int) : The sample rate of the sources and mixtures.
n_src (int) : The number of sources in the mixture. Actually, this is fixed to 2 for our tasks. Need to be specified for N-singing training (future work).
segment (int, optional) : The desired sources and mixtures length in s.
"""
dataset_name = "MedleyVox"
def __init__(
self,
root_dir,
metadata_dir=None,
task="duet",
sample_rate=24000,
n_src=2,
segment=None,
return_id=True,
drop_duet=False,
):
self.root_dir = root_dir # /path/to/data/test_medleyDB
self.metadata_dir = metadata_dir # ./testset/testset_config
self.task = task.lower()
self.return_id = return_id
# Get the csv corresponding to the task
if self.task == "unison":
self.total_segments_list = glob.glob(f"{self.root_dir}/unison/*/*")
elif self.task == "duet":
self.total_segments_list = glob.glob(f"{self.root_dir}/duet/*/*")
if drop_duet:
total_segments_list = list(
filter(
lambda x: x.split("/")[-2] not in exclude_duet,
self.total_segments_list,
)
)
print(
f"Drop {len(self.total_segments_list) - len(total_segments_list)} duet songs."
)
self.total_segments_list = total_segments_list
elif self.task == "main_vs_rest":
self.total_segments_list = glob.glob(f"{self.root_dir}/rest/*/*")
elif self.task == "n_singing":
self.total_segments_list = (
glob.glob(f"{self.root_dir}/unison/*/*")
+ glob.glob(f"{self.root_dir}/duet/*/*")
+ glob.glob(f"{self.root_dir}/rest/*/*")
)
self.segment = segment
self.sample_rate = sample_rate
self.n_src = n_src
def __len__(self):
return len(self.total_segments_list)
def __getitem__(self, idx):
song_name = self.total_segments_list[idx].split("/")[-2]
segment_name = self.total_segments_list[idx].split("/")[-1]
mixture_path = (
f"{self.total_segments_list[idx]}/mix/{song_name} - {segment_name}.wav"
)
self.mixture_path = mixture_path
sources_path_list = glob.glob(f"{self.total_segments_list[idx]}/gt/*.wav")
if self.task == "main_vs_rest" or self.task == "n_singing":
if os.path.exists(
f"{self.metadata_dir}/V1_rest_vocals_only_config/{song_name}.json"
):
metadata_json_path = (
f"{self.metadata_dir}/V1_rest_vocals_only_config/{song_name}.json"
)
elif os.path.exists(
f"{self.metadata_dir}/V2_vocals_only_config/{song_name}.json"
):
metadata_json_path = (
f"{self.metadata_dir}/V2_vocals_only_config/{song_name}.json"
)
else:
print("main vs. rest metadata not found.")
raise AttributeError
with open(metadata_json_path, "r") as json_file:
metadata_json = json.load(json_file)
# Read sources
sources_list = []
ids = []
if self.task == "main_vs_rest" or self.task == "n_singing":
gt_main_name = metadata_json[segment_name]["main_vocal"]
gt_source, sr = librosa.load(
f"{self.total_segments_list[idx]}/gt/{gt_main_name} - {segment_name}.wav",
sr=self.sample_rate,
)
gt_rest_list = metadata_json[segment_name]["other_vocals"]
ids.append(f"{gt_main_name} - {segment_name}")
rest_sources_list = []
for other_vocal_name in gt_rest_list:
s, sr = librosa.load(
f"{self.total_segments_list[idx]}/gt/{other_vocal_name} - {segment_name}.wav",
sr=self.sample_rate,
)
rest_sources_list.append(s)
ids.append(f"{other_vocal_name} - {segment_name}")
rest_sources_list = np.stack(rest_sources_list, axis=0)
gt_rest = rest_sources_list.sum(0)
sources_list.append(gt_source)
sources_list.append(gt_rest)
else: # self.task == 'unison' or self.task == 'duet'
for i, source_path in enumerate(sources_path_list):
s, sr = librosa.load(source_path, sr=self.sample_rate)
sources_list.append(s)
ids.append(os.path.basename(source_path).replace(".wav", ""))
# Read the mixture
mixture, sr = librosa.load(mixture_path, sr=self.sample_rate)
# Convert to torch tensor
mixture = torch.as_tensor(mixture, dtype=torch.float32)
# Stack sources
sources = np.vstack(sources_list)
# Convert sources to tensor
sources = torch.as_tensor(sources, dtype=torch.float32)
if not self.return_id:
return mixture, sources
# 5400-34479-0005_4973-24515-0007.wav
# id1, id2 = mixture_path.split("/")[-1].split(".")[0].split("_")
return mixture, sources, ids