|
import os |
|
import csv |
|
import random |
|
import hashlib |
|
import datasets |
|
|
|
_HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{os.path.basename(__file__)[:-3]}" |
|
|
|
_DOMAIN = f"{_HOMEPAGE}/resolve/master/data" |
|
|
|
_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 300 pop songs in .mp3 format, sourced from the NetEase music, accompanied by a structure annotation file for each song in .txt format. The annotator for music structure is a professional musician and teacher from the China Conservatory of Music. For the statistics of the dataset, there are 208 Chinese songs, 87 English songs, three Korean songs and two Japanese songs. The song structures are labeled as follows: intro, re-intro, verse, chorus, pre-chorus, post-chorus, bridge, interlude and ending. Fig. 7 shows the frequency of each segment label that appears in the set. The labels chorus and verse are the two most prevalent segment labels in the dataset and they are the most common segment in Western popular music. Among them, the number of “Postchorus” tags is the least, with only two present. |
|
|
|
Unlike the above three datasets for classification, this one has not undergone pre-processing such as spectrogram transform. Thus we provide the original content only. The integrated version of the dataset is organized based on audio files, with each item structured into three columns: The first column contains the audio of the song in .mp3 format, sampled at 44,100 Hz. The second column consists of lists indicating the time points that mark the boundaries of different song sections, while the third column contains lists corresponding to the labels of the song structures listed in the second column. Strictly speaking, the first column represents the data, while the subsequent two columns represent the label. |
|
""" |
|
|
|
_URLS = { |
|
"audio": f"{_DOMAIN}/audio.zip", |
|
"mel": f"{_DOMAIN}/mel.zip", |
|
"label": f"{_DOMAIN}/label.zip", |
|
} |
|
|
|
|
|
class song_structure(datasets.GeneratorBasedBuilder): |
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
features=datasets.Features( |
|
{ |
|
"audio": datasets.Audio(sampling_rate=22050), |
|
"mel": datasets.Image(), |
|
"label": datasets.Sequence( |
|
feature={ |
|
"onset_time": datasets.Value("uint32"), |
|
"offset_time": datasets.Value("uint32"), |
|
"structure": datasets.Value("string"), |
|
} |
|
), |
|
} |
|
), |
|
supervised_keys=("audio", "label"), |
|
homepage=_HOMEPAGE, |
|
license="CC-BY-NC-ND", |
|
version="1.2.0", |
|
citation=_CITATION, |
|
description=_DESCRIPTION, |
|
) |
|
|
|
def _parse_txt_label(self, txt_file): |
|
label = [] |
|
with open(txt_file, mode="r", encoding="utf-8") as file: |
|
reader = csv.reader(file, delimiter="\t") |
|
for row in reader: |
|
if len(row) == 3: |
|
label.append( |
|
{ |
|
"onset_time": int(row[0]), |
|
"offset_time": int(row[1]), |
|
"structure": str(row[2]), |
|
} |
|
) |
|
|
|
return 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"]) |
|
txt_files = dl_manager.download_and_extract(_URLS["label"]) |
|
files = {} |
|
for path in dl_manager.iter_files([audio_files]): |
|
fname: str = os.path.basename(path) |
|
if fname.endswith(".mp3"): |
|
item_id = self._str2md5(fname.split(".mp")[0]) |
|
files[item_id] = {"audio": path} |
|
|
|
for path in dl_manager.iter_files([mel_files]): |
|
fname: str = os.path.basename(path) |
|
if fname.endswith(".jpg"): |
|
item_id = self._str2md5(fname.split(".jp")[0]) |
|
files[item_id]["mel"] = path |
|
|
|
for path in dl_manager.iter_files([txt_files]): |
|
fname: str = os.path.basename(path) |
|
if fname.endswith(".txt"): |
|
item_id = self._str2md5(fname.split(".tx")[0]) |
|
files[item_id]["label"] = self._parse_txt_label(path) |
|
|
|
dataset = list(files.values()) |
|
random.shuffle(dataset) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, gen_kwargs={"files": dataset} |
|
), |
|
] |
|
|
|
def _generate_examples(self, files): |
|
for i, path in enumerate(files): |
|
yield i, path |
|
|