# coding=utf-8 """Pianos sound classification dataset.""" import os import random import textwrap import datasets import itertools import typing as tp from pathlib import Path from ._pianos import _PITCHES _COMPRESSED_FILENAME = 'pianos.zip' _NAMES = [ "PearlRiver", "YoungChang", "Steinway-T", "Hsinghai", "Kawai", "Steinway", "Kawai-G", "Yamaha", ] SAMPLE_RATE = 16_000 _CITATION = """\ @dataset{zhaorui_liu_2021_5676893, author = {Zhaorui Liu, Monan Zhou, Shenyang Xu, Yuan Wang, Zhaowen Wang, Wei Li and Zijin Li}, title = {CCMUSIC DATABASE: A Music Data Sharing Platform for Computational Musicology Research}, month = {nov}, year = {2021}, publisher = {Zenodo}, version = {1.1}, doi = {10.5281/zenodo.5676893}, url = {https://doi.org/10.5281/zenodo.5676893} } """ _DESCRIPTION = """\ 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. """ class PianosConfig(datasets.BuilderConfig): """BuilderConfig for Pianos.""" def __init__(self, features, **kwargs): super(PianosConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs) self.features = features class Pianos(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = [ PianosConfig( features=datasets.Features( { "file": datasets.Value("string"), "audio": datasets.Audio(sampling_rate=SAMPLE_RATE), "sound": datasets.Value("string"), "label": datasets.ClassLabel(names=_NAMES), } ), name="pianos", description=textwrap.dedent(_DESCRIPTION), ), ] def _info(self): return datasets.DatasetInfo( description=textwrap.dedent(_DESCRIPTION), features=self.config.features, supervised_keys=None, homepage="", citation=textwrap.dedent(_CITATION), task_templates=None, ) def _split_generators(self, dl_manager): data_files = dl_manager.extract(_COMPRESSED_FILENAME) dataset = [] for path in dl_manager.iter_files([data_files]): fname = os.path.basename(path) if fname.endswith(".wav"): dataset.append( { "file": path, "audio": path, "label": os.path.basename(os.path.dirname(path)), "sound": os.path.basename(os.path.dirname(path)), } ) random.shuffle(dataset) count = len(dataset) p80 = int(0.8 * count) p90 = int(0.9 * count) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={"files": dataset[:p80]} ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, gen_kwargs={"files": dataset[p80:p90]} ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={"files": dataset[p90:]} ), ] def _generate_examples(self, files): for guid, path in enumerate(files): yield guid, { "id": str(guid), "file": path["file"], "audio": path["audio"], "label": path["label"], "sound": path["sound"], }