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