Spaces:
V3N0M
/
Runtime error

File size: 6,229 Bytes
7bc29af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

import argparse
import os
from collections import defaultdict, namedtuple
from pathlib import Path

import musdb
import numpy as np
import torch as th
import tqdm
from torch.utils.data import DataLoader

from .audio import AudioFile

ChunkInfo = namedtuple("ChunkInfo", ["file_index", "offset", "local_index"])


class Rawset:
    """
    Dataset of raw, normalized, float32 audio files
    """
    def __init__(self, path, samples=None, stride=None, channels=2, streams=None):
        self.path = Path(path)
        self.channels = channels
        self.samples = samples
        if stride is None:
            stride = samples if samples is not None else 0
        self.stride = stride
        entries = defaultdict(list)
        for root, folders, files in os.walk(self.path, followlinks=True):
            folders.sort()
            files.sort()
            for file in files:
                if file.endswith(".raw"):
                    path = Path(root) / file
                    name, stream = path.stem.rsplit('.', 1)
                    entries[(path.parent.relative_to(self.path), name)].append(int(stream))

        self._entries = list(entries.keys())

        sizes = []
        self._lengths = []
        ref_streams = sorted(entries[self._entries[0]])
        assert ref_streams == list(range(len(ref_streams)))
        if streams is None:
            self.streams = ref_streams
        else:
            self.streams = streams
        for entry in sorted(entries.keys()):
            streams = entries[entry]
            assert sorted(streams) == ref_streams
            file = self._path(*entry)
            length = file.stat().st_size // (4 * channels)
            if samples is None:
                sizes.append(1)
            else:
                if length < samples:
                    self._entries.remove(entry)
                    continue
                sizes.append((length - samples) // stride + 1)
            self._lengths.append(length)
        if not sizes:
            raise ValueError(f"Empty dataset {self.path}")
        self._cumulative_sizes = np.cumsum(sizes)
        self._sizes = sizes

    def __len__(self):
        return self._cumulative_sizes[-1]

    @property
    def total_length(self):
        return sum(self._lengths)

    def chunk_info(self, index):
        file_index = np.searchsorted(self._cumulative_sizes, index, side='right')
        if file_index == 0:
            local_index = index
        else:
            local_index = index - self._cumulative_sizes[file_index - 1]
        return ChunkInfo(offset=local_index * self.stride,
                         file_index=file_index,
                         local_index=local_index)

    def _path(self, folder, name, stream=0):
        return self.path / folder / (name + f'.{stream}.raw')

    def __getitem__(self, index):
        chunk = self.chunk_info(index)
        entry = self._entries[chunk.file_index]

        length = self.samples or self._lengths[chunk.file_index]
        streams = []
        to_read = length * self.channels * 4
        for stream_index, stream in enumerate(self.streams):
            offset = chunk.offset * 4 * self.channels
            file = open(self._path(*entry, stream=stream), 'rb')
            file.seek(offset)
            content = file.read(to_read)
            assert len(content) == to_read
            content = np.frombuffer(content, dtype=np.float32)
            content = content.copy()  # make writable
            streams.append(th.from_numpy(content).view(length, self.channels).t())
        return th.stack(streams, dim=0)

    def name(self, index):
        chunk = self.chunk_info(index)
        folder, name = self._entries[chunk.file_index]
        return folder / name


class MusDBSet:
    def __init__(self, mus, streams=slice(None), samplerate=44100, channels=2):
        self.mus = mus
        self.streams = streams
        self.samplerate = samplerate
        self.channels = channels

    def __len__(self):
        return len(self.mus.tracks)

    def __getitem__(self, index):
        track = self.mus.tracks[index]
        return (track.name, AudioFile(track.path).read(channels=self.channels,
                                                       seek_time=0,
                                                       streams=self.streams,
                                                       samplerate=self.samplerate))


def build_raw(mus, destination, normalize, workers, samplerate, channels):
    destination.mkdir(parents=True, exist_ok=True)
    loader = DataLoader(MusDBSet(mus, channels=channels, samplerate=samplerate),
                        batch_size=1,
                        num_workers=workers,
                        collate_fn=lambda x: x[0])
    for name, streams in tqdm.tqdm(loader):
        if normalize:
            ref = streams[0].mean(dim=0)  # use mono mixture as reference
            streams = (streams - ref.mean()) / ref.std()
        for index, stream in enumerate(streams):
            open(destination / (name + f'.{index}.raw'), "wb").write(stream.t().numpy().tobytes())


def main():
    parser = argparse.ArgumentParser('rawset')
    parser.add_argument('--workers', type=int, default=10)
    parser.add_argument('--samplerate', type=int, default=44100)
    parser.add_argument('--channels', type=int, default=2)
    parser.add_argument('musdb', type=Path)
    parser.add_argument('destination', type=Path)

    args = parser.parse_args()

    build_raw(musdb.DB(root=args.musdb, subsets=["train"], split="train"),
              args.destination / "train",
              normalize=True,
              channels=args.channels,
              samplerate=args.samplerate,
              workers=args.workers)
    build_raw(musdb.DB(root=args.musdb, subsets=["train"], split="valid"),
              args.destination / "valid",
              normalize=True,
              samplerate=args.samplerate,
              channels=args.channels,
              workers=args.workers)


if __name__ == "__main__":
    main()