comparison experiment-reverb/code/features_extract.py @ 0:246d5546657c

initial commit, needs cleanup
author Emmanouil Theofanis Chourdakis <e.t.chourdakis@qmul.ac.uk>
date Wed, 14 Dec 2016 13:15:48 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:246d5546657c
1 #!/usr/bin/python2
2 # -*- coding: utf-8 -*-
3 """
4 Created on Fri Apr 17 10:32:20 2015
5
6 @author: Emmanouil Chourdakis
7 """
8
9 # Note, reference everything!
10
11 from sys import argv
12 import sys
13
14
15 if __name__=="__main__":
16 if len(argv) != 2:
17 print("Incorrect number of arguments:")
18 print("Usage: ")
19 print("%s <trackdir>")
20 print("")
21 print("Arguments:")
22 print("<trackdir>\tThe directory containing the tracks, features will be stored in the same directory as .yaml files")
23
24 sys.exit(-1)
25 else:
26
27 print("[II] Loading libraries")
28
29 import essentia
30 from essentia import Pool
31 from essentia.standard import *
32 import csv
33 import yaml
34
35
36
37
38
39 # reqyures matplotlib
40 from pylab import *
41
42 #requires numpy
43 from numpy import *
44
45 #requires scikit-learn
46 from sklearn.metrics import pairwise_distances
47
48 # For searching the directory
49 from glob import glob
50
51 traindir = argv[1]
52
53 songs_in_dir = glob("%s/*.wav" % traindir)
54
55 print "[II] Using files: %s" % songs_in_dir
56
57
58 for f_ in songs_in_dir:
59 d = {}
60 v = {}
61
62 fname = f_
63
64
65 outfname = "%s_features.yaml" % f_.split('.')[0]
66
67 print "[II] Using: %s" % f_
68 print "[II] and output: %s" % outfname
69
70
71
72 if outfname.partition('.')[-1].lower() not in ['json', 'yaml']:
73 print("Please choose a .json or .yaml as an output file.")
74 sys.exit(-1)
75 else:
76 if outfname.partition('.')[-1].lower() == 'json':
77 output = YamlOutput(filename = outfname, format='json')
78 else:
79 output = YamlOutput(filename = outfname, format='yaml')
80
81 print("[II] Feature extraction of `%s\'" % fname)
82
83 # Sampling Rate
84 SR = 16000.0
85
86 # Sampling Frequency
87 T = 1.0/SR
88
89 # FrameSize
90 tframeSize = 23 #ms
91 frameSize = int(ceil(tframeSize*SR/1000)) if mod(ceil(tframeSize*SR/1000),2) == 0 \
92 else int(floor(tframeSize*SR/1000))
93
94 # HopSize
95 hopSize = frameSize/2
96
97 # Load Audio
98 audio = MonoLoader(filename = fname, sampleRate=16000)()
99
100
101 #Window Frames
102 w = Windowing(size = frameSize, type = 'hamming')
103
104 # Spectrum
105 spec = Spectrum(size=1024)
106
107 # Pool to append mean and variance
108 pool = Pool()
109 globalPool = Pool()
110
111 # Below are Features to be used in the feature extraction stage
112 # We use, Spectral Contrast, MFCCs, Zero-Crossing rate, RMS,
113 # Crest Factor, Spectral Centroid, Spectral Occupation, Spectral Flux
114
115 # Spectral Contrast
116 sc = SpectralContrast(frameSize = frameSize, highFrequencyBound = 8000, sampleRate = SR)
117
118 # MFCCs
119 mfccs = MFCC(highFrequencyBound = 8000, sampleRate = SR)
120
121 # Spectral Centroid
122 centroid = Centroid(range = SR/2)
123
124 # Spectral Roll-Off
125 rolloff = RollOff(sampleRate = SR, cutoff = 0.9)
126
127 # Spectral Flux
128 flux = Flux()
129
130 # Zero Crossing Rate
131 zcr = ZeroCrossingRate()
132
133 # RMS
134 rms = RMS()
135
136 # Crest Factor
137 crest = Crest()
138
139
140
141
142 # Segmentation based on Onset detection-based temporal modeling
143 print("[II] Calculating features for %s, please wait..." % fname)
144 # Onset Detection
145
146 print("[II] Splitting to onsets...")
147
148 onsetdetection = OnsetDetectionGlobal(frameSize = frameSize, hopSize = hopSize, sampleRate = SR)(audio)
149 onsets = Onsets()(essentia.array([onsetdetection]), [1])
150
151
152
153 print("[II] done, extracting features...")
154 for o in range(0, len(onsets)-1):
155 IOI = audio[onsets[o]*SR:onsets[o+1]*SR]
156
157
158
159
160 if len(IOI) == 0:
161 break;
162
163
164 for frame in FrameGenerator(IOI, frameSize, hopSize):
165 # Temporal Features
166
167 zerocrossingrate = zcr(frame)
168 rmsvalues = rms(frame)
169
170 # Spectral features
171 framespectrum = spec(w(frame))
172 framecontrast = sc(framespectrum)
173 mfcc_coeffs = mfccs(framespectrum)[1]
174 spectralcentroid = centroid(framespectrum)
175 spectralrolloff = rolloff(framespectrum)
176 spectralflux = rolloff(framespectrum)
177
178
179
180 pool.add('lowlevel.zcr', zerocrossingrate)
181 pool.add('lowlevel.rms', rmsvalues)
182 pool.add('lowlevel.spectrum.centroid', spectralcentroid)
183 pool.add('lowlevel.spectrum.rolloff', spectralrolloff)
184 pool.add('lowlevel.mfcc.coeffs', mfcc_coeffs)
185 pool.add('lowlevel.spectrum.magnitude', framespectrum)
186 pool.add('lowlevel.contrast.contrast', framecontrast[0])
187 pool.add('lowlevel.contrast.valleys', framecontrast[1])
188 pool.add('lowlevel.spectrum.flux', spectralflux)
189
190
191
192 spectrumfull = pool['lowlevel.spectrum.magnitude']
193 spectralcontrast = pool['lowlevel.contrast.contrast']
194 spectralvalleys = pool['lowlevel.contrast.valleys']
195 spectralcentroidfeature = pool['lowlevel.spectrum.centroid']
196 spectralrollofffeature = pool['lowlevel.spectrum.rolloff']
197 spectralfluxfeature = pool['lowlevel.spectrum.flux']
198
199 spectralfeature = concatenate((spectralcontrast,spectralvalleys),1)
200 mfccfeature = pool['lowlevel.mfcc.coeffs']
201 zcrfeature = pool['lowlevel.zcr']
202 rmsfeature = pool['lowlevel.rms']
203 crestfeature = crest(rmsfeature)
204
205
206 meanspectralfeature = mean(spectralfeature, 0)
207 for i in range(0, shape(spectralfeature)[1]):
208 globalPool.add('spectralcontrast_%d' % i , meanspectralfeature[i])
209 globalPool.add('spectralcentroid', mean(spectralcentroidfeature, 0))
210 globalPool.add('spectralrolloff', mean(spectralrollofffeature, 0))
211 globalPool.add('spectralflux', mean(spectralfluxfeature, 0))
212
213 # Expand mfccs
214 meanmfcc = mean(mfccfeature, 0)
215 for i in range(0, shape(mfccfeature)[1]):
216 globalPool.add('mfcc_%d' % i, meanmfcc[i])
217
218
219 globalPool.add('zcr', mean(zcrfeature, 0))
220 globalPool.add('rms', mean(rmsfeature, 0))
221 globalPool.add('crest', crestfeature)
222
223 pool.clear()
224
225 print("[II] done.")
226
227
228 print("[II] Saving data to %s:" % outfname)
229 globalPool.add("metadata.filename", fname)
230 output(globalPool)
231