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