From cf80e59e80a6fd2c867af36155d99db419a6cfab Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Tue, 12 Feb 2013 04:42:50 -0500 Subject: [PATCH] demos/demo_onset_file.py: added onset example --- python/demos/demo_onset_file.py | 53 +++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 python/demos/demo_onset_file.py diff --git a/python/demos/demo_onset_file.py b/python/demos/demo_onset_file.py new file mode 100755 index 00000000..cea96986 --- /dev/null +++ b/python/demos/demo_onset_file.py @@ -0,0 +1,53 @@ +#! /usr/bin/env python + +import sys +from aubio import onset, source +from numpy import array, hstack, zeros + +win_s = 512 # fft size +hop_s = win_s / 2 # hop size +samplerate = 44100 +downsample = 2 # used to plot n samples / hop_s + +if len(sys.argv) < 2: + print "Usage: %s " % sys.argv[0] + sys.exit(1) + +filename = sys.argv[1] +onsets = [] +oldonsets = [] + +s = source(filename, samplerate, hop_s) +o = onset("default", win_s, hop_s) + +block_read = 0 +allsamples_max = zeros(0,) +while True: + samples, read = s() + new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0) + allsamples_max = hstack([allsamples_max, new_maxes]) + isbeat = o(samples) + if isbeat: + thisbeat = (block_read - 4. + isbeat[0]) * hop_s / samplerate + print "%.4f" % thisbeat + onsets.append (thisbeat) + # old onset + thisbeat = (block_read - 3. ) * hop_s / samplerate + oldonsets.append (thisbeat) + block_read += 1 + if read < hop_s: break + +# do plotting +from numpy import arange +from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot, axis +allsamples_max = (allsamples_max > 0) * allsamples_max +allsamples_max_times = [ float(t) * hop_s / downsample / samplerate for t in range(len(allsamples_max)) ] +plot(allsamples_max_times, allsamples_max, '-b') +plot(allsamples_max_times, -allsamples_max, '-b') +axis(xmin = 0., xmax = max(allsamples_max_times) ) +for stamp in onsets: plot([stamp, stamp], [-1., 1.], '.-r') +for stamp in oldonsets: plot([stamp, stamp], [-1., 1.], '.-g') +xlabel('time (s)') +ylabel('amplitude') +show() + -- 2.26.2