[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 |
---|
[dbd19ea] | 10 | from aubio.gnuplot import gnuplot_create, gnuplot_addargs |
---|
[aa3637a] | 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") |
---|
[dbd19ea] | 25 | gnuplot_addargs(parser) |
---|
[aa3637a] | 26 | (options, args) = parser.parse_args() |
---|
| 27 | if not options.filename: |
---|
| 28 | print "no file name given\n", usage |
---|
| 29 | sys.exit(1) |
---|
| 30 | return options, args |
---|
| 31 | |
---|
| 32 | def plotdata(x,y,plottitle="",**keyw): |
---|
| 33 | import Gnuplot |
---|
| 34 | return Gnuplot.Data(x, y, title="%s" % plottitle,**keyw) |
---|
| 35 | |
---|
| 36 | options, args = parse_args() |
---|
| 37 | filename = options.filename |
---|
| 38 | xsize = float(options.xsize) |
---|
| 39 | ysize = float(options.ysize) |
---|
| 40 | |
---|
| 41 | printframe = int(options.printframe) |
---|
| 42 | |
---|
| 43 | if options.outplot and printframe > 0: |
---|
| 44 | extension = options.outplot.split('.')[-1] |
---|
| 45 | outplot = '.'.join(options.outplot.split('.')[:-1]) |
---|
| 46 | else: |
---|
| 47 | extension = '' |
---|
| 48 | outplot = None |
---|
[dbd19ea] | 49 | f = gnuplot_create(outplot,extension,options) |
---|
[aa3637a] | 50 | |
---|
| 51 | params = taskparams() |
---|
| 52 | params.onsetmode = 'specdiff' |
---|
| 53 | task = taskbeat(filename,params=params) |
---|
| 54 | |
---|
| 55 | hopsize = params.hopsize |
---|
| 56 | bufsize = params.bufsize |
---|
| 57 | btstep = task.btstep |
---|
| 58 | winlen = task.btwinlen |
---|
| 59 | laglen = winlen/4 |
---|
| 60 | step = winlen/4 |
---|
| 61 | |
---|
| 62 | timesig = 0 |
---|
| 63 | maxnumelem = 4 |
---|
| 64 | gp = 0 |
---|
| 65 | counter = 0 |
---|
| 66 | flagconst = 0 |
---|
| 67 | constthresh = 3.901 |
---|
| 68 | g_var = 3.901 |
---|
| 69 | rp = 0 |
---|
| 70 | rp1 = 0 |
---|
| 71 | rp2 = 0 |
---|
| 72 | g_mu = 0 |
---|
| 73 | |
---|
| 74 | rayparam = 48/512.*winlen |
---|
| 75 | |
---|
| 76 | t = [i for i in range(hopsize)] |
---|
| 77 | #tlong = [i for i in range(hopsize*(btstep-1))] |
---|
| 78 | #tall = [i for i in range(hopsize*btstep)] |
---|
[9c3b696] | 79 | sig = [0 for i in range(hopsize*btstep*4)] |
---|
[aa3637a] | 80 | dfx = [i for i in range(winlen)] |
---|
| 81 | dfframe = [0 for i in range(winlen)] |
---|
| 82 | dfrev = [0 for i in range(winlen)] |
---|
| 83 | acframe = [0 for i in range(winlen/2)] |
---|
| 84 | |
---|
| 85 | localacf = [0 for i in range(winlen)] |
---|
| 86 | inds = [0 for i in range(maxnumelem)] |
---|
| 87 | |
---|
| 88 | acx = [i for i in range(laglen)] |
---|
| 89 | acfout = [0 for i in range(laglen)] |
---|
| 90 | |
---|
| 91 | phwv = [0 for i in range(2*laglen)] |
---|
| 92 | phwvx = [i for i in range(2*laglen)] |
---|
| 93 | |
---|
| 94 | dfwvnorm = exp(log(2.0)*(winlen+2.)/rayparam); |
---|
| 95 | dfwv = [exp(log(2.)*(i+1.)/rayparam)/dfwvnorm for i in range(winlen)] |
---|
| 96 | |
---|
| 97 | gwv = [exp(-.5*(j+1.-g_mu)**2/g_var**2) for j in range(laglen)] |
---|
| 98 | rwv = [(i+1.)/rayparam**2 * exp(-(i+1.)**2 / (2.*rayparam)**2) |
---|
| 99 | for i in range(0,laglen)] |
---|
| 100 | acf = fvec(winlen,1) |
---|
| 101 | |
---|
| 102 | nrframe = 0 |
---|
| 103 | while (task.readsize == params.hopsize): |
---|
| 104 | task() |
---|
| 105 | #print task.pos2 |
---|
[9c3b696] | 106 | sig[:-hopsize] = [i for i in sig[-(btstep*4-1)*hopsize:]] |
---|
[aa3637a] | 107 | sig[-hopsize:] = [task.myvec.get(i,0) for i in t] |
---|
| 108 | |
---|
| 109 | #g('set xrange [%f:%f]' % (t[0],t[-1])) |
---|
| 110 | #time.sleep(.2) |
---|
| 111 | if task.pos2==btstep-1: |
---|
| 112 | nrframe += 1 |
---|
| 113 | dfframe = [task.dfframe.get(i,0) for i in range(winlen)] |
---|
| 114 | # start beattracking_do |
---|
| 115 | for i in range(winlen): |
---|
| 116 | dfrev[winlen-1-i] = 0. |
---|
| 117 | dfrev[winlen-1-i] = dfframe[i]*dfwv[i] |
---|
| 118 | aubio_autocorr(task.dfframe(),acf()); |
---|
| 119 | acframe = [acf.get(i,0) for i in range(winlen/2)] |
---|
| 120 | if printframe == nrframe or printframe == -1: |
---|
[9c3b696] | 121 | d = [[plotdata(range(0,btstep*hopsize*4,4),sig[0:-1:4],plottitle="input signal", with='lines')]] |
---|
[aa3637a] | 122 | d += [[plotdata(range(-winlen,0),dfframe,plottitle="onset detection", with='lines')]] |
---|
| 123 | d += [[plotdata(range(winlen/2),acframe,plottitle="autocorrelation", with='lines')]] |
---|
| 124 | |
---|
| 125 | # plot all this |
---|
| 126 | if printframe == nrframe or printframe == -1: |
---|
| 127 | |
---|
| 128 | f('set lmargin 4') |
---|
| 129 | f('set rmargin 4') |
---|
| 130 | f('set size %f,%f' % (1.0*xsize,1.0*ysize) ) |
---|
| 131 | f('set key spacing 1.3') |
---|
| 132 | f('set multiplot') |
---|
| 133 | |
---|
| 134 | f('set size %f,%f' % (1.0*xsize,0.33*ysize) ) |
---|
| 135 | f('set orig %f,%f' % (0.0*xsize,0.66*ysize) ) |
---|
[9c3b696] | 136 | f('set xrange [%f:%f]' % (0,btstep*hopsize*4) ) |
---|
[dbd19ea] | 137 | f('set yrange [%f:%f]' % (-1.2*max(sig),1.2*max(sig)) ) |
---|
[aa3637a] | 138 | f.title('Input signal') |
---|
| 139 | f.xlabel('time (samples)') |
---|
| 140 | f.plot(*d[0]) |
---|
| 141 | f('set size %f,%f' % (1.0*xsize,0.33*ysize) ) |
---|
| 142 | f('set orig %f,%f' % (0.0*xsize,0.33*ysize) ) |
---|
| 143 | f('set xrange [%f:%f]' % (-winlen,0) ) |
---|
[dbd19ea] | 144 | f('set autoscale y') |
---|
[aa3637a] | 145 | f.title('Onset detection function') |
---|
| 146 | f.xlabel('time (df samples)') |
---|
| 147 | f.plot(*d[1]) |
---|
| 148 | f('set size %f,%f' % (1.0*xsize,0.33*ysize) ) |
---|
| 149 | f('set orig %f,%f' % (0.0*xsize,0.00*ysize) ) |
---|
| 150 | f('set xrange [%f:%f]' % (0,winlen/2) ) |
---|
| 151 | f.title('Autocorrelation') |
---|
| 152 | f.xlabel('lag (df samples)') |
---|
| 153 | f.plot(*d[2]) |
---|
| 154 | f('set nomultiplot') |
---|
| 155 | if printframe == -1: a = sys.stdin.read() |
---|
| 156 | elif 0 < printframe and printframe < nrframe: |
---|
| 157 | break |
---|