File size: 3,147 Bytes
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
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}
            })
        )

    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, start):
        return {name: {"path": f"{song_name}/{name}",
                       "array": S[i, start:start+self.WINDOW_SIZE, :], "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)

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

                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, start=S.shape[1] - self.WINDOW_SIZE)
            }

            id_ += 1