-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeature_extraction.py
316 lines (285 loc) · 12.3 KB
/
feature_extraction.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
import sys, os
import essentia.standard
from essentia.streaming import *
import numpy as np
import pandas as pd
import glob
FILE_EXT = (".wav")
#for now we extract all features, we will restrict them later to the relevant ones here
class FeatureExtractor(essentia.streaming.CompositeBase):
def __init__(self, frameSize=2048,
hopSize=1024,
sampleRate = 44100.):
super(FeatureExtractor, self).__init__()
halfSampleRate = sampleRate/2
minFrequency = sampleRate/frameSize
fc = FrameCutter(frameSize=frameSize,
hopSize=hopSize)
# define the features
# zerocrossingrate
zcr = ZeroCrossingRate()
fc.frame >> zcr.signal
# strong decay
#sd = StrongDecay(sampleRate = sampleRate)
#fc.frame >> sd.signal
# windowing:
w = Windowing(type='blackmanharris62')
fc.frame >> w.frame
# spectrum:
spec = Spectrum()
w.frame >> spec.frame
# spectral energy:
energy = Energy()
spec.spectrum >> energy.array
# spectral rms:
rms = RMS()
spec.spectrum >> rms.array
# spectral centroid:
square = UnaryOperator(type='square')
centroid = Centroid(range=halfSampleRate)
spec.spectrum >> square.array >> centroid.array
# spectral central moments:
cm = CentralMoments(range=halfSampleRate)
ds = DistributionShape()
spec.spectrum >> cm.array
cm.centralMoments >> ds.centralMoments
# mfcc:
mfcc = MFCC(numberBands = 40, numberCoefficients = 13, sampleRate = sampleRate)
spec.spectrum >> mfcc.spectrum
mfcc.bands >> None
# lpc:
lpc = LPC(order = 10, sampleRate = sampleRate)
spec.spectrum >> lpc.frame
lpc.reflection >> None
# spectral decrease:
square = UnaryOperator(type='square')
decrease = Decrease(range=halfSampleRate)
spec.spectrum >> square.array >> decrease.array
# spectral energy ratio:
ebr_low = EnergyBand(startCutoffFrequency=20,
stopCutoffFrequency=150,
sampleRate = sampleRate)
ebr_mid_low = EnergyBand(startCutoffFrequency=150,
stopCutoffFrequency=800,
sampleRate = sampleRate)
ebr_mid_hi = EnergyBand(startCutoffFrequency=800,
stopCutoffFrequency=4000,
sampleRate = sampleRate)
ebr_hi = EnergyBand(startCutoffFrequency=4000,
stopCutoffFrequency=20000,
sampleRate = sampleRate)
spec.spectrum >> ebr_low.spectrum
spec.spectrum >> ebr_mid_low.spectrum
spec.spectrum >> ebr_mid_hi.spectrum
spec.spectrum >> ebr_hi.spectrum
# spectral hfc:
hfc = HFC(sampleRate = sampleRate)
spec.spectrum >> hfc.spectrum
# spectral flux:
flux = Flux()
spec.spectrum >> flux.spectrum
# spectral roll off:
ro = RollOff(sampleRate = sampleRate)
spec.spectrum >> ro.spectrum
# spectral strong peak:
sp = StrongPeak()
spec.spectrum >> sp.spectrum
# bark bands:
barkBands = BarkBands(numberBands=27, sampleRate = sampleRate)
spec.spectrum >> barkBands.spectrum
# spectral crest:
crest = Crest()
barkBands.bands >> crest.array
# spectral flatness db:
flatness = FlatnessDB()
barkBands.bands >> flatness.array
# spectral barkbands central moments:
cmbb = CentralMoments(range=26) # BarkBands::numberBands - 1
dsbb = DistributionShape()
barkBands.bands >> cmbb.array
cmbb.centralMoments >> dsbb.centralMoments
# spectral complexity:
scx = SpectralComplexity(magnitudeThreshold=0.005, sampleRate = sampleRate)
spec.spectrum >> scx.spectrum
# pitch detection:
pitch = PitchYinFFT(frameSize=frameSize, sampleRate = sampleRate)
spec.spectrum >> pitch.spectrum
pitch.pitch >> None
# pitch salience:
ps = PitchSalience(sampleRate = sampleRate)
spec.spectrum >> ps.spectrum
# spectral contrast:
sc = SpectralContrast(frameSize=frameSize,
sampleRate=sampleRate,
numberBands=6,
lowFrequencyBound=20,
highFrequencyBound=11000,
neighbourRatio=0.4,
staticDistribution=0.15)
spec.spectrum >> sc.spectrum
# spectral peaks
peaks = SpectralPeaks(orderBy='frequency',
minFrequency=minFrequency,
sampleRate = sampleRate)
spec.spectrum >> peaks.spectrum
# dissonance:
diss = Dissonance()
peaks.frequencies >> diss.frequencies
peaks.magnitudes >> diss.magnitudes
# harmonic peaks:
harmPeaks = HarmonicPeaks()
peaks.frequencies >> harmPeaks.frequencies
peaks.magnitudes >> harmPeaks.magnitudes
pitch.pitch >> harmPeaks.pitch
# tristimulus
tristimulus = Tristimulus()
harmPeaks.harmonicFrequencies >> tristimulus.frequencies
harmPeaks.harmonicMagnitudes >> tristimulus.magnitudes
# odd2even
odd2even = OddToEvenHarmonicEnergyRatio()
harmPeaks.harmonicFrequencies >> odd2even.frequencies
harmPeaks.harmonicMagnitudes >> odd2even.magnitudes
# inharmonicity
inharmonicity = Inharmonicity()
harmPeaks.harmonicFrequencies >> inharmonicity.frequencies
harmPeaks.harmonicMagnitudes >> inharmonicity.magnitudes
# define inputs:
self.inputs['signal'] = fc.signal
# define outputs:
# zerocrossingrate
self.outputs['zcr'] = zcr.zeroCrossingRate
# strong decay
# self.outputs['strong_decay'] = sd.strongDecay
# spectral energy:
self.outputs['spectral_energy'] = energy.energy
# spectral rms:
self.outputs['spectral_rms'] = rms.rms
# MFCC rate:
self.outputs['mfcc'] = mfcc.mfcc
# LPC coef:
self.outputs['lpc'] = lpc.lpc
# spectral centroid
self.outputs['spectral_centroid'] = centroid.centroid
# spectral kurtosis
self.outputs['spectral_kurtosis'] = ds.kurtosis
# spectral spread
self.outputs['spectral_spread'] = ds.spread
# spectral skewness
self.outputs['spectral_skewness'] = ds.skewness
# spectral dissonance
self.outputs['spectral_dissonance'] = diss.dissonance
# spectral contrast
self.outputs['sccoeffs'] = sc.spectralContrast
self.outputs['scvalleys'] = sc.spectralValley
# spectral decrease:
self.outputs['spectral_decrease'] = decrease.decrease
# spectral energy ratio:
self.outputs['spectral_energyband_low'] = ebr_low.energyBand
self.outputs['spectral_energyband_middle_low'] = ebr_mid_low.energyBand
self.outputs['spectral_energyband_middle_high'] = ebr_mid_hi.energyBand
self.outputs['spectral_energyband_high'] = ebr_hi.energyBand
# spectral hfc:
self.outputs['hfc'] = hfc.hfc
# spectral flux:
self.outputs['spectral_flux'] = flux.flux
# spectral roll off:
self.outputs['spectral_rolloff'] = ro.rollOff
# spectral strong peak:
self.outputs['spectral_strongpeak'] = sp.strongPeak
# bark bands:
self.outputs['barkbands'] = barkBands.bands
# spectral crest:
self.outputs['spectral_crest'] = crest.crest
# spectral flatness db:
self.outputs['spectral_flatness_db'] = flatness.flatnessDB
# spectral barkbands central moments:
self.outputs['barkbands_kurtosis'] = dsbb.kurtosis
self.outputs['barkbands_spread'] = dsbb.spread
self.outputs['barkbands_skewness'] = dsbb.skewness
#spectral complexity
self.outputs['spectral_complexity'] = scx.spectralComplexity
# pitch confidence
self.outputs['pitch_instantaneous_confidence'] = pitch.pitchConfidence
# pitch salience
self.outputs['pitch_salience'] = ps.pitchSalience
# inharmonicity
self.outputs['inharmonicity'] = inharmonicity.inharmonicity
# odd2even
self.outputs['oddtoevenharmonicenergyratio'] = odd2even.oddToEvenHarmonicEnergyRatio
# tristimuli
self.outputs['tristimulus'] = tristimulus.tristimulus
def extract_for_one(filename):
loader = essentia.streaming.EqloudLoader(filename=filename)
fEx = FeatureExtractor(frameSize=2048, hopSize=1024, sampleRate=loader.paramValue('sampleRate'))
p = essentia.Pool()
loader.audio >> fEx.signal
for desc, output in fEx.outputs.items():
output >> (p, desc)
essentia.run(loader)
stats = ['mean', 'var', 'dmean', 'dvar']
statsPool = essentia.standard.PoolAggregator(defaultStats=stats)(p)
return statsPool
def files_with_classname(directory, extentions):
for root, dirs, files in os.walk(directory):
for basename in files:
if basename.lower().endswith(extentions):
filename = os.path.join(root, basename)
if len(basename.split('.')) > 2:
class_name = basename.split('.')[0] # IOWA DATASET ONLY!
elif len(basename.split('[')) > 2:
class_name = basename.split('[')[1] # IRMAS DATASET ONLY!
else:
class_name = root.split('/')[-1][:-1]
yield filename, class_name
def convert_pool_to_dataframe(essentia_pool, class_name, filename):
pool_dict = dict()
for desc in essentia_pool.descriptorNames():
if type(essentia_pool[desc]) is float:
pool_dict[desc] = essentia_pool[desc]
elif type(essentia_pool[desc]) is numpy.ndarray:
# we have to treat multivariate descriptors differently
for i, value in enumerate(essentia_pool[desc]):
feature_name = "{desc_name}{desc_number}.{desc_stat}".format(
desc_name=desc.split('.')[0],
desc_number=i,
desc_stat=desc.split('.')[1])
pool_dict[feature_name] = value
pool_dict['class'] = class_name
return pd.DataFrame(pool_dict, index=[os.path.basename(filename)])
if __name__ == '__main__':
feature_set = pd.DataFrame()
features = pd.DataFrame()
# reading all files recursively
files = glob.glob('Datasets\TrainingData\cel\*.wav')
for filename in files:
# Preprocess the music file to get features from it
checkFilename = filename[26:]
if ('[cel]' in checkFilename[0:5] or '[cel]' in checkFilename[5:10]):
instrument_code = 17
elif ('[flu]' in checkFilename[0:5] or '[flu]' in checkFilename[5:10]):
instrument_code = 33
elif ('[gac]' in checkFilename[0:5] or '[gac]' in checkFilename[5:10]):
instrument_code = 11
elif ('[gel]' in checkFilename[0:5] or '[gel]' in checkFilename[5:10]):
instrument_code = 13
elif ('[org]' in checkFilename[0:5] or '[org]' in checkFilename[5:10]):
instrument_code = 6
elif ('[pia]' in checkFilename[0:5] or '[pia]' in checkFilename[5:10]):
instrument_code = 1
elif ('[sax]' in checkFilename[0:5] or '[sax]' in checkFilename[5:10]):
instrument_code = 25
elif ('[tru]' in checkFilename[0:5] or '[tru]' in checkFilename[5:10]):
instrument_code = 21
elif ('[vio]' in checkFilename[0:5] or '[vio]' in checkFilename[5:10]):
instrument_code = 15
elif ('[cla]' in checkFilename[0:5] or '[cla' in checkFilename[5:10]):
instrument_code = 31
elif ('[voi]' in checkFilename[0:5] or '[voi]' in checkFilename[5:10]):
instrument_code = 51
else:
instrument_code = 0
print('Unknown instrument found in the file', checkFilename)
feature = extract_for_one(filename)
features = features.append(convert_pool_to_dataframe(feature, instrument_code, filename))
outfile = 'features_cel.csv'
features.to_csv(outfile, index=False)