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']): |
---|
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 | self.plotpitchtessiture(plot, |
---|
94 | truth, |
---|
95 | allMchr, |
---|
96 | plottitle="%s %s" % (self.v['mode'],"%12"), |
---|
97 | plotmode='lines') |
---|
98 | """ |
---|
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 | d.append(plot) |
---|
107 | outplot = "_-_".join(('pitchtessiture',title)) |
---|
108 | self.xmin = 20. #min(self.v['truth']) |
---|
109 | self.xmax = 110. #max(self.v['truth']) |
---|
110 | for ext in ('ps','png','svg',''): |
---|
111 | self.plotplotpitchtessiture(d, |
---|
112 | plottitle="".join(['Performance against MIDI Note number (', |
---|
113 | title, |
---|
114 | ", %s" % len(self.sndlist), " samples)"]), |
---|
115 | outplot=outplot, |
---|
116 | extension=ext) |
---|
117 | #d.append('beta = .25,orig(x) title \"-2 octave\"') |
---|
118 | #d.append('beta = .50,orig(x) title \"-1 octave\"') |
---|
119 | #d.append('beta = 1.0,orig(x) title \"original\"') |
---|
120 | #d.append('beta = 2.0,orig(x) title \"+1 octave\"') |
---|
121 | |
---|
122 | """ |
---|
123 | Plot functions |
---|
124 | """ |
---|
125 | |
---|
126 | def plotpitchtessiture(self,d,lx,ly,plottitle="",plotmode='linespoints'): |
---|
127 | import Gnuplot, Gnuplot.funcutils |
---|
128 | d.append(Gnuplot.Data(lx, ly, with=plotmode, title="%s" % (plottitle) )) |
---|
129 | |
---|
130 | def plotplotpitchtessiture(self,d,plottitle='',outplot=0,extension='',multiplot=1): |
---|
131 | from aubio.gnuplot import gnuplot_create |
---|
132 | g = gnuplot_create(outplot=outplot,extension=extension) |
---|
133 | #g.title(plottitle) |
---|
134 | #g('orig(x) = beta*x') |
---|
135 | #g.xlabel('original pitch (Hz)') |
---|
136 | #g.ylabel('detected pitch (Hz)') |
---|
137 | #g('set key left top') |
---|
138 | #g('set log xy') |
---|
139 | #g('set xrange [50:2000]') |
---|
140 | g('set yrange [0:100]') |
---|
141 | #g.plot(*d) |
---|
142 | if multiplot: |
---|
143 | g('set multiplot') |
---|
144 | for i in range(len(d)): |
---|
145 | # plot onset detection functions |
---|
146 | g('set size 1,%f' % ( 1.0/float(len(d)) ) ) |
---|
147 | g('set origin 0,%f' % ( 1.0*float(len(d)-i-1)/float(len(d)) ) ) |
---|
148 | # erase axis |
---|
149 | g('set border 3') |
---|
150 | g('set xtics nomirror') |
---|
151 | g('set ytics nomirror') |
---|
152 | g('set key left top') |
---|
153 | g('set xrange [%f:%f]' % (self.xmin,self.xmax)) #(self.xmax - (self.xmax-self.xmin)*5./4.,self.xmax)) |
---|
154 | #g.ylabel('%Correct detections') |
---|
155 | if i == len(d)-1: |
---|
156 | g.xlabel(plottitle) |
---|
157 | g.plot(*d[i]) |
---|
158 | g('unset title') |
---|
159 | g('unset multiplot') |
---|
160 | else: |
---|
161 | g.plot(*d) |
---|
162 | |
---|
163 | |
---|
164 | if __name__ == "__main__": |
---|
165 | import sys |
---|
166 | if len(sys.argv) > 1: datapath = sys.argv[1] |
---|
167 | else: print "error: a path is required"; sys.exit(1) |
---|
168 | if len(sys.argv) > 2: |
---|
169 | for each in sys.argv[3:-1]: print each |
---|
170 | modes = ['schmitt', 'yin', 'yinfft', 'mcomb', 'fcomb'] |
---|
171 | |
---|
172 | params = taskparams() |
---|
173 | params.bufsize = 2048 #4096 |
---|
174 | params.hopsize = 256 |
---|
175 | params.silence = -70. |
---|
176 | params.pitchsmooth = 0 |
---|
177 | params.pitchmax = 20000 |
---|
178 | params.pitchmin = 20 |
---|
179 | params.pitchyinfft = 0.95 |
---|
180 | benchpitch = benchpitch(datapath,params=params) |
---|
181 | benchpitch.task = taskpitch |
---|
182 | |
---|
183 | #benchpitch.titles = [ 'mode', 'thres', 'avg', 'avgdist' ] |
---|
184 | #benchpitch.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s" ] |
---|
185 | try: |
---|
186 | benchpitch.run_bench(modes=modes) |
---|
187 | except KeyboardInterrupt: |
---|
188 | print "Interrupted by user" |
---|
189 | sys.exit(1) |
---|
190 | |
---|
191 | sys.exit(0) |
---|