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 | |
---|
22 | block_read = 0 |
---|
23 | allsamples_max = zeros(0,) |
---|
24 | while True: |
---|
25 | samples, read = s() |
---|
26 | new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0) |
---|
27 | allsamples_max = hstack([allsamples_max, new_maxes]) |
---|
28 | isbeat = o(samples) |
---|
29 | if isbeat: |
---|
30 | thisbeat = (block_read - 4. + isbeat[0]) * hop_s / samplerate |
---|
31 | print "%.4f" % thisbeat |
---|
32 | onsets.append (thisbeat) |
---|
33 | block_read += 1 |
---|
34 | if read < hop_s: break |
---|
35 | |
---|
36 | # do plotting |
---|
37 | from numpy import arange |
---|
38 | from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot, axis |
---|
39 | allsamples_max = (allsamples_max > 0) * allsamples_max |
---|
40 | allsamples_max_times = [ float(t) * hop_s / downsample / samplerate for t in range(len(allsamples_max)) ] |
---|
41 | plot(allsamples_max_times, allsamples_max, '-b') |
---|
42 | plot(allsamples_max_times, -allsamples_max, '-b') |
---|
43 | axis(xmin = 0., xmax = max(allsamples_max_times) ) |
---|
44 | for stamp in onsets: plot([stamp, stamp], [-1., 1.], '.-r') |
---|
45 | xlabel('time (s)') |
---|
46 | ylabel('amplitude') |
---|
47 | show() |
---|
48 | |
---|