source: python/demos/demo_tempo_plot.py @ 735a739

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

python/demos/demo_tempo_plot.py: import first

  • Property mode set to 100755
File size: 2.6 KB
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
8
9if len(sys.argv) < 2:
10    print "Usage: %s <filename> [samplerate]" % sys.argv[0]
11    sys.exit(1)
12
13filename = sys.argv[1]
14
15samplerate = 0
16if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
17
18s = source(filename, samplerate, hop_s)
19samplerate = s.samplerate
20o = tempo("default", win_s, hop_s, samplerate)
21
22# tempo detection delay, in samples
23# default to 4 blocks delay to catch up with
24delay = 4. * hop_s
25
26# list of beats, in samples
27beats = []
28
29# total number of frames read
30total_frames = 0
31while 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
42beats = map( lambda x: x / float(samplerate), beats)
43
44bpms = [60./(b - a) for a,b in zip(beats[:-1],beats[1:])]
45
46if len(bpms):
47    # do plotting
48    from numpy import array, arange, mean, median
49    import matplotlib.pyplot as plt
50    print 'mean period:', "%.2f" % mean(bpms), 'bpm', 'median', "%.2f" % median(bpms), 'bpm'
51    print 'plotting', filename
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
82else:
83    print 'mean period:', "%.2f" % 0, 'bpm', 'median', "%.2f" % 0, 'bpm', 
84    print 'nothing to plot, file too short?'
Note: See TracBrowser for help on using the repository browser.