File size: 4,738 Bytes
2bdaa97 f174fb1 2bdaa97 |
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 |
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({
"id": datasets.Value("string"),
"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": key,
"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]},
} |