[aa3637a] | 1 | #! /usr/bin/python |
---|
| 2 | |
---|
| 3 | """ this file was written by Paul Brossier |
---|
| 4 | it is released under the GNU/GPL license. |
---|
| 5 | """ |
---|
| 6 | |
---|
| 7 | import sys,time |
---|
| 8 | from aubio.task import taskbeat,taskparams |
---|
| 9 | from aubio.aubioclass import fvec, aubio_autocorr |
---|
| 10 | from aubio.gnuplot import gnuplot_create |
---|
| 11 | from aubio.aubiowrapper import * |
---|
| 12 | from math import exp,log |
---|
| 13 | |
---|
| 14 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
---|
| 15 | |
---|
| 16 | def parse_args(): |
---|
| 17 | from optparse import OptionParser |
---|
| 18 | parser = OptionParser(usage=usage) |
---|
| 19 | parser.add_option("-i","--input", |
---|
| 20 | action="store", dest="filename", |
---|
| 21 | help="input sound file") |
---|
| 22 | parser.add_option("-n","--printframe", |
---|
| 23 | action="store", dest="printframe", default=-1, |
---|
| 24 | help="make a plot of the n_th frame") |
---|
| 25 | parser.add_option("-x","--xsize", |
---|
| 26 | action="store", dest="xsize", default=1., |
---|
| 27 | type='float', help="define xsize for plot") |
---|
| 28 | parser.add_option("-y","--ysize", |
---|
| 29 | action="store", dest="ysize", default=1., |
---|
| 30 | type='float', help="define ysize for plot") |
---|
| 31 | parser.add_option("-O","--outplot", |
---|
| 32 | action="store", dest="outplot", default=None, |
---|
| 33 | help="save plot to output.{ps,png}") |
---|
| 34 | (options, args) = parser.parse_args() |
---|
| 35 | if not options.filename: |
---|
| 36 | print "no file name given\n", usage |
---|
| 37 | sys.exit(1) |
---|
| 38 | return options, args |
---|
| 39 | |
---|
| 40 | def plotdata(x,y,plottitle="",**keyw): |
---|
| 41 | import Gnuplot |
---|
| 42 | return Gnuplot.Data(x, y, title="%s" % plottitle,**keyw) |
---|
| 43 | |
---|
| 44 | options, args = parse_args() |
---|
| 45 | filename = options.filename |
---|
| 46 | xsize = float(options.xsize) |
---|
| 47 | ysize = float(options.ysize) |
---|
| 48 | |
---|
| 49 | printframe = int(options.printframe) |
---|
| 50 | |
---|
| 51 | if options.outplot and printframe > 0: |
---|
| 52 | extension = options.outplot.split('.')[-1] |
---|
| 53 | outplot = '.'.join(options.outplot.split('.')[:-1]) |
---|
| 54 | else: |
---|
| 55 | extension = '' |
---|
| 56 | outplot = None |
---|
| 57 | f = gnuplot_create(outplot=outplot,extension=extension) |
---|
| 58 | |
---|
| 59 | params = taskparams() |
---|
| 60 | params.onsetmode = 'specdiff' |
---|
| 61 | task = taskbeat(filename,params=params) |
---|
| 62 | |
---|
| 63 | hopsize = params.hopsize |
---|
| 64 | bufsize = params.bufsize |
---|
| 65 | btstep = task.btstep |
---|
| 66 | winlen = task.btwinlen |
---|
| 67 | laglen = winlen/4 |
---|
| 68 | step = winlen/4 |
---|
| 69 | |
---|
| 70 | timesig = 0 |
---|
| 71 | maxnumelem = 4 |
---|
| 72 | gp = 0 |
---|
| 73 | counter = 0 |
---|
| 74 | flagconst = 0 |
---|
| 75 | constthresh = 3.901 |
---|
| 76 | g_var = 3.901 |
---|
| 77 | rp = 0 |
---|
| 78 | rp1 = 0 |
---|
| 79 | rp2 = 0 |
---|
| 80 | g_mu = 0 |
---|
| 81 | |
---|
| 82 | rayparam = 48/512.*winlen |
---|
| 83 | |
---|
| 84 | t = [i for i in range(hopsize)] |
---|
| 85 | #tlong = [i for i in range(hopsize*(btstep-1))] |
---|
| 86 | #tall = [i for i in range(hopsize*btstep)] |
---|
| 87 | sig = [0 for i in range(hopsize*btstep)] |
---|
| 88 | dfx = [i for i in range(winlen)] |
---|
| 89 | dfframe = [0 for i in range(winlen)] |
---|
| 90 | dfrev = [0 for i in range(winlen)] |
---|
| 91 | acframe = [0 for i in range(winlen/2)] |
---|
| 92 | |
---|
| 93 | localacf = [0 for i in range(winlen)] |
---|
| 94 | inds = [0 for i in range(maxnumelem)] |
---|
| 95 | |
---|
| 96 | acx = [i for i in range(laglen)] |
---|
| 97 | acfout = [0 for i in range(laglen)] |
---|
| 98 | |
---|
| 99 | phwv = [0 for i in range(2*laglen)] |
---|
| 100 | phwvx = [i for i in range(2*laglen)] |
---|
| 101 | |
---|
| 102 | dfwvnorm = exp(log(2.0)*(winlen+2.)/rayparam); |
---|
| 103 | dfwv = [exp(log(2.)*(i+1.)/rayparam)/dfwvnorm for i in range(winlen)] |
---|
| 104 | |
---|
| 105 | gwv = [exp(-.5*(j+1.-g_mu)**2/g_var**2) for j in range(laglen)] |
---|
| 106 | rwv = [(i+1.)/rayparam**2 * exp(-(i+1.)**2 / (2.*rayparam)**2) |
---|
| 107 | for i in range(0,laglen)] |
---|
| 108 | acf = fvec(winlen,1) |
---|
| 109 | |
---|
| 110 | if options.outplot and printframe > 0: |
---|
| 111 | extension = options.outplot.split('.')[-1] |
---|
| 112 | outplot = '.'.join(options.outplot.split('.')[:-1]) |
---|
| 113 | else: |
---|
| 114 | extension = '' |
---|
| 115 | outplot = None |
---|
| 116 | f = gnuplot_create(outplot=outplot,extension=extension) |
---|
| 117 | nrframe = 0 |
---|
| 118 | while (task.readsize == params.hopsize): |
---|
| 119 | task() |
---|
| 120 | #print task.pos2 |
---|
| 121 | sig[:-hopsize] = [i for i in sig[-(btstep-1)*hopsize:]] |
---|
| 122 | sig[-hopsize:] = [task.myvec.get(i,0) for i in t] |
---|
| 123 | |
---|
| 124 | #g('set xrange [%f:%f]' % (t[0],t[-1])) |
---|
| 125 | #time.sleep(.2) |
---|
| 126 | if task.pos2==btstep-1: |
---|
| 127 | nrframe += 1 |
---|
| 128 | dfframe = [task.dfframe.get(i,0) for i in range(winlen)] |
---|
| 129 | # start beattracking_do |
---|
| 130 | for i in range(winlen): |
---|
| 131 | dfrev[winlen-1-i] = 0. |
---|
| 132 | dfrev[winlen-1-i] = dfframe[i]*dfwv[i] |
---|
| 133 | aubio_autocorr(task.dfframe(),acf()); |
---|
| 134 | acframe = [acf.get(i,0) for i in range(winlen/2)] |
---|
| 135 | if printframe == nrframe or printframe == -1: |
---|
| 136 | d = [[plotdata(range(btstep*hopsize),sig,plottitle="input signal", with='lines')]] |
---|
| 137 | d += [[plotdata(range(-winlen,0),dfframe,plottitle="onset detection", with='lines')]] |
---|
| 138 | d += [[plotdata(range(winlen/2),acframe,plottitle="autocorrelation", with='lines')]] |
---|
| 139 | |
---|
| 140 | # plot all this |
---|
| 141 | if printframe == nrframe or printframe == -1: |
---|
| 142 | |
---|
| 143 | f('set lmargin 4') |
---|
| 144 | f('set rmargin 4') |
---|
| 145 | f('set size %f,%f' % (1.0*xsize,1.0*ysize) ) |
---|
| 146 | f('set key spacing 1.3') |
---|
| 147 | f('set multiplot') |
---|
| 148 | |
---|
| 149 | f('set size %f,%f' % (1.0*xsize,0.33*ysize) ) |
---|
| 150 | f('set orig %f,%f' % (0.0*xsize,0.66*ysize) ) |
---|
| 151 | f('set xrange [%f:%f]' % (0,btstep*hopsize) ) |
---|
| 152 | f.title('Input signal') |
---|
| 153 | f.xlabel('time (samples)') |
---|
| 154 | f.plot(*d[0]) |
---|
| 155 | f('set size %f,%f' % (1.0*xsize,0.33*ysize) ) |
---|
| 156 | f('set orig %f,%f' % (0.0*xsize,0.33*ysize) ) |
---|
| 157 | f('set xrange [%f:%f]' % (-winlen,0) ) |
---|
| 158 | f.title('Onset detection function') |
---|
| 159 | f.xlabel('time (df samples)') |
---|
| 160 | f.plot(*d[1]) |
---|
| 161 | f('set size %f,%f' % (1.0*xsize,0.33*ysize) ) |
---|
| 162 | f('set orig %f,%f' % (0.0*xsize,0.00*ysize) ) |
---|
| 163 | f('set xrange [%f:%f]' % (0,winlen/2) ) |
---|
| 164 | f.title('Autocorrelation') |
---|
| 165 | f.xlabel('lag (df samples)') |
---|
| 166 | f.plot(*d[2]) |
---|
| 167 | f('set nomultiplot') |
---|
| 168 | if printframe == -1: a = sys.stdin.read() |
---|
| 169 | elif 0 < printframe and printframe < nrframe: |
---|
| 170 | break |
---|