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: |
---|
57 | def __init__(self,type,buf,chan): |
---|
58 | self.od = new_aubio_onsetdetection(type,buf,chan) |
---|
59 | def do(self,tc,tf): |
---|
60 | aubio_onsetdetection(self.od,tc(),tf()) |
---|
61 | def __del__(self): |
---|
62 | aubio_onsetdetection_free(self.od) |
---|
63 | |
---|
64 | class peakpick: |
---|
65 | def __init__(self,threshold=0.1): |
---|
66 | self.pp = new_aubio_peakpicker(threshold) |
---|
67 | def do(self,fv): |
---|
68 | return aubio_peakpick_pimrt(fv(),self.pp) |
---|
69 | def __del__(self): |
---|
70 | del_aubio_peakpicker(self.pp) |
---|
71 | |
---|
72 | class onsetpick: |
---|
73 | def __init__(self,bufsize,hopsize,channels,myvec,threshold,mode='dual'): |
---|
74 | self.myfft = cvec(bufsize,channels) |
---|
75 | self.pv = pvoc(bufsize,hopsize,channels) |
---|
76 | if mode in [complexdomain,hfc,phase,energy,specdiff] : |
---|
77 | self.myod = onsetdetection(mode,bufsize,channels) |
---|
78 | self.myonset = fvec(1,channels) |
---|
79 | else: |
---|
80 | self.myod = onsetdetection(hfc,bufsize,channels) |
---|
81 | self.myod2 = onsetdetection(complexdomain,bufsize,channels) |
---|
82 | self.myonset = fvec(1,channels) |
---|
83 | self.myonset2 = fvec(1,channels) |
---|
84 | self.mode = mode |
---|
85 | self.pp = peakpick(float(threshold)) |
---|
86 | |
---|
87 | def do(self,myvec): |
---|
88 | self.pv.do(myvec,self.myfft) |
---|
89 | self.myod.do(self.myfft,self.myonset) |
---|
90 | if self.mode == 'dual': |
---|
91 | self.myod2.do(self.myfft,self.myonset2) |
---|
92 | self.myonset.set(self.myonset.get(0,0)*self.myonset2.get(0,0),0,0) |
---|
93 | return self.pp.do(self.myonset),self.myonset.get(0,0) |
---|
94 | |
---|
95 | def getonsetsfunc(filein,threshold,silence,bufsize=1024,hopsize=512,mode='dual'): |
---|
96 | #bufsize = 1024 |
---|
97 | #hopsize = bufsize/2 |
---|
98 | frameread = 0 |
---|
99 | filei = sndfile(filein) |
---|
100 | channels = filei.channels() |
---|
101 | myvec = fvec(hopsize,channels) |
---|
102 | readsize = filei.read(hopsize,myvec) |
---|
103 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode) |
---|
104 | mylist = list() |
---|
105 | #ovalist = [0., 0., 0., 0., 0., 0.] |
---|
106 | ovalist = [0., 0., 0., 0., 0.] |
---|
107 | ofunclist = [] |
---|
108 | while(readsize): |
---|
109 | readsize = filei.read(hopsize,myvec) |
---|
110 | isonset,val = opick.do(myvec) |
---|
111 | if (aubio_silence_detection(myvec(),silence)): |
---|
112 | isonset=0 |
---|
113 | ovalist.append(val) |
---|
114 | ovalist.pop(0) |
---|
115 | ofunclist.append(val) |
---|
116 | if (isonset == 1): |
---|
117 | i=len(ovalist)-1 |
---|
118 | # find local minima before peak |
---|
119 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
120 | i -= 1 |
---|
121 | now = (frameread+1-i) |
---|
122 | if now > 0 : |
---|
123 | mylist.append(now) |
---|
124 | else: |
---|
125 | now = 0 |
---|
126 | mylist.append(now) |
---|
127 | frameread += 1 |
---|
128 | return mylist, ofunclist |
---|
129 | |
---|
130 | |
---|
131 | def getonsetscausal(filein,threshold,silence,bufsize=1024,hopsize=512,mode='dual'): |
---|
132 | frameread = 0 |
---|
133 | filei = sndfile(filein) |
---|
134 | channels = filei.channels() |
---|
135 | myvec = fvec(hopsize,channels) |
---|
136 | readsize = filei.read(hopsize,myvec) |
---|
137 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode) |
---|
138 | mylist = list() |
---|
139 | while(readsize): |
---|
140 | readsize = filei.read(hopsize,myvec) |
---|
141 | isonset,val = opick.do(myvec) |
---|
142 | if (aubio_silence_detection(myvec(),silence)): |
---|
143 | isonset=0 |
---|
144 | if (isonset == 1): |
---|
145 | now = frameread |
---|
146 | if now > 0 : |
---|
147 | mylist.append(now) |
---|
148 | else: |
---|
149 | now = 0 |
---|
150 | mylist.append(now) |
---|
151 | frameread += 1 |
---|
152 | return mylist |
---|
153 | |
---|
154 | |
---|
155 | def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,mode='dual'): |
---|
156 | frameread = 0 |
---|
157 | filei = sndfile(filein) |
---|
158 | channels = filei.channels() |
---|
159 | samplerate= filei.samplerate() |
---|
160 | myvec = fvec(hopsize,channels) |
---|
161 | readsize = filei.read(hopsize,myvec) |
---|
162 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode) |
---|
163 | mylist = list() |
---|
164 | #ovalist = [0., 0., 0., 0., 0., 0.] |
---|
165 | ovalist = [0., 0., 0., 0., 0.] |
---|
166 | while(readsize): |
---|
167 | readsize = filei.read(hopsize,myvec) |
---|
168 | isonset,val = opick.do(myvec) |
---|
169 | if (aubio_silence_detection(myvec(),silence)): |
---|
170 | isonset=0 |
---|
171 | ovalist.append(val) |
---|
172 | ovalist.pop(0) |
---|
173 | if (isonset == 1): |
---|
174 | i=len(ovalist)-1 |
---|
175 | # find local minima before peak |
---|
176 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
177 | i -= 1 |
---|
178 | now = (frameread+1-i) |
---|
179 | if now > 0 : |
---|
180 | mylist.append(now) |
---|
181 | else: |
---|
182 | now = 0 |
---|
183 | mylist.append(now) |
---|
184 | frameread += 1 |
---|
185 | return mylist |
---|
186 | |
---|
187 | |
---|
188 | class pitchpick: |
---|
189 | def __init__(self,bufsize,hopsize,channels,myvec,srate): |
---|
190 | self.myfft = cvec(bufsize,channels) |
---|
191 | self.pv = pvoc(bufsize,hopsize,channels) |
---|
192 | self.pitchp = new_aubio_pitchmcomb(bufsize,channels) |
---|
193 | self.filt = filter(srate,"adsgn") |
---|
194 | |
---|
195 | def do(self,myvec): |
---|
196 | #self.filt.do(myvec) |
---|
197 | #self.filt.do(myvec) |
---|
198 | self.pv.do(myvec,self.myfft) |
---|
199 | return aubio_pitchmcomb_detect(self.pitchp,self.myfft()) |
---|
200 | |
---|
201 | class filter: |
---|
202 | def __init__(self,srate,type=None): |
---|
203 | if (type=="adsgn"): |
---|
204 | self.filter = new_aubio_adsgn_filter(srate) |
---|
205 | def __del__(self): |
---|
206 | #del_aubio_filter(self.filter) |
---|
207 | pass |
---|
208 | def do(self,myvec): |
---|
209 | aubio_filter_do(self.filter,myvec()) |
---|