1 | from aubio.aubioclass import * |
---|
2 | from onset import taskonset |
---|
3 | |
---|
4 | class taskbeat(taskonset): |
---|
5 | def __init__(self,input,params=None,output=None): |
---|
6 | """ open the input file and initialize arguments |
---|
7 | parameters should be set *before* calling this method. |
---|
8 | """ |
---|
9 | taskonset.__init__(self,input,output=None,params=params) |
---|
10 | self.btwinlen = 512**2/self.params.hopsize |
---|
11 | self.btstep = self.btwinlen/4 |
---|
12 | self.btoutput = fvec(self.btstep,self.channels) |
---|
13 | self.dfframe = fvec(self.btwinlen,self.channels) |
---|
14 | self.bt = beattracking(self.btwinlen,self.channels) |
---|
15 | self.pos2 = 0 |
---|
16 | self.old = -1000 |
---|
17 | |
---|
18 | def __call__(self): |
---|
19 | taskonset.__call__(self) |
---|
20 | #results = taskonset.__call__(self) |
---|
21 | # write to current file |
---|
22 | if self.pos2 == self.btstep - 1 : |
---|
23 | self.bt.do(self.dfframe,self.btoutput) |
---|
24 | for i in range (self.btwinlen - self.btstep): |
---|
25 | self.dfframe.set(self.dfframe.get(i+self.btstep,0),i,0) |
---|
26 | for i in range(self.btwinlen - self.btstep, self.btwinlen): |
---|
27 | self.dfframe.set(0,i,0) |
---|
28 | self.pos2 = -1; |
---|
29 | self.pos2 += 1 |
---|
30 | val = self.opick.pp.getval() |
---|
31 | #if not results: val = 0 |
---|
32 | #else: val = results[1] |
---|
33 | self.dfframe.set(val,self.btwinlen - self.btstep + self.pos2,0) |
---|
34 | i=0 |
---|
35 | for i in range(1,int( self.btoutput.get(0,0) ) ): |
---|
36 | if self.pos2 == self.btoutput.get(i,0) and \ |
---|
37 | aubio_silence_detection(self.myvec(), |
---|
38 | self.params.silence)!=1: |
---|
39 | now = self.frameread-0 |
---|
40 | period = (60 * self.params.samplerate) / ((now - self.old) * self.params.hopsize) |
---|
41 | self.old = now |
---|
42 | return now*self.params.step,period |
---|
43 | |
---|
44 | def eval(self,results,tol=0.20,tolcontext=0.25): |
---|
45 | obeats = self.gettruth() |
---|
46 | etime = [result[0] for result in results] |
---|
47 | otime = [obeat[0] for obeat in obeats] |
---|
48 | CML_tot, CML_max, CML_start, CML_end = 0,0,0,0 |
---|
49 | AML_tot, AML_max, AML_start, AML_end = 0,0,0,0 |
---|
50 | AMLd_tot, AMLd_max, AMLd_start, AMLd_end = 0,0,0,0 |
---|
51 | AMLh_tot, AMLh_max, AMLh_start, AMLh_end = 0,0,0,0 |
---|
52 | AMLo_tot, AMLo_max, AMLo_start, AMLo_end = 0,0,0,0 |
---|
53 | # results iteration |
---|
54 | j = 1 |
---|
55 | # for each annotation |
---|
56 | for i in range(2,len(otime)-2): |
---|
57 | if j+1 >= len(etime): break |
---|
58 | count = 0 |
---|
59 | # look for next matching beat |
---|
60 | while otime[i] > etime[j] - (otime[i] - otime[i+1])*tol: |
---|
61 | if count > 0: |
---|
62 | #print "spurious etime" |
---|
63 | if CML_end - CML_start > CML_max: |
---|
64 | CML_max = CML_end - CML_start |
---|
65 | CML_start, CML_end = j, j |
---|
66 | if AMLh_end - AMLh_start > AMLh_max: |
---|
67 | AMLh_max = AMLh_end - AMLh_start |
---|
68 | AMLh_start, AMLh_end = j, j |
---|
69 | if AMLd_end - AMLd_start > AMLd_max: |
---|
70 | AMLd_max = AMLd_end - AMLd_start |
---|
71 | AMLd_start, AMLd_end = j, j |
---|
72 | if AMLo_end - AMLo_start > AMLo_max: |
---|
73 | AMLo_max = AMLo_end - AMLo_start |
---|
74 | AMLo_start, AMLo_end = j, j |
---|
75 | j += 1 |
---|
76 | count += 1 |
---|
77 | if j+1 >= len(etime): break |
---|
78 | #print otime[i-1],etime[j-1]," ",otime[i],etime[j]," ",otime[i+1],etime[j+1] |
---|
79 | prevtempo = (otime[i] - otime[i-1]) |
---|
80 | nexttempo = (otime[i+1] - otime[i]) |
---|
81 | |
---|
82 | current0 = (etime[j] > otime[i] - prevtempo*tol) |
---|
83 | current1 = (etime[j] < otime[i] + prevtempo*tol) |
---|
84 | |
---|
85 | # check correct tempo |
---|
86 | prev0 = (etime[j-1] > otime[i-1] - prevtempo*tolcontext) |
---|
87 | prev1 = (etime[j-1] < otime[i-1] + prevtempo*tolcontext) |
---|
88 | next0 = (etime[j+1] > otime[i+1] - nexttempo*tolcontext) |
---|
89 | next1 = (etime[j+1] < otime[i+1] + nexttempo*tolcontext) |
---|
90 | |
---|
91 | # check for off beat |
---|
92 | prevoffb0 = (etime[j-1] > otime[i-1] - prevtempo/2 - prevtempo*tolcontext) |
---|
93 | prevoffb1 = (etime[j-1] < otime[i-1] - prevtempo/2 + prevtempo*tolcontext) |
---|
94 | nextoffb0 = (etime[j+1] > otime[i+1] - nexttempo/2 - nexttempo*tolcontext) |
---|
95 | nextoffb1 = (etime[j+1] < otime[i+1] - nexttempo/2 + nexttempo*tolcontext) |
---|
96 | |
---|
97 | # check half tempo |
---|
98 | prevhalf0 = (etime[j-1] > otime[i-1] + prevtempo - prevtempo/2*tolcontext) |
---|
99 | prevhalf1 = (etime[j-1] < otime[i-1] + prevtempo + prevtempo/2*tolcontext) |
---|
100 | nexthalf0 = (etime[j+1] > otime[i+1] - nexttempo - nexttempo/2*tolcontext) |
---|
101 | nexthalf1 = (etime[j+1] < otime[i+1] - nexttempo + nexttempo/2*tolcontext) |
---|
102 | |
---|
103 | # check double tempo |
---|
104 | prevdoub0 = (etime[j-1] > otime[i-1] - prevtempo - prevtempo*2*tolcontext) |
---|
105 | prevdoub1 = (etime[j-1] < otime[i-1] - prevtempo + prevtempo*2*tolcontext) |
---|
106 | nextdoub0 = (etime[j+1] > otime[i+1] + nexttempo - nexttempo*2*tolcontext) |
---|
107 | nextdoub1 = (etime[j+1] < otime[i+1] + nexttempo + nexttempo*2*tolcontext) |
---|
108 | |
---|
109 | if current0 and current1 and prev0 and prev1 and next0 and next1: |
---|
110 | #print "YES!" |
---|
111 | CML_end = j |
---|
112 | CML_tot += 1 |
---|
113 | else: |
---|
114 | if CML_end - CML_start > CML_max: |
---|
115 | CML_max = CML_end - CML_start |
---|
116 | CML_start, CML_end = j, j |
---|
117 | if current0 and current1 and prevhalf0 and prevhalf1 and nexthalf0 and nexthalf1: |
---|
118 | AMLh_end = j |
---|
119 | AMLh_tot += 1 |
---|
120 | else: |
---|
121 | if AMLh_end - AMLh_start > AMLh_max: |
---|
122 | AMLh_max = AMLh_end - AMLh_start |
---|
123 | AMLh_start, AMLh_end = j, j |
---|
124 | if current0 and current1 and prevdoub0 and prevdoub1 and nextdoub0 and nextdoub1: |
---|
125 | AMLd_end = j |
---|
126 | AMLd_tot += 1 |
---|
127 | else: |
---|
128 | if AMLd_end - AMLd_start > AMLd_max: |
---|
129 | AMLd_max = AMLd_end - AMLd_start |
---|
130 | AMLd_start, AMLd_end = j, j |
---|
131 | if current0 and current1 and prevoffb0 and prevoffb1 and nextoffb0 and nextoffb1: |
---|
132 | AMLo_end = j |
---|
133 | AMLo_tot += 1 |
---|
134 | else: |
---|
135 | if AMLo_end - AMLo_start > AMLo_max: |
---|
136 | AMLo_max = AMLo_end - AMLo_start |
---|
137 | AMLo_start, AMLo_end = j, j |
---|
138 | # look for next matching beat |
---|
139 | count = 0 |
---|
140 | while otime[i] > etime[j] - (otime[i] - otime[i+1])*tolcontext: |
---|
141 | j += 1 |
---|
142 | if count > 0: |
---|
143 | #print "spurious etime" |
---|
144 | start = j |
---|
145 | count += 1 |
---|
146 | total = float(len(otime)) |
---|
147 | CML_tot /= total |
---|
148 | AMLh_tot /= total |
---|
149 | AMLd_tot /= total |
---|
150 | AMLo_tot /= total |
---|
151 | CML_cont = CML_max/total |
---|
152 | AMLh_cont = AMLh_max/total |
---|
153 | AMLd_cont = AMLd_max/total |
---|
154 | AMLo_cont = AMLo_max/total |
---|
155 | return CML_cont, CML_tot, AMLh_cont, AMLh_tot, AMLd_cont, AMLd_tot, AMLo_cont, AMLo_tot |
---|
156 | |
---|
157 | # for i in allfreq: |
---|
158 | # freq.append(float(i) / 2. / N * samplerate ) |
---|
159 | # while freq[i]>freqs[j]: |
---|
160 | # j += 1 |
---|
161 | # a0 = weight[j-1] |
---|
162 | # a1 = weight[j] |
---|
163 | # f0 = freqs[j-1] |
---|
164 | # f1 = freqs[j] |
---|
165 | # if f0!=0: |
---|
166 | # iweight.append((a1-a0)/(f1-f0)*freq[i] + (a0 - (a1 - a0)/(f1/f0 -1.))) |
---|
167 | # else: |
---|
168 | # iweight.append((a1-a0)/(f1-f0)*freq[i] + a0) |
---|
169 | # while freq[i]>freqs[j]: |
---|
170 | # j += 1 |
---|
171 | |
---|
172 | |
---|
173 | def gettruth(self): |
---|
174 | import os.path |
---|
175 | from aubio.txtfile import read_datafile |
---|
176 | datafile = self.input.replace('.wav','.txt') |
---|
177 | if not os.path.isfile(datafile): |
---|
178 | print "no ground truth " |
---|
179 | return False,False |
---|
180 | else: |
---|
181 | values = read_datafile(datafile,depth=0) |
---|
182 | old = -1000 |
---|
183 | for i in range(len(values)): |
---|
184 | now = values[i] |
---|
185 | period = 60 / (now - old) |
---|
186 | old = now |
---|
187 | values[i] = [now,period] |
---|
188 | return values |
---|
189 | |
---|
190 | |
---|
191 | def plot(self,oplots,results): |
---|
192 | import Gnuplot |
---|
193 | oplots.append(Gnuplot.Data(results,with='linespoints',title="auto")) |
---|
194 | |
---|
195 | def plotplot(self,oplots,outplot=None): |
---|
196 | import Gnuplot |
---|
197 | from aubio.gnuplot import gnuplot_init, audio_to_array, make_audio_plot |
---|
198 | import re |
---|
199 | # audio data |
---|
200 | #time,data = audio_to_array(self.input) |
---|
201 | #f = make_audio_plot(time,data) |
---|
202 | |
---|
203 | g = gnuplot_init(outplot) |
---|
204 | oplots = [Gnuplot.Data(self.gettruth(),with='linespoints',title="orig")] + oplots |
---|
205 | g.plot(*oplots) |
---|