source: python.old/aubio/task/beat.py @ b554e41

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since b554e41 was b554e41, checked in by Paul Brossier <piem@piem.org>, 11 years ago

moved out old python interface

  • Property mode set to 100644
File size: 8.9 KB
Line 
1from aubio.aubioclass import *
2from onset import taskonset
3
4class 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,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        def eval2(self,results,tol=0.2):
173                truth = self.gettruth()
174                obeats = [i[0] for i in truth] 
175                ebeats = [i[0]*self.params.step for i in results] 
176                NP = max(len(obeats), len(ebeats))
177                N  = int(round(max(max(obeats), max(ebeats))*100.)+100)
178                W  = int(round(tol*100.*60./median([i[1] for i in truth], len(truth)/2)))
179                ofunc = [0 for i in range(N+W)]
180                efunc = [0 for i in range(N+W)]
181                for i in obeats: ofunc[int(round(i*100.)+W)] = 1
182                for i in ebeats: efunc[int(round(i*100.)+W)] = 1
183                assert len(obeats) == sum(ofunc)
184                autocor = 0; m =0
185                for m in range (-W, W):
186                        for i in range(W,N):
187                                autocor += ofunc[i] * efunc[i-m] 
188                autocor /= float(NP)
189                return autocor
190       
191        def evaluation(self,results,tol=0.2,start=5.):
192
193                """ beat tracking evaluation function
194
195                computes P-score of experimental results (ebeats)
196                        against ground truth annotations (obeats) """
197
198                from aubio.median import short_find as median
199                truth = self.gettruth()
200                ebeats = [i[0]*self.params.step for i in results] 
201                obeats = [i[0] for i in truth] 
202
203                # trim anything found before start
204                while obeats[0] < start: obeats.pop(0)
205                while ebeats[0] < start: ebeats.pop(0)
206                # maximum number of beats found
207                NP = max(len(obeats), len(ebeats))
208                # length of ofunc and efunc vector
209                N  = int(round(max(max(obeats), max(ebeats))*100.)+100)
210                # compute W median of ground truth tempi
211                tempi = []
212                for i in range(1,len(obeats)): tempi.append(obeats[i]-obeats[i-1])
213                W  = int(round(tol*100.*median(tempi,len(tempi)/2)))
214                # build ofunc and efunc functions, starting with W zeros 
215                ofunc = [0 for i in range(N+W)]
216                efunc = [0 for i in range(N+W)]
217                for i in obeats: ofunc[int(round(i*100.)+W)] = 1
218                for i in ebeats: efunc[int(round(i*100.)+W)] = 1
219                # optional: make sure we didn't miss any beats 
220                assert len(obeats) == sum(ofunc)
221                assert len(ebeats) == sum(efunc)
222                # compute auto correlation
223                autocor = 0; m =0
224                for m in range (-W, W):
225                  for i in range(W,N):
226                    autocor += ofunc[i] * efunc[i-m] 
227                autocor /= float(NP)
228                return autocor
229
230        def gettruth(self):
231                import os.path
232                from aubio.txtfile import read_datafile
233                datafile = self.input.replace('.wav','.txt')
234                if not os.path.isfile(datafile):
235                        print "no ground truth "
236                        return False,False
237                else:
238                        values = read_datafile(datafile,depth=0)
239                        old = -1000
240                        for i in range(len(values)):
241                                now = values[i]
242                                period = 60 / (now - old)
243                                old = now
244                                values[i] = [now,period]
245                return values
246       
247
248        def plot(self,oplots,results):
249                import Gnuplot
250                oplots.append(Gnuplot.Data(results,with_='linespoints',title="auto"))
251
252        def plotplot(self,wplot,oplots,outplot=None,extension=None,xsize=1.,ysize=1.,spectro=False):
253                import Gnuplot
254                from aubio.gnuplot import gnuplot_create, audio_to_array, make_audio_plot
255                import re
256                # audio data
257                #time,data = audio_to_array(self.input)
258                #f = make_audio_plot(time,data)
259
260                g = gnuplot_create(outplot=outplot, extension=extension)
261                oplots = [Gnuplot.Data(self.gettruth(),with_='linespoints',title="orig")] + oplots
262                g.plot(*oplots)
Note: See TracBrowser for help on using the repository browser.