forked from librosa/librosa
/
hpss_beats.py
executable file
·86 lines (62 loc) · 2.5 KB
/
hpss_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
#!/usr/bin/env python
'''
CREATED:2013-02-12 16:33:40 by Brian McFee <brm2132@columbia.edu>
Beat tracking with HPSS filtering
Usage: ./hpss_beats.py [-h] input_audio.mp3 output_beats.csv
'''
from __future__ import print_function
import argparse
import numpy as np
import sys
import librosa
# Some magic number defaults, FFT window and hop length
N_FFT = 2048
# We use a hop of 512 here so that the HPSS spectrogram input
# matches the default beat tracker parameters
HOP_LENGTH = 512
def hpss_beats(input_file, output_csv):
'''HPSS beat tracking
:parameters:
- input_file : str
Path to input audio file (wav, mp3, m4a, flac, etc.)
- output_file : str
Path to save beat event timestamps as a CSV file
'''
# Load the file
print('Loading ', input_file)
y, sr = librosa.load(input_file)
# Do HPSS
print('Harmonic-percussive separation ... ')
y = librosa.effects.percussive(y)
# Construct onset envelope from percussive component
print('Tracking beats on percussive component')
onset_env = librosa.onset.onset_strength(y=y,
sr=sr,
hop_length=HOP_LENGTH,
n_fft=N_FFT,
aggregate=np.median)
# Track the beats
tempo, beats = librosa.beat.beat_track(onset_envelope=onset_env,
sr=sr,
hop_length=HOP_LENGTH)
beat_times = librosa.frames_to_time(beats,
sr=sr,
hop_length=HOP_LENGTH)
# Save the output
print('Saving beats to ', output_csv)
librosa.output.times_csv(output_csv, beat_times)
def process_arguments(args):
'''Argparse function to get the program parameters'''
parser = argparse.ArgumentParser(description='HPSS beat-tracking example')
parser.add_argument('input_file',
action='store',
help='path to the input file (wav, mp3, etc)')
parser.add_argument('output_file',
action='store',
help='path to the output file (csv of beat times)')
return vars(parser.parse_args(args))
if __name__ == '__main__':
# Get the parameters
parameters = process_arguments(sys.argv[1:])
# Run the beat tracker
hpss_beats(parameters['input_file'], parameters['output_file'])