source: python/aubio/aubioclass.py @ 54bf847

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

add Kullback Liebler onset detection function and its modified version

  • Property mode set to 100644
File size: 8.7 KB
RevLine 
[96fb8ad]1from aubiowrapper import *
2
3class fvec:
4    def __init__(self,size,chan):
5        self.vec = new_fvec(size,chan)
6    def __call__(self):
7        return self.vec
8    def __del__(self):
9        del_fvec(self())
10    def get(self,pos,chan):
11        return fvec_read_sample(self(),chan,pos)
12    def set(self,value,pos,chan):
13        return fvec_write_sample(self(),value,chan,pos)
14    def channel(self,chan):
15        return fvec_get_channel(self(),chan)
16    def data(self):
17        return fvec_get_data(self())
18
19class cvec:
20    def __init__(self,size,chan):
21        self.vec = new_cvec(size,chan)
22    def __call__(self):
23        return self.vec
24    def __del__(self):
25        del_cvec(self())
26
27class sndfile:
28    def __init__(self,filename,model=None):
29        if (model!=None):
30            self.file = new_file_wo(model.file,filename)
31        else:
32            self.file = new_file_ro(filename)
33    def __del__(self):
34        del_file(self.file)
35    def info(self):
36        file_info(self.file)
37    def samplerate(self):
38        return aubio_file_samplerate(self.file)
39    def channels(self):
40        return aubio_file_channels(self.file)
41    def read(self,nfram,vecread):
42        return file_read(self.file,nfram,vecread())
43    def write(self,nfram,vecwrite):
44        return file_write(self.file,nfram,vecwrite())
45
46class pvoc:
47    def __init__(self,buf,hop,chan):
48        self.pv = new_aubio_pvoc(buf,hop,chan)
49    def __del__(self):
50        del_aubio_pvoc(self.pv)
51    def do(self,tf,tc):
52        aubio_pvoc_do(self.pv,tf(),tc())
53    def rdo(self,tc,tf):
54        aubio_pvoc_rdo(self.pv,tc(),tf())
55
56class onsetdetection:
[71f98f1]57    """ class for aubio_onsetdetection """
[96fb8ad]58    def __init__(self,type,buf,chan):
59        self.od = new_aubio_onsetdetection(type,buf,chan)
60    def do(self,tc,tf):
61        aubio_onsetdetection(self.od,tc(),tf())
62    def __del__(self):
63        aubio_onsetdetection_free(self.od)
64
65class peakpick:
[71f98f1]66    """ class for aubio_peakpicker """
[96fb8ad]67    def __init__(self,threshold=0.1):
68        self.pp = new_aubio_peakpicker(threshold)
69    def do(self,fv):
70        return aubio_peakpick_pimrt(fv(),self.pp)
71    def __del__(self):
72        del_aubio_peakpicker(self.pp)
73
74class onsetpick:
[71f98f1]75    """ superclass for aubio_pvoc + aubio_onsetdetection + aubio_peakpicker """
[80c0417]76    def __init__(self,bufsize,hopsize,channels,myvec,threshold,mode='dual',derivate=False):
[96fb8ad]77        self.myfft    = cvec(bufsize,channels)
78        self.pv       = pvoc(bufsize,hopsize,channels)
[b31f262]79        if mode in ['dual'] :
[19b56b0]80                self.myod     = onsetdetection(hfc,bufsize,channels)
81                self.myod2    = onsetdetection(complexdomain,bufsize,channels)
82                self.myonset  = fvec(1,channels)
83                self.myonset2 = fvec(1,channels)
[b31f262]84        else: 
85                self.myod     = onsetdetection(mode,bufsize,channels)
86                self.myonset  = fvec(1,channels)
[19b56b0]87        self.mode     = mode
[96fb8ad]88        self.pp       = peakpick(float(threshold))
[80c0417]89        self.derivate = derivate
90        self.oldval   = 0.
[96fb8ad]91
92    def do(self,myvec): 
93        self.pv.do(myvec,self.myfft)
94        self.myod.do(self.myfft,self.myonset)
[19b56b0]95        if self.mode == 'dual':
96                self.myod2.do(self.myfft,self.myonset2)
97                self.myonset.set(self.myonset.get(0,0)*self.myonset2.get(0,0),0,0)
[80c0417]98        if self.derivate:
99                val         = self.myonset.get(0,0)
100                dval        = val - self.oldval
101                self.oldval = val
102                if dval > 0: self.myonset.set(dval,0,0)
103                else:  self.myonset.set(0.,0,0)
[96fb8ad]104        return self.pp.do(self.myonset),self.myonset.get(0,0)
105
[80c0417]106def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,
107                mode='dual',localmin=False,storefunc=False,derivate=False):
[19b56b0]108        frameread = 0
109        filei     = sndfile(filein)
110        channels  = filei.channels()
111        myvec     = fvec(hopsize,channels)
112        readsize  = filei.read(hopsize,myvec)
[80c0417]113        opick     = onsetpick(bufsize,hopsize,channels,myvec,threshold,
114                         mode=mode,derivate=derivate)
[19b56b0]115        mylist    = list()
[98df9f4]116        if localmin:
117                ovalist   = [0., 0., 0., 0., 0.]
118        if storefunc:
119                ofunclist = []
[19b56b0]120        while(readsize):
121                readsize = filei.read(hopsize,myvec)
122                isonset,val = opick.do(myvec)
123                if (aubio_silence_detection(myvec(),silence)):
124                        isonset=0
[98df9f4]125                if localmin:
[80c0417]126                        if val > 0: ovalist.append(val)
127                        else: ovalist.append(0)
[98df9f4]128                        ovalist.pop(0)
129                if storefunc:
130                        ofunclist.append(val)
[19b56b0]131                if (isonset == 1):
[98df9f4]132                        if localmin:
133                                i=len(ovalist)-1
134                                # find local minima before peak
135                                while ovalist[i-1] < ovalist[i] and i > 0:
136                                        i -= 1
137                                now = (frameread+1-i)
[19b56b0]138                        else:
[98df9f4]139                                now = frameread
[19b56b0]140                        if now > 0 :
141                                mylist.append(now)
142                        else:
143                                now = 0
144                                mylist.append(now)
145                frameread += 1
[98df9f4]146        if storefunc: return mylist, ofunclist
147        else: return mylist
148
[80c0417]149def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512):
[98df9f4]150    frameread = 0
151    readsize  = hopsize
152    filei     = sndfile(filein)
153    framestep = hopsize/(filei.samplerate()+0.)
154    channels  = filei.channels()
155    newname   = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:])
156    fileo     = sndfile(newname,model=filei)
157    myvec     = fvec(hopsize,channels)
158    mycopy    = fvec(hopsize,channels)
159    while(readsize==hopsize):
160        readsize = filei.read(hopsize,myvec)
161        # write to current file
162        if len(slicetimes) and frameread >= slicetimes[0]:
163            slicetimes.pop(0)
164            # write up to 1st zero crossing
165            zerocross = 0
166            while ( abs( myvec.get(zerocross,0) ) > zerothres ):
[80c0417]167                zerocross += 1
[98df9f4]168            writesize = fileo.write(zerocross,myvec)
169            fromcross = 0
170            while (zerocross < readsize):
[80c0417]171                for i in range(channels):
172                    mycopy.set(myvec.get(zerocross,i),fromcross,i)
173                    fromcross += 1
174                    zerocross += 1
[98df9f4]175            del fileo
176            fileo = sndfile("%s%s%f%s%s" % 
[80c0417]177                (filein.split(".")[0].split("/")[-1],".",
178                frameread*framestep,".",filein.split(".")[-1]),model=filei)
[98df9f4]179            writesize = fileo.write(fromcross,mycopy)
180        else:
181            writesize = fileo.write(readsize,myvec)
182        frameread += 1
183    del fileo
[19b56b0]184
185
[d53e4df]186def getsilences(filein,hopsize=512,silence=-70):
187    frameread = 0
188    filei     = sndfile(filein)
189    srate     = filei.samplerate()
190    channels  = filei.channels()
191    myvec     = fvec(hopsize,channels)
192    readsize  = filei.read(hopsize,myvec)
193    mylist    = []
194    wassilence = 0
195    while(readsize==hopsize):
196        readsize = filei.read(hopsize,myvec)
197        if (aubio_silence_detection(myvec(),silence)==1):
198            if wassilence == 0:
199                mylist.append(frameread)
200                wassilence == 1
201        else: wassilence = 0
202        frameread += 1
203    return mylist
[96fb8ad]204
[d53e4df]205def getpitch(filein,mode=aubio_mcomb,bufsize=1024,hopsize=512,omode=aubio_freq,
206        samplerate=44100.):
207    frameread = 0
208    filei     = sndfile(filein)
209    srate     = filei.samplerate()
210    channels  = filei.channels()
211    myvec     = fvec(hopsize,channels)
212    readsize  = filei.read(hopsize,myvec)
213    pitchdet  = pitchdetection(mode=mode,bufsize=bufsize,hopsize=hopsize,
214                         channels=channels,samplerate=srate,omode=omode)
215    mylist    = []
216    while(readsize==hopsize):
217        readsize = filei.read(hopsize,myvec)
218        freq = pitchdet(myvec)
219        #print "%.3f     %.2f" % (now,freq)
220        mylist.append(freq)
221        frameread += 1
222    return mylist
223
224class pitchdetection:
225    def __init__(self,mode=aubio_mcomb,bufsize=2048,hopsize=1024,
226        channels=1,samplerate=44100.,omode=aubio_freq):
227        self.pitchp = new_aubio_pitchdetection(bufsize,hopsize,channels,
228                samplerate,mode,omode)
229        #self.filt     = filter(srate,"adsgn")
230    def __del__(self):
231        del_aubio_pitchdetection(self.pitchp)
232    def __call__(self,myvec): 
233        #self.filt(myvec)
234        return aubio_pitchdetection(self.pitchp,myvec())
[96fb8ad]235
236class filter:
237    def __init__(self,srate,type=None):
238        if (type=="adsgn"):
239            self.filter = new_aubio_adsgn_filter(srate)
240    def __del__(self):
241        #del_aubio_filter(self.filter)
242        pass
[d53e4df]243    def __call__(self,myvec):
[96fb8ad]244        aubio_filter_do(self.filter,myvec())
Note: See TracBrowser for help on using the repository browser.