[cf80e59] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | from aubio import onset, source |
---|
[8fb567c] | 5 | from numpy import hstack, zeros |
---|
[cf80e59] | 6 | |
---|
| 7 | win_s = 512 # fft size |
---|
[4120fbc] | 8 | hop_s = win_s // 2 # hop size |
---|
[cf80e59] | 9 | |
---|
| 10 | if len(sys.argv) < 2: |
---|
[4120fbc] | 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 | # list of onsets, in samples |
---|
[e6f7a4a] | 24 | onsets = [] |
---|
| 25 | |
---|
| 26 | # storage for plotted data |
---|
[f36277c] | 27 | desc = [] |
---|
| 28 | tdesc = [] |
---|
[cf80e59] | 29 | allsamples_max = zeros(0,) |
---|
[e6f7a4a] | 30 | downsample = 2 # to plot n samples / hop_s |
---|
| 31 | |
---|
[459e46f] | 32 | # total number of frames read |
---|
[e6f7a4a] | 33 | total_frames = 0 |
---|
[cf80e59] | 34 | while True: |
---|
| 35 | samples, read = s() |
---|
[8b884ef] | 36 | if o(samples): |
---|
[4120fbc] | 37 | print("%f" % (o.get_last_s())) |
---|
[8b884ef] | 38 | onsets.append(o.get_last()) |
---|
[e6f7a4a] | 39 | # keep some data to plot it later |
---|
[4120fbc] | 40 | new_maxes = (abs(samples.reshape(hop_s//downsample, downsample))).max(axis=0) |
---|
[cf80e59] | 41 | allsamples_max = hstack([allsamples_max, new_maxes]) |
---|
[f36277c] | 42 | desc.append(o.get_descriptor()) |
---|
| 43 | tdesc.append(o.get_thresholded_descriptor()) |
---|
[e6f7a4a] | 44 | total_frames += read |
---|
[cf80e59] | 45 | if read < hop_s: break |
---|
| 46 | |
---|
[129b269] | 47 | if 1: |
---|
| 48 | # do plotting |
---|
| 49 | import matplotlib.pyplot as plt |
---|
| 50 | allsamples_max = (allsamples_max > 0) * allsamples_max |
---|
| 51 | allsamples_max_times = [ float(t) * hop_s / downsample / samplerate for t in range(len(allsamples_max)) ] |
---|
| 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 | plt1.plot(allsamples_max_times, allsamples_max, '-b') |
---|
| 56 | plt1.plot(allsamples_max_times, -allsamples_max, '-b') |
---|
[459e46f] | 57 | for stamp in onsets: |
---|
| 58 | stamp /= float(samplerate) |
---|
| 59 | plt1.plot([stamp, stamp], [-1., 1.], '-r') |
---|
[129b269] | 60 | plt1.axis(xmin = 0., xmax = max(allsamples_max_times) ) |
---|
| 61 | plt1.xaxis.set_visible(False) |
---|
| 62 | plt1.yaxis.set_visible(False) |
---|
| 63 | desc_times = [ float(t) * hop_s / samplerate for t in range(len(desc)) ] |
---|
[4120fbc] | 64 | desc_max = max(desc) if max(desc) != 0 else 1. |
---|
| 65 | desc_plot = [d / desc_max for d in desc] |
---|
[129b269] | 66 | plt2.plot(desc_times, desc_plot, '-g') |
---|
[4120fbc] | 67 | tdesc_plot = [d / desc_max for d in tdesc] |
---|
[459e46f] | 68 | for stamp in onsets: |
---|
| 69 | stamp /= float(samplerate) |
---|
| 70 | plt2.plot([stamp, stamp], [min(tdesc_plot), max(desc_plot)], '-r') |
---|
[129b269] | 71 | plt2.plot(desc_times, tdesc_plot, '-y') |
---|
| 72 | plt2.axis(ymin = min(tdesc_plot), ymax = max(desc_plot)) |
---|
| 73 | plt.xlabel('time (s)') |
---|
| 74 | #plt.savefig('/tmp/t.png', dpi=200) |
---|
| 75 | plt.show() |
---|