-
Notifications
You must be signed in to change notification settings - Fork 0
/
featuresExtraction.py
415 lines (341 loc) · 16.2 KB
/
featuresExtraction.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import pandas as pd
from scipy.io import wavfile
import scipy
import numpy as np
import os
import glob
from pyAudioAnalysis import audioFeatureExtraction
import sys
import time
from librosa.beat import beat_track
import librosa
try:
import essentia
import essentia.standard as es
except ImportError:
print("Essentia not installed!")
def create_fft(fn):
sample_rate, X = wavfile.read(fn)
fft_features = abs(scipy.fft(X)[:1000])
base_fn, ext = os.path.splitext(fn)
data_fn = base_fn + ".fft"
np.save(data_fn, fft_features)
def dataset_fft():
os.chdir("genres/")
genre_folders = [x for x in os.walk("./")]
genre_folders.pop(0)
for g in genre_folders:
for s in g[2][1:]:
ruta = g[0]+"/"+s
print(ruta)
create_fft(ruta)
def read_fft():
features = []
labels = []
os.chdir("genres/")
genre_folders = [x for x in os.walk("./")]
genre_folders.pop(0)
for g in genre_folders:
for s in g[2][1:]:
if s[-3:]=="npy":
ruta = g[0]+"/"+s
features.append(np.load(ruta))
labels.append(g[0][2:])
return features, labels
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
def pyAudioFeatures(dataset_csv="CSV/beatsdataset.csv", dataset_folder="/Users/Capa/Datasets/beatsdataset/",
mtWin=1, mtStep=1, stWin=0.05, stStep=0.05):
"""
This method extracts the pyAudioAnalysis features from all the dataset given and it saves them in a .csv file using
pandas.
:param str dataset_csv: File to write the dataset extraction of features
:param str dataset_folder: Folder containing the dataset, with one folder for each class
:param float mtWin: Size of the mid term analysis window
:param float mtStep: Size of the step of the analysis mid term window
:param float stWin: Size of the short term analysis window
:param float stStep: Size of the step of the analysis short term window
:return pandas.DataFrame: DataFrame object with features and labels of the dataset
"""
genre_list = [x for x in os.walk(dataset_folder)][0][1] # The class folder names inside the dataset folder
if(not os.path.exists(dataset_folder)): # Error if the folder given does not exist
print("The dataset folder : " + dataset_folder + " does not exist.\n")
return
if(os.path.exists(dataset_csv)): # If the .csv file exist, ask if the user wants to overwrite it
print("The dataset_csv file already exists.\n")
if(not query_yes_no("Do you want to overwrite it?")):
return
dirs = [os.path.join(dataset_folder,f) for f in genre_list] # The class folder full address
[featuresTotal, labelsTotal, _] = audioFeatureExtraction.dirsWavFeatureExtraction(dirs, mtWin, mtStep, stWin, stStep, True) # Using pyAudioAnalysis library
features = [] # Features list
labels = [] # Labels list
for i in range(len(genre_list)): # Filling features and labels with the result of audioFeatureExtraction.dirsWavFeatureExtraction()
for ii in range(len(featuresTotal[i])):
features.append(featuresTotal[i][ii])
labels.append(labelsTotal[i])
df = pd.DataFrame.from_records(features) # Generate DataFrame with the features
features_names = ["ZCR", "Energy", "EnergyEntropy", "SpectralCentroid",
"SpectralSpread", "SpectralEntropy", "SpectralFlux",
"SpectralRolloff", "MFCCs", "ChromaVector", "ChromaDeviation"]
features_names_full = []
if(df.shape[1]==70): # pyAudioAnalysis gets 70 features
features_metrics = ["m", "std"]
else: # if using pyAudioAnalysis modified which gives skews and kurts
features_metrics = ["m", "std", "skew", "kurt"]
for j in range(len(features_metrics)): # Generate the names for the features
offset = j * 34
for i in range(1, 9):
features_names_full.append(str(i + offset) + "-" + features_names[i - 1] + features_metrics[j])
for i in range(9, 22):
features_names_full.append(str(i + offset) + "-" + features_names[8] + str(i - 8) + features_metrics[j])
for i in range(22, 34):
features_names_full.append(str(i + offset) + "-" + features_names[9] + str(i - 21) + features_metrics[j])
features_names_full.append(str(34 + offset) + "-" + features_names[10] + features_metrics[j])
features_names_full.append(str(len(features_metrics)*34+1)+"-BPM")
features_names_full.append(str(len(features_metrics)*34+2)+"-BPMconf")
df.columns = features_names_full # Added the features names to the DataFrame object
df["class"] = labels # Finally added the class column
pd.DataFrame.to_csv(df,dataset_csv) # Export the DataFrame to a .csv file for easy import later
return df # And return the DataFrame
def dirsExtractBPM(dataset_folder):
'''
Folder with sub-folder to extract all the BPMs
:param str dataset_folder: Folder with folder of audio files
:return list: List ob BPMs of all the songs in the sub-folders
'''
BPMs = [] # features
genre_list = [x for x in os.walk(dataset_folder)][0][1]
dirs = [os.path.join(dataset_folder,f) for f in genre_list]
for d in dirs:
BPMs.extend(dirExtractBPM(d))
return BPMs
def dirExtractBPM(folder):
'''
Generate a list of BPM of each song in the given folder
:param str folder: Folder with audio file to extract BPMs
:return list: List of BPMs of every song in the folder
'''
BPMs = []
types = ('*.wav', '*.aif', '*.mp3', '*.au', '*.aiff', '*.flac')
audioFiles = []
for files in types:
audioFiles.extend(glob.glob(os.path.join(folder, files)))
for audioFile in audioFiles:
BPMs.append(fileExtractBPM(audioFile))
return BPMs
def fileExtractBPM(fileRoute):
'''
It gets the BPM from an audio file
:param str fileRoute: Audio file to extract BPM
:return float: BPM of the audio file
'''
x, Fs = librosa.load(fileRoute)
x = librosa.resample(x, Fs, 22050)
x = librosa.to_mono(x)
return extractBPM(x)
def extractBPM(x):
'''
Extract the BPM from the list of samples of an audio file.
It tries to use essentia, if it is not installed then it used librosa.
:param list x: List of samples from an audio file
:return float: BPM of the samples of the audio file
'''
try:
rhythm_extractor = RhythmExtractor()
bpm, _, _, _ = rhythm_extractor(x)
except NameError:
# Essentia is not in the system, using librosa instead
bpm, _ = beat_track(x)
return round(bpm)
def extractFeatures(Fs, x, mtWin, mtStep, stWin, stStep):
'''
Extract 71 feature of a singe audio file where x are the sample and Fs the frequency rate.
:param Fs: Frequency rate of the audio file
:param x: List of samples
:param float mtWin: Mid-Term analysis window
:param float mtStep: Mid-Term step
:param float stWin: Short-Term analysis window
:param float stStep: Short-Term step
:return list: List of 71 features
'''
t1 = time.clock()
[MidTermFeatures, stFeatures, _] = audioFeatureExtraction.mtFeatureExtraction(x, Fs, round(mtWin*Fs), round(mtStep*Fs), round(Fs*stWin), round(Fs * stStep))
[beat, beatConf] = audioFeatureExtraction.beatExtraction(stFeatures, stStep)
MidTermFeatures = np.transpose(MidTermFeatures)
MidTermFeatures = MidTermFeatures.mean(axis=0) # long term averaging of mid-term statistics
MidTermFeatures = np.append(MidTermFeatures, beat)
MidTermFeatures = np.append(MidTermFeatures, beatConf)
#MidTermFeatures = np.append(MidTermFeatures, extractBPM(x))
t2 = time.clock()
print("Processing time : " + str(t2-t1))
return MidTermFeatures
def extractFeaturesFolder(dataset_csv="CSV/beatsdataset.csv", dataset_folder="/Users/Capa/Datasets/beatsdataset/",
mtWin=1, mtStep=1, stWin=0.05, stStep=0.05):
'''
MAIN METHOD.
Extracts 71 audio features from every audio file in the dataset_folder.
Write the result in the dataset_csv file which is a pandas.DataFrame.
The dataset_folder must have audio files classified in sub-folders and
the name of the class is the name of the sub-folder which contains them.
:param str dataset_csv: File to write the pandas.DataFrame with features and labels
:param str dataset_folder: Folder with the audio samples to extract features from them
:param float mtWin: Size of mid term analysis windows
:param float mtStep: Step of mid term analysis windows
:param float stWin: Size of short term analysis windows
:param float stStep: Step of short term analysis windows
:return: DataFrame with the classes and 71 features for each audio file
'''
if(not os.path.exists(dataset_folder)): # Error if the folder given does not exist
print("The dataset folder : " + dataset_folder + " does not exist.\n")
return
if(os.path.exists(dataset_csv)): # If the .csv file exist, ask if the user wants to overwrite it
print("The dataset_csv file already exists.\n")
if(not query_yes_no("Do you want to overwrite it?")):
return
df = pyAudioFeatures("pyaa-"+dataset_csv, dataset_folder, mtWin, mtStep, stWin, stStep)
bpms = dirsExtractBPM(dataset_folder)
df["71-BPM"] = bpms
#swap
columns = list(df.columns)
n = len(columns)-1
aux = columns[n]
columns[n]=columns[n-1]
columns[n-1] = aux
df = df[columns]
pd.DataFrame.to_csv(df,dataset_csv) # Export the DataFrame to a .csv file for easy import later
return df
def file_extract_essentia(audio_file):
rhythm_feats = ["rhythm.bpm",
"rhythm.bpm_histogram_first_peak_bpm",
"rhythm.bpm_histogram_first_peak_weight",
"rhythm.bpm_histogram_second_peak_bpm",
"rhythm.bpm_histogram_second_peak_spread",
"rhythm.bpm_histogram_second_peak_weight",
"rhythm.danceability",
"rhythm.beats_loudness.mean",
"rhythm.beats_loudness.stdev",
"rhythm.onset_rate",
"rhythm.beats_loudness_band_ratio.mean",
"rhythm.beats_loudness_band_ratio.stdev"
]
features_total, features_frames = es.MusicExtractor(endTime=120)(audio_file)
features = []
for i in range(0,len(rhythm_feats)-2):
features.append(features_total[rhythm_feats[i]])
for i in range(len(rhythm_feats)-2,len(rhythm_feats)):
bands = features_total[rhythm_feats[i]]
for j in range(0,len(bands)):
features.append(bands[j])
x, Fs = librosa.load(audio_file)
x = librosa.resample(x, Fs, 22050)
x = librosa.to_mono(x)
max_len = 22050*120
if(len(x)>max_len):
x = x[:max_len]
py_feats = extractFeatures(22050, x, 1, 1, 0.05, 0.05)
py_feats = list(np.append(py_feats, np.array(features)))
return py_feats
def essentia_from_folder(dataset_csv="beatsdataset_essentia.csv", dataset_folder="/Users/Capa/Datasets/beatsdataset/"):
if(not os.path.exists(dataset_folder)): # Error if the folder given does not exist
print("The dataset folder : " + dataset_folder + " does not exist.\n")
return
if(os.path.exists(dataset_csv)): # If the .csv file exist, ask if the user wants to overwrite it
print("The dataset_csv file already exists.\n")
if(not query_yes_no("Do you want to overwrite it?")):
return
features = dirs_extract_essentia(dataset_folder)
df = pd.DataFrame.from_records(features) # Generate DataFrame with the features
features_names_essentia = ["bpm",
"bpm_histogram_first_peak_bpm",
"bpm_histogram_first_peak_weight",
"bpm_histogram_second_peak_bpm",
"bpm_histogram_second_peak_spread",
"bpm_histogram_second_peak_weight",
"danceability",
"beats_loudness.mean",
"beats_loudness.stdev",
"onset_rate",
"beats_loudness_band_ratio.mean1","beats_loudness_band_ratio.mean2",
"beats_loudness_band_ratio.mean3","beats_loudness_band_ratio.mean4",
"beats_loudness_band_ratio.mean5","beats_loudness_band_ratio.mean6",
"beats_loudness_band_ratio.stdev1","beats_loudness_band_ratio.stdev2",
"beats_loudness_band_ratio.stdev3","beats_loudness_band_ratio.stdev4",
"beats_loudness_band_ratio.stdev5","beats_loudness_band_ratio.stdev6",
"class","id"
]
features_names = ["ZCR", "Energy", "EnergyEntropy", "SpectralCentroid",
"SpectralSpread", "SpectralEntropy", "SpectralFlux",
"SpectralRolloff", "MFCCs", "ChromaVector", "ChromaDeviation"]
features_names_full = []
features_metrics = ["m", "std"]
for j in range(len(features_metrics)): # Generate the names for the features
offset = j * 34
for i in range(1, 9):
features_names_full.append(str(i + offset) + "-" + features_names[i - 1] + features_metrics[j])
for i in range(9, 22):
features_names_full.append(str(i + offset) + "-" + features_names[8] + str(i - 8) + features_metrics[j])
for i in range(22, 34):
features_names_full.append(str(i + offset) + "-" + features_names[9] + str(i - 21) + features_metrics[j])
features_names_full.append(str(34 + offset) + "-" + features_names[10] + features_metrics[j])
features_names_full.append(str(len(features_metrics) * 34 + 1) + "-BPM")
features_names_full.append(str(len(features_metrics) * 34 + 2) + "-BPMconf")
features_names_full.extend(features_names_essentia)
df.columns = features_names_full # Added the features names to the DataFrame object
pd.DataFrame.to_csv(df, dataset_csv) # Export the DataFrame to a .csv file for easy import later
return df # And return the DataFrame
def dirs_extract_essentia(dataset_folder):
'''
Folder with sub-folder to extract all the essentia features
:param str dataset_folder: Folder with folder of audio files
:return list: List ob features of all the songs in the sub-folders
'''
features = [] # features
genre_list = [x for x in os.walk(dataset_folder)][0][1]
dirs = [os.path.join(dataset_folder,f) for f in genre_list]
for d in dirs:
features.extend(dir_extract_essentia(d))
return features
def dir_extract_essentia(folder):
'''
Generate a list of essentia features of each song in the given folder
:param str folder: Folder with audio files to extract features
:return list: List of features of every song in the folder
'''
features = []
types = ('*.wav', '*.aif', '*.mp3', '*.au', '*.aiff', '*.flac')
audioFiles = []
genre = os.path.basename(folder)
for files in types:
audioFiles.extend(glob.glob(os.path.join(folder, files)))
for audioFile in audioFiles:
feats = file_extract_essentia(audioFile)
feats.append(genre)
feats.append(os.path.basename(audioFile))
features.append(feats)
return features