-
Notifications
You must be signed in to change notification settings - Fork 0
/
adder.py
346 lines (301 loc) · 12.1 KB
/
adder.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
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 18 17:40:50 2015
@author: xavilizarraga
"""
# Script to compare vynil features with cd features
#Experiment 1
# first, we need to import our essentia module. It is aptly named 'essentia'!
import essentia
from pylab import *
from numpy import *
# as there are 2 operating modes in essentia which have the same algorithms,
# these latter are dispatched into 2 submodules:
import essentia.standard
import essentia.streaming
def play(audiofile):
import os, sys
# NB: this only works with linux!! mplayer crashes!
if sys.platform == 'linux2':
os.system('mplayer %s' % audiofile)
if sys.platform == 'darwin':
import subprocess
subprocess.call(["afplay", audiofile])
else:
print 'Not playing audio...'
filename1 = '../../Courses/UPF/SMC/Term 2nd/Audio and Music Analysis/Labs/Audio quality/dataset/a_d_dataset/Bonnie M/cd/1- boney-m-daddy-cool-150306_0324.wav'
filename2 = '../../Courses/UPF/SMC/Term 2nd/Audio and Music Analysis/Labs/Audio quality/dataset/a_d_dataset/Bonnie M/vinyl/1- Daddy Cool-150306_0324.wav'
"""
print('Playing audio excerpts...')
play(filename1)
play(filename2)
"""
namespace1 = 'lowlevelcd'
namespace2 = 'lowlevelvy'
#plot condition
pcont = 0
# spectral analysis parameters
hopSize = 128
frameSize = 2048
sampleRate = 44100
# So, first let's load audio files
# to make sure it's not a trick, let's show the original "audio" to you:
# start by instantiating the audio loader:
loader1 = essentia.standard.MonoLoader(filename = filename1)
loader2 = essentia.standard.MonoLoader(filename = filename2)
# Perform the loading:
audio1 = loader1()
audio2 = loader2()
# Normalize audio data, because vinyl versions were recorded from a mixer
audio1 = audio1/max(audio1)
audio2 = audio2/max(audio2)
# Import essentia standard algorithms
from essentia.standard import *
# Initialize Essentia Objects
w = Windowing(type = 'hann')
spectrum = Spectrum()
mfcc = MFCC()
pool = essentia.Pool()
logattacktime = LogAttackTime()
centroid = Centroid()
hfc = HFC()
energy = Energy()
lowp = LowPass(cutoffFrequency = 80) # 100 hz
crest = Crest()
envelope = Envelope(attackTime=0.003/5,releaseTime=200./5)
flatnessDB = FlatnessDB()
rolloff = RollOff()
distributionshape = DistributionShape()
zerocrossing = ZeroCrossingRate()
highp = HighPass(cutoffFrequency = 15000) # 100 hz
centralmoments = CentralMoments()
strongdecay = StrongDecay()
flatnesssfx = FlatnessSFX()
derivative = Derivative()
entropy = Entropy()
dcremoval = DCRemoval(cutoffFrequency=20)
energybandratiohf = EnergyBandRatio(sampleRate = 44100, startFrequency= 10000, stopFrequency = 22050)
energybandratiolf = EnergyBandRatio(sampleRate = 44100, startFrequency= 0, stopFrequency = 80)
# Bag Of Features for cd version
for frame in FrameGenerator(audio1, frameSize = 2048, hopSize = 128):
frame = dcremoval(frame)
pool.add('lowlevelcd.logattacktime', logattacktime(frame))
pool.add('lowlevelcd.lfenergy', energy(lowp(w(frame))))
pool.add('lowlevelcd.hfenergy', energy(highp(w(frame))))
pool.add('lowlevelcd.crestfactor',crest(abs(w(frame))))
pool.add('lowlevelcd.rolloff', rolloff((w(frame))))
pool.add('lowlevelcd.derivative', std(abs((derivative(w(frame))))-median(abs(derivative(w(frame))))))
pool.add('lowlevelcd.spec_logattacktime', logattacktime(spectrum(w(frame))))
pool.add('lowlevelcd.centroid', (sampleRate/2.)*(centroid(spectrum(derivative(w(frame))))))
pool.add('lowlevelcd.hfc', hfc(spectrum(w(frame))))
pool.add('lowlevelcd.spectralcrest', crest(spectrum(w(frame))))
pool.add('lowlevelcd.strongdecay', strongdecay(spectrum(w(frame))))
pool.add('lowlevelcd.flatnesssfx', flatnesssfx(spectrum(w(frame))))
pool.add('lowlevelcd.hfbandratio', energybandratiohf(spectrum(w(frame))))
pool.add('lowlevelcd.lfbandratio', energybandratiolf(spectrum(w(frame))))
pool.add('lowlevelcd.entropy', entropy(spectrum(w(frame))))
# Bag Of Features for vinyl version
for frame in FrameGenerator(audio2, frameSize = 2048, hopSize = 128):
frame = dcremoval(frame)
pool.add('lowlevelvy.logattacktime', logattacktime(frame))
pool.add('lowlevelvy.spec_logattacktime', logattacktime(spectrum(w(frame))))
pool.add('lowlevelvy.centroid', (sampleRate/2.)*(centroid(spectrum(derivative(w(frame))))))
pool.add('lowlevelvy.hfc', hfc(spectrum(w(frame))))
pool.add('lowlevelvy.lfenergy', energy(lowp(w(frame))))
pool.add('lowlevelvy.hfenergy', energy(highp(w(frame))))
pool.add('lowlevelvy.crestfactor',crest(abs(w(frame))))
pool.add('lowlevelvy.spectralcrest', crest(spectrum(w(frame))))
pool.add('lowlevelvy.rolloff', rolloff((w(frame))))
pool.add('lowlevelvy.strongdecay', strongdecay(spectrum(w(frame))))
pool.add('lowlevelvy.flatnesssfx', flatnesssfx(spectrum(w(frame))))
pool.add('lowlevelvy.derivative', std(abs((derivative(w(frame))))-median(abs(derivative(w(frame))))))
pool.add('lowlevelvy.hfbandratio', energybandratiohf(spectrum(w(frame))))
pool.add('lowlevelvy.lfbandratio', energybandratiolf(spectrum(w(frame))))
pool.add('lowlevelvy.entropy', entropy(spectrum(w(frame))))
nframes = float(len(audio1))/hopSize
# time-frame mapping vector
t1 = np.arange(0,float(len(audio1))/sampleRate,float(hopSize)/sampleRate)
t2 = np.arange(0,float(len(audio2))/sampleRate,float(hopSize)/sampleRate)
if (pcond != 0):
# Drawing LLD - HFC
plt.figure(1, figsize=(11.5, 9))
plt.subplot(4,1,1)
plt.plot(np.arange(audio1.size)/float(sampleRate), audio1, 'b')
plt.axis([0, audio1.size/float(sampleRate), min(audio1), max(audio1)])
xlabel('Time(s)'), plt.ylabel('amplitude')
plt.title('x (cd version.wav)')
plt.subplot(4,1,2)
plt.plot(np.arange(audio2.size)/float(sampleRate), audio2, 'b')
plt.axis([0, audio2.size/float(sampleRate), min(audio2), max(audio2)])
plt.ylabel('amplitude')
xlabel('Time(s)'),plt.title('x (vinyl version.wav)')
plt.autoscale(tight=True)
plt.subplot(4,1,3)
plot(t1,pool['lowlevelcd.hfc'].T[1:])
axis('tight'), xlabel('Time(s)')
title('High Frequency Content (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,4)
plot(t2,pool['lowlevelvy.hfc'].T[1:])
axis('tight'), xlabel('Time(s)')
title('High Frequency Content (vinyl version)')
plt.autoscale(tight=True)
plt.show()
# LF energy and HFenergy
plt.figure(2, figsize=(11.5, 9))
plt.subplot(4,1,1)
plot(t1,pool['lowlevelcd.lfenergy'].T[1:])
axis('tight'), xlabel('Time(s)')
title('LFEnergy (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,2)
plot(t2,pool['lowlevelvy.lfenergy'].T[1:])
axis('tight'), xlabel('Time(s)')
title('LFEnergy (vinyl version)')
plt.autoscale(tight=True)
plt.subplot(4,1,3)
plot(t1,pool['lowlevelcd.hfenergy'].T[1:])
axis('tight'), xlabel('Time(s)')
title('HFEnergy (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,4)
plot(t2,pool['lowlevelvy.hfenergy'].T[1:])
axis('tight'), xlabel('Time(s)')
title('HFEnergy (vinyl version)')
plt.autoscale(tight=True)
plt.show()
# log attack time - Crest Factor (Time domain)
plt.figure(3, figsize=(11.5, 9))
plt.subplot(4,1,1)
plot(pool['lowlevelcd.logattacktime'].T[1:])
axis('tight'), xlabel('Time(s)')
title('LogattackTime (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,2)
plot(pool['lowlevelvy.logattacktime'].T[1:])
axis('tight'), xlabel('Time(s)')
title('LogAttackTime (vinyl version)')
plt.autoscale(tight=True)
plt.subplot(4,1,3)
plot(t1,pool['lowlevelcd.crestfactor'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Crst Factor (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,4)
plot(t2,pool['lowlevelvy.crestfactor'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Crest Factor (vinyl version)')
plt.autoscale(tight=True)
plt.show()
# Spectral centroid of derivative - Derivative (Time domain)
plt.figure(4, figsize=(11.5, 9))
plt.subplot(4,1,1)
plot(t1,pool['lowlevelcd.centroid'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Spectral dCentroid (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,2)
plot(t2,pool['lowlevelvy.centroid'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Spectral dCentroid (vinyl version)')
plt.autoscale(tight=True)
plt.subplot(4,1,3)
plot(t1,pool['lowlevelcd.derivative'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Derivative (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,4)
plot(t2,pool['lowlevelvy.derivative'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Derivative (vinyl version)')
plt.autoscale(tight=True)
plt.show()
# Low Frequency band ratio - High Frequency band ratio
plt.figure(5, figsize=(11.5, 9))
plt.subplot(4,1,1)
plot(t1,pool['lowlevelcd.lfbandratio'].T[1:])
axis('tight'), xlabel('Time(s)')
title('LF Band Ratio (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,2)
plot(t2,pool['lowlevelvy.lfbandratio'].T[1:])
axis('tight'), xlabel('Time(s)')
title('LF Band Ratio (vinyl version)')
plt.autoscale(tight=True)
plt.subplot(4,1,3)
plot(t1,pool['lowlevelcd.hfbandratio'].T[1:])
axis('tight'), xlabel('Time(s)')
title('HF Band Ratio (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,4)
plot(t2,pool['lowlevelvy.hfbandratio'].T[1:])
axis('tight'), xlabel('Time(s)')
title('HF Band Ratio (vinyl version)')
plt.autoscale(tight=True)
plt.show()
# Rolloff (Time Domain) - Flatness SFX
plt.figure(6, figsize=(11.5, 9))
plt.subplot(4,1,1)
plot(t1,pool['lowlevelcd.rolloff'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Roll Off (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,2)
plot(t2,pool['lowlevelvy.rolloff'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Roll Off (vinyl version)')
plt.autoscale(tight=True)
plt.subplot(4,1,3)
plot(t1,pool['lowlevelcd.flatnesssfx'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Flatness SFX (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,4)
plot(t2,pool['lowlevelvy.flatnesssfx'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Flatness SFX (vinyl version)')
plt.autoscale(tight=True)
plt.show()
# Strong Decay (Frequency Domain) -Entropy
plt.figure(7, figsize=(11.5, 9))
plt.subplot(4,1,1)
plot(t1,pool['lowlevelcd.strongdecay'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Strong Decay (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,2)
plot(t2,pool['lowlevelvy.strongdecay'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Strong Decay (vinyl version)')
plt.autoscale(tight=True)
plt.subplot(4,1,3)
plot(t1,pool['lowlevelcd.entropy'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Entropy (cd version)')
plt.autoscale(tight=True)
plt.subplot(4,1,4)
plot(t2,pool['lowlevelvy.entropy'].T[1:])
axis('tight'), xlabel('Time(s)')
title('Entropy (vinyl version)')
plt.autoscale(tight=True)
plt.show()
# Histogram for cd version and vynil version
figure(8)
plt.subplot(2,1,1)
plt.hist(audio1,50), xlim([-1,1]),title('cd version')
plt.subplot(2,1,2)
plt.hist(audio2,50), xlim([-1,1]),title('vinyl version')
# Computing the kurtosis or skewness. CD version looks a normal
# distribution whereas vinyl version is a skewed normal distribution with a short deviation or IQR.
stats = ['mean','median','var','min','max','dmean','dmean2','dvar','dvar2' ]
megalopool = PoolAggregator(defaultStats=stats)(pool)
# save to output file
YamlOutput(filename='outputFeatures.yaml')(megalopool)
pathGiven = '../../Courses/UPF/SMC/Term 2nd/Audio and Music Analysis/Labs/Audio quality/dataset/a_d_dataset/'
clusters = ['cd','vinyl']
parts = pathGiven.rstrip("/").split("/")
parentDir = "/".join(parts[:-1])
arffFilename = "/".join([parentDir, parts[-1]+".arff"])
wekafile=file(arffFilename, "w+")
relation_name = "quality"
wekafile.write("@RELATION quality\n\n")