source: python/aubio/aubioclass.py @ 98df9f4

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

cleaned up getonsets, merged aubioonset and aubiocut, added some options

  • Property mode set to 100644
File size: 7.0 KB
Line 
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:
57    """ class for aubio_onsetdetection """
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:
66    """ class for aubio_peakpicker """
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:
75    """ superclass for aubio_pvoc + aubio_onsetdetection + aubio_peakpicker """
76    def __init__(self,bufsize,hopsize,channels,myvec,threshold,mode='dual'):
77        self.myfft    = cvec(bufsize,channels)
78        self.pv       = pvoc(bufsize,hopsize,channels)
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
88        self.pp       = peakpick(float(threshold))
89
90    def do(self,myvec): 
91        self.pv.do(myvec,self.myfft)
92        self.myod.do(self.myfft,self.myonset)
93        if self.mode == 'dual':
94                self.myod2.do(self.myfft,self.myonset2)
95                self.myonset.set(self.myonset.get(0,0)*self.myonset2.get(0,0),0,0)
96        return self.pp.do(self.myonset),self.myonset.get(0,0)
97
98def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,mode='dual',localmin=False,storefunc=False):
99        frameread = 0
100        filei     = sndfile(filein)
101        channels  = filei.channels()
102        myvec     = fvec(hopsize,channels)
103        readsize  = filei.read(hopsize,myvec)
104        opick     = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode)
105        mylist    = list()
106        if localmin:
107                ovalist   = [0., 0., 0., 0., 0.]
108        if storefunc:
109                ofunclist = []
110        while(readsize):
111                readsize = filei.read(hopsize,myvec)
112                isonset,val = opick.do(myvec)
113                if (aubio_silence_detection(myvec(),silence)):
114                        isonset=0
115                if localmin:
116                        ovalist.append(val)
117                        ovalist.pop(0)
118                if storefunc:
119                        ofunclist.append(val)
120                if (isonset == 1):
121                        if localmin:
122                                i=len(ovalist)-1
123                                # find local minima before peak
124                                while ovalist[i-1] < ovalist[i] and i > 0:
125                                        i -= 1
126                                now = (frameread+1-i)
127                        else:
128                                now = frameread
129                        if now > 0 :
130                                mylist.append(now)
131                        else:
132                                now = 0
133                                mylist.append(now)
134                frameread += 1
135        if storefunc: return mylist, ofunclist
136        else: return mylist
137
138def cutfile(filein,slicetimes,zerothres=0.002,bufsize=1024,hopsize=512):
139    frameread = 0
140    readsize  = hopsize
141    filei     = sndfile(filein)
142    framestep = hopsize/(filei.samplerate()+0.)
143    channels  = filei.channels()
144    newname   = "%s%f%s" % ("/tmp/",0.0000000,filein[-4:])
145    fileo     = sndfile(newname,model=filei)
146    myvec     = fvec(hopsize,channels)
147    mycopy    = fvec(hopsize,channels)
148    while(readsize==hopsize):
149        readsize = filei.read(hopsize,myvec)
150        # write to current file
151        if len(slicetimes) and frameread >= slicetimes[0]:
152            slicetimes.pop(0)
153            # write up to 1st zero crossing
154            zerocross = 0
155            while ( abs( myvec.get(zerocross,0) ) > zerothres ):
156                zerocross += 1
157            writesize = fileo.write(zerocross,myvec)
158            fromcross = 0
159            while (zerocross < readsize):
160                for i in range(channels):
161                        mycopy.set(myvec.get(zerocross,i),fromcross,i)
162                fromcross += 1
163                zerocross += 1
164            del fileo
165            fileo = sndfile("%s%s%f%s%s" % 
166                (filein.split(".")[0].split("/")[-1],".",
167                frameread*framestep,".",filein.split(".")[-1]),model=filei)
168            writesize = fileo.write(fromcross,mycopy)
169        else:
170            writesize = fileo.write(readsize,myvec)
171        frameread += 1
172    del fileo
173
174
175class pitchpick:
176    def __init__(self,bufsize,hopsize,channels,myvec,srate):
177        self.myfft    = cvec(bufsize,channels)
178        self.pv       = pvoc(bufsize,hopsize,channels)
179        self.pitchp   = new_aubio_pitchmcomb(bufsize,channels)
180        self.filt     = filter(srate,"adsgn")
181
182    def do(self,myvec): 
183        #self.filt.do(myvec)
184        #self.filt.do(myvec)
185        self.pv.do(myvec,self.myfft)
186        return aubio_pitchmcomb_detect(self.pitchp,self.myfft())
187
188class filter:
189    def __init__(self,srate,type=None):
190        if (type=="adsgn"):
191            self.filter = new_aubio_adsgn_filter(srate)
192    def __del__(self):
193        #del_aubio_filter(self.filter)
194        pass
195    def do(self,myvec):
196        aubio_filter_do(self.filter,myvec())
Note: See TracBrowser for help on using the repository browser.