File size: 6,081 Bytes
b4bbbc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0ba5a2
b4bbbc2
d0ba5a2
b4bbbc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90f3a87
b4bbbc2
 
 
90f3a87
 
 
 
 
b4bbbc2
 
90f3a87
 
 
b4bbbc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90f3a87
b4bbbc2
 
 
90f3a87
 
 
 
 
b4bbbc2
 
90f3a87
 
 
b4bbbc2
 
 
d0ba5a2
b4bbbc2
 
 
 
 
 
 
 
 
d0ba5a2
b4bbbc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import datasets
import pandas as pd
from datasets.tasks import AudioClassification


_NAMES = [
    # Chinese 0-36
    "gao_hu",
    "er_hu",
    "zhong_hu",
    "ge_hu",
    "di_yin_ge_hu",
    "jing_hu",
    "ban_hu",
    "bang_di",
    "qu_di",
    "xin_di",
    "da_di",
    "gao_yin_sheng",
    "zhong_yin_sheng",
    "di_yin_sheng",
    "gao_yin_suo_na",
    "zhong_yin_suo_na",
    "ci_zhong_yin_suo_na",
    "di_yin_suo_na",
    "gao_yin_guan",
    "zhong_yin_guan",
    "di_yin_guan",
    "bei_di_yin_guan",
    "ba_wu",
    "xun",
    "xiao",
    "liu_qin",
    "xiao_ruan",
    "pi_pa",
    "yang_qin",
    "zhong_ruan",
    "da_ruan",
    "gu_zheng",
    "gu_qin",
    "kong_hou",
    "san_xian",
    "yun_luo",
    "bian_zhong",
    # Western 37-60
    "violin",
    "viola",
    "cello",
    "double_bass",
    "piccolo",
    "flute",
    "oboe",
    "clarinet",
    "bassoon",
    "saxophone",
    "trumpet",
    "trombone",
    "horn",
    "tuba",
    "harp",
    "tubular_bells",
    "bells",
    "xylophone",
    "vibraphone",
    "marimba",
    "piano",
    "clavichord",
    "accordion",
    "organ",
]

_HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{os.path.basename(__file__)[:-3]}"

_DOMAIN = f"{_HOMEPAGE}/resolve/master/data"

_URLS = {
    "audio": f"{_DOMAIN}/audio.zip",
    "mel": f"{_DOMAIN}/mel.zip",
    "Chinese": f"{_DOMAIN}/Chinese.csv",
    "Western": f"{_DOMAIN}/Western.csv",
}


class instrument_timbre(datasets.GeneratorBasedBuilder):
    def _info(self):
        return datasets.DatasetInfo(
            features=datasets.Features(
                {
                    "audio": datasets.Audio(sampling_rate=44100),
                    "mel": datasets.Image(),
                    "instrument": datasets.features.ClassLabel(names=_NAMES),
                    "slim": datasets.Value("float32"),
                    "bright": datasets.Value("float32"),
                    "dark": datasets.Value("float32"),
                    "sharp": datasets.Value("float32"),
                    "thick": datasets.Value("float32"),
                    "thin": datasets.Value("float32"),
                    "vigorous": datasets.Value("float32"),
                    "silvery": datasets.Value("float32"),
                    "raspy": datasets.Value("float32"),
                    "full": datasets.Value("float32"),
                    "coarse": datasets.Value("float32"),
                    "pure": datasets.Value("float32"),
                    "hoarse": datasets.Value("float32"),
                    "consonant": datasets.Value("float32"),
                    "mellow": datasets.Value("float32"),
                    "muddy": datasets.Value("float32"),
                }
            ),
            supervised_keys=("audio", "instrument"),
            homepage=_HOMEPAGE,
            license="CC-BY-NC-ND",
            version="1.2.0",
            task_templates=[
                AudioClassification(
                    task="audio-classification",
                    audio_column="audio",
                    label_column="instrument",
                )
            ],
        )

    def _split_generators(self, dl_manager):
        audio_files = dl_manager.download_and_extract(_URLS["audio"])
        mel_files = dl_manager.download_and_extract(_URLS["mel"])
        cn_ins_eval = dl_manager.download(_URLS["Chinese"])
        en_ins_eval = dl_manager.download(_URLS["Western"])
        cn_labels = pd.read_csv(cn_ins_eval, index_col="instrument_id")
        en_labels = pd.read_csv(en_ins_eval, index_col="instrument_id")
        cn_dataset, en_dataset = {}, {}
        for path in dl_manager.iter_files([audio_files]):
            fname: str = os.path.basename(path)
            i = int(fname.split(".wa")[0]) - 1
            if fname.endswith(".wav"):
                region = os.path.basename(os.path.dirname(path))
                labels = cn_labels if region == "Chinese" else en_labels
                data = {
                    "audio": path,
                    "mel": "",
                    "instrument": labels.iloc[i]["instrument_name"],
                    "slim": labels.iloc[i]["slim"],
                    "bright": labels.iloc[i]["bright"],
                    "dark": labels.iloc[i]["dim"],
                    "sharp": labels.iloc[i]["sharp"],
                    "thick": labels.iloc[i]["thick"],
                    "thin": labels.iloc[i]["thin"],
                    "vigorous": labels.iloc[i]["solid"],
                    "silvery": labels.iloc[i]["clear"],
                    "raspy": labels.iloc[i]["dry"],
                    "full": labels.iloc[i]["plump"],
                    "coarse": labels.iloc[i]["rough"],
                    "pure": labels.iloc[i]["pure"],
                    "hoarse": labels.iloc[i]["hoarse"],
                    "consonant": labels.iloc[i]["harmonious"],
                    "mellow": labels.iloc[i]["soft"],
                    "muddy": labels.iloc[i]["turbid"],
                }
                if region == "Chinese":
                    cn_dataset[i] = data

                else:
                    en_dataset[i] = data

        for path in dl_manager.iter_files([mel_files]):
            fname = os.path.basename(path)
            i = int(fname.split(".jp")[0]) - 1
            if fname.endswith(".jpg"):
                if os.path.basename(os.path.dirname(path)) == "Chinese":
                    cn_dataset[i]["mel"] = path

                else:
                    en_dataset[i]["mel"] = path

        return [
            datasets.SplitGenerator(
                name="Chinese",
                gen_kwargs={
                    "files": [cn_dataset[k] for k in sorted(cn_dataset)],
                },
            ),
            datasets.SplitGenerator(
                name="Western",
                gen_kwargs={
                    "files": [en_dataset[k] for k in sorted(en_dataset)],
                },
            ),
        ]

    def _generate_examples(self, files):
        for i, path in enumerate(files):
            yield i, path