forked from somtobking/DreamStream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beat_tracker.py
65 lines (44 loc) · 1.72 KB
/
beat_tracker.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
'''
CREATED:2013-02-11 18:37:30 by Brian McFee <[email protected]>
Track beat events in an audio file
Usage: ./beat_tracker.py [-h] input_file.mp3 output_beats.csv
'''
from __future__ import print_function
import argparse
import sys
import librosa
def run_beat_tracker(audio_filename):
# Run the beat tracker
beats = beat_track(audio_filename)
# print(audio_filename)
return beats
def beat_track(input_file):
'''Beat tracking function
: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
'''
print('Loading ', input_file)
y, sr = librosa.load(input_file, sr=22050)
# Use a default hop size of 512 samples @ 22KHz ~= 23ms
hop_length = 512
# This is the window length used by default in stft
print('Tracking beats')
tempo, beats = librosa.beat.beat_track(y=y, sr=sr, hop_length=hop_length)
print('Estimated tempo: {:0.2f} beats per minute'.format(tempo))
# save output
# 'beats' will contain the frame numbers of beat events.
beat_times = librosa.frames_to_time(beats, sr=sr, hop_length=hop_length)
return beat_times
def process_arguments(args):
'''Argparse function to get the program parameters'''
parser = argparse.ArgumentParser(description='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))