1 | #! /usr/bin/python |
---|
2 | |
---|
3 | import sys |
---|
4 | import numarray |
---|
5 | import Gnuplot, Gnuplot.funcutils |
---|
6 | from aubio.aubioclass import * |
---|
7 | import aubio.gnuplot |
---|
8 | import aubio.txtfile |
---|
9 | from aubio.onsetcompare import onset_roc |
---|
10 | |
---|
11 | |
---|
12 | usage = "usage: %s [options] soundfile" % sys.argv[0] |
---|
13 | |
---|
14 | def parse_args(): |
---|
15 | from optparse import OptionParser |
---|
16 | parser = OptionParser(usage=usage) |
---|
17 | parser.add_option("-v","--verbose", |
---|
18 | action="store_true", dest="verbose", default=False, |
---|
19 | help="make lots of noise") |
---|
20 | parser.add_option("-q","--quiet", |
---|
21 | action="store_false", dest="verbose", default=True, |
---|
22 | help="be quiet [default]") |
---|
23 | parser.add_option("-t","--threshold", |
---|
24 | action="store", dest="threshold", default=0.3, |
---|
25 | help="onset detection threshold [default=0.3]") |
---|
26 | parser.add_option("-s","--silence", |
---|
27 | action="store", dest="silence", default=-70, |
---|
28 | help="silence [default=-70]") |
---|
29 | def check_mode(option, opt, value, parser): |
---|
30 | nvalue = parser.rargs[0] |
---|
31 | if nvalue == 'complexdomain' : setattr(parser.values, option.dest, complexdomain) |
---|
32 | elif nvalue == 'hfc' : setattr(parser.values, option.dest, hfc) |
---|
33 | elif nvalue == 'phase' : setattr(parser.values, option.dest, phase) |
---|
34 | elif nvalue == 'specdiff' : setattr(parser.values, option.dest, specdiff) |
---|
35 | elif nvalue == 'energy' : setattr(parser.values, option.dest, energy) |
---|
36 | elif nvalue == 'dual' : setattr(parser.values, option.dest, 'dual') |
---|
37 | parser.add_option("-m","--mode", |
---|
38 | action="callback", callback=check_mode, dest="mode", default='dual', |
---|
39 | help="onsetdetection mode [default=dual]") |
---|
40 | parser.add_option("-o","--outplot", |
---|
41 | action="store", dest="outplot", default=None, |
---|
42 | help="be quiet [default=None]") |
---|
43 | (options, args) = parser.parse_args() |
---|
44 | if not len(args): |
---|
45 | print "no file name given\n", usage |
---|
46 | sys.exit(1) |
---|
47 | return options, args |
---|
48 | |
---|
49 | options, args = parse_args() |
---|
50 | |
---|
51 | filename = args[0] #FIXME should move to optparse |
---|
52 | threshold = float(options.threshold) |
---|
53 | silence = float(options.silence) |
---|
54 | |
---|
55 | print options.mode |
---|
56 | onsets, ofunc = getonsetsfunc(filename,threshold,silence,mode=options.mode) |
---|
57 | |
---|
58 | g = Gnuplot.Gnuplot(debug=1, persist=1) |
---|
59 | |
---|
60 | if options.outplot: |
---|
61 | extension = options.outplot.split('.')[-1] |
---|
62 | if extension == 'ps': extension = 'postscript' |
---|
63 | g('set terminal %s' % extension) |
---|
64 | g('set output \'%s\'' % options.outplot) |
---|
65 | |
---|
66 | g('set multiplot') |
---|
67 | |
---|
68 | # onset detection function |
---|
69 | downtime = (512./44100.)*numarray.arange(len(ofunc)) |
---|
70 | d = Gnuplot.Data(downtime,ofunc,with='lines') |
---|
71 | |
---|
72 | # detected onsets |
---|
73 | x1 = (512./44100.)*numarray.array(onsets) |
---|
74 | y1 = max(ofunc)*numarray.ones(len(onsets)) |
---|
75 | e = Gnuplot.Data(x1,-y1,with='impulses') |
---|
76 | e2= Gnuplot.Data(x1,y1,with='impulses') |
---|
77 | |
---|
78 | # truth |
---|
79 | import os.path |
---|
80 | datafile = filename.replace('.wav','.txt') |
---|
81 | if not os.path.isfile(datafile): |
---|
82 | print "truth file not found" |
---|
83 | t = Gnuplot.Data(0,0,with='impulses') |
---|
84 | else: |
---|
85 | t_onsets = aubio.txtfile.read_datafile(datafile) |
---|
86 | y2 = max(ofunc)*numarray.ones(len(t_onsets)) |
---|
87 | x2 = numarray.array(t_onsets).resize(len(t_onsets)) |
---|
88 | t = Gnuplot.Data(x2,y2,with='impulses') |
---|
89 | |
---|
90 | eps = 4*0.012 |
---|
91 | |
---|
92 | orig, missed, merged, expc, bad, doubled = \ |
---|
93 | onset_roc(x2,x1,eps) |
---|
94 | print orig, missed, merged, expc, bad, doubled |
---|
95 | print "GD %2.8f\t" % (100*float(orig-missed-merged)/(orig)), |
---|
96 | print "FP %2.8f\t" % (100*float(bad+doubled)/(orig)) , |
---|
97 | print "GD-merged %2.8f\t" % (100*float(orig-missed)/(orig)) , |
---|
98 | print "FP-pruned %2.8f\t" % (100*float(bad)/(orig)) |
---|
99 | |
---|
100 | # audio data |
---|
101 | time,data = aubio.gnuplot.audio_to_array(filename) |
---|
102 | f = aubio.gnuplot.make_audio_plot(time,data) |
---|
103 | |
---|
104 | # hack to align left axis |
---|
105 | g('set lmargin 15') |
---|
106 | |
---|
107 | g('set size 1,0.3') |
---|
108 | g('set origin 0,0.7') |
---|
109 | g('set xrange [0:%f]' % max(time)) |
---|
110 | g('set yrange [-1:1]') |
---|
111 | g.ylabel('amplitude') |
---|
112 | g.plot(f,e,t) |
---|
113 | |
---|
114 | g('set size 1,0.7') |
---|
115 | g('set origin 0,0') |
---|
116 | g('set xrange [0:%f]' % (512./44100.*len(ofunc))) |
---|
117 | g('set yrange [0:%f]' % (max(ofunc)*1.01)) |
---|
118 | g.xlabel('time') |
---|
119 | g.ylabel('onset detection value') |
---|
120 | |
---|
121 | g.plot(d,e2) |
---|
122 | |
---|
123 | g('unset multiplot') |
---|
124 | |
---|