-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeat_tool.py
More file actions
324 lines (296 loc) · 9.2 KB
/
beat_tool.py
File metadata and controls
324 lines (296 loc) · 9.2 KB
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
import matplotlib.pyplot as plt
import argparse
import multiprocessing
import numpy
from madmom.io.audio import write_wave_file
from headbang import HeadbangBeatTracker
from headbang.util import load_wav, overlay_clicks
from headbang.params import DEFAULTS
def main():
parser = argparse.ArgumentParser(
description="Accurate percussive beat tracking for metal songs",
)
beat_args = parser.add_argument_group("beat arguments")
beat_args.add_argument(
"--algorithms",
type=str,
default=DEFAULTS["algorithms"],
help="List of beat tracking algorithms to apply (default=%(default)s)",
)
beat_args.add_argument(
"--onset-align-threshold-s",
type=float,
default=DEFAULTS["onset_align_threshold_s"],
help="How close beats should align with onsets (in seconds) (default=%(default)s)",
)
onset_args = parser.add_argument_group("onsets arguments")
onset_args.add_argument(
"--max-no-beats",
type=float,
default=DEFAULTS["max_no_beats"],
help="Segments with missing beats to substitute onsets (default=%(default)s)",
)
onset_args.add_argument(
"--onset-near-threshold-s",
type=float,
default=DEFAULTS["onset_near_threshold_s"],
help="How close onsets should be (in seconds) when supplementing onset information (default=%(default)s)",
)
onset_args.add_argument(
"--onset-silence-threshold",
type=float,
default=DEFAULTS["onset_silence_threshold"],
help="Silence threshold",
)
parser.add_argument(
"--n-pool",
type=int,
default=multiprocessing.cpu_count() - 1,
help="How many threads to use in multiprocessing pool (default=%(default)s)",
)
parser.add_argument(
"--show-plots",
action="store_true",
help="Display plots of intermediate steps describing the algorithm using matplotlib",
)
parser.add_argument(
"--disable-onsets",
action="store_true",
help="disable onset alignment, only output consensus beats",
)
parser.add_argument(
"--disable-transient-shaper",
action="store_true",
help="disable transient shaping, only use percussive separation",
)
parser.add_argument(
"--beats-out",
type=str,
default="",
help="output beats txt file (default=%(default)s)",
)
hpss_args = parser.add_argument_group("hpss arguments")
hpss_args.add_argument(
"--harmonic-margin",
type=float,
default=DEFAULTS["harmonic_margin"],
help="Separation margin for HPSS harmonic iteration (default=%(default)s)",
)
hpss_args.add_argument(
"--harmonic-frame",
type=int,
default=DEFAULTS["harmonic_frame"],
help="T-F/frame size for HPSS harmonic iteration (default=%(default)s)",
)
hpss_args.add_argument(
"--percussive-margin",
type=float,
default=DEFAULTS["percussive_margin"],
help="Separation margin for HPSS percussive iteration (default=%(default)s)",
)
hpss_args.add_argument(
"--percussive-frame",
type=int,
default=DEFAULTS["percussive_frame"],
help="T-F/frame size for HPSS percussive iteration (default=%(default)s)",
)
tshaper_args = parser.add_argument_group("multiband transient shaper arguments")
tshaper_args.add_argument(
"--fast-attack-ms",
type=int,
default=DEFAULTS["fast_attack_ms"],
help="Fast attack (ms) (default=%(default)s)",
)
tshaper_args.add_argument(
"--slow-attack-ms",
type=int,
default=DEFAULTS["slow_attack_ms"],
help="Slow attack (ms) (default=%(default)s)",
)
tshaper_args.add_argument(
"--release-ms",
type=int,
default=DEFAULTS["release_ms"],
help="Release (ms) (default=%(default)s)",
)
tshaper_args.add_argument(
"--power-memory-ms",
type=int,
default=DEFAULTS["power_memory_ms"],
help="Power filter memory (ms) (default=%(default)s)",
)
tshaper_args.add_argument(
"--filter-order",
type=int,
default=DEFAULTS["filter_order"],
help="Bandpass (butter) filter order (default=%(default)s)",
)
parser.add_argument("wav_in", help="input wav file")
parser.add_argument("wav_out", help="output wav file")
args = parser.parse_args()
print("Loading file {0} with 44100 sampling rate".format(args.wav_in))
x = load_wav(args.wav_in)
pool = multiprocessing.Pool(args.n_pool)
hbt = HeadbangBeatTracker(
pool,
# consensus beat tracking params
args.algorithms,
args.onset_align_threshold_s,
# perccussive onset alignment params
args.disable_onsets,
args.max_no_beats,
args.onset_near_threshold_s,
args.onset_silence_threshold,
# hpss params
args.harmonic_margin,
args.harmonic_frame,
args.percussive_margin,
args.percussive_frame,
# transient shaper params
args.fast_attack_ms,
args.slow_attack_ms,
args.release_ms,
args.power_memory_ms,
args.filter_order,
args.disable_transient_shaper,
)
beats = None
print("Applying HeadbangBeatTracker algorithm")
beats = hbt.beats(x)
if args.beats_out:
print("Writing beat locations to file {0}".format(args.beats_out))
with open(args.beats_out, "w") as f:
for b in beats:
f.write(f"{b}\n")
print("Overlaying clicks at beat locations")
x_stereo = load_wav(args.wav_in, stereo=True)
x_with_clicks = overlay_clicks(x_stereo, beats)
print("Writing output with clicks to {0}".format(args.wav_out))
write_wave_file(x_with_clicks, args.wav_out, sample_rate=44100)
if args.show_plots:
print("Displaying plots")
generate_all_plots(
x,
hbt.cbt.beat_results,
hbt.beat_consensus,
hbt.onsets,
hbt.xp,
hbt.xp_hpss,
hbt.aligned,
hbt.to_concat,
)
def generate_all_plots(
x, beat_results, beat_consensus, onsets, xp, xp_hpss, aligned, to_concat
):
timestamps = [i / 44100.0 for i in range(len(x))]
plt.figure(1)
plt.title("Input waveform")
plt.plot(timestamps, x)
plt.xlabel("time (seconds)")
plt.ylabel("amplitude")
plt.show()
plt.figure(1)
plt.title("Input waveform with all beats")
plt.plot(timestamps, x)
plt.xlabel("time (seconds)")
plt.ylabel("amplitude")
for i, beats in enumerate(beat_results):
# offset each different algo
plt.plot(
beats,
numpy.zeros(len(beats), dtype=numpy.float) + i * 0.12,
marker="o",
linestyle="None",
markersize=10,
)
plt.ylim([-1, 1])
plt.show()
plt.figure(1)
plt.title("Input waveform with beat consensus")
plt.plot(timestamps, x)
plt.xlabel("time (seconds)")
plt.ylabel("amplitude")
plt.plot(
beat_consensus,
numpy.zeros(len(beat_consensus)),
marker="o",
linestyle="None",
color="red",
markersize=10,
)
plt.legend(["waveform", "beats"])
plt.show()
plt.figure(1)
plt.title("Percussive-attack-enhanced waveform with onsets")
plt.plot(timestamps, xp)
plt.xlabel("time (seconds)")
plt.ylabel("amplitude")
plt.plot(
onsets,
numpy.zeros(len(onsets)),
marker="o",
linestyle="None",
color="orange",
markersize=10,
)
plt.legend(["waveform", "onsets"])
plt.show()
plt.figure(1)
plt.title("Waveform with onset-aligned beats")
plt.plot(timestamps, x)
plt.xlabel("time (seconds)")
plt.ylabel("amplitude")
plt.plot(
aligned,
numpy.zeros(len(aligned)),
marker="o",
linestyle="None",
color="red",
markersize=5,
)
plt.plot(
onsets,
numpy.zeros(len(onsets)),
marker="x",
linestyle="None",
color="orange",
markersize=10,
)
plt.legend(["waveform", "beats", "onsets"])
plt.show()
if to_concat.size > 0:
plt.figure(1)
plt.title("Waveform with aligned beats and supplemented onsets")
plt.plot(timestamps, x)
plt.xlabel("time (seconds)")
plt.ylabel("amplitude")
plt.plot(
aligned,
numpy.zeros(len(aligned)),
marker="o",
linestyle="None",
color="red",
markersize=10,
)
plt.plot(
to_concat,
numpy.zeros(len(to_concat)),
marker="o",
linestyle="None",
color="orange",
markersize=10,
)
plt.legend(["waveform", "beats", "onsets"])
plt.show()
plt.figure(1)
plt.title("Percussive separation after iterative HPSS")
plt.plot(timestamps, xp_hpss)
plt.xlabel("time (seconds)")
plt.ylabel("amplitude")
plt.show()
plt.figure(1)
plt.title("Percussive separation with enhanced attacks")
plt.plot(timestamps, xp)
plt.xlabel("time (seconds)")
plt.ylabel("amplitude")
plt.show()