[13c3fba] | 1 | from aubio.task.task import task |
---|
| 2 | from aubio.aubioclass import * |
---|
| 3 | |
---|
| 4 | class taskonset(task): |
---|
| 5 | def __init__(self,input,output=None,params=None): |
---|
| 6 | """ open the input file and initialize arguments |
---|
| 7 | parameters should be set *before* calling this method. |
---|
| 8 | """ |
---|
| 9 | task.__init__(self,input,params=params) |
---|
| 10 | self.opick = onsetpick(self.params.bufsize, |
---|
| 11 | self.params.hopsize, |
---|
| 12 | self.myvec, |
---|
| 13 | self.params.threshold, |
---|
[515c7b2] | 14 | mode=self.params.onsetmode, |
---|
[13c3fba] | 15 | dcthreshold=self.params.dcthreshold, |
---|
| 16 | derivate=self.params.derivate) |
---|
| 17 | self.olist = [] |
---|
| 18 | self.ofunc = [] |
---|
| 19 | self.maxofunc = 0 |
---|
| 20 | self.last = 0 |
---|
| 21 | if self.params.localmin: |
---|
| 22 | self.ovalist = [0., 0., 0., 0., 0.] |
---|
| 23 | |
---|
| 24 | def __call__(self): |
---|
| 25 | task.__call__(self) |
---|
| 26 | isonset,val = self.opick.do(self.myvec) |
---|
| 27 | if (aubio_silence_detection(self.myvec(),self.params.silence)): |
---|
| 28 | isonset=0 |
---|
| 29 | if self.params.storefunc: |
---|
| 30 | self.ofunc.append(val) |
---|
| 31 | if self.params.localmin: |
---|
| 32 | if val > 0: self.ovalist.append(val) |
---|
| 33 | else: self.ovalist.append(0) |
---|
| 34 | self.ovalist.pop(0) |
---|
[a9e27e4] | 35 | if (isonset > 0.): |
---|
[13c3fba] | 36 | if self.params.localmin: |
---|
| 37 | # find local minima before peak |
---|
| 38 | i=len(self.ovalist)-1 |
---|
| 39 | while self.ovalist[i-1] < self.ovalist[i] and i > 0: |
---|
| 40 | i -= 1 |
---|
| 41 | now = (self.frameread+1-i) |
---|
| 42 | else: |
---|
| 43 | now = self.frameread |
---|
| 44 | # take back delay |
---|
| 45 | if self.params.delay != 0.: now -= self.params.delay |
---|
| 46 | if now < 0 : |
---|
| 47 | now = 0 |
---|
| 48 | if self.params.mintol: |
---|
| 49 | # prune doubled |
---|
| 50 | if (now - self.last) > self.params.mintol: |
---|
| 51 | self.last = now |
---|
| 52 | return now, val |
---|
| 53 | else: |
---|
| 54 | return now, val |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | def fprint(self,foo): |
---|
| 58 | print self.params.step*foo[0] |
---|
| 59 | |
---|
| 60 | def eval(self,inputdata,ftru,mode='roc',vmode=''): |
---|
[43938de] | 61 | from aubio.txtfile import read_datafile |
---|
| 62 | from aubio.onsetcompare import onset_roc, onset_diffs, onset_rocloc |
---|
[13c3fba] | 63 | ltru = read_datafile(ftru,depth=0) |
---|
| 64 | lres = [] |
---|
| 65 | for i in range(len(inputdata)): lres.append(inputdata[i][0]*self.params.step) |
---|
| 66 | if vmode=='verbose': |
---|
| 67 | print "Running with mode %s" % self.params.onsetmode, |
---|
| 68 | print " and threshold %f" % self.params.threshold, |
---|
| 69 | print " on file", self.input |
---|
| 70 | #print ltru; print lres |
---|
| 71 | if mode == 'local': |
---|
| 72 | l = onset_diffs(ltru,lres,self.params.tol) |
---|
| 73 | mean = 0 |
---|
| 74 | for i in l: mean += i |
---|
| 75 | if len(l): mean = "%.3f" % (mean/len(l)) |
---|
| 76 | else: mean = "?0" |
---|
| 77 | return l, mean |
---|
| 78 | elif mode == 'roc': |
---|
| 79 | self.orig, self.missed, self.merged, \ |
---|
| 80 | self.expc, self.bad, self.doubled = \ |
---|
| 81 | onset_roc(ltru,lres,self.params.tol) |
---|
| 82 | elif mode == 'rocloc': |
---|
| 83 | self.v = {} |
---|
| 84 | self.v['orig'], self.v['missed'], self.v['Tm'], \ |
---|
| 85 | self.v['expc'], self.v['bad'], self.v['Td'], \ |
---|
| 86 | self.v['l'], self.v['labs'] = \ |
---|
| 87 | onset_rocloc(ltru,lres,self.params.tol) |
---|
| 88 | |
---|
| 89 | def plot(self,onsets,ofunc,wplot,oplots,nplot=False): |
---|
| 90 | import Gnuplot, Gnuplot.funcutils |
---|
| 91 | import aubio.txtfile |
---|
| 92 | import os.path |
---|
[5f23f66] | 93 | from numpy import arange, array, ones |
---|
[13c3fba] | 94 | from aubio.onsetcompare import onset_roc |
---|
| 95 | |
---|
| 96 | x1,y1,y1p = [],[],[] |
---|
| 97 | oplot = [] |
---|
| 98 | if self.params.onsetmode in ('mkl','kl'): ofunc[0:10] = [0] * 10 |
---|
| 99 | |
---|
| 100 | self.lenofunc = len(ofunc) |
---|
| 101 | self.maxofunc = max(ofunc) |
---|
| 102 | # onset detection function |
---|
[fcacd88] | 103 | downtime = arange(len(ofunc))*self.params.step |
---|
[5f23f66] | 104 | oplot.append(Gnuplot.Data(downtime,ofunc,with_='lines',title=self.params.onsetmode)) |
---|
[13c3fba] | 105 | |
---|
| 106 | # detected onsets |
---|
| 107 | if not nplot: |
---|
| 108 | for i in onsets: |
---|
| 109 | x1.append(i[0]*self.params.step) |
---|
| 110 | y1.append(self.maxofunc) |
---|
| 111 | y1p.append(-self.maxofunc) |
---|
[fcacd88] | 112 | #x1 = array(onsets)*self.params.step |
---|
| 113 | #y1 = self.maxofunc*ones(len(onsets)) |
---|
[13c3fba] | 114 | if x1: |
---|
[5f23f66] | 115 | oplot.append(Gnuplot.Data(x1,y1,with_='impulses')) |
---|
| 116 | wplot.append(Gnuplot.Data(x1,y1p,with_='impulses')) |
---|
[13c3fba] | 117 | |
---|
[45fcbb8] | 118 | oplots.append((oplot,self.params.onsetmode,self.maxofunc)) |
---|
[13c3fba] | 119 | |
---|
| 120 | # check if ground truth datafile exists |
---|
| 121 | datafile = self.input.replace('.wav','.txt') |
---|
| 122 | if datafile == self.input: datafile = "" |
---|
| 123 | if not os.path.isfile(datafile): |
---|
| 124 | self.title = "" #"(no ground truth)" |
---|
| 125 | else: |
---|
| 126 | t_onsets = aubio.txtfile.read_datafile(datafile) |
---|
[fcacd88] | 127 | x2 = array(t_onsets).resize(len(t_onsets)) |
---|
| 128 | y2 = self.maxofunc*ones(len(t_onsets)) |
---|
[5f23f66] | 129 | wplot.append(Gnuplot.Data(x2,y2,with_='impulses')) |
---|
[13c3fba] | 130 | |
---|
| 131 | tol = 0.050 |
---|
| 132 | |
---|
| 133 | orig, missed, merged, expc, bad, doubled = \ |
---|
| 134 | onset_roc(x2,x1,tol) |
---|
| 135 | self.title = "GD %2.3f%% FP %2.3f%%" % \ |
---|
| 136 | ((100*float(orig-missed-merged)/(orig)), |
---|
| 137 | (100*float(bad+doubled)/(orig))) |
---|
| 138 | |
---|
| 139 | |
---|
[9b138a8] | 140 | def plotplot(self,wplot,oplots,outplot=None,extension=None,xsize=1.,ysize=1.,spectro=False): |
---|
| 141 | from aubio.gnuplot import gnuplot_create, audio_to_array, make_audio_plot, audio_to_spec |
---|
[13c3fba] | 142 | import re |
---|
| 143 | # prepare the plot |
---|
[9b138a8] | 144 | g = gnuplot_create(outplot=outplot, extension=extension) |
---|
[470a772] | 145 | g('set title \'%s\'' % (re.sub('.*/','',self.input))) |
---|
[9b138a8] | 146 | if spectro: |
---|
| 147 | g('set size %f,%f' % (xsize,1.3*ysize) ) |
---|
| 148 | else: |
---|
| 149 | g('set size %f,%f' % (xsize,ysize) ) |
---|
[13c3fba] | 150 | g('set multiplot') |
---|
| 151 | |
---|
| 152 | # hack to align left axis |
---|
[45fcbb8] | 153 | g('set lmargin 3') |
---|
| 154 | g('set rmargin 6') |
---|
| 155 | |
---|
[9b138a8] | 156 | if spectro: |
---|
| 157 | import Gnuplot |
---|
| 158 | minf = 50 |
---|
| 159 | maxf = 500 |
---|
| 160 | data,time,freq = audio_to_spec(self.input,minf=minf,maxf=maxf) |
---|
| 161 | g('set size %f,%f' % (1.24*xsize , 0.34*ysize) ) |
---|
| 162 | g('set origin %f,%f' % (-0.12,0.65*ysize)) |
---|
| 163 | g('set xrange [0.:%f]' % time[-1]) |
---|
| 164 | g('set yrange [%f:%f]' % (minf,maxf)) |
---|
| 165 | g('set pm3d map') |
---|
| 166 | g('unset colorbox') |
---|
| 167 | g('set lmargin 0') |
---|
| 168 | g('set rmargin 0') |
---|
| 169 | g('set tmargin 0') |
---|
| 170 | g('set palette rgbformulae -25,-24,-32') |
---|
[470a772] | 171 | g.xlabel('time (s)',offset=(0,1.)) |
---|
[9b138a8] | 172 | g.ylabel('freq (Hz)') |
---|
[470a772] | 173 | g('set origin 0,%f' % (1.0*ysize) ) |
---|
| 174 | g('set format x "%1.1f"') |
---|
[9b138a8] | 175 | #if log: |
---|
| 176 | # g('set yrange [%f:%f]' % (max(10,minf),maxf)) |
---|
| 177 | # g('set log y') |
---|
| 178 | g.splot(Gnuplot.GridData(data,time,freq, binary=1, title='')) |
---|
| 179 | else: |
---|
| 180 | # plot waveform and onsets |
---|
[470a772] | 181 | time,data = audio_to_array(self.input) |
---|
| 182 | wplot = [make_audio_plot(time,data)] + wplot |
---|
[9b138a8] | 183 | g('set origin 0,%f' % (0.7*ysize) ) |
---|
[470a772] | 184 | g('set size %f,%f' % (xsize,0.3*ysize)) |
---|
[9b138a8] | 185 | g('set format y "%1f"') |
---|
[470a772] | 186 | g('set xrange [0:%f]' % max(time)) |
---|
| 187 | g('set yrange [-1:1]') |
---|
| 188 | g('set noytics') |
---|
| 189 | g('set y2tics -1,1') |
---|
| 190 | g.xlabel('time (s)',offset=(0,0.7)) |
---|
| 191 | g.ylabel('amplitude') |
---|
| 192 | g.plot(*wplot) |
---|
[13c3fba] | 193 | |
---|
[470a772] | 194 | # default settings for next plots |
---|
| 195 | g('unset title') |
---|
| 196 | g('set format x ""') |
---|
| 197 | g('set format y "%3e"') |
---|
| 198 | g('set tmargin 0') |
---|
| 199 | g.xlabel('') |
---|
[13c3fba] | 200 | |
---|
[470a772] | 201 | N = len(oplots) |
---|
| 202 | y = 0.7*ysize # the vertical proportion of the plot taken by onset functions |
---|
| 203 | delta = 0.035 # the constant part of y taken by last plot label and data |
---|
| 204 | for i in range(N): |
---|
| 205 | # plot onset detection functions |
---|
| 206 | g('set size %f,%f' % ( xsize, (y-delta)/N)) |
---|
| 207 | g('set origin 0,%f' % ((N-i-1)*(y-delta)/N + delta )) |
---|
| 208 | g('set nokey') |
---|
| 209 | g('set xrange [0:%f]' % (self.lenofunc*self.params.step)) |
---|
| 210 | g('set yrange [0:%f]' % (1.1*oplots[i][2])) |
---|
| 211 | g('set y2tics ("0" 0, "%d" %d)' % (round(oplots[i][2]),round(oplots[i][2]))) |
---|
| 212 | g.ylabel(oplots[i][1]) |
---|
| 213 | if i == N-1: |
---|
| 214 | g('set size %f,%f' % ( xsize, (y-delta)/N + delta ) ) |
---|
| 215 | g('set origin 0,0') |
---|
| 216 | g.xlabel('time (s)', offset=(0,0.7)) |
---|
| 217 | g('set format x') |
---|
| 218 | g.plot(*oplots[i][0]) |
---|
[13c3fba] | 219 | |
---|
[470a772] | 220 | g('unset multiplot') |
---|