import json import datasets import os from scipy.io import wavfile _CITATION = """\ @misc{nsynth2017, Author = {Jesse Engel and Cinjon Resnick and Adam Roberts and Sander Dieleman and Douglas Eck and Karen Simonyan and Mohammad Norouzi}, Title = {Neural Audio Synthesis of Musical Notes with WaveNet Autoencoders}, Year = {2017}, Eprint = {arXiv:1704.01279}, } """ _DESCRIPTION = """\ The NSynth dataset is an audio dataset containing over 300,000 musical notes across over 1000 commercially-sampled instruments, distinguished by pitch, timbre, and envelope. Each recording was made by playing and holding a musical note for three seconds and letting it decay for one second. The collection of four-second recordings ranges over every pitch on a standard MIDI piano (or as many as possible for the given instrument), played at five different velocities. This dataset was created as an attempt to establish a high-quality entry point into audio machine learning, in response to the surge of breakthroughs in generative modeling of images due to the abundance of approachable image datasets (MNIST, CIFAR, ImageNet). NSynth is meant to be both a benchmark for audio ML and a foundation to be expanded on with future datasets. """ _HOMEPAGE = "https://magenta.tensorflow.org/datasets/nsynth" _LICENSE = "Creative Commons Attribution 4.0 International (CC BY 4.0)" class NSynth(datasets.GeneratorBasedBuilder): def _info(self): features = datasets.Features({ "note": datasets.Value("int64"), "note_str": datasets.Value("string"), "instrument": datasets.Value("int64"), "instrument_str": datasets.Value("string"), "pitch": datasets.Value("int64"), "velocity": datasets.Value("int64"), "sample_rate": datasets.Value("int64"), "qualities": datasets.features.Sequence(datasets.Value("int64")), "qualities_str": datasets.features.Sequence(datasets.Value("string")), "instrument_family": datasets.Value("int64"), "instrument_family_str": datasets.Value("string"), "instrument_source": datasets.Value("int64"), "instrument_source_str": datasets.Value("string"), "audio": datasets.features.Audio(sampling_rate=16000), }) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): dl_paths = dl_manager.download_and_extract({ 'train': 'http://download.magenta.tensorflow.org/datasets/nsynth/nsynth-train.jsonwav.tar.gz', 'validation': 'http://download.magenta.tensorflow.org/datasets/nsynth/nsynth-valid.jsonwav.tar.gz', 'test': 'http://download.magenta.tensorflow.org/datasets/nsynth/nsynth-test.jsonwav.tar.gz', }) 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) key = filename.replace('.wav', '') yield key, { "note": examples[key]["note"], "note_str": examples[key]["note_str"], "instrument": examples[key]["instrument"], "instrument_str": examples[key]["instrument_str"], "pitch": examples[key]["pitch"], "velocity": examples[key]["velocity"], "sample_rate": examples[key]["sample_rate"], "qualities": examples[key]["qualities"], "qualities_str": examples[key]["qualities_str"], "instrument_family": examples[key]["instrument_family"], "instrument_family_str": examples[key]["instrument_family_str"], "instrument_source": examples[key]["instrument_source"], "instrument_source_str": examples[key]["instrument_source_str"], "audio": {'path': audio_filename, 'bytes': wavfile.read(audio_filename)[1]}, }