-
Notifications
You must be signed in to change notification settings - Fork 1
/
feature_extract.py
177 lines (127 loc) · 5.61 KB
/
feature_extract.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import sys
import librosa
import numpy as np
from multiprocessing import Pool
import argparse
import damp_config as config
N_WORKERS = 5
def parallel_mel(track, audio_dir, save_dir, ext):
audiofile = os.path.join(audio_dir, track)
savefile = os.path.join(save_dir, track.replace(ext, '.npy'))
if not os.path.exists(os.path.dirname(savefile)):
os.makedirs(os.path.dirname(savefile), exist_ok=True)
if os.path.exists(savefile):
print (savefile, ":already exists")
return
try :
y, _ = librosa.load(audiofile, sr=config.sr)
except :
print (savefile, ":unable to load")
return
S = librosa.core.stft(y, n_fft=config.n_fft, hop_length=config.hop_length)
X = np.abs(S)
mel_basis = librosa.filters.mel(sr=config.sr, n_fft=config.n_fft, n_mels=config.n_mels)
mel_S = np.dot(mel_basis, X)
mel_S = np.log10(1+10*mel_S)
mel_S = mel_S.astype(np.float32)
print (mel_S.shape, savefile)
np.save(savefile, mel_S)
def process_msd_singer():
import msd_config
global N_WORKERS
data_dir = msd_config.data_dir
save_dir = msd_config.mel_dir
audio_dir = msd_config.audio_dir
ext = '.mp3' # .wav for ss .mp3 for mix
#### training data
x_train, x_valid, x_test = np.load(os.path.join(config.data_dir, 'generator_dcnn_train_data_1000_d.npy'))
all_tracks = []
for track in x_train:
all_tracks.append((track[1].replace('.npy', ext), audio_dir, save_dir, ext))
for track in x_valid:
all_tracks.append((track[1].replace('.npy', ext), audio_dir, save_dir, ext))
for track in x_test:
all_tracks.append((track[1].replace('.npy', ext), audio_dir, save_dir, ext))
print (len(all_tracks))
all_tracks = [(all_tracks[i]) for i in range(len(all_tracks))]
with Pool(N_WORKERS) as p:
p.starmap(parallel_mel, all_tracks)
print ("training data done")
del all_tracks
#### testing data
x_unseen_train, x_unseen_test = np.load(os.path.join(config.data_dir, 'gen_dcnn_unseen_data_500_d.npy'))
all_tracks = []
for track in x_train:
all_tracks.append(track[1].replace('.npy', ext), audio_dir, save_dir, ext)
for track in x_test:
all_tracks.append(track[1].replace('.npy', ext), audio_dir, save_dir, ext)
all_tracks = [(all_tracks[i]) for i in range(len(all_tracks))]
with Pool(N_WORKERS) as p:
p.starmap(parallel_mel, all_tracks)
print ("testing data done")
del x_unseen_train, x_unseen_test, all_tracks
print ("computing mean, std...")
all_mels = []
for track in x_train :
artist_id, feat_path, start_frame = track
feat = np.load(config.mel_path + feat_path.replace(ext, '.npy'))[:, start_frame : start_frame + config.input_frame_len]
all_mels.append(feat)
print ("mean:",np.mean(all_mels), "std:", np.std(all_mels))
def process_damp(audio_dir, mel_dir, ext):
import damp_config
from utils import load_data_segment
global N_WORKERS
train_artists = np.load(os.path.join(damp_config.data_dir, 'artist_1000.npy'))
train_list, _ = load_data_segment(os.path.join(damp_config.data_dir, 'train_artist_track_1000.pkl'), train_artists)
valid_list, _ = load_data_segment(os.path.join(damp_config.data_dir, 'valid_artist_track_1000.pkl'), train_artists)
unseen_train_artists = np.load(os.path.join(damp_config.data_dir, 'unseen_artist_300_2.npy'))
unseen_train_list, _ = load_data_segment(os.path.join(damp_config.data_dir, 'unseen_model_artist_track_300_2.pkl'), unseen_train_artists)
unseen_valid_list, _ = load_data_segment(os.path.join(damp_config.data_dir, 'unseen_eval_artist_track_300_2.pkl'), unseen_train_artists)
all_tracks = set()
for i in range(len(train_list)):
_, feat_path, _ = train_list[i]
feat_path = feat_path.replace('.npy', ext)
all_tracks.add((feat_path, audio_dir, mel_dir, ext))
for i in range(len(valid_list)):
_, feat_path, _ = valid_list[i]
feat_path = feat_path.replace('.npy',ext)
all_tracks.add((feat_path, audio_dir, mel_dir, ext))
all_tracks = list(all_tracks)
print (len(all_tracks))
if not os.path.exists(mel_dir):
os.makedirs(mel_dir)
with Pool(N_WORKERS) as p:
p.starmap(parallel_mel, all_tracks)
all_tracks = set()
for i in range(len(unseen_train_list)):
_, feat_path, _ = unseen_train_list[i]
feat_path =feat_path.replace('.npy', ext)
all_tracks.add((feat_path, audio_dir, mel_dir, ext))
for i in range(len(unseen_valid_list)):
_, feat_path, _ = unseen_valid_list[i]
feat_path = feat_path.replace('.npy', ext)
all_tracks.add((feat_path, audio_dir, mel_dir, ext))
all_tracks = list(all_tracks)
print (len(all_tracks))
with Pool(N_WORKERS) as p:
p.starmap(parallel_mel, all_tracks)
def process_damp_mix():
import damp_config
process_damp(damp_config.mix_audio_dir, damp_config.mix_mel_dir, '.wav')
def process_damp_vocal():
import damp_config
process_damp(damp_config.vocal_audio_dir, damp_config.vocal_mel_dir, '.m4a')
if __name__ == '__main__' :
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, required=True, choices=['msd', 'damp_mix', 'damp_vocal'])
args = parser.parse_args()
print(args)
if args.dataset == 'msd' :
process_msd_singer()
elif args.dataset == 'damp_mix':
process_damp_mix()
elif args.dataset == 'damp_vocal' :
process_damp_vocal()
else :
print("Error: Wrong dataset name. Check argument")