[459e46f] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | from aubio import tempo, source |
---|
| 5 | |
---|
| 6 | win_s = 512 # fft size |
---|
| 7 | hop_s = win_s / 2 # hop size |
---|
| 8 | |
---|
| 9 | if len(sys.argv) < 2: |
---|
| 10 | print "Usage: %s <filename> [samplerate]" % sys.argv[0] |
---|
| 11 | sys.exit(1) |
---|
| 12 | |
---|
| 13 | filename = sys.argv[1] |
---|
| 14 | |
---|
| 15 | samplerate = 0 |
---|
| 16 | if len( sys.argv ) > 2: samplerate = int(sys.argv[2]) |
---|
| 17 | |
---|
| 18 | s = source(filename, samplerate, hop_s) |
---|
| 19 | samplerate = s.samplerate |
---|
| 20 | o = tempo("default", win_s, hop_s, samplerate) |
---|
| 21 | |
---|
| 22 | # tempo detection delay, in samples |
---|
| 23 | # default to 4 blocks delay to catch up with |
---|
| 24 | delay = 4. * hop_s |
---|
| 25 | |
---|
| 26 | # list of beats, in samples |
---|
| 27 | beats = [] |
---|
| 28 | |
---|
| 29 | # total number of frames read |
---|
| 30 | total_frames = 0 |
---|
| 31 | while True: |
---|
| 32 | samples, read = s() |
---|
| 33 | is_beat = o(samples) |
---|
| 34 | if is_beat: |
---|
| 35 | this_beat = int(total_frames - delay + is_beat[0] * hop_s) |
---|
| 36 | #print "%f" % (this_beat / float(samplerate)) |
---|
| 37 | beats.append(this_beat) |
---|
| 38 | total_frames += read |
---|
| 39 | if read < hop_s: break |
---|
| 40 | |
---|
| 41 | #convert samples to seconds |
---|
| 42 | beats = map( lambda x: x / float(samplerate), beats) |
---|
| 43 | |
---|
| 44 | bpms = [60./(b - a) for a,b in zip(beats[:-1],beats[1:])] |
---|
| 45 | |
---|
| 46 | if len(bpms): |
---|
| 47 | # do plotting |
---|
| 48 | from numpy import array, arange, mean, median |
---|
| 49 | import matplotlib.pyplot as plt |
---|
[3cde629] | 50 | print 'mean period:', "%.2f" % mean(bpms), 'bpm', 'median', "%.2f" % median(bpms), 'bpm' |
---|
| 51 | print 'plotting', filename |
---|
[459e46f] | 52 | plt1 = plt.axes([0.1, 0.75, 0.8, 0.19]) |
---|
| 53 | plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1) |
---|
| 54 | plt.rc('lines',linewidth='.8') |
---|
| 55 | for stamp in beats: plt1.plot([stamp, stamp], [-1., 1.], '-r') |
---|
| 56 | plt1.axis(xmin = 0., xmax = total_frames / float(samplerate) ) |
---|
| 57 | plt1.xaxis.set_visible(False) |
---|
| 58 | plt1.yaxis.set_visible(False) |
---|
| 59 | |
---|
| 60 | # plot actual periods |
---|
| 61 | plt2.plot(beats[1:], bpms, '-', label = 'raw') |
---|
| 62 | |
---|
| 63 | # plot moving median of 5 last periods |
---|
| 64 | median_win_s = 5 |
---|
| 65 | bpms_median = [ median(bpms[i:i + median_win_s:1]) for i in range(len(bpms) - median_win_s ) ] |
---|
| 66 | plt2.plot(beats[median_win_s+1:], bpms_median, '-', label = 'median of %d' % median_win_s) |
---|
| 67 | # plot moving median of 10 last periods |
---|
| 68 | median_win_s = 20 |
---|
| 69 | bpms_median = [ median(bpms[i:i + median_win_s:1]) for i in range(len(bpms) - median_win_s ) ] |
---|
| 70 | plt2.plot(beats[median_win_s+1:], bpms_median, '-', label = 'median of %d' % median_win_s) |
---|
| 71 | |
---|
| 72 | plt2.axis(ymin = min(bpms), ymax = max(bpms)) |
---|
| 73 | #plt2.axis(ymin = 40, ymax = 240) |
---|
| 74 | plt.xlabel('time (mm:ss)') |
---|
| 75 | plt.ylabel('beats per minute (bpm)') |
---|
| 76 | plt2.set_xticklabels([ "%02d:%02d" % (t/60, t%60) for t in plt2.get_xticks()[:-1]], rotation = 50) |
---|
| 77 | |
---|
| 78 | #plt.savefig('/tmp/t.png', dpi=200) |
---|
| 79 | plt2.legend() |
---|
| 80 | plt.show() |
---|
| 81 | |
---|
| 82 | else: |
---|
| 83 | print 'mean period:', "%.2f" % 0, 'bpm', 'median', "%.2f" % 0, 'bpm', |
---|
| 84 | print 'nothing to plot, file too short?' |
---|