-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprep_data.py
executable file
·120 lines (81 loc) · 3.42 KB
/
prep_data.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import json
import os
import config
import librosa
import utils
import numpy as np
import matplotlib.pyplot as plt
import essentia
from essentia.standard import Envelope
import h5py
# def main():
# with open(os.path.join(config.wav_dir,'sounds_temporal_deacrease_duration.json')) as jfile:
# data = json.load(jfile)
# files_to_use = [[x['id'], x['ac_analysis']] for x in data if x['duration']<1 and not x['id'] in config.do_not_use]
# lengths = []
# count = 0
# do_not_use = []
# with h5py.File(config.feats_dir+'feats.hdf5', mode='w') as hdf5_file:
# hdf5_file.create_dataset("waveform", [len(files_to_use), config.fs], np.float32)
# hdf5_file.create_dataset("envelope", [len(files_to_use), config.fs], np.float32)
# hdf5_file.create_dataset("mask", [len(files_to_use), config.fs], np.float32)
# hdf5_file.create_dataset("features", [len(files_to_use),len(config.feats_to_use)], np.float32)
# count = 0
# for lf in files_to_use:
# audio,fs = librosa.load(os.path.join(config.wav_dir+'sounds/',str(lf[0])+'.wav'), sr = config.fs)
# length = len(audio)
# if length<=config.fs:
# env = Envelope(releaseTime=50, attackTime=5)
# envelope = env(essentia.array(audio))
# audio = np.pad(audio, [0, config.fs - length], mode = 'constant')
# envelope = np.pad(envelope, [0, config.fs - length], mode = 'constant')
# mask = np.zeros(config.fs)
# mask[:length] = 1
# features = [lf[1][x] for x in config.feats_to_use]
# with h5py.File(config.feats_dir+'feats.hdf5', mode='a') as hdf5_file:
# hdf5_file["waveform"][count,:] = audio
# hdf5_file["envelope"][count,:] = envelope
# hdf5_file["mask"][count,:] = mask
# hdf5_file["features"][count,:] = features
# count+=1
# utils.progress(count,len(files_to_use))
def main():
files_to_use = [x for x in os.listdir(config.wav_dir) if x.endswith('.wav') and not x.startswith('.')]
lengths = []
count = 0
do_not_use = []
with h5py.File(config.feats_dir+'kick_feats.hdf5', mode='w') as hdf5_file:
hdf5_file.create_dataset("waveform", [len(files_to_use), config.fs], np.float32)
hdf5_file.create_dataset("envelope", [len(files_to_use), config.fs], np.float32)
hdf5_file.create_dataset("mask", [len(files_to_use), config.fs], np.float32)
hdf5_file.create_dataset("features", [len(files_to_use),len(config.feats_to_use)], np.float32)
count = 0
for lf in files_to_use:
try:
audio,fs = librosa.load(os.path.join(config.wav_dir, lf), sr = config.fs)
length = len(audio)
if length<=config.fs:
env = Envelope(releaseTime=50, attackTime=5)
envelope = env(essentia.array(audio))
audio = np.pad(audio, [0, config.fs - length], mode = 'constant')
envelope = np.pad(envelope, [0, config.fs - length], mode = 'constant')
mask = np.zeros(config.fs)
mask[:length] = 1
with open(os.path.join(config.ana_dir, lf[:-4]+'_analysis.json'), 'r') as f:
feat_dict = json.load(f)
features = [feat_dict[x] for x in config.feats_to_use]
if any(elem is None for elem in features):
do_not_use.append(lf)
else:
with h5py.File(config.feats_dir+'kick_feats.hdf5', mode='a') as hdf5_file:
hdf5_file["waveform"][count,:] = audio
hdf5_file["envelope"][count,:] = envelope
hdf5_file["mask"][count,:] = mask
hdf5_file["features"][count,:] = features
except:
do_not_use.append(lf)
count+=1
utils.progress(count,len(files_to_use))
print(do_not_use)
if __name__ == '__main__':
main()