-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ2.py
126 lines (97 loc) · 6.82 KB
/
Q2.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
import librosa
import numpy as np
import utils
import scipy.signal as sps
from os import listdir
Ballroom = ['ChaCha', 'Jive', 'Quickstep', 'Rumba', 'Samba', 'Tango', 'Viennese waltz', 'Waltz']
ISMIR2004 = ['Abba', 'Alan_Parsons_Project', 'Alirio Diaz', 'aphex_twin', 'Asian_Dub_Foundation',
'Autechre', 'Aviador_Dro', 'Bach', 'Bach - Walcha', 'Bebel_Gilberto', 'Bela_Bartok',
'Bernstein_conducts_Stravinsky', 'Billie Holiday CD1', 'Bjork', 'Cabaret_Voltaire',
'Carlinhos_Brown', 'charles_mingus', 'Classic', 'Elton Medeiros, Nelson Sargento & Galo Preto',
'Fado', 'Femi_kuti', 'Genesis', 'greek', 'GUITARE+', 'John Frusciante', 'john_coltrane',
'Jose\' Merce\'', 'Kocani Orkester', 'Manu_Chao', 'more greek', 'Nina Pastori', 'Olivier Chassain',
'Papakonstantinou', 'Paulinho da Viola & Elton Medeiros', 'Santana', 'Songs', 'Teresa Cristina',
'Tomatito', 'Vangelis', 'Xatzidakis']
Tempo = [1, 1/2, 1/3, 2, 3]
for ds_id, dataset in enumerate([Ballroom, ISMIR2004]):
if ds_id == 0:
# continue
print('#### Ballroom Dataset')
else:
print('#### ISMIR2004')
for g_id, genre in enumerate(dataset):
AVG_P_SCORE = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
AVG_ALOTC_SCORE = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
if(ds_id == 0):
folder_path = 'Ballroom/BallroomData/' + genre
else:
folder_path = 'ISMIR2004/' + genre + '/wav'
files = listdir(folder_path)
for f_id, wavfile in enumerate(files):
if(ds_id == 0):
anno_bpm_file = 'Ballroom/BallroomAnnotations/ballroomGroundTruth/' + wavfile[:-3] + 'bpm'
else:
anno_bpm_file = 'ISMIR2004/' + genre + '/annotation/' + wavfile[:-4] + ' beat.bpm'
y, sr = librosa.load('{folder}/{file}'.format(folder = folder_path, file = wavfile))
hop_length = 512
oenv = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop_length)
tempogram_auto = librosa.feature.tempogram(onset_envelope=oenv, sr=sr, hop_length=hop_length, norm=None)
tempogram_fourier = librosa.feature.fourier_tempogram(onset_envelope=oenv, sr=sr, hop_length=hop_length)
# estimate tempo get T1, T2
tempo_vector_auto = np.sum(tempogram_auto, axis=1)
tempo_vector_fourier = np.sum(np.abs(tempogram_fourier), axis=1)
tempo_vector_auto = [x/tempogram_auto.shape[1] for x in tempo_vector_auto]
tempo_vector_fourier = [x/tempogram_fourier.shape[1] for x in tempo_vector_fourier]
peak_id_auto = sps.argrelmax(np.array(tempo_vector_auto))
peak_id_fourier = sps.argrelmax(np.array(tempo_vector_fourier))
auto_frequency = librosa.tempo_frequencies(len(tempo_vector_auto))
fourier_frequency = librosa.fourier_tempo_frequencies(hop_length = hop_length)
temp = []
for id in peak_id_auto[0]:
temp.append([tempo_vector_auto[id], id])
tempo_vector_auto = temp
temp = []
for id in peak_id_fourier[0]:
temp.append([tempo_vector_fourier[id], id])
tempo_vector_fourier = temp
tempo_vector_auto = sorted(tempo_vector_auto, key=lambda x: x[0], reverse=True)
tempo_vector_fourier = sorted(tempo_vector_fourier, key=lambda x:x[0], reverse=True)
reference_bpm = ''
with open(anno_bpm_file, 'r') as anno:
reference_str_bpm = anno.readline()
reference_bpm = utils.str2float(reference_str_bpm)
# tempo_vector = [[max_avg_tempogram1, max_id1], [max_avg_tempogram2, max_id2], ...]
T12_AUTO = [auto_frequency[tempo_vector_auto[0][1]], auto_frequency[tempo_vector_auto[1][1]]]
T12_FOURIER = [fourier_frequency[tempo_vector_fourier[0][1]], fourier_frequency[tempo_vector_fourier[1][1]]]
# p_score
# altoc_score
s_auto = (tempo_vector_auto[0][0])/(tempo_vector_auto[0][0] + tempo_vector_auto[1][0])
s_fourier = (tempo_vector_fourier[0][0])/(tempo_vector_fourier[0][0] + tempo_vector_fourier[1][0])
for i,t in enumerate(Tempo):
AVG_P_SCORE[0][i] += utils.P_SCORE([tempi*t for tempi in T12_AUTO], s_auto, reference_bpm)
AVG_P_SCORE[1][i] += utils.P_SCORE([tempi*t for tempi in T12_FOURIER], s_fourier, reference_bpm)
AVG_ALOTC_SCORE[0][i] += utils.ALOTC_SCORE([tempi*t for tempi in T12_AUTO], reference_bpm)
AVG_ALOTC_SCORE[1][i] += utils.ALOTC_SCORE([tempi*t for tempi in T12_FOURIER], reference_bpm)
# print('=========================================================================================')
#print(wavfile)
# print('--AUTO-----------------------------------------------------------------------------------')
# print('estimate:', T12_AUTO[0], T12_AUTO[1])
# print('P_SCORE:', P_SCORE_AC)
# print('ALOTC_SCORE:', ALOTC_SCORE_AC)
# print('--FOURIER--------------------------------------------------------------------------------')
# print('estimate:', T12_FOURIER[0], T12_FOURIER[1])
# print('P_SCORE:', P_SCORE_FOURIER)
# print('ALOTC_SCORE:', ALOTC_SCORE_FOURIER)
# print('--REFERENCE------------------------------------------------------------------------------')
# print(reference_bpm)
# print('=========================================================================================')
# print()
AVG_P_SCORE = [[score/len(files) for score in AVG_P_SCORE[0]], [score/len(files) for score in AVG_P_SCORE[1]]]
AVG_ALOTC_SCORE = [[score/len(files) for score in AVG_ALOTC_SCORE[0]], [score/len(files) for score in AVG_ALOTC_SCORE[1]]]
print('|{}|AC/Fourier|[T1, T2]|x1/2|x1/3|x2|x3|'.format(genre))
print('|-----|----------|--------|------------|------------|----------|----------|')
print('|{}|{}|{:6f}|{:6f}|{:6f}|{:6f}|{:6f}|'.format("P","AC", AVG_P_SCORE[0][0], AVG_P_SCORE[0][1], AVG_P_SCORE[0][2], AVG_P_SCORE[0][3], AVG_P_SCORE[0][4]))
print('|{}|{}|{:6f}|{:6f}|{:6f}|{:6f}|{:6f}|'.format('SCORE',"FOURIER", AVG_P_SCORE[1][0], AVG_P_SCORE[1][1], AVG_P_SCORE[1][2], AVG_P_SCORE[1][3], AVG_P_SCORE[1][4]))
print('|{}|{}|{:6f}|{:6f}|{:6f}|{:6f}|{:6f}|'.format("ALOTC","AC", AVG_ALOTC_SCORE[0][0], AVG_ALOTC_SCORE[0][1], AVG_ALOTC_SCORE[0][2], AVG_ALOTC_SCORE[0][3], AVG_ALOTC_SCORE[0][4]))
print('|{}|{}|{:6f}|{:6f}|{:6f}|{:6f}|{:6f}|'.format('SCORE',"FOURIER", AVG_ALOTC_SCORE[1][0], AVG_ALOTC_SCORE[1][1], AVG_ALOTC_SCORE[1][2], AVG_ALOTC_SCORE[1][3], AVG_ALOTC_SCORE[1][4]))
print()