-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluate_beats.py
222 lines (188 loc) · 10.3 KB
/
evaluate_beats.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
try:
import madmom
MADMOM = True
except ModuleNotFoundError:
print('madmom not installed. Some options are disabled.')
MADMOM = False
import argparse
import pretty_midi as pm
try:
import sounddevice as sd
SOUND = True
except (OSError, ModuleNotFoundError):
print('Sound not available! All "play" options are disabled')
SOUND = False
import os
import warnings
import numpy as np
import mir_eval
import eval_utils
import beats_utils
import pickle
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('input_folder',type=str)
parser.add_argument('--load',action='store_true',help="use pre-computed values (in the same input folder)")
parser.add_argument('--save',type=str,help="save GT and estimated beats as CSV files in specified folder ('same' saves in input_folder)")
parser.add_argument("--max_len",type=str,help="test on the first max_len seconds of each text file. Anything other than a number will evaluate on whole files. Default is 30s.",
default=30)
parser.add_argument('--midi',action='store_true',help="synthesize MIDI file instead of loading wav files")
parser.add_argument('--subbeats',action='store_true',help='also compute subbeat subdivisions')
parser.add_argument('--file',type=str,help="analyse one specific file only (or any file containing the string)")
parser.add_argument('--play_GT', action='store_true',help="play audio with ground-truth beat and downbeat positions (and subbeats if --subbeat is used)")
parser.add_argument('--play_estim',action='store_true',help="play audio with estimated beat and downbeat positions (and subbeats if --subbeat is used)" )
parser.add_argument('--play_both',action='store_true',help="play audio with both ground-truth and estimated beat and downbeat positions (and subbeats if --subbeat is used)" )
parser.add_argument('--save_res', help="Save the F-measures in the given pickle file.")
parser.add_argument('--longer', action='store_true', help='Evaluate including checking longer beat lengths '
'(every 2 or 3 estimated beat).')
parser.add_argument('--shorter', action='store_true', help='Evaluate including checking shorter beat lengths '
'(every 2 or 3 GT beat).')
args = parser.parse_args()
input_folder = args.input_folder
if not SOUND and (args.play_GT or args.play_estim or args.play_both):
warnings.warn("No sound can be played on this device! Ignoring --play options")
try:
max_len = float(args.max_len)
section = [0,max_len]
print(f"Evaluate on first {args.max_len} seconds")
except:
max_len = None
section=None
print(f"Evaluate on whole files")
all_Fs = []
all_Fs_sub = []
if args.midi:
extension = '.mid'
else:
extension = '.wav'
results = {}
for fn in sorted(os.listdir(input_folder)):
if fn.endswith(extension) and not fn.startswith('.'):
if args.file is None or args.file in fn:
filename_input = os.path.join(input_folder,fn)
print(fn)
if args.midi:
midi_name = filename_input
else:
midi_name = filename_input.replace('.wav','.mid')
# Get ground truth beats
midi_data = pm.PrettyMIDI(midi_name)
beats_GT = midi_data.get_beats()
if max_len is not None:
beats_GT = beats_GT[beats_GT<max_len]
downbeats_GT = midi_data.get_downbeats()
if max_len is not None:
downbeats_GT = downbeats_GT[downbeats_GT<max_len]
if args.subbeats:
subbeats_ticks = np.arange(0,midi_data.time_to_tick(max_len),midi_data.resolution/2)
subbeats_GT = np.array([midi_data.tick_to_time(tick) for tick in subbeats_ticks])
else:
subbeats_GT = None
if args.load:
beats = np.loadtxt(filename_input.replace(extension,'_b_est.csv'))
if max_len is not None:
beats = beats[beats<max_len]
if args.play_estim or args.play_midi or args.play_both:
if args.midi:
sig = midi_data.fluidsynth()
# print(midi_data.instruments)
if max_len is not None:
sig = sig[:int(max_len*44100)]
else:
sig_proc = madmom.audio.signal.SignalProcessor(sample_rate=44100, num_channels=1, start=0, stop=max_len,dtype=np.float32)
sig = sig_proc(filename_input)
else:
# Estimate beat positions
if args.midi:
sig = midi_data.fluidsynth()
# print(midi_data.instruments)
if max_len is not None:
sig = sig[:int(max_len*44100)]
else:
sig_proc = madmom.audio.signal.SignalProcessor(sample_rate=44100, num_channels=1, start=0, stop=max_len,dtype=np.float32)
sig = sig_proc(filename_input)
fs = sig.sample_rate
dur = sig.length
proc_beat = madmom.features.RNNBeatProcessor()
act_beat = proc_beat(sig)
# proc_onsets = madmom.features.SpectralOnsetProcessor(method='superflux')
# act_onsets = proc_onsets(sig)
# confidence1,spec_norm1 = beats_utils.get_confidence_entropy(act_onsets)
# confidence2,spec_norm2 = beats_utils.get_confidence_entropy(act_beat)
# confidence3 = beats_utils.get_confidence_spectral_flatness(act_beat)
# confidence4 = beats_utils.get_confidence_spectral_flatness(act_onsets)
# print(np.mean(confidence1),np.mean(confidence2))
# print(np.mean(confidence3),np.mean(confidence4))
# import matplotlib.pyplot as plt
# plt.subplot(221)
# plt.plot(act_beat)
# plt.subplot(222)
# plt.plot(act_onsets)
# plt.subplot(223)
# plt.imshow(spec_norm1,aspect='auto',origin='lower')
# plt.subplot(224)
# plt.imshow(spec_norm1,aspect='auto',origin='lower')
# plt.show()
proc_beattrack = madmom.features.BeatTrackingProcessor(fps=100)
beats = proc_beattrack(act_beat)
F = mir_eval.beat.f_measure(beats_GT,beats)
if args.longer:
F = max(F, mir_eval.beat.f_measure(beats_GT,beats[::2]))
F = max(F, mir_eval.beat.f_measure(beats_GT,beats[1::2]))
F = max(F, mir_eval.beat.f_measure(beats_GT,beats[::3]))
F = max(F, mir_eval.beat.f_measure(beats_GT,beats[1::3]))
F = max(F, mir_eval.beat.f_measure(beats_GT,beats[2::3]))
if args.shorter:
F = max(F, mir_eval.beat.f_measure(beats_GT[::2],beats))
F = max(F, mir_eval.beat.f_measure(beats_GT[1::2],beats))
F = max(F, mir_eval.beat.f_measure(beats_GT[::3],beats))
F = max(F, mir_eval.beat.f_measure(beats_GT[1::3],beats))
F = max(F, mir_eval.beat.f_measure(beats_GT[2::3],beats))
all_Fs += [F]
results[fn] = F
print(f"Beat F-measure: {F}")
# print(f"GT: {beats_GT}")
# print(f"Est: {beats}")
if args.subbeats:
if args.load:
subbeats = np.loadtxt(filename_input.replace(extension,'_sb_est.csv'))
else:
n_subdivisions, subbeats = beats_utils.get_subbeat_divisions(beats,act_beat)
sub_F = mir_eval.beat.f_measure(subbeats_GT,subbeats)
all_Fs_sub += [sub_F]
print(f"Sub-beat F-measure: {sub_F}")
print(f"Estimated subdivisions: {n_subdivisions}")
print(f"Time Signatures: {midi_data.time_signature_changes}")
else:
subbeats = None
if args.save is not None:
if args.save == 'same':
save_path = input_folder
else:
save_path = args.save
np.savetxt(os.path.join(save_path,fn.replace(extension,'_b_act.csv')),act_beat)
np.savetxt(os.path.join(save_path,fn.replace(extension,'_b_gt.csv')),beats_GT)
np.savetxt(os.path.join(save_path,fn.replace(extension,'_b_est.csv')),beats)
if args.subbeats:
np.savetxt(os.path.join(save_path,fn.replace(extension,'_sb_gt.csv')),subbeats_GT)
np.savetxt(os.path.join(save_path,fn.replace(extension,'_sb_est.csv')),subbeats)
if (args.play_GT or args.play_both) and SOUND:
sig_mix_ratio = 0.7
sig_beats = beats_utils.sonify_beats(beats_GT,None,subbeats_GT)
if max_len is not None:
sig_beats = sig_beats[:int(max_len*44100)]
audio = eval_utils.mix_sounds(sig_beats,sig,sig_mix_ratio)
eval_utils.play_audio(audio)
if (args.play_estim or args.play_both) and SOUND:
sig_mix_ratio = 0.7
sig_beats = beats_utils.sonify_beats(beats,None,subbeats)
if max_len is not None:
sig_beats = sig_beats[:int(max_len*44100)]
audio = eval_utils.mix_sounds(sig_beats,sig,sig_mix_ratio)
eval_utils.play_audio(audio)
print(f"Average beat F-measure: {sum(all_Fs)/len(all_Fs)}")
if args.subbeats:
print(f"Average sub_beat F-measure: {sum(all_Fs_sub)/len(all_Fs_sub)}")
if args.save_res is not None:
with open(args.save_res, 'wb') as file:
pickle.dump(results, file)