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 > 0.): |
---|
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=''): |
---|
63 | from aubio.txtfile import read_datafile |
---|
64 | from aubio.onsetcompare import onset_roc, onset_diffs, onset_rocloc |
---|
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 | from numpy import arange, array, ones |
---|
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 = 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 = array(onsets)*self.params.step |
---|
115 | #y1 = self.maxofunc*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 | |
---|
120 | oplots.append((oplot,self.params.onsetmode,self.maxofunc)) |
---|
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 = array(t_onsets).resize(len(t_onsets)) |
---|
130 | y2 = self.maxofunc*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,extension=None,xsize=1.,ysize=1.,spectro=False): |
---|
143 | from aubio.gnuplot import gnuplot_create, audio_to_array, make_audio_plot, audio_to_spec |
---|
144 | import re |
---|
145 | # prepare the plot |
---|
146 | g = gnuplot_create(outplot=outplot, extension=extension) |
---|
147 | g('set title \'%s\'' % (re.sub('.*/','',self.input))) |
---|
148 | if spectro: |
---|
149 | g('set size %f,%f' % (xsize,1.3*ysize) ) |
---|
150 | else: |
---|
151 | g('set size %f,%f' % (xsize,ysize) ) |
---|
152 | g('set multiplot') |
---|
153 | |
---|
154 | # hack to align left axis |
---|
155 | g('set lmargin 3') |
---|
156 | g('set rmargin 6') |
---|
157 | |
---|
158 | if spectro: |
---|
159 | import Gnuplot |
---|
160 | minf = 50 |
---|
161 | maxf = 500 |
---|
162 | data,time,freq = audio_to_spec(self.input,minf=minf,maxf=maxf) |
---|
163 | g('set size %f,%f' % (1.24*xsize , 0.34*ysize) ) |
---|
164 | g('set origin %f,%f' % (-0.12,0.65*ysize)) |
---|
165 | g('set xrange [0.:%f]' % time[-1]) |
---|
166 | g('set yrange [%f:%f]' % (minf,maxf)) |
---|
167 | g('set pm3d map') |
---|
168 | g('unset colorbox') |
---|
169 | g('set lmargin 0') |
---|
170 | g('set rmargin 0') |
---|
171 | g('set tmargin 0') |
---|
172 | g('set palette rgbformulae -25,-24,-32') |
---|
173 | g.xlabel('time (s)',offset=(0,1.)) |
---|
174 | g.ylabel('freq (Hz)') |
---|
175 | g('set origin 0,%f' % (1.0*ysize) ) |
---|
176 | g('set format x "%1.1f"') |
---|
177 | #if log: |
---|
178 | # g('set yrange [%f:%f]' % (max(10,minf),maxf)) |
---|
179 | # g('set log y') |
---|
180 | g.splot(Gnuplot.GridData(data,time,freq, binary=1, title='')) |
---|
181 | else: |
---|
182 | # plot waveform and onsets |
---|
183 | time,data = audio_to_array(self.input) |
---|
184 | wplot = [make_audio_plot(time,data)] + wplot |
---|
185 | g('set origin 0,%f' % (0.7*ysize) ) |
---|
186 | g('set size %f,%f' % (xsize,0.3*ysize)) |
---|
187 | g('set format y "%1f"') |
---|
188 | g('set xrange [0:%f]' % max(time)) |
---|
189 | g('set yrange [-1:1]') |
---|
190 | g('set noytics') |
---|
191 | g('set y2tics -1,1') |
---|
192 | g.xlabel('time (s)',offset=(0,0.7)) |
---|
193 | g.ylabel('amplitude') |
---|
194 | g.plot(*wplot) |
---|
195 | |
---|
196 | # default settings for next plots |
---|
197 | g('unset title') |
---|
198 | g('set format x ""') |
---|
199 | g('set format y "%3e"') |
---|
200 | g('set tmargin 0') |
---|
201 | g.xlabel('') |
---|
202 | |
---|
203 | N = len(oplots) |
---|
204 | y = 0.7*ysize # the vertical proportion of the plot taken by onset functions |
---|
205 | delta = 0.035 # the constant part of y taken by last plot label and data |
---|
206 | for i in range(N): |
---|
207 | # plot onset detection functions |
---|
208 | g('set size %f,%f' % ( xsize, (y-delta)/N)) |
---|
209 | g('set origin 0,%f' % ((N-i-1)*(y-delta)/N + delta )) |
---|
210 | g('set nokey') |
---|
211 | g('set xrange [0:%f]' % (self.lenofunc*self.params.step)) |
---|
212 | g('set yrange [0:%f]' % (1.1*oplots[i][2])) |
---|
213 | g('set y2tics ("0" 0, "%d" %d)' % (round(oplots[i][2]),round(oplots[i][2]))) |
---|
214 | g.ylabel(oplots[i][1]) |
---|
215 | if i == N-1: |
---|
216 | g('set size %f,%f' % ( xsize, (y-delta)/N + delta ) ) |
---|
217 | g('set origin 0,0') |
---|
218 | g.xlabel('time (s)', offset=(0,0.7)) |
---|
219 | g('set format x') |
---|
220 | g.plot(*oplots[i][0]) |
---|
221 | |
---|
222 | g('unset multiplot') |
---|