[4cc9fe5] | 1 | #! /usr/bin/python |
---|
| 2 | |
---|
| 3 | from aubio.bench.config import * |
---|
| 4 | from aubio.bench.node import * |
---|
| 5 | |
---|
[75139a9] | 6 | class onset_parameters: |
---|
| 7 | def __init__(self): |
---|
| 8 | """ set default parameters """ |
---|
| 9 | self.silence = -70 |
---|
| 10 | self.derivate = False |
---|
| 11 | self.localmin = False |
---|
| 12 | self.bufsize = 512 |
---|
| 13 | self.hopsize = 256 |
---|
| 14 | self.samplerate = 44100 |
---|
| 15 | self.tol = 0.05 |
---|
| 16 | self.step = float(self.hopsize)/float(self.samplerate) |
---|
| 17 | self.threshold = 0.1 |
---|
| 18 | self.mode = 'dual' |
---|
| 19 | |
---|
| 20 | class taskonset(task): |
---|
| 21 | |
---|
| 22 | def pretty_print(self,values): |
---|
| 23 | for i in range(len(values)): |
---|
| 24 | print self.formats[i] % values[i], |
---|
| 25 | print |
---|
| 26 | |
---|
| 27 | def compute_results(self): |
---|
| 28 | self.P = 100*float(self.expc-self.missed-self.merged)/(self.expc-self.missed-self.merged + self.bad+self.doubled) |
---|
| 29 | self.R = 100*float(self.expc-self.missed-self.merged)/(self.expc-self.missed-self.merged + self.missed+self.merged) |
---|
| 30 | if self.R < 0: self.R = 0 |
---|
| 31 | self.F = 2* self.P*self.R / (self.P+self.R) |
---|
| 32 | |
---|
| 33 | self.values = [self.params.mode, |
---|
| 34 | "%2.3f" % self.params.threshold, |
---|
| 35 | self.orig, |
---|
| 36 | self.expc, |
---|
| 37 | self.missed, |
---|
| 38 | self.merged, |
---|
| 39 | self.bad, |
---|
| 40 | self.doubled, |
---|
| 41 | (self.orig-self.missed-self.merged), |
---|
| 42 | "%2.3f" % (100*float(self.orig-self.missed-self.merged)/(self.orig)), |
---|
| 43 | "%2.3f" % (100*float(self.bad+self.doubled)/(self.orig)), |
---|
| 44 | "%2.3f" % (100*float(self.orig-self.missed)/(self.orig)), |
---|
| 45 | "%2.3f" % (100*float(self.bad)/(self.orig)), |
---|
| 46 | "%2.3f" % self.P, |
---|
| 47 | "%2.3f" % self.R, |
---|
| 48 | "%2.3f" % self.F ] |
---|
| 49 | |
---|
| 50 | def compute_onset(self,input,output): |
---|
| 51 | from aubio.tasks import getonsets, get_onset_mode |
---|
| 52 | from aubio.onsetcompare import onset_roc, onset_diffs |
---|
| 53 | from aubio.txtfile import read_datafile |
---|
| 54 | amode = 'roc' |
---|
| 55 | vmode = 'verbose' |
---|
| 56 | vmode = '' |
---|
| 57 | lres, ofunc = getonsets(input, |
---|
| 58 | self.params.threshold, |
---|
| 59 | self.params.silence, |
---|
| 60 | mode=get_onset_mode(self.params.mode), |
---|
| 61 | localmin=self.params.localmin, |
---|
| 62 | derivate=self.params.derivate, |
---|
| 63 | bufsize=self.params.bufsize, |
---|
| 64 | hopsize=self.params.hopsize, |
---|
| 65 | storefunc=False) |
---|
| 66 | |
---|
| 67 | for i in range(len(lres)): lres[i] = lres[i]*self.params.step |
---|
| 68 | ltru = read_datafile(input.replace('.wav','.txt'),depth=0) |
---|
| 69 | if vmode=='verbose': |
---|
| 70 | print "Running with mode %s" % self.params.mode, |
---|
| 71 | print " and threshold %f" % self.params.threshold, |
---|
| 72 | print " on file", input |
---|
| 73 | #print ltru; print lres |
---|
| 74 | if amode == 'localisation': |
---|
| 75 | l = onset_diffs(ltru,lres,self.params.tol) |
---|
| 76 | mean = 0 |
---|
| 77 | for i in l: mean += i |
---|
| 78 | if len(l): print "%.3f" % (mean/len(l)) |
---|
| 79 | else: print "?0" |
---|
| 80 | elif amode == 'roc': |
---|
| 81 | orig, missed, merged, expc, bad, doubled = onset_roc(ltru,lres,self.params.tol) |
---|
| 82 | self.orig += orig |
---|
| 83 | self.missed += missed |
---|
| 84 | self.merged += merged |
---|
| 85 | self.expc += expc |
---|
| 86 | self.bad += bad |
---|
| 87 | self.doubled += doubled |
---|
| 88 | self.compute_results() |
---|
| 89 | |
---|
| 90 | def compute_data(self): |
---|
| 91 | self.orig, self.missed, self.merged, self.expc, \ |
---|
| 92 | self.bad, self.doubled = 0, 0, 0, 0, 0, 0 |
---|
| 93 | act_on_data(self.compute_onset,self.datadir,self.resdir, \ |
---|
| 94 | suffix='',filter='f -name \'*.wav\'') |
---|
| 95 | |
---|
| 96 | def run_bench(self,modes=['dual'],thresholds=[0.5]): |
---|
| 97 | self.modes = modes |
---|
| 98 | self.thresholds = thresholds |
---|
| 99 | |
---|
| 100 | self.pretty_print(self.titles) |
---|
| 101 | for mode in self.modes: |
---|
| 102 | self.params.mode = mode |
---|
| 103 | for threshold in self.thresholds: |
---|
| 104 | self.params.threshold = threshold |
---|
| 105 | self.compute_data() |
---|
| 106 | self.compute_results() |
---|
| 107 | self.pretty_print(self.values) |
---|
[4cc9fe5] | 108 | |
---|
[75139a9] | 109 | def auto_learn(self,modes=['dual'],thresholds=[0.1,1.5]): |
---|
| 110 | """ simple dichotomia like algorithm to optimise threshold """ |
---|
| 111 | self.modes = modes |
---|
| 112 | self.pretty_print(self.titles) |
---|
| 113 | for mode in self.modes: |
---|
| 114 | steps = 10 |
---|
| 115 | lesst = thresholds[0] |
---|
| 116 | topt = thresholds[1] |
---|
| 117 | self.params.mode = mode |
---|
| 118 | |
---|
| 119 | self.params.threshold = topt |
---|
| 120 | self.compute_data() |
---|
| 121 | self.pretty_print(self.values) |
---|
| 122 | topF = self.F |
---|
| 123 | |
---|
| 124 | self.params.threshold = lesst |
---|
| 125 | self.compute_data() |
---|
| 126 | self.pretty_print(self.values) |
---|
| 127 | lessF = self.F |
---|
| 128 | |
---|
| 129 | for i in range(steps): |
---|
| 130 | self.params.threshold = ( lesst + topt ) * .5 |
---|
| 131 | self.compute_data() |
---|
| 132 | self.pretty_print(self.values) |
---|
| 133 | if self.F == 100.0 or self.F == topF: |
---|
| 134 | print "assuming we converged, stopping" |
---|
| 135 | break |
---|
| 136 | #elif abs(self.F - topF) < 0.01 : |
---|
| 137 | # print "done converging" |
---|
| 138 | # break |
---|
| 139 | if topF < self.F: |
---|
| 140 | #lessF = topF |
---|
| 141 | #lesst = topt |
---|
| 142 | topF = self.F |
---|
| 143 | topt = self.params.threshold |
---|
| 144 | elif lessF < self.F: |
---|
| 145 | lessF = self.F |
---|
| 146 | lesst = self.params.threshold |
---|
| 147 | if topt == lesst: |
---|
| 148 | lesst /= 2. |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | #modes = [ 'complex' ] |
---|
| 152 | modes = ['complex', 'energy', 'phase', 'specdiff', 'kl', 'mkl', 'dual'] |
---|
| 153 | #thresholds = [1.5] |
---|
| 154 | thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5] |
---|
| 155 | |
---|
| 156 | #datapath = "%s%s" % (DATADIR,'/onset/DB/*/') |
---|
| 157 | datapath = "%s%s" % (DATADIR,'/onset/DB/PercussivePhrases/RobertRich') |
---|
| 158 | respath = '/var/tmp/DB-testings' |
---|
[4cc9fe5] | 159 | |
---|
[75139a9] | 160 | taskonset = taskonset(datapath,respath) |
---|
[4cc9fe5] | 161 | |
---|
[75139a9] | 162 | taskonset.params = onset_parameters() |
---|
[4cc9fe5] | 163 | |
---|
[75139a9] | 164 | taskonset.titles = [ 'mode', 'thres', 'orig', 'expc', 'missd', 'mergd', |
---|
| 165 | 'bad', 'doubl', 'corrt', 'GD', 'FP', 'GD-merged', 'FP-pruned', |
---|
| 166 | 'prec', 'recl', 'dist' ] |
---|
| 167 | taskonset.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s", |
---|
| 168 | "| %6s", "| %6s", "| %6s", "| %8s", "| %8s", "| %8s", "| %8s", |
---|
| 169 | "| %6s", "| %6s", "| %6s"] |
---|
[4cc9fe5] | 170 | |
---|
[75139a9] | 171 | #taskonset.run_bench(modes=modes,thresholds=thresholds) |
---|
| 172 | taskonset.auto_learn(modes=modes) |
---|
[4cc9fe5] | 173 | |
---|
| 174 | # gatherdata |
---|
| 175 | #act_on_data(my_print,datapath,respath,suffix='.txt',filter='f -name \'*.wav\'') |
---|
| 176 | # gatherthreshold |
---|
| 177 | # gathermodes |
---|
| 178 | # comparediffs |
---|
| 179 | # gatherdiffs |
---|
| 180 | |
---|
| 181 | |
---|