source: python/demos/demo_beats_and_tempo.py @ 90a8f2f

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

demos/demo_beats_and_tempo.py: skip plot if not beats

  • Property mode set to 100755
File size: 1013 bytes
RevLine 
[7175ed4]1#! /usr/bin/env python
[3bba0e0]2
3import sys
4from aubio import tempo, source
5
6win_s = 512                 # fft size
7hop_s = win_s / 2           # hop size
8samplerate = 44100
9
10if len(sys.argv) < 2:
11    print "Usage: %s <filename>" % sys.argv[0]
12    sys.exit(1)
13
14filename = sys.argv[1]
15beats = []
16
17s = source(filename, samplerate, hop_s)
18t = tempo("default", win_s, hop_s)
19
20block_read = 0
21while True:
22    samples, read = s()
23    isbeat = t(samples)
24    if isbeat:
25        thisbeat = (block_read * hop_s + isbeat[0]) / samplerate
26        print "%.4f" % thisbeat
27        beats.append (thisbeat)
28    block_read += 1
29    if read < hop_s: break
30
31periods = [60./(b - a) for a,b in zip(beats[:-1],beats[1:])]
32
33from numpy import mean, median
[d5e846c]34if len(periods):
35    print 'mean period:', "%.2f" % mean(periods), 'bpm', 'median', "%.2f" % median(periods), 'bpm'
36    if 0:
37        from pylab import plot, show
38        plot(beats[1:], periods)
39        show()
40else:
41    print 'mean period:', "%.2f" % 0, 'bpm', 'median', "%.2f" % 0, 'bpm'
Note: See TracBrowser for help on using the repository browser.