[96fb8ad] | 1 | from aubiowrapper import * |
---|
| 2 | |
---|
| 3 | class 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 | |
---|
| 19 | class 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 | |
---|
| 27 | class 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 | |
---|
| 46 | class 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 | |
---|
| 56 | class 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 | |
---|
| 65 | class 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 | |
---|
| 74 | class onsetpick: |
---|
[71f98f1] | 75 | """ superclass for aubio_pvoc + aubio_onsetdetection + aubio_peakpicker """ |
---|
[19b56b0] | 76 | def __init__(self,bufsize,hopsize,channels,myvec,threshold,mode='dual'): |
---|
[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)) |
---|
| 89 | |
---|
| 90 | def do(self,myvec): |
---|
| 91 | self.pv.do(myvec,self.myfft) |
---|
| 92 | self.myod.do(self.myfft,self.myonset) |
---|
[19b56b0] | 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) |
---|
[96fb8ad] | 96 | return self.pp.do(self.myonset),self.myonset.get(0,0) |
---|
| 97 | |
---|
[19b56b0] | 98 | def getonsetsfunc(filein,threshold,silence,bufsize=1024,hopsize=512,mode='dual'): |
---|
| 99 | #bufsize = 1024 |
---|
| 100 | #hopsize = bufsize/2 |
---|
| 101 | frameread = 0 |
---|
| 102 | filei = sndfile(filein) |
---|
| 103 | channels = filei.channels() |
---|
| 104 | myvec = fvec(hopsize,channels) |
---|
| 105 | readsize = filei.read(hopsize,myvec) |
---|
| 106 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode) |
---|
| 107 | mylist = list() |
---|
| 108 | #ovalist = [0., 0., 0., 0., 0., 0.] |
---|
| 109 | ovalist = [0., 0., 0., 0., 0.] |
---|
| 110 | ofunclist = [] |
---|
| 111 | while(readsize): |
---|
| 112 | readsize = filei.read(hopsize,myvec) |
---|
| 113 | isonset,val = opick.do(myvec) |
---|
| 114 | if (aubio_silence_detection(myvec(),silence)): |
---|
| 115 | isonset=0 |
---|
| 116 | ovalist.append(val) |
---|
| 117 | ovalist.pop(0) |
---|
| 118 | ofunclist.append(val) |
---|
| 119 | if (isonset == 1): |
---|
| 120 | i=len(ovalist)-1 |
---|
| 121 | # find local minima before peak |
---|
| 122 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
| 123 | i -= 1 |
---|
| 124 | now = (frameread+1-i) |
---|
| 125 | if now > 0 : |
---|
| 126 | mylist.append(now) |
---|
| 127 | else: |
---|
| 128 | now = 0 |
---|
| 129 | mylist.append(now) |
---|
| 130 | frameread += 1 |
---|
| 131 | return mylist, ofunclist |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | def getonsetscausal(filein,threshold,silence,bufsize=1024,hopsize=512,mode='dual'): |
---|
| 135 | frameread = 0 |
---|
| 136 | filei = sndfile(filein) |
---|
| 137 | channels = filei.channels() |
---|
| 138 | myvec = fvec(hopsize,channels) |
---|
| 139 | readsize = filei.read(hopsize,myvec) |
---|
| 140 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode) |
---|
| 141 | mylist = list() |
---|
| 142 | while(readsize): |
---|
| 143 | readsize = filei.read(hopsize,myvec) |
---|
| 144 | isonset,val = opick.do(myvec) |
---|
| 145 | if (aubio_silence_detection(myvec(),silence)): |
---|
| 146 | isonset=0 |
---|
| 147 | if (isonset == 1): |
---|
| 148 | now = frameread |
---|
| 149 | if now > 0 : |
---|
| 150 | mylist.append(now) |
---|
| 151 | else: |
---|
| 152 | now = 0 |
---|
| 153 | mylist.append(now) |
---|
| 154 | frameread += 1 |
---|
| 155 | return mylist |
---|
| 156 | |
---|
| 157 | def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,mode='dual'): |
---|
| 158 | frameread = 0 |
---|
| 159 | filei = sndfile(filein) |
---|
| 160 | channels = filei.channels() |
---|
| 161 | samplerate= filei.samplerate() |
---|
| 162 | myvec = fvec(hopsize,channels) |
---|
| 163 | readsize = filei.read(hopsize,myvec) |
---|
| 164 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode) |
---|
| 165 | mylist = list() |
---|
| 166 | #ovalist = [0., 0., 0., 0., 0., 0.] |
---|
| 167 | ovalist = [0., 0., 0., 0., 0.] |
---|
| 168 | while(readsize): |
---|
| 169 | readsize = filei.read(hopsize,myvec) |
---|
| 170 | isonset,val = opick.do(myvec) |
---|
| 171 | if (aubio_silence_detection(myvec(),silence)): |
---|
| 172 | isonset=0 |
---|
| 173 | ovalist.append(val) |
---|
| 174 | ovalist.pop(0) |
---|
| 175 | if (isonset == 1): |
---|
| 176 | i=len(ovalist)-1 |
---|
| 177 | # find local minima before peak |
---|
| 178 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
| 179 | i -= 1 |
---|
| 180 | now = (frameread+1-i) |
---|
| 181 | if now > 0 : |
---|
| 182 | mylist.append(now) |
---|
| 183 | else: |
---|
| 184 | now = 0 |
---|
| 185 | mylist.append(now) |
---|
| 186 | frameread += 1 |
---|
| 187 | return mylist |
---|
| 188 | |
---|
[96fb8ad] | 189 | class pitchpick: |
---|
| 190 | def __init__(self,bufsize,hopsize,channels,myvec,srate): |
---|
| 191 | self.myfft = cvec(bufsize,channels) |
---|
| 192 | self.pv = pvoc(bufsize,hopsize,channels) |
---|
| 193 | self.pitchp = new_aubio_pitchmcomb(bufsize,channels) |
---|
| 194 | self.filt = filter(srate,"adsgn") |
---|
| 195 | |
---|
| 196 | def do(self,myvec): |
---|
| 197 | #self.filt.do(myvec) |
---|
| 198 | #self.filt.do(myvec) |
---|
| 199 | self.pv.do(myvec,self.myfft) |
---|
| 200 | return aubio_pitchmcomb_detect(self.pitchp,self.myfft()) |
---|
| 201 | |
---|
| 202 | class filter: |
---|
| 203 | def __init__(self,srate,type=None): |
---|
| 204 | if (type=="adsgn"): |
---|
| 205 | self.filter = new_aubio_adsgn_filter(srate) |
---|
| 206 | def __del__(self): |
---|
| 207 | #del_aubio_filter(self.filter) |
---|
| 208 | pass |
---|
| 209 | def do(self,myvec): |
---|
| 210 | aubio_filter_do(self.filter,myvec()) |
---|