forked from MarsCrop/apicultor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_mir_analysis.py
350 lines (308 loc) · 15.9 KB
/
run_mir_analysis.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import sys
import json
import numpy as np
import logging
from .utils.algorithms import *
from soundfile import read
from collections import defaultdict, OrderedDict
from .sonification.Sonification import hfc_onsets
from smst.utils.synth import *
import random
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
ext_filter = ['.mp3','.ogg','.wav','.wma', '.amr'] # check if all extensions are supported by the library
#ext_filter = ['.mp3','.ogg','.undefined','.wav','.wma','.mid', '.amr'] # .mid is not an audio file, why is the reason to have .undefined extension?
# descriptores de interés
descriptors = [
'lowlevel.spectral_centroid',
'lowlevel.spectral_contrast',
'lowlevel.dissonance',
'lowlevel.hfc',
'lowlevel.mfcc',
'loudness.level',
'sfx.logattacktime',
'sfx.inharmonicity',
'rhythm.bpm',
'metadata.duration',
'lowlevel.spectral_flux',
'lowlevel.zero_crossing_rate',
'rhythm.onsets_hfc',
'rhythm.onsets_by_polar_distance',
'highlevel.chords_sequence',
'highlevel.danceability'
]
bands = np.arange(5,5550,367)
def process_file(inputSoundFile, tag_dir, input_filename, genre):
descriptors_dir = (tag_dir+'/'+'descriptores')
if not os.path.exists(descriptors_dir):
os.makedirs(descriptors_dir)
print("Creando directorio para archivos .json")
json_output = descriptors_dir + '/' + input_filename + "_band0.json"
print(json_output)
if os.path.exists(json_output) is True:
raise IOError('File exists')
if os.path.exists(json_output) is False:
pass
try:
pending_descriptions
except Exception as e:
pending_descriptions = []
input_signal, sampleRate = read(inputSoundFile)
input_signal = mono_stereo(input_signal)
retrieve = MIR(input_signal, sampleRate)
retrieve.signal = retrieve.IIR(retrieve.signal, 40, 'highpass')
retrieve.mel_bands_global()
onsets = []
for i in retrieve.FrameGenerator():
retrieve.window()
retrieve.Spectrum()
retrieve.Phase(retrieve.fft(retrieve.frame))
onsets.append(retrieve.detect_by_polar())
#retrieve.onsets_by_polar_distance(onsets)
# compute for all frames in our audio and add it to the pool
pool = defaultdict(list)
#i = 0
#if not sampleRate == 96000:
# retrieve.n_bands = 40
#else:
# retrieve_n_bands = 31 #we can't use 40 bands when fs is vinyl type, 31 is the limit
#genre = sys.argv[2]
pcps = []
first = True
j = 0
retrieve.onsets_by_flux()
if genre == 'reggaeton':
print(np.loadtxt('/home/mc/Descargas/means/reggaeton.txt'))
print(np.loadtxt('/root/Descargas/reggaeton'))
y = np.loadtxt('/home/mc/Descargas/means/reggaeton.txt')
w = np.loadtxt('/root/Descargas/reggaeton')
elif genre == 'pop':
y = np.loadtxt('/home/mc/Descargas/means/pop.txt')
w = np.loadtxt('/root/Descargas/pop')
elif genre == 'romantico':
y = np.loadtxt('/home/mc/Descargas/means/romantico.txt')
w = np.loadtxt('/root/Descargas/romantico')
for share in retrieve.audio_signal_spectrum:
retrieve.magnitude_spectrum = share
retrieve.spectral_peaks()
mags = retrieve.magnitudes
freq = retrieve.frequencies
band_split = int(mags.size/16)
lower = 0
retrieve.frame = retrieve.frames_onset[j]
retrieve.Envelope()
retrieve.AttackTime()
zcr = retrieve.zcr()
eighth_contrast = retrieve.contrast()
retrieve.MFCC_seq()
Danceability = danceability(retrieve.frame, np.array([w]), y, retrieve.fs)
for i in range(16):
try:
retrieve.M = len(mags)
retrieve.magnitudes = mags[lower:lower+band_split]
retrieve.frequencies = freq[lower:lower+band_split]
retrieve.phase = retrieve.phase_signal[i][lower:lower+band_split]
if first is True:
pool['lowlevel.complexity'].append([])
pool['lowlevel.complexity'][i].append(retrieve.magnitudes.size)
moments = central_moments(retrieve)
rolloff = roll_off(retrieve.magnitude_spectrum,retrieve.fs)
if first is True:
pool['lowlevel.rolloff'].append([])
pool['lowlevel.rolloff'][i].append(rolloff)
kurtosis = dist_shape(moments)[2]
if first is True:
pool['lowlevel.kurtosis'].append([])
pool['lowlevel.kurtosis'][i].append(kurtosis)
namespace = 'lowlevel'
desc_name = namespace + '.spectral_centroid'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
c = retrieve.centroid()
if first is True:
pool[desc_name].append([])
pool[desc_name][i].append(c)
desc_name = namespace + '.hfc'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
if first is True:
pool[desc_name].append([])
pool[desc_name][i].append(retrieve.hfc())
retrieve.fundamental_frequency()
retrieve.harmonic_peaks()
if retrieve.harmonic_magnitudes.size is 1:
retrieve.harmonic_magnitudes = np.append(retrieve.harmonic_magnitudes,retrieve.harmonic_magnitudes)
if retrieve.harmonic_frequencies.size is 1:
retrieve.harmonic_frequencies = np.append(retrieve.harmonic_frequencies/2,retrieve.harmonic_frequencies)
desc_name = namespace + '.dissonance'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
if first is True:
pool[desc_name].append([])
pool[desc_name][i].append(retrieve.dissonance())
desc_name = 'sfx.inharmonicity'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
try:
if first is True:
pool[desc_name].append([])
pool[desc_name][i].append(retrieve.inharmonicity())
except Exception as e:
try:
pool[desc_name][i]
except Exception as e:
pass
pool[desc_name][i].append(1)
desc_name = 'sfx.logattacktime'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
if first is True:
pool[desc_name].append([])
pool[desc_name][i].append(retrieve.attack_time)
namespace = 'loudness'
desc_name = namespace + '.level'
retrieve.Loudness()
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
if first is True:
pool[desc_name].append([])
pool[desc_name][i].append(retrieve.loudness)
namespace = 'lowlevel'
desc_name = namespace + '.zero_crossing_rate'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
if first is True:
pool[desc_name].append([])
pool[desc_name][i].append(zcr)
namespace = 'highlevel'
desc_name = namespace + '.danceability'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
if first is True:
pool[desc_name].append([])
pool[desc_name][i].append(Danceability)
namespace='lowlevel'
desc_name = namespace + '.spectral_contrast'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
if first is True:
pool[desc_name].append([])
pool[desc_name][i].append(np.mean(eighth_contrast[0]))
if first is True:
pool[namespace+'.spectral_valleys'].append([])
pool[namespace + '.spectral_valleys'][i].append(np.mean(eighth_contrast[1]))
desc_name = namespace + '.mfcc'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
if first is True:
pool[desc_name].append([])
pool[desc_name][i].append(np.mean(retrieve.mfcc_seq))
if first is True:
pool[desc_name+'_bands'].append([])
pool[desc_name+'_bands'][i].append(np.mean(retrieve.mel_bands))
lower += band_split
print ("Processing Frame " + str(i))
except Exception as e:
print(e,desc_name)
continue
first = False
j += 1
KEYS = []
for band in range(16):
KEYS.append([])
for key in pool.keys():
KEYS[band].append((key,np.mean(pool[key][band])))
odict = OrderedDict(KEYS[band])
with open(descriptors_dir+'/'+input_filename+'_band'+str(band)+'.json','w') as f:
json.dump(odict, f)
if True:
return
#bpm
namespace = 'rhythm'
desc_name = namespace + '.bpm'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
retrieve.onsets_strength()
retrieve.bpm()
pool[desc_name].append(retrieve.tempo)
pool['rhythm.bpm_ticks'].append(retrieve.ticks / retrieve.M)
#spectral flux
namespace = 'lowlevel'
desc_name = namespace + '.spectral_flux'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
try:
retrieve.mel_dbs
pool[desc_name].append(np.mean(retrieve.flux(retrieve.mel_dbs)))
except Exception as e:
retrieve.mel_bands_global()
pool[desc_name].append(np.mean(retrieve.flux(retrieve.mel_dbs)))
#onsets by polar distance
namespace = 'rhythm'
desc_name = namespace + '.onsets_by_polar_distance'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
pool[desc_name].append(onsets_indexes)
#onsets by high frequency content
namespace = 'rhythm'
desc_name = namespace + '.onsets_hfc'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
pool[desc_name].append(np.mean(hfc_onsets(retrieve.signal) / retrieve.fs))
#chords sequence
namespace = 'highlevel'
desc_name = namespace + '.chords_sequence'
retrieve.audio_signal_spectrum = [] #empty the buffers to make chord analysis
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
try:
pool[desc_name].append(chord_sequence(retrieve,pcps))
except Exception as e:
pool[desc_name].append('No chord') #drum loop, melody or tone
#duration
namespace = 'metadata'
desc_name = namespace + '.duration'
if (desc_name in descriptors and os.path.exists(json_output) is False) or (pending_descriptions != [] and desc_name in pending_descriptions):
pool[desc_name].append(retrieve.duration)
print("Obtaining mean values")
sorted_keys = ["lowlevel.dissonance", "lowlevel.mfcc_bands", "sfx.inharmonicity", "rhythm.bpm", "lowlevel.spectral_contrast", "lowlevel.spectral_centroid", "rhythm.bpm_ticks", "lowlevel.mfcc", "loudness.level", "metadata.duration","lowlevel.spectral_valleys", "sfx.logattacktime", "lowlevel.hfc", 'lowlevel.spectral_flux','lowlevel.zero_crossing_rate','rhythm.onsets_hfc','rhythm.onsets_flux','highlevel.danceability','highlevel.chords_sequence'] #sort the keys correctly to prevent bad scaling
keys = []
for k in sorted_keys:
try:
keys.append((k+'.mean', np.mean(pool.get(k)).real))
except Exception as e:
keys.append((k+'.mean', pool.get(k)))
pool_stats = OrderedDict(keys)
print (pool_stats)
if not os.path.exists(json_output):
with open(json_output, 'w') as outfile:
json.dump(pool_stats, outfile) #write to file
else:
with open(json_output) as outfile:
data = json.load(outfile)
data.update(pool_stats)
with open(json_output, 'w') as outfile:
outfile.write(json.dumps(pool_stats, indent = 2))
print(json_output)
#()
Usage = "./run_MIR_analysis.py [FILES_DIR]"
def main():
if len(sys.argv) < 2:
print("\nBad amount of input arguments\n\t", Usage, "\n")
print("Example:\n\t./run_MIR_analysis.py data\n\t./run_MIR_analysis.py samples\n")
sys.exit(1)
try:
files_dir = sys.argv[1]
genre = sys.argv[2]
if not os.path.exists(files_dir):
raise IOError("Must download sounds")
error_count = 0
for subdir, dirs, files in os.walk(files_dir):
for f in files:
if not os.path.splitext(f)[1] in ext_filter:
continue
tag_dir = subdir
input_filename = f
audio_input = subdir+'/'+f
try:
print(( "\n*** Processing %s\n"%audio_input ))
process_file( audio_input, tag_dir, input_filename, genre)
except Exception as e:
print(logger.exception(e))
error_count += 1
continue
print(("Errors: %i"%error_count))
sys.exit( -error_count )
except Exception as e:
logger.exception(e)
exit(1)
if __name__ == '__main__':
main()