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 | |
---|
10 | if len(sys.argv) < 2: |
---|
11 | print "Usage: %s <filename> [samplerate]" % sys.argv[0] |
---|
12 | sys.exit(1) |
---|
13 | |
---|
14 | filename = sys.argv[1] |
---|
15 | |
---|
16 | samplerate = 0 |
---|
17 | if len( sys.argv ) > 2: samplerate = int(sys.argv[2]) |
---|
18 | |
---|
19 | s = source(filename, samplerate, hop_s) |
---|
20 | samplerate = s.samplerate |
---|
21 | o = onset("default", win_s, hop_s, samplerate) |
---|
22 | |
---|
23 | # list of onsets, in samples |
---|
24 | onsets = [] |
---|
25 | |
---|
26 | # storage for plotted data |
---|
27 | desc = [] |
---|
28 | tdesc = [] |
---|
29 | allsamples_max = zeros(0,) |
---|
30 | downsample = 2 # to plot n samples / hop_s |
---|
31 | |
---|
32 | # total number of frames read |
---|
33 | total_frames = 0 |
---|
34 | while True: |
---|
35 | samples, read = s() |
---|
36 | is_onset = o(samples) |
---|
37 | if is_onset: |
---|
38 | this_onset = o.get_last_onset() |
---|
39 | print "%f" % (this_onset / float(samplerate)) |
---|
40 | onsets.append(this_onset) |
---|
41 | # keep some data to plot it later |
---|
42 | new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0) |
---|
43 | allsamples_max = hstack([allsamples_max, new_maxes]) |
---|
44 | desc.append(o.get_descriptor()) |
---|
45 | tdesc.append(o.get_thresholded_descriptor()) |
---|
46 | total_frames += read |
---|
47 | if read < hop_s: break |
---|
48 | |
---|
49 | if 1: |
---|
50 | # do plotting |
---|
51 | from numpy import arange |
---|
52 | import matplotlib.pyplot as plt |
---|
53 | allsamples_max = (allsamples_max > 0) * allsamples_max |
---|
54 | allsamples_max_times = [ float(t) * hop_s / downsample / samplerate for t in range(len(allsamples_max)) ] |
---|
55 | plt1 = plt.axes([0.1, 0.75, 0.8, 0.19]) |
---|
56 | plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1) |
---|
57 | plt.rc('lines',linewidth='.8') |
---|
58 | plt1.plot(allsamples_max_times, allsamples_max, '-b') |
---|
59 | plt1.plot(allsamples_max_times, -allsamples_max, '-b') |
---|
60 | for stamp in onsets: |
---|
61 | stamp /= float(samplerate) |
---|
62 | plt1.plot([stamp, stamp], [-1., 1.], '-r') |
---|
63 | plt1.axis(xmin = 0., xmax = max(allsamples_max_times) ) |
---|
64 | plt1.xaxis.set_visible(False) |
---|
65 | plt1.yaxis.set_visible(False) |
---|
66 | desc_times = [ float(t) * hop_s / samplerate for t in range(len(desc)) ] |
---|
67 | desc_plot = [d / max(desc) for d in desc] |
---|
68 | plt2.plot(desc_times, desc_plot, '-g') |
---|
69 | tdesc_plot = [d / max(desc) for d in tdesc] |
---|
70 | for stamp in onsets: |
---|
71 | stamp /= float(samplerate) |
---|
72 | plt2.plot([stamp, stamp], [min(tdesc_plot), max(desc_plot)], '-r') |
---|
73 | plt2.plot(desc_times, tdesc_plot, '-y') |
---|
74 | plt2.axis(ymin = min(tdesc_plot), ymax = max(desc_plot)) |
---|
75 | plt.xlabel('time (s)') |
---|
76 | #plt.savefig('/tmp/t.png', dpi=200) |
---|
77 | plt.show() |
---|