import os import random import hashlib import datasets from datasets.tasks import AudioClassification _DBNAME = os.path.basename(__file__).split(".")[0] _DOMAIN = f"https://www.modelscope.cn/api/v1/datasets/ccmusic-database/{_DBNAME}/repo?Revision=master&FilePath=data" _HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{_DBNAME}" _NAMES = { "vibrato": "颤音", "upward_portamento": "上滑音", "downward_portamento": "下滑音", "returning_portamento": "回滑音", "glissando": "刮奏, 花指", "tremolo": "摇指", "harmonics": "泛音", "plucks": "勾, 打, 抹, 托, ...", } _CITATION = """\ @dataset{zhaorui_liu_2021_5676893, author = {Monan Zhou, Shenyang Xu, Zhaorui Liu, Zhaowen Wang, Feng Yu, Wei Li and Baoqiang Han}, title = {CCMusic: an Open and Diverse Database for Chinese and General Music Information Retrieval Research}, month = {mar}, year = {2024}, publisher = {HuggingFace}, version = {1.2}, url = {https://huggingface.co/ccmusic-database} } """ _DESCRIPTION = """\ The raw dataset comprises 2,824 audio clips showcasing various guzheng playing techniques. Specifically, 2,328 clips were sourced from virtual sound banks, while 496 clips were performed by a skilled professional guzheng artist. These recordings encompass a comprehensive range of tones inherent to the guzheng instrument. Categorization of the clips is based on the diverse playing techniques characteristic of the guzheng, the clips are divided into eight categories: Vibrato (chanyin), Upward Portamento (shanghuayin), Downward Portamento (xiahuayin), Returning Portamento (huihuayin), Glissando (guazou, huazhi), Tremolo (yaozhi), Harmonic (fanyin), Plucks (gou, da, mo, tuo…). Due to the pre-existing split in the raw dataset, wherein the data has been partitioned approximately in a 4:1 ratio for training and testing sets, we uphold the original data division approach. In contrast to utilizing platform-specific automated splitting mechanisms, we directly employ the pre-split data for subsequent integration steps. """ _URLS = { "audio": f"{_DOMAIN}/audio.zip", "mel": f"{_DOMAIN}/mel.zip", } class GZ_IsoTech(datasets.GeneratorBasedBuilder): def _info(self): return datasets.DatasetInfo( features=datasets.Features( { "audio": datasets.Audio(sampling_rate=22050), "mel": datasets.Image(), "label": datasets.features.ClassLabel(names=list(_NAMES.keys())), "cname": datasets.Value("string"), } ), supervised_keys=("audio", "label"), homepage=_HOMEPAGE, license="CC-BY-NC-ND", version="1.2.0", citation=_CITATION, description=_DESCRIPTION, task_templates=[ AudioClassification( task="audio-classification", audio_column="audio", label_column="label", ) ], ) def _str2md5(self, original_string: str): md5_obj = hashlib.md5() md5_obj.update(original_string.encode("utf-8")) return md5_obj.hexdigest() def _split_generators(self, dl_manager): audio_files = dl_manager.download_and_extract(_URLS["audio"]) mel_files = dl_manager.download_and_extract(_URLS["mel"]) train_files, test_files = {}, {} for path in dl_manager.iter_files([audio_files]): fname: str = os.path.basename(path) dirname = os.path.dirname(path) splt = os.path.basename(os.path.dirname(dirname)) if fname.endswith(".wav"): cls = f"{splt}/{os.path.basename(dirname)}/" item_id = self._str2md5(cls + fname.split(".wa")[0]) if splt == "train": train_files[item_id] = {"audio": path} else: test_files[item_id] = {"audio": path} for path in dl_manager.iter_files([mel_files]): fname = os.path.basename(path) dirname = os.path.dirname(path) splt = os.path.basename(os.path.dirname(dirname)) if fname.endswith(".jpg"): cls = f"{splt}/{os.path.basename(dirname)}/" item_id = self._str2md5(cls + fname.split(".jp")[0]) if splt == "train": train_files[item_id]["mel"] = path else: test_files[item_id]["mel"] = path trainset = list(train_files.values()) testset = list(test_files.values()) random.shuffle(trainset) random.shuffle(testset) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={"files": trainset}, ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={"files": testset}, ), ] def _generate_examples(self, files): for i, path in enumerate(files): pt = os.path.basename(os.path.dirname(path["audio"])) yield i, { "audio": path["audio"], "mel": path["mel"], "label": pt, "cname": _NAMES[pt], }