source: python/aubio/aubioclass.py @ d09cad2

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

merged aubioplot-onsets into aubiocut, added some more options

  • Property mode set to 100644
File size: 7.5 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)
[19b56b0]79        if mode in [complexdomain,hfc,phase,energy,specdiff]  :
80                self.myod     = onsetdetection(mode,bufsize,channels)
81                self.myonset  = fvec(1,channels)
82        else: 
83                self.myod     = onsetdetection(hfc,bufsize,channels)
84                self.myod2    = onsetdetection(complexdomain,bufsize,channels)
85                self.myonset  = fvec(1,channels)
86                self.myonset2 = fvec(1,channels)
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
[96fb8ad]186class pitchpick:
187    def __init__(self,bufsize,hopsize,channels,myvec,srate):
188        self.myfft    = cvec(bufsize,channels)
189        self.pv       = pvoc(bufsize,hopsize,channels)
190        self.pitchp   = new_aubio_pitchmcomb(bufsize,channels)
191        self.filt     = filter(srate,"adsgn")
192
193    def do(self,myvec): 
194        #self.filt.do(myvec)
195        #self.filt.do(myvec)
196        self.pv.do(myvec,self.myfft)
197        return aubio_pitchmcomb_detect(self.pitchp,self.myfft())
198
199class filter:
200    def __init__(self,srate,type=None):
201        if (type=="adsgn"):
202            self.filter = new_aubio_adsgn_filter(srate)
203    def __del__(self):
204        #del_aubio_filter(self.filter)
205        pass
206    def do(self,myvec):
207        aubio_filter_do(self.filter,myvec())
Note: See TracBrowser for help on using the repository browser.