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 detection function selected\n", usage |
---|
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 "unknown detection function selected\n", usage |
---|
142 | sys.exit(1) |
---|
143 | |
---|
144 | |
---|
145 | def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512, |
---|
146 | mode='dual',localmin=False,storefunc=False,derivate=False): |
---|
147 | frameread = 0 |
---|
148 | filei = sndfile(filein) |
---|
149 | channels = filei.channels() |
---|
150 | myvec = fvec(hopsize,channels) |
---|
151 | readsize = filei.read(hopsize,myvec) |
---|
152 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold, |
---|
153 | mode=mode,derivate=derivate) |
---|
154 | mylist = list() |
---|
155 | if localmin: |
---|
156 | ovalist = [0., 0., 0., 0., 0.] |
---|
157 | if storefunc: |
---|
158 | ofunclist = [] |
---|
159 | while(readsize): |
---|
160 | readsize = filei.read(hopsize,myvec) |
---|
161 | isonset,val = opick.do(myvec) |
---|
162 | if (aubio_silence_detection(myvec(),silence)): |
---|
163 | isonset=0 |
---|
164 | if localmin: |
---|
165 | if val > 0: ovalist.append(val) |
---|
166 | else: ovalist.append(0) |
---|
167 | ovalist.pop(0) |
---|
168 | if storefunc: |
---|
169 | ofunclist.append(val) |
---|
170 | if (isonset == 1): |
---|
171 | if localmin: |
---|
172 | i=len(ovalist)-1 |
---|
173 | # find local minima before peak |
---|
174 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
175 | i -= 1 |
---|
176 | now = (frameread+1-i) |
---|
177 | else: |
---|
178 | now = frameread |
---|
179 | if now > 0 : |
---|
180 | mylist.append(now) |
---|
181 | else: |
---|
182 | now = 0 |
---|
183 | mylist.append(now) |
---|
184 | frameread += 1 |
---|
185 | if storefunc: return mylist, ofunclist |
---|
186 | else: return mylist |
---|
187 | |
---|
188 | def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512): |
---|
189 | frameread = 0 |
---|
190 | readsize = hopsize |
---|
191 | filei = sndfile(filein) |
---|
192 | framestep = hopsize/(filei.samplerate()+0.) |
---|
193 | channels = filei.channels() |
---|
194 | newname = "%s%s%f%s%s" % (filein.split(".")[0].split("/")[-1],".", |
---|
195 | frameread*framestep,".",filein.split(".")[-1]) |
---|
196 | fileo = sndfile(newname,model=filei) |
---|
197 | myvec = fvec(hopsize,channels) |
---|
198 | mycopy = fvec(hopsize,channels) |
---|
199 | while(readsize==hopsize): |
---|
200 | readsize = filei.read(hopsize,myvec) |
---|
201 | # write to current file |
---|
202 | if len(slicetimes) and frameread >= slicetimes[0]: |
---|
203 | slicetimes.pop(0) |
---|
204 | # write up to 1st zero crossing |
---|
205 | zerocross = 0 |
---|
206 | while ( abs( myvec.get(zerocross,0) ) > zerothres ): |
---|
207 | zerocross += 1 |
---|
208 | writesize = fileo.write(zerocross,myvec) |
---|
209 | fromcross = 0 |
---|
210 | while (zerocross < readsize): |
---|
211 | for i in range(channels): |
---|
212 | mycopy.set(myvec.get(zerocross,i),fromcross,i) |
---|
213 | fromcross += 1 |
---|
214 | zerocross += 1 |
---|
215 | del fileo |
---|
216 | fileo = sndfile("%s%s%f%s%s" % |
---|
217 | (filein.split(".")[0].split("/")[-1],".", |
---|
218 | frameread*framestep,".",filein.split(".")[-1]),model=filei) |
---|
219 | writesize = fileo.write(fromcross,mycopy) |
---|
220 | else: |
---|
221 | writesize = fileo.write(readsize,myvec) |
---|
222 | frameread += 1 |
---|
223 | del fileo |
---|
224 | |
---|
225 | |
---|
226 | def getsilences(filein,hopsize=512,silence=-70): |
---|
227 | frameread = 0 |
---|
228 | filei = sndfile(filein) |
---|
229 | srate = filei.samplerate() |
---|
230 | channels = filei.channels() |
---|
231 | myvec = fvec(hopsize,channels) |
---|
232 | readsize = filei.read(hopsize,myvec) |
---|
233 | mylist = [] |
---|
234 | wassilence = 0 |
---|
235 | while(readsize==hopsize): |
---|
236 | readsize = filei.read(hopsize,myvec) |
---|
237 | if (aubio_silence_detection(myvec(),silence)==1): |
---|
238 | if wassilence == 0: |
---|
239 | mylist.append(frameread) |
---|
240 | wassilence == 1 |
---|
241 | else: wassilence = 0 |
---|
242 | frameread += 1 |
---|
243 | return mylist |
---|
244 | |
---|
245 | def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq, |
---|
246 | samplerate=44100.,silence=-70): |
---|
247 | frameread = 0 |
---|
248 | filei = sndfile(filein) |
---|
249 | srate = filei.samplerate() |
---|
250 | channels = filei.channels() |
---|
251 | myvec = fvec(hopsize,channels) |
---|
252 | readsize = filei.read(hopsize,myvec) |
---|
253 | pitchdet = pitchdetection(mode=mode,bufsize=bufsize,hopsize=hopsize, |
---|
254 | channels=channels,samplerate=srate,omode=omode) |
---|
255 | mylist = [] |
---|
256 | while(readsize==hopsize): |
---|
257 | readsize = filei.read(hopsize,myvec) |
---|
258 | freq = pitchdet(myvec) |
---|
259 | #print "%.3f %.2f" % (now,freq) |
---|
260 | if (aubio_silence_detection(myvec(),silence)!=1): |
---|
261 | mylist.append(freq) |
---|
262 | else: |
---|
263 | mylist.append(0) |
---|
264 | frameread += 1 |
---|
265 | return mylist |
---|
266 | |
---|
267 | class pitchdetection: |
---|
268 | def __init__(self,mode=aubio_pitch_mcomb,bufsize=2048,hopsize=1024, |
---|
269 | channels=1,samplerate=44100.,omode=aubio_pitchm_freq): |
---|
270 | self.pitchp = new_aubio_pitchdetection(bufsize,hopsize,channels, |
---|
271 | samplerate,mode,omode) |
---|
272 | #self.filt = filter(srate,"adsgn") |
---|
273 | def __del__(self): |
---|
274 | del_aubio_pitchdetection(self.pitchp) |
---|
275 | def __call__(self,myvec): |
---|
276 | #self.filt(myvec) |
---|
277 | return aubio_pitchdetection(self.pitchp,myvec()) |
---|
278 | |
---|
279 | class filter: |
---|
280 | def __init__(self,srate,type=None): |
---|
281 | if (type=="adsgn"): |
---|
282 | self.filter = new_aubio_adsgn_filter(srate) |
---|
283 | def __del__(self): |
---|
284 | #del_aubio_filter(self.filter) |
---|
285 | pass |
---|
286 | def __call__(self,myvec): |
---|
287 | aubio_filter_do(self.filter,myvec()) |
---|