source: python/demos/demo_bpm_extract.py @ d1a7a90

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since d1a7a90 was d7019c4, checked in by Paul Brossier <piem@piem.org>, 11 years ago

python/demos/demo_bpm_extract.py: make executable

Signed-off-by: Paul Brossier <piem@piem.org>

  • Property mode set to 100755
File size: 1.4 KB
Line 
1#! /usr/bin/env python
2
3from aubio import source, tempo
4from numpy import median, diff
5
6def get_file_bpm(path, params = {}):
7    """ Calculate the beats per minute (bpm) of a given file.
8        path: path to the file
9        param: dictionary of parameters
10    """
11    try:
12        win_s = params['win_s']
13        samplerate = params['samplerate']
14        hop_s = params['hop_s']
15    except:
16        """
17        # super fast
18        samplerate, win_s, hop_s = 4000, 128, 64
19        # fast
20        samplerate, win_s, hop_s = 8000, 512, 128
21        """
22        # default:
23        samplerate, win_s, hop_s = 44100, 1024, 512
24
25    s = source(path, samplerate, hop_s)
26    samplerate = s.samplerate
27    o = tempo("specdiff", win_s, hop_s, samplerate)
28    # List of beats, in samples
29    beats = []
30    # Total number of frames read
31    total_frames = 0
32
33    while True:
34        samples, read = s()
35        is_beat = o(samples)
36        if is_beat:
37            this_beat = o.get_last_s()
38            beats.append(this_beat)
39            #if o.get_confidence() > .2 and len(beats) > 2.:
40            #    break
41        total_frames += read
42        if read < hop_s:
43            break
44
45    # Convert to periods and to bpm
46    bpms = 60./diff(beats)
47    b = median(bpms)
48    return b
49
50if __name__ == '__main__':
51    import sys
52    for f in sys.argv[1:]:
53        bpm = get_file_bpm(f)
54        print "%6s" % ("%.2f" % bpm), f
Note: See TracBrowser for help on using the repository browser.