File size: 3,869 Bytes
2763816
 
 
 
 
 
6fb3b71
2763816
 
 
 
 
 
 
 
 
 
 
 
 
69ba139
2763816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69ba139
2763816
 
69ba139
2763816
 
aeccc9b
 
 
 
 
 
69ba139
aeccc9b
 
 
 
 
 
2763816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aeccc9b
 
 
 
 
 
 
 
 
 
 
 
061e719
aeccc9b
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
# coding=utf-8

"""Nsynth Database."""


import os
import json
import textwrap
import datasets
import itertools
import typing as tp
from pathlib import Path
from sklearn.model_selection import train_test_split

SAMPLE_RATE = 16_000

_TRAIN_FILENAME = 'nsynth-train.jsonwav.tar.gz'
_VAL_FILENAME = 'nsynth-valid.jsonwav.tar.gz'
_TEST_FILENAME = 'nsynth-test.jsonwav.tar.gz'

_INSTRUMENT_CLASSES = ['bass', 'brass', 'flute', 'guitar', 'keyboard', 'mallet', 'organ', 'reed', 'string', 'synth_lead', 'vocal']


class NsynthConfig(datasets.BuilderConfig):
    """BuilderConfig for Nsynth."""
    
    def __init__(self, features, **kwargs):
        super(NsynthConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
        self.features = features


class Nsynth(datasets.GeneratorBasedBuilder):

    BUILDER_CONFIGS = [
        NsynthConfig(
            features=datasets.Features(
                {
                    # "file": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
                    "instrument": datasets.Value("string"),
                    "label": datasets.ClassLabel(names=_INSTRUMENT_CLASSES),
                }
            ),
            name="instrument", 
            description='',
        ), 
        NsynthConfig(
            features=datasets.Features(
                {
                    # "file": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
                    "pitch": datasets.Value("string"),
                    "label": datasets.ClassLabel(names=[str(idx) for idx in range(128)]),
                }
            ),
            name="pitch", 
            description='',
        ), 
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description="",
            features=self.config.features,
            supervised_keys=None,
            homepage="",
            citation="",
            task_templates=None,
        )

    def _split_generators(self, dl_manager):
        dl_paths = dl_manager.extract({
            'train': _TRAIN_FILENAME,
            'validation': _VAL_FILENAME,
            'test': _TEST_FILENAME,
        })
        return[
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": dl_paths['train']}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"filepath": dl_paths['validation']}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"filepath": dl_paths['test']}
            ),
        ]

    def _generate_examples(self, filepath):
        dir_list = os.listdir(filepath)
        folder_path = os.path.join(filepath, dir_list[0])
        examples_path = os.path.join(folder_path, 'examples.json')
        with open(examples_path) as examples_file:
            examples = json.load(examples_file)
        wav_dict = {}
        audio_path = os.path.join(folder_path, "audio")

        for filename in os.listdir(audio_path):
            audio_filename = os.path.join(audio_path, filename)
            guid = filename.replace('.wav', '')
            if self.config.name == 'instrument':
                yield guid, {
                    "id": str(guid),
                    "audio": audio_filename, 
                    "instrument": examples[guid]["instrument_family_str"], 
                    "label": examples[guid]["instrument_family_str"], 
                }
            elif self.config.name == 'pitch':
                yield guid, {
                    "id": str(guid),
                    "audio": audio_filename, 
                    "pitch": examples[guid]["pitch"], 
                    "label": examples[guid]["pitch"], 
                }