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