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