[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): |
---|
[c7713b2] | 27 | return self.get_norm(pos,chan) |
---|
| 28 | def set(self,val,pos,chan): |
---|
| 29 | self.set_norm(val,chan,pos) |
---|
| 30 | def get_norm(self,pos,chan): |
---|
[dcc8d90] | 31 | return cvec_read_norm(self(),chan,pos) |
---|
[c7713b2] | 32 | def set_norm(self,val,pos,chan): |
---|
| 33 | cvec_write_norm(self(),val,chan,pos) |
---|
| 34 | def get_phas(self,pos,chan): |
---|
| 35 | return cvec_read_phas(self(),chan,pos) |
---|
| 36 | def set_phas(self,val,pos,chan): |
---|
| 37 | cvec_write_phas(self(),val,chan,pos) |
---|
[96fb8ad] | 38 | |
---|
| 39 | class sndfile: |
---|
| 40 | def __init__(self,filename,model=None): |
---|
| 41 | if (model!=None): |
---|
[5e9c68a] | 42 | self.file = new_aubio_sndfile_wo(model.file,filename) |
---|
[96fb8ad] | 43 | else: |
---|
[5e9c68a] | 44 | self.file = new_aubio_sndfile_ro(filename) |
---|
[634d238] | 45 | if self.file == None: |
---|
[b07ff1e] | 46 | raise IOError, "failed opening file %s" % filename |
---|
[96fb8ad] | 47 | def __del__(self): |
---|
[cea30b8] | 48 | if self.file != None: del_aubio_sndfile(self.file) |
---|
[96fb8ad] | 49 | def info(self): |
---|
[5e9c68a] | 50 | aubio_sndfile_info(self.file) |
---|
[96fb8ad] | 51 | def samplerate(self): |
---|
[5e9c68a] | 52 | return aubio_sndfile_samplerate(self.file) |
---|
[96fb8ad] | 53 | def channels(self): |
---|
[5e9c68a] | 54 | return aubio_sndfile_channels(self.file) |
---|
[96fb8ad] | 55 | def read(self,nfram,vecread): |
---|
[5e9c68a] | 56 | return aubio_sndfile_read(self.file,nfram,vecread()) |
---|
[96fb8ad] | 57 | def write(self,nfram,vecwrite): |
---|
[5e9c68a] | 58 | return aubio_sndfile_write(self.file,nfram,vecwrite()) |
---|
[96fb8ad] | 59 | |
---|
| 60 | class pvoc: |
---|
| 61 | def __init__(self,buf,hop,chan): |
---|
| 62 | self.pv = new_aubio_pvoc(buf,hop,chan) |
---|
| 63 | def __del__(self): |
---|
| 64 | del_aubio_pvoc(self.pv) |
---|
| 65 | def do(self,tf,tc): |
---|
| 66 | aubio_pvoc_do(self.pv,tf(),tc()) |
---|
| 67 | def rdo(self,tc,tf): |
---|
| 68 | aubio_pvoc_rdo(self.pv,tc(),tf()) |
---|
| 69 | |
---|
| 70 | class onsetdetection: |
---|
[31907fd] | 71 | """ class for aubio_specdesc """ |
---|
[b4f5967] | 72 | def __init__(self,mode,buf,chan): |
---|
[31907fd] | 73 | self.od = new_aubio_specdesc(mode,buf,chan) |
---|
[96fb8ad] | 74 | def do(self,tc,tf): |
---|
[31907fd] | 75 | aubio_specdesc_do(self.od,tc(),tf()) |
---|
[96fb8ad] | 76 | def __del__(self): |
---|
[31907fd] | 77 | del_aubio_specdesc(self.od) |
---|
[96fb8ad] | 78 | |
---|
| 79 | class peakpick: |
---|
[71f98f1] | 80 | """ class for aubio_peakpicker """ |
---|
[96fb8ad] | 81 | def __init__(self,threshold=0.1): |
---|
[9f07d52] | 82 | self.pp = new_aubio_peakpicker(1) |
---|
| 83 | self.out = new_fvec(1, 1) |
---|
| 84 | aubio_peakpicker_set_threshold (self.pp, threshold) |
---|
[96fb8ad] | 85 | def do(self,fv): |
---|
[9f07d52] | 86 | aubio_peakpicker_do(self.pp, fv(), self.out) |
---|
| 87 | return fvec_read_sample(self.out, 0, 0) |
---|
[27f2c08] | 88 | def getval(self): |
---|
[b07ff1e] | 89 | return aubio_peakpicker_get_adaptive_threshold(self.pp) |
---|
[96fb8ad] | 90 | def __del__(self): |
---|
| 91 | del_aubio_peakpicker(self.pp) |
---|
| 92 | |
---|
| 93 | class onsetpick: |
---|
[31907fd] | 94 | """ superclass for aubio_pvoc + aubio_specdesc + aubio_peakpicker """ |
---|
[897b455] | 95 | def __init__(self,bufsize,hopsize,channels,myvec,threshold,mode='dual',derivate=False,dcthreshold=0): |
---|
[96fb8ad] | 96 | self.myfft = cvec(bufsize,channels) |
---|
| 97 | self.pv = pvoc(bufsize,hopsize,channels) |
---|
[b31f262] | 98 | if mode in ['dual'] : |
---|
[b4f5967] | 99 | self.myod = onsetdetection("hfc",bufsize,channels) |
---|
| 100 | self.myod2 = onsetdetection("mkl",bufsize,channels) |
---|
[19b56b0] | 101 | self.myonset = fvec(1,channels) |
---|
| 102 | self.myonset2 = fvec(1,channels) |
---|
[b31f262] | 103 | else: |
---|
| 104 | self.myod = onsetdetection(mode,bufsize,channels) |
---|
| 105 | self.myonset = fvec(1,channels) |
---|
[19b56b0] | 106 | self.mode = mode |
---|
[96fb8ad] | 107 | self.pp = peakpick(float(threshold)) |
---|
[80c0417] | 108 | self.derivate = derivate |
---|
[cadf07b] | 109 | self.dcthreshold = dcthreshold |
---|
[80c0417] | 110 | self.oldval = 0. |
---|
[96fb8ad] | 111 | |
---|
| 112 | def do(self,myvec): |
---|
| 113 | self.pv.do(myvec,self.myfft) |
---|
| 114 | self.myod.do(self.myfft,self.myonset) |
---|
[19b56b0] | 115 | if self.mode == 'dual': |
---|
[cadf07b] | 116 | self.myod2.do(self.myfft,self.myonset2) |
---|
| 117 | self.myonset.set(self.myonset.get(0,0)*self.myonset2.get(0,0),0,0) |
---|
[80c0417] | 118 | if self.derivate: |
---|
[cadf07b] | 119 | val = self.myonset.get(0,0) |
---|
| 120 | dval = val - self.oldval |
---|
| 121 | self.oldval = val |
---|
| 122 | if dval > 0: self.myonset.set(dval,0,0) |
---|
| 123 | else: self.myonset.set(0.,0,0) |
---|
[08f6688] | 124 | isonset, dval = self.pp.do(self.myonset),self.myonset.get(0,0) |
---|
[cadf07b] | 125 | if self.dcthreshold: |
---|
[08f6688] | 126 | if dval < self.dcthreshold: isonset = 0 |
---|
| 127 | return isonset, dval |
---|
[96fb8ad] | 128 | |
---|
[ca1abdd] | 129 | class pitch: |
---|
[fe163ad] | 130 | def __init__(self,mode="mcomb",bufsize=2048,hopsize=1024, |
---|
| 131 | channels=1,samplerate=44100.,omode="freq",tolerance=0.1): |
---|
[ca1abdd] | 132 | self.pitchp = new_aubio_pitch(mode,bufsize,hopsize,channels, |
---|
[fe163ad] | 133 | samplerate) |
---|
[3027c6e] | 134 | self.mypitch = fvec(1, channels) |
---|
[ca1abdd] | 135 | aubio_pitch_set_unit(self.pitchp,omode) |
---|
| 136 | aubio_pitch_set_tolerance(self.pitchp,tolerance) |
---|
[d53e4df] | 137 | #self.filt = filter(srate,"adsgn") |
---|
| 138 | def __del__(self): |
---|
[ca1abdd] | 139 | del_aubio_pitch(self.pitchp) |
---|
[d53e4df] | 140 | def __call__(self,myvec): |
---|
[ca1abdd] | 141 | aubio_pitch_do(self.pitchp,myvec(), self.mypitch()) |
---|
[3027c6e] | 142 | return self.mypitch.get(0,0) |
---|
[96fb8ad] | 143 | |
---|
| 144 | class filter: |
---|
| 145 | def __init__(self,srate,type=None): |
---|
| 146 | if (type=="adsgn"): |
---|
| 147 | self.filter = new_aubio_adsgn_filter(srate) |
---|
| 148 | def __del__(self): |
---|
| 149 | #del_aubio_filter(self.filter) |
---|
| 150 | pass |
---|
[d53e4df] | 151 | def __call__(self,myvec): |
---|
[96fb8ad] | 152 | aubio_filter_do(self.filter,myvec()) |
---|
[27f2c08] | 153 | |
---|
| 154 | class beattracking: |
---|
| 155 | """ class for aubio_beattracking """ |
---|
| 156 | def __init__(self,winlen,channels): |
---|
| 157 | self.p = new_aubio_beattracking(winlen,channels) |
---|
| 158 | def do(self,dfframe,out): |
---|
| 159 | return aubio_beattracking_do(self.p,dfframe(),out()) |
---|
| 160 | def __del__(self): |
---|
| 161 | del_aubio_beattracking(self.p) |
---|
| 162 | |
---|