[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 | #a = [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)] |
---|
| 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 | nrframe = 0 |
---|
| 111 | while (task.readsize == params.hopsize): |
---|
| 112 | task() |
---|
| 113 | #print task.pos2 |
---|
| 114 | #a[:-hopsize] = [i for i in a[-(btstep-1)*hopsize:]] |
---|
| 115 | #a[-hopsize:] = [task.myvec.get(i,0) for i in t] |
---|
| 116 | |
---|
| 117 | #g('set xrange [%f:%f]' % (t[0],t[-1])) |
---|
| 118 | #time.sleep(.2) |
---|
| 119 | if task.pos2==btstep-1: |
---|
| 120 | nrframe += 1 |
---|
| 121 | dfframe = [task.dfframe.get(i,0) for i in range(winlen)] |
---|
| 122 | if printframe == nrframe or printframe == -1: |
---|
| 123 | d = [[plotdata(range(-winlen,0),dfframe,plottitle="onset detection", with='lines')]] |
---|
| 124 | # start beattracking_do |
---|
| 125 | for i in range(winlen): |
---|
| 126 | dfrev[winlen-1-i] = 0. |
---|
| 127 | dfrev[winlen-1-i] = dfframe[i]*dfwv[i] |
---|
| 128 | aubio_autocorr(task.dfframe(),acf()); |
---|
| 129 | acframe = [acf.get(i,0) for i in range(winlen)] |
---|
| 130 | if not timesig: |
---|
| 131 | numelem = 4 |
---|
| 132 | else: |
---|
| 133 | numelem = timesig |
---|
| 134 | |
---|
| 135 | old = 0 |
---|
| 136 | acfout = [0 for i in range(winlen/4)] |
---|
| 137 | for i in range(1,laglen-1): |
---|
| 138 | for a in range(1,numelem+1): |
---|
| 139 | for b in range (1-a,a): |
---|
| 140 | acfout[i] += acframe[a*(i+1)+b-1] * 1./(2.*a-1.)*rwv[i] |
---|
| 141 | if old < acfout[i]: |
---|
| 142 | old = acfout[i] |
---|
| 143 | maxi = i |
---|
| 144 | rp = max(maxi,1); |
---|
| 145 | |
---|
| 146 | if printframe == nrframe or printframe == -1: |
---|
| 147 | rwvs = [rwv[i]*max(acframe) for i in range(len(rwv))] |
---|
| 148 | d += [[plotdata(acx,acfout,plottitle="comb filterbank", with='lines', axes='x1y1'), |
---|
| 149 | plotdata([rp,rp],[1.2*old,min(acfout)],plottitle="period", with='impulses', axes='x1y1'), |
---|
| 150 | plotdata(acx,rwvs,plottitle="L_w", with='lines', axes='x1y1')]] |
---|
| 151 | |
---|
| 152 | # getperiod |
---|
| 153 | inds = [0 for i in range(maxnumelem)] |
---|
| 154 | localacf = [0 for i in range(winlen)] |
---|
| 155 | period = 0 |
---|
| 156 | for a in range(1,4+1): |
---|
| 157 | for b in range(1-a,a): |
---|
| 158 | localacf[a*rp+b-1] = acframe[a*rp+b-1] |
---|
| 159 | for i in range(numelem): |
---|
| 160 | maxindex = 0 |
---|
| 161 | maxval = 0.0 |
---|
| 162 | for j in range(rp*(i+1)+i): |
---|
| 163 | if localacf[j] > maxval: |
---|
| 164 | maxval = localacf[j] |
---|
| 165 | maxind = j |
---|
| 166 | localacf[j] = 0 |
---|
| 167 | inds[i] = maxind |
---|
| 168 | for i in range(numelem): |
---|
| 169 | period += inds[i]/(i+1.) |
---|
| 170 | period = period/numelem |
---|
| 171 | #print "period", period |
---|
| 172 | |
---|
| 173 | # checkstate |
---|
| 174 | if gp: |
---|
| 175 | # context dependant model |
---|
| 176 | acfout = [0 for i in range(winlen/4)] |
---|
| 177 | old = 0 |
---|
| 178 | for i in range(laglen-1): |
---|
| 179 | for a in range(timesig): |
---|
| 180 | for b in range(1-a,a): |
---|
| 181 | acfout[i] += acframe[a*(i+1)+b-1] * gwv[i] |
---|
| 182 | if old < acfout[i]: |
---|
| 183 | old = acfout[i] |
---|
| 184 | maxi = i |
---|
| 185 | gp = maxi |
---|
| 186 | else: |
---|
| 187 | # general model |
---|
| 188 | gp = 0 |
---|
| 189 | #print "gp", gp |
---|
| 190 | if printframe == nrframe or printframe == -1: |
---|
| 191 | gwvs = [gwv[i]*max(acfout) for i in range(len(gwv))] |
---|
| 192 | d += [[plotdata(acx,acfout,plottitle="comb filterbank", with='lines', axes='x1y1'), |
---|
| 193 | plotdata(gp,old,plottitle="period", with='impulses', axes='x1y1'), |
---|
| 194 | plotdata(acx,gwvs,plottitle="L_{gw}", with='lines', axes='x1y1')]] |
---|
| 195 | |
---|
| 196 | if counter == 0: |
---|
| 197 | # initial step |
---|
| 198 | if abs(gp-rp) > 2.*constthresh: |
---|
| 199 | flagstep = 1 |
---|
| 200 | counter = 3 |
---|
| 201 | else: |
---|
| 202 | flagstep = 0 |
---|
| 203 | #print "flagstep", flagstep |
---|
| 204 | #print "rp2,rp1,rp", rp2,rp1,rp |
---|
| 205 | acfw = [dfframe[i]*dfwv[i] for i in range(winlen)] |
---|
| 206 | |
---|
| 207 | if counter == 1 and flagstep == 1: |
---|
| 208 | # "3rd frame after flagstep set" |
---|
| 209 | if abs(2.*rp-rp1- rp2) < constthresh: |
---|
| 210 | flagconst = 1 |
---|
| 211 | counter = 0 |
---|
| 212 | else: |
---|
| 213 | flagconst = 0 |
---|
| 214 | counter = 2 |
---|
| 215 | elif counter > 0: |
---|
| 216 | counter -= 1 |
---|
| 217 | |
---|
| 218 | rp2 = rp1; rp1 = rp |
---|
| 219 | |
---|
| 220 | if flagconst: |
---|
| 221 | # "first run of new hypothesis" |
---|
| 222 | gp = rp |
---|
| 223 | g_mu = gp |
---|
| 224 | timesig = 4 #FIXME |
---|
| 225 | gwv = [exp(-.5*(j+1.-g_mu)**2/g_var**2) for j in range(laglen)] |
---|
| 226 | flagconst = 0 |
---|
| 227 | bp = gp |
---|
| 228 | phwv = [1 for i in range(2*laglen)] |
---|
| 229 | elif timesig: |
---|
| 230 | # "contex dependant" |
---|
| 231 | bp = gp |
---|
| 232 | if step > lastbeat: |
---|
| 233 | phwv = [exp(-.5*(1.+j-step+lastbeat)**2/(bp/8.)) for j in range(2*laglen)] |
---|
| 234 | else: |
---|
| 235 | print "NOT using phase weighting" |
---|
| 236 | phwv = [1 for i in range(2*laglen)] |
---|
| 237 | else: |
---|
| 238 | # "initial state" |
---|
| 239 | bp = rp |
---|
| 240 | phwv = [1 for i in range(2*laglen)] |
---|
| 241 | |
---|
| 242 | while bp < 25: |
---|
| 243 | print "WARNING, doubling the beat period" |
---|
| 244 | bp *= 2 |
---|
| 245 | |
---|
| 246 | # |
---|
| 247 | phout = [0. for i in range(winlen)] |
---|
| 248 | |
---|
| 249 | kmax = int(winlen/float(bp)); |
---|
| 250 | |
---|
| 251 | old = 0 |
---|
| 252 | for i in range(bp): |
---|
| 253 | phout[i] = 0. |
---|
| 254 | for k in range(kmax): |
---|
| 255 | phout[i] += dfrev[i+bp*k] * phwv[i] |
---|
| 256 | if phout[i] > old: |
---|
| 257 | old = phout[i] |
---|
| 258 | maxi = i |
---|
| 259 | maxindex = maxi |
---|
| 260 | if (maxindex == winlen - 1): maxindex = 0 |
---|
| 261 | phase = 1 + maxindex |
---|
| 262 | i = 1 |
---|
| 263 | beat = bp - phase |
---|
| 264 | beats= [] |
---|
| 265 | if beat >= 0: beats.append(beat) |
---|
| 266 | while beat+bp < step: |
---|
| 267 | beat += bp |
---|
| 268 | beats.append(beat) |
---|
| 269 | lastbeat = beat |
---|
| 270 | #print beats, |
---|
| 271 | #print "the lastbeat is", lastbeat |
---|
| 272 | |
---|
| 273 | # plot all this |
---|
| 274 | if printframe == nrframe or printframe == -1: |
---|
| 275 | phwvs = [phwv[i]*max(phout) for i in range(len(phwv))] |
---|
| 276 | d += [[plotdata(range(-laglen,0),phwvs[laglen:0:-1],plottitle="A_{gw}", with='lines',axes='x1y1'), |
---|
| 277 | plotdata(range(-laglen,0),phout[laglen:0:-1],plottitle="df", with='lines'), |
---|
| 278 | plotdata(-phase,old,plottitle="phase", with='impulses', axes='x1y1'), |
---|
| 279 | plotdata([i for i in beats],[old for i in beats],plottitle="predicted", with='impulses') |
---|
| 280 | ]] |
---|
| 281 | #d += [[plotdata(dfx,dfwv,plottitle="phase weighting", with='lines', axes='x1y2'), |
---|
| 282 | # plotdata(dfx,dfrev,plottitle="df reverse", with='lines', axes='x1y1')]] |
---|
| 283 | #d += [[plotdata(dfx,phout,plottitle="phase", with='lines', axes='x1y2')]] |
---|
| 284 | #d += [[plotdata(dfx,dfwv,plottitle="phase weighting", with='lines', axes='x1y2'), |
---|
| 285 | # plotdata(dfx,dfrev,plottitle="df reverse", with='lines', axes='x1y1')]] |
---|
| 286 | #d += [[plotdata(dfx,phout,plottitle="phase", with='lines', axes='x1y2')]] |
---|
| 287 | |
---|
| 288 | f('set lmargin 4') |
---|
| 289 | f('set rmargin 4') |
---|
| 290 | f('set size %f,%f' % (1.0*xsize,1.0*ysize) ) |
---|
| 291 | f('set key spacing 1.3') |
---|
| 292 | f('set multiplot') |
---|
| 293 | |
---|
| 294 | f('set size %f,%f' % (1.0*xsize,0.33*ysize) ) |
---|
| 295 | f('set orig %f,%f' % (0.0*xsize,0.66*ysize) ) |
---|
| 296 | f('set xrange [%f:%f]' % (-winlen,0) ) |
---|
| 297 | f.title('Onset detection function') |
---|
| 298 | f.xlabel('time (df samples)') |
---|
| 299 | f.plot(*d[0]) |
---|
| 300 | f('set size %f,%f' % (0.5*xsize,0.33*ysize) ) |
---|
| 301 | f('set orig %f,%f' % (0.0*xsize,0.33*ysize) ) |
---|
| 302 | f('set xrange [%f:%f]' % (0,laglen) ) |
---|
| 303 | f.title('Period detection: Rayleygh weighting') |
---|
| 304 | f.xlabel('lag (df samples)') |
---|
| 305 | f.plot(*d[1]) |
---|
| 306 | f('set size %f,%f' % (0.5*xsize,0.33*ysize) ) |
---|
| 307 | f('set orig %f,%f' % (0.5*xsize,0.33*ysize) ) |
---|
| 308 | f('set xrange [%f:%f]' % (0,laglen) ) |
---|
| 309 | f.title('Period detection: Gaussian weighting') |
---|
| 310 | f.xlabel('lag (df samples)') |
---|
| 311 | f.plot(*d[2]) |
---|
| 312 | f('set size %f,%f' % (1.0*xsize,0.33*ysize) ) |
---|
| 313 | f('set orig %f,%f' % (0.0*xsize,0.00*ysize) ) |
---|
| 314 | f('set xrange [%f:%f]' % (-laglen,laglen) ) |
---|
| 315 | f.title('Phase detection and predicted beats') |
---|
| 316 | f.xlabel('time (df samples)') |
---|
| 317 | f.plot(*d[3]) |
---|
| 318 | f('set nomultiplot') |
---|
| 319 | if printframe == -1: a = sys.stdin.read() |
---|
| 320 | elif 0 < printframe and printframe < nrframe: |
---|
| 321 | break |
---|