File size: 4,006 Bytes
d8599ba 6e93b7b d8599ba 6e93b7b d8599ba 6e93b7b d8599ba 6e93b7b d8599ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
import os
import random
import hashlib
import datasets
from datasets.tasks import AudioClassification
_HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{os.path.basename(__file__)[:-3]}"
_DOMAIN = f"{_HOMEPAGE}/resolve/master/data"
_NAMES = {
"vibrato": ["颤音", "chan4_yin1"],
"upward_portamento": ["上滑音", "shang4_hua2_yin1"],
"downward_portamento": ["下滑音", "xia4_hua2_yin1"],
"returning_portamento": ["回滑音", "hui2_hua2_yin1"],
"glissando": ["刮奏, 花指", "gua1_zou4/hua1_zhi3"],
"tremolo": ["摇指", "yao2_zhi3"],
"harmonics": ["泛音", "fan4_yin1"],
"plucks": ["勾, 打, 抹, 托, ...", "gou1/da3/mo3/tuo1/etc"],
}
_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=44100),
"mel": datasets.Image(),
"label": datasets.features.ClassLabel(names=list(_NAMES.keys())),
"cname": datasets.Value("string"),
"pinyin": datasets.Value("string"),
}
),
supervised_keys=("audio", "label"),
homepage=_HOMEPAGE,
license="CC-BY-NC-ND",
version="1.2.0",
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][0],
"pinyin": _NAMES[pt][1],
}
|