[cf80e59] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | from aubio import onset, source |
---|
| 5 | from numpy import array, hstack, zeros |
---|
| 6 | |
---|
| 7 | win_s = 512 # fft size |
---|
| 8 | hop_s = win_s / 2 # hop size |
---|
| 9 | samplerate = 44100 |
---|
| 10 | downsample = 2 # used to plot n samples / hop_s |
---|
| 11 | |
---|
| 12 | if len(sys.argv) < 2: |
---|
| 13 | print "Usage: %s <filename>" % sys.argv[0] |
---|
| 14 | sys.exit(1) |
---|
| 15 | |
---|
| 16 | filename = sys.argv[1] |
---|
| 17 | onsets = [] |
---|
| 18 | |
---|
| 19 | s = source(filename, samplerate, hop_s) |
---|
| 20 | o = onset("default", win_s, hop_s) |
---|
| 21 | |
---|
[f36277c] | 22 | desc = [] |
---|
| 23 | tdesc = [] |
---|
| 24 | |
---|
[cf80e59] | 25 | block_read = 0 |
---|
| 26 | allsamples_max = zeros(0,) |
---|
| 27 | while True: |
---|
| 28 | samples, read = s() |
---|
| 29 | new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0) |
---|
| 30 | allsamples_max = hstack([allsamples_max, new_maxes]) |
---|
| 31 | isbeat = o(samples) |
---|
[f36277c] | 32 | desc.append(o.get_descriptor()) |
---|
| 33 | tdesc.append(o.get_thresholded_descriptor()) |
---|
[cf80e59] | 34 | if isbeat: |
---|
| 35 | thisbeat = (block_read - 4. + isbeat[0]) * hop_s / samplerate |
---|
| 36 | print "%.4f" % thisbeat |
---|
| 37 | onsets.append (thisbeat) |
---|
| 38 | block_read += 1 |
---|
| 39 | if read < hop_s: break |
---|
| 40 | |
---|
| 41 | # do plotting |
---|
| 42 | from numpy import arange |
---|
[f36277c] | 43 | import matplotlib.pyplot as plt |
---|
[cf80e59] | 44 | allsamples_max = (allsamples_max > 0) * allsamples_max |
---|
| 45 | allsamples_max_times = [ float(t) * hop_s / downsample / samplerate for t in range(len(allsamples_max)) ] |
---|
[f36277c] | 46 | plt1 = plt.axes([0.1, 0.75, 0.8, 0.19]) |
---|
| 47 | plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1) |
---|
| 48 | plt.rc('lines',linewidth='.8') |
---|
| 49 | plt1.plot(allsamples_max_times, allsamples_max, '-b') |
---|
| 50 | plt1.plot(allsamples_max_times, -allsamples_max, '-b') |
---|
| 51 | for stamp in onsets: plt1.plot([stamp, stamp], [-1., 1.], '-r') |
---|
| 52 | plt1.axis(xmin = 0., xmax = max(allsamples_max_times) ) |
---|
| 53 | plt1.xaxis.set_visible(False) |
---|
| 54 | desc_times = [ float(t) * hop_s / samplerate for t in range(len(desc)) ] |
---|
| 55 | desc_plot = [d / max(desc) for d in desc] |
---|
| 56 | plt2.plot(desc_times, desc_plot, '-g') |
---|
| 57 | tdesc_plot = [d / max(desc) for d in tdesc] |
---|
| 58 | for stamp in onsets: plt2.plot([stamp, stamp], [min(tdesc_plot), max(desc_plot)], '-r') |
---|
| 59 | plt2.plot(desc_times, tdesc_plot, '-y') |
---|
| 60 | plt2.axis(ymin = min(tdesc_plot), ymax = max(desc_plot)) |
---|
| 61 | plt.xlabel('time (s)') |
---|
| 62 | #plt.savefig('/tmp/t.png', dpi=200) |
---|
| 63 | plt.show() |
---|