# coding=utf-8 """RAVDESS paralinguistics classification dataset.""" import os import textwrap import datasets import itertools import typing as tp from pathlib import Path from sklearn.model_selection import train_test_split _COMPRESSED_FILENAME = 'ravdess.zip' SAMPLE_RATE = 48_000 RAVDESS_EMOTIONS_MAPPING = { '01': 'neutral', '02': 'calm', '03': 'happy', '04': 'sad', '05': 'angry', '06': 'fearful', '07': 'disgust', '08': 'surprised', } RAVDESS_ACTOR_FOLD_MAPPING = { '01': '5', '02': '1', '03': '2', '04': '5', '05': '1', '06': '2', '07': '2', '08': '4', '09': '5', '10': '3', '11': '3', '12': '3', '13': '2', '14': '1', '15': '1', '16': '1', '17': '4', '18': '2', '19': '3', '20': '3', '21': '4', '22': '5', '23': '4', '24': '4', } CLASSES = list(RAVDESS_EMOTIONS_MAPPING.values()) class RavdessConfig(datasets.BuilderConfig): """BuilderConfig for RAVDESS.""" def __init__(self, features, **kwargs): super(RavdessConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs) self.features = features class RAVDESS(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = [ RavdessConfig( features=datasets.Features( { "file": datasets.Value("string"), "audio": datasets.Audio(sampling_rate=SAMPLE_RATE), "emotion": datasets.Value("string"), "label": datasets.ClassLabel(names=CLASSES), } ), name=f"fold{f}", description='', ) for f in range(1, 6) ] def _info(self): return datasets.DatasetInfo( description="", features=self.config.features, supervised_keys=None, homepage="", citation="", task_templates=None, ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" archive_path = dl_manager.extract(_COMPRESSED_FILENAME) extensions = ['.wav'] _, _walker = fast_scandir(archive_path, extensions, recursive=True) _walker_with_fold = [(fileid, default_find_fold(fileid)) for fileid in _walker] if self.config.name == 'fold1': train_fold = ['2', '3', '4', '5'] test_fold = ['1'] elif self.config.name == 'fold2': train_fold = ['1', '3', '4', '5'] test_fold = ['2'] elif self.config.name == 'fold3': train_fold = ['1', '2', '4', '5'] test_fold = ['3'] elif self.config.name == 'fold4': train_fold = ['1', '2', '3', '5'] test_fold = ['4'] elif self.config.name == 'fold5': train_fold = ['1', '2', '3', '4'] test_fold = ['5'] train_walker = [fileid for fileid, fold in _walker_with_fold if fold in train_fold] test_walker = [fileid for fileid, fold in _walker_with_fold if fold in test_fold] return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={"audio_paths": train_walker, "split": "train"} ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={"audio_paths": test_walker, "split": "test"} ), ] def _generate_examples(self, audio_paths, split=None): for guid, audio_path in enumerate(audio_paths): yield guid, { "id": str(guid), "file": audio_path, "audio": audio_path, "emotion": default_find_classes(audio_path), "label": default_find_classes(audio_path), } def default_find_classes(audio_path): return RAVDESS_EMOTIONS_MAPPING.get(Path(audio_path).name.split('-')[2]) def default_find_fold(audio_path): actor_id = Path(audio_path).parent.stem.split('_')[1] return RAVDESS_ACTOR_FOLD_MAPPING.get(actor_id) def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False): # Scan files recursively faster than glob # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py subfolders, files = [], [] try: # hope to avoid 'permission denied' by this try for f in os.scandir(path): try: # 'hope to avoid too many levels of symbolic links' error if f.is_dir(): subfolders.append(f.path) elif f.is_file(): if os.path.splitext(f.name)[1].lower() in exts: files.append(f.path) except Exception: pass except Exception: pass if recursive: for path in list(subfolders): sf, f = fast_scandir(path, exts, recursive=recursive) subfolders.extend(sf) files.extend(f) # type: ignore return subfolders, files