File size: 15,055 Bytes
0c79644
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# Copyright (c) Meta Platforms, Inc. and 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.

from functools import partial
from itertools import product
import json
import math
import os
import random
import typing as tp

import pytest
import torch
from torch.utils.data import DataLoader

from audiocraft.data.audio_dataset import (
    AudioDataset,
    AudioMeta,
    _get_audio_meta,
    load_audio_meta,
    save_audio_meta
)
from audiocraft.data.zip import PathInZip

from ..common_utils import TempDirMixin, get_white_noise, save_wav


class TestAudioMeta(TempDirMixin):

    def test_get_audio_meta(self):
        sample_rates = [8000, 16_000]
        channels = [1, 2]
        duration = 1.
        for sample_rate, ch in product(sample_rates, channels):
            n_frames = int(duration * sample_rate)
            wav = get_white_noise(ch, n_frames)
            path = self.get_temp_path('sample.wav')
            save_wav(path, wav, sample_rate)
            m = _get_audio_meta(path, minimal=True)
            assert m.path == path, 'path does not match'
            assert m.sample_rate == sample_rate, 'sample rate does not match'
            assert m.duration == duration, 'duration does not match'
            assert m.amplitude is None
            assert m.info_path is None

    def test_save_audio_meta(self):
        audio_meta = [
            AudioMeta("mypath1", 1., 16_000, None, None, PathInZip('/foo/bar.zip:/relative/file1.json')),
            AudioMeta("mypath2", 2., 16_000, None, None, PathInZip('/foo/bar.zip:/relative/file2.json'))
            ]
        empty_audio_meta = []
        for idx, meta in enumerate([audio_meta, empty_audio_meta]):
            path = self.get_temp_path(f'data_{idx}_save.jsonl')
            save_audio_meta(path, meta)
            with open(path, 'r') as f:
                lines = f.readlines()
                read_meta = [AudioMeta.from_dict(json.loads(line)) for line in lines]
                assert len(read_meta) == len(meta)
                for m, read_m in zip(meta, read_meta):
                    assert m == read_m

    def test_load_audio_meta(self):
        try:
            import dora
        except ImportError:
            dora = None  # type: ignore

        audio_meta = [
            AudioMeta("mypath1", 1., 16_000, None, None, PathInZip('/foo/bar.zip:/relative/file1.json')),
            AudioMeta("mypath2", 2., 16_000, None, None, PathInZip('/foo/bar.zip:/relative/file2.json'))
            ]
        empty_meta = []
        for idx, meta in enumerate([audio_meta, empty_meta]):
            path = self.get_temp_path(f'data_{idx}_load.jsonl')
            with open(path, 'w') as f:
                for m in meta:
                    json_str = json.dumps(m.to_dict()) + '\n'
                    f.write(json_str)
            read_meta = load_audio_meta(path)
            assert len(read_meta) == len(meta)
            for m, read_m in zip(meta, read_meta):
                if dora:
                    m.path = dora.git_save.to_absolute_path(m.path)
                assert m == read_m, f'original={m}, read={read_m}'


