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", ] _HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{os.path.basename(__file__)[:-3]}" _DOMAIN = f"{_HOMEPAGE}/resolve/master/data" _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( features=datasets.Features( { "audio": datasets.Audio(sampling_rate=44100), "mel": datasets.Image(), "instrument": datasets.features.ClassLabel(names=_NAMES), "slim": datasets.Value("float32"), "bright": datasets.Value("float32"), "dark": datasets.Value("float32"), "sharp": datasets.Value("float32"), "thick": datasets.Value("float32"), "thin": datasets.Value("float32"), "vigorous": datasets.Value("float32"), "silvery": datasets.Value("float32"), "raspy": datasets.Value("float32"), "full": datasets.Value("float32"), "coarse": datasets.Value("float32"), "pure": datasets.Value("float32"), "hoarse": datasets.Value("float32"), "consonant": datasets.Value("float32"), "mellow": datasets.Value("float32"), "muddy": datasets.Value("float32"), } ), supervised_keys=("audio", "instrument"), homepage=_HOMEPAGE, license="CC-BY-NC-ND", version="1.2.0", 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"], "dark": labels.iloc[i]["dim"], "sharp": labels.iloc[i]["sharp"], "thick": labels.iloc[i]["thick"], "thin": labels.iloc[i]["thin"], "vigorous": labels.iloc[i]["solid"], "silvery": labels.iloc[i]["clear"], "raspy": labels.iloc[i]["dry"], "full": labels.iloc[i]["plump"], "coarse": labels.iloc[i]["rough"], "pure": labels.iloc[i]["pure"], "hoarse": labels.iloc[i]["hoarse"], "consonant": labels.iloc[i]["harmonious"], "mellow": labels.iloc[i]["soft"], "muddy": 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