Showing the changes to python (2 of 2 files). Show all
Unified · Split
| @@ -1,4 +1,5 @@ | |||
| 1 | from aubioclass import * | 1 | from aubioclass import * |
| 2 | from bench.node import bench | ||
| 2 | 3 | ||
| 3 | def get_onset_mode(nvalue): | 4 | def get_onset_mode(nvalue): |
| 4 | """ utility function to convert a string to aubio_onsetdetection_type """ | 5 | """ utility function to convert a string to aubio_onsetdetection_type """ |
| @@ -23,6 +24,21 @@ def get_onset_mode(nvalue): · 18 unmodified lines | |||
| 23 | print "unknown onset detection function selected" | 24 | print "unknown onset detection function selected" |
| 24 | sys.exit(1) | 25 | sys.exit(1) |
| 25 | 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 | |||
| 26 | def check_onset_mode(option, opt, value, parser): | 42 | def check_onset_mode(option, opt, value, parser): |
| 27 | """ wrapper function to convert a list of modes to | 43 | """ wrapper function to convert a list of modes to |
| 28 | aubio_onsetdetection_type """ | 44 | aubio_onsetdetection_type """ |
| @@ -37,18 +53,7 @@ def check_pitch_mode(option, opt, value, parser): · 8 unmodified lines | |||
| 37 | nvalues = parser.rargs[0].split(',') | 53 | nvalues = parser.rargs[0].split(',') |
| 38 | val = [] | 54 | val = [] |
| 39 | for nvalue in nvalues: | 55 | for nvalue in nvalues: |
| 40 | if nvalue == 'mcomb' : | 56 | val.append(get_pitch_mode(nvalue)) |
| 41 | val.append(aubio_pitch_mcomb) | ||
| 42 | elif nvalue == 'yin' : | ||
| 43 | val.append(aubio_pitch_yin) | ||
| 44 | elif nvalue == 'fcomb' : | ||
| 45 | val.append(aubio_pitch_fcomb) | ||
| 46 | elif nvalue == 'schmitt': | ||
| 47 | val.append(aubio_pitch_schmitt) | ||
| 48 | else: | ||
| 49 | import sys | ||
| 50 | print "error: unknown pitch detection function selected" | ||
| 51 | sys.exit(1) | ||
| 52 | setattr(parser.values, option.dest, val) | 57 | setattr(parser.values, option.dest, val) |
| 53 | 58 | ||
| 54 | def check_pitchm_mode(option, opt, value, parser): | 59 | def check_pitchm_mode(option, opt, value, parser): |
| @@ -166,6 +171,7 @@ def getsilences(filein,hopsize=512,silence=-70): · 111 unmodified lines | |||
| 166 | frameread += 1 | 171 | frameread += 1 |
| 167 | return mylist | 172 | return mylist |
| 168 | 173 | ||
| 174 | |||
| 169 | def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq, | 175 | def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq, |
| 170 | samplerate=44100.,silence=-70): | 176 | samplerate=44100.,silence=-70): |
| 171 | frameread = 0 | 177 | frameread = 0 |
| @@ -188,3 +194,105 @@ def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_ · 16 unmodified lines | |||
| 188 | frameread += 1 | 194 | frameread += 1 |
| 189 | return mylist | 195 | return mylist |
| 190 | 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 | |||
| @@ -1,34 +1,56 @@ | |||
| 1 | #! /usr/bin/python | 1 | #! /usr/bin/python |
| 2 | 2 | ||
| 3 | #from conf.aubio_benchrc import * | ||
| 4 | from aubio.bench.config import * | ||
| 5 | from aubio.bench.node import * | 3 | from aubio.bench.node import * |
| 6 | import os | 4 | from aubio.tasks import * |
| 7 | 5 | ||
| 8 | datapath = "%s%s" % (DATADIR,'/pitch/isolated/piano/011pfnof') | 6 | class benchpitch(bench): |
| 9 | respath = '/var/tmp/isolated/testing' | 7 | |
| 10 | 8 | def compute_file(self,input,output): | |
| 11 | MODES = 'yin', 'mcomb', 'fcomb', 'schmitt' | 9 | filetask = self.task(input,params=self.params) |
| 12 | 10 | computed_data = filetask.compute_all() | |
| 13 | # prepareresultpath | 11 | results = filetask.eval(computed_data) |
| 14 | act_on_results(mkdir,datapath,respath,filter='d') | 12 | self.results.append(results) |
| 15 | 13 | truth = filetask.gettruth() | |
| 16 | def compute_data(input,output): | 14 | #print input, results, results - float(input.split('.')[-2]) |
| 17 | aubiocmd = "%s%s %s%s" % \ | 15 | self.pretty_print((self.params.mode, truth, |
| 18 | ("LD_LIBRARY_PATH=",LD_LIBRARY_PATH,AUBIOHOME,"/python/aubiopitch") | 16 | truth - results[0], results[0], |
| 19 | for m in MODES: | 17 | truth - results[1], results[1])) |
| 20 | cmd = "%s --input \"%s\" --mode %s --verbose --units midi > \"%s--%s.txt\"" \ | 18 | |
| 21 | % (aubiocmd,input,m,output,m) | 19 | def compute_data(self): |
| 22 | runcommand(cmd,debug=0) | 20 | self.orig, self.missed, self.merged, self.expc, \ |
| 23 | 21 | self.bad, self.doubled = 0, 0, 0, 0, 0, 0 | |
| 24 | 22 | act_on_data(self.compute_file,self.datadir, \ | |
| 25 | # computedata | 23 | suffix='',filter='f -name \'*.wav\'') |
| 26 | act_on_data(compute_data,datapath,respath,suffix='',filter='f -name \'*.wav\'') | 24 | |
| 27 | 25 | def compute_results(self,truth): | |
| 28 | # gatherdata | 26 | for i in self.results: print i |
| 29 | #act_on_data(my_print,datapath,respath,suffix='.txt',filter='f -name \'*.wav\'') | 27 | |
| 30 | # gatherthreshold | 28 | def run_bench(self,modes=['dual']): |
| 31 | # gathermodes | 29 | self.modes = modes |
| 32 | # comparediffs | 30 | self.pretty_print(self.titles) |
| 33 | # gatherdiffs | 31 | for mode in self.modes: |
| 34 | 32 | self.params.mode = mode | |
| 33 | self.compute_data() | ||
| 34 | #self.compute_results() | ||
| 35 | #self.pretty_print(self.results) | ||
| 36 | |||
| 37 | if __name__ == "__main__": | ||
| 38 | import sys | ||
| 39 | if len(sys.argv) > 1: datapath = sys.argv[1] | ||
| 40 | else: print "error: a path is required"; sys.exit(1) | ||
| 41 | |||
| 42 | modes = ['schmitt', 'yin', 'mcomb', 'fcomb'] | ||
| 43 | |||
| 44 | benchpitch = benchpitch(datapath) | ||
| 45 | benchpitch.params = taskparams() | ||
| 46 | benchpitch.task = taskpitch | ||
| 47 | |||
| 48 | benchpitch.titles = [ 'mode', 'thres', 'avg', 'avgdist' ] | ||
| 49 | benchpitch.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s" ] | ||
| 50 | try: | ||
| 51 | benchpitch.run_bench(modes=modes) | ||
| 52 | except KeyboardInterrupt: | ||
| 53 | print "Interrupted by user" | ||
| 54 | sys.exit(1) | ||
| 55 | |||
| 56 | sys.exit(0) | ||