source: python/demos/demo_bpm_extract.py @ cf5c2ca

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampler
Last change on this file since cf5c2ca was dc654f8, checked in by Paul Brossier <piem@piem.org>, 9 years ago

python/: fix comparisons to None

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