[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()) |
---|
[b8976d6] | 26 | def get(self,pos,chan): |
---|
| 27 | return fvec_read_sample(self(),chan,pos) |
---|
[96fb8ad] | 28 | |
---|
| 29 | class sndfile: |
---|
| 30 | def __init__(self,filename,model=None): |
---|
| 31 | if (model!=None): |
---|
[5e9c68a] | 32 | self.file = new_aubio_sndfile_wo(model.file,filename) |
---|
[96fb8ad] | 33 | else: |
---|
[5e9c68a] | 34 | self.file = new_aubio_sndfile_ro(filename) |
---|
[96fb8ad] | 35 | def __del__(self): |
---|
[5e9c68a] | 36 | del_aubio_sndfile(self.file) |
---|
[96fb8ad] | 37 | def info(self): |
---|
[5e9c68a] | 38 | aubio_sndfile_info(self.file) |
---|
[96fb8ad] | 39 | def samplerate(self): |
---|
[5e9c68a] | 40 | return aubio_sndfile_samplerate(self.file) |
---|
[96fb8ad] | 41 | def channels(self): |
---|
[5e9c68a] | 42 | return aubio_sndfile_channels(self.file) |
---|
[96fb8ad] | 43 | def read(self,nfram,vecread): |
---|
[5e9c68a] | 44 | return aubio_sndfile_read(self.file,nfram,vecread()) |
---|
[96fb8ad] | 45 | def write(self,nfram,vecwrite): |
---|
[5e9c68a] | 46 | return aubio_sndfile_write(self.file,nfram,vecwrite()) |
---|
[96fb8ad] | 47 | |
---|
| 48 | class 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 | |
---|
| 58 | class 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 | |
---|
| 67 | class 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 | |
---|
| 76 | class 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'] : |
---|
[5cf415f] | 82 | self.myod = onsetdetection(aubio_onset_hfc,bufsize,channels) |
---|
| 83 | self.myod2 = onsetdetection(aubio_onset_complex,bufsize,channels) |
---|
[19b56b0] | 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 | |
---|
[5cf415f] | 108 | def check_onset_mode(option, opt, value, parser): |
---|
| 109 | nvalue = parser.rargs[0] |
---|
| 110 | if nvalue == 'complexdomain' or nvalue == 'complex' : |
---|
| 111 | setattr(parser.values, option.dest, aubio_onset_complex) |
---|
| 112 | elif nvalue == 'hfc' : |
---|
| 113 | setattr(parser.values, option.dest, aubio_onset_hfc) |
---|
| 114 | elif nvalue == 'phase' : |
---|
| 115 | setattr(parser.values, option.dest, aubio_onset_phase) |
---|
| 116 | elif nvalue == 'specdiff' : |
---|
| 117 | setattr(parser.values, option.dest, aubio_onset_specdiff) |
---|
| 118 | elif nvalue == 'energy' : |
---|
| 119 | setattr(parser.values, option.dest, aubio_onset_energy) |
---|
| 120 | elif nvalue == 'kl' : |
---|
| 121 | setattr(parser.values, option.dest, aubio_onset_kl) |
---|
| 122 | elif nvalue == 'mkl' : |
---|
| 123 | setattr(parser.values, option.dest, aubio_onset_mkl) |
---|
| 124 | elif nvalue == 'dual' : |
---|
| 125 | setattr(parser.values, option.dest, 'dual') |
---|
| 126 | else: |
---|
[aa17581] | 127 | print "unknown onset detection function selected" |
---|
[5cf415f] | 128 | sys.exit(1) |
---|
| 129 | |
---|
| 130 | def check_pitch_mode(option, opt, value, parser): |
---|
| 131 | nvalue = parser.rargs[0] |
---|
[aa17581] | 132 | if nvalue == 'mcomb' : |
---|
[5cf415f] | 133 | setattr(parser.values, option.dest, aubio_pitch_mcomb) |
---|
[aa17581] | 134 | elif nvalue == 'yin' : |
---|
[5cf415f] | 135 | setattr(parser.values, option.dest, aubio_pitch_yin) |
---|
[aa17581] | 136 | elif nvalue == 'fcomb' : |
---|
[5cf415f] | 137 | setattr(parser.values, option.dest, aubio_pitch_fcomb) |
---|
[aa17581] | 138 | elif nvalue == 'schmitt': |
---|
[5cf415f] | 139 | setattr(parser.values, option.dest, aubio_pitch_schmitt) |
---|
| 140 | else: |
---|
[aa17581] | 141 | print "error: unknown pitch detection function selected" |
---|
| 142 | sys.exit(1) |
---|
| 143 | |
---|
| 144 | def check_pitchm_mode(option, opt, value, parser): |
---|
| 145 | nvalue = parser.rargs[0] |
---|
| 146 | if nvalue == 'freq' : |
---|
| 147 | setattr(parser.values, option.dest, aubio_pitchm_freq) |
---|
| 148 | elif nvalue == 'midi' : |
---|
| 149 | setattr(parser.values, option.dest, aubio_pitchm_midi) |
---|
| 150 | elif nvalue == 'cent' : |
---|
| 151 | setattr(parser.values, option.dest, aubio_pitchm_cent) |
---|
| 152 | elif nvalue == 'bin' : |
---|
| 153 | setattr(parser.values, option.dest, aubio_pitchm_bin) |
---|
| 154 | else: |
---|
| 155 | print "error: unknown pitch detection output selected" |
---|
[5cf415f] | 156 | sys.exit(1) |
---|
| 157 | |
---|
| 158 | |
---|
[80c0417] | 159 | def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512, |
---|
| 160 | mode='dual',localmin=False,storefunc=False,derivate=False): |
---|
[19b56b0] | 161 | frameread = 0 |
---|
| 162 | filei = sndfile(filein) |
---|
| 163 | channels = filei.channels() |
---|
| 164 | myvec = fvec(hopsize,channels) |
---|
| 165 | readsize = filei.read(hopsize,myvec) |
---|
[80c0417] | 166 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold, |
---|
| 167 | mode=mode,derivate=derivate) |
---|
[19b56b0] | 168 | mylist = list() |
---|
[98df9f4] | 169 | if localmin: |
---|
| 170 | ovalist = [0., 0., 0., 0., 0.] |
---|
| 171 | if storefunc: |
---|
| 172 | ofunclist = [] |
---|
[19b56b0] | 173 | while(readsize): |
---|
| 174 | readsize = filei.read(hopsize,myvec) |
---|
| 175 | isonset,val = opick.do(myvec) |
---|
| 176 | if (aubio_silence_detection(myvec(),silence)): |
---|
| 177 | isonset=0 |
---|
[98df9f4] | 178 | if localmin: |
---|
[80c0417] | 179 | if val > 0: ovalist.append(val) |
---|
| 180 | else: ovalist.append(0) |
---|
[98df9f4] | 181 | ovalist.pop(0) |
---|
| 182 | if storefunc: |
---|
| 183 | ofunclist.append(val) |
---|
[19b56b0] | 184 | if (isonset == 1): |
---|
[98df9f4] | 185 | if localmin: |
---|
| 186 | i=len(ovalist)-1 |
---|
| 187 | # find local minima before peak |
---|
| 188 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
| 189 | i -= 1 |
---|
| 190 | now = (frameread+1-i) |
---|
[19b56b0] | 191 | else: |
---|
[98df9f4] | 192 | now = frameread |
---|
[19b56b0] | 193 | if now > 0 : |
---|
| 194 | mylist.append(now) |
---|
| 195 | else: |
---|
| 196 | now = 0 |
---|
| 197 | mylist.append(now) |
---|
| 198 | frameread += 1 |
---|
[98df9f4] | 199 | if storefunc: return mylist, ofunclist |
---|
| 200 | else: return mylist |
---|
| 201 | |
---|
[80c0417] | 202 | def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512): |
---|
[98df9f4] | 203 | frameread = 0 |
---|
| 204 | readsize = hopsize |
---|
| 205 | filei = sndfile(filein) |
---|
| 206 | framestep = hopsize/(filei.samplerate()+0.) |
---|
| 207 | channels = filei.channels() |
---|
[aa17581] | 208 | newname = "%s%s%09.5f%s%s" % (filein.split(".")[0].split("/")[-1],".", |
---|
[855ed0a] | 209 | frameread*framestep,".",filein.split(".")[-1]) |
---|
[98df9f4] | 210 | fileo = sndfile(newname,model=filei) |
---|
| 211 | myvec = fvec(hopsize,channels) |
---|
| 212 | mycopy = fvec(hopsize,channels) |
---|
| 213 | while(readsize==hopsize): |
---|
| 214 | readsize = filei.read(hopsize,myvec) |
---|
| 215 | # write to current file |
---|
| 216 | if len(slicetimes) and frameread >= slicetimes[0]: |
---|
| 217 | slicetimes.pop(0) |
---|
| 218 | # write up to 1st zero crossing |
---|
| 219 | zerocross = 0 |
---|
| 220 | while ( abs( myvec.get(zerocross,0) ) > zerothres ): |
---|
[80c0417] | 221 | zerocross += 1 |
---|
[98df9f4] | 222 | writesize = fileo.write(zerocross,myvec) |
---|
| 223 | fromcross = 0 |
---|
| 224 | while (zerocross < readsize): |
---|
[80c0417] | 225 | for i in range(channels): |
---|
| 226 | mycopy.set(myvec.get(zerocross,i),fromcross,i) |
---|
| 227 | fromcross += 1 |
---|
| 228 | zerocross += 1 |
---|
[98df9f4] | 229 | del fileo |
---|
[aa17581] | 230 | fileo = sndfile("%s%s%09.5f%s%s" % |
---|
[80c0417] | 231 | (filein.split(".")[0].split("/")[-1],".", |
---|
| 232 | frameread*framestep,".",filein.split(".")[-1]),model=filei) |
---|
[98df9f4] | 233 | writesize = fileo.write(fromcross,mycopy) |
---|
| 234 | else: |
---|
| 235 | writesize = fileo.write(readsize,myvec) |
---|
| 236 | frameread += 1 |
---|
| 237 | del fileo |
---|
[19b56b0] | 238 | |
---|
| 239 | |
---|
[d53e4df] | 240 | def getsilences(filein,hopsize=512,silence=-70): |
---|
| 241 | frameread = 0 |
---|
| 242 | filei = sndfile(filein) |
---|
| 243 | srate = filei.samplerate() |
---|
| 244 | channels = filei.channels() |
---|
| 245 | myvec = fvec(hopsize,channels) |
---|
| 246 | readsize = filei.read(hopsize,myvec) |
---|
| 247 | mylist = [] |
---|
| 248 | wassilence = 0 |
---|
| 249 | while(readsize==hopsize): |
---|
| 250 | readsize = filei.read(hopsize,myvec) |
---|
| 251 | if (aubio_silence_detection(myvec(),silence)==1): |
---|
| 252 | if wassilence == 0: |
---|
| 253 | mylist.append(frameread) |
---|
| 254 | wassilence == 1 |
---|
| 255 | else: wassilence = 0 |
---|
| 256 | frameread += 1 |
---|
| 257 | return mylist |
---|
[96fb8ad] | 258 | |
---|
[5e9c68a] | 259 | def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq, |
---|
[b8976d6] | 260 | samplerate=44100.,silence=-70): |
---|
[d53e4df] | 261 | frameread = 0 |
---|
| 262 | filei = sndfile(filein) |
---|
| 263 | srate = filei.samplerate() |
---|
| 264 | channels = filei.channels() |
---|
| 265 | myvec = fvec(hopsize,channels) |
---|
| 266 | readsize = filei.read(hopsize,myvec) |
---|
| 267 | pitchdet = pitchdetection(mode=mode,bufsize=bufsize,hopsize=hopsize, |
---|
| 268 | channels=channels,samplerate=srate,omode=omode) |
---|
| 269 | mylist = [] |
---|
| 270 | while(readsize==hopsize): |
---|
| 271 | readsize = filei.read(hopsize,myvec) |
---|
| 272 | freq = pitchdet(myvec) |
---|
| 273 | #print "%.3f %.2f" % (now,freq) |
---|
[b8976d6] | 274 | if (aubio_silence_detection(myvec(),silence)!=1): |
---|
| 275 | mylist.append(freq) |
---|
| 276 | else: |
---|
[aa17581] | 277 | mylist.append(-1.) |
---|
[d53e4df] | 278 | frameread += 1 |
---|
| 279 | return mylist |
---|
| 280 | |
---|
| 281 | class pitchdetection: |
---|
[5e9c68a] | 282 | def __init__(self,mode=aubio_pitch_mcomb,bufsize=2048,hopsize=1024, |
---|
| 283 | channels=1,samplerate=44100.,omode=aubio_pitchm_freq): |
---|
[d53e4df] | 284 | self.pitchp = new_aubio_pitchdetection(bufsize,hopsize,channels, |
---|
| 285 | samplerate,mode,omode) |
---|
| 286 | #self.filt = filter(srate,"adsgn") |
---|
| 287 | def __del__(self): |
---|
| 288 | del_aubio_pitchdetection(self.pitchp) |
---|
| 289 | def __call__(self,myvec): |
---|
| 290 | #self.filt(myvec) |
---|
| 291 | return aubio_pitchdetection(self.pitchp,myvec()) |
---|
[96fb8ad] | 292 | |
---|
| 293 | class filter: |
---|
| 294 | def __init__(self,srate,type=None): |
---|
| 295 | if (type=="adsgn"): |
---|
| 296 | self.filter = new_aubio_adsgn_filter(srate) |
---|
| 297 | def __del__(self): |
---|
| 298 | #del_aubio_filter(self.filter) |
---|
| 299 | pass |
---|
[d53e4df] | 300 | def __call__(self,myvec): |
---|
[96fb8ad] | 301 | aubio_filter_do(self.filter,myvec()) |
---|