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", ] _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}" _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 encompasses subjective timbre evaluation scores comprising 16 terms, such as bright, dark, raspy, etc, evaluated across 37 Chinese instruments and 24 Western instruments by 14 participants with musical backgrounds in a subjective evaluation experiment. Additionally, it includes 10 reports on spectrogram analysis of 10 instruments. During the integration, we have crafted the Chinese part and the Non-Chinese part into two splits. Each split is composed of multiple data entries, with each entry structured across 18 columns. The Chinese split encompasses 37 entries, while the Non-Chinese split includes 24 entries. The premier column of each data entry presents the instrument recordings in the .wav format, sampled at a rate of 22,050 Hz. The second column provides the Chinese pinyin or English name of the instrument. The subsequent 16 columns correspond to the 10-point score of the 16 terms. This dataset is suitable for conducting timber analysis of musical instruments and can also be utilized for various single or multiple regression tasks related to term scoring. """ _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( description=_DESCRIPTION, features=datasets.Features( { "audio": datasets.Audio(sampling_rate=22050), "mel": datasets.Image(), "instrument": datasets.features.ClassLabel(names=_NAMES), "slim": datasets.Value("float32"), "bright": datasets.Value("float32"), "dim": datasets.Value("float32"), "sharp": datasets.Value("float32"), "thick": datasets.Value("float32"), "thin": datasets.Value("float32"), "solid": datasets.Value("float32"), "clear": datasets.Value("float32"), "dry": datasets.Value("float32"), "plump": datasets.Value("float32"), "rough": datasets.Value("float32"), "pure": datasets.Value("float32"), "hoarse": datasets.Value("float32"), "harmonious": datasets.Value("float32"), "soft": datasets.Value("float32"), "turbid": datasets.Value("float32"), } ), supervised_keys=("audio", "instrument"), homepage=_HOMEPAGE, license="CC-BY-NC-ND", version="1.2.0", citation=_CITATION, 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"], "dim": labels.iloc[i]["dim"], "sharp": labels.iloc[i]["sharp"], "thick": labels.iloc[i]["thick"], "thin": labels.iloc[i]["thin"], "solid": labels.iloc[i]["solid"], "clear": labels.iloc[i]["clear"], "dry": labels.iloc[i]["dry"], "plump": labels.iloc[i]["plump"], "rough": labels.iloc[i]["rough"], "pure": labels.iloc[i]["pure"], "hoarse": labels.iloc[i]["hoarse"], "harmonious": labels.iloc[i]["harmonious"], "soft": labels.iloc[i]["soft"], "turbid": 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