source: python/demos/demo_beats_and_tempo.py @ 9a226ef

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

moved tests to subdirectory

  • Property mode set to 100755
File size: 875 bytes
Line 
1#! /usr/bin/env python
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
34print 'mean period:', mean(periods), 'bpm'
35print 'median period:', median(periods), 'bpm'
36
37from pylab import plot, show
38plot(beats[1:], periods)
39show()
Note: See TracBrowser for help on using the repository browser.