yangwang825 commited on
Commit
2763816
·
verified ·
1 Parent(s): 9159456

Create nsynth-script.py

Browse files
Files changed (1) hide show
  1. nsynth-script.py +97 -0
nsynth-script.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ """Nsynth Database."""
4
+
5
+
6
+ import os
7
+ import textwrap
8
+ import datasets
9
+ import itertools
10
+ import typing as tp
11
+ from pathlib import Path
12
+ from sklearn.model_selection import train_test_split
13
+
14
+ SAMPLE_RATE = 16_000
15
+
16
+ _TRAIN_FILENAME = 'nsynth-train.jsonwav.tar.gz'
17
+ _VAL_FILENAME = 'nsynth-valid.jsonwav.tar.gz'
18
+ _TEST_FILENAME = 'nsynth-test.jsonwav.tar.gz'
19
+
20
+ CLASSES = ['bass', 'brass', 'flute', 'guitar', 'keyboard', 'mallet', 'organ', 'reed', 'string', 'synth_lead', 'vocal']
21
+
22
+
23
+ class NsynthConfig(datasets.BuilderConfig):
24
+ """BuilderConfig for Nsynth."""
25
+
26
+ def __init__(self, features, **kwargs):
27
+ super(NsynthConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
28
+ self.features = features
29
+
30
+
31
+ class Nsynth(datasets.GeneratorBasedBuilder):
32
+
33
+ BUILDER_CONFIGS = [
34
+ NsynthConfig(
35
+ features=datasets.Features(
36
+ {
37
+ "file": datasets.Value("string"),
38
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
39
+ "instrument": datasets.Value("string"),
40
+ "label": datasets.ClassLabel(names=CLASSES),
41
+ }
42
+ ),
43
+ name="nsynth",
44
+ description='',
45
+ ),
46
+ ]
47
+
48
+ def _info(self):
49
+ return datasets.DatasetInfo(
50
+ description="",
51
+ features=self.config.features,
52
+ supervised_keys=None,
53
+ homepage="",
54
+ citation="",
55
+ task_templates=None,
56
+ )
57
+
58
+ def _split_generators(self, dl_manager):
59
+ dl_paths = dl_manager.extract({
60
+ 'train': _TRAIN_FILENAME,
61
+ 'validation': _VAL_FILENAME,
62
+ 'test': _TEST_FILENAME,
63
+ })
64
+ return[
65
+ datasets.SplitGenerator(
66
+ name=datasets.Split.TRAIN,
67
+ gen_kwargs={"filepath": dl_paths['train']}
68
+ ),
69
+ datasets.SplitGenerator(
70
+ name=datasets.Split.VALIDATION,
71
+ gen_kwargs={"filepath": dl_paths['validation']}
72
+ ),
73
+ datasets.SplitGenerator(
74
+ name=datasets.Split.TEST,
75
+ gen_kwargs={"filepath": dl_paths['test']}
76
+ ),
77
+ ]
78
+
79
+ def _generate_examples(self, filepath):
80
+ dir_list = os.listdir(filepath)
81
+ folder_path = os.path.join(filepath, dir_list[0])
82
+ examples_path = os.path.join(folder_path, 'examples.json')
83
+ with open(examples_path) as examples_file:
84
+ examples = json.load(examples_file)
85
+ wav_dict = {}
86
+ audio_path = os.path.join(folder_path, "audio")
87
+
88
+ for filename in os.listdir(audio_path):
89
+ audio_filename = os.path.join(audio_path, filename)
90
+ guid = filename.replace('.wav', '')
91
+ yield guid, {
92
+ "id": str(guid),
93
+ # "file": audio_path,
94
+ "audio": audio_filename,
95
+ "instrument": examples[key]["instrument_family_str"],
96
+ "label": examples[key]["instrument_family_str"],
97
+ }