| |
|
|
| """Nsynth Database.""" |
|
|
|
|
| import os |
| import json |
| import textwrap |
| import datasets |
| import itertools |
| import typing as tp |
| from pathlib import Path |
| from sklearn.model_selection import train_test_split |
|
|
| SAMPLE_RATE = 16_000 |
|
|
| _TRAIN_FILENAME = 'nsynth-train.jsonwav.tar.gz' |
| _VAL_FILENAME = 'nsynth-valid.jsonwav.tar.gz' |
| _TEST_FILENAME = 'nsynth-test.jsonwav.tar.gz' |
|
|
| _INSTRUMENT_CLASSES = ['bass', 'brass', 'flute', 'guitar', 'keyboard', 'mallet', 'organ', 'reed', 'string', 'synth_lead', 'vocal'] |
|
|
|
|
| class NsynthConfig(datasets.BuilderConfig): |
| """BuilderConfig for Nsynth.""" |
| |
| def __init__(self, features, **kwargs): |
| super(NsynthConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs) |
| self.features = features |
|
|
|
|
| class Nsynth(datasets.GeneratorBasedBuilder): |
|
|
| BUILDER_CONFIGS = [ |
| NsynthConfig( |
| features=datasets.Features( |
| { |
| |
| "audio": datasets.Audio(sampling_rate=SAMPLE_RATE), |
| "instrument": datasets.Value("string"), |
| "label": datasets.ClassLabel(names=_INSTRUMENT_CLASSES), |
| } |
| ), |
| name="instrument", |
| description='', |
| ), |
| NsynthConfig( |
| features=datasets.Features( |
| { |
| |
| "audio": datasets.Audio(sampling_rate=SAMPLE_RATE), |
| "pitch": datasets.Value("string"), |
| "label": datasets.ClassLabel(names=[str(idx) for idx in range(128)]), |
| } |
| ), |
| name="pitch", |
| description='', |
| ), |
| ] |
|
|
| 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): |
| dl_paths = dl_manager.extract({ |
| 'train': _TRAIN_FILENAME, |
| 'validation': _VAL_FILENAME, |
| 'test': _TEST_FILENAME, |
| }) |
| return[ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| gen_kwargs={"filepath": dl_paths['train']} |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.VALIDATION, |
| gen_kwargs={"filepath": dl_paths['validation']} |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.TEST, |
| gen_kwargs={"filepath": dl_paths['test']} |
| ), |
| ] |
|
|
| def _generate_examples(self, filepath): |
| dir_list = os.listdir(filepath) |
| folder_path = os.path.join(filepath, dir_list[0]) |
| examples_path = os.path.join(folder_path, 'examples.json') |
| with open(examples_path) as examples_file: |
| examples = json.load(examples_file) |
| wav_dict = {} |
| audio_path = os.path.join(folder_path, "audio") |
|
|
| for filename in os.listdir(audio_path): |
| audio_filename = os.path.join(audio_path, filename) |
| guid = filename.replace('.wav', '') |
| if self.config.name == 'instrument': |
| yield guid, { |
| "id": str(guid), |
| "audio": audio_filename, |
| "instrument": examples[guid]["instrument_family_str"], |
| "label": examples[guid]["instrument_family_str"], |
| } |
| elif self.config.name == 'pitch': |
| yield guid, { |
| "id": str(guid), |
| "audio": audio_filename, |
| "pitch": examples[guid]["pitch"], |
| "label": examples[guid]["pitch"], |
| } |