1 | from aubioclass import * |
---|
2 | from bench.node import bench |
---|
3 | |
---|
4 | def get_onset_mode(nvalue): |
---|
5 | """ utility function to convert a string to aubio_onsetdetection_type """ |
---|
6 | if nvalue == 'complexdomain' or nvalue == 'complex' : |
---|
7 | return aubio_onset_complex |
---|
8 | elif nvalue == 'hfc' : |
---|
9 | return aubio_onset_hfc |
---|
10 | elif nvalue == 'phase' : |
---|
11 | return aubio_onset_phase |
---|
12 | elif nvalue == 'specdiff' : |
---|
13 | return aubio_onset_specdiff |
---|
14 | elif nvalue == 'energy' : |
---|
15 | return aubio_onset_energy |
---|
16 | elif nvalue == 'kl' : |
---|
17 | return aubio_onset_kl |
---|
18 | elif nvalue == 'mkl' : |
---|
19 | return aubio_onset_mkl |
---|
20 | elif nvalue == 'dual' : |
---|
21 | return 'dual' |
---|
22 | else: |
---|
23 | import sys |
---|
24 | print "unknown onset detection function selected" |
---|
25 | sys.exit(1) |
---|
26 | |
---|
27 | def get_pitch_mode(nvalue): |
---|
28 | """ utility function to convert a string to aubio_pitchdetection_type """ |
---|
29 | if nvalue == 'mcomb' : |
---|
30 | return aubio_pitch_mcomb |
---|
31 | elif nvalue == 'yin' : |
---|
32 | return aubio_pitch_yin |
---|
33 | elif nvalue == 'fcomb' : |
---|
34 | return aubio_pitch_fcomb |
---|
35 | elif nvalue == 'schmitt': |
---|
36 | return aubio_pitch_schmitt |
---|
37 | else: |
---|
38 | import sys |
---|
39 | print "error: unknown pitch detection function selected" |
---|
40 | sys.exit(1) |
---|
41 | |
---|
42 | def check_onset_mode(option, opt, value, parser): |
---|
43 | """ wrapper function to convert a list of modes to |
---|
44 | aubio_onsetdetection_type """ |
---|
45 | nvalues = parser.rargs[0].split(',') |
---|
46 | val = [] |
---|
47 | for nvalue in nvalues: |
---|
48 | val.append(get_onset_mode(nvalue)) |
---|
49 | setattr(parser.values, option.dest, val) |
---|
50 | |
---|
51 | def check_pitch_mode(option, opt, value, parser): |
---|
52 | """ utility function to convert a string to aubio_pitchdetection_type""" |
---|
53 | nvalues = parser.rargs[0].split(',') |
---|
54 | val = [] |
---|
55 | for nvalue in nvalues: |
---|
56 | val.append(get_pitch_mode(nvalue)) |
---|
57 | setattr(parser.values, option.dest, val) |
---|
58 | |
---|
59 | def check_pitchm_mode(option, opt, value, parser): |
---|
60 | """ utility function to convert a string to aubio_pitchdetection_mode """ |
---|
61 | nvalue = parser.rargs[0] |
---|
62 | if nvalue == 'freq' : |
---|
63 | setattr(parser.values, option.dest, aubio_pitchm_freq) |
---|
64 | elif nvalue == 'midi' : |
---|
65 | setattr(parser.values, option.dest, aubio_pitchm_midi) |
---|
66 | elif nvalue == 'cent' : |
---|
67 | setattr(parser.values, option.dest, aubio_pitchm_cent) |
---|
68 | elif nvalue == 'bin' : |
---|
69 | setattr(parser.values, option.dest, aubio_pitchm_bin) |
---|
70 | else: |
---|
71 | import sys |
---|
72 | print "error: unknown pitch detection output selected" |
---|
73 | sys.exit(1) |
---|
74 | |
---|
75 | |
---|
76 | def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512, |
---|
77 | mode='dual',localmin=False,storefunc=False,derivate=False): |
---|
78 | frameread = 0 |
---|
79 | filei = sndfile(filein) |
---|
80 | channels = filei.channels() |
---|
81 | myvec = fvec(hopsize,channels) |
---|
82 | readsize = filei.read(hopsize,myvec) |
---|
83 | opick = onsetpick(bufsize,hopsize,channels,myvec,threshold, |
---|
84 | mode=mode,derivate=derivate) |
---|
85 | mylist = list() |
---|
86 | if localmin: |
---|
87 | ovalist = [0., 0., 0., 0., 0.] |
---|
88 | ofunclist = [] |
---|
89 | while(readsize): |
---|
90 | readsize = filei.read(hopsize,myvec) |
---|
91 | isonset,val = opick.do(myvec) |
---|
92 | if (aubio_silence_detection(myvec(),silence)): |
---|
93 | isonset=0 |
---|
94 | if localmin: |
---|
95 | if val > 0: ovalist.append(val) |
---|
96 | else: ovalist.append(0) |
---|
97 | ovalist.pop(0) |
---|
98 | if storefunc: |
---|
99 | ofunclist.append(val) |
---|
100 | if (isonset == 1): |
---|
101 | if localmin: |
---|
102 | i=len(ovalist)-1 |
---|
103 | # find local minima before peak |
---|
104 | while ovalist[i-1] < ovalist[i] and i > 0: |
---|
105 | i -= 1 |
---|
106 | now = (frameread+1-i) |
---|
107 | else: |
---|
108 | now = frameread |
---|
109 | if now > 0 : |
---|
110 | mylist.append(now) |
---|
111 | else: |
---|
112 | now = 0 |
---|
113 | mylist.append(now) |
---|
114 | frameread += 1 |
---|
115 | return mylist, ofunclist |
---|
116 | |
---|
117 | def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512): |
---|
118 | frameread = 0 |
---|
119 | readsize = hopsize |
---|
120 | filei = sndfile(filein) |
---|
121 | framestep = hopsize/(filei.samplerate()+0.) |
---|
122 | channels = filei.channels() |
---|
123 | newname = "%s%s%09.5f%s%s" % (filein.split(".")[0].split("/")[-1],".", |
---|
124 | frameread*framestep,".",filein.split(".")[-1]) |
---|
125 | fileo = sndfile(newname,model=filei) |
---|
126 | myvec = fvec(hopsize,channels) |
---|
127 | mycopy = fvec(hopsize,channels) |
---|
128 | while(readsize==hopsize): |
---|
129 | readsize = filei.read(hopsize,myvec) |
---|
130 | # write to current file |
---|
131 | if len(slicetimes) and frameread >= slicetimes[0]: |
---|
132 | slicetimes.pop(0) |
---|
133 | # write up to 1st zero crossing |
---|
134 | zerocross = 0 |
---|
135 | while ( abs( myvec.get(zerocross,0) ) > zerothres ): |
---|
136 | zerocross += 1 |
---|
137 | writesize = fileo.write(zerocross,myvec) |
---|
138 | fromcross = 0 |
---|
139 | while (zerocross < readsize): |
---|
140 | for i in range(channels): |
---|
141 | mycopy.set(myvec.get(zerocross,i),fromcross,i) |
---|
142 | fromcross += 1 |
---|
143 | zerocross += 1 |
---|
144 | del fileo |
---|
145 | fileo = sndfile("%s%s%09.5f%s%s" % |
---|
146 | (filein.split(".")[0].split("/")[-1],".", |
---|
147 | frameread*framestep,".",filein.split(".")[-1]),model=filei) |
---|
148 | writesize = fileo.write(fromcross,mycopy) |
---|
149 | else: |
---|
150 | writesize = fileo.write(readsize,myvec) |
---|
151 | frameread += 1 |
---|
152 | del fileo |
---|
153 | |
---|
154 | |
---|
155 | def getsilences(filein,hopsize=512,silence=-70): |
---|
156 | frameread = 0 |
---|
157 | filei = sndfile(filein) |
---|
158 | srate = filei.samplerate() |
---|
159 | channels = filei.channels() |
---|
160 | myvec = fvec(hopsize,channels) |
---|
161 | readsize = filei.read(hopsize,myvec) |
---|
162 | mylist = [] |
---|
163 | wassilence = 0 |
---|
164 | while(readsize==hopsize): |
---|
165 | readsize = filei.read(hopsize,myvec) |
---|
166 | if (aubio_silence_detection(myvec(),silence)==1): |
---|
167 | if wassilence == 0: |
---|
168 | mylist.append(frameread) |
---|
169 | wassilence == 1 |
---|
170 | else: wassilence = 0 |
---|
171 | frameread += 1 |
---|
172 | return mylist |
---|
173 | |
---|
174 | |
---|
175 | def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq, |
---|
176 | samplerate=44100.,silence=-70): |
---|
177 | frameread = 0 |
---|
178 | filei = sndfile(filein) |
---|
179 | srate = filei.samplerate() |
---|
180 | channels = filei.channels() |
---|
181 | myvec = fvec(hopsize,channels) |
---|
182 | readsize = filei.read(hopsize,myvec) |
---|
183 | pitchdet = pitchdetection(mode=mode,bufsize=bufsize,hopsize=hopsize, |
---|
184 | channels=channels,samplerate=srate,omode=omode) |
---|
185 | mylist = [] |
---|
186 | while(readsize==hopsize): |
---|
187 | readsize = filei.read(hopsize,myvec) |
---|
188 | freq = pitchdet(myvec) |
---|
189 | #print "%.3f %.2f" % (now,freq) |
---|
190 | if (aubio_silence_detection(myvec(),silence)!=1): |
---|
191 | mylist.append(freq) |
---|
192 | else: |
---|
193 | mylist.append(-1.) |
---|
194 | frameread += 1 |
---|
195 | return mylist |
---|
196 | |
---|
197 | |
---|
198 | class taskparams: |
---|
199 | """ default parameters for task classes """ |
---|
200 | def __init__(self,input=None,output=None): |
---|
201 | self.silence = -70 |
---|
202 | self.derivate = False |
---|
203 | self.localmin = False |
---|
204 | self.bufsize = 512 |
---|
205 | self.hopsize = 256 |
---|
206 | self.samplerate = 44100 |
---|
207 | self.tol = 0.05 |
---|
208 | self.step = float(self.hopsize)/float(self.samplerate) |
---|
209 | self.threshold = 0.1 |
---|
210 | self.mode = 'yin' |
---|
211 | self.omode = aubio_pitchm_freq |
---|
212 | |
---|
213 | class task(taskparams): |
---|
214 | def __init__(self,input,output=None,params=None): |
---|
215 | """ open the input file and initialize default argument """ |
---|
216 | if params == None: self.params = taskparams() |
---|
217 | else: self.params = params |
---|
218 | self.input = input |
---|
219 | self.filei = sndfile(self.input) |
---|
220 | self.srate = self.filei.samplerate() |
---|
221 | self.channels = self.filei.channels() |
---|
222 | self.output = output |
---|
223 | def compute_step(self): |
---|
224 | pass |
---|
225 | def compute_all(self): |
---|
226 | """ Compute data """ |
---|
227 | mylist = [] |
---|
228 | while(self.readsize==self.params.hopsize): |
---|
229 | mylist.append(self()) |
---|
230 | return mylist |
---|
231 | |
---|
232 | def eval(self,results): |
---|
233 | """ Eval data """ |
---|
234 | pass |
---|
235 | |
---|
236 | def plot(self): |
---|
237 | """ Plot data """ |
---|
238 | pass |
---|
239 | |
---|
240 | class taskpitch(task): |
---|
241 | #def __init__(self,input,output): |
---|
242 | # pass |
---|
243 | # task.__init__(self,input) |
---|
244 | # #taskparams.__init__(self) |
---|
245 | def __init__(self,input,params=None): |
---|
246 | task.__init__(self,input,params=params) |
---|
247 | self.myvec = fvec(self.params.hopsize,self.channels) |
---|
248 | self.frameread = 0 |
---|
249 | self.readsize = self.params.hopsize |
---|
250 | self.pitchdet = pitchdetection(mode=get_pitch_mode(self.params.mode), |
---|
251 | bufsize=self.params.bufsize, |
---|
252 | hopsize=self.params.hopsize, |
---|
253 | channels=self.channels, |
---|
254 | samplerate=self.srate, |
---|
255 | omode=self.params.omode) |
---|
256 | |
---|
257 | def __call__(self): |
---|
258 | self.readsize = self.filei.read(self.params.hopsize,self.myvec) |
---|
259 | freq = self.pitchdet(self.myvec) |
---|
260 | #print "%.3f %.2f" % (now,freq) |
---|
261 | self.frameread += 1 |
---|
262 | if (aubio_silence_detection(self.myvec(),self.params.silence)!=1): |
---|
263 | return freq |
---|
264 | else: |
---|
265 | return -1. |
---|
266 | |
---|
267 | def gettruth(self): |
---|
268 | return float(self.input.split('.')[-2]) |
---|
269 | |
---|
270 | |
---|
271 | def eval(self,results): |
---|
272 | from median import short_find |
---|
273 | self.truth = self.gettruth() |
---|
274 | num = 0 |
---|
275 | sum = 0 |
---|
276 | res = [] |
---|
277 | for i in results: |
---|
278 | if i == -1: pass |
---|
279 | else: |
---|
280 | res.append(i) |
---|
281 | sum += i |
---|
282 | num += 1 |
---|
283 | avg = aubio_freqtomidi(sum / float(num)) |
---|
284 | avgdist = self.truth - avg |
---|
285 | med = aubio_freqtomidi(short_find(res,len(res)/2)) |
---|
286 | meddist = self.truth - med |
---|
287 | return avgdist, meddist |
---|
288 | |
---|
289 | def plot(self): |
---|
290 | from aubio.gnuplot import plot_pitch |
---|
291 | plot_pitch(self.input, |
---|
292 | pitch, |
---|
293 | samplerate=samplerate, |
---|
294 | hopsize=self.params.hopsize, |
---|
295 | outplot=options.outplot) |
---|
296 | |
---|
297 | |
---|
298 | |
---|