[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 | |
---|
| 142 | def plotplot(self,wplot,oplots,outplot=None): |
---|
| 143 | from aubio.gnuplot import gnuplot_init, audio_to_array, make_audio_plot |
---|
| 144 | import re |
---|
| 145 | # audio data |
---|
| 146 | time,data = audio_to_array(self.input) |
---|
| 147 | wplot = [make_audio_plot(time,data)] + wplot |
---|
| 148 | # prepare the plot |
---|
| 149 | g = gnuplot_init(outplot) |
---|
| 150 | |
---|
| 151 | g('set multiplot') |
---|
| 152 | |
---|
| 153 | # hack to align left axis |
---|
[45fcbb8] | 154 | g('set lmargin 3') |
---|
| 155 | g('set rmargin 6') |
---|
[13c3fba] | 156 | g('set tmargin 0') |
---|
| 157 | g('set format x ""') |
---|
[45fcbb8] | 158 | g('set format y "%3e"') |
---|
[13c3fba] | 159 | g('set noytics') |
---|
| 160 | |
---|
| 161 | for i in range(len(oplots)): |
---|
| 162 | # plot onset detection functions |
---|
| 163 | g('set size 1,%f' % (0.7/(len(oplots)))) |
---|
[45fcbb8] | 164 | g('set origin 0,%f' % ((len(oplots)-float(i)-1)*0.7/(len(oplots)))) |
---|
[13c3fba] | 165 | g('set xrange [0:%f]' % (self.lenofunc*self.params.step)) |
---|
[45fcbb8] | 166 | g('set nokey') |
---|
| 167 | g('set yrange [0:%f]' % (1.1*oplots[i][2])) |
---|
| 168 | g('set y2tics ("0" 0, "%d" %d)' % (round(oplots[i][2]),round(oplots[i][2]))) |
---|
| 169 | g.ylabel(oplots[i][1]) |
---|
| 170 | if i == len(oplots)-1: |
---|
| 171 | g.xlabel('time (s)',offset=(0,0.7)) |
---|
| 172 | g.plot(*oplots[i][0]) |
---|
| 173 | |
---|
| 174 | g('set tmargin 2') |
---|
| 175 | g.xlabel('time (s)',offset=(0,0.7)) |
---|
[13c3fba] | 176 | g('set format x "%1.1f"') |
---|
[45fcbb8] | 177 | g('set format y "%1f"') |
---|
| 178 | g('set y2tics -1,1') |
---|
| 179 | |
---|
[13c3fba] | 180 | |
---|
| 181 | g('set title \'%s %s\'' % (re.sub('.*/','',self.input),self.title)) |
---|
| 182 | |
---|
| 183 | # plot waveform and onsets |
---|
| 184 | g('set size 1,0.3') |
---|
| 185 | g('set origin 0,0.7') |
---|
| 186 | g('set xrange [0:%f]' % max(time)) |
---|
| 187 | g('set yrange [-1:1]') |
---|
| 188 | g.ylabel('amplitude') |
---|
| 189 | g.plot(*wplot) |
---|
| 190 | |
---|
| 191 | g('unset multiplot') |
---|
| 192 | |
---|
| 193 | |
---|