jg583 commited on
Commit
859cc41
1 Parent(s): e4197d8
Files changed (1) hide show
  1. NSynth.py +54 -62
NSynth.py CHANGED
@@ -1,7 +1,7 @@
1
  import json
2
  import datasets
3
  import os
4
- import wave
5
 
6
  _CITATION = """\
7
  @misc{nsynth2017,
@@ -16,18 +16,18 @@ _CITATION = """\
16
 
17
  _DESCRIPTION = """\
18
  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.
19
-
20
  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.
21
-
22
  """
23
 
24
  _HOMEPAGE = "https://magenta.tensorflow.org/datasets/nsynth#note-qualities"
25
 
26
  _LICENSE = "Creative Commons Attribution 4.0 International (CC BY 4.0)"
27
 
 
28
  class NSynth(datasets.GeneratorBasedBuilder):
29
  def _info(self):
30
  features = datasets.Features({
 
31
  "note": datasets.Value("int64"),
32
  "note_str": datasets.Value("string"),
33
  "instrument": datasets.Value("int64"),
@@ -41,68 +41,60 @@ class NSynth(datasets.GeneratorBasedBuilder):
41
  "instrument_family_str": datasets.Value("string"),
42
  "instrument_source": datasets.Value("int64"),
43
  "instrument_source_str": datasets.Value("string"),
44
- "audio": datasets.features.Audio(mono = False)
45
- })
46
  return datasets.DatasetInfo(
47
  description=_DESCRIPTION,
48
  features=features,
49
  homepage=_HOMEPAGE,
50
  license=_LICENSE,
51
  citation=_CITATION,
52
- )
53
- def _split_generators(self, dl_manager):
54
- dl_paths = dl_manager.download_and_extract({
55
- 'train': 'http://download.magenta.tensorflow.org/datasets/nsynth/nsynth-train.jsonwav.tar.gz',
56
- 'valid': 'http://download.magenta.tensorflow.org/datasets/nsynth/nsynth-valid.jsonwav.tar.gz',
57
- 'test': 'http://download.magenta.tensorflow.org/datasets/nsynth/nsynth-test.jsonwav.tar.gz',
58
- })
59
- return[
60
- datasets.SplitGenerator(
61
- name=datasets.Split.TRAIN,
62
- gen_kwargs={
63
- "filepath": dl_paths['train']
64
- },
65
- ),
66
- datasets.SplitGenerator(
67
- name=datasets.Split.VALIDATION,
68
- gen_kwargs={
69
- "filepath": dl_paths['valid']
70
- },
71
- ),
72
- datasets.SplitGenerator(
73
- name=datasets.Split.TEST,
74
- gen_kwargs={
75
- "filepath": dl_paths['test']
76
- },
77
- ),
78
- ]
79
- def _generate_examples(self, filepath):
80
- with open(filepath) as f:
81
- dir_list = os.listdir(filepath)
82
- if "examples.json" in dir_list:
83
- examples = json.load(filepath.join("/examples.json"))
84
- if "audio" in dir_list:
85
- wav_dict = {}
86
- audio_path = filepath.join("/audio")
87
- for filename in audio_path:
88
- with wave.open(filename, 'rb') as wav_file:
89
- key = wav_file.replace('.wav', '')
90
- wav_dict[key] = wav_file.readframes(wav_file.getnframes())
91
- with open(json_data) as examples:
92
- for key in examples:
93
- yield key, {
94
- "note": key["note"],
95
- "note_str": key["note_str"],
96
- "instrument": key["instrument"],
97
- "instrument_str": key["instrument_str"],
98
- "pitch": key["pitch"],
99
- "velocity": key["velocity"],
100
- "sample_rate": key["sample_rate"],
101
- "qualities": key["qualities"],
102
- "qualities_str": key["qualities_str"],
103
- "instrument_family": key["instrument_family"],
104
- "instrument_family_str": key["instrument_family_str"],
105
- "instrument_source": key["instrument_source"],
106
- "instrument_source_str": key["instrument_source_str"],
107
- "audio": wav_dict[key]
108
- }
 
