add loading script
Browse files- musdb18.py +94 -0
musdb18.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
from pathlib import Path
|
3 |
+
import stempeg
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
|
7 |
+
_DESCRIPTION = """\
|
8 |
+
MUSDB18 music source separation dataset
|
9 |
+
|
10 |
+
to open original stem file (mp4), which is done internally you need stempeg library.
|
11 |
+
Outcome of mp4 file is a 3 dimensional np_array [n_stems, n_samples, sample_rate].
|
12 |
+
|
13 |
+
firt dimension meanings: {
|
14 |
+
0: mixture.
|
15 |
+
1: drugs,
|
16 |
+
2: bass,
|
17 |
+
3: others,
|
18 |
+
4:vocals,
|
19 |
+
}
|
20 |
+
|
21 |
+
Original dataset is not cutted in any parts, but here I cut each song in 10 seconds chunks with 1 sec overlap.
|
22 |
+
"""
|
23 |
+
|
24 |
+
_DESCRIPTION = "musdb dataset"
|
25 |
+
|
26 |
+
|
27 |
+
class Musdb18Dataset(datasets.GeneratorBasedBuilder):
|
28 |
+
DEFAULT_WRITER_BATCH_SIZE = 300
|
29 |
+
SAMPLING_RATE = 44100
|
30 |
+
WINDOW_SIZE = SAMPLING_RATE * 10 # 10s windows
|
31 |
+
INSTRUMENT_NAMES = ["mixture", "drums", "bass", "other", "vocals"]
|
32 |
+
|
33 |
+
#! To configure different configurations (length of window is the only thing)
|
34 |
+
# use datasets.BuilderConfig
|
35 |
+
|
36 |
+
def _info(self):
|
37 |
+
return datasets.DatasetInfo(
|
38 |
+
description=_DESCRIPTION,
|
39 |
+
features=datasets.Features({
|
40 |
+
"name": datasets.Value("string"),
|
41 |
+
"n_window": datasets.Value("int16"),
|
42 |
+
**{name: datasets.Audio(sampling_rate=self.SAMPLING_RATE, mono=False)
|
43 |
+
for name in self.INSTRUMENT_NAMES}
|
44 |
+
})
|
45 |
+
)
|
46 |
+
|
47 |
+
def _split_generators(self, dl_manager):
|
48 |
+
#! you must have your folder locally!
|
49 |
+
archive_path = dl_manager.download_and_extract(
|
50 |
+
"https://zenodo.org/record/1117372/files/musdb18.zip?download=1")
|
51 |
+
|
52 |
+
return [
|
53 |
+
datasets.SplitGenerator(
|
54 |
+
name=datasets.Split.TRAIN,
|
55 |
+
gen_kwargs={
|
56 |
+
"audio_path": f"{archive_path}/train"}
|
57 |
+
),
|
58 |
+
datasets.SplitGenerator(
|
59 |
+
name=datasets.Split.TEST,
|
60 |
+
gen_kwargs={
|
61 |
+
"audio_path": f"{archive_path}/test"
|
62 |
+
}
|
63 |
+
)
|
64 |
+
]
|
65 |
+
|
66 |
+
def _generate_stem_dict(self, S, song_name, start):
|
67 |
+
return {name: {"path": f"{song_name}/{name}",
|
68 |
+
"array": S[i, start:start+self.WINDOW_SIZE, :], "sampling_rate": self.SAMPLING_RATE}
|
69 |
+
for i, name in enumerate(self.INSTRUMENT_NAMES)}
|
70 |
+
|
71 |
+
def _generate_examples(self, audio_path):
|
72 |
+
id_ = 0
|
73 |
+
for stems_path in Path(audio_path).iterdir():
|
74 |
+
song_name = stems_path.stem
|
75 |
+
S, sr = stempeg.read_stems(
|
76 |
+
str(stems_path), dtype=np.float32, multiprocess=False)
|
77 |
+
|
78 |
+
for idx, start in enumerate(range(0, S.shape[1], self.WINDOW_SIZE)):
|
79 |
+
yield id_, {
|
80 |
+
"name": song_name,
|
81 |
+
"n_window": idx,
|
82 |
+
**self._generate_stem_dict(S, song_name, start)
|
83 |
+
}
|
84 |
+
|
85 |
+
id_ += 1
|
86 |
+
|
87 |
+
# It's very rare for song to have exactly 3 minutes
|
88 |
+
yield id_, {
|
89 |
+
"name": song_name,
|
90 |
+
"n_window": idx+1,
|
91 |
+
**self._generate_stem_dict(S, song_name, start=S.shape[1] - self.WINDOW_SIZE)
|
92 |
+
}
|
93 |
+
|
94 |
+
id_ += 1
|