class TestAudioDataset(TempDirMixin):

    def _create_audio_files(self,
                            root_name: str,
                            num_examples: int,
                            durations: tp.Union[float, tp.Tuple[float, float]] = (0.1, 1.),
                            sample_rate: int = 16_000,
                            channels: int = 1):
        root_dir = self.get_temp_dir(root_name)
        for i in range(num_examples):
            if isinstance(durations, float):
                duration = durations
            elif isinstance(durations, tuple) and len(durations) == 1:
                duration = durations[0]
            elif isinstance(durations, tuple) and len(durations) == 2:
                duration = random.uniform(durations[0], durations[1])
            else:
                assert False
            n_frames = int(duration * sample_rate)
            wav = get_white_noise(channels, n_frames)
            path = os.path.join(root_dir, f'example_{i}.wav')
            save_wav(path, wav, sample_rate)
        return root_dir

    def _create_audio_dataset(self,
                              root_name: str,
                              total_num_examples: int,
                              durations: tp.Union[float, tp.Tuple[float, float]] = (0.1, 1.),
                              sample_rate: int = 16_000,
                              channels: int = 1,
                              segment_duration: tp.Optional[float] = None,
                              num_examples: int = 10,
                              shuffle: bool = True,
                              return_info: bool = False):
        root_dir = self._create_audio_files(root_name, total_num_examples, durations, sample_rate, channels)
        dataset = AudioDataset.from_path(root_dir,
                                         minimal_meta=True,
                                         segment_duration=segment_duration,
                                         num_samples=num_examples,
                                         sample_rate=sample_rate,
                                         channels=channels,
                                         shuffle=shuffle,
                                         return_info=return_info)
        return dataset

    def test_dataset_full(self):
        total_examples = 10
        min_duration, max_duration = 1., 4.
        sample_rate = 16_000
        channels = 1
        dataset = self._create_audio_dataset(
            'dset', total_examples, durations=(min_duration, max_duration),
            sample_rate=sample_rate, channels=channels, segment_duration=None)
        assert len(dataset) == total_examples
        assert dataset.sample_rate == sample_rate
        assert dataset.channels == channels
        for idx in range(len(dataset)):
            sample = dataset[idx]
            assert sample.shape[0] == channels
            assert sample.shape[1] <= int(max_duration * sample_rate)
            assert sample.shape[1] >= int(min_duration * sample_rate)

    def test_dataset_segment(self):
        total_examples = 10
        num_samples = 20
        min_duration, max_duration = 1., 4.
        segment_duration = 1.
        sample_rate = 16_000
        channels = 1
        dataset = self._create_audio_dataset(
            'dset', total_examples, durations=(min_duration, max_duration), sample_rate=sample_rate,
            channels=channels, segment_duration=segment_duration, num_examples=num_samples)
        assert len(dataset) == num_samples
        assert dataset.sample_rate == sample_rate
        assert dataset.channels == channels
        for idx in range(len(dataset)):
            sample = dataset[idx]
            assert sample.shape[0] == channels
            assert sample.shape[1] == int(segment_duration * sample_rate)

    def test_dataset_equal_audio_and_segment_durations(self):
        total_examples = 1
        num_samples = 2
        audio_duration = 1.
        segment_duration = 1.
        sample_rate = 16_000
        channels = 1
        dataset = self._create_audio_dataset(
            'dset', total_examples, durations=audio_duration, sample_rate=sample_rate,
            channels=channels, segment_duration=segment_duration, num_examples=num_samples)
        assert len(dataset) == num_samples
        assert dataset.sample_rate == sample_rate
        assert dataset.channels == channels
        for idx in range(len(dataset)):
            sample = dataset[idx]
            assert sample.shape[0] == channels
            assert sample.shape[1] == int(segment_duration * sample_rate)
        # the random seek_time adds variability on audio read
        sample_1 = dataset[0]
        sample_2 = dataset[1]
        assert not torch.allclose(sample_1, sample_2)

    def test_dataset_samples(self):
        total_examples = 1
        num_samples = 2
        audio_duration = 1.
        segment_duration = 1.
        sample_rate = 16_000
        channels = 1

        create_dataset = partial(
            self._create_audio_dataset,
            'dset', total_examples, durations=audio_duration, sample_rate=sample_rate,
            channels=channels, segment_duration=segment_duration, num_examples=num_samples,
        )

        dataset = create_dataset(shuffle=True)
        # when shuffle = True, we have different inputs for the same index across epoch
        sample_1 = dataset[0]
        sample_2 = dataset[0]
        assert not torch.allclose(sample_1, sample_2)

        dataset_noshuffle = create_dataset(shuffle=False)
        # when shuffle = False, we have same inputs for the same index across epoch
        sample_1 = dataset_noshuffle[0]
        sample_2 = dataset_noshuffle[0]
        assert torch.allclose(sample_1, sample_2)

    def test_dataset_return_info(self):
        total_examples = 10
        num_samples = 20
        min_duration, max_duration = 1., 4.
        segment_duration = 1.
        sample_rate = 16_000
        channels = 1
        dataset = self._create_audio_dataset(
            'dset', total_examples, durations=(min_duration, max_duration), sample_rate=sample_rate,
            channels=channels, segment_duration=segment_duration, num_examples=num_samples, return_info=True)
        assert len(dataset) == num_samples
        assert dataset.sample_rate == sample_rate
        assert dataset.channels == channels
        for idx in range(len(dataset)):
            sample, segment_info = dataset[idx]
            assert sample.shape[0] == channels
            assert sample.shape[1] == int(segment_duration * sample_rate)
            assert segment_info.sample_rate == sample_rate
            assert segment_info.total_frames == int(segment_duration * sample_rate)
            assert segment_info.n_frames <= int(segment_duration * sample_rate)
            assert segment_info.seek_time >= 0

    def test_dataset_return_info_no_segment_duration(self):
        total_examples = 10
        num_samples = 20
        min_duration, max_duration = 1., 4.
        segment_duration = None
        sample_rate = 16_000
        channels = 1
        dataset = self._create_audio_dataset(
            'dset', total_examples, durations=(min_duration, max_duration), sample_rate=sample_rate,
            channels=channels, segment_duration=segment_duration, num_examples=num_samples, return_info=True)
        assert len(dataset) == total_examples
        assert dataset.sample_rate == sample_rate
        assert dataset.channels == channels
        for idx in range(len(dataset)):
            sample, segment_info = dataset[idx]
            assert sample.shape[0] == channels
            assert sample.shape[1] == segment_info.total_frames
            assert segment_info.sample_rate == sample_rate
            assert segment_info.n_frames <= segment_info.total_frames

    def test_dataset_collate_fn(self):
        total_examples = 10
        num_samples = 20
        min_duration, max_duration = 1., 4.
        segment_duration = 1.
        sample_rate = 16_000
        channels = 1
        dataset = self._create_audio_dataset(
            'dset', total_examples, durations=(min_duration, max_duration), sample_rate=sample_rate,
            channels=channels, segment_duration=segment_duration, num_examples=num_samples, return_info=False)
        batch_size = 4
        dataloader = DataLoader(
            dataset,
            batch_size=batch_size,
            num_workers=0
        )
        for idx, batch in enumerate(dataloader):
            assert batch.shape[0] == batch_size

    @pytest.mark.parametrize("segment_duration", [1.0, None])
    def test_dataset_with_meta_collate_fn(self, segment_duration):
        total_examples = 10
        num_samples = 20
        min_duration, max_duration = 1., 4.
        segment_duration = 1.
        sample_rate = 16_000
        channels = 1
        dataset = self._create_audio_dataset(
            'dset', total_examples, durations=(min_duration, max_duration), sample_rate=sample_rate,
            channels=channels, segment_duration=segment_duration, num_examples=num_samples, return_info=True)
        batch_size = 4
        dataloader = DataLoader(
            dataset,
            batch_size=batch_size,
            collate_fn=dataset.collater,
            num_workers=0
        )
        for idx, batch in enumerate(dataloader):
            wav, infos = batch
            assert wav.shape[0] == batch_size
            assert len(infos) == batch_size

    @pytest.mark.parametrize("segment_duration,sample_on_weight,sample_on_duration,a_hist,b_hist,c_hist", [
        [1, True, True, 0.5, 0.5, 0.0],
        [1, False, True, 0.25, 0.5, 0.25],
        [1, True, False, 0.666, 0.333, 0.0],
        [1, False, False, 0.333, 0.333, 0.333],
        [None, False, False, 0.333, 0.333, 0.333]])
    def test_sample_with_weight(self, segment_duration, sample_on_weight, sample_on_duration, a_hist, b_hist, c_hist):
        random.seed(1234)
        rng = torch.Generator()
        rng.manual_seed(1234)

        def _get_histogram(dataset, repetitions=20_000):
            counts = {file_meta.path: 0. for file_meta in meta}
            for _ in range(repetitions):
                file_meta = dataset.sample_file(rng)
                counts[file_meta.path] += 1
            return {name: count / repetitions for name, count in counts.items()}

        meta = [
           AudioMeta(path='a', duration=5, sample_rate=1, weight=2),
           AudioMeta(path='b', duration=10, sample_rate=1, weight=None),
           AudioMeta(path='c', duration=5, sample_rate=1, weight=0),
        ]
        dataset = AudioDataset(
            meta, segment_duration=segment_duration, sample_on_weight=sample_on_weight,
            sample_on_duration=sample_on_duration)
        hist = _get_histogram(dataset)
        assert math.isclose(hist['a'], a_hist, abs_tol=0.01)
        assert math.isclose(hist['b'], b_hist, abs_tol=0.01)
        assert math.isclose(hist['c'], c_hist, abs_tol=0.01)

    def test_meta_duration_filter_all(self):
        meta = [
           AudioMeta(path='a', duration=5, sample_rate=1, weight=2),
           AudioMeta(path='b', duration=10, sample_rate=1, weight=None),
           AudioMeta(path='c', duration=5, sample_rate=1, weight=0),
        ]
        try:
            AudioDataset(meta, segment_duration=11, min_segment_ratio=1)
            assert False
        except AssertionError:
            assert True

    def test_meta_duration_filter_long(self):
        meta = [
           AudioMeta(path='a', duration=5, sample_rate=1, weight=2),
           AudioMeta(path='b', duration=10, sample_rate=1, weight=None),
           AudioMeta(path='c', duration=5, sample_rate=1, weight=0),
        ]
        dataset = AudioDataset(meta, segment_duration=None, min_segment_ratio=1, max_audio_duration=7)
        assert len(dataset) == 2