#! /usr/bin/python """ this file was written by Paul Brossier it is released under the GNU/GPL license. """ import sys,time from aubio.task import taskbeat,taskparams from aubio.aubioclass import fvec, aubio_autocorr from aubio.gnuplot import gnuplot_create from aubio.aubiowrapper import * from math import exp,log usage = "usage: %s [options] -i soundfile" % sys.argv[0] def parse_args(): from optparse import OptionParser parser = OptionParser(usage=usage) parser.add_option("-i","--input", action="store", dest="filename", help="input sound file") parser.add_option("-n","--printframe", action="store", dest="printframe", default=-1, help="make a plot of the n_th frame") parser.add_option("-x","--xsize", action="store", dest="xsize", default=1., type='float', help="define xsize for plot") parser.add_option("-y","--ysize", action="store", dest="ysize", default=1., type='float', help="define ysize for plot") parser.add_option("-O","--outplot", action="store", dest="outplot", default=None, help="save plot to output.{ps,png}") (options, args) = parser.parse_args() if not options.filename: print "no file name given\n", usage sys.exit(1) return options, args def plotdata(x,y,plottitle="",**keyw): import Gnuplot return Gnuplot.Data(x, y, title="%s" % plottitle,**keyw) options, args = parse_args() filename = options.filename xsize = float(options.xsize) ysize = float(options.ysize) printframe = int(options.printframe) if options.outplot and printframe > 0: extension = options.outplot.split('.')[-1] outplot = '.'.join(options.outplot.split('.')[:-1]) else: extension = '' outplot = None f = gnuplot_create(outplot=outplot,extension=extension) params = taskparams() params.onsetmode = 'specdiff' task = taskbeat(filename,params=params) hopsize = params.hopsize bufsize = params.bufsize btstep = task.btstep winlen = task.btwinlen laglen = winlen/4 step = winlen/4 timesig = 0 maxnumelem = 4 gp = 0 counter = 0 flagconst = 0 constthresh = 3.901 g_var = 3.901 rp = 0 rp1 = 0 rp2 = 0 g_mu = 0 rayparam = 48/512.*winlen t = [i for i in range(hopsize)] #tlong = [i for i in range(hopsize*(btstep-1))] #tall = [i for i in range(hopsize*btstep)] sig = [0 for i in range(hopsize*btstep)] dfx = [i for i in range(winlen)] dfframe = [0 for i in range(winlen)] dfrev = [0 for i in range(winlen)] acframe = [0 for i in range(winlen/2)] localacf = [0 for i in range(winlen)] inds = [0 for i in range(maxnumelem)] acx = [i for i in range(laglen)] acfout = [0 for i in range(laglen)] phwv = [0 for i in range(2*laglen)] phwvx = [i for i in range(2*laglen)] dfwvnorm = exp(log(2.0)*(winlen+2.)/rayparam); dfwv = [exp(log(2.)*(i+1.)/rayparam)/dfwvnorm for i in range(winlen)] gwv = [exp(-.5*(j+1.-g_mu)**2/g_var**2) for j in range(laglen)] rwv = [(i+1.)/rayparam**2 * exp(-(i+1.)**2 / (2.*rayparam)**2) for i in range(0,laglen)] acf = fvec(winlen,1) if options.outplot and printframe > 0: extension = options.outplot.split('.')[-1] outplot = '.'.join(options.outplot.split('.')[:-1]) else: extension = '' outplot = None f = gnuplot_create(outplot=outplot,extension=extension) nrframe = 0 while (task.readsize == params.hopsize): task() #print task.pos2 sig[:-hopsize] = [i for i in sig[-(btstep-1)*hopsize:]] sig[-hopsize:] = [task.myvec.get(i,0) for i in t] #g('set xrange [%f:%f]' % (t[0],t[-1])) #time.sleep(.2) if task.pos2==btstep-1: nrframe += 1 dfframe = [task.dfframe.get(i,0) for i in range(winlen)] # start beattracking_do for i in range(winlen): dfrev[winlen-1-i] = 0. dfrev[winlen-1-i] = dfframe[i]*dfwv[i] aubio_autocorr(task.dfframe(),acf()); acframe = [acf.get(i,0) for i in range(winlen/2)] if printframe == nrframe or printframe == -1: d = [[plotdata(range(btstep*hopsize),sig,plottitle="input signal", with='lines')]] d += [[plotdata(range(-winlen,0),dfframe,plottitle="onset detection", with='lines')]] d += [[plotdata(range(winlen/2),acframe,plottitle="autocorrelation", with='lines')]] # plot all this if printframe == nrframe or printframe == -1: f('set lmargin 4') f('set rmargin 4') f('set size %f,%f' % (1.0*xsize,1.0*ysize) ) f('set key spacing 1.3') f('set multiplot') f('set size %f,%f' % (1.0*xsize,0.33*ysize) ) f('set orig %f,%f' % (0.0*xsize,0.66*ysize) ) f('set xrange [%f:%f]' % (0,btstep*hopsize) ) f.title('Input signal') f.xlabel('time (samples)') f.plot(*d[0]) f('set size %f,%f' % (1.0*xsize,0.33*ysize) ) f('set orig %f,%f' % (0.0*xsize,0.33*ysize) ) f('set xrange [%f:%f]' % (-winlen,0) ) f.title('Onset detection function') f.xlabel('time (df samples)') f.plot(*d[1]) f('set size %f,%f' % (1.0*xsize,0.33*ysize) ) f('set orig %f,%f' % (0.0*xsize,0.00*ysize) ) f('set xrange [%f:%f]' % (0,winlen/2) ) f.title('Autocorrelation') f.xlabel('lag (df samples)') f.plot(*d[2]) f('set nomultiplot') if printframe == -1: a = sys.stdin.read() elif 0 < printframe and printframe < nrframe: break