1 | #! /usr/bin/python |
---|
2 | |
---|
3 | from aubio.bench.node import * |
---|
4 | from aubio.task import * |
---|
5 | |
---|
6 | class benchpitch(bench): |
---|
7 | |
---|
8 | """ list of values to store per file """ |
---|
9 | valuenames = ['mode'] |
---|
10 | """ list of lists to store per file """ |
---|
11 | valuelists = ['truth', 'osil', 'esil', 'opit', 'epit', 'echr', |
---|
12 | 'Msil', 'Mpit', 'Mchr', |
---|
13 | 'TotalPit', 'TotalPit', 'TotalChr' ] |
---|
14 | """ list of values to print per dir """ |
---|
15 | printnames = [ 'mode', 'MinPit', 'MaxPit', 'TotalSil', 'TotalPit', 'TotalChr'] |
---|
16 | |
---|
17 | """ per dir """ |
---|
18 | formats = {'mode': "%12s" , |
---|
19 | 'truth': "%s", |
---|
20 | 'osil': "%s", 'esil': "%s", |
---|
21 | 'opit': "%s", 'epit': "%s", 'echr': "%s", |
---|
22 | 'TotalPit': "%s", 'TotalSil': "%s", 'TotalChr': "%s", |
---|
23 | 'MinPit': "%s", 'MaxPit': "%s", |
---|
24 | 'Msil': "%s", 'Mpit': "%s", 'Mchr': "%s"} |
---|
25 | |
---|
26 | def dir_eval(self): |
---|
27 | """ evaluate statistical data over the directory """ |
---|
28 | v = self.v |
---|
29 | v['mode'] = self.params.pitchmode |
---|
30 | |
---|
31 | def file_exec(self,input,output): |
---|
32 | filetask = self.task(input,params=self.params) |
---|
33 | computed_data = filetask.compute_all() |
---|
34 | osil, esil, opit, epit, echr = filetask.eval(computed_data,tol=0.5) |
---|
35 | self.v['truth'].append(int(filetask.truth)) |
---|
36 | assert opit > 0 |
---|
37 | |
---|
38 | self.v['osil'].append(osil) |
---|
39 | self.v['esil'].append(esil) |
---|
40 | self.v['opit'].append(opit) |
---|
41 | self.v['epit'].append(epit) |
---|
42 | self.v['echr'].append(echr) |
---|
43 | |
---|
44 | self.v['Msil'].append(esil/float(osil)*100.) |
---|
45 | self.v['Mpit'].append(epit/float(opit)*100.) |
---|
46 | self.v['Mchr'].append(echr/float(opit)*100.) |
---|
47 | #print results#, computed_data |
---|
48 | #print input, results, results - float(input.split('.')[-2]) |
---|
49 | |
---|
50 | def run_bench(self,modes=['schmitt'],multiplot=0): |
---|
51 | from os.path import basename |
---|
52 | self.modes = modes |
---|
53 | self.pretty_titles() |
---|
54 | d = [] |
---|
55 | for mode in self.modes: |
---|
56 | self.params.pitchmode = mode |
---|
57 | self.dir_exec() |
---|
58 | self.dir_eval() |
---|
59 | truth = [i for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
60 | allOsil = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
61 | allEsil = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
62 | allOpit = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
63 | allEpit = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
64 | allEchr = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
65 | allMsil = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
66 | allMpit = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
67 | allMchr = [0 for i in range(min(self.v['truth']),max(self.v['truth'])+1)] |
---|
68 | for i in range(len(self.v['truth'])): |
---|
69 | allOsil[self.v['truth'][i]-min(self.v['truth'])] += self.v['osil'][i] |
---|
70 | allEsil[self.v['truth'][i]-min(self.v['truth'])] += self.v['esil'][i] |
---|
71 | allOpit[self.v['truth'][i]-min(self.v['truth'])] += self.v['opit'][i] |
---|
72 | allEpit[self.v['truth'][i]-min(self.v['truth'])] += self.v['epit'][i] |
---|
73 | allEchr[self.v['truth'][i]-min(self.v['truth'])] += self.v['echr'][i] |
---|
74 | for i in range(len(truth)): |
---|
75 | allOsil[i] = max(1,allOsil[i]) |
---|
76 | allOpit[i] = max(1,allOpit[i]) |
---|
77 | allMsil[i] = allEsil[i]/float(allOsil[i])*100. |
---|
78 | allMpit[i] = allEpit[i]/float(allOpit[i])*100. |
---|
79 | allMchr[i] = allEchr[i]/float(allOpit[i])*100. |
---|
80 | self.v['TotalSil'] = sum(allMsil)/len(truth) |
---|
81 | self.v['TotalPit'] = sum(allMpit)/len(truth) |
---|
82 | self.v['TotalChr'] = sum(allMchr)/len(truth) |
---|
83 | self.v['MinPit'] = min(truth) |
---|
84 | self.v['MaxPit'] = max(truth) |
---|
85 | self.pretty_print() |
---|
86 | |
---|
87 | plot = [] |
---|
88 | self.plotpitchtessiture(plot, |
---|
89 | truth, |
---|
90 | allMpit, |
---|
91 | plottitle="%s %s" % (self.v['mode'],self.params.bufsize), |
---|
92 | plotmode='lines') |
---|
93 | """ |
---|
94 | self.plotpitchtessiture(plot, |
---|
95 | truth, |
---|
96 | allMchr, |
---|
97 | plottitle="%s %s" % (self.v['mode'],"%12"), |
---|
98 | plotmode='lines') |
---|
99 | self.plotpitchtessiture(plot, |
---|
100 | truth, |
---|
101 | allMsil, |
---|
102 | plottitle="%s %s" % (self.v['mode'],"sil"), |
---|
103 | plotmode='lines') |
---|
104 | """ |
---|
105 | title = basename(self.datadir) |
---|
106 | if multiplot: |
---|
107 | d.append(plot) |
---|
108 | else: |
---|
109 | d += plot |
---|
110 | outplot = "_-_".join(('pitchtessiture',title)) |
---|
111 | self.xmin = min(self.v['truth']) #20. |
---|
112 | self.xmax = max(self.v['truth']) |
---|
113 | for ext in ('ps','png','svg'): #,''): |
---|
114 | self.plotplotpitchtessiture(d, |
---|
115 | plottitle="".join(['Performance against MIDI Note number (', |
---|
116 | title, |
---|
117 | ", %s" % len(self.sndlist), " samples)"]), |
---|
118 | outplot=outplot, |
---|
119 | extension=ext,multiplot=multiplot) |
---|
120 | #d.append('beta = .25,orig(x) title \"-2 octave\"') |
---|
121 | #d.append('beta = .50,orig(x) title \"-1 octave\"') |
---|
122 | #d.append('beta = 1.0,orig(x) title \"original\"') |
---|
123 | #d.append('beta = 2.0,orig(x) title \"+1 octave\"') |
---|
124 | |
---|
125 | """ |
---|
126 | Plot functions |
---|
127 | """ |
---|
128 | |
---|
129 | def plotpitchtessiture(self,d,lx,ly,plottitle="",plotmode='linespoints'): |
---|
130 | import Gnuplot, Gnuplot.funcutils |
---|
131 | d.append(Gnuplot.Data(lx, ly, with=plotmode, title="%s" % (plottitle) )) |
---|
132 | |
---|
133 | def plotplotpitchtessiture(self,d,plottitle='',outplot=0,extension='',multiplot=1): |
---|
134 | from aubio.gnuplot import gnuplot_create |
---|
135 | g = gnuplot_create(outplot=outplot,extension=extension) |
---|
136 | #g.title(plottitle) |
---|
137 | #g('orig(x) = beta*x') |
---|
138 | g.title(plottitle) |
---|
139 | g('set yrange [50:100]') |
---|
140 | # erase axis |
---|
141 | g('set xrange [%f:%f]' % (self.xmin,self.xmax)) #(self.xmax - (self.xmax-self.xmin)*5./4.,self.xmax)) |
---|
142 | #g.plot(*d) |
---|
143 | g('set border 3') |
---|
144 | g('set xtics nomirror') |
---|
145 | g('set ytics nomirror') |
---|
146 | g('set key bottom') |
---|
147 | if multiplot: |
---|
148 | g('set multiplot') |
---|
149 | for i in range(len(d)): |
---|
150 | # plot onset detection functions |
---|
151 | g('set size 1,%f' % ( 1.0/float(len(d)) ) ) |
---|
152 | g('set origin 0,%f' % ( 1.0*float(len(d)-i-1)/float(len(d)) ) ) |
---|
153 | #g.ylabel('%Correct detections') |
---|
154 | g('set xrange [%f:%f]' % (self.xmin,self.xmax)) #(self.xmax - (self.xmax-self.xmin)*5./4.,self.xmax)) |
---|
155 | g.plot(*d[i]) |
---|
156 | g('unset title') |
---|
157 | g('unset multiplot') |
---|
158 | else: |
---|
159 | g.plot(*d) |
---|
160 | |
---|
161 | |
---|
162 | if __name__ == "__main__": |
---|
163 | import sys |
---|
164 | if len(sys.argv) > 1: datapath = sys.argv[1] |
---|
165 | else: print "error: a path is required"; sys.exit(1) |
---|
166 | if len(sys.argv) > 2: |
---|
167 | for each in sys.argv[3:-1]: print each |
---|
168 | modes = ['schmitt', 'yin', 'yinfft', 'mcomb', 'fcomb'] |
---|
169 | #modes = ['mcomb'] |
---|
170 | |
---|
171 | params = taskparams() |
---|
172 | params.bufsize = 2048 # 4096 |
---|
173 | params.hopsize = 256 |
---|
174 | params.silence = -60. |
---|
175 | params.pitchsmooth = 0 |
---|
176 | params.pitchmax = 20000 |
---|
177 | params.pitchmin = 20 |
---|
178 | params.pitchyinfft = 0.95 |
---|
179 | benchpitch = benchpitch(datapath,params=params) |
---|
180 | benchpitch.task = taskpitch |
---|
181 | |
---|
182 | #benchpitch.titles = [ 'mode', 'thres', 'avg', 'avgdist' ] |
---|
183 | #benchpitch.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s" ] |
---|
184 | try: |
---|
185 | benchpitch.run_bench(modes=modes) |
---|
186 | except KeyboardInterrupt: |
---|
187 | print "Interrupted by user" |
---|
188 | sys.exit(1) |
---|
189 | |
---|
190 | sys.exit(0) |
---|