yangwang825 commited on
Commit
7e9b79a
·
verified ·
1 Parent(s): f22560f

Create pianos.py

Browse files
Files changed (1) hide show
  1. pianos.py +125 -0
pianos.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ """Pianos pitch classification dataset."""
4
+
5
+
6
+ import os
7
+ import random
8
+ import textwrap
9
+ import datasets
10
+ import itertools
11
+ import typing as tp
12
+ from pathlib import Path
13
+
14
+ from ._pianos import _PITCHES
15
+
16
+ _COMPRESSED_FILENAME = 'pianos.zip'
17
+
18
+ _NAMES = [
19
+ "PearlRiver",
20
+ "YoungChang",
21
+ "Steinway-T",
22
+ "Hsinghai",
23
+ "Kawai",
24
+ "Steinway",
25
+ "Kawai-G",
26
+ "Yamaha",
27
+ ]
28
+
29
+ SAMPLE_RATE = 16_000
30
+
31
+ _CITATION = """\
32
+ @dataset{zhaorui_liu_2021_5676893,
33
+ author = {Zhaorui Liu, Monan Zhou, Shenyang Xu, Yuan Wang, Zhaowen Wang, Wei Li and Zijin Li},
34
+ title = {CCMUSIC DATABASE: A Music Data Sharing Platform for Computational Musicology Research},
35
+ month = {nov},
36
+ year = {2021},
37
+ publisher = {Zenodo},
38
+ version = {1.1},
39
+ doi = {10.5281/zenodo.5676893},
40
+ url = {https://doi.org/10.5281/zenodo.5676893}
41
+ }
42
+ """
43
+
44
+ _DESCRIPTION = """\
45
+ Piano-Sound-Quality is a dataset of piano sound. It consists of 8 kinds of pianos including PearlRiver, YoungChang, Steinway-T, Hsinghai, Kawai, Steinway, Kawai-G, Yamaha(recorded by Shaohua Ji with SONY PCM-D100). Data was annotated by students from the China Conservatory of Music (CCMUSIC) in Beijing and collected by Monan Zhou.
46
+ """
47
+
48
+
49
+ class PianosConfig(datasets.BuilderConfig):
50
+ """BuilderConfig for Pianos."""
51
+
52
+ def __init__(self, features, **kwargs):
53
+ super(PianosConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
54
+ self.features = features
55
+
56
+
57
+ class pianos(datasets.GeneratorBasedBuilder):
58
+
59
+ BUILDER_CONFIGS = [
60
+ MswcConfig(
61
+ features=datasets.Features(
62
+ {
63
+ "file": datasets.Value("string"),
64
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
65
+ "pitch": datasets.Value("string"),
66
+ "label": datasets.ClassLabel(names=list(_PITCHES.values()),
67
+ }
68
+ ),
69
+ name="pitch",
70
+ description=textwrap.dedent(_DESCRIPTION),
71
+ ),
72
+ ]
73
+
74
+ def _info(self):
75
+ return datasets.DatasetInfo(
76
+ description=textwrap.dedent(_DESCRIPTION),
77
+ features=self.config.features,
78
+ supervised_keys=None,
79
+ homepage="",
80
+ citation=textwrap.dedent(_CITATION),
81
+ task_templates=None,
82
+ )
83
+
84
+ def _split_generators(self, dl_manager):
85
+ data_files = dl_manager.extract(_COMPRESSED_FILENAME)
86
+ dataset = []
87
+
88
+ for path in dl_manager.iter_files([data_files]):
89
+ fname = os.path.basename(path)
90
+ if fname.endswith(".wav"):
91
+ dataset.append(
92
+ {
93
+ "file": path,
94
+ "audio": path,
95
+ # "label": os.path.basename(os.path.dirname(path)),
96
+ "label": _PITCHES[fname.split("_")[0]],
97
+ "pitch": _PITCHES[fname.split("_")[0]],
98
+ }
99
+ )
100
+
101
+ random.shuffle(dataset)
102
+ count = len(dataset)
103
+ p80 = int(0.8 * count)
104
+ p90 = int(0.9 * count)
105
+
106
+ return [
107
+ datasets.SplitGenerator(
108
+ name=datasets.Split.TRAIN, gen_kwargs={"files": dataset[:p80]}
109
+ ),
110
+ datasets.SplitGenerator(
111
+ name=datasets.Split.VALIDATION, gen_kwargs={"files": dataset[p80:p90]}
112
+ ),
113
+ datasets.SplitGenerator(
114
+ name=datasets.Split.TEST, gen_kwargs={"files": dataset[p90:]}
115
+ ),
116
+ ]
117
+
118
+ def _generate_examples(self, files):
119
+ for guid, path in enumerate(files):
120
+ yield guid, {
121
+ "id": str(guid),
122
+ "audio": path["audio"],
123
+ "label": path["label"],
124
+ "pitch": path["pitch"],
125
+ }