pianos / piano_sound_quality.py
George
upl all
07529c3
raw
history blame
5.2 kB
import io
import os
import wave
import zipfile
import datasets
import requests
from datasets.tasks import AudioClassification
# Once upload a new piano brand, please register its name here
_NAMES = [
"1_PearlRiver",
"2_YoungChang",
"3_Steinway-T",
"4_Hsinghai",
"5_Kawai",
"6_Steinway",
"7_Kawai-G",
"8_Yamaha",
]
_DBNAME = os.path.basename(__file__).split('.')[0]
_HOMEPAGE = "https://huggingface.co/datasets/ccmusic-database/" + _DBNAME
_CITATION = """\
@dataset{zhaorui_liu_2021_5676893,
author = {Zhaorui Liu and Zijin Li},
title = {{Music Data Sharing Platform for Computational Musicology Research (CCMUSIC DATASET)}},
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-Database is a dataset of piano sound.
It consists of 8 kinds of pianos including
PearlRiver, YoungChang, Steinway-T, Hsinghai, Kawai, Steinway, Kawai-G, Yamaha.
Data was annotated by students from the China Conservatory of Music (CCMUSIC) in Beijing
and collected by George Chou.
"""
_URLS = {piano: _HOMEPAGE + "/resolve/main/data/" +
piano + ".zip" for piano in _NAMES}
_PITCHES = {"009": "A2", "010": "A2#/B2b", "011": "B2", "100": "C1", "101": "C1#/D1b", "102": "D1", "103": "D1#/E1b",
"104": "E1", "105": "F1", "106": "F1#/G1b", "107": "G1", "108": "G1#/A1b", "109": "A1", "110": "A1#/B1b",
"111": "B1", "200": "C", "201": "C#/Db", "202": "D", "203": "D#/Eb", "204": "E", "205": "F", "206": "F#/Gb",
"207": "G", "208": "G#/Ab", "209": "A", "210": "A#/Bb", "211": "B", "300": "c", "301": "c#/db", "302": "d",
"303": "d#/eb", "304": "e", "305": "f", "306": "f#/gb", "307": "g", "308": "g#/ab", "309": "a", "310": "a#/bb",
"311": "b", "400": "c1", "401": "c1#/d1b", "402": "d1", "403": "d1#/e1b", "404": "e1", "405": "f1",
"406": "f1#/g1b", "407": "g1", "408": "g1#/a1b", "409": "a1", "410": "a1#/b1b", "411": "b1", "500": "c2",
"501": "c2#/d2b", "502": "d2", "503": "d2#/e2b", "504": "e2", "505": "f2", "506": "f2#/g2b", "507": "g2",
"508": "g2#/a2b", "509": "a2", "510": "a2#/b2b", "511": "b2", "600": "c3", "601": "c3#/d3b", "602": "d3",
"603": "d3#/e3b", "604": "e3", "605": "f3", "606": "f3#/g3b", "607": "g3", "608": "g3#/a3b", "609": "a3",
"610": "a3#/b3b", "611": "b3", "700": "c4", "701": "c4#/d4b", "702": "d4", "703": "d4#/e4b", "704": "e4",
"705": "f4", "706": "f4#/g4b", "707": "g4", "708": "g4#/a4b", "709": "a4", "710": "a4#/b4b", "711": "b4",
"800": "c5"}
class piano_sound_quality(datasets.GeneratorBasedBuilder):
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"audio": datasets.Audio(sampling_rate=44_100),
"label": datasets.features.ClassLabel(names=_NAMES),
"pitch": datasets.Value("string"),
"duration": datasets.Value("float64"),
}
),
supervised_keys=("audio", "label"),
homepage=_HOMEPAGE,
license="mit",
citation=_CITATION,
task_templates=[
AudioClassification(
task="audio-classification",
audio_column="audio",
label_column="label",
)
],
)
def _get_wav_duration(self, file_bytes):
with wave.open(io.BytesIO(file_bytes), 'r') as wav_file:
frames = wav_file.getnframes()
rate = wav_file.getframerate()
duration = frames / float(rate)
return round(duration, 3)
def _read_zip(self, zip_url, wav_file_path):
resp = requests.get(zip_url)
with zipfile.ZipFile(io.BytesIO(resp.content)) as zip_file:
with zip_file.open(wav_file_path) as file:
file_data = file.read()
return self._get_wav_duration(file_data)
def _split_generators(self, dl_manager):
data_files = dl_manager.download_and_extract(_URLS)
split_generator = []
for index in _URLS.keys():
split_generator.append(
datasets.SplitGenerator(
name=index.replace('-', '_'),
gen_kwargs={
"files": dl_manager.iter_files([data_files[index]]),
},
)
)
return split_generator
def _generate_examples(self, files):
for i, path in enumerate(files):
file_name = os.path.basename(path)
if file_name.endswith(".wav"):
yield i, {
"audio": path,
"label": os.path.basename(os.path.dirname(path)),
"pitch": _PITCHES[file_name[1:4]],
"duration": self._read_zip(path.split('::')[1], path.split('::')[0].split('//')[1]),
}