File size: 3,876 Bytes
1857370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8a36a6
 
 
 
 
 
 
 
 
 
 
 
 
 
1857370
 
 
 
 
f8a36a6
 
1857370
 
 
 
f8a36a6
1857370
 
 
f8a36a6
 
1857370
 
ea33064
f8a36a6
 
 
 
 
 
 
 
1857370
 
 
 
 
 
f8a36a6
 
 
 
 
 
 
 
 
1857370
f8a36a6
 
 
1857370
 
 
f8a36a6
 
 
1857370
 
 
 
 
 
 
f8a36a6
 
 
 
1857370
 
 
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
import datasets
from pathlib import Path
import stempeg
import numpy as np


_DESCRIPTION = """\
    MUSDB18 music source separation dataset

    to open original stem file (mp4), which is done internally you need stempeg library.
    Outcome of mp4 file is a 3 dimensional np_array [n_stems, n_samples, sample_rate].
    
    firt dimension meanings: {
        0: mixture.
        1: drugs,
        2: bass,
        3: others,
        4:vocals,
    }

    Original dataset is not cutted in any parts, but here I cut each song in 10 seconds chunks with 1 sec overlap.
    """

_DESCRIPTION = "musdb dataset"


class Musdb18Dataset(datasets.GeneratorBasedBuilder):
    DEFAULT_WRITER_BATCH_SIZE = 300
    SAMPLING_RATE = 44100
    WINDOW_SIZE = SAMPLING_RATE * 10  # 10s windows
    INSTRUMENT_NAMES = ["mixture", "drums", "bass", "other", "vocals"]

    #! To configure different configurations (length of window is the only thing)
    # use datasets.BuilderConfig

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "name": datasets.Value("string"),
                    "n_window": datasets.Value("int16"),
                    **{
                        name: datasets.Audio(
                            sampling_rate=self.SAMPLING_RATE, mono=False
                        )
                        for name in self.INSTRUMENT_NAMES
                    },
                    "mean": datasets.Value("float"),
                    "std": datasets.Value("float"),
                }
            ),
        )

    def _split_generators(self, dl_manager):
        #! you must have your folder locally!
        archive_path = dl_manager.download_and_extract(
            "https://zenodo.org/record/1117372/files/musdb18.zip?download=1"
        )

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"audio_path": f"{archive_path}/train"},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"audio_path": f"{archive_path}/test"},
            ),
        ]

    def _generate_stem_dict(self, S, song_name, end):
        return {
            name: {
                "path": f"{song_name}/{name}",
                "array": S[i, end - self.WINDOW_SIZE : end, :],
                "sampling_rate": self.SAMPLING_RATE,
            }
            for i, name in enumerate(self.INSTRUMENT_NAMES)
        }

    def _generate_examples(self, audio_path):
        id_ = 0
        for stems_path in Path(audio_path).iterdir():
            song_name = stems_path.stem
            S, sr = stempeg.read_stems(
                str(stems_path), dtype=np.float32, multiprocess=False
            )

            mixture = S.sum(axis=0).T
            assert mixture.shape[0] == 2
            # from (n_instr, n_chann, n_samp) -> (n_chann, n_samp)
            mixture = mixture.mean(0)  # channel_wise mean -> (n_samples,)
            mean = mixture.mean().item()
            std = mixture.std().item()

            for idx, end in enumerate(
                range(self.WINDOW_SIZE, S.shape[1], self.WINDOW_SIZE)
            ):
                yield id_, {
                    "name": song_name,
                    "n_window": idx,
                    **self._generate_stem_dict(S, song_name, end),
                    "mean": mean,
                    "std": std,
                }

                id_ += 1

            # It's very rare for song to have exactly 3 minutes
            yield id_, {
                "name": song_name,
                "n_window": idx + 1,
                **self._generate_stem_dict(S, song_name, end=S.shape[1]),
                "mean": mean,
                "std": std,
            }

            id_ += 1