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 | def get(self,pos,chan): |
---|
27 | return fvec_read_sample(self(),chan,pos) |
---|
28 | |
---|
29 | class sndfile: |
---|
30 | def __init__(self,filename,model=None): |
---|
31 | if (model!=None): |
---|
32 | self.file = new_aubio_sndfile_wo(model.file,filename) |
---|
33 | else: |
---|
34 | self.file = new_aubio_sndfile_ro(filename) |
---|
35 | def __del__(self): |
---|
36 | del_aubio_sndfile(self.file) |
---|
37 | def info(self): |
---|
38 | aubio_sndfile_info(self.file) |
---|
39 | def samplerate(self): |
---|
40 | return aubio_sndfile_samplerate(self.file) |
---|
41 | def channels(self): |
---|
42 | return aubio_sndfile_channels(self.file) |
---|
43 | def read(self,nfram,vecread): |
---|
44 | return aubio_sndfile_read(self.file,nfram,vecread()) |
---|
45 | def write(self,nfram,vecwrite): |
---|
46 | return aubio_sndfile_write(self.file,nfram,vecwrite()) |
---|
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: |
---|
59 | """ class for aubio_onsetdetection """ |
---|
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: |
---|
68 | """ class for aubio_peakpicker """ |
---|
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: |
---|
77 | """ superclass for aubio_pvoc + aubio_onsetdetection + aubio_peakpicker """ |
---|
78 | def __init__(self,bufsize,hopsize,channels,myvec,threshold,mode='dual',derivate=False): |
---|
79 | self.myfft = cvec(bufsize,channels) |
---|
80 | self.pv = pvoc(bufsize,hopsize,channels) |
---|
81 | if mode in ['dual'] : |
---|
82 | self.myod = onsetdetection(aubio_onset_hfc,bufsize,channels) |
---|
83 | self.myod2 = onsetdetection(aubio_onset_complex,bufsize,channels) |
---|
84 | self.myonset = fvec(1,channels) |
---|
85 | self.myonset2 = fvec(1,channels) |
---|
86 | else: |
---|
87 | self.myod = onsetdetection(mode,bufsize,channels) |
---|
88 | self.myonset = fvec(1,channels) |
---|
89 | self.mode = mode |
---|
90 | self.pp = peakpick(float(threshold)) |
---|
91 | self.derivate = derivate |
---|
92 | self.oldval = 0. |
---|
93 | |
---|
94 | def do(self,myvec): |
---|
95 | self.pv.do(myvec,self.myfft) |
---|
96 | self.myod.do(self.myfft,self.myonset) |
---|
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) |
---|
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) |
---|
106 | return self.pp.do(self.myonset),self.myonset.get(0,0) |
---|
107 | |
---|
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: |
---|
127 | print "unknown onset detection function selected" |
---|
128 | sys.exit(1) |
---|
129 | |
---|
130 | def check_pitch_mode(option, opt, value, parser): |
---|
131 | nvalue = parser.rargs[0] |
---|
132 | if nvalue == 'mcomb' : |
---|
133 | setattr(parser.values, option.dest, aubio_pitch_mcomb) |
---|
134 | elif nvalue == 'yin' : |
---|
135 | setattr(parser.values, option.dest, aubio_pitch_yin) |
---|
136 | elif nvalue == 'fcomb' : |
---|
137 | setattr(parser.values, option.dest, aubio_pitch_fcomb) |
---|
138 | elif nvalue == 'schmitt': |
---|
139 | setattr(parser.values, option.dest, aubio_pitch_schmitt) |
---|
140 | else: |
---|
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" |
---|
156 | sys.exit(1) |
---|
157 | |
---|
158 | |
---|
159 | def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512, |
---|
160 | mode='dual',localmin=False,storefunc=False,derivate=False): |
---|
161 | frameread = 0 |
---|
162 | filei = sndfile(filein) |
---|
163 | channels = filei.channels() |
---|
164 | myvec = fvec(hopsize,channels) |
---|
165 | readsize = filei.read(hopsize,myvec) |
---|
166 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold, |
---|
167 | mode=mode,derivate=derivate) |
---|
168 | mylist = list() |
---|
169 | if localmin: |
---|
170 | ovalist = [0., 0., 0., 0., 0.] |
---|
171 | if storefunc: |
---|
172 | ofunclist = [] |
---|
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 |
---|
178 | if localmin: |
---|
179 | if val > 0: ovalist.append(val) |
---|
180 | else: ovalist.append(0) |
---|
181 | ovalist.pop(0) |
---|
182 | if storefunc: |
---|
183 | ofunclist.append(val) |
---|
184 | if (isonset == 1): |
---|
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) |
---|
191 | else: |
---|
192 | now = frameread |
---|
193 | if now > 0 : |
---|
194 | mylist.append(now) |
---|
195 | else: |
---|
196 | now = 0 |
---|
197 | mylist.append(now) |
---|
198 | frameread += 1 |
---|
199 | if storefunc: return mylist, ofunclist |
---|
200 | else: return mylist |
---|
201 | |
---|
202 | def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512): |
---|
203 | frameread = 0 |
---|
204 | readsize = hopsize |
---|
205 | filei = sndfile(filein) |
---|
206 | framestep = hopsize/(filei.samplerate()+0.) |
---|
207 | channels = filei.channels() |
---|
208 | newname = "%s%s%09.5f%s%s" % (filein.split(".")[0].split("/")[-1],".", |
---|
209 | frameread*framestep,".",filein.split(".")[-1]) |
---|
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 ): |
---|
221 | zerocross += 1 |
---|
222 | writesize = fileo.write(zerocross,myvec) |
---|
223 | fromcross = 0 |
---|
224 | while (zerocross < readsize): |
---|
225 | for i in range(channels): |
---|
226 | mycopy.set(myvec.get(zerocross,i),fromcross,i) |
---|
227 | fromcross += 1 |
---|
228 | zerocross += 1 |
---|
229 | del fileo |
---|
230 | fileo = sndfile("%s%s%09.5f%s%s" % |
---|
231 | (filein.split(".")[0].split("/")[-1],".", |
---|
232 | frameread*framestep,".",filein.split(".")[-1]),model=filei) |
---|
233 | writesize = fileo.write(fromcross,mycopy) |
---|
234 | else: |
---|
235 | writesize = fileo.write(readsize,myvec) |
---|
236 | frameread += 1 |
---|
237 | del fileo |
---|
238 | |
---|
239 | |
---|
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 |
---|
258 | |
---|
259 | def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq, |
---|
260 | samplerate=44100.,silence=-70): |
---|
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) |
---|
274 | if (aubio_silence_detection(myvec(),silence)!=1): |
---|
275 | mylist.append(freq) |
---|
276 | else: |
---|
277 | mylist.append(-1.) |
---|
278 | frameread += 1 |
---|
279 | return mylist |
---|
280 | |
---|
281 | class pitchdetection: |
---|
282 | def __init__(self,mode=aubio_pitch_mcomb,bufsize=2048,hopsize=1024, |
---|
283 | channels=1,samplerate=44100.,omode=aubio_pitchm_freq): |
---|
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()) |
---|
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 |
---|
300 | def __call__(self,myvec): |
---|
301 | aubio_filter_do(self.filter,myvec()) |
---|