source: python/aubio/aubioclass.py @ 8d84c0b

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

added cvec.get(pos,chan), added silence param to getpitch()

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