1
  import json
2
  import datasets
3
  import os
4
+ from scipy.io import wavfile
5
 
6
  _CITATION = """\
7
  @misc{nsynth2017,
 
16
 
17
  _DESCRIPTION = """\
18
  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.
 
19
  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.
 
20
  """
21
 
22
  _HOMEPAGE = "https://magenta.tensorflow.org/datasets/nsynth#note-qualities"
23
 
24
  _LICENSE = "Creative Commons Attribution 4.0 International (CC BY 4.0)"
25
 
26
+
27
  class NSynth(datasets.GeneratorBasedBuilder):
28
  def _info(self):
29
  features = datasets.Features({
30
+ "id": datasets.Value("string"),
31
  "note": datasets.Value("int64"),
32
  "note_str": datasets.Value("string"),
33
  "instrument": datasets.Value("int64"),
 
41
  "instrument_family_str": datasets.Value("string"),
42
  "instrument_source": datasets.Value("int64"),
43
  "instrument_source_str": datasets.Value("string"),
44
+ "audio": datasets.features.Audio()
45
+ })
46
  return datasets.DatasetInfo(
47
  description=_DESCRIPTION,
48
  features=features,
49
  homepage=_HOMEPAGE,
50
  license=_LICENSE,
51
  citation=_CITATION,
52
+ )
53
+ def _split_generators(self, dl_manager):
54
+ dl_paths = dl_manager.download_and_extract({
55
+ 'train': 'http://download.magenta.tensorflow.org/datasets/nsynth/nsynth-train.jsonwav.tar.gz',
56
+ 'validation': 'http://download.magenta.tensorflow.org/datasets/nsynth/nsynth-valid.jsonwav.tar.gz',
57
+ 'test': 'http://download.magenta.tensorflow.org/datasets/nsynth/nsynth-test.jsonwav.tar.gz',
58
+ })
59
+ return[
60
+ datasets.SplitGenerator(
61
+ name=datasets.Split.TRAIN,
62
+ gen_kwargs={"filepath": dl_paths['train']}
63
+ ),
64
+ datasets.SplitGenerator(
65
+ name=datasets.Split.VALIDATION,
66
+ gen_kwargs={"filepath": dl_paths['validation']}
67
+ ),
68
+ datasets.SplitGenerator(
69
+ name=datasets.Split.TEST,
70
+ gen_kwargs={"filepath": dl_paths['test']}
71
+ ),
72
+ ]
73
+ def _generate_examples(self, filepath):
74
+ dir_list = os.listdir(filepath)
75
+ folder_path = os.path.join(filepath, dir_list[0])
76
+ examples_path = os.path.join(folder_path, 'examples.json')
77
+ with open(examples_path) as examples_file:
78
+ examples = json.load(examples_file)
79
+ wav_dict = {}
80
+ audio_path = os.path.join(folder_path, "audio")
81
+ for filename in os.listdir(audio_path):
82
+ audio_filename = os.path.join(audio_path, filename)
83
+ key = filename.replace('.wav', '')
84
+ yield key, {
85
+ "id": key,
86
+ "note": examples[key]["note"],
87
+ "note_str": examples[key]["note_str"],
88
+ "instrument": examples[key]["instrument"],
89
+ "instrument_str": examples[key]["instrument_str"],
90
+ "pitch": examples[key]["pitch"],
91
+ "velocity": examples[key]["velocity"],
92
+ "sample_rate": examples[key]["sample_rate"],
93
+ "qualities": examples[key]["qualities"],
94
+ "qualities_str": examples[key]["qualities_str"],
95
+ "instrument_family": examples[key]["instrument_family"],
96
+ "instrument_family_str": examples[key]["instrument_family_str"],
97
+ "instrument_source": examples[key]["instrument_source"],
98
+ "instrument_source_str": examples[key]["instrument_source_str"],
99
+ "audio": {'path': audio_filename, 'bytes': wavfile.read(audio_filename)[1]}
100
+